blob: d4973be25eb5c533eadd8cdba2d32b5d4ebd30d1 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- ProfilePaths.cpp - interface to insert instrumentation --*- C++ -*-===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Anand Shukla70a61382002-02-26 19:00:48 +00009//
Chris Lattner44d2c352003-10-13 03:32:08 +000010// This inserts instrumentation for counting execution of paths though a given
11// function Its implemented as a "Function" Pass, and called using opt
Anand Shukla70a61382002-02-26 19:00:48 +000012//
13// This pass is implemented by using algorithms similar to
14// 1."Efficient Path Profiling": Ball, T. and Larus, J. R.,
Chris Lattner44d2c352003-10-13 03:32:08 +000015// Proceedings of Micro-29, Dec 1996, Paris, France.
Anand Shukla70a61382002-02-26 19:00:48 +000016// 2."Efficiently Counting Program events with support for on-line
17// "queries": Ball T., ACM Transactions on Programming Languages
Chris Lattner44d2c352003-10-13 03:32:08 +000018// and systems, Sep 1994.
Anand Shukla70a61382002-02-26 19:00:48 +000019//
Chris Lattner44d2c352003-10-13 03:32:08 +000020// The algorithms work on a Graph constructed over the nodes made from Basic
21// Blocks: The transformations then take place on the constructed graph
22// (implementation in Graph.cpp and GraphAuxiliary.cpp) and finally, appropriate
23// instrumentation is placed over suitable edges. (code inserted through
24// EdgeCode.cpp).
Anand Shukla70a61382002-02-26 19:00:48 +000025//
Chris Lattner44d2c352003-10-13 03:32:08 +000026// The algorithm inserts code such that every acyclic path in the CFG of a
27// function is identified through a unique number. the code insertion is optimal
28// in the sense that its inserted over a minimal set of edges. Also, the
29// algorithm makes sure than initialization, path increment and counter update
30// can be collapsed into minimum number of edges.
31//
Anand Shukla70a61382002-02-26 19:00:48 +000032//===----------------------------------------------------------------------===//
33
Chris Lattner15435fd2002-05-07 19:18:48 +000034#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Anand Shukla70a61382002-02-26 19:00:48 +000035#include "llvm/Support/CFG.h"
Chris Lattnerca142372002-04-28 19:55:58 +000036#include "llvm/Constants.h"
Anand Shukla70a61382002-02-26 19:00:48 +000037#include "llvm/DerivedTypes.h"
38#include "llvm/iMemory.h"
Anand Shukla2fd8edd2003-06-05 06:02:46 +000039#include "llvm/iOperators.h"
40#include "llvm/iOther.h"
Anand Shukla77dca142002-09-20 16:44:35 +000041#include "llvm/Module.h"
Chris Lattner2f04a0d2003-01-14 22:33:56 +000042#include "Graph.h"
Anand Shukla21906892002-06-25 21:14:58 +000043#include <fstream>
Brian Gaekeb8a4ed62003-10-10 18:46:52 +000044#include <cstdio>
Anand Shukla70a61382002-02-26 19:00:48 +000045
Chris Lattner37104aa2002-04-29 14:57:45 +000046struct ProfilePaths : public FunctionPass {
Chris Lattner7076ff22002-06-25 16:13:21 +000047 bool runOnFunction(Function &F);
Chris Lattnerd2095502002-02-26 20:04:59 +000048
49 // Before this pass, make sure that there is only one
Chris Lattner62b7fd12002-04-07 20:49:59 +000050 // entry and only one exit node for the function in the CFG of the function
Chris Lattnerd2095502002-02-26 20:04:59 +000051 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000052 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner40eb9da2002-08-08 19:01:28 +000053 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattnerd2095502002-02-26 20:04:59 +000054 }
55};
56
Chris Lattnera2c09852002-07-26 21:12:44 +000057static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
Chris Lattnerb28b6802002-07-23 18:06:35 +000058
Anand Shukla21906892002-06-25 21:14:58 +000059static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
60 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
Anand Shukla70a61382002-02-26 19:00:48 +000061 if(((*si)->getElement())==BB){
62 return *si;
63 }
64 }
65 return NULL;
66}
67
Chris Lattner62b7fd12002-04-07 20:49:59 +000068//Per function pass for inserting counters and trigger code
Chris Lattner7076ff22002-06-25 16:13:21 +000069bool ProfilePaths::runOnFunction(Function &F){
Anand Shukla21906892002-06-25 21:14:58 +000070
71 static int mn = -1;
Anand Shukla2fd8edd2003-06-05 06:02:46 +000072 static int CountCounter = 1;
Anand Shuklafd61c602002-07-18 20:56:47 +000073 if(F.isExternal()) {
Anand Shukla11f42622002-07-08 19:36:01 +000074 return false;
75 }
76
77 //increment counter for instrumented functions. mn is now function#
78 mn++;
79
Chris Lattnerf9986852002-04-27 07:27:19 +000080 // Transform the cfg s.t. we have just one exit node
Chris Lattner10a032a2003-09-10 20:35:33 +000081 BasicBlock *ExitNode =
82 getAnalysis<UnifyFunctionExitNodes>().getReturnBlock();
Anand Shukla21906892002-06-25 21:14:58 +000083
84 //iterating over BBs and making graph
85 std::vector<Node *> nodes;
86 std::vector<Edge> edges;
87
Anand Shukla70a61382002-02-26 19:00:48 +000088 Node *tmp;
Chris Lattnera2620ac2002-11-09 00:49:43 +000089 Node *exitNode = 0, *startNode = 0;
Anand Shukla70a61382002-02-26 19:00:48 +000090
Misha Brukman8b2bd4e2003-10-10 17:57:28 +000091 // The nodes must be uniquely identified:
Chris Lattnerf9986852002-04-27 07:27:19 +000092 // That is, no two nodes must hav same BB*
Anand Shukla70a61382002-02-26 19:00:48 +000093
Chris Lattner7076ff22002-06-25 16:13:21 +000094 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
95 Node *nd=new Node(BB);
Anand Shukla21906892002-06-25 21:14:58 +000096 nodes.push_back(nd);
Chris Lattner7076ff22002-06-25 16:13:21 +000097 if(&*BB == ExitNode)
Anand Shukla70a61382002-02-26 19:00:48 +000098 exitNode=nd;
Chris Lattner889f6202003-04-23 16:37:45 +000099 if(BB==F.begin())
Anand Shukla70a61382002-02-26 19:00:48 +0000100 startNode=nd;
101 }
102
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000103 // now do it again to insert edges
Chris Lattner7076ff22002-06-25 16:13:21 +0000104 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
105 Node *nd=findBB(nodes, BB);
Anand Shukla70a61382002-02-26 19:00:48 +0000106 assert(nd && "No node for this edge!");
Anand Shukla21906892002-06-25 21:14:58 +0000107
Chris Lattnera9400952003-09-24 22:06:25 +0000108 for(succ_iterator s=succ_begin(BB), se=succ_end(BB); s!=se; ++s){
Anand Shukla70a61382002-02-26 19:00:48 +0000109 Node *nd2=findBB(nodes,*s);
110 assert(nd2 && "No node for this edge!");
111 Edge ed(nd,nd2,0);
Anand Shukla21906892002-06-25 21:14:58 +0000112 edges.push_back(ed);
Anand Shukla70a61382002-02-26 19:00:48 +0000113 }
114 }
115
116 Graph g(nodes,edges, startNode, exitNode);
117
Anand Shuklad9a6af02002-09-16 05:24:49 +0000118#ifdef DEBUG_PATH_PROFILES
119 std::cerr<<"Original graph\n";
120 printGraph(g);
121#endif
Anand Shukla70a61382002-02-26 19:00:48 +0000122
Anand Shukla11f42622002-07-08 19:36:01 +0000123 BasicBlock *fr = &F.front();
Anand Shukla70a61382002-02-26 19:00:48 +0000124
Anand Shukla11f42622002-07-08 19:36:01 +0000125 // The graph is made acyclic: this is done
126 // by removing back edges for now, and adding them later on
Chris Lattner10a032a2003-09-10 20:35:33 +0000127 std::vector<Edge> be;
Anand Shuklafd61c602002-07-18 20:56:47 +0000128 std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
129 g.getBackEdges(be, nodePriority);
Anand Shuklad9a6af02002-09-16 05:24:49 +0000130
131#ifdef DEBUG_PATH_PROFILES
132 std::cerr<<"BackEdges-------------\n";
Chris Lattner10a032a2003-09-10 20:35:33 +0000133 for (std::vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
Anand Shuklad9a6af02002-09-16 05:24:49 +0000134 printEdge(*VI);
135 cerr<<"\n";
136 }
137 std::cerr<<"------\n";
138#endif
Anand Shukla21906892002-06-25 21:14:58 +0000139
140#ifdef DEBUG_PATH_PROFILES
Anand Shukla11f42622002-07-08 19:36:01 +0000141 cerr<<"Backedges:"<<be.size()<<endl;
Anand Shukla21906892002-06-25 21:14:58 +0000142#endif
Anand Shukla11f42622002-07-08 19:36:01 +0000143 //Now we need to reflect the effect of back edges
144 //This is done by adding dummy edges
145 //If a->b is a back edge
146 //Then we add 2 back edges for it:
147 //1. from root->b (in vector stDummy)
148 //and 2. from a->exit (in vector exDummy)
Chris Lattner10a032a2003-09-10 20:35:33 +0000149 std::vector<Edge> stDummy;
150 std::vector<Edge> exDummy;
Anand Shukla11f42622002-07-08 19:36:01 +0000151 addDummyEdges(stDummy, exDummy, g, be);
Anand Shukla21906892002-06-25 21:14:58 +0000152
Anand Shuklad9a6af02002-09-16 05:24:49 +0000153#ifdef DEBUG_PATH_PROFILES
154 std::cerr<<"After adding dummy edges\n";
155 printGraph(g);
156#endif
157
Anand Shukla11f42622002-07-08 19:36:01 +0000158 // Now, every edge in the graph is assigned a weight
159 // This weight later adds on to assign path
160 // numbers to different paths in the graph
161 // All paths for now are acyclic,
162 // since no back edges in the graph now
163 // numPaths is the number of acyclic paths in the graph
Anand Shuklad9a6af02002-09-16 05:24:49 +0000164 int numPaths=valueAssignmentToEdges(g, nodePriority, be);
Anand Shukla21906892002-06-25 21:14:58 +0000165
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000166 //if(numPaths<=1) return false;
167
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000168 static GlobalVariable *threshold = NULL;
169 static bool insertedThreshold = false;
170
171 if(!insertedThreshold){
172 threshold = new GlobalVariable(Type::IntTy, false,
173 GlobalValue::ExternalLinkage, 0,
174 "reopt_threshold");
175
176 F.getParent()->getGlobalList().push_back(threshold);
177 insertedThreshold = true;
178 }
179
180 assert(threshold && "GlobalVariable threshold not defined!");
181
182
183 if(fr->getParent()->getName() == "main"){
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000184 //initialize threshold
Chris Lattner25bc3f82003-08-31 00:21:59 +0000185
186 // FIXME: THIS IS HORRIBLY BROKEN. FUNCTION PASSES CANNOT DO THIS, EXCEPT
187 // IN THEIR INITIALIZE METHOD!!
188 Function *initialize =
189 F.getParent()->getOrInsertFunction("reoptimizerInitialize", Type::VoidTy,
190 PointerType::get(Type::IntTy), 0);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000191
Chris Lattner10a032a2003-09-10 20:35:33 +0000192 std::vector<Value *> trargs;
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000193 trargs.push_back(threshold);
Chris Lattner25bc3f82003-08-31 00:21:59 +0000194 new CallInst(initialize, trargs, "", fr->begin());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000195 }
196
197
Anand Shuklafd61c602002-07-18 20:56:47 +0000198 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000199
200#ifdef DEBUG_PATH_PROFILES
201 printGraph(g);
202#endif
203
Anand Shukla11f42622002-07-08 19:36:01 +0000204 //create instruction allocation r and count
205 //r is the variable that'll act like an accumulator
206 //all along the path, we just add edge values to r
207 //and at the end, r reflects the path number
208 //count is an array: count[x] would store
209 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000210
Anand Shukla11f42622002-07-08 19:36:01 +0000211 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000212 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000213 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000214
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000215 //Instruction *countVar=new
216 //AllocaInst(Type::IntTy,
217 // ConstantUInt::get(Type::UIntTy, numPaths), "Count");
218
219 //initialize counter array!
220 std::vector<Constant*> arrayInitialize;
221 for(int xi=0; xi<numPaths; xi++)
222 arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
223
Chris Lattner379a8d22003-04-16 20:28:45 +0000224 const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
225 Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000226 char tempChar[20];
227 sprintf(tempChar, "Count%d", CountCounter);
228 CountCounter++;
229 std::string countStr = tempChar;
Chris Lattner379a8d22003-04-16 20:28:45 +0000230 GlobalVariable *countVar = new GlobalVariable(ATy, false,
231 GlobalValue::InternalLinkage,
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000232 initializer, countStr,
Chris Lattner379a8d22003-04-16 20:28:45 +0000233 F.getParent());
Anand Shukla2fd8edd2003-06-05 06:02:46 +0000234
Anand Shukla11f42622002-07-08 19:36:01 +0000235 // insert initialization code in first (entry) BB
236 // this includes initializing r and count
Chris Lattner5dac64f2003-09-20 14:39:18 +0000237 insertInTopBB(&F.getEntryBlock(), numPaths, rVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000238
Anand Shukla11f42622002-07-08 19:36:01 +0000239 //now process the graph: get path numbers,
240 //get increments along different paths,
241 //and assign "increments" and "updates" (to r and count)
242 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000243 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
244 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000245
Chris Lattner62b7fd12002-04-07 20:49:59 +0000246 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000247}