Advertisement
Guest User

Untitled

a guest
May 7th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //===- Cpl.cpp - LLVM Pass for the CPL analysis homework ------------------===//
  2. //
  3. //                     The LLVM Compiler Infrastructure
  4. //
  5. // This FILE is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This FILE implements the entry point of the Cpl pass used for the CPL hw
  11. //
  12. //===----------------------------------------------------------------------===//
  13.  
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/IR/InstIterator.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/IR/Constants.h"
  22.  
  23.  
  24. #include <iostream>  
  25. #include <fstream>
  26. #include <sstream>
  27.  
  28. using namespace llvm;
  29.  
  30. #define DEBUG_TYPE "cpl"
  31.  
  32. // statistic that counts the score
  33. STATISTIC(stats, " # cpl - Static score of all instructions");
  34.  
  35. // score FILE parameter
  36. static cl::opt<std::string> scoreFileName("cpl-score-file", cl::desc("Specify input filename for mypass"));
  37.  
  38. namespace {
  39.  
  40.   static std::vector<Function*> constructors;
  41.   static std::vector<Function*> destructors;
  42.  
  43.   std::vector<Function *> parseGlobalCtorsAndDtors(GlobalVariable *GV) {
  44.     if (GV->getInitializer()->isNullValue())
  45.       return std::vector<Function *>();
  46.     ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
  47.     std::vector<Function *> Result;
  48.     Result.reserve(CA->getNumOperands());
  49.     for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
  50.       ConstantStruct *CS = cast<ConstantStruct>(*i);
  51.       Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
  52.     }
  53.     return Result;
  54.   }
  55.  
  56.   GlobalVariable *findGlobalCtorsAndDtors(Module &M, std::string what) {
  57.     GlobalVariable *GV = M.getGlobalVariable(what);
  58.     if (!GV)
  59.       return nullptr;
  60.    
  61.     // Verify that the initializer is simple enough for us to handle. We are
  62.     // only allowed to optimize the initializer if it is unique.
  63.     if (!GV->hasUniqueInitializer())
  64.       return nullptr;
  65.  
  66.     if (isa<ConstantAggregateZero>(GV->getInitializer()))
  67.       return GV;
  68.     ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
  69.  
  70.     for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
  71.       if (isa<ConstantAggregateZero>(*i))
  72.         continue;
  73.       ConstantStruct *CS = cast<ConstantStruct>(*i);
  74.       if (isa<ConstantPointerNull>(CS->getOperand(1)))
  75.         continue;
  76.  
  77.       // Must have a function or null ptr.
  78.       if (!isa<Function>(CS->getOperand(1)))
  79.         return nullptr;
  80.  
  81.       // Init priority must be standard.
  82.       ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
  83.       if (CI->getZExtValue() != 65535)
  84.         return nullptr;
  85.     }
  86.  
  87.     return GV;
  88.   }
  89.  
  90.  
  91.   struct CplStatic : public FunctionPass {
  92.     static char ID;
  93.  
  94.     CplStatic() : FunctionPass(ID) {}
  95.  
  96.     bool doInitialization(Module& m) {
  97.       GlobalVariable *global_ctors = findGlobalCtorsAndDtors(m, "llvm.global_ctors");
  98.       GlobalVariable *global_dtors = findGlobalCtorsAndDtors(m, "llvm.global_dtors");
  99.  
  100.       if(global_ctors != nullptr)
  101.         constructors = parseGlobalCtorsAndDtors(global_ctors);
  102.      
  103.       if(global_dtors != nullptr)
  104.         constructors = parseGlobalCtorsAndDtors(global_dtors);
  105.  
  106.       return false;
  107.     }
  108.  
  109.     bool runOnFunction(Function &F) override {
  110.       std::string line;
  111.       std::ifstream in(scoreFileName);
  112.       std::ofstream out("out.out");
  113.       std::map<std::string, int> instructionCost;
  114.  
  115.       if(F.isIntrinsic())
  116.         return false;
  117.  
  118.       for(auto ctor_F : constructors)
  119.         if(F.getName() == ctor_F->getName())
  120.           return false;
  121.  
  122.       for(auto dtor_F : destructors)
  123.         if(F.getName() == dtor_F->getName())
  124.           return false;
  125.  
  126.       instructionCost["default"] = 0; //in caz ca nu e in fisierul de referinta.
  127.  
  128.       while (std::getline(in, line)) {
  129.         std::istringstream iss(line);
  130.         int value;
  131.         std::string instructionName;
  132.  
  133.         iss>>instructionName>>value;
  134.         instructionCost[instructionName] = value;
  135.       }
  136.  
  137.       auto it = inst_begin(F);
  138.       while(it != inst_end(F)) {
  139.         Instruction* instr = &(*it);
  140.         std::string opCodeName = std::string(instr->getOpcodeName());
  141.  
  142.         if(instructionCost.find(opCodeName) != instructionCost.end()) {
  143.           stats += instructionCost[opCodeName];
  144.         }
  145.         else {
  146.           stats += instructionCost["default"];
  147.         }
  148.  
  149.         ++it;
  150.       }
  151.  
  152.       in.close();
  153.       out.close();
  154.  
  155.       return false;
  156.     }
  157.   };
  158. }
  159.  
  160. char CplStatic::ID = 0;
  161. static RegisterPass<CplStatic> X("cpl-static", "CPL Static Analysis Pass");
  162.  
  163. namespace {
  164.  
  165.   struct CplDynamic : public ModulePass {
  166.     static char ID;
  167.  
  168.     CplDynamic() : ModulePass(ID) {}
  169.  
  170.     bool runOnModule(Module &M) override {
  171.       // TODO: Cpl Dynamic Score Analysis
  172.  
  173.       return false;
  174.     }
  175.  
  176.   };
  177. }
  178.  
  179. char CplDynamic::ID = 0;
  180. static RegisterPass<CplDynamic> Y("cpl-dynamic", "CPL Dynamic Analysis Pass");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement