blob: 26fa86661614d354c373e95bc077d98151362dd5 [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"
Chris Lattnerf9986852002-04-27 07:27:19 +000028#include "llvm/Transforms/UnifyFunctionExitNodes.h"
Anand Shukla70a61382002-02-26 19:00:48 +000029#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"
Chris Lattnerca142372002-04-28 19:55:58 +000032#include "llvm/Constants.h"
Anand Shukla70a61382002-02-26 19:00:48 +000033#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 Lattnerc8e66542002-04-27 06:56:12 +000040class ProfilePaths: public FunctionPass {
Chris Lattnerd2095502002-02-26 20:04:59 +000041 public:
Chris Lattnerc8e66542002-04-27 06:56:12 +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 Lattnerf9986852002-04-27 07:27:19 +000048 AU.addRequired(UnifyFunctionExitNodes::ID);
Chris Lattnerd2095502002-02-26 20:04:59 +000049 }
50};
51
52// createProfilePathsPass - Create a new pass to add path profiling
53//
54Pass *createProfilePathsPass() {
55 return new ProfilePaths();
56}
57
58
Chris Lattner5328c6f2002-02-26 19:40:28 +000059static Node *findBB(std::set<Node *> &st, BasicBlock *BB){
60 for(std::set<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 Lattnerc8e66542002-04-27 06:56:12 +000069bool ProfilePaths::runOnFunction(Function *M){
Chris Lattnerf9986852002-04-27 07:27:19 +000070 // Transform the cfg s.t. we have just one exit node
71 BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Anand Shukla70a61382002-02-26 19:00:48 +000072
Chris Lattnerf9986852002-04-27 07:27:19 +000073 // iterating over BBs and making graph
Anand Shukla70a61382002-02-26 19:00:48 +000074 std::set<Node *> nodes;
75 std::set<Edge> edges;
76 Node *tmp;
77 Node *exitNode, *startNode;
78
Chris Lattnerf9986852002-04-27 07:27:19 +000079 // The nodes must be uniquesly identified:
80 // That is, no two nodes must hav same BB*
Anand Shukla70a61382002-02-26 19:00:48 +000081
Chris Lattnerf9986852002-04-27 07:27:19 +000082 // First enter just nodes: later enter edges
Chris Lattner62b7fd12002-04-07 20:49:59 +000083 for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shukla70a61382002-02-26 19:00:48 +000084 Node *nd=new Node(*BB);
85 nodes.insert(nd);
86 if(*BB==ExitNode)
87 exitNode=nd;
88 if(*BB==M->front())
89 startNode=nd;
90 }
91
Chris Lattnerf9986852002-04-27 07:27:19 +000092 // now do it againto insert edges
Chris Lattner62b7fd12002-04-07 20:49:59 +000093 for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shukla70a61382002-02-26 19:00:48 +000094 Node *nd=findBB(nodes, *BB);
95 assert(nd && "No node for this edge!");
96 for(BasicBlock::succ_iterator s=succ_begin(*BB), se=succ_end(*BB);
97 s!=se; ++s){
98 Node *nd2=findBB(nodes,*s);
99 assert(nd2 && "No node for this edge!");
100 Edge ed(nd,nd2,0);
101 edges.insert(ed);
102 }
103 }
104
105 Graph g(nodes,edges, startNode, exitNode);
106
107#ifdef DEBUG_PATH_PROFILES
108 printGraph(g);
109#endif
110
111 BasicBlock *fr=M->front();
112
Chris Lattnerf9986852002-04-27 07:27:19 +0000113 // If only one BB, don't instrument
Anand Shukla70a61382002-02-26 19:00:48 +0000114 if (M->getBasicBlocks().size() == 1) {
Chris Lattnerf9986852002-04-27 07:27:19 +0000115 // The graph is made acyclic: this is done
116 // by removing back edges for now, and adding them later on
Chris Lattner5328c6f2002-02-26 19:40:28 +0000117 vector<Edge> be;
Anand Shukla70a61382002-02-26 19:00:48 +0000118 g.getBackEdges(be);
119#ifdef DEBUG_PATH_PROFILES
120 cerr<<"Backedges:"<<be.size()<<endl;
121#endif
Chris Lattnerf9986852002-04-27 07:27:19 +0000122 // Now we need to reflect the effect of back edges
123 // This is done by adding dummy edges
124 // If a->b is a back edge
125 // Then we add 2 back edges for it:
126 // 1. from root->b (in vector stDummy)
127 // and 2. from a->exit (in vector exDummy)
Chris Lattner5328c6f2002-02-26 19:40:28 +0000128 vector<Edge> stDummy;
129 vector<Edge> exDummy;
Anand Shukla70a61382002-02-26 19:00:48 +0000130 addDummyEdges(stDummy, exDummy, g, be);
131
Chris Lattnerf9986852002-04-27 07:27:19 +0000132 // Now, every edge in the graph is assigned a weight
133 // This weight later adds on to assign path
134 // numbers to different paths in the graph
135 // All paths for now are acyclic,
136 // since no back edges in the graph now
137 // numPaths is the number of acyclic paths in the graph
Anand Shukla70a61382002-02-26 19:00:48 +0000138 int numPaths=valueAssignmentToEdges(g);
139
Chris Lattnerf9986852002-04-27 07:27:19 +0000140 // create instruction allocation r and count
141 // r is the variable that'll act like an accumulator
142 // all along the path, we just add edge values to r
143 // and at the end, r reflects the path number
144 // count is an array: count[x] would store
145 // the number of executions of path numbered x
Anand Shukla70a61382002-02-26 19:00:48 +0000146 Instruction *rVar=new
147 AllocaInst(PointerType::get(Type::IntTy),
148 ConstantUInt::get(Type::UIntTy,1),"R");
149
150 Instruction *countVar=new
151 AllocaInst(PointerType::get(Type::IntTy),
152 ConstantUInt::get(Type::UIntTy, numPaths), "Count");
153
Chris Lattnerf9986852002-04-27 07:27:19 +0000154 // insert initialization code in first (entry) BB
155 // this includes initializing r and count
Anand Shukla70a61382002-02-26 19:00:48 +0000156 insertInTopBB(M->getEntryNode(),numPaths, rVar, countVar);
157
Chris Lattnerf9986852002-04-27 07:27:19 +0000158 // now process the graph: get path numbers,
159 // get increments along different paths,
160 // and assign "increments" and "updates" (to r and count)
161 // "optimally". Finally, insert llvm code along various edges
Anand Shukla70a61382002-02-26 19:00:48 +0000162 processGraph(g, rVar, countVar, be, stDummy, exDummy);
163 }
164
Chris Lattner62b7fd12002-04-07 20:49:59 +0000165 return true; // Always modifies function
Anand Shukla70a61382002-02-26 19:00:48 +0000166}