Trim #includes.
[oota-llvm.git] / lib / Transforms / Instrumentation / RSProfiling.cpp
index ad9a841cb0d935cd424af47fb285f3dea182e992..45a7c712d8c7168368c3418de95d400abd64772a 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                      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.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -18,7 +18,7 @@
 // backedges.  At each connection point a choice is made as to whether to jump
 // to the profiled code (take a sample) or execute the unprofiled code.
 //
-// It is highly recommeneded that after this pass one runs mem2reg and adce
+// It is highly recommended that after this pass one runs mem2reg and adce
 // (instcombine load-vn gdce dse also are good to run afterwards)
 //
 // This design is intended to make the profiling passes independent of the RS
@@ -37,6 +37,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/Intrinsics.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Support/CommandLine.h"
 #include <set>
 #include <map>
 #include <queue>
-#include <list>
 using namespace llvm;
 
 namespace {
   enum RandomMeth {
     GBV, GBVO, HOSTCC
   };
+}
 
-  cl::opt<RandomMeth> RandomMethod("profile-randomness",
-      cl::desc("How to randomly choose to profile:"),
-      cl::values(
-                 clEnumValN(GBV, "global", "global counter"),
-                 clEnumValN(GBVO, "ra_global", 
-                            "register allocated global counter"),
-                 clEnumValN(HOSTCC, "rdcc", "cycle counter"),
-                 clEnumValEnd));
+static cl::opt<RandomMeth> RandomMethod("profile-randomness",
+    cl::desc("How to randomly choose to profile:"),
+    cl::values(
+               clEnumValN(GBV, "global", "global counter"),
+               clEnumValN(GBVO, "ra_global", 
+                          "register allocated global counter"),
+               clEnumValN(HOSTCC, "rdcc", "cycle counter"),
+               clEnumValEnd));
   
+namespace {
   /// NullProfilerRS - The basic profiler that does nothing.  It is the default
   /// profiler and thus terminates RSProfiler chains.  It is useful for 
   /// measuring framework overhead
   class VISIBILITY_HIDDEN NullProfilerRS : public RSProfilers {
   public:
-    static const int ID; // Pass identifcation, replacement for typeid
+    static char ID; // Pass identification, replacement for typeid
     bool isProfiling(Value* v) {
       return false;
     }
@@ -80,14 +82,14 @@ namespace {
       AU.setPreservesAll();
     }
   };
+}
 
-  const int RSProfilers::ID = 0;
-  static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
-  const int NullProfilerRS::ID = 0;
-  static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
-                                         "Measure profiling framework overhead");
-  static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
+static RegisterAnalysisGroup<RSProfilers> A("Profiling passes");
+static RegisterPass<NullProfilerRS> NP("insert-null-profiling-rs",
+                                       "Measure profiling framework overhead");
+static RegisterAnalysisGroup<RSProfilers, true> NPT(NP);
 
+namespace {
   /// Chooser - Something that chooses when to make a sample of the profiled code
   class VISIBILITY_HIDDEN Chooser {
   public:
@@ -141,7 +143,7 @@ namespace {
 
   /// ProfilerRS - Insert the random sampling framework
   struct VISIBILITY_HIDDEN ProfilerRS : public FunctionPass {
-    static const int ID; // Pass identifcation, replacement for typeid
+    static char ID; // Pass identification, replacement for typeid
     ProfilerRS() : FunctionPass((intptr_t)&ID) {}
 
     std::map<Value*, Value*> TransCache;
@@ -159,12 +161,16 @@ namespace {
     bool doInitialization(Module &M);
     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
   };
-
-  const int ProfilerRS::ID = 0;
-  RegisterPass<ProfilerRS> X("insert-rs-profiling-framework",
-                             "Insert random sampling instrumentation framework");
 }
 
+static RegisterPass<ProfilerRS>
+X("insert-rs-profiling-framework",
+  "Insert random sampling instrumentation framework");
+
+char RSProfilers::ID = 0;
+char NullProfilerRS::ID = 0;
+char ProfilerRS::ID = 0;
+
 //Local utilities
 static void ReplacePhiPred(BasicBlock* btarget, 
                            BasicBlock* bold, BasicBlock* bnew);
@@ -208,16 +214,16 @@ void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
   ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), 
                              "countercc", t);
 
-  Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
+  Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
                                         "counternew", t);
   new StoreInst(nv, Counter, t);
   t->setCondition(s);
   
   //reset counter
   BasicBlock* oldnext = t->getSuccessor(0);
-  BasicBlock* resetblock = new BasicBlock("reset", oldnext->getParent(), 
-                                          oldnext);
-  TerminatorInst* t2 = new BranchInst(oldnext, resetblock);
+  BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(), 
+                                              oldnext);
+  TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
   t->setSuccessor(0, resetblock);
   new StoreInst(ResetValue, Counter, t2);
   ReplacePhiPred(oldnext, bb, resetblock);
@@ -258,14 +264,11 @@ void GlobalRandomCounterOpt::PrepFunction(Function* F) {
         new StoreInst(l, Counter, bib);
         
         BasicBlock* bb = cast<InvokeInst>(bib)->getNormalDest();
-        BasicBlock::iterator i = bb->begin();
-        while (isa<PHINode>(i))
-          ++i;
+        BasicBlock::iterator i = bb->getFirstNonPHI();
         l = new LoadInst(Counter, "counter", i);
         
         bb = cast<InvokeInst>(bib)->getUnwindDest();
-        i = bb->begin();
-        while (isa<PHINode>(i)) ++i;
+        i = bb->getFirstNonPHI();
         l = new LoadInst(Counter, "counter", i);
         new StoreInst(l, AI, i);
       } else if (isa<UnwindInst>(&*bib) || isa<ReturnInst>(&*bib)) {
@@ -283,16 +286,16 @@ void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
   ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), 
                              "countercc", t);
 
-  Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
+  Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
                                         "counternew", t);
   new StoreInst(nv, AI, t);
   t->setCondition(s);
   
   //reset counter
   BasicBlock* oldnext = t->getSuccessor(0);
-  BasicBlock* resetblock = new BasicBlock("reset", oldnext->getParent(), 
-                                          oldnext);
-  TerminatorInst* t2 = new BranchInst(oldnext, resetblock);
+  BasicBlock* resetblock = BasicBlock::Create("reset", oldnext->getParent(), 
+                                              oldnext);
+  TerminatorInst* t2 = BranchInst::Create(oldnext, resetblock);
   t->setSuccessor(0, resetblock);
   new StoreInst(ResetValue, AI, t2);
   ReplacePhiPred(oldnext, bb, resetblock);
@@ -300,7 +303,7 @@ void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
 
 
 CycleCounter::CycleCounter(Module& m, uint64_t resetmask) : rm(resetmask) {
-  F = m.getOrInsertFunction("llvm.readcyclecounter", Type::Int64Ty, NULL);
+  F = Intrinsic::getDeclaration(&m, Intrinsic::readcyclecounter);
 }
 
 CycleCounter::~CycleCounter() {}
@@ -310,9 +313,9 @@ void CycleCounter::PrepFunction(Function* F) {}
 void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
   BranchInst* t = cast<BranchInst>(bb->getTerminator());
   
-  CallInst* c = new CallInst(F, "rdcc", t);
+  CallInst* c = CallInst::Create(F, "rdcc", t);
   BinaryOperator* b = 
-    BinaryOperator::createAnd(c, ConstantInt::get(Type::Int64Ty, rm),
+    BinaryOperator::CreateAnd(c, ConstantInt::get(Type::Int64Ty, rm),
                               "mrdcc", t);
   
   ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
@@ -336,8 +339,8 @@ bool RSProfilers_std::isProfiling(Value* v) {
 void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
                                           GlobalValue *CounterArray) {
   // Insert the increment after any alloca or PHI instructions...
-  BasicBlock::iterator InsertPos = BB->begin();
-  while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
+  BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
+  while (isa<AllocaInst>(InsertPos))
     ++InsertPos;
   
   // Create the getelementptr constant expression
@@ -350,7 +353,7 @@ void RSProfilers_std::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNu
   // Load, increment and store the value back.
   Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
   profcode.insert(OldVal);
-  Value *NewVal = BinaryOperator::createAdd(OldVal,
+  Value *NewVal = BinaryOperator::CreateAdd(OldVal,
                                             ConstantInt::get(Type::Int32Ty, 1),
                                             "NewCounter", InsertPos);
   profcode.insert(NewVal);
@@ -374,8 +377,8 @@ Value* ProfilerRS::Translate(Value* v) {
     if (bb == &bb->getParent()->getEntryBlock())
       TransCache[bb] = bb; //don't translate entry block
     else
-      TransCache[bb] = new BasicBlock("dup_" + bb->getName(), bb->getParent(), 
-                                      NULL);
+      TransCache[bb] = BasicBlock::Create("dup_" + bb->getName(),
+                                          bb->getParent(), NULL);
     return TransCache[bb];
   } else if (Instruction* i = dyn_cast<Instruction>(v)) {
     //we have already translated this
@@ -463,16 +466,16 @@ void ProfilerRS::ProcessBackEdge(BasicBlock* src, BasicBlock* dst, Function& F)
   
   //a:
   Function::iterator BBN = src; ++BBN;
-  BasicBlock* bbC = new BasicBlock("choice", &F, BBN);
+  BasicBlock* bbC = BasicBlock::Create("choice", &F, BBN);
   //ChoicePoints.insert(bbC);
   BBN = cast<BasicBlock>(Translate(src));
-  BasicBlock* bbCp = new BasicBlock("choice", &F, ++BBN);
+  BasicBlock* bbCp = BasicBlock::Create("choice", &F, ++BBN);
   ChoicePoints.insert(bbCp);
   
   //b:
-  new BranchInst(cast<BasicBlock>(Translate(dst)), bbC);
-  new BranchInst(dst, cast<BasicBlock>(Translate(dst)), 
-                 ConstantInt::get(Type::Int1Ty, true), bbCp);
+  BranchInst::Create(cast<BasicBlock>(Translate(dst)), bbC);
+  BranchInst::Create(dst, cast<BasicBlock>(Translate(dst)), 
+                     ConstantInt::get(Type::Int1Ty, true), bbCp);
   //c:
   {
     TerminatorInst* iB = src->getTerminator();
@@ -526,10 +529,11 @@ bool ProfilerRS::runOnFunction(Function& F) {
     //oh, and add the edge from the reg2mem created entry node to the 
     //duplicated second node
     TerminatorInst* T = F.getEntryBlock().getTerminator();
-    ReplaceInstWithInst(T, new BranchInst(T->getSuccessor(0),
-                                          cast<BasicBlock>(
-                                            Translate(T->getSuccessor(0))),
-                                          ConstantInt::get(Type::Int1Ty, true)));
+    ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0),
+                                              cast<BasicBlock>(
+                                                Translate(T->getSuccessor(0))),
+                                              ConstantInt::get(Type::Int1Ty,
+                                                               true)));
     
     //do whatever is needed now that the function is duplicated
     c->PrepFunction(&F);