1 //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by Owen Anderson and is distributed under the 6 // University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass transforms loops by placing phi nodes at the end of the loops for 11 // all values that are live across the loop boundary. For example, it turns 12 // the left into the right code: 13 // 14 // for (...) for (...) 15 // if (c) if(c) 16 // X1 = ... X1 = ... 17 // else else 18 // X2 = ... X2 = ... 19 // X3 = phi(X1, X2) X3 = phi(X1, X2) 20 // ... = X3 + 4 X4 = phi(X3) 21 // ... = X4 + 4 22 // 23 // This is still valid LLVM; the extra phi nodes are purely redundant, and will 24 // be trivially eliminated by InstCombine. The major benefit of this 25 // transformation is that it makes many other loop optimizations, such as 26 // LoopUnswitching, simpler. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #include "llvm/Transforms/Scalar.h" 31 #include "llvm/Pass.h" 32 #include "llvm/Function.h" 33 #include "llvm/Instructions.h" 34 #include "llvm/ADT/SetVector.h" 35 #include "llvm/ADT/Statistic.h" 36 #include "llvm/Analysis/Dominators.h" 37 #include "llvm/Analysis/LoopInfo.h" 38 #include "llvm/Support/CFG.h" 39 #include <algorithm> 40 #include <map> 41 42 using namespace llvm; 43 44 namespace { 45 static Statistic<> NumLCSSA("lcssa", 46 "Number of live out of a loop variables"); 47 48 class LCSSA : public FunctionPass { 49 public: 50 51 52 LoopInfo *LI; // Loop information 53 DominatorTree *DT; // Dominator Tree for the current Function... 54 DominanceFrontier *DF; // Current Dominance Frontier 55 std::vector<BasicBlock*> LoopBlocks; 56 57 virtual bool runOnFunction(Function &F); 58 bool visitSubloop(Loop* L); 59 void processInstruction(Instruction* Instr, 60 const std::vector<BasicBlock*>& exitBlocks); 61 62 /// This transformation requires natural loop information & requires that 63 /// loop preheaders be inserted into the CFG. It maintains both of these, 64 /// as well as the CFG. It also requires dominator information. 65 /// 66 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 67 AU.setPreservesCFG(); 68 AU.addRequiredID(LoopSimplifyID); 69 AU.addPreservedID(LoopSimplifyID); 70 AU.addRequired<LoopInfo>(); 71 AU.addRequired<DominatorTree>(); 72 AU.addRequired<DominanceFrontier>(); 73 } 74 private: 75 SetVector<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L); 76 Instruction *getValueDominatingBlock(BasicBlock *BB, 77 std::map<BasicBlock*, Instruction*>& PotDoms) { 78 return getValueDominatingDTNode(DT->getNode(BB), PotDoms); 79 } 80 Instruction *getValueDominatingDTNode(DominatorTree::Node *Node, 81 std::map<BasicBlock*, Instruction*>& PotDoms); 82 83 /// inLoop - returns true if the given block is within the current loop 84 const bool inLoop(BasicBlock* B) { 85 return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B); 86 } 87 }; 88 89 RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass"); 90 } 91 92 FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); } 93 const PassInfo *llvm::LCSSAID = X.getPassInfo(); 94 95 /// runOnFunction - Process all loops in the function, inner-most out. 96 bool LCSSA::runOnFunction(Function &F) { 97 bool changed = false; 98 99 LI = &getAnalysis<LoopInfo>(); 100 DF = &getAnalysis<DominanceFrontier>(); 101 DT = &getAnalysis<DominatorTree>(); 102 103 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) { 104 changed |= visitSubloop(*I); 105 } 106 107 return changed; 108 } 109 110 /// visitSubloop - Recursively process all subloops, and then process the given 111 /// loop if it has live-out values. 112 bool LCSSA::visitSubloop(Loop* L) { 113 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) 114 visitSubloop(*I); 115 116 // Speed up queries by creating a sorted list of blocks 117 LoopBlocks.clear(); 118 LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); 119 std::sort(LoopBlocks.begin(), LoopBlocks.end()); 120 121 SetVector<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L); 122 123 // If no values are affected, we can save a lot of work, since we know that 124 // nothing will be changed. 125 if (AffectedValues.empty()) 126 return false; 127 128 std::vector<BasicBlock*> exitBlocks; 129 L->getExitBlocks(exitBlocks); 130 131 132 // Iterate over all affected values for this loop and insert Phi nodes 133 // for them in the appropriate exit blocks 134 135 for (SetVector<Instruction*>::iterator I = AffectedValues.begin(), 136 E = AffectedValues.end(); I != E; ++I) { 137 processInstruction(*I, exitBlocks); 138 } 139 140 assert(L->isLCSSAForm()); 141 142 return true; 143 } 144 145 /// processInstruction - Given a live-out instruction, insert LCSSA Phi nodes, 146 /// eliminate all out-of-loop uses. 147 void LCSSA::processInstruction(Instruction* Instr, 148 const std::vector<BasicBlock*>& exitBlocks) 149 { 150 ++NumLCSSA; // We are applying the transformation 151 152 std::map<BasicBlock*, Instruction*> Phis; 153 154 // Add the base instruction to the Phis list. This makes tracking down 155 // the dominating values easier when we're filling in Phi nodes. This will 156 // be removed later, before we perform use replacement. 157 Phis[Instr->getParent()] = Instr; 158 159 // Phi nodes that need to be IDF-processed 160 std::vector<PHINode*> workList; 161 162 for (std::vector<BasicBlock*>::const_iterator BBI = exitBlocks.begin(), 163 BBE = exitBlocks.end(); BBI != BBE; ++BBI) { 164 Instruction*& phi = Phis[*BBI]; 165 if (phi == 0 && 166 DT->getNode(Instr->getParent())->dominates(DT->getNode(*BBI))) { 167 phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa", 168 (*BBI)->begin()); 169 workList.push_back(cast<PHINode>(phi)); 170 } 171 } 172 173 // Phi nodes that need to have their incoming values filled. 174 std::vector<PHINode*> needIncomingValues; 175 176 // Calculate the IDF of these LCSSA Phi nodes, inserting new Phi's where 177 // necessary. Keep track of these new Phi's in the "Phis" map. 178 while (!workList.empty()) { 179 PHINode *CurPHI = workList.back(); 180 workList.pop_back(); 181 182 // Even though we've removed this Phi from the work list, we still need 183 // to fill in its incoming values. 184 needIncomingValues.push_back(CurPHI); 185 186 // Get the current Phi's DF, and insert Phi nodes. Add these new 187 // nodes to our worklist. 188 DominanceFrontier::const_iterator it = DF->find(CurPHI->getParent()); 189 if (it != DF->end()) { 190 const DominanceFrontier::DomSetType &S = it->second; 191 for (DominanceFrontier::DomSetType::const_iterator P = S.begin(), 192 PE = S.end(); P != PE; ++P) { 193 if (DT->getNode(Instr->getParent())->dominates(DT->getNode(*P))) { 194 Instruction *&Phi = Phis[*P]; 195 if (Phi == 0) { 196 // Still doesn't have operands... 197 Phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa", 198 (*P)->begin()); 199 200 workList.push_back(cast<PHINode>(Phi)); 201 } 202 } 203 } 204 } 205 } 206 207 // Fill in all Phis we've inserted that need their incoming values filled in. 208 for (std::vector<PHINode*>::iterator IVI = needIncomingValues.begin(), 209 IVE = needIncomingValues.end(); IVI != IVE; ++IVI) { 210 for (pred_iterator PI = pred_begin((*IVI)->getParent()), 211 E = pred_end((*IVI)->getParent()); PI != E; ++PI) 212 (*IVI)->addIncoming(getValueDominatingBlock(*PI, Phis), 213 *PI); 214 } 215 216 // Find all uses of the affected value, and replace them with the 217 // appropriate Phi. 218 std::vector<Instruction*> Uses; 219 for (Instruction::use_iterator UI = Instr->use_begin(), UE = Instr->use_end(); 220 UI != UE; ++UI) { 221 Instruction* use = cast<Instruction>(*UI); 222 BasicBlock* UserBB = use->getParent(); 223 if (PHINode* p = dyn_cast<PHINode>(use)) { 224 unsigned OperandNo = UI.getOperandNo(); 225 UserBB = p->getIncomingBlock(OperandNo/2); 226 } 227 228 // Don't need to update uses within the loop body. 229 if (!inLoop(use->getParent())) 230 Uses.push_back(use); 231 } 232 233 for (std::vector<Instruction*>::iterator II = Uses.begin(), IE = Uses.end(); 234 II != IE; ++II) { 235 if (PHINode* phi = dyn_cast<PHINode>(*II)) { 236 for (unsigned int i = 0; i < phi->getNumIncomingValues(); ++i) { 237 if (phi->getIncomingValue(i) == Instr) { 238 Instruction* dominator = 239 getValueDominatingBlock(phi->getIncomingBlock(i), Phis); 240 phi->setIncomingValue(i, dominator); 241 } 242 } 243 } else { 244 Value *NewVal = getValueDominatingBlock((*II)->getParent(), Phis); 245 (*II)->replaceUsesOfWith(Instr, NewVal); 246 } 247 } 248 } 249 250 /// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that 251 /// are used by instructions outside of it. 252 SetVector<Instruction*> LCSSA::getLoopValuesUsedOutsideLoop(Loop *L) { 253 254 // FIXME: For large loops, we may be able to avoid a lot of use-scanning 255 // by using dominance information. In particular, if a block does not 256 // dominate any of the loop exits, then none of the values defined in the 257 // block could be used outside the loop. 258 259 SetVector<Instruction*> AffectedValues; 260 for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); 261 BB != E; ++BB) { 262 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I) 263 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 264 ++UI) { 265 BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); 266 if (PHINode* p = dyn_cast<PHINode>(*UI)) { 267 unsigned OperandNo = UI.getOperandNo(); 268 UserBB = p->getIncomingBlock(OperandNo/2); 269 } 270 271 if (!inLoop(UserBB)) { 272 AffectedValues.insert(I); 273 break; 274 } 275 } 276 } 277 return AffectedValues; 278 } 279 280 /// getValueDominatingBlock - Return the value within the potential dominators 281 /// map that dominates the given block. 282 Instruction *LCSSA::getValueDominatingDTNode(DominatorTree::Node *Node, 283 std::map<BasicBlock*, Instruction*>& PotDoms) { 284 assert(Node != 0 && "Didn't find dom value?"); 285 Instruction *&CacheSlot = PotDoms[Node->getBlock()]; 286 if (CacheSlot) return CacheSlot; 287 288 // Otherwise, return the value of the idom and remember this for next time. 289 return CacheSlot = getValueDominatingDTNode(Node->getIDom(), PotDoms); 290 } 291