Fix bug I introduced
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / EdgeCode.cpp
index e377b163d0d3b59d8cee6b2380f02d410dd001df..8668be1590599412914135c1a42c437dcbf78248 100644 (file)
@@ -1,4 +1,4 @@
-//===-- EdgeCode.cpp - generate LLVM instrumentation code --------*- C++ -*--=//
+//===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
 //It implements the class EdgeCode: which provides 
 //support for inserting "appropriate" instrumentation at
 //designated points in the graph
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Instrumentation/Graph.h"
-#include "llvm/BasicBlock.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
 #include "llvm/iOperators.h"
 #include "llvm/iPHINode.h"
 #include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
-#include "llvm/GlobalVariable.h"
-#include "llvm/Constants.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Function.h"
 #include <string.h>
 #include <stdio.h>
-#include <iostream>
 
 #define INSERT_LOAD_COUNT
 #define INSERT_STORE
 using std::vector;
 
 
-void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, 
-                   Value *cnt){ 
+static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
+                           Value *cnt, Instruction *InsertPos){ 
   static int i=-1;
   i++;
   char gstr[100];
   sprintf(gstr,"globalVar%d",i);
   std::string globalVarName=gstr;
-  SymbolTable *ST = M->getSymbolTable();
   vector<const Type*> args;
   //args.push_back(PointerType::get(Type::SByteTy));
   args.push_back(Type::IntTy);
   args.push_back(Type::IntTy);
   args.push_back(Type::IntTy);
-  const FunctionType *MTy =
-    FunctionType::get(Type::VoidTy, args, false);
+  const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
 
   //  Function *triggerMeth = M->getOrInsertFunction("trigger", MTy);
   Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
@@ -64,9 +55,9 @@ void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
   
   //M->getGlobalList().push_back(gbl);
 
-  vector<Value *> elargs;
-  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
-  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
+  //vector<Value *> elargs;
+  //elargs.push_back(ConstantSInt::get(Type::LongTy, 0));
+  //elargs.push_back(ConstantSInt::get(Type::LongTy, 0));
 
   // commented out bb name frm which its called
   //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst");
@@ -81,12 +72,7 @@ void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
   //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this
   trargs.push_back(pathNo);
   trargs.push_back(cnt);
-  Instruction *callInst=new CallInst(trigMeth,trargs);
-
-  BasicBlock::InstListType& instList=BB->getInstList();
-  BasicBlock::iterator here=instList.begin();
-  //here = ++instList.insert(here, getElmntInst);
-  instList.insert(here,callInst);
+  Instruction *callInst=new CallInst(trigMeth, trargs, "", InsertPos);
 }
 
 
