montimaj

InsertInst.cpp

Jun 28th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. /**
  2.  * Intrinsic testing.
  3.  * @author Sayantan Majumdar
  4. */
  5.  
  6. #include "llvm/IR/InstrTypes.h"
  7. #include "llvm/IR/Instructions.h"
  8. #include "llvm/IR/IRBuilder.h"
  9. #include "llvm/IR/Intrinsics.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/Pass.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/Support/raw_ostream.h"
  14.  
  15. #include<vector>
  16.  
  17. using namespace llvm;
  18.  
  19. #define DEBUG_TYPE "insertinst"
  20.  
  21. namespace {
  22.   struct InsertInstruction : public FunctionPass {
  23.     static char ID; // Pass identification, replacement for typeid
  24.     InsertInstruction() : FunctionPass(ID) {}
  25.   private:
  26.     void eraseOldInstructions(std::vector<Instruction*>& v) {
  27.         for(auto I: v) I->eraseFromParent();
  28.     }
  29.    
  30.     bool runOnFunction(Function &F) override {
  31.         bool modified = false;
  32.         std::vector<Instruction*> old_inst_vec;
  33.         for(auto& B: F) {
  34.             for(auto& I: B) {
  35.                 if(I.getOpcode() == Instruction::FAdd) {
  36.                     old_inst_vec.push_back(&I);
  37.                     Value* lhs = I.getOperand(0);
  38.                     Value* rhs = I.getOperand(1);            
  39.                     IRBuilder<> builder(&I);
  40.                     Value* add_value = builder.CreateFAdd(lhs, rhs);
  41.                     Value* five =  ConstantFP::get(lhs->getType(),5.0);                    
  42.                     Value* final_value[] = {builder.CreateFAdd(add_value, five)};
  43.                     //Type* ty[] = {Type::getFloatTy(F.getParent()->getContext())};
  44.                     Function* new_func = Intrinsic::getDeclaration(F.getParent(), Intrinsic::foo_sqrt_test);
  45.                     Value* sqrt = CallInst::Create(new_func, final_value, "", &I);
  46.                     I.replaceAllUsesWith(sqrt);
  47.                     modified = true;
  48.                 }
  49.             }
  50.         }
  51.         if(modified) eraseOldInstructions(old_inst_vec);
  52.         return modified;
  53.     }
  54.   };
  55. }
  56.  
  57. char InsertInstruction::ID = 0;
  58. static RegisterPass<InsertInstruction> X("insertinst", "Insert Instruction");
Add Comment
Please, Sign In to add comment