2003-10-13 03:32:08 +00:00
|
|
|
//===-- InstLoops.cpp -----------------------------------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-13 03:32:08 +00:00
|
|
|
//
|
2004-05-14 21:21:52 +00:00
|
|
|
// This is the first-level instrumentation pass for the Reoptimizer. It
|
|
|
|
// instrument the back-edges of loops by inserting a basic block
|
|
|
|
// containing a call to llvm_first_trigger (the first-level trigger function),
|
|
|
|
// and inserts an initialization call to the main() function.
|
2003-10-13 03:32:08 +00:00
|
|
|
//
|
2002-11-03 01:45:20 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-07-10 21:55:57 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2002-11-03 01:45:20 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2002-11-03 01:45:20 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Type.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2005-01-07 05:42:13 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2004-05-14 21:21:52 +00:00
|
|
|
#include "../ProfilingUtils.h"
|
2002-11-03 01:45:20 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-11-03 01:45:20 +00:00
|
|
|
//this is used to color vertices
|
|
|
|
//during DFS
|
|
|
|
|
|
|
|
enum Color{
|
|
|
|
WHITE,
|
|
|
|
GREY,
|
|
|
|
BLACK
|
|
|
|
};
|
|
|
|
|
2003-10-13 03:32:08 +00:00
|
|
|
namespace {
|
2003-08-12 22:00:24 +00:00
|
|
|
typedef std::map<BasicBlock *, BasicBlock *> BBMap;
|
2003-07-10 21:55:57 +00:00
|
|
|
struct InstLoops : public FunctionPass {
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<DominatorSet>();
|
|
|
|
}
|
|
|
|
private:
|
2004-05-14 21:21:52 +00:00
|
|
|
Function *inCountMth;
|
2003-07-10 21:55:57 +00:00
|
|
|
DominatorSet *DS;
|
|
|
|
void getBackEdgesVisit(BasicBlock *u,
|
|
|
|
std::map<BasicBlock *, Color > &color,
|
2005-04-21 23:48:37 +00:00
|
|
|
std::map<BasicBlock *, int > &d,
|
2003-08-12 22:00:24 +00:00
|
|
|
int &time, BBMap &be);
|
|
|
|
void removeRedundant(BBMap &be);
|
|
|
|
void findAndInstrumentBackEdges(Function &F);
|
2003-07-10 21:55:57 +00:00
|
|
|
public:
|
2004-05-14 21:21:52 +00:00
|
|
|
bool doInitialization(Module &M);
|
2003-07-10 21:55:57 +00:00
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
};
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-08-12 22:00:24 +00:00
|
|
|
RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
|
2003-07-10 21:55:57 +00:00
|
|
|
}
|
2002-11-03 01:45:20 +00:00
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
//helper function to get back edges: it is called by
|
2002-11-03 01:45:20 +00:00
|
|
|
//the "getBackEdges" function below
|
2003-07-10 21:55:57 +00:00
|
|
|
void InstLoops::getBackEdgesVisit(BasicBlock *u,
|
2002-11-03 01:45:20 +00:00
|
|
|
std::map<BasicBlock *, Color > &color,
|
2005-04-21 23:48:37 +00:00
|
|
|
std::map<BasicBlock *, int > &d,
|
2003-08-12 22:00:24 +00:00
|
|
|
int &time, BBMap &be) {
|
2002-11-03 01:45:20 +00:00
|
|
|
color[u]=GREY;
|
|
|
|
time++;
|
|
|
|
d[u]=time;
|
|
|
|
|
2003-09-24 22:06:25 +00:00
|
|
|
for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
|
2002-11-03 01:45:20 +00:00
|
|
|
BasicBlock *BB = *vl;
|
|
|
|
|
|
|
|
if(color[BB]!=GREY && color[BB]!=BLACK){
|
2003-08-12 22:00:24 +00:00
|
|
|
getBackEdgesVisit(BB, color, d, time, be);
|
2002-11-03 01:45:20 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-11-03 01:45:20 +00:00
|
|
|
//now checking for d and f vals
|
2003-07-10 21:55:57 +00:00
|
|
|
else if(color[BB]==GREY){
|
2002-11-03 01:45:20 +00:00
|
|
|
//so v is ancestor of u if time of u > time of v
|
|
|
|
if(d[u] >= d[BB]){
|
2003-07-10 21:55:57 +00:00
|
|
|
//u->BB is a backedge
|
|
|
|
be[u] = BB;
|
2002-11-03 01:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
color[u]=BLACK;//done with visiting the node and its neighbors
|
|
|
|
}
|
|
|
|
|
2003-07-10 21:55:57 +00:00
|
|
|
//look at all BEs, and remove all BEs that are dominated by other BE's in the
|
|
|
|
//set
|
2003-08-12 22:00:24 +00:00
|
|
|
void InstLoops::removeRedundant(BBMap &be) {
|
2003-07-10 21:55:57 +00:00
|
|
|
std::vector<BasicBlock *> toDelete;
|
2005-04-21 23:48:37 +00:00
|
|
|
for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
|
2003-08-12 22:00:24 +00:00
|
|
|
ME = be.end(); MI != ME; ++MI)
|
|
|
|
for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI)
|
|
|
|
if(DS->properlyDominates(MI->first, MMI->first))
|
2003-07-10 21:55:57 +00:00
|
|
|
toDelete.push_back(MMI->first);
|
2003-08-12 22:00:24 +00:00
|
|
|
// Remove all the back-edges we found from be.
|
2005-04-21 23:48:37 +00:00
|
|
|
for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(),
|
2003-08-12 22:00:24 +00:00
|
|
|
VE = toDelete.end(); VI != VE; ++VI)
|
2003-07-10 21:55:57 +00:00
|
|
|
be.erase(*VI);
|
|
|
|
}
|
2002-11-03 01:45:20 +00:00
|
|
|
|
|
|
|
//getting the backedges in a graph
|
|
|
|
//Its a variation of DFS to get the backedges in the graph
|
|
|
|
//We get back edges by associating a time
|
|
|
|
//and a color with each vertex.
|
|
|
|
//The time of a vertex is the time when it was first visited
|
|
|
|
//The color of a vertex is initially WHITE,
|
|
|
|
//Changes to GREY when it is first visited,
|
|
|
|
//and changes to BLACK when ALL its neighbors
|
|
|
|
//have been visited
|
|
|
|
//So we have a back edge when we meet a successor of
|
|
|
|
//a node with smaller time, and GREY color
|
2003-08-12 22:00:24 +00:00
|
|
|
void InstLoops::findAndInstrumentBackEdges(Function &F){
|
2002-11-03 01:45:20 +00:00
|
|
|
std::map<BasicBlock *, Color > color;
|
|
|
|
std::map<BasicBlock *, int> d;
|
2003-08-12 22:00:24 +00:00
|
|
|
BBMap be;
|
2002-11-03 01:45:20 +00:00
|
|
|
int time=0;
|
2003-08-12 22:00:24 +00:00
|
|
|
getBackEdgesVisit(F.begin(), color, d, time, be);
|
2003-07-10 21:55:57 +00:00
|
|
|
|
|
|
|
removeRedundant(be);
|
|
|
|
|
|
|
|
for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
|
|
|
|
ME = be.end(); MI != ME; ++MI){
|
|
|
|
BasicBlock *u = MI->first;
|
|
|
|
BasicBlock *BB = MI->second;
|
2004-05-14 21:21:52 +00:00
|
|
|
// We have a back-edge from BB --> u.
|
|
|
|
DEBUG (std::cerr << "Instrumenting back-edge from " << BB->getName ()
|
|
|
|
<< "-->" << u->getName () << "\n");
|
|
|
|
// Split the back-edge, inserting a new basic block on it, and modify the
|
|
|
|
// source BB's terminator accordingly.
|
|
|
|
BasicBlock *newBB = new BasicBlock("backEdgeInst", u->getParent());
|
2003-07-10 21:55:57 +00:00
|
|
|
BranchInst *ti = cast<BranchInst>(u->getTerminator());
|
2004-05-14 21:21:52 +00:00
|
|
|
unsigned char index = ((ti->getSuccessor(0) == BB) ? 0 : 1);
|
|
|
|
|
2003-07-10 21:55:57 +00:00
|
|
|
assert(ti->getNumSuccessors() > index && "Not enough successors!");
|
|
|
|
ti->setSuccessor(index, newBB);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-07-10 21:55:57 +00:00
|
|
|
BasicBlock::InstListType < = newBB->getInstList();
|
2003-11-20 18:25:24 +00:00
|
|
|
lt.push_back(new CallInst(inCountMth));
|
|
|
|
new BranchInst(BB, newBB);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-05-14 21:21:52 +00:00
|
|
|
// Now, set the sources of Phi nodes corresponding to the back-edge
|
|
|
|
// in BB to come from the instrumentation block instead.
|
2005-04-21 23:48:37 +00:00
|
|
|
for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
|
2004-05-14 21:21:52 +00:00
|
|
|
BB2Inst != BBend; ++BB2Inst) {
|
|
|
|
if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)) {
|
|
|
|
int bbIndex = phiInst->getBasicBlockIndex(u);
|
|
|
|
if (bbIndex>=0)
|
|
|
|
phiInst->setIncomingBlock(bbIndex, newBB);
|
2003-07-10 21:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-11-03 01:45:20 +00:00
|
|
|
}
|
|
|
|
|
2004-05-14 21:21:52 +00:00
|
|
|
bool InstLoops::doInitialization (Module &M) {
|
|
|
|
inCountMth = M.getOrInsertFunction("llvm_first_trigger", Type::VoidTy, 0);
|
|
|
|
return true; // Module was modified.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnFunction - Entry point for FunctionPass that inserts calls to
|
|
|
|
/// trigger function.
|
2003-08-12 22:00:24 +00:00
|
|
|
///
|
2002-11-03 01:45:20 +00:00
|
|
|
bool InstLoops::runOnFunction(Function &F){
|
2004-05-14 21:21:52 +00:00
|
|
|
if (F.isExternal ())
|
2003-07-10 21:55:57 +00:00
|
|
|
return false;
|
2004-05-14 21:21:52 +00:00
|
|
|
|
|
|
|
DS = &getAnalysis<DominatorSet> ();
|
|
|
|
|
2003-08-12 22:00:24 +00:00
|
|
|
// Add a call to reoptimizerInitialize() to beginning of function named main.
|
2004-05-14 21:21:52 +00:00
|
|
|
if (F.getName() == "main")
|
|
|
|
InsertProfilingInitCall (&F, "reoptimizerInitialize");
|
|
|
|
|
2003-08-12 22:00:24 +00:00
|
|
|
findAndInstrumentBackEdges(F);
|
|
|
|
return true; // Function was modified.
|
2002-11-03 01:45:20 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-09-30 20:14:29 +00:00
|
|
|
FunctionPass *createLoopInstrumentationPass () {
|
|
|
|
return new InstLoops();
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|