Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- InstLoops.cpp -----------------------------------------------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 10 | // 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 Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 14 | // |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Dominators.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CFG.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 19 | #include "llvm/iOther.h" |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 21 | #include "llvm/iTerminators.h" |
| 22 | #include "llvm/iPHINode.h" |
| 23 | #include "llvm/Module.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 25 | #include "Support/Debug.h" |
| 26 | #include "../ProfilingUtils.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 27 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 30 | //this is used to color vertices |
| 31 | //during DFS |
| 32 | |
| 33 | enum Color{ |
| 34 | WHITE, |
| 35 | GREY, |
| 36 | BLACK |
| 37 | }; |
| 38 | |
Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 39 | namespace { |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 40 | typedef std::map<BasicBlock *, BasicBlock *> BBMap; |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 41 | struct InstLoops : public FunctionPass { |
| 42 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 43 | AU.addRequired<DominatorSet>(); |
| 44 | } |
| 45 | private: |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 46 | Function *inCountMth; |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 47 | DominatorSet *DS; |
| 48 | void getBackEdgesVisit(BasicBlock *u, |
| 49 | std::map<BasicBlock *, Color > &color, |
| 50 | std::map<BasicBlock *, int > &d, |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 51 | int &time, BBMap &be); |
| 52 | void removeRedundant(BBMap &be); |
| 53 | void findAndInstrumentBackEdges(Function &F); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 54 | public: |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 55 | bool doInitialization(Module &M); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 56 | bool runOnFunction(Function &F); |
| 57 | }; |
| 58 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 59 | RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling"); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 60 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 61 | |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 62 | //helper function to get back edges: it is called by |
| 63 | //the "getBackEdges" function below |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 64 | void InstLoops::getBackEdgesVisit(BasicBlock *u, |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 65 | std::map<BasicBlock *, Color > &color, |
| 66 | std::map<BasicBlock *, int > &d, |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 67 | int &time, BBMap &be) { |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 68 | color[u]=GREY; |
| 69 | time++; |
| 70 | d[u]=time; |
| 71 | |
Chris Lattner | a940095 | 2003-09-24 22:06:25 +0000 | [diff] [blame] | 72 | for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 73 | BasicBlock *BB = *vl; |
| 74 | |
| 75 | if(color[BB]!=GREY && color[BB]!=BLACK){ |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 76 | getBackEdgesVisit(BB, color, d, time, be); |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //now checking for d and f vals |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 80 | else if(color[BB]==GREY){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 81 | //so v is ancestor of u if time of u > time of v |
| 82 | if(d[u] >= d[BB]){ |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 83 | //u->BB is a backedge |
| 84 | be[u] = BB; |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | color[u]=BLACK;//done with visiting the node and its neighbors |
| 89 | } |
| 90 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 91 | //look at all BEs, and remove all BEs that are dominated by other BE's in the |
| 92 | //set |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 93 | void InstLoops::removeRedundant(BBMap &be) { |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 94 | std::vector<BasicBlock *> toDelete; |
| 95 | for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(), |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 96 | 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 Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 99 | toDelete.push_back(MMI->first); |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 100 | // Remove all the back-edges we found from be. |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 101 | for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(), |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 102 | VE = toDelete.end(); VI != VE; ++VI) |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 103 | be.erase(*VI); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 104 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 105 | |
| 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 Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 117 | void InstLoops::findAndInstrumentBackEdges(Function &F){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 118 | std::map<BasicBlock *, Color > color; |
| 119 | std::map<BasicBlock *, int> d; |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 120 | BBMap be; |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 121 | int time=0; |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 122 | getBackEdgesVisit(F.begin(), color, d, time, be); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 123 | |
| 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 Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 130 | // 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 Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 136 | BranchInst *ti = cast<BranchInst>(u->getTerminator()); |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 137 | unsigned char index = ((ti->getSuccessor(0) == BB) ? 0 : 1); |
| 138 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 139 | assert(ti->getNumSuccessors() > index && "Not enough successors!"); |
| 140 | ti->setSuccessor(index, newBB); |
| 141 | |
| 142 | BasicBlock::InstListType < = newBB->getInstList(); |
Chris Lattner | 2af5172 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 143 | lt.push_back(new CallInst(inCountMth)); |
| 144 | new BranchInst(BB, newBB); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 145 | |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 146 | // Now, set the sources of Phi nodes corresponding to the back-edge |
| 147 | // in BB to come from the instrumentation block instead. |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 148 | for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end(); |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 149 | 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 Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 159 | bool 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 Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 166 | /// |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 167 | bool InstLoops::runOnFunction(Function &F){ |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 168 | if (F.isExternal ()) |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 169 | return false; |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 170 | |
| 171 | DS = &getAnalysis<DominatorSet> (); |
| 172 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 173 | // Add a call to reoptimizerInitialize() to beginning of function named main. |
Brian Gaeke | 27e4943 | 2004-05-14 21:21:52 +0000 | [diff] [blame] | 174 | if (F.getName() == "main") |
| 175 | InsertProfilingInitCall (&F, "reoptimizerInitialize"); |
| 176 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 177 | findAndInstrumentBackEdges(F); |
| 178 | return true; // Function was modified. |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 179 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 180 | |
| 181 | } // End llvm namespace |