Rename include/llvm/Transforms/Instrumentation/TraceFunctions.h to Instrumentation.h
[oota-llvm.git] / lib / Transforms / Instrumentation / TraceValues.cpp
index 3239e73c98183c3234b62ad31e04c13bceecba3b..8c44d4d28ca88df6ee5567bc17b1f02b927fdbbc 100644 (file)
@@ -5,8 +5,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Instrumentation/TraceValues.h"
-#include "llvm/GlobalVariable.h"
+#include "llvm/Transforms/Instrumentation.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
@@ -27,8 +26,8 @@ DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
                   cl::desc("Disable pointer hashing"));
 
 static cl::list<string>
-TraceFuncName("tracefunc", cl::desc("trace only specific functions"),
-              cl::value_desc("function"), cl::Hidden);
+TraceFuncNames("tracefunc", cl::desc("trace only specific functions"),
+              cl::value_desc("function"), cl::Hidden);
 
 static void TraceValuesAtBBExit(BasicBlock *BB,
                                 Function *Printf, Function* HashPtrToSeqNum,
@@ -38,13 +37,12 @@ static void TraceValuesAtBBExit(BasicBlock *BB,
 // or if the function is in the specified list.
 // 
 inline static bool
-TraceThisFunction(Function &func)
+TraceThisFunction(Function &F)
 {
-  if (TraceFuncName.size() == 0)
-    return true;
+  if (TraceFuncNames.empty()) return true;
 
-  return std::find(TraceFuncName.begin(), TraceFuncName.end(), func.getName())
-                  != TraceFuncName.end();
+  return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
+                  != TraceFuncNames.end();
 }
 
 
@@ -79,7 +77,7 @@ namespace {
     bool runOnFunction(Function &F);
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.preservesCFG();
+      AU.setPreservesCFG();
     }
   };
 
@@ -180,7 +178,7 @@ static inline bool TraceThisOpCode(unsigned opCode) {
   // Explicitly test for opCodes *not* to trace so that any new opcodes will
   // be traced by default (VoidTy's are already excluded)
   // 
-  return (opCode  < Instruction::FirstOtherOp &&
+  return (opCode  < Instruction::OtherOpsBegin &&
           opCode != Instruction::Alloca &&
           opCode != Instruction::PHINode &&
           opCode != Instruction::Cast);
@@ -201,7 +199,7 @@ static string getPrintfCodeFor(const Value *V) {
     return "0x%p";
   else if (isa<PointerType>(V->getType()))
     return DisablePtrHashing ? "0x%p" : "%d";
-  else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
+  else if (V->getType()->isIntegral())
     return "%d";
   
   assert(0 && "Illegal value to print out...");
@@ -209,16 +207,21 @@ static string getPrintfCodeFor(const Value *V) {
 }
 
 
-static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
+static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
                             string Message,
                             Function *Printf, Function* HashPtrToSeqNum) {
   // Escape Message by replacing all % characters with %% chars.
-  unsigned Offset = 0;
-  while ((Offset = Message.find('%', Offset)) != string::npos) {
-    Message.replace(Offset, 1, "%%");
-    Offset += 2;  // Skip over the new %'s
+  string Tmp;
+  std::swap(Tmp, Message);
+  string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
+  while (I != Tmp.end()) {
+    Message.append(Tmp.begin(), I);
+    Message += "%%";
+    ++I; // Make sure to erase the % as well...
+    Tmp.erase(Tmp.begin(), I);
+    I = std::find(Tmp.begin(), Tmp.end(), '%');
   }
-
+  Message += Tmp;
   Module *Mod = BB->getParent()->getParent();
 
   // Turn the marker string into a global variable...
@@ -227,89 +230,60 @@ static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
   // Turn the format string into an sbyte *
   Instruction *GEP = 
     new GetElementPtrInst(fmtVal,
-                          vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
-                          "trstr");
-  BBI = ++BB->getInstList().insert(BBI, GEP);
+                          vector<Value*>(2,ConstantSInt::get(Type::LongTy, 0)),
+                          "trstrp", InsertBefore);
   
   // Insert a call to the hash function if this is a pointer value
   if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
     const Type *SBP = PointerType::get(Type::SByteTy);
-    if (V->getType() != SBP) {   // Cast pointer to be sbyte*
-      Instruction *I = new CastInst(V, SBP, "Hash_cast");
-      BBI = ++BB->getInstList().insert(BBI, I);
-      V = I;
-    }
+    if (V->getType() != SBP)     // Cast pointer to be sbyte*
+      V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
 
     vector<Value*> HashArgs(1, V);
-    V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
-    BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
+    V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
   }
   
   // Insert the first print instruction to print the string flag:
   vector<Value*> PrintArgs;
   PrintArgs.push_back(GEP);
   if (V) PrintArgs.push_back(V);
-  Instruction *I = new CallInst(Printf, PrintArgs, "trace");
-  BBI = ++BB->getInstList().insert(BBI, I);
+  new CallInst(Printf, PrintArgs, "trace", InsertBefore);
 }
                             
 
 static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
-                                   BasicBlock::iterator &BBI,
+                                   Instruction *InsertBefore,
                                    const string &Message, Function *Printf,
                                    Function* HashPtrToSeqNum) {
   std::ostringstream OutStr;
   if (V) WriteAsOperand(OutStr, V);
-  InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
+  InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
                   Printf, HashPtrToSeqNum);
 }
 
 static void 
 InsertReleaseInst(Value *V, BasicBlock *BB,
-                  BasicBlock::iterator &BBI,
+                  Instruction *InsertBefore,
                   Function* ReleasePtrFunc) {
   
   const Type *SBP = PointerType::get(Type::SByteTy);
-  if (V->getType() != SBP) {   // Cast pointer to be sbyte*
-    Instruction *I = new CastInst(V, SBP, "RPSN_cast");
-    BBI = ++BB->getInstList().insert(BBI, I);
-    V = I;
-  }
+  if (V->getType() != SBP)    // Cast pointer to be sbyte*
+    V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
+
   vector<Value*> releaseArgs(1, V);
-  Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
-  BBI = ++BB->getInstList().insert(BBI, I);
+  new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
 }
 
 static void 
 InsertRecordInst(Value *V, BasicBlock *BB,
-                 BasicBlock::iterator &BBI,
+                 Instruction *InsertBefore,
                  Function* RecordPtrFunc) {
     const Type *SBP = PointerType::get(Type::SByteTy);
-  if (V->getType() != SBP) {   // Cast pointer to be sbyte*
-    Instruction *I = new CastInst(V, SBP, "RP_cast");
-    BBI = ++BB->getInstList().insert(BBI, I);
-    V = I;
-  }
-  vector<Value*> releaseArgs(1, V);
-  Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
-  BBI = ++BB->getInstList().insert(BBI, I);
-}
+  if (V->getType() != SBP)     // Cast pointer to be sbyte*
+    V = new CastInst(V, SBP, "RP_cast", InsertBefore);
 