@@ -97,156 +83,126 @@ void getEdgeCode::getCode(Instruction *rInst,
                          Function *M, 
                          BasicBlock *BB, int numPaths, int MethNo){
   
-  BasicBlock::InstListType& instList=BB->getInstList();
-  BasicBlock::iterator here=instList.begin();
+  Instruction *InsertPos = BB->begin();
   
   //case: r=k code to be inserted
   switch(cond){
   case 1:{
     Value *val=ConstantSInt::get(Type::IntTy,inc);
 #ifdef INSERT_STORE
-    Instruction *stInst=new StoreInst(val, rInst);
-    here = ++instList.insert(here,stInst);
+    Instruction *stInst=new StoreInst(val, rInst, InsertPos);
 #endif
     break;
     }
 
   //case: r=0 to be inserted
-  case 2:{
-    Value *val=ConstantSInt::get(Type::IntTy,0);
+  case 2:
 #ifdef INSERT_STORE
-    Instruction *stInst=new StoreInst(val, rInst);
-    here = ++instList.insert(here,stInst);
+    new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst, InsertPos);
 #endif
     break;
-  }
     
   //r+=k
   case 3:{
-    
-    Instruction *ldInst=new LoadInst(rInst, "ti1");
-    Value *val=ConstantSInt::get(Type::IntTy,inc);
-    Instruction *addIn=BinaryOperator::
-      create(Instruction::Add, ldInst, val,"ti2");
-#ifdef INSERT_STORE
-    Instruction *stInst=new StoreInst(addIn, rInst);
-#endif
-    here = ++instList.insert(here,ldInst);
-    here = ++instList.insert(here,addIn);
+    Instruction *ldInst = new LoadInst(rInst, "ti1", InsertPos);
+    Value *val = ConstantSInt::get(Type::IntTy,inc);
+    Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
+                                          "ti2", InsertPos);
 #ifdef INSERT_STORE
-    here = ++instList.insert(here,stInst);
+    new StoreInst(addIn, rInst, InsertPos);
 #endif
     break;
   }
 
   //count[inc]++
   case 4:{
-    
     assert(inc>=0 && inc<=numPaths && "inc out of bound!");
    
-    Instruction *ldInst=new 
-      LoadInst(countInst,vector<Value *>
-              (1,ConstantUInt::get(Type::UIntTy, inc)), "ti1");
-    Value *val=ConstantSInt::get(Type::IntTy,1);
-    Instruction *addIn=BinaryOperator::
-      create(Instruction::Add, ldInst, val,"ti2");
+    Instruction *Idx = new GetElementPtrInst(countInst, 
+                 vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
+                                             "", InsertPos);
+
+    Instruction *ldInst=new LoadInst(Idx, "ti1", InsertPos);
+    Value *val = ConstantSInt::get(Type::IntTy, 1);
+    Instruction *addIn =
+      BinaryOperator::create(Instruction::Add, ldInst, val,"ti2", InsertPos);
+
+#ifdef INSERT_STORE
+    Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
+#endif
 
     //insert trigger
     getTriggerCode(M->getParent(), BB, MethNo, 
-                  ConstantSInt::get(Type::IntTy,inc), addIn);
-    here=instList.begin();
+                  ConstantSInt::get(Type::IntTy,inc), addIn, InsertPos);
     //end trigger code
 
     assert(inc>=0 && "IT MUST BE POSITIVE NOW");
-#ifdef INSERT_STORE
-    Instruction *stInst=new 
-      StoreInst(addIn, countInst, vector<Value *>
-               (1, ConstantUInt::get(Type::UIntTy,inc)));
-#endif
-    here = ++instList.insert(here,ldInst);
-    here = ++instList.insert(here,addIn);
-#ifdef INSERT_STORE
-    here = ++instList.insert(here,stInst);
-#endif
     break;
   }
 
   //case: count[r+inc]++
   case 5:{
-    
+   
     //ti1=inc+r
-    Instruction *ldIndex=new LoadInst(rInst, "ti1");
+    Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
     Value *val=ConstantSInt::get(Type::IntTy,inc);
     Instruction *addIndex=BinaryOperator::
-      create(Instruction::Add, ldIndex, val,"ti2");
+      create(Instruction::Add, ldIndex, val,"ti2", InsertPos);
     //erase following 1 line
     //Value *valtemp=ConstantSInt::get(Type::IntTy,999);
     //now load count[addIndex]
     
     Instruction *castInst=new CastInst(addIndex, 
-                                      Type::UIntTy,"ctin");
-    Instruction *ldInst=new 
-      LoadInst(countInst, vector<Value *>(1,castInst), "ti3");
+                                      Type::LongTy,"ctin", InsertPos);
+    Instruction *Idx = new GetElementPtrInst(countInst, 
+                                             vector<Value*>(1,castInst), "",
+                                             InsertPos);
+
+    Instruction *ldInst=new LoadInst(Idx, "ti3", InsertPos);
     Value *cons=ConstantSInt::get(Type::IntTy,1);
     //count[addIndex]++
-    Instruction *addIn=BinaryOperator::
-      create(Instruction::Add, ldInst, cons,"ti4");
-    
-    //insert trigger
-    getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn);
-    here=instList.begin();
-    //end trigger code
+    Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, cons,
+                                          "ti4", InsertPos);
     
 #ifdef INSERT_STORE
     ///*
-    Instruction *stInst=new 
-      StoreInst(addIn, countInst, 
-               vector<Value *>(1,castInst));
+    new StoreInst(addIn, Idx, InsertPos);
     //*/
 #endif
