blob: 500208756aecb8f9e3ed32463b99de73e891aca5 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- InstLoops.cpp -----------------------------------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner44d2c352003-10-13 03:32:08 +00009//
Brian Gaeke27e49432004-05-14 21:21:52 +000010// This is the first-level instrumentation pass for the Reoptimizer. It
11// instrument the back-edges of loops by inserting a basic block
12// containing a call to llvm_first_trigger (the first-level trigger function),
13// and inserts an initialization call to the main() function.
Chris Lattner44d2c352003-10-13 03:32:08 +000014//
Anand Shukla320febb2002-11-03 01:45:20 +000015//===----------------------------------------------------------------------===//
16
Anand Shuklab3d794a2003-07-10 21:55:57 +000017#include "llvm/Analysis/Dominators.h"
Anand Shukla320febb2002-11-03 01:45:20 +000018#include "llvm/Support/CFG.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Anand Shukla320febb2002-11-03 01:45:20 +000020#include "llvm/Module.h"
Anand Shukla320febb2002-11-03 01:45:20 +000021#include "llvm/Pass.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000022#include "llvm/Type.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Jeff Cohen844410b2005-01-07 05:42:13 +000024#include "llvm/Transforms/Instrumentation.h"
Brian Gaeke27e49432004-05-14 21:21:52 +000025#include "../ProfilingUtils.h"
Anand Shukla320febb2002-11-03 01:45:20 +000026
Brian Gaeke960707c2003-11-11 22:41:34 +000027namespace llvm {
28
Anand Shukla320febb2002-11-03 01:45:20 +000029//this is used to color vertices
30//during DFS
31
32enum Color{
33 WHITE,
34 GREY,
35 BLACK
36};
37
Chris Lattner44d2c352003-10-13 03:32:08 +000038namespace {
Brian Gaeke709a16a2003-08-12 22:00:24 +000039 typedef std::map<BasicBlock *, BasicBlock *> BBMap;
Anand Shuklab3d794a2003-07-10 21:55:57 +000040 struct InstLoops : public FunctionPass {
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<DominatorSet>();
43 }
44 private:
Brian Gaeke27e49432004-05-14 21:21:52 +000045 Function *inCountMth;
Anand Shuklab3d794a2003-07-10 21:55:57 +000046 DominatorSet *DS;
47 void getBackEdgesVisit(BasicBlock *u,
48 std::map<BasicBlock *, Color > &color,
49 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000050 int &time, BBMap &be);
51 void removeRedundant(BBMap &be);
52 void findAndInstrumentBackEdges(Function &F);
Anand Shuklab3d794a2003-07-10 21:55:57 +000053 public:
Brian Gaeke27e49432004-05-14 21:21:52 +000054 bool doInitialization(Module &M);
Anand Shuklab3d794a2003-07-10 21:55:57 +000055 bool runOnFunction(Function &F);
56 };
57
Brian Gaeke709a16a2003-08-12 22:00:24 +000058 RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
Anand Shuklab3d794a2003-07-10 21:55:57 +000059}
Anand Shukla320febb2002-11-03 01:45:20 +000060
Anand Shukla320febb2002-11-03 01:45:20 +000061//helper function to get back edges: it is called by
62//the "getBackEdges" function below
Anand Shuklab3d794a2003-07-10 21:55:57 +000063void InstLoops::getBackEdgesVisit(BasicBlock *u,
Anand Shukla320febb2002-11-03 01:45:20 +000064 std::map<BasicBlock *, Color > &color,
65 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000066 int &time, BBMap &be) {
Anand Shukla320febb2002-11-03 01:45:20 +000067 color[u]=GREY;
68 time++;
69 d[u]=time;
70
Chris Lattnera9400952003-09-24 22:06:25 +000071 for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
Anand Shukla320febb2002-11-03 01:45:20 +000072 BasicBlock *BB = *vl;
73
74 if(color[BB]!=GREY && color[BB]!=BLACK){
Brian Gaeke709a16a2003-08-12 22:00:24 +000075 getBackEdgesVisit(BB, color, d, time, be);
Anand Shukla320febb2002-11-03 01:45:20 +000076 }
77
78 //now checking for d and f vals
Anand Shuklab3d794a2003-07-10 21:55:57 +000079 else if(color[BB]==GREY){
Anand Shukla320febb2002-11-03 01:45:20 +000080 //so v is ancestor of u if time of u > time of v
81 if(d[u] >= d[BB]){
Anand Shuklab3d794a2003-07-10 21:55:57 +000082 //u->BB is a backedge
83 be[u] = BB;
Anand Shukla320febb2002-11-03 01:45:20 +000084 }
85 }
86 }
87 color[u]=BLACK;//done with visiting the node and its neighbors
88}
89
Anand Shuklab3d794a2003-07-10 21:55:57 +000090//look at all BEs, and remove all BEs that are dominated by other BE's in the
91//set
Brian Gaeke709a16a2003-08-12 22:00:24 +000092void InstLoops::removeRedundant(BBMap &be) {
Anand Shuklab3d794a2003-07-10 21:55:57 +000093 std::vector<BasicBlock *> toDelete;
94 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +000095 ME = be.end(); MI != ME; ++MI)
96 for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI)
97 if(DS->properlyDominates(MI->first, MMI->first))
Anand Shuklab3d794a2003-07-10 21:55:57 +000098 toDelete.push_back(MMI->first);
Brian Gaeke709a16a2003-08-12 22:00:24 +000099 // Remove all the back-edges we found from be.
Anand Shuklab3d794a2003-07-10 21:55:57 +0000100 for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +0000101 VE = toDelete.end(); VI != VE; ++VI)
Anand Shuklab3d794a2003-07-10 21:55:57 +0000102 be.erase(*VI);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000103}
Anand Shukla320febb2002-11-03 01:45:20 +0000104
105//getting the backedges in a graph
106//Its a variation of DFS to get the backedges in the graph
107//We get back edges by associating a time
108//and a color with each vertex.
109//The time of a vertex is the time when it was first visited
110//The color of a vertex is initially WHITE,
111//Changes to GREY when it is first visited,
112//and changes to BLACK when ALL its neighbors
113//have been visited
114//So we have a back edge when we meet a successor of
115//a node with smaller time, and GREY color
Brian Gaeke709a16a2003-08-12 22:00:24 +0000116void InstLoops::findAndInstrumentBackEdges(Function &F){
Anand Shukla320febb2002-11-03 01:45:20 +0000117 std::map<BasicBlock *, Color > color;
118 std::map<BasicBlock *, int> d;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000119 BBMap be;
Anand Shukla320febb2002-11-03 01:45:20 +0000120 int time=0;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000121 getBackEdgesVisit(F.begin(), color, d, time, be);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000122
123 removeRedundant(be);
124
125 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
126 ME = be.end(); MI != ME; ++MI){
127 BasicBlock *u = MI->first;
128 BasicBlock *BB = MI->second;
Brian Gaeke27e49432004-05-14 21:21:52 +0000129 // We have a back-edge from BB --> u.
130 DEBUG (std::cerr << "Instrumenting back-edge from " << BB->getName ()
131 << "-->" << u->getName () << "\n");
132 // Split the back-edge, inserting a new basic block on it, and modify the
133 // source BB's terminator accordingly.
134 BasicBlock *newBB = new BasicBlock("backEdgeInst", u->getParent());
Anand Shuklab3d794a2003-07-10 21:55:57 +0000135 BranchInst *ti = cast<BranchInst>(u->getTerminator());
Brian Gaeke27e49432004-05-14 21:21:52 +0000136 unsigned char index = ((ti->getSuccessor(0) == BB) ? 0 : 1);
137
Anand Shuklab3d794a2003-07-10 21:55:57 +0000138 assert(ti->getNumSuccessors() > index && "Not enough successors!");
139 ti->setSuccessor(index, newBB);
140
141 BasicBlock::InstListType &lt = newBB->getInstList();
Chris Lattner2af51722003-11-20 18:25:24 +0000142 lt.push_back(new CallInst(inCountMth));
143 new BranchInst(BB, newBB);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000144
Brian Gaeke27e49432004-05-14 21:21:52 +0000145 // Now, set the sources of Phi nodes corresponding to the back-edge
146 // in BB to come from the instrumentation block instead.
Anand Shuklab3d794a2003-07-10 21:55:57 +0000147 for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
Brian Gaeke27e49432004-05-14 21:21:52 +0000148 BB2Inst != BBend; ++BB2Inst) {
149 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)) {
150 int bbIndex = phiInst->getBasicBlockIndex(u);
151 if (bbIndex>=0)
152 phiInst->setIncomingBlock(bbIndex, newBB);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000153 }
154 }
155 }
Anand Shukla320febb2002-11-03 01:45:20 +0000156}
157
Brian Gaeke27e49432004-05-14 21:21:52 +0000158bool InstLoops::doInitialization (Module &M) {
159 inCountMth = M.getOrInsertFunction("llvm_first_trigger", Type::VoidTy, 0);
160 return true; // Module was modified.
161}
162
163/// runOnFunction - Entry point for FunctionPass that inserts calls to
164/// trigger function.
Brian Gaeke709a16a2003-08-12 22:00:24 +0000165///
Anand Shukla320febb2002-11-03 01:45:20 +0000166bool InstLoops::runOnFunction(Function &F){
Brian Gaeke27e49432004-05-14 21:21:52 +0000167 if (F.isExternal ())
Anand Shuklab3d794a2003-07-10 21:55:57 +0000168 return false;
Brian Gaeke27e49432004-05-14 21:21:52 +0000169
170 DS = &getAnalysis<DominatorSet> ();
171
Brian Gaeke709a16a2003-08-12 22:00:24 +0000172 // Add a call to reoptimizerInitialize() to beginning of function named main.
Brian Gaeke27e49432004-05-14 21:21:52 +0000173 if (F.getName() == "main")
174 InsertProfilingInitCall (&F, "reoptimizerInitialize");
175
Brian Gaeke709a16a2003-08-12 22:00:24 +0000176 findAndInstrumentBackEdges(F);
177 return true; // Function was modified.
Anand Shukla320febb2002-11-03 01:45:20 +0000178}
Brian Gaeke960707c2003-11-11 22:41:34 +0000179
Brian Gaeke33e834e2004-09-30 20:14:29 +0000180FunctionPass *createLoopInstrumentationPass () {
181 return new InstLoops();
182}
183
Brian Gaeke960707c2003-11-11 22:41:34 +0000184} // End llvm namespace