1 //===-- InstLoops.cpp ---------------------------------------- ---*- C++ -*--=//
2 // Pass to instrument loops
4 // At every backedge, insert a counter for that backedge and a call function
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Reoptimizer/InstLoops.h"
8 #include "llvm/Support/CFG.h"
9 #include "llvm/Constants.h"
10 #include "llvm/iMemory.h"
11 #include "llvm/GlobalVariable.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/iOther.h"
14 #include "llvm/iOperators.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/iPHINode.h"
17 #include "llvm/Module.h"
18 #include "llvm/Function.h"
19 #include "llvm/Pass.h"
21 //this is used to color vertices
30 struct InstLoops : public FunctionPass {
31 bool runOnFunction(Function &F);
34 static RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
36 // createProfilePathsPass - Create a new pass to add path profiling
38 Pass *createInstLoopsPass() {
39 return new InstLoops();
43 //helper function to get back edges: it is called by
44 //the "getBackEdges" function below
45 void getBackEdgesVisit(BasicBlock *u,
46 std::map<BasicBlock *, Color > &color,
47 std::map<BasicBlock *, int > &d,
48 int &time, Value *threshold) {
54 for(BasicBlock::succ_iterator vl = succ_begin(u),
55 ve = succ_end(u); vl != ve; ++vl){
59 if(color[BB]!=GREY && color[BB]!=BLACK){
60 getBackEdgesVisit(BB, color, d, time, threshold);
63 //now checking for d and f vals
65 //so v is ancestor of u if time of u > time of v
67 //insert a new basic block: modify terminator accordingly!
68 BasicBlock *newBB = new BasicBlock("", u->getParent());
69 BranchInst *ti = cast<BranchInst>(u->getTerminator());
70 unsigned char index = 1;
71 if(ti->getSuccessor(0) == BB){
74 assert(ti->getNumSuccessors() > index && "Not enough successors!");
75 ti->setSuccessor(index, newBB);
77 //insert global variable of type int
78 Constant *initializer = Constant::getNullValue(Type::IntTy);
79 GlobalVariable *countVar = new GlobalVariable(Type::IntTy, false, true,
82 u->getParent()->getParent());
85 Instruction *ldInst = new LoadInst(countVar,"");
89 BinaryOperator::create(Instruction::Add, ldInst,
90 ConstantSInt::get(Type::IntTy,1), "");
93 Instruction *stInst = new StoreInst(addIn, countVar);
96 Instruction *etr = new LoadInst(threshold, "threshold");
97 Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
100 BasicBlock *callTrigger = new BasicBlock("", u->getParent());
101 //branch to calltrigger, or *vl
102 Instruction *newBr = new BranchInst(callTrigger, BB, cmpInst);
104 BasicBlock::InstListType < = newBB->getInstList();
106 lt.push_back(ldInst);
108 lt.push_back(stInst);
110 lt.push_back(cmpInst);
113 //Now add instructions to the triggerCall BB
114 //now create a call function
115 //call llvm_first_trigger(int *x);
116 std::vector<const Type*> inCountArgs;
117 inCountArgs.push_back(PointerType::get(Type::IntTy));
119 const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
121 Function *inCountMth =
122 u->getParent()->getParent()->getOrInsertFunction("llvm_first_trigger", cFty);
124 assert(inCountMth && "Initialize method could not be inserted!");
126 std::vector<Value *> iniArgs;
127 iniArgs.push_back(countVar);
128 Instruction *call = new CallInst(inCountMth, iniArgs, "");
129 callTrigger->getInstList().push_back(call);
130 callTrigger->getInstList().push_back(new BranchInst(BB));
132 //now iterate over *vl, and set its Phi nodes right
133 for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
134 BB2Inst != BBend; ++BB2Inst){
136 if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
137 int bbIndex = phiInst->getBasicBlockIndex(u);
139 phiInst->setIncomingBlock(bbIndex, newBB);
141 Value *val = phiInst->getIncomingValue((unsigned int)bbIndex);
142 phiInst->addIncoming(val, callTrigger);
149 color[u]=BLACK;//done with visiting the node and its neighbors
153 //getting the backedges in a graph
154 //Its a variation of DFS to get the backedges in the graph
155 //We get back edges by associating a time
156 //and a color with each vertex.
157 //The time of a vertex is the time when it was first visited
158 //The color of a vertex is initially WHITE,
159 //Changes to GREY when it is first visited,
160 //and changes to BLACK when ALL its neighbors
162 //So we have a back edge when we meet a successor of
163 //a node with smaller time, and GREY color
164 void getBackEdges(Function &F, Value *threshold){
165 std::map<BasicBlock *, Color > color;
166 std::map<BasicBlock *, int> d;
168 getBackEdgesVisit(F.begin(), color, d, time, threshold);
171 //Per function pass for inserting counters and call function
172 bool InstLoops::runOnFunction(Function &F){
174 static GlobalVariable *threshold = NULL;
175 static bool insertedThreshold = false;
177 if(!insertedThreshold){
178 threshold = new GlobalVariable(Type::IntTy, false, false, 0,
181 F.getParent()->getGlobalList().push_back(threshold);
182 insertedThreshold = true;
185 if(F.getName() == "main"){
186 //intialize threshold
187 std::vector<const Type*> initialize_args;
188 initialize_args.push_back(PointerType::get(Type::IntTy));
190 const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
192 Function *initialMeth = F.getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
193 assert(initialMeth && "Initialize method could not be inserted!");
195 std::vector<Value *> trargs;
196 trargs.push_back(threshold);
198 new CallInst(initialMeth, trargs, "", F.begin()->begin());
201 assert(threshold && "GlobalVariable threshold not defined!");
207 getBackEdges(F, threshold);