2002-02-26 19:00:48 +00:00
|
|
|
//===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This inserts intrumentation for counting
|
2002-04-07 20:49:59 +00:00
|
|
|
// execution of paths though a given function
|
|
|
|
// Its implemented as a "Function" Pass, and called using opt
|
2002-02-26 19:00:48 +00:00
|
|
|
//
|
|
|
|
// This pass is implemented by using algorithms similar to
|
|
|
|
// 1."Efficient Path Profiling": Ball, T. and Larus, J. R.,
|
|
|
|
// Proceedings of Micro-29, Dec 1996, Paris, France.
|
|
|
|
// 2."Efficiently Counting Program events with support for on-line
|
|
|
|
// "queries": Ball T., ACM Transactions on Programming Languages
|
|
|
|
// and systems, Sep 1994.
|
|
|
|
//
|
|
|
|
// The algorithms work on a Graph constructed over the nodes
|
|
|
|
// made from Basic Blocks: The transformations then take place on
|
|
|
|
// the constucted graph (implementation in Graph.cpp and GraphAuxillary.cpp)
|
|
|
|
// and finally, appropriate instrumentation is placed over suitable edges.
|
|
|
|
// (code inserted through EdgeCode.cpp).
|
|
|
|
//
|
|
|
|
// The algorithm inserts code such that every acyclic path in the CFG
|
2002-04-07 20:49:59 +00:00
|
|
|
// of a function is identified through a unique number. the code insertion
|
2002-02-26 19:00:48 +00:00
|
|
|
// is optimal in the sense that its inserted over a minimal set of edges. Also,
|
|
|
|
// the algorithm makes sure than initialization, path increment and counter
|
2002-06-25 21:14:58 +00:00
|
|
|
// update can be collapsed into minimum number of edges.
|
2002-02-26 19:00:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-07 19:18:48 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2002-02-26 19:00:48 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-02-26 19:00:48 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/iMemory.h"
|
2003-06-05 06:02:46 +00:00
|
|
|
#include "llvm/iOperators.h"
|
|
|
|
#include "llvm/iOther.h"
|
2002-09-20 16:44:35 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-01-14 22:33:56 +00:00
|
|
|
#include "Graph.h"
|
2002-06-25 21:14:58 +00:00
|
|
|
#include <fstream>
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/stdio.h"
|
2002-02-26 19:00:48 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2002-04-29 14:57:45 +00:00
|
|
|
struct ProfilePaths : public FunctionPass {
|
2002-06-25 16:13:21 +00:00
|
|
|
bool runOnFunction(Function &F);
|
2002-02-26 20:04:59 +00:00
|
|
|
|
|
|
|
// Before this pass, make sure that there is only one
|
2002-04-07 20:49:59 +00:00
|
|
|
// entry and only one exit node for the function in the CFG of the function
|
2002-02-26 20:04:59 +00:00
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:28 +00:00
|
|
|
AU.addRequired<UnifyFunctionExitNodes>();
|
2002-02-26 20:04:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-07-26 21:12:44 +00:00
|
|
|
static RegisterOpt<ProfilePaths> X("paths", "Profile Paths");
|
2002-07-23 18:06:35 +00:00
|
|
|
|
2002-06-25 21:14:58 +00:00
|
|
|
static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
|
|
|
|
for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
|
2002-02-26 19:00:48 +00:00
|
|
|
if(((*si)->getElement())==BB){
|
|
|
|
return *si;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
//Per function pass for inserting counters and trigger code
|
2002-06-25 16:13:21 +00:00
|
|
|
bool ProfilePaths::runOnFunction(Function &F){
|
2002-06-25 21:14:58 +00:00
|
|
|
|
|
|
|
static int mn = -1;
|
2003-06-05 06:02:46 +00:00
|
|
|
static int CountCounter = 1;
|
2002-07-18 20:56:47 +00:00
|
|
|
if(F.isExternal()) {
|
2002-07-08 19:36:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//increment counter for instrumented functions. mn is now function#
|
|
|
|
mn++;
|
|
|
|
|
2002-04-27 07:27:19 +00:00
|
|
|
// Transform the cfg s.t. we have just one exit node
|
|
|
|
BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
|
2002-06-25 21:14:58 +00:00
|
|
|
|
|
|
|
//iterating over BBs and making graph
|
|
|
|
std::vector<Node *> nodes;
|
|
|
|
std::vector<Edge> edges;
|
|
|
|
|
2002-02-26 19:00:48 +00:00
|
|
|
Node *tmp;
|
2002-11-09 00:49:43 +00:00
|
|
|
Node *exitNode = 0, *startNode = 0;
|
2002-02-26 19:00:48 +00:00
|
|
|
|
2002-04-27 07:27:19 +00:00
|
|
|
// The nodes must be uniquesly identified:
|
|
|
|
// That is, no two nodes must hav same BB*
|
2002-02-26 19:00:48 +00:00
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
|
|
|
|
Node *nd=new Node(BB);
|
2002-06-25 21:14:58 +00:00
|
|
|
nodes.push_back(nd);
|
2002-06-25 16:13:21 +00:00
|
|
|
if(&*BB == ExitNode)
|
2002-02-26 19:00:48 +00:00
|
|
|
exitNode=nd;
|
2003-04-23 16:37:45 +00:00
|
|
|
if(BB==F.begin())
|
2002-02-26 19:00:48 +00:00
|
|
|
startNode=nd;
|
|
|
|
}
|
|
|
|
|
2002-04-27 07:27:19 +00:00
|
|
|
// now do it againto insert edges
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
|
|
|
|
Node *nd=findBB(nodes, BB);
|
2002-02-26 19:00:48 +00:00
|
|
|
assert(nd && "No node for this edge!");
|
2002-06-25 21:14:58 +00:00
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB);
|
2002-02-26 19:00:48 +00:00
|
|
|
s!=se; ++s){
|
|
|
|
Node *nd2=findBB(nodes,*s);
|
|
|
|
assert(nd2 && "No node for this edge!");
|
|
|
|
Edge ed(nd,nd2,0);
|
2002-06-25 21:14:58 +00:00
|
|
|
edges.push_back(ed);
|
2002-02-26 19:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Graph g(nodes,edges, startNode, exitNode);
|
|
|
|
|
2002-09-16 05:24:49 +00:00
|
|
|
#ifdef DEBUG_PATH_PROFILES
|
|
|
|
std::cerr<<"Original graph\n";
|
|
|
|
printGraph(g);
|
|
|
|
#endif
|
2002-02-26 19:00:48 +00:00
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
BasicBlock *fr = &F.front();
|
2002-02-26 19:00:48 +00:00
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
// The graph is made acyclic: this is done
|
|
|
|
// by removing back edges for now, and adding them later on
|
|
|
|
vector<Edge> be;
|
2002-07-18 20:56:47 +00:00
|
|
|
std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
|
|
|
|
g.getBackEdges(be, nodePriority);
|
2002-09-16 05:24:49 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PATH_PROFILES
|
|
|
|
std::cerr<<"BackEdges-------------\n";
|
|
|
|
for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
|
|
|
|
printEdge(*VI);
|
|
|
|
cerr<<"\n";
|
|
|
|
}
|
|
|
|
std::cerr<<"------\n";
|
|
|
|
#endif
|
2002-06-25 21:14:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PATH_PROFILES
|
2002-07-08 19:36:01 +00:00
|
|
|
cerr<<"Backedges:"<<be.size()<<endl;
|
2002-06-25 21:14:58 +00:00
|
|
|
#endif
|
2002-07-08 19:36:01 +00:00
|
|
|
//Now we need to reflect the effect of back edges
|
|
|
|
//This is done by adding dummy edges
|
|
|
|
//If a->b is a back edge
|
|
|
|
//Then we add 2 back edges for it:
|
|
|
|
//1. from root->b (in vector stDummy)
|
|
|
|
//and 2. from a->exit (in vector exDummy)
|
|
|
|
vector<Edge> stDummy;
|
|
|
|
vector<Edge> exDummy;
|
|
|
|
addDummyEdges(stDummy, exDummy, g, be);
|
|
|
|
|
2002-09-16 05:24:49 +00:00
|
|
|
#ifdef DEBUG_PATH_PROFILES
|
|
|
|
std::cerr<<"After adding dummy edges\n";
|
|
|
|
printGraph(g);
|
|
|
|
#endif
|
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
// Now, every edge in the graph is assigned a weight
|
|
|
|
// This weight later adds on to assign path
|
|
|
|
// numbers to different paths in the graph
|
|
|
|
// All paths for now are acyclic,
|
|
|
|
// since no back edges in the graph now
|
|
|
|
// numPaths is the number of acyclic paths in the graph
|
2002-09-16 05:24:49 +00:00
|
|
|
int numPaths=valueAssignmentToEdges(g, nodePriority, be);
|
2002-07-08 19:36:01 +00:00
|
|
|
|
2003-02-14 20:41:53 +00:00
|
|
|
//if(numPaths<=1) return false;
|
|
|
|
|
2003-06-05 06:02:46 +00:00
|
|
|
static GlobalVariable *threshold = NULL;
|
|
|
|
static bool insertedThreshold = false;
|
|
|
|
|
|
|
|
if(!insertedThreshold){
|
|
|
|
threshold = new GlobalVariable(Type::IntTy, false,
|
|
|
|
GlobalValue::ExternalLinkage, 0,
|
|
|
|
"reopt_threshold");
|
|
|
|
|
|
|
|
F.getParent()->getGlobalList().push_back(threshold);
|
|
|
|
insertedThreshold = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(threshold && "GlobalVariable threshold not defined!");
|
|
|
|
|
|
|
|
|
|
|
|
if(fr->getParent()->getName() == "main"){
|
|
|
|
//intialize threshold
|
|
|
|
vector<const Type*> initialize_args;
|
|
|
|
initialize_args.push_back(PointerType::get(Type::IntTy));
|
|
|
|
|
|
|
|
const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
|
|
|
|
false);
|
|
|
|
Function *initialMeth = fr->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
|
|
|
|
assert(initialMeth && "Initialize method could not be inserted!");
|
|
|
|
|
|
|
|
vector<Value *> trargs;
|
|
|
|
trargs.push_back(threshold);
|
|
|
|
|
|
|
|
new CallInst(initialMeth, trargs, "", fr->begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 20:56:47 +00:00
|
|
|
if(numPaths<=1 || numPaths >5000) return false;
|
2002-09-16 05:24:49 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PATH_PROFILES
|
|
|
|
printGraph(g);
|
|
|
|
#endif
|
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
//create instruction allocation r and count
|
|
|
|
//r is the variable that'll act like an accumulator
|
|
|
|
//all along the path, we just add edge values to r
|
|
|
|
//and at the end, r reflects the path number
|
|
|
|
//count is an array: count[x] would store
|
|
|
|
//the number of executions of path numbered x
|
|
|
|
|
|
|
|
Instruction *rVar=new
|
2002-09-16 05:24:49 +00:00
|
|
|
AllocaInst(Type::IntTy,
|
2002-07-08 19:36:01 +00:00
|
|
|
ConstantUInt::get(Type::UIntTy,1),"R");
|
2002-09-16 05:24:49 +00:00
|
|
|
|
2003-02-14 20:41:53 +00:00
|
|
|
//Instruction *countVar=new
|
|
|
|
//AllocaInst(Type::IntTy,
|
|
|
|
// ConstantUInt::get(Type::UIntTy, numPaths), "Count");
|
|
|
|
|
|
|
|
//initialize counter array!
|
|
|
|
std::vector<Constant*> arrayInitialize;
|
|
|
|
for(int xi=0; xi<numPaths; xi++)
|
|
|
|
arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
|
|
|
|
|
2003-04-16 20:28:45 +00:00
|
|
|
const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
|
|
|
|
Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
|
2003-06-05 06:02:46 +00:00
|
|
|
char tempChar[20];
|
|
|
|
sprintf(tempChar, "Count%d", CountCounter);
|
|
|
|
CountCounter++;
|
|
|
|
std::string countStr = tempChar;
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable *countVar = new GlobalVariable(ATy, false,
|
|
|
|
GlobalValue::InternalLinkage,
|
2003-06-05 06:02:46 +00:00
|
|
|
initializer, countStr,
|
2003-04-16 20:28:45 +00:00
|
|
|
F.getParent());
|
2003-06-05 06:02:46 +00:00
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
// insert initialization code in first (entry) BB
|
|
|
|
// this includes initializing r and count
|
2003-02-14 20:41:53 +00:00
|
|
|
insertInTopBB(&F.getEntryNode(),numPaths, rVar, threshold);
|
2002-02-26 19:00:48 +00:00
|
|
|
|
2002-07-08 19:36:01 +00:00
|
|
|
//now process the graph: get path numbers,
|
|
|
|
//get increments along different paths,
|
|
|
|
//and assign "increments" and "updates" (to r and count)
|
|
|
|
//"optimally". Finally, insert llvm code along various edges
|
2002-09-20 16:44:35 +00:00
|
|
|
processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn,
|
|
|
|
threshold);
|
2002-07-18 20:56:47 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
return true; // Always modifies function
|
2002-02-26 19:00:48 +00:00
|
|
|
}
|