blob: 48d5e85e6464d10f39c359654fcf159be8657a92 [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
27#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
Chris Lattner15435fd2002-05-07 19:18:48 +000028#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Anand Shukla77dca142002-09-20 16:44:35 +000029#include "llvm/Transforms/Instrumentation/Graph.h"
Anand Shukla70a61382002-02-26 19:00:48 +000030#include "llvm/Support/CFG.h"
Chris Lattnerca142372002-04-28 19:55:58 +000031#include "llvm/Constants.h"
Anand Shukla70a61382002-02-26 19:00:48 +000032#include "llvm/DerivedTypes.h"
33#include "llvm/iMemory.h"
Anand Shukla77dca142002-09-20 16:44:35 +000034#include "llvm/GlobalVariable.h"
35#include "llvm/Module.h"
Anand Shukla21906892002-06-25 21:14:58 +000036#include <iostream>
37#include <fstream>
Anand Shukla70a61382002-02-26 19:00:48 +000038
39using std::vector;
40
Chris Lattner37104aa2002-04-29 14:57:45 +000041struct ProfilePaths : public FunctionPass {
Chris Lattner7076ff22002-06-25 16:13:21 +000042 bool runOnFunction(Function &F);
Chris Lattnerd2095502002-02-26 20:04:59 +000043
44 // Before this pass, make sure that there is only one
Chris Lattner62b7fd12002-04-07 20:49:59 +000045 // entry and only one exit node for the function in the CFG of the function
Chris Lattnerd2095502002-02-26 20:04:59 +000046 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000047 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner40eb9da2002-08-08 19:01:28 +000048 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattnerd2095502002-02-26 20:04:59 +000049 }
50};
51
Chris Lattnera2c09852002-07-26 21:12:44 +000052static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
Chris Lattnerb28b6802002-07-23 18:06:35 +000053
Chris Lattnerd2095502002-02-26 20:04:59 +000054// createProfilePathsPass - Create a new pass to add path profiling
55//
56Pass *createProfilePathsPass() {
57 return new ProfilePaths();
58}
59
60
Anand Shukla21906892002-06-25 21:14:58 +000061static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
62 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
Anand Shukla70a61382002-02-26 19:00:48 +000063 if(((*si)->getElement())==BB){
64 return *si;
65 }
66 }
67 return NULL;
68}
69
Chris Lattner62b7fd12002-04-07 20:49:59 +000070//Per function pass for inserting counters and trigger code
Chris Lattner7076ff22002-06-25 16:13:21 +000071bool ProfilePaths::runOnFunction(Function &F){
Anand Shukla21906892002-06-25 21:14:58 +000072
73 static int mn = -1;
Anand Shukla11f42622002-07-08 19:36:01 +000074
Anand Shuklafd61c602002-07-18 20:56:47 +000075 if(F.isExternal()) {
Anand Shukla11f42622002-07-08 19:36:01 +000076 return false;
77 }
78
79 //increment counter for instrumented functions. mn is now function#
80 mn++;
81
Chris Lattnerf9986852002-04-27 07:27:19 +000082 // Transform the cfg s.t. we have just one exit node
83 BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Anand Shukla21906892002-06-25 21:14:58 +000084
85 //iterating over BBs and making graph
86 std::vector<Node *> nodes;
87 std::vector<Edge> edges;
88
Anand Shukla70a61382002-02-26 19:00:48 +000089 Node *tmp;
90 Node *exitNode, *startNode;
91
Chris Lattnerf9986852002-04-27 07:27:19 +000092 // The nodes must be uniquesly identified:
93 // That is, no two nodes must hav same BB*
Anand Shukla70a61382002-02-26 19:00:48 +000094
Chris Lattner7076ff22002-06-25 16:13:21 +000095 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
96 Node *nd=new Node(BB);
Anand Shukla21906892002-06-25 21:14:58 +000097 nodes.push_back(nd);
Chris Lattner7076ff22002-06-25 16:13:21 +000098 if(&*BB == ExitNode)
Anand Shukla70a61382002-02-26 19:00:48 +000099 exitNode=nd;
Chris Lattner7076ff22002-06-25 16:13:21 +0000100 if(&*BB==F.begin())
Anand Shukla70a61382002-02-26 19:00:48 +0000101 startNode=nd;
102 }
103
Chris Lattnerf9986852002-04-27 07:27:19 +0000104 // now do it againto insert edges
Chris Lattner7076ff22002-06-25 16:13:21 +0000105 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
106 Node *nd=findBB(nodes, BB);
Anand Shukla70a61382002-02-26 19:00:48 +0000107 assert(nd && "No node for this edge!");
Anand Shukla21906892002-06-25 21:14:58 +0000108
Chris Lattner7076ff22002-06-25 16:13:21 +0000109 for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB);
Anand Shukla70a61382002-02-26 19:00:48 +0000110 s!=se; ++s){
111 Node *nd2=findBB(nodes,*s);
112 assert(nd2 && "No node for this edge!");
113 Edge ed(nd,nd2,0);
Anand Shukla21906892002-06-25 21:14:58 +0000114 edges.push_back(ed);
Anand Shukla70a61382002-02-26 19:00:48 +0000115 }
116 }
117
118 Graph g(nodes,edges, startNode, exitNode);
119
Anand Shuklad9a6af02002-09-16 05:24:49 +0000120#ifdef DEBUG_PATH_PROFILES
121 std::cerr<<"Original graph\n";
122 printGraph(g);
123#endif
Anand Shukla70a61382002-02-26 19:00:48 +0000124
Anand Shukla11f42622002-07-08 19:36:01 +0000125 BasicBlock *fr = &F.front();
Anand Shukla70a61382002-02-26 19:00:48 +0000126
Anand Shukla11f42622002-07-08 19:36:01 +0000127 // The graph is made acyclic: this is done
128 // by removing back edges for now, and adding them later on
129 vector<Edge> be;
Anand Shuklafd61c602002-07-18 20:56:47 +0000130 std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
131 g.getBackEdges(be, nodePriority);
Anand Shuklad9a6af02002-09-16 05:24:49 +0000132
133#ifdef DEBUG_PATH_PROFILES
134 std::cerr<<"BackEdges-------------\n";
135 for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
136 printEdge(*VI);
137 cerr<<"\n";
138 }
139 std::cerr<<"------\n";
140#endif
Anand Shukla21906892002-06-25 21:14:58 +0000141
142#ifdef DEBUG_PATH_PROFILES
Anand Shukla11f42622002-07-08 19:36:01 +0000143 cerr<<"Backedges:"<<be.size()<<endl;
Anand Shukla21906892002-06-25 21:14:58 +0000144#endif
Anand Shukla11f42622002-07-08 19:36:01 +0000145 //Now we need to reflect the effect of back edges
146 //This is done by adding dummy edges
147 //If a->b is a back edge
148 //Then we add 2 back edges for it:
149 //1. from root->b (in vector stDummy)
150 //and 2. from a->exit (in vector exDummy)
151 vector<Edge> stDummy;
152 vector<Edge> exDummy;
153 addDummyEdges(stDummy, exDummy, g, be);
Anand Shukla21906892002-06-25 21:14:58 +0000154
Anand Shuklad9a6af02002-09-16 05:24:49 +0000155#ifdef DEBUG_PATH_PROFILES
156 std::cerr<<"After adding dummy edges\n";
157 printGraph(g);
158#endif
159
Anand Shukla11f42622002-07-08 19:36:01 +0000160 // Now, every edge in the graph is assigned a weight
161 // This weight later adds on to assign path
162 // numbers to different paths in the graph
163 // All paths for now are acyclic,
164 // since no back edges in the graph now
165 // numPaths is the number of acyclic paths in the graph
Anand Shuklad9a6af02002-09-16 05:24:49 +0000166 int numPaths=valueAssignmentToEdges(g, nodePriority, be);
Anand Shukla21906892002-06-25 21:14:58 +0000167
Anand Shuklafd61c602002-07-18 20:56:47 +0000168 if(numPaths<=1 || numPaths >5000) return false;
Anand Shuklad9a6af02002-09-16 05:24:49 +0000169
170#ifdef DEBUG_PATH_PROFILES
171 printGraph(g);
172#endif
173
Anand Shukla11f42622002-07-08 19:36:01 +0000174 //create instruction allocation r and count
175 //r is the variable that'll act like an accumulator
176 //all along the path, we just add edge values to r
177 //and at the end, r reflects the path number
178 //count is an array: count[x] would store
179 //the number of executions of path numbered x
Anand Shukla21906892002-06-25 21:14:58 +0000180
Anand Shukla11f42622002-07-08 19:36:01 +0000181 Instruction *rVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000182 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000183 ConstantUInt::get(Type::UIntTy,1),"R");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000184
Anand Shukla11f42622002-07-08 19:36:01 +0000185 Instruction *countVar=new
Anand Shuklad9a6af02002-09-16 05:24:49 +0000186 AllocaInst(Type::IntTy,
Anand Shukla11f42622002-07-08 19:36:01 +0000187 ConstantUInt::get(Type::UIntTy, numPaths), "Count");
Anand Shuklad9a6af02002-09-16 05:24:49 +0000188
Anand Shukla77dca142002-09-20 16:44:35 +0000189 static GlobalVariable *threshold = NULL;
190 static bool insertedThreshold = false;
191
192 if(!insertedThreshold){
193 threshold = new GlobalVariable(Type::IntTy, false, true, 0,
194 "reopt_threshold");
195
196 F.getParent()->getGlobalList().push_back(threshold);
197 insertedThreshold = true;
198 }
199
200 assert(threshold && "GlobalVariable threshold not defined!");
201
Anand Shukla11f42622002-07-08 19:36:01 +0000202 // insert initialization code in first (entry) BB
203 // this includes initializing r and count
Anand Shukla77dca142002-09-20 16:44:35 +0000204 insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar, threshold);
Anand Shukla70a61382002-02-26 19:00:48 +0000205
Anand Shukla11f42622002-07-08 19:36:01 +0000206 //now process the graph: get path numbers,
207 //get increments along different paths,
208 //and assign "increments" and "updates" (to r and count)
209 //"optimally". Finally, insert llvm code along various edges
Anand Shukla77dca142002-09-20 16:44:35 +0000210 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
211 threshold);
Anand Shuklafd61c602002-07-18 20:56:47 +0000212
Chris Lattner62b7fd12002-04-07 20:49:59 +0000213 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000214}