blob: 138f58238a1ea2af1434a10f2d895d77405a1247 [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>
Anand Shukla2fd8edd2003-06-05 06:02:46 +000037#include <stdio.h>
Anand Shukla70a61382002-02-26 19:00:48 +000038using std::vector;
39
Chris Lattner37104aa2002-04-29 14:57:45 +000040struct ProfilePaths : public FunctionPass {
Chris Lattner7076ff22002-06-25 16:13:21 +000041 bool runOnFunction(Function &F);
Chris Lattnerd2095502002-02-26 20:04:59 +000042
43 // Before this pass, make sure that there is only one
Chris Lattner62b7fd12002-04-07 20:49:59 +000044 // entry and only one exit node for the function in the CFG of the function
Chris Lattnerd2095502002-02-26 20:04:59 +000045 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000046 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner40eb9da2002-08-08 19:01:28 +000047 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattnerd2095502002-02-26 20:04:59 +000048 }
49};
50
Chris Lattnera2c09852002-07-26 21:12:44 +000051static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
Chris Lattnerb28b6802002-07-23 18:06:35 +000052
Anand Shukla21906892002-06-25 21:14:58 +000053static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
54 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
Anand Shukla70a61382002-02-26 19:00:48 +000055 if(((*si)->getElement())==BB){
56 return *si;
57 }
58 }
59 return NULL;
60}
61
Chris Lattner62b7fd12002-04-07 20:49:59 +000062//Per function pass for inserting counters and trigger code
Chris Lattner7076ff22002-06-25 16:13:21 +000063bool ProfilePaths::runOnFunction(Function &F){
Anand Shukla21906892002-06-25 21:14:58 +000064
65 static int mn = -1;
Anand Shukla2fd8edd2003-06-05 06:02:46 +000066 static int CountCounter = 1;
Anand Shuklafd61c602002-07-18 20:56:47 +000067 if(F.isExternal()) {
Anand Shukla11f42622002-07-08 19:36:01 +000068 return false;
69 }
70
71 //increment counter for instrumented functions. mn is now function#
72 mn++;
73
Chris Lattnerf9986852002-04-27 07:27:19 +000074 // Transform the cfg s.t. we have just one exit node
75 BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
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
121 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";
127 for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
128 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)
143 vector<Edge> stDummy;
144 vector<Edge> exDummy;
145 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
179 vector<const Type*> initialize_args;
180 initialize_args.push_back(PointerType::get(Type::IntTy));
181
182 const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
183 false);
184 Function *initialMeth = fr->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
185 assert(initialMeth && "Initialize method could not be inserted!");
186
187 vector<Value *> trargs;
188 trargs.push_back(threshold);
189
190 new CallInst(initialMeth, trargs, "", fr->begin());
191 }
192
193
Anand Shuklafd61c602002-07-18 20:56:47 +0000194 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000195
196#ifdef DEBUG_PATH_PROFILES
197 printGraph(g);
198#endif
199
Anand Shukla11f42622002-07-08 19:36:01 +0000200 //create instruction allocation r and count
201 //r is the variable that'll act like an accumulator
202 //all along the path, we just add edge values to r
203 //and at the end, r reflects the path number
204 //count is an array: count[x] would store
205 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000206
Anand Shukla11f42622002-07-08 19:36:01 +0000207 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000208 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000209 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000210
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000211 //Instruction *countVar=new
212 //AllocaInst(Type::IntTy,
213 // ConstantUInt::get(Type::UIntTy, numPaths), "Count");
214
215 //initialize counter array!
216 std::vector<Constant*> arrayInitialize;
217 for(int xi=0; xi<numPaths; xi++)
218 arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
219
Chris Lattner379a8d22003-04-16 20:28:45 +0000220 const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
221 Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000222 char tempChar[20];
223 sprintf(tempChar, "Count%d", CountCounter);
224 CountCounter++;
225 std::string countStr = tempChar;
Chris Lattner379a8d22003-04-16 20:28:45 +0000226 GlobalVariable *countVar = new GlobalVariable(ATy, false,
227 GlobalValue::InternalLinkage,
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000228 initializer, countStr,
Chris Lattner379a8d22003-04-16 20:28:45 +0000229 F.getParent());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000230
Anand Shukla11f42622002-07-08 19:36:01 +0000231 // insert initialization code in first (entry) BB
232 // this includes initializing r and count
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000233 insertInTopBB(&F.getEntryNode(),numPaths, rVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000234
Anand Shukla11f42622002-07-08 19:36:01 +0000235 //now process the graph: get path numbers,
236 //get increments along different paths,
237 //and assign "increments" and "updates" (to r and count)
238 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000239 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
240 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000241
Chris Lattner62b7fd12002-04-07 20:49:59 +0000242 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000243}