blob: 4c689b12eecb87e5eaa760c2406ecfcc02405015 [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 Shukla77dca142002-09-20 16:44:35 +000032#include "llvm/Module.h"
Chris Lattner2f04a0d2003-01-14 22:33:56 +000033#include "Graph.h"
Anand Shukla21906892002-06-25 21:14:58 +000034#include <fstream>
Anand Shukla70a61382002-02-26 19:00:48 +000035
36using std::vector;
37
Chris Lattner37104aa2002-04-29 14:57:45 +000038struct ProfilePaths : public FunctionPass {
Chris Lattner7076ff22002-06-25 16:13:21 +000039 bool runOnFunction(Function &F);
Chris Lattnerd2095502002-02-26 20:04:59 +000040
41 // Before this pass, make sure that there is only one
Chris Lattner62b7fd12002-04-07 20:49:59 +000042 // entry and only one exit node for the function in the CFG of the function
Chris Lattnerd2095502002-02-26 20:04:59 +000043 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000044 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner40eb9da2002-08-08 19:01:28 +000045 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattnerd2095502002-02-26 20:04:59 +000046 }
47};
48
Chris Lattnera2c09852002-07-26 21:12:44 +000049static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
Chris Lattnerb28b6802002-07-23 18:06:35 +000050
Anand Shukla21906892002-06-25 21:14:58 +000051static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
52 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
Anand Shukla70a61382002-02-26 19:00:48 +000053 if(((*si)->getElement())==BB){
54 return *si;
55 }
56 }
57 return NULL;
58}
59
Chris Lattner62b7fd12002-04-07 20:49:59 +000060//Per function pass for inserting counters and trigger code
Chris Lattner7076ff22002-06-25 16:13:21 +000061bool ProfilePaths::runOnFunction(Function &F){
Anand Shukla21906892002-06-25 21:14:58 +000062
63 static int mn = -1;
Anand Shukla11f42622002-07-08 19:36:01 +000064
Anand Shuklafd61c602002-07-18 20:56:47 +000065 if(F.isExternal()) {
Anand Shukla11f42622002-07-08 19:36:01 +000066 return false;
67 }
68
69 //increment counter for instrumented functions. mn is now function#
70 mn++;
71
Chris Lattnerf9986852002-04-27 07:27:19 +000072 // Transform the cfg s.t. we have just one exit node
73 BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Anand Shukla21906892002-06-25 21:14:58 +000074
75 //iterating over BBs and making graph
76 std::vector<Node *> nodes;
77 std::vector<Edge> edges;
78
Anand Shukla70a61382002-02-26 19:00:48 +000079 Node *tmp;
Chris Lattnera2620ac2002-11-09 00:49:43 +000080 Node *exitNode = 0, *startNode = 0;
Anand Shukla70a61382002-02-26 19:00:48 +000081
Chris Lattnerf9986852002-04-27 07:27:19 +000082 // The nodes must be uniquesly identified:
83 // That is, no two nodes must hav same BB*
Anand Shukla70a61382002-02-26 19:00:48 +000084
Chris Lattner7076ff22002-06-25 16:13:21 +000085 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
86 Node *nd=new Node(BB);
Anand Shukla21906892002-06-25 21:14:58 +000087 nodes.push_back(nd);
Chris Lattner7076ff22002-06-25 16:13:21 +000088 if(&*BB == ExitNode)
Anand Shukla70a61382002-02-26 19:00:48 +000089 exitNode=nd;
Chris Lattner7076ff22002-06-25 16:13:21 +000090 if(&*BB==F.begin())
Anand Shukla70a61382002-02-26 19:00:48 +000091 startNode=nd;
92 }
93
Chris Lattnerf9986852002-04-27 07:27:19 +000094 // now do it againto insert edges
Chris Lattner7076ff22002-06-25 16:13:21 +000095 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
96 Node *nd=findBB(nodes, BB);
Anand Shukla70a61382002-02-26 19:00:48 +000097 assert(nd && "No node for this edge!");
Anand Shukla21906892002-06-25 21:14:58 +000098
Chris Lattner7076ff22002-06-25 16:13:21 +000099 for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB);
Anand Shukla70a61382002-02-26 19:00:48 +0000100 s!=se; ++s){
101 Node *nd2=findBB(nodes,*s);
102 assert(nd2 && "No node for this edge!");
103 Edge ed(nd,nd2,0);
Anand Shukla21906892002-06-25 21:14:58 +0000104 edges.push_back(ed);
Anand Shukla70a61382002-02-26 19:00:48 +0000105 }
106 }
107
108 Graph g(nodes,edges, startNode, exitNode);
109
Anand Shuklad9a6af02002-09-16 05:24:49 +0000110#ifdef DEBUG_PATH_PROFILES
111 std::cerr<<"Original graph\n";
112 printGraph(g);
113#endif
Anand Shukla70a61382002-02-26 19:00:48 +0000114
Anand Shukla11f42622002-07-08 19:36:01 +0000115 BasicBlock *fr = &F.front();
Anand Shukla70a61382002-02-26 19:00:48 +0000116
Anand Shukla11f42622002-07-08 19:36:01 +0000117 // The graph is made acyclic: this is done
118 // by removing back edges for now, and adding them later on
119 vector<Edge> be;
Anand Shuklafd61c602002-07-18 20:56:47 +0000120 std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
121 g.getBackEdges(be, nodePriority);
Anand Shuklad9a6af02002-09-16 05:24:49 +0000122
123#ifdef DEBUG_PATH_PROFILES
124 std::cerr<<"BackEdges-------------\n";
125 for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
126 printEdge(*VI);
127 cerr<<"\n";
128 }
129 std::cerr<<"------\n";
130#endif
Anand Shukla21906892002-06-25 21:14:58 +0000131
132#ifdef DEBUG_PATH_PROFILES
Anand Shukla11f42622002-07-08 19:36:01 +0000133 cerr<<"Backedges:"<<be.size()<<endl;
Anand Shukla21906892002-06-25 21:14:58 +0000134#endif
Anand Shukla11f42622002-07-08 19:36:01 +0000135 //Now we need to reflect the effect of back edges
136 //This is done by adding dummy edges
137 //If a->b is a back edge
138 //Then we add 2 back edges for it:
139 //1. from root->b (in vector stDummy)
140 //and 2. from a->exit (in vector exDummy)
141 vector<Edge> stDummy;
142 vector<Edge> exDummy;
143 addDummyEdges(stDummy, exDummy, g, be);
Anand Shukla21906892002-06-25 21:14:58 +0000144
Anand Shuklad9a6af02002-09-16 05:24:49 +0000145#ifdef DEBUG_PATH_PROFILES
146 std::cerr<<"After adding dummy edges\n";
147 printGraph(g);
148#endif
149
Anand Shukla11f42622002-07-08 19:36:01 +0000150 // Now, every edge in the graph is assigned a weight
151 // This weight later adds on to assign path
152 // numbers to different paths in the graph
153 // All paths for now are acyclic,
154 // since no back edges in the graph now
155 // numPaths is the number of acyclic paths in the graph
Anand Shuklad9a6af02002-09-16 05:24:49 +0000156 int numPaths=valueAssignmentToEdges(g, nodePriority, be);
Anand Shukla21906892002-06-25 21:14:58 +0000157
Anand Shuklafd61c602002-07-18 20:56:47 +0000158 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000159
160#ifdef DEBUG_PATH_PROFILES
161 printGraph(g);
162#endif
163
Anand Shukla11f42622002-07-08 19:36:01 +0000164 //create instruction allocation r and count
165 //r is the variable that'll act like an accumulator
166 //all along the path, we just add edge values to r
167 //and at the end, r reflects the path number
168 //count is an array: count[x] would store
169 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000170
Anand Shukla11f42622002-07-08 19:36:01 +0000171 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000172 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000173 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000174
Anand Shukla11f42622002-07-08 19:36:01 +0000175 Instruction *countVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000176 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000177 ConstantUInt::get(Type::UIntTy, numPaths), "Count");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000178
Anand Shukla77dca142002-09-20 16:44:35 +0000179 static GlobalVariable *threshold = NULL;
180 static bool insertedThreshold = false;
181
182 if(!insertedThreshold){
Anand Shukla12b05f32002-10-12 20:33:47 +0000183 threshold = new GlobalVariable(Type::IntTy, false, false, 0,
Anand Shukla77dca142002-09-20 16:44:35 +0000184 "reopt_threshold");
185
186 F.getParent()->getGlobalList().push_back(threshold);
187 insertedThreshold = true;
188 }
189
190 assert(threshold && "GlobalVariable threshold not defined!");
191
Anand Shukla11f42622002-07-08 19:36:01 +0000192 // insert initialization code in first (entry) BB
193 // this includes initializing r and count
Anand Shukla77dca142002-09-20 16:44:35 +0000194 insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000195
Anand Shukla11f42622002-07-08 19:36:01 +0000196 //now process the graph: get path numbers,
197 //get increments along different paths,
198 //and assign "increments" and "updates" (to r and count)
199 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000200 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
201 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000202
Chris Lattner62b7fd12002-04-07 20:49:59 +0000203 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000204}