blob: d2e8d14e8440d36c65265487ef3e10ea78b901ec [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- InstLoops.cpp -----------------------------------------------------===//
2//
Anand Shukla320febb2002-11-03 01:45:20 +00003// Pass to instrument loops
4//
5// At every backedge, insert a counter for that backedge and a call function
Chris Lattner44d2c352003-10-13 03:32:08 +00006//
Anand Shukla320febb2002-11-03 01:45:20 +00007//===----------------------------------------------------------------------===//
8
Anand Shuklab3d794a2003-07-10 21:55:57 +00009#include "llvm/Analysis/Dominators.h"
Anand Shukla320febb2002-11-03 01:45:20 +000010#include "llvm/Support/CFG.h"
11#include "llvm/Constants.h"
12#include "llvm/iMemory.h"
13#include "llvm/GlobalVariable.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/iOther.h"
16#include "llvm/iOperators.h"
17#include "llvm/iTerminators.h"
18#include "llvm/iPHINode.h"
19#include "llvm/Module.h"
20#include "llvm/Function.h"
21#include "llvm/Pass.h"
22
23//this is used to color vertices
24//during DFS
25
26enum Color{
27 WHITE,
28 GREY,
29 BLACK
30};
31
Chris Lattner44d2c352003-10-13 03:32:08 +000032namespace {
Brian Gaeke709a16a2003-08-12 22:00:24 +000033 typedef std::map<BasicBlock *, BasicBlock *> BBMap;
Anand Shuklab3d794a2003-07-10 21:55:57 +000034 struct InstLoops : public FunctionPass {
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired<DominatorSet>();
37 }
38 private:
39 DominatorSet *DS;
40 void getBackEdgesVisit(BasicBlock *u,
41 std::map<BasicBlock *, Color > &color,
42 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000043 int &time, BBMap &be);
44 void removeRedundant(BBMap &be);
45 void findAndInstrumentBackEdges(Function &F);
Anand Shuklab3d794a2003-07-10 21:55:57 +000046 public:
47 bool runOnFunction(Function &F);
48 };
49
Brian Gaeke709a16a2003-08-12 22:00:24 +000050 RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
Anand Shuklab3d794a2003-07-10 21:55:57 +000051}
Anand Shukla320febb2002-11-03 01:45:20 +000052
Anand Shukla320febb2002-11-03 01:45:20 +000053//helper function to get back edges: it is called by
54//the "getBackEdges" function below
Anand Shuklab3d794a2003-07-10 21:55:57 +000055void InstLoops::getBackEdgesVisit(BasicBlock *u,
Anand Shukla320febb2002-11-03 01:45:20 +000056 std::map<BasicBlock *, Color > &color,
57 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000058 int &time, BBMap &be) {
Anand Shukla320febb2002-11-03 01:45:20 +000059 color[u]=GREY;
60 time++;
61 d[u]=time;
62
Chris Lattnera9400952003-09-24 22:06:25 +000063 for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
Anand Shukla320febb2002-11-03 01:45:20 +000064 BasicBlock *BB = *vl;
65
66 if(color[BB]!=GREY && color[BB]!=BLACK){
Brian Gaeke709a16a2003-08-12 22:00:24 +000067 getBackEdgesVisit(BB, color, d, time, be);
Anand Shukla320febb2002-11-03 01:45:20 +000068 }
69
70 //now checking for d and f vals
Anand Shuklab3d794a2003-07-10 21:55:57 +000071 else if(color[BB]==GREY){
Anand Shukla320febb2002-11-03 01:45:20 +000072 //so v is ancestor of u if time of u > time of v
73 if(d[u] >= d[BB]){
Anand Shuklab3d794a2003-07-10 21:55:57 +000074 //u->BB is a backedge
75 be[u] = BB;
Anand Shukla320febb2002-11-03 01:45:20 +000076 }
77 }
78 }
79 color[u]=BLACK;//done with visiting the node and its neighbors
80}
81
Anand Shuklab3d794a2003-07-10 21:55:57 +000082//look at all BEs, and remove all BEs that are dominated by other BE's in the
83//set
Brian Gaeke709a16a2003-08-12 22:00:24 +000084void InstLoops::removeRedundant(BBMap &be) {
Anand Shuklab3d794a2003-07-10 21:55:57 +000085 std::vector<BasicBlock *> toDelete;
86 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +000087 ME = be.end(); MI != ME; ++MI)
88 for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI)
89 if(DS->properlyDominates(MI->first, MMI->first))
Anand Shuklab3d794a2003-07-10 21:55:57 +000090 toDelete.push_back(MMI->first);
Brian Gaeke709a16a2003-08-12 22:00:24 +000091 // Remove all the back-edges we found from be.
Anand Shuklab3d794a2003-07-10 21:55:57 +000092 for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +000093 VE = toDelete.end(); VI != VE; ++VI)
Anand Shuklab3d794a2003-07-10 21:55:57 +000094 be.erase(*VI);
Anand Shuklab3d794a2003-07-10 21:55:57 +000095}
Anand Shukla320febb2002-11-03 01:45:20 +000096
97//getting the backedges in a graph
98//Its a variation of DFS to get the backedges in the graph
99//We get back edges by associating a time
100//and a color with each vertex.
101//The time of a vertex is the time when it was first visited
102//The color of a vertex is initially WHITE,
103//Changes to GREY when it is first visited,
104//and changes to BLACK when ALL its neighbors
105//have been visited
106//So we have a back edge when we meet a successor of
107//a node with smaller time, and GREY color
Brian Gaeke709a16a2003-08-12 22:00:24 +0000108void InstLoops::findAndInstrumentBackEdges(Function &F){
Anand Shukla320febb2002-11-03 01:45:20 +0000109 std::map<BasicBlock *, Color > color;
110 std::map<BasicBlock *, int> d;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000111 BBMap be;
Anand Shukla320febb2002-11-03 01:45:20 +0000112 int time=0;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000113 getBackEdgesVisit(F.begin(), color, d, time, be);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000114
115 removeRedundant(be);
116
Chris Lattner25bc3f82003-08-31 00:21:59 +0000117 // FIXME: THIS IS HORRIBLY BROKEN. FunctionPass's cannot do this, except in
118 // their initialize function!!
119 Function *inCountMth =
120 F.getParent()->getOrInsertFunction("llvm_first_trigger",
121 Type::VoidTy, 0);
122
Anand Shuklab3d794a2003-07-10 21:55:57 +0000123 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
124 ME = be.end(); MI != ME; ++MI){
125 BasicBlock *u = MI->first;
126 BasicBlock *BB = MI->second;
127 //std::cerr<<"Edge from: "<<BB->getName()<<"->"<<u->getName()<<"\n";
128 //insert a new basic block: modify terminator accordingly!
129 BasicBlock *newBB = new BasicBlock("", u->getParent());
130 BranchInst *ti = cast<BranchInst>(u->getTerminator());
131 unsigned char index = 1;
132 if(ti->getSuccessor(0) == BB){
133 index = 0;
134 }
135 assert(ti->getNumSuccessors() > index && "Not enough successors!");
136 ti->setSuccessor(index, newBB);
137
138 BasicBlock::InstListType &lt = newBB->getInstList();
139
Brian Gaeke709a16a2003-08-12 22:00:24 +0000140 Instruction *call = new CallInst(inCountMth);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000141 lt.push_back(call);
142 lt.push_back(new BranchInst(BB));
143
144 //now iterate over *vl, and set its Phi nodes right
145 for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
146 BB2Inst != BBend; ++BB2Inst){
147
148 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
149 int bbIndex = phiInst->getBasicBlockIndex(u);
150 if(bbIndex>=0){
151 phiInst->setIncomingBlock(bbIndex, newBB);
152 }
153 }
154 }
155 }
Anand Shukla320febb2002-11-03 01:45:20 +0000156}
157
Brian Gaeke709a16a2003-08-12 22:00:24 +0000158/// Entry point for FunctionPass that inserts calls to trigger function.
159///
Anand Shukla320febb2002-11-03 01:45:20 +0000160bool InstLoops::runOnFunction(Function &F){
Anand Shuklab3d794a2003-07-10 21:55:57 +0000161 DS = &getAnalysis<DominatorSet>();
Anand Shuklab3d794a2003-07-10 21:55:57 +0000162 if(F.isExternal()) {
163 return false;
164 }
Brian Gaeke709a16a2003-08-12 22:00:24 +0000165 // Add a call to reoptimizerInitialize() to beginning of function named main.
Anand Shukla320febb2002-11-03 01:45:20 +0000166 if(F.getName() == "main"){
Brian Gaeke709a16a2003-08-12 22:00:24 +0000167 std::vector<const Type*> argTypes; // Empty formal parameter list.
168 const FunctionType *Fty = FunctionType::get(Type::VoidTy, argTypes, false);
169 Function *initialMeth =
170 F.getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
Anand Shukla320febb2002-11-03 01:45:20 +0000171 assert(initialMeth && "Initialize method could not be inserted!");
Brian Gaeke709a16a2003-08-12 22:00:24 +0000172 new CallInst(initialMeth, "", F.begin()->begin()); // Insert it.
Anand Shukla320febb2002-11-03 01:45:20 +0000173 }
Brian Gaeke709a16a2003-08-12 22:00:24 +0000174 findAndInstrumentBackEdges(F);
175 return true; // Function was modified.
Anand Shukla320febb2002-11-03 01:45:20 +0000176}