-    here = ++instList.insert(here,ldIndex);
-    here = ++instList.insert(here,addIndex);
-    here = ++instList.insert(here,castInst);
-    here = ++instList.insert(here,ldInst);
-    here = ++instList.insert(here,addIn);
-#ifdef INSERT_STORE
-    here = ++instList.insert(here,stInst);
-#endif
+
+    //insert trigger
+    getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn, InsertPos);
+    //end trigger code
+
     break;
   }
 
     //case: count[r]+
   case 6:{
-    
     //ti1=inc+r
-    Instruction *ldIndex=new LoadInst(rInst, "ti1");
+    Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
     
     //now load count[addIndex]
-    Instruction *castInst2=new 
-      CastInst(ldIndex, Type::UIntTy,"ctin");
-    Instruction *ldInst=new 
-      LoadInst(countInst, vector<Value *>(1,castInst2), "ti2");
+    Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin",InsertPos);
+    Instruction *Idx = new GetElementPtrInst(countInst, 
+                                             vector<Value*>(1,castInst2), "",
+                                             InsertPos);
+    
+    Instruction *ldInst=new LoadInst(Idx, "ti2", InsertPos);
     Value *cons=ConstantSInt::get(Type::IntTy,1);
 
     //count[addIndex]++
-    Instruction *addIn=BinaryOperator::
-      create(Instruction::Add, ldInst, cons,"ti3"); 
+    Instruction *addIn=BinaryOperator::create(Instruction::Add, ldInst,
+                                              cons,"ti3", InsertPos);
 
-    //insert trigger
-    getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn);
-    here=instList.begin();
-    //end trigger code
 #ifdef INSERT_STORE
-    Instruction *stInst=new 
-      StoreInst(addIn, countInst, vector<Value *>(1,castInst2));
-#endif
-    here = ++instList.insert(here,ldIndex);
-    here = ++instList.insert(here,castInst2);
-    here = instList.insert(here,ldInst);
-    here = instList.insert(here,addIn);
-#ifdef INSERT_STORE
-    here = instList.insert(here,stInst);
+    new StoreInst(addIn, Idx, InsertPos);
 #endif
+    //insert trigger
+    getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn, InsertPos);
+    //end trigger code
+    
     break;
   }
     
@@ -277,30 +233,25 @@ void insertInTopBB(BasicBlock *front,
                   Instruction *countVar){
   //rVar is variable r, 
   //countVar is array Count, and these are allocatted outside
-  
-  //store uint 0, uint *%R, uint 0
-  vector<Value *> idx;
-  idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
-  Instruction *stInstr=new StoreInst(ConstantInt::get(Type::IntTy, 0), rVar, 
-                                    idx);
 
+  Value *Int0 = ConstantInt::get(Type::IntTy, 0);
+  
   //now push all instructions in front of the BB
-  BasicBlock::InstListType& instList=front->getInstList();
-  BasicBlock::iterator here=instList.begin();
-  here=++front->getInstList().insert(here, rVar);
-  here=++front->getInstList().insert(here,countVar);
+  BasicBlock::iterator here=front->begin();
+  front->getInstList().insert(here, rVar);
+  front->getInstList().insert(here,countVar);
   
   //Initialize Count[...] with 0
 
-  for(int i=0;i<k; i++){
-    Instruction *stInstrC=new 
-      StoreInst(ConstantInt::get(Type::IntTy, 0), 
-               countVar, std::vector<Value *>
-               (1,ConstantUInt::get(Type::UIntTy, i))); 
-    here=++front->getInstList().insert(here,stInstrC);
+  for (int i=0;i<k; i++){
+    Value *GEP2 = new GetElementPtrInst(countVar,
+                          vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
+                                        "", here);
+    new StoreInst(Int0, GEP2, here);
   }
 
-  here = ++front->getInstList().insert(here,stInstr);
+  //store uint 0, uint *%R
+  new StoreInst(Int0, rVar, here);
 }
 
 
@@ -331,9 +282,6 @@ void insertBB(Edge ed,
   TerminatorInst *TI=BB1->getTerminator();
   BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent());
 
-  //get code for the new BB
-  edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno);
   //Is terminator a branch instruction?
   //then we need to change branch destinations to include new BB
 
@@ -358,6 +306,10 @@ void insertBB(Edge ed,
     Instruction *newBI2=new BranchInst(BB2);
     newBB->getInstList().push_back(newBI2);
   }
+
+  //get code for the new BB
+  edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno);
+
   
   //std::cerr<<"After casting\n";
   //get code for the new BB