Guest User

Untitled

a guest
Feb 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include "llvm/Pass.h"
  2. #include "llvm/IR/Function.h"
  3. #include "llvm/IR/BasicBlock.h"
  4. #include "llvm/IR/Instructions.h"
  5. #include "llvm/IR/IRBuilder.h"
  6. #include "llvm/Support/raw_ostream.h"
  7. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  8.  
  9. using namespace llvm;
  10.  
  11. namespace {
  12.     struct Hello : public FunctionPass {
  13.         static char ID;
  14.         Hello() : FunctionPass(ID) {}
  15.  
  16.         bool runOnFunction(Function &F) override;
  17.     };
  18. }
  19.  
  20. bool Hello::runOnFunction(Function &F) {
  21.     bool changed = false;
  22.  
  23.     for (auto BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
  24.         for (auto II = BI->begin(), IE = BI->end(); II != IE; ++II) {
  25.             BinaryOperator *BO = dyn_cast<BinaryOperator>(&*II);
  26.  
  27.             if (!BO)
  28.                 continue;
  29.  
  30.             unsigned opcode = BO->getOpcode();
  31.             errs() << BO->getOpcodeName() << "\n";
  32.  
  33.             if (opcode != Instruction::Add) {
  34.                 continue;
  35.             }
  36.  
  37.             changed = true;
  38.  
  39.             IRBuilder<> builder(BO);
  40.             Value* V = builder.CreateAdd(builder.CreateXor(BO->getOperand(0), BO->getOperand(1)),
  41.                        builder.CreateMul(ConstantInt::get(BO->getType(), 2),
  42.                        builder.CreateAnd(BO->getOperand(0), BO->getOperand(1))));
  43.  
  44.             ReplaceInstWithValue(BI->getInstList(), II, V);
  45.         }
  46.     }
  47.  
  48.     return changed;
  49. }
  50.  
  51. char Hello::ID = 0;
  52. static RegisterPass<Hello> X("hello", "Count opcodes per functions", false, false);
Add Comment
Please, Sign In to add comment