Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 1 | //===- LoopPreheaders.cpp - Loop Preheader Insertion Pass -----------------===// |
| 2 | // |
| 3 | // Insert Loop pre-headers into the CFG for each function in the module. This |
| 4 | // pass updates loop information and dominator information. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "llvm/Transforms/Scalar.h" |
| 9 | #include "llvm/Analysis/Dominators.h" |
| 10 | #include "llvm/Analysis/LoopInfo.h" |
| 11 | #include "llvm/Function.h" |
| 12 | #include "llvm/iTerminators.h" |
| 13 | #include "llvm/iPHINode.h" |
| 14 | #include "llvm/Constant.h" |
| 15 | #include "llvm/Support/CFG.h" |
| 16 | #include "Support/StatisticReporter.h" |
| 17 | |
| 18 | namespace { |
| 19 | Statistic<> NumInserted("preheaders\t- Number of pre-header nodes inserted"); |
| 20 | |
| 21 | struct Preheaders : public FunctionPass { |
| 22 | virtual bool runOnFunction(Function &F); |
| 23 | |
| 24 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 25 | // We need loop information to identify the loops... |
| 26 | AU.addRequired<LoopInfo>(); |
| 27 | |
| 28 | AU.addPreserved<LoopInfo>(); |
| 29 | AU.addPreserved<DominatorSet>(); |
| 30 | AU.addPreserved<ImmediateDominators>(); |
| 31 | AU.addPreserved<DominatorTree>(); |
| 32 | AU.addPreservedID(BreakCriticalEdgesID); // No crit edges added.... |
| 33 | } |
| 34 | private: |
| 35 | bool ProcessLoop(Loop *L); |
| 36 | void InsertPreheaderForLoop(Loop *L); |
| 37 | }; |
| 38 | |
| 39 | RegisterOpt<Preheaders> X("preheaders", |
| 40 | "Insert a pre-header node for every loop in the CFG"); |
| 41 | } |
| 42 | |
| 43 | // Publically exposed interface to pass... |
| 44 | const PassInfo *LoopPreheadersID = X.getPassInfo(); |
| 45 | Pass *createLoopPreheaderInsertionPass() { return new Preheaders(); } |
| 46 | |
| 47 | |
| 48 | /// runOnFunction - Run down all loops in the CFG (recursively, but we could do |
| 49 | /// it in any convenient order) inserting preheaders... |
| 50 | /// |
| 51 | bool Preheaders::runOnFunction(Function &F) { |
| 52 | bool Changed = false; |
| 53 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 54 | |
| 55 | for (unsigned i = 0, e = LI.getTopLevelLoops().size(); i != e; ++i) |
| 56 | Changed |= ProcessLoop(LI.getTopLevelLoops()[i]); |
| 57 | |
| 58 | return Changed; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /// ProcessLoop - Walk the loop structure in depth first order, ensuring that |
| 63 | /// all loops have preheaders. |
| 64 | /// |
| 65 | bool Preheaders::ProcessLoop(Loop *L) { |
| 66 | bool Changed = false; |
| 67 | |
| 68 | // Does the loop already have a preheader? If so, don't modify the loop... |
| 69 | if (L->getLoopPreheader() == 0) { |
| 70 | InsertPreheaderForLoop(L); |
| 71 | NumInserted++; |
| 72 | Changed = true; |
| 73 | } |
| 74 | |
| 75 | const std::vector<Loop*> &SubLoops = L->getSubLoops(); |
| 76 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 77 | Changed |= ProcessLoop(SubLoops[i]); |
| 78 | return Changed; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a |
| 83 | /// preheader, this method is called to insert one. This method has two phases: |
| 84 | /// preheader insertion and analysis updating. |
| 85 | /// |
| 86 | void Preheaders::InsertPreheaderForLoop(Loop *L) { |
| 87 | BasicBlock *Header = L->getHeader(); |
| 88 | |
| 89 | // Compute the set of predecessors of the loop that are not in the loop. |
| 90 | std::vector<BasicBlock*> OutsideBlocks; |
| 91 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 92 | PI != PE; ++PI) |
| 93 | if (!L->contains(*PI)) // Coming in from outside the loop? |
| 94 | OutsideBlocks.push_back(*PI); // Keep track of it... |
| 95 | |
| 96 | assert(OutsideBlocks.size() != 1 && "Loop already has a preheader!"); |
| 97 | |
| 98 | // Create new basic block, insert right before the header of the loop... |
| 99 | BasicBlock *NewBB = new BasicBlock(Header->getName()+".preheader", Header); |
| 100 | |
| 101 | // The preheader first gets an unconditional branch to the loop header... |
| 102 | BranchInst *BI = new BranchInst(Header); |
| 103 | NewBB->getInstList().push_back(BI); |
| 104 | |
| 105 | // For every PHI node in the loop body, insert a PHI node into NewBB where |
| 106 | // the incoming values from the out of loop edges are moved to NewBB. We |
| 107 | // have two possible cases here. If the loop is dead, we just insert dummy |
| 108 | // entries into the PHI nodes for the new edge. If the loop is not dead, we |
| 109 | // move the incoming edges in Header into new PHI nodes in NewBB. |
| 110 | // |
| 111 | if (!OutsideBlocks.empty()) { // Is the loop not obviously dead? |
| 112 | for (BasicBlock::iterator I = Header->begin(); |
| 113 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) { |
| 114 | |
| 115 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 116 | PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); |
| 117 | |
| 118 | // Move all of the edges from blocks outside the loop to the new PHI |
| 119 | for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) { |
| 120 | Value *V = PN->removeIncomingValue(OutsideBlocks[i]); |
| 121 | NewPHI->addIncoming(V, OutsideBlocks[i]); |
| 122 | } |
| 123 | |
| 124 | // Add an incoming value to the PHI node in the loop for the preheader |
| 125 | // edge |
| 126 | PN->addIncoming(NewPHI, NewBB); |
| 127 | } |
| 128 | |
| 129 | // Now that the PHI nodes are updated, actually move the edges from |
| 130 | // OutsideBlocks to point to NewBB instead of Header. |
| 131 | // |
| 132 | for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) { |
| 133 | TerminatorInst *TI = OutsideBlocks[i]->getTerminator(); |
| 134 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) |
| 135 | if (TI->getSuccessor(s) == Header) |
| 136 | TI->setSuccessor(s, NewBB); |
| 137 | } |
| 138 | |
| 139 | } else { // Otherwise the loop is dead... |
| 140 | for (BasicBlock::iterator I = Header->begin(); |
| 141 | PHINode *PN = dyn_cast<PHINode>(&*I); ++I) |
| 142 | // Insert dummy values as the incoming value... |
| 143 | PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | //===--------------------------------------------------------------------===// |
| 148 | // Update analysis results now that we have preformed the transformation |
| 149 | // |
| 150 | |
| 151 | // We know that we have loop information to update... update it now. |
| 152 | if (Loop *Parent = L->getParentLoop()) |
| 153 | Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>()); |
| 154 | |
| 155 | // Update dominator information if it is around... |
| 156 | if (DominatorSet *DS = getAnalysisToUpdate<DominatorSet>()) { |
| 157 | // We need to add information about the fact that NewBB dominates Header. |
| 158 | DS->addDominator(Header, NewBB); |
| 159 | |
| 160 | // The blocks that dominate NewBB are the blocks that dominate Header, |
| 161 | // minus Header, plus NewBB. |
| 162 | DominatorSet::DomSetType DomSet = DS->getDominators(Header); |
| 163 | DomSet.erase(Header); // Header does not dominate us... |
| 164 | DS->addBasicBlock(NewBB, DomSet); |
| 165 | } |
| 166 | |
| 167 | // Update immediate dominator information if we have it... |
| 168 | if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) { |
| 169 | // Whatever i-dominated the header node now immediately dominates NewBB |
| 170 | ID->addNewBlock(NewBB, ID->get(Header)); |
| 171 | |
| 172 | // The preheader now is the immediate dominator for the header node... |
| 173 | ID->setImmediateDominator(Header, NewBB); |
| 174 | } |
| 175 | |
| 176 | // Update DominatorTree information if it is active. |
| 177 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
| 178 | // The immediate dominator of the preheader is the immediate dominator of |
| 179 | // the old header. |
| 180 | // |
| 181 | DominatorTree::Node *HeaderNode = DT->getNode(Header); |
| 182 | DominatorTree::Node *PHNode = DT->createNewNode(NewBB, HeaderNode); |
| 183 | |
| 184 | // Change the header node so that PNHode is the new immediate dominator |
| 185 | DT->changeImmediateDominator(HeaderNode, PHNode); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |