blob: 5d53d230c9416a4804317b0bd5d6a7ec827f9c18 [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"
Anand Shukla320febb2002-11-03 01:45:20 +000019#include "llvm/iOther.h"
Brian Gaeke27e49432004-05-14 21:21:52 +000020#include "llvm/Type.h"
Anand Shukla320febb2002-11-03 01:45:20 +000021#include "llvm/iTerminators.h"
22#include "llvm/iPHINode.h"
23#include "llvm/Module.h"
Anand Shukla320febb2002-11-03 01:45:20 +000024#include "llvm/Pass.h"
Brian Gaeke27e49432004-05-14 21:21:52 +000025#include "Support/Debug.h"
26#include "../ProfilingUtils.h"
Anand Shukla320febb2002-11-03 01:45:20 +000027
Brian Gaeke960707c2003-11-11 22:41:34 +000028namespace llvm {
29
Anand Shukla320febb2002-11-03 01:45:20 +000030//this is used to color vertices
31//during DFS
32
33enum Color{
34 WHITE,
35 GREY,
36 BLACK
37};
38
Chris Lattner44d2c352003-10-13 03:32:08 +000039namespace {
Brian Gaeke709a16a2003-08-12 22:00:24 +000040 typedef std::map<BasicBlock *, BasicBlock *> BBMap;
Anand Shuklab3d794a2003-07-10 21:55:57 +000041 struct InstLoops : public FunctionPass {
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<DominatorSet>();
44 }
45 private:
Brian Gaeke27e49432004-05-14 21:21:52 +000046 Function *inCountMth;
Anand Shuklab3d794a2003-07-10 21:55:57 +000047 DominatorSet *DS;
48 void getBackEdgesVisit(BasicBlock *u,
49 std::map<BasicBlock *, Color > &color,
50 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000051 int &time, BBMap &be);
52 void removeRedundant(BBMap &be);
53 void findAndInstrumentBackEdges(Function &F);
Anand Shuklab3d794a2003-07-10 21:55:57 +000054 public:
Brian Gaeke27e49432004-05-14 21:21:52 +000055 bool doInitialization(Module &M);
Anand Shuklab3d794a2003-07-10 21:55:57 +000056 bool runOnFunction(Function &F);
57 };
58
Brian Gaeke709a16a2003-08-12 22:00:24 +000059 RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
Anand Shuklab3d794a2003-07-10 21:55:57 +000060}
Anand Shukla320febb2002-11-03 01:45:20 +000061
Anand Shukla320febb2002-11-03 01:45:20 +000062//helper function to get back edges: it is called by
63//the "getBackEdges" function below
Anand Shuklab3d794a2003-07-10 21:55:57 +000064void InstLoops::getBackEdgesVisit(BasicBlock *u,
Anand Shukla320febb2002-11-03 01:45:20 +000065 std::map<BasicBlock *, Color > &color,
66 std::map<BasicBlock *, int > &d,
Brian Gaeke709a16a2003-08-12 22:00:24 +000067 int &time, BBMap &be) {
Anand Shukla320febb2002-11-03 01:45:20 +000068 color[u]=GREY;
69 time++;
70 d[u]=time;
71
Chris Lattnera9400952003-09-24 22:06:25 +000072 for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
Anand Shukla320febb2002-11-03 01:45:20 +000073 BasicBlock *BB = *vl;
74
75 if(color[BB]!=GREY && color[BB]!=BLACK){
Brian Gaeke709a16a2003-08-12 22:00:24 +000076 getBackEdgesVisit(BB, color, d, time, be);
Anand Shukla320febb2002-11-03 01:45:20 +000077 }
78
79 //now checking for d and f vals
Anand Shuklab3d794a2003-07-10 21:55:57 +000080 else if(color[BB]==GREY){
Anand Shukla320febb2002-11-03 01:45:20 +000081 //so v is ancestor of u if time of u > time of v
82 if(d[u] >= d[BB]){
Anand Shuklab3d794a2003-07-10 21:55:57 +000083 //u->BB is a backedge
84 be[u] = BB;
Anand Shukla320febb2002-11-03 01:45:20 +000085 }
86 }
87 }
88 color[u]=BLACK;//done with visiting the node and its neighbors
89}
90
Anand Shuklab3d794a2003-07-10 21:55:57 +000091//look at all BEs, and remove all BEs that are dominated by other BE's in the
92//set
Brian Gaeke709a16a2003-08-12 22:00:24 +000093void InstLoops::removeRedundant(BBMap &be) {
Anand Shuklab3d794a2003-07-10 21:55:57 +000094 std::vector<BasicBlock *> toDelete;
95 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +000096 ME = be.end(); MI != ME; ++MI)
97 for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI)
98 if(DS->properlyDominates(MI->first, MMI->first))
Anand Shuklab3d794a2003-07-10 21:55:57 +000099 toDelete.push_back(MMI->first);
Brian Gaeke709a16a2003-08-12 22:00:24 +0000100 // Remove all the back-edges we found from be.
Anand Shuklab3d794a2003-07-10 21:55:57 +0000101 for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(),
Brian Gaeke709a16a2003-08-12 22:00:24 +0000102 VE = toDelete.end(); VI != VE; ++VI)
Anand Shuklab3d794a2003-07-10 21:55:57 +0000103 be.erase(*VI);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000104}
Anand Shukla320febb2002-11-03 01:45:20 +0000105
106//getting the backedges in a graph
107//Its a variation of DFS to get the backedges in the graph
108//We get back edges by associating a time
109//and a color with each vertex.
110//The time of a vertex is the time when it was first visited
111//The color of a vertex is initially WHITE,
112//Changes to GREY when it is first visited,
113//and changes to BLACK when ALL its neighbors
114//have been visited
115//So we have a back edge when we meet a successor of
116//a node with smaller time, and GREY color
Brian Gaeke709a16a2003-08-12 22:00:24 +0000117void InstLoops::findAndInstrumentBackEdges(Function &F){
Anand Shukla320febb2002-11-03 01:45:20 +0000118 std::map<BasicBlock *, Color > color;
119 std::map<BasicBlock *, int> d;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000120 BBMap be;
Anand Shukla320febb2002-11-03 01:45:20 +0000121 int time=0;
Brian Gaeke709a16a2003-08-12 22:00:24 +0000122 getBackEdgesVisit(F.begin(), color, d, time, be);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000123
124 removeRedundant(be);
125
126 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
127 ME = be.end(); MI != ME; ++MI){
128 BasicBlock *u = MI->first;
129 BasicBlock *BB = MI->second;
Brian Gaeke27e49432004-05-14 21:21:52 +0000130 // We have a back-edge from BB --> u.
131 DEBUG (std::cerr << "Instrumenting back-edge from " << BB->getName ()
132 << "-->" << u->getName () << "\n");
133 // Split the back-edge, inserting a new basic block on it, and modify the
134 // source BB's terminator accordingly.
135 BasicBlock *newBB = new BasicBlock("backEdgeInst", u->getParent());
Anand Shuklab3d794a2003-07-10 21:55:57 +0000136 BranchInst *ti = cast<BranchInst>(u->getTerminator());
Brian Gaeke27e49432004-05-14 21:21:52 +0000137 unsigned char index = ((ti->getSuccessor(0) == BB) ? 0 : 1);
138
Anand Shuklab3d794a2003-07-10 21:55:57 +0000139 assert(ti->getNumSuccessors() > index && "Not enough successors!");
140 ti->setSuccessor(index, newBB);
141
142 BasicBlock::InstListType &lt = newBB->getInstList();
Chris Lattner2af51722003-11-20 18:25:24 +0000143 lt.push_back(new CallInst(inCountMth));
144 new BranchInst(BB, newBB);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000145
Brian Gaeke27e49432004-05-14 21:21:52 +0000146 // Now, set the sources of Phi nodes corresponding to the back-edge
147 // in BB to come from the instrumentation block instead.
Anand Shuklab3d794a2003-07-10 21:55:57 +0000148 for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
Brian Gaeke27e49432004-05-14 21:21:52 +0000149 BB2Inst != BBend; ++BB2Inst) {
150 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)) {
151 int bbIndex = phiInst->getBasicBlockIndex(u);
152 if (bbIndex>=0)
153 phiInst->setIncomingBlock(bbIndex, newBB);
Anand Shuklab3d794a2003-07-10 21:55:57 +0000154 }
155 }
156 }
Anand Shukla320febb2002-11-03 01:45:20 +0000157}
158
Brian Gaeke27e49432004-05-14 21:21:52 +0000159bool InstLoops::doInitialization (Module &M) {
160 inCountMth = M.getOrInsertFunction("llvm_first_trigger", Type::VoidTy, 0);
161 return true; // Module was modified.
162}
163
164/// runOnFunction - Entry point for FunctionPass that inserts calls to
165/// trigger function.
Brian Gaeke709a16a2003-08-12 22:00:24 +0000166///
Anand Shukla320febb2002-11-03 01:45:20 +0000167bool InstLoops::runOnFunction(Function &F){
Brian Gaeke27e49432004-05-14 21:21:52 +0000168 if (F.isExternal ())
Anand Shuklab3d794a2003-07-10 21:55:57 +0000169 return false;
Brian Gaeke27e49432004-05-14 21:21:52 +0000170
171 DS = &getAnalysis<DominatorSet> ();
172
Brian Gaeke709a16a2003-08-12 22:00:24 +0000173 // Add a call to reoptimizerInitialize() to beginning of function named main.
Brian Gaeke27e49432004-05-14 21:21:52 +0000174 if (F.getName() == "main")
175 InsertProfilingInitCall (&F, "reoptimizerInitialize");
176
Brian Gaeke709a16a2003-08-12 22:00:24 +0000177 findAndInstrumentBackEdges(F);
178 return true; // Function was modified.
Anand Shukla320febb2002-11-03 01:45:20 +0000179}
Brian Gaeke960707c2003-11-11 22:41:34 +0000180
181} // End llvm namespace