blob: aa46048ae0343b9d27f53816b247b3f156ac1e69 [file] [log] [blame]
Anand Shukla70a61382002-02-26 19:00:48 +00001//===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
2//
3// This inserts intrumentation for counting
Chris Lattner62b7fd12002-04-07 20:49:59 +00004// execution of paths though a given function
5// Its implemented as a "Function" Pass, and called using opt
Anand Shukla70a61382002-02-26 19:00:48 +00006//
7// This pass is implemented by using algorithms similar to
8// 1."Efficient Path Profiling": Ball, T. and Larus, J. R.,
9// Proceedings of Micro-29, Dec 1996, Paris, France.
10// 2."Efficiently Counting Program events with support for on-line
11// "queries": Ball T., ACM Transactions on Programming Languages
12// and systems, Sep 1994.
13//
14// The algorithms work on a Graph constructed over the nodes
15// made from Basic Blocks: The transformations then take place on
16// the constucted graph (implementation in Graph.cpp and GraphAuxillary.cpp)
17// and finally, appropriate instrumentation is placed over suitable edges.
18// (code inserted through EdgeCode.cpp).
19//
20// The algorithm inserts code such that every acyclic path in the CFG
Chris Lattner62b7fd12002-04-07 20:49:59 +000021// of a function is identified through a unique number. the code insertion
Anand Shukla70a61382002-02-26 19:00:48 +000022// is optimal in the sense that its inserted over a minimal set of edges. Also,
23// the algorithm makes sure than initialization, path increment and counter
Anand Shukla21906892002-06-25 21:14:58 +000024// update can be collapsed into minimum number of edges.
Anand Shukla70a61382002-02-26 19:00:48 +000025//===----------------------------------------------------------------------===//
26
Chris Lattner15435fd2002-05-07 19:18:48 +000027#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Anand Shukla70a61382002-02-26 19:00:48 +000028#include "llvm/Support/CFG.h"
Chris Lattnerca142372002-04-28 19:55:58 +000029#include "llvm/Constants.h"
Anand Shukla70a61382002-02-26 19:00:48 +000030#include "llvm/DerivedTypes.h"
31#include "llvm/iMemory.h"
Anand Shukla2fd8edd2003-06-05 06:02:46 +000032#include "llvm/iOperators.h"
33#include "llvm/iOther.h"
Anand Shukla77dca142002-09-20 16:44:35 +000034#include "llvm/Module.h"
Chris Lattner2f04a0d2003-01-14 22:33:56 +000035#include "Graph.h"
Anand Shukla21906892002-06-25 21:14:58 +000036#include <fstream>
John Criswell3ef61af2003-06-30 21:59:07 +000037#include "Config/stdio.h"
Anand Shukla70a61382002-02-26 19:00:48 +000038
Chris Lattner37104aa2002-04-29 14:57:45 +000039struct ProfilePaths : public FunctionPass {
Chris Lattner7076ff22002-06-25 16:13:21 +000040 bool runOnFunction(Function &F);
Chris Lattnerd2095502002-02-26 20:04:59 +000041
42 // Before this pass, make sure that there is only one
Chris Lattner62b7fd12002-04-07 20:49:59 +000043 // entry and only one exit node for the function in the CFG of the function
Chris Lattnerd2095502002-02-26 20:04:59 +000044 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000045 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner40eb9da2002-08-08 19:01:28 +000046 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattnerd2095502002-02-26 20:04:59 +000047 }
48};
49
Chris Lattnera2c09852002-07-26 21:12:44 +000050static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
Chris Lattnerb28b6802002-07-23 18:06:35 +000051
Anand Shukla21906892002-06-25 21:14:58 +000052static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
53 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
Anand Shukla70a61382002-02-26 19:00:48 +000054 if(((*si)->getElement())==BB){
55 return *si;
56 }
57 }
58 return NULL;
59}
60
Chris Lattner62b7fd12002-04-07 20:49:59 +000061//Per function pass for inserting counters and trigger code
Chris Lattner7076ff22002-06-25 16:13:21 +000062bool ProfilePaths::runOnFunction(Function &F){
Anand Shukla21906892002-06-25 21:14:58 +000063
64 static int mn = -1;
Anand Shukla2fd8edd2003-06-05 06:02:46 +000065 static int CountCounter = 1;
Anand Shuklafd61c602002-07-18 20:56:47 +000066 if(F.isExternal()) {
Anand Shukla11f42622002-07-08 19:36:01 +000067 return false;
68 }
69
70 //increment counter for instrumented functions. mn is now function#
71 mn++;
72
Chris Lattnerf9986852002-04-27 07:27:19 +000073 // Transform the cfg s.t. we have just one exit node
Chris Lattner10a032a2003-09-10 20:35:33 +000074 BasicBlock *ExitNode =
75 getAnalysis<UnifyFunctionExitNodes>().getReturnBlock();
Anand Shukla21906892002-06-25 21:14:58 +000076
77 //iterating over BBs and making graph
78 std::vector<Node *> nodes;
79 std::vector<Edge> edges;
80
Anand Shukla70a61382002-02-26 19:00:48 +000081 Node *tmp;
Chris Lattnera2620ac2002-11-09 00:49:43 +000082 Node *exitNode = 0, *startNode = 0;
Anand Shukla70a61382002-02-26 19:00:48 +000083
Chris Lattnerf9986852002-04-27 07:27:19 +000084 // The nodes must be uniquesly identified:
85 // That is, no two nodes must hav same BB*
Anand Shukla70a61382002-02-26 19:00:48 +000086
Chris Lattner7076ff22002-06-25 16:13:21 +000087 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
88 Node *nd=new Node(BB);
Anand Shukla21906892002-06-25 21:14:58 +000089 nodes.push_back(nd);
Chris Lattner7076ff22002-06-25 16:13:21 +000090 if(&*BB == ExitNode)
Anand Shukla70a61382002-02-26 19:00:48 +000091 exitNode=nd;
Chris Lattner889f6202003-04-23 16:37:45 +000092 if(BB==F.begin())
Anand Shukla70a61382002-02-26 19:00:48 +000093 startNode=nd;
94 }
95
Chris Lattnerf9986852002-04-27 07:27:19 +000096 // now do it againto insert edges
Chris Lattner7076ff22002-06-25 16:13:21 +000097 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
98 Node *nd=findBB(nodes, BB);
Anand Shukla70a61382002-02-26 19:00:48 +000099 assert(nd && "No node for this edge!");
Anand Shukla21906892002-06-25 21:14:58 +0000100
Chris Lattner7076ff22002-06-25 16:13:21 +0000101 for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB);
Anand Shukla70a61382002-02-26 19:00:48 +0000102 s!=se; ++s){
103 Node *nd2=findBB(nodes,*s);
104 assert(nd2 && "No node for this edge!");
105 Edge ed(nd,nd2,0);
Anand Shukla21906892002-06-25 21:14:58 +0000106 edges.push_back(ed);
Anand Shukla70a61382002-02-26 19:00:48 +0000107 }
108 }
109
110 Graph g(nodes,edges, startNode, exitNode);
111
Anand Shuklad9a6af02002-09-16 05:24:49 +0000112#ifdef DEBUG_PATH_PROFILES
113 std::cerr<<"Original graph\n";
114 printGraph(g);
115#endif
Anand Shukla70a61382002-02-26 19:00:48 +0000116
Anand Shukla11f42622002-07-08 19:36:01 +0000117 BasicBlock *fr = &F.front();
Anand Shukla70a61382002-02-26 19:00:48 +0000118
Anand Shukla11f42622002-07-08 19:36:01 +0000119 // The graph is made acyclic: this is done
120 // by removing back edges for now, and adding them later on
Chris Lattner10a032a2003-09-10 20:35:33 +0000121 std::vector<Edge> be;
Anand Shuklafd61c602002-07-18 20:56:47 +0000122 std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
123 g.getBackEdges(be, nodePriority);
Anand Shuklad9a6af02002-09-16 05:24:49 +0000124
125#ifdef DEBUG_PATH_PROFILES
126 std::cerr<<"BackEdges-------------\n";
Chris Lattner10a032a2003-09-10 20:35:33 +0000127 for (std::vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
Anand Shuklad9a6af02002-09-16 05:24:49 +0000128 printEdge(*VI);
129 cerr<<"\n";
130 }
131 std::cerr<<"------\n";
132#endif
Anand Shukla21906892002-06-25 21:14:58 +0000133
134#ifdef DEBUG_PATH_PROFILES
Anand Shukla11f42622002-07-08 19:36:01 +0000135 cerr<<"Backedges:"<<be.size()<<endl;
Anand Shukla21906892002-06-25 21:14:58 +0000136#endif
Anand Shukla11f42622002-07-08 19:36:01 +0000137 //Now we need to reflect the effect of back edges
138 //This is done by adding dummy edges
139 //If a->b is a back edge
140 //Then we add 2 back edges for it:
141 //1. from root->b (in vector stDummy)
142 //and 2. from a->exit (in vector exDummy)
Chris Lattner10a032a2003-09-10 20:35:33 +0000143 std::vector<Edge> stDummy;
144 std::vector<Edge> exDummy;
Anand Shukla11f42622002-07-08 19:36:01 +0000145 addDummyEdges(stDummy, exDummy, g, be);
Anand Shukla21906892002-06-25 21:14:58 +0000146
Anand Shuklad9a6af02002-09-16 05:24:49 +0000147#ifdef DEBUG_PATH_PROFILES
148 std::cerr<<"After adding dummy edges\n";
149 printGraph(g);
150#endif
151
Anand Shukla11f42622002-07-08 19:36:01 +0000152 // Now, every edge in the graph is assigned a weight
153 // This weight later adds on to assign path
154 // numbers to different paths in the graph
155 // All paths for now are acyclic,
156 // since no back edges in the graph now
157 // numPaths is the number of acyclic paths in the graph
Anand Shuklad9a6af02002-09-16 05:24:49 +0000158 int numPaths=valueAssignmentToEdges(g, nodePriority, be);
Anand Shukla21906892002-06-25 21:14:58 +0000159
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000160 //if(numPaths<=1) return false;
161
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000162 static GlobalVariable *threshold = NULL;
163 static bool insertedThreshold = false;
164
165 if(!insertedThreshold){
166 threshold = new GlobalVariable(Type::IntTy, false,
167 GlobalValue::ExternalLinkage, 0,
168 "reopt_threshold");
169
170 F.getParent()->getGlobalList().push_back(threshold);
171 insertedThreshold = true;
172 }
173
174 assert(threshold && "GlobalVariable threshold not defined!");
175
176
177 if(fr->getParent()->getName() == "main"){
178 //intialize threshold
Chris Lattner25bc3f82003-08-31 00:21:59 +0000179
180 // FIXME: THIS IS HORRIBLY BROKEN. FUNCTION PASSES CANNOT DO THIS, EXCEPT
181 // IN THEIR INITIALIZE METHOD!!
182 Function *initialize =
183 F.getParent()->getOrInsertFunction("reoptimizerInitialize", Type::VoidTy,
184 PointerType::get(Type::IntTy), 0);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000185
Chris Lattner10a032a2003-09-10 20:35:33 +0000186 std::vector<Value *> trargs;
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000187 trargs.push_back(threshold);
Chris Lattner25bc3f82003-08-31 00:21:59 +0000188 new CallInst(initialize, trargs, "", fr->begin());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000189 }
190
191
Anand Shuklafd61c602002-07-18 20:56:47 +0000192 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000193
194#ifdef DEBUG_PATH_PROFILES
195 printGraph(g);
196#endif
197
Anand Shukla11f42622002-07-08 19:36:01 +0000198 //create instruction allocation r and count
199 //r is the variable that'll act like an accumulator
200 //all along the path, we just add edge values to r
201 //and at the end, r reflects the path number
202 //count is an array: count[x] would store
203 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000204
Anand Shukla11f42622002-07-08 19:36:01 +0000205 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000206 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000207 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000208
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000209 //Instruction *countVar=new
210 //AllocaInst(Type::IntTy,
211 // ConstantUInt::get(Type::UIntTy, numPaths), "Count");
212
213 //initialize counter array!
214 std::vector<Constant*> arrayInitialize;
215 for(int xi=0; xi<numPaths; xi++)
216 arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
217
Chris Lattner379a8d22003-04-16 20:28:45 +0000218 const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
219 Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000220 char tempChar[20];
221 sprintf(tempChar, "Count%d", CountCounter);
222 CountCounter++;
223 std::string countStr = tempChar;
Chris Lattner379a8d22003-04-16 20:28:45 +0000224 GlobalVariable *countVar = new GlobalVariable(ATy, false,
225 GlobalValue::InternalLinkage,
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000226 initializer, countStr,
Chris Lattner379a8d22003-04-16 20:28:45 +0000227 F.getParent());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000228
Anand Shukla11f42622002-07-08 19:36:01 +0000229 // insert initialization code in first (entry) BB
230 // this includes initializing r and count
Chris Lattner5dac64f2003-09-20 14:39:18 +0000231 insertInTopBB(&F.getEntryBlock(), numPaths, rVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000232
Anand Shukla11f42622002-07-08 19:36:01 +0000233 //now process the graph: get path numbers,
234 //get increments along different paths,
235 //and assign "increments" and "updates" (to r and count)
236 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000237 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
238 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000239
Chris Lattner62b7fd12002-04-07 20:49:59 +0000240 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000241}