Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | None | 0 0
  1. //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
  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 two versions of the LLVM "Hello World" pass described
  11. // in docs/WritingAnLLVMPass.html
  12. //
  13. //===----------------------------------------------------------------------===//
  14.  
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/Transforms/Scalar.h"
  20. #include "llvm/IR/InstIterator.h"
  21. #include "llvm/IR/Instruction.h"
  22. #include "llvm/Target/TargetLibraryInfo.h"
  23. #include "llvm/Transforms/Utils/Local.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/IR/Instructions.h"
  26. #include "llvm/Analysis/ConstantFolding.h"
  27. #include "llvm/ADT/BitVector.h"
  28. #include "llvm/IR/ValueMap.h"
  29. #include "llvm/IR/BasicBlock.h"
  30. #include "llvm/IR/CFG.h"
  31. #include "llvm/ADT/SmallVector.h"
  32. #include <vector>
  33. #include <set>
  34. using namespace llvm;
  35.  
  36. #define DEBUG_TYPE "hello"
  37.  
  38. STATISTIC(HelloCounter, "Counts number of functions greeted");
  39.  
  40. namespace {
  41.   // Hello2 - The second implementation with getAnalysisUsage implemented.
  42.   struct Hello2 : public FunctionPass {
  43.     static char ID; // Pass identification, replacement for typeid
  44.     Hello2() : FunctionPass(ID) {}
  45.  
  46.     bool runOnFunction(Function &F) override {
  47.       ++HelloCounter;
  48.       errs() << "Hello: ";
  49.       errs().write_escaped(F.getName()) << '\n';
  50.       return false;
  51.     }
  52.  
  53.     // We don't modify the program, so we preserve all analyses.
  54.     void getAnalysisUsage(AnalysisUsage &AU) const override {
  55.       AU.setPreservesAll();
  56.     }
  57.   };
  58. }
  59.  
  60. char Hello2::ID = 0;
  61. static RegisterPass<Hello2>
  62. Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
  63.  
  64.  
  65. namespace {
  66.   // PrintBasicBlock
  67.   struct PrintBasicBlock : public FunctionPass {
  68.     static char ID;
  69.     PrintBasicBlock(): FunctionPass(ID) {}
  70.  
  71.     bool runOnFunction(Function &F) override {
  72.       errs() << "Function " << F.getName() << " basic blocks:\n";
  73.  
  74.       for (BasicBlock &BB: F) {
  75.         printBasicBlock(BB);
  76.       }
  77.  
  78.       return false;
  79.     }
  80.  
  81.     void printBasicBlock(BasicBlock &BB) {
  82.       for (Instruction &I: BB) {
  83.         errs() << I << "\n";
  84.       }
  85.       errs() << "\n\n";
  86.     }
  87.   };
  88. }
  89.  
  90. char PrintBasicBlock::ID = 0;
  91. static RegisterPass<PrintBasicBlock>
  92. Z("printbasicblock", "Prints Basic Blocks");
  93.  
  94.  
  95. namespace {
  96.   //===--------------------------------------------------------------------===//
  97.   // DeadCodeElimination pass implementation
  98.   //
  99.   struct DCE : public FunctionPass {
  100.     static char ID; // Pass identification, replacement for typeid
  101.     DCE() : FunctionPass(ID) {
  102.       initializeDCEPass(*PassRegistry::getPassRegistry());
  103.     }
  104.  
  105.     bool runOnFunction(Function &F) override;
  106.  
  107.     void getAnalysisUsage(AnalysisUsage &AU) const override {
  108.       AU.setPreservesCFG();
  109.     }
  110.  };
  111. }
  112.  
  113. bool DCE::runOnFunction(Function &F) {
  114.   if (skipOptnoneFunction(F))
  115.     return false;
  116.  
  117.   TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
  118.  
  119.   // Start out with all of the instructions in the worklist...
  120.   std::vector<Instruction*> WorkList;
  121.   for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
  122.     WorkList.push_back(&*i);
  123.  
  124.   // Loop over the worklist finding instructions that are dead.  If they are
  125.   // dead make them drop all of their uses, making other instructions
  126.   // potentially dead, and work until the worklist is empty.
  127.   //
  128.   bool MadeChange = false;
  129.   while (!WorkList.empty()) {
  130.     Instruction *I = WorkList.back();
  131.     WorkList.pop_back();
  132.  
  133.     errs() << "Current instruction: " << *I << "\n";
  134.     errs() << "Instructions that use this one: \n";
  135.     for (User *U: I->users())
  136.       if (Instruction *Inst = dyn_cast<Instruction>(U)) {
  137.         errs() << *Inst << "\n";
  138.     }
  139.     errs() << "\n\n";
  140.  
  141.     if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
  142.       // Loop over all of the values that the instruction uses, if there are
  143.       // instructions being used, add them to the worklist, because they might
  144.       // go dead after this one is removed.
  145.       //
  146.  
  147.       for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) {
  148.         if (Instruction *Used = dyn_cast<Instruction>(*OI)) {
  149.           WorkList.push_back(Used);
  150.         }
  151.       }
  152.  
  153.       // Remove the instruction.
  154.       I->eraseFromParent();
  155.  
  156.       // Remove the instruction from the worklist if it still exists in it.
  157.       WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
  158.                      WorkList.end());
  159.  
  160.       MadeChange = true;
  161.     }
  162.   }
  163.   return MadeChange;
  164. }
  165.  
  166. char DCE::ID = 0;
  167. static RegisterPass<DCE> XX("mydce", "My DCE Pass");
  168.  
  169. FunctionPass *llvm::createDeadCodeEliminationPass() {
  170.   return new DCE();
  171. }
  172.  
  173.  
  174. namespace {
  175.   //===--------------------------------------------------------------------===//
  176.   // DeadCodeElimination pass implementation
  177.   //
  178.   struct RemoveDeadBlocks : public FunctionPass {
  179.     static char ID; // Pass identification, replacement for typeid
  180.     RemoveDeadBlocks() : FunctionPass(ID) {}
  181.  
  182.     virtual bool runOnFunction(Function &F) override {
  183.       bool first_block = true;
  184.  
  185.       std::vector<BasicBlock*> toRemove;
  186.  
  187.       for (BasicBlock &BB: F) {
  188.         if (!first_block && pred_empty(&BB)) {
  189.           toRemove.push_back(&BB);
  190.         }
  191.         first_block = false;
  192.       }
  193.  
  194.       for (BasicBlock *BB: toRemove)
  195.         BB->removeFromParent();
  196.  
  197.       return true;
  198.     }
  199.  
  200.     void getAnalysisUsage(AnalysisUsage &AU) const override {
  201.       AU.setPreservesCFG();
  202.     }
  203.  };
  204. }
  205.  
  206. char RemoveDeadBlocks::ID = 0;
  207. static RegisterPass<RemoveDeadBlocks>
  208. RMDB("removedeadblocks", "Removes Dead Basic Blocks");
  209.  
  210.  
  211. namespace {
  212.   //===--------------------------------------------------------------------===//
  213.   // DeadCodeElimination pass implementation
  214.   //
  215.   struct JumpToJump : public FunctionPass {
  216.     static char ID; // Pass identification, replacement for typeid
  217.     JumpToJump() : FunctionPass(ID) {}
  218.  
  219.     virtual bool runOnFunction(Function &F) override {
  220.       for (BasicBlock &BB: F) {
  221.         BranchInst *crt_bi;
  222.         if (!BB.empty() && (crt_bi = dyn_cast<BranchInst>(&(*BB.begin())))) {
  223.           if (crt_bi->getNumSuccessors() != 1)
  224.             continue;
  225.           BasicBlock *nextBB = crt_bi->getSuccessor(0);
  226.  
  227.           for (auto it = pred_begin(&BB); it != pred_end(&BB); ++it) {
  228.             BasicBlock *predBB = *it;
  229.             for (Instruction &I: *predBB) {
  230.               Instruction *crtI = &I;
  231.               if (BranchInst *bi = dyn_cast<BranchInst>(crtI)) {
  232.                 unsigned int n =  bi->getNumSuccessors();
  233.  
  234.                 for (unsigned int i = 0; i < n; ++i) {
  235.                   BasicBlock *succBB = bi->getSuccessor(i);
  236.                   if (succBB == &BB) {
  237.                     bi->setSuccessor(i, nextBB);
  238.                   }
  239.                 }
  240.               }
  241.             }
  242.           }
  243.         }
  244.       }
  245.  
  246.       return true;
  247.     }
  248.  
  249.     void getAnalysisUsage(AnalysisUsage &AU) const override {
  250.       AU.setPreservesCFG();
  251.     }
  252.  };
  253. }
  254.  
  255. char JumpToJump::ID = 0;
  256. static RegisterPass<JumpToJump>
  257. JTJ("jumptojump", "Optimize jump to jump");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement