blob: aa2cfdd2189609065994ce771303af73fdde1289 [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
24// update can be collapsed into minmimum number of edges.
25//===----------------------------------------------------------------------===//
26
27#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
28#include "llvm/Transforms/UnifyMethodExitNodes.h"
29#include "llvm/Support/CFG.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000030#include "llvm/Function.h"
Anand Shukla70a61382002-02-26 19:00:48 +000031#include "llvm/BasicBlock.h"
32#include "llvm/ConstantVals.h"
33#include "llvm/DerivedTypes.h"
34#include "llvm/iMemory.h"
Chris Lattnerd2095502002-02-26 20:04:59 +000035#include "llvm/Pass.h"
Anand Shukla70a61382002-02-26 19:00:48 +000036#include "Graph.h"
37
38using std::vector;
39
Chris Lattnerd2095502002-02-26 20:04:59 +000040class ProfilePaths: public MethodPass {
41 public:
Chris Lattner62b7fd12002-04-07 20:49:59 +000042 bool runOnMethod(Function *M);
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 //
47 void ProfilePaths::getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
48 Pass::AnalysisSet &Destroyed,
49 Pass::AnalysisSet &Provided) {
50 Requires.push_back(UnifyMethodExitNodes::ID);
51 }
52};
53
54// createProfilePathsPass - Create a new pass to add path profiling
55//
56Pass *createProfilePathsPass() {
57 return new ProfilePaths();
58}
59
60
Chris Lattner5328c6f2002-02-26 19:40:28 +000061static Node *findBB(std::set<Node *> &st, BasicBlock *BB){
62 for(std::set<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
71bool ProfilePaths::runOnMethod(Function *M){
Anand Shukla70a61382002-02-26 19:00:48 +000072 //Transform the cfg s.t. we have just one exit node
73 BasicBlock *ExitNode =
74 getAnalysis<UnifyMethodExitNodes>().getExitNode();
75
76 //iterating over BBs and making graph
77 std::set<Node *> nodes;
78 std::set<Edge> edges;
79 Node *tmp;
80 Node *exitNode, *startNode;
81
82 //The nodes must be uniquesly identified:
83 //That is, no two nodes must hav same BB*
84
85 //First enter just nodes: later enter edges
Chris Lattner62b7fd12002-04-07 20:49:59 +000086 for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shukla70a61382002-02-26 19:00:48 +000087 Node *nd=new Node(*BB);
88 nodes.insert(nd);
89 if(*BB==ExitNode)
90 exitNode=nd;
91 if(*BB==M->front())
92 startNode=nd;
93 }
94
95 //now do it againto insert edges
Chris Lattner62b7fd12002-04-07 20:49:59 +000096 for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shukla70a61382002-02-26 19:00:48 +000097 Node *nd=findBB(nodes, *BB);
98 assert(nd && "No node for this edge!");
99 for(BasicBlock::succ_iterator s=succ_begin(*BB), se=succ_end(*BB);
100 s!=se; ++s){
101 Node *nd2=findBB(nodes,*s);
102 assert(nd2 && "No node for this edge!");
103 Edge ed(nd,nd2,0);
104 edges.insert(ed);
105 }
106 }
107
108 Graph g(nodes,edges, startNode, exitNode);
109
110#ifdef DEBUG_PATH_PROFILES
111 printGraph(g);
112#endif
113
114 BasicBlock *fr=M->front();
115
116 //If only one BB, don't instrument
117 if (M->getBasicBlocks().size() == 1) {
118 //The graph is made acyclic: this is done
119 //by removing back edges for now, and adding them later on
Chris Lattner5328c6f2002-02-26 19:40:28 +0000120 vector<Edge> be;
Anand Shukla70a61382002-02-26 19:00:48 +0000121 g.getBackEdges(be);
122#ifdef DEBUG_PATH_PROFILES
123 cerr<<"Backedges:"<<be.size()<<endl;
124#endif
125 //Now we need to reflect the effect of back edges
126 //This is done by adding dummy edges
127 //If a->b is a back edge
128 //Then we add 2 back edges for it:
129 //1. from root->b (in vector stDummy)
130 //and 2. from a->exit (in vector exDummy)
Chris Lattner5328c6f2002-02-26 19:40:28 +0000131 vector<Edge> stDummy;
132 vector<Edge> exDummy;
Anand Shukla70a61382002-02-26 19:00:48 +0000133 addDummyEdges(stDummy, exDummy, g, be);
134
135 //Now, every edge in the graph is assigned a weight
136 //This weight later adds on to assign path
137 //numbers to different paths in the graph
138 // All paths for now are acyclic,
139 //since no back edges in the graph now
140 //numPaths is the number of acyclic paths in the graph
141 int numPaths=valueAssignmentToEdges(g);
142
143 //create instruction allocation r and count
144 //r is the variable that'll act like an accumulator
145 //all along the path, we just add edge values to r
146 //and at the end, r reflects the path number
147 //count is an array: count[x] would store
148 //the number of executions of path numbered x
149 Instruction *rVar=new
150 AllocaInst(PointerType::get(Type::IntTy),
151 ConstantUInt::get(Type::UIntTy,1),"R");
152
153 Instruction *countVar=new
154 AllocaInst(PointerType::get(Type::IntTy),
155 ConstantUInt::get(Type::UIntTy, numPaths), "Count");
156
157 //insert initialization code in first (entry) BB
158 //this includes initializing r and count
159 insertInTopBB(M->getEntryNode(),numPaths, rVar, countVar);
160
161 //now process the graph: get path numbers,
162 //get increments along different paths,
163 //and assign "increments" and "updates" (to r and count)
164 //"optimally". Finally, insert llvm code along various edges
165 processGraph(g, rVar, countVar, be, stDummy, exDummy);
166 }
167
Chris Lattner62b7fd12002-04-07 20:49:59 +0000168 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000169}