blob: df2b764e7c8a2977ef4433e1c3820fc0e7c838c8 [file] [log] [blame]
Anand Shukla70a61382002-02-26 19:00:48 +00001//===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
2//
Misha Brukman8b2bd4e2003-10-10 17:57:28 +00003// This inserts instrumentation 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
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000016// the constructed graph (implementation in Graph.cpp and GraphAuxiliary.cpp)
Anand Shukla70a61382002-02-26 19:00:48 +000017// 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>
Brian Gaekeb8a4ed62003-10-10 18:46:52 +000037#include <cstdio>
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
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000084 // The nodes must be uniquely identified:
Chris Lattnerf9986852002-04-27 07:27:19 +000085 // 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
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000096 // now do it again to 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 Lattnera9400952003-09-24 22:06:25 +0000101 for(succ_iterator s=succ_begin(BB), se=succ_end(BB); s!=se; ++s){
Anand Shukla70a61382002-02-26 19:00:48 +0000102 Node *nd2=findBB(nodes,*s);
103 assert(nd2 && "No node for this edge!");
104 Edge ed(nd,nd2,0);
Anand Shukla21906892002-06-25 21:14:58 +0000105 edges.push_back(ed);
Anand Shukla70a61382002-02-26 19:00:48 +0000106 }
107 }
108
109 Graph g(nodes,edges, startNode, exitNode);
110
Anand Shuklad9a6af02002-09-16 05:24:49 +0000111#ifdef DEBUG_PATH_PROFILES
112 std::cerr<<"Original graph\n";
113 printGraph(g);
114#endif
Anand Shukla70a61382002-02-26 19:00:48 +0000115
Anand Shukla11f42622002-07-08 19:36:01 +0000116 BasicBlock *fr = &F.front();
Anand Shukla70a61382002-02-26 19:00:48 +0000117
Anand Shukla11f42622002-07-08 19:36:01 +0000118 // The graph is made acyclic: this is done
119 // by removing back edges for now, and adding them later on
Chris Lattner10a032a2003-09-10 20:35:33 +0000120 std::vector<Edge> be;
Anand Shuklafd61c602002-07-18 20:56:47 +0000121 std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
122 g.getBackEdges(be, nodePriority);
Anand Shuklad9a6af02002-09-16 05:24:49 +0000123
124#ifdef DEBUG_PATH_PROFILES
125 std::cerr<<"BackEdges-------------\n";
Chris Lattner10a032a2003-09-10 20:35:33 +0000126 for (std::vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
Anand Shuklad9a6af02002-09-16 05:24:49 +0000127 printEdge(*VI);
128 cerr<<"\n";
129 }
130 std::cerr<<"------\n";
131#endif
Anand Shukla21906892002-06-25 21:14:58 +0000132
133#ifdef DEBUG_PATH_PROFILES
Anand Shukla11f42622002-07-08 19:36:01 +0000134 cerr<<"Backedges:"<<be.size()<<endl;
Anand Shukla21906892002-06-25 21:14:58 +0000135#endif
Anand Shukla11f42622002-07-08 19:36:01 +0000136 //Now we need to reflect the effect of back edges
137 //This is done by adding dummy edges
138 //If a->b is a back edge
139 //Then we add 2 back edges for it:
140 //1. from root->b (in vector stDummy)
141 //and 2. from a->exit (in vector exDummy)
Chris Lattner10a032a2003-09-10 20:35:33 +0000142 std::vector<Edge> stDummy;
143 std::vector<Edge> exDummy;
Anand Shukla11f42622002-07-08 19:36:01 +0000144 addDummyEdges(stDummy, exDummy, g, be);
Anand Shukla21906892002-06-25 21:14:58 +0000145
Anand Shuklad9a6af02002-09-16 05:24:49 +0000146#ifdef DEBUG_PATH_PROFILES
147 std::cerr<<"After adding dummy edges\n";
148 printGraph(g);
149#endif
150
Anand Shukla11f42622002-07-08 19:36:01 +0000151 // Now, every edge in the graph is assigned a weight
152 // This weight later adds on to assign path
153 // numbers to different paths in the graph
154 // All paths for now are acyclic,
155 // since no back edges in the graph now
156 // numPaths is the number of acyclic paths in the graph
Anand Shuklad9a6af02002-09-16 05:24:49 +0000157 int numPaths=valueAssignmentToEdges(g, nodePriority, be);
Anand Shukla21906892002-06-25 21:14:58 +0000158
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000159 //if(numPaths<=1) return false;
160
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000161 static GlobalVariable *threshold = NULL;
162 static bool insertedThreshold = false;
163
164 if(!insertedThreshold){
165 threshold = new GlobalVariable(Type::IntTy, false,
166 GlobalValue::ExternalLinkage, 0,
167 "reopt_threshold");
168
169 F.getParent()->getGlobalList().push_back(threshold);
170 insertedThreshold = true;
171 }
172
173 assert(threshold && "GlobalVariable threshold not defined!");
174
175
176 if(fr->getParent()->getName() == "main"){
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000177 //initialize threshold
Chris Lattner25bc3f82003-08-31 00:21:59 +0000178
179 // FIXME: THIS IS HORRIBLY BROKEN. FUNCTION PASSES CANNOT DO THIS, EXCEPT
180 // IN THEIR INITIALIZE METHOD!!
181 Function *initialize =
182 F.getParent()->getOrInsertFunction("reoptimizerInitialize", Type::VoidTy,
183 PointerType::get(Type::IntTy), 0);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000184
Chris Lattner10a032a2003-09-10 20:35:33 +0000185 std::vector<Value *> trargs;
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000186 trargs.push_back(threshold);
Chris Lattner25bc3f82003-08-31 00:21:59 +0000187 new CallInst(initialize, trargs, "", fr->begin());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000188 }
189
190
Anand Shuklafd61c602002-07-18 20:56:47 +0000191 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000192
193#ifdef DEBUG_PATH_PROFILES
194 printGraph(g);
195#endif
196
Anand Shukla11f42622002-07-08 19:36:01 +0000197 //create instruction allocation r and count
198 //r is the variable that'll act like an accumulator
199 //all along the path, we just add edge values to r
200 //and at the end, r reflects the path number
201 //count is an array: count[x] would store
202 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000203
Anand Shukla11f42622002-07-08 19:36:01 +0000204 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000205 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000206 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000207
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000208 //Instruction *countVar=new
209 //AllocaInst(Type::IntTy,
210 // ConstantUInt::get(Type::UIntTy, numPaths), "Count");
211
212 //initialize counter array!
213 std::vector<Constant*> arrayInitialize;
214 for(int xi=0; xi<numPaths; xi++)
215 arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
216
Chris Lattner379a8d22003-04-16 20:28:45 +0000217 const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
218 Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000219 char tempChar[20];
220 sprintf(tempChar, "Count%d", CountCounter);
221 CountCounter++;
222 std::string countStr = tempChar;
Chris Lattner379a8d22003-04-16 20:28:45 +0000223 GlobalVariable *countVar = new GlobalVariable(ATy, false,
224 GlobalValue::InternalLinkage,
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000225 initializer, countStr,
Chris Lattner379a8d22003-04-16 20:28:45 +0000226 F.getParent());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000227
Anand Shukla11f42622002-07-08 19:36:01 +0000228 // insert initialization code in first (entry) BB
229 // this includes initializing r and count
Chris Lattner5dac64f2003-09-20 14:39:18 +0000230 insertInTopBB(&F.getEntryBlock(), numPaths, rVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000231
Anand Shukla11f42622002-07-08 19:36:01 +0000232 //now process the graph: get path numbers,
233 //get increments along different paths,
234 //and assign "increments" and "updates" (to r and count)
235 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000236 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
237 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000238
Chris Lattner62b7fd12002-04-07 20:49:59 +0000239 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000240}