When a function takes a variable number of pointer arguments, with a zero
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / EdgeCode.cpp
index ba4a9013a73987a0a25a9a3130044d2fab1556c0..bf94943788781b48b74cad8f1924499acb8087b3 100644 (file)
@@ -1,48 +1,54 @@
 //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
-//It implements the class EdgeCode: which provides 
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//It implements the class EdgeCode: which provides
 //support for inserting "appropriate" instrumentation at
 //designated points in the graph
 //
-//It also has methods to insert initialization code in 
+//It also has methods to insert initialization code in
 //top block of cfg
 //===----------------------------------------------------------------------===//
 
 #include "Graph.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/iMemory.h"
-#include "llvm/iTerminators.h"
-#include "llvm/iOther.h"
-#include "llvm/iOperators.h"
-#include "llvm/iPHINode.h"
+#include "llvm/Instructions.h"
 #include "llvm/Module.h"
-#include <stdio.h>
 
 #define INSERT_LOAD_COUNT
 #define INSERT_STORE
 
+
 using std::vector;
 
+namespace llvm {
 
 static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
-                           Value *cnt){ 
-  
-  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);
-  args.push_back(PointerType::get(Type::IntTy));
-  const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
-
-  Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
+                           Value *cnt, Instruction *rInst){
+
+  vector<Value *> tmpVec;
+  tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+  tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+  Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//,
+  BB->getInstList().push_back(Idx);
+
+  const Type *PIntTy = PointerType::get(Type::IntTy);
+  Function *trigMeth = M->getOrInsertFunction("trigger", Type::VoidTy,
+                                              Type::IntTy, Type::IntTy,
+                                              PIntTy, PIntTy, (Type *)0);
   assert(trigMeth && "trigger method could not be inserted!");
 
   vector<Value *> trargs;
 
   trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
   trargs.push_back(pathNo);
-  trargs.push_back(cnt);
+  trargs.push_back(Idx);
+  trargs.push_back(rInst);
 
   Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
   BB->getInstList().push_back(callInst);
@@ -52,18 +58,18 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
 
 //get the code to be inserted on the edge
 //This is determined from cond (1-6)
-void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, 
-                         Function *M, BasicBlock *BB, 
+void getEdgeCode::getCode(Instruction *rInst, Value *countInst,
+                          Function *M, BasicBlock *BB,
                           vector<Value *> &retVec){
-  
+
   //Instruction *InsertPos = BB->getInstList().begin();
-  
+
   //now check for cdIn and cdOut
   //first put cdOut
   if(cdOut!=NULL){
     cdOut->getCode(rInst, countInst, M, BB, retVec);
   }
-  
+
   if(cdIn!=NULL){
     cdIn->getCode(rInst, countInst, M, BB, retVec);
   }
@@ -87,7 +93,7 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
 #endif
     break;
   }
-    
+
   //r+=k
   case 3:{
     Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos);
@@ -105,34 +111,39 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
 
   //count[inc]++
   case 4:{
-    Instruction *Idx = new GetElementPtrInst(countInst, 
-                 vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
-                                             "");//, InsertPos);
+    vector<Value *> tmpVec;
+    tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+    tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc));
+    Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
+
+    //Instruction *Idx = new GetElementPtrInst(countInst,
+    //           vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
+    //                                       "");//, InsertPos);
     BB->getInstList().push_back(Idx);
 
     Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
     BB->getInstList().push_back(ldInst);
+
     Value *val = ConstantSInt::get(Type::IntTy, 1);
     //Instruction *addIn =
     Instruction *newCount =
       BinaryOperator::create(Instruction::Add, ldInst, val,"ti2");
     BB->getInstList().push_back(newCount);
-    
+
 
 #ifdef INSERT_STORE
     //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
     Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos);
     BB->getInstList().push_back(stInst);
 #endif
-    
+
     Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc);
 
     retVec.push_back(newCount);
     retVec.push_back(trAddIndex);
     //insert trigger
-    //getTriggerCode(M->getParent(), BB, MethNo, 
-    //    ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
+    //getTriggerCode(M->getParent(), BB, MethNo,
+    //     ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
     //end trigger code
 
     assert(inc>=0 && "IT MUST BE POSITIVE NOW");
@@ -141,7 +152,7 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
 
   //case: count[r+inc]++
   case 5:{
-   
+
     //ti1=inc+r
     Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
     BB->getInstList().push_back(ldIndex);
@@ -150,14 +161,16 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
     Instruction *addIndex=BinaryOperator::
       create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos);
     BB->getInstList().push_back(addIndex);
-    
+
     //now load count[addIndex]
-    Instruction *castInst=new CastInst(addIndex, 
-                                      Type::LongTy,"ctin");//, InsertPos);
+    Instruction *castInst=new CastInst(addIndex,
+                                       Type::LongTy,"ctin");//, InsertPos);
     BB->getInstList().push_back(castInst);
 
-    Instruction *Idx = new GetElementPtrInst(countInst, 
-                                             vector<Value*>(1,castInst), "");//,
+    vector<Value *> tmpVec;
+    tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+    tmpVec.push_back(castInst);
+    Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
     //                                             InsertPos);
     BB->getInstList().push_back(Idx);
 
@@ -166,10 +179,11 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
 
     Value *cons=ConstantSInt::get(Type::IntTy,1);
     //count[addIndex]++
-    Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst, 
+    //std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n";
+    Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
                                                    cons,"");
     BB->getInstList().push_back(newCount);
-    
+
 #ifdef INSERT_STORE
     Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
     BB->getInstList().push_back(stInst);
@@ -194,10 +208,16 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
     Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
     BB->getInstList().push_back(castInst2);
 
-    Instruction *Idx = new GetElementPtrInst(countInst, 
-                                             vector<Value*>(1,castInst2), "");
+    vector<Value *> tmpVec;
+    tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+    tmpVec.push_back(castInst2);
+    Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
+
+    //Instruction *Idx = new GetElementPtrInst(countInst,
+    //                                       vector<Value*>(1,castInst2), "");
+
     BB->getInstList().push_back(Idx);
-    
+
     Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
     BB->getInstList().push_back(ldInst);
 
@@ -217,7 +237,7 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
     retVec.push_back(ldIndex);
     break;
   }
-    
+
   }
 }
 
@@ -225,26 +245,25 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
 
 //Insert the initialization code in the top BB
 //this includes initializing r, and count
-//r is like an accumulator, that 
+//r is like an accumulator, that
 //keeps on adding increments as we traverse along a path
 //and at the end of the path, r contains the path
 //number of that path
 //Count is an array, where Count[k] represents
 //the number of executions of path k
-void insertInTopBB(BasicBlock *front, 
-                  int k, 
-                  Instruction *rVar, 
-                  Instruction *countVar, Value *threshold){
-  //rVar is variable r, 
-  //countVar is array Count, and these are allocatted outside
+void insertInTopBB(BasicBlock *front,
+                   int k,
+                   Instruction *rVar, Value *threshold){
+  //rVar is variable r,
+  //countVar is count[]
 
   Value *Int0 = ConstantInt::get(Type::IntTy, 0);
-  
+
   //now push all instructions in front of the BB
   BasicBlock::iterator here=front->begin();
   front->getInstList().insert(here, rVar);
-  front->getInstList().insert(here,countVar);
-  
+  //front->getInstList().insert(here,countVar);
+
   //Initialize Count[...] with 0
 
   //for (int i=0;i<k; i++){
@@ -256,60 +275,28 @@ void insertInTopBB(BasicBlock *front,
 
   //store uint 0, uint *%R
   new StoreInst(Int0, rVar, here);
-
-  //insert initialize function for initializing 
-  vector<const Type*> inCountArgs;
-  inCountArgs.push_back(PointerType::get(Type::IntTy));
-  inCountArgs.push_back(Type::IntTy);
-
-  const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs, 
-                                               false);
-  Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
-  assert(inCountMth && "Initialize method could not be inserted!");
-
-  vector<Value *> iniArgs;
-  iniArgs.push_back(countVar);
-  iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
-  new CallInst(inCountMth, iniArgs, "", here);
-  
-
-  if(front->getParent()->getName() == "main"){
-    //intialize threshold
-    vector<const Type*> initialize_args;
-    initialize_args.push_back(PointerType::get(Type::IntTy));
-    
-    const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
-                                                false);
-    Function *initialMeth = front->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
-    assert(initialMeth && "Initialize method could not be inserted!");
-    
-    vector<Value *> trargs;
-    trargs.push_back(threshold);
-  
-    new CallInst(initialMeth, trargs, "", here);
-  }
 }
 
 
 //insert a basic block with appropriate code
 //along a given edge
 void insertBB(Edge ed,
-             getEdgeCode *edgeCode, 
-             Instruction *rInst, 
-             Instruction *countInst, 
-             int numPaths, int Methno, Value *threshold){
+              getEdgeCode *edgeCode,
+              Instruction *rInst,
+              Value *countInst,
+              int numPaths, int Methno, Value *threshold){
 
   BasicBlock* BB1=ed.getFirst()->getElement();
   BasicBlock* BB2=ed.getSecond()->getElement();
-  
+
 #ifdef DEBUG_PATH_PROFILES
   //debugging info
   cerr<<"Edges with codes ######################\n";
   cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
   cerr<<"########################\n";
 #endif
-  
-  //We need to insert a BB between BB1 and BB2 
+
+  //We need to insert a BB between BB1 and BB2
   TerminatorInst *TI=BB1->getTerminator();
   BasicBlock *newBB=new BasicBlock("counter", BB1->getParent());
 
@@ -329,7 +316,7 @@ void insertBB(Edge ed,
   else{
       if(BI->getSuccessor(0)==BB2)
       BI->setSuccessor(0, newBB);
-    
+
     if(BI->getSuccessor(1)==BB2)
       BI->setSuccessor(1, newBB);
   }
@@ -337,35 +324,33 @@ void insertBB(Edge ed,
   BasicBlock *triggerBB = NULL;
   if(retVec.size()>0){
     triggerBB = new BasicBlock("trigger", BB1->getParent());
-    getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno, 
-                   retVec[1], countInst);//retVec[0]);
+    getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
+                   retVec[1], countInst, rInst);//retVec[0]);
 
     //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
     Instruction *etr = new LoadInst(threshold, "threshold");
-    
-    //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n"; 
-    Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr, 
+
+    //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n";
+    Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
                                            retVec[0], "");
     Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst);
     //newBB->getInstList().push_back(castInst);
     newBB->getInstList().push_back(etr);
     newBB->getInstList().push_back(cmpInst);
     newBB->getInstList().push_back(newBI2);
-    
+
     //triggerBB->getInstList().push_back(triggerInst);
-    Instruction *triggerBranch = new BranchInst(BB2);
-    triggerBB->getInstList().push_back(triggerBranch);
+    new BranchInst(BB2, 0, 0, triggerBB);
   }
   else{
-    Instruction *newBI2=new BranchInst(BB2);
-    newBB->getInstList().push_back(newBI2);
+    new BranchInst(BB2, 0, 0, newBB);
   }
 
   //now iterate over BB2, and set its Phi nodes right
-  for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end(); 
+  for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
       BB2Inst != BBend; ++BB2Inst){
-   
-    if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
+
+    if(PHINode *phiInst=dyn_cast<PHINode>(BB2Inst)){
       int bbIndex=phiInst->getBasicBlockIndex(BB1);
       assert(bbIndex>=0);
       phiInst->setIncomingBlock(bbIndex, newBB);
@@ -380,3 +365,4 @@ void insertBB(Edge ed,
   }
 }
 
+} // End llvm namespace