blob: 21f3b25c0e933450c3a49418cf54c4f939c8ff56 [file] [log] [blame]
Anand Shukla320febb2002-11-03 01:45:20 +00001//===-- InstLoops.cpp ---------------------------------------- ---*- C++ -*--=//
2// Pass to instrument loops
3//
4// At every backedge, insert a counter for that backedge and a call function
5//===----------------------------------------------------------------------===//
6
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"
20
21//this is used to color vertices
22//during DFS
23
24enum Color{
25 WHITE,
26 GREY,
27 BLACK
28};
29
30struct InstLoops : public FunctionPass {
31 bool runOnFunction(Function &F);
32};
33
34static RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
35
36// createProfilePathsPass - Create a new pass to add path profiling
37//
38Pass *createInstLoopsPass() {
39 return new InstLoops();
40}
41
42
43//helper function to get back edges: it is called by
44//the "getBackEdges" function below
45void getBackEdgesVisit(BasicBlock *u,
46 std::map<BasicBlock *, Color > &color,
47 std::map<BasicBlock *, int > &d,
48 int &time, Value *threshold) {
49
50 color[u]=GREY;
51 time++;
52 d[u]=time;
53
54 for(BasicBlock::succ_iterator vl = succ_begin(u),
55 ve = succ_end(u); vl != ve; ++vl){
56
57 BasicBlock *BB = *vl;
58
59 if(color[BB]!=GREY && color[BB]!=BLACK){
60 getBackEdgesVisit(BB, color, d, time, threshold);
61 }
62
63 //now checking for d and f vals
64 if(color[BB]==GREY){
65 //so v is ancestor of u if time of u > time of v
66 if(d[u] >= d[BB]){
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){
72 index = 0;
73 }
74 assert(ti->getNumSuccessors() > index && "Not enough successors!");
75 ti->setSuccessor(index, newBB);
76
77 //insert global variable of type int
78 Constant *initializer = Constant::getNullValue(Type::IntTy);
79 GlobalVariable *countVar = new GlobalVariable(Type::IntTy, false, true,
80 initializer,
81 "loopCounter",
82 u->getParent()->getParent());
83
84 //load the variable
85 Instruction *ldInst = new LoadInst(countVar,"");
86
87 //increment
88 Instruction *addIn =
89 BinaryOperator::create(Instruction::Add, ldInst,
90 ConstantSInt::get(Type::IntTy,1), "");
91
92 //store
93 Instruction *stInst = new StoreInst(addIn, countVar);
94
95
96 Instruction *etr = new LoadInst(threshold, "threshold");
97 Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
98 addIn, "");
99
100 BasicBlock *callTrigger = new BasicBlock("", u->getParent());
101 //branch to calltrigger, or *vl
102 Instruction *newBr = new BranchInst(callTrigger, BB, cmpInst);
103
104 BasicBlock::InstListType &lt = newBB->getInstList();
105
106 lt.push_back(ldInst);
107 lt.push_back(addIn);
108 lt.push_back(stInst);
109 lt.push_back(etr);
110 lt.push_back(cmpInst);
111 lt.push_back(newBr);
112
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));
118
119 const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
120 false);
121 Function *inCountMth =
122 u->getParent()->getParent()->getOrInsertFunction("llvm_first_trigger", cFty);
123
124 assert(inCountMth && "Initialize method could not be inserted!");
125
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));
131
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){
135
136 if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
137 int bbIndex = phiInst->getBasicBlockIndex(u);
138 if(bbIndex>=0){
139 phiInst->setIncomingBlock(bbIndex, newBB);
140
141 Value *val = phiInst->getIncomingValue((unsigned int)bbIndex);
142 phiInst->addIncoming(val, callTrigger);
143 }
144 }
145 }
146 }
147 }
148 }
149 color[u]=BLACK;//done with visiting the node and its neighbors
150}
151
152
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
161//have been visited
162//So we have a back edge when we meet a successor of
163//a node with smaller time, and GREY color
164void getBackEdges(Function &F, Value *threshold){
165 std::map<BasicBlock *, Color > color;
166 std::map<BasicBlock *, int> d;
167 int time=0;
168 getBackEdgesVisit(F.begin(), color, d, time, threshold);
169}
170
171//Per function pass for inserting counters and call function
172bool InstLoops::runOnFunction(Function &F){
173
174 static GlobalVariable *threshold = NULL;
175 static bool insertedThreshold = false;
176
177 if(!insertedThreshold){
178 threshold = new GlobalVariable(Type::IntTy, false, false, 0,
179 "reopt_threshold");
180
181 F.getParent()->getGlobalList().push_back(threshold);
182 insertedThreshold = true;
183 }
184
185 if(F.getName() == "main"){
186 //intialize threshold
187 std::vector<const Type*> initialize_args;
188 initialize_args.push_back(PointerType::get(Type::IntTy));
189
190 const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
191 false);
192 Function *initialMeth = F.getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
193 assert(initialMeth && "Initialize method could not be inserted!");
194
195 std::vector<Value *> trargs;
196 trargs.push_back(threshold);
197
198 new CallInst(initialMeth, trargs, "", F.begin()->begin());
199 }
200
201 assert(threshold && "GlobalVariable threshold not defined!");
202
203 if(F.isExternal()) {
204 return false;
205 }
206
207 getBackEdges(F, threshold);
208
209 return true;
210}