-static void
-InsertPushOnEntryFunc(Function *M,
-                      Function* PushOnEntryFunc) {
-  // Get an iterator to point to the insertion location
-  BasicBlock &BB = M->getEntryNode();
-  BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
-                                                   vector<Value*>()));
-}
-
-static void 
-InsertReleaseRecordedInst(BasicBlock *BB,
-                          Function* ReleaseOnReturnFunc) {
-  BasicBlock::iterator BBI = --BB->end();
-  BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
-                                                     vector<Value*>()));
+  vector<Value*> releaseArgs(1, V);
+  new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
 }
 
 // Look for alloca and free instructions. These are the ptrs to release.
@@ -320,16 +294,11 @@ static void
 ReleasePtrSeqNumbers(BasicBlock *BB,
                      ExternalFuncs& externalFuncs) {
   
-  for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
+  for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
     if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
-      InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
+      InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
     else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
-      {
-        BasicBlock::iterator nextI = ++II;
-        InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);     
-        II = --nextI;
-      }
-  }
+      InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
 }  
 
 
@@ -348,8 +317,7 @@ static void TraceValuesAtBBExit(BasicBlock *BB,
   // Get an iterator to point to the insertion location, which is
   // just before the terminator instruction.
   // 
-  BasicBlock::iterator InsertPos = --BB->end();
-  assert(InsertPos->isTerminator());
+  TerminatorInst *InsertPos = BB->getTerminator();
   
   std::ostringstream OutStr;
   WriteAsOperand(OutStr, BB, false);
@@ -360,21 +328,17 @@ static void TraceValuesAtBBExit(BasicBlock *BB,
   // The print instructions must go before InsertPos, so we use the
   // instruction *preceding* InsertPos to check when to terminate the loop.
   // 
-  if (InsertPos != BB->begin()) { // there's at least one instr before InsertPos
-    BasicBlock::iterator II = BB->begin(), IEincl = InsertPos;
-    --IEincl;
-    do {                          // do from II up to IEincl, inclusive
-      if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
-        assert(valuesStoredInFunction &&
-               "Should not be printing a store instruction at function exit");
-        LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
-                                  "reload."+SI->getPointerOperand()->getName());
-        InsertPos = ++BB->getInstList().insert(InsertPos, LI);
-        valuesStoredInFunction->push_back(LI);
-      }
-      if (ShouldTraceValue(II))
-        InsertVerbosePrintInst(II, BB, InsertPos, "  ", Printf,HashPtrToSeqNum);
-    } while (II++ != IEincl);
+  for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
+    if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
+      assert(valuesStoredInFunction &&
+             "Should not be printing a store instruction at function exit");
+      LoadInst *LI = new LoadInst(SI->getPointerOperand(), "reload." +
+                                  SI->getPointerOperand()->getName(),
+                                  InsertPos);
+      valuesStoredInFunction->push_back(LI);
+    }
+    if (ShouldTraceValue(II))
+      InsertVerbosePrintInst(II, BB, InsertPos, "  ", Printf, HashPtrToSeqNum);
   }
 }
 
@@ -382,17 +346,17 @@ static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
                                                  Function* HashPtrToSeqNum){
   // Get an iterator to point to the insertion location
   BasicBlock &BB = F.getEntryNode();
-  BasicBlock::iterator BBI = BB.begin();
+  Instruction *InsertPos = BB.begin();
 
   std::ostringstream OutStr;
-  WriteAsOperand(OutStr, &F, true);
-  InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
+  WriteAsOperand(OutStr, &F);
+  InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
                   Printf, HashPtrToSeqNum);
 
   // Now print all the incoming arguments
   unsigned ArgNo = 0;
   for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
-    InsertVerbosePrintInst(I, &BB, BBI,
+    InsertVerbosePrintInst(I, &BB, InsertPos,
                            "  Arg #" + utostr(ArgNo) + ": ", Printf,
                            HashPtrToSeqNum);
   }
@@ -403,17 +367,16 @@ static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
                                                 Function *Printf,
                                                 Function* HashPtrToSeqNum) {
   // Get an iterator to point to the insertion location
-  BasicBlock::iterator BBI = --BB->end();
-  ReturnInst &Ret = cast<ReturnInst>(BB->back());
+  ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
   
   std::ostringstream OutStr;
   WriteAsOperand(OutStr, BB->getParent(), true);
-  InsertPrintInst(0, BB, BBI, "LEAVING  FUNCTION: " + OutStr.str(),
+  InsertPrintInst(0, BB, Ret, "LEAVING  FUNCTION: " + OutStr.str(),
                   Printf, HashPtrToSeqNum);
   
   // print the return value, if any
   if (BB->getParent()->getReturnType() != Type::VoidTy)
-    InsertPrintInst(Ret.getReturnValue(), BB, BBI, "  Returning: ",
+    InsertPrintInst(Ret->getReturnValue(), BB, Ret, "  Returning: ",
                     Printf, HashPtrToSeqNum);
 }
 
@@ -431,8 +394,9 @@ bool InsertTraceCode::runOnFunction(Function &F) {
   
   // Push a pointer set for recording alloca'd pointers at entry.
   if (!DisablePtrHashing)
-    InsertPushOnEntryFunc(&F, externalFuncs.PushOnEntryFunc);
-  
+    new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
+                 F.getEntryNode().begin());
+
   for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
     if (isa<ReturnInst>(BB->getTerminator()))
       exitBlocks.push_back(BB); // record this as an exit block
@@ -452,8 +416,8 @@ bool InsertTraceCode::runOnFunction(Function &F) {
       
       // Release all recorded pointers before RETURN.  Do this LAST!
       if (!DisablePtrHashing)
-        InsertReleaseRecordedInst(exitBlocks[i],
-                                  externalFuncs.ReleaseOnReturnFunc);
+        new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
+                     exitBlocks[i]->getTerminator());
     }
   
   return true;