Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //===- Cpl.cpp - LLVM Pass for the CPL analysis homework ------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This FILE is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This FILE implements the entry point of the Cpl pass used for the CPL hw
- //
- //===----------------------------------------------------------------------===//
- #include "llvm/ADT/Statistic.h"
- #include "llvm/IR/Function.h"
- #include "llvm/IR/Module.h"
- #include "llvm/Pass.h"
- #include "llvm/Support/raw_ostream.h"
- #include "llvm/IR/InstIterator.h"
- #include "llvm/Support/CommandLine.h"
- #include "llvm/IR/Constants.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- using namespace llvm;
- #define DEBUG_TYPE "cpl"
- // statistic that counts the score
- STATISTIC(stats, " # cpl - Static score of all instructions");
- // score FILE parameter
- static cl::opt<std::string> scoreFileName("cpl-score-file", cl::desc("Specify input filename for mypass"));
- namespace {
- static std::vector<Function*> constructors;
- static std::vector<Function*> destructors;
- std::vector<Function *> parseGlobalCtorsAndDtors(GlobalVariable *GV) {
- if (GV->getInitializer()->isNullValue())
- return std::vector<Function *>();
- ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
- std::vector<Function *> Result;
- Result.reserve(CA->getNumOperands());
- for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
- ConstantStruct *CS = cast<ConstantStruct>(*i);
- Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
- }
- return Result;
- }
- GlobalVariable *findGlobalCtorsAndDtors(Module &M, std::string what) {
- GlobalVariable *GV = M.getGlobalVariable(what);
- if (!GV)
- return nullptr;
- // Verify that the initializer is simple enough for us to handle. We are
- // only allowed to optimize the initializer if it is unique.
- if (!GV->hasUniqueInitializer())
- return nullptr;
- if (isa<ConstantAggregateZero>(GV->getInitializer()))
- return GV;
- ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
- for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
- if (isa<ConstantAggregateZero>(*i))
- continue;
- ConstantStruct *CS = cast<ConstantStruct>(*i);
- if (isa<ConstantPointerNull>(CS->getOperand(1)))
- continue;
- // Must have a function or null ptr.
- if (!isa<Function>(CS->getOperand(1)))
- return nullptr;
- // Init priority must be standard.
- ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
- if (CI->getZExtValue() != 65535)
- return nullptr;
- }
- return GV;
- }
- struct CplStatic : public FunctionPass {
- static char ID;
- CplStatic() : FunctionPass(ID) {}
- bool doInitialization(Module& m) {
- GlobalVariable *global_ctors = findGlobalCtorsAndDtors(m, "llvm.global_ctors");
- GlobalVariable *global_dtors = findGlobalCtorsAndDtors(m, "llvm.global_dtors");
- if(global_ctors != nullptr)
- constructors = parseGlobalCtorsAndDtors(global_ctors);
- if(global_dtors != nullptr)
- constructors = parseGlobalCtorsAndDtors(global_dtors);
- return false;
- }
- bool runOnFunction(Function &F) override {
- std::string line;
- std::ifstream in(scoreFileName);
- std::ofstream out("out.out");
- std::map<std::string, int> instructionCost;
- if(F.isIntrinsic())
- return false;
- for(auto ctor_F : constructors)
- if(F.getName() == ctor_F->getName())
- return false;
- for(auto dtor_F : destructors)
- if(F.getName() == dtor_F->getName())
- return false;
- instructionCost["default"] = 0; //in caz ca nu e in fisierul de referinta.
- while (std::getline(in, line)) {
- std::istringstream iss(line);
- int value;
- std::string instructionName;
- iss>>instructionName>>value;
- instructionCost[instructionName] = value;
- }
- auto it = inst_begin(F);
- while(it != inst_end(F)) {
- Instruction* instr = &(*it);
- std::string opCodeName = std::string(instr->getOpcodeName());
- if(instructionCost.find(opCodeName) != instructionCost.end()) {
- stats += instructionCost[opCodeName];
- }
- else {
- stats += instructionCost["default"];
- }
- ++it;
- }
- in.close();
- out.close();
- return false;
- }
- };
- }
- char CplStatic::ID = 0;
- static RegisterPass<CplStatic> X("cpl-static", "CPL Static Analysis Pass");
- namespace {
- struct CplDynamic : public ModulePass {
- static char ID;
- CplDynamic() : ModulePass(ID) {}
- bool runOnModule(Module &M) override {
- // TODO: Cpl Dynamic Score Analysis
- return false;
- }
- };
- }
- char CplDynamic::ID = 0;
- static RegisterPass<CplDynamic> Y("cpl-dynamic", "CPL Dynamic Analysis Pass");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement