Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 1 | //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===// |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 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 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
| 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/Instructions.h" |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/Dominators.h" |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/LoopInfo.h" |
| 37 | #include "llvm/Support/CFG.h" |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Owen Anderson | 1310e42 | 2006-05-27 18:47:11 +0000 | [diff] [blame] | 39 | #include <map> |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | namespace { |
Owen Anderson | 152d063 | 2006-05-28 19:33:28 +0000 | [diff] [blame] | 44 | static Statistic<> NumLCSSA("lcssa", |
| 45 | "Number of live out of a loop variables"); |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame] | 46 | |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 47 | class LCSSA : public FunctionPass { |
| 48 | public: |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame] | 49 | |
| 50 | |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 51 | LoopInfo *LI; // Loop information |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 52 | DominatorTree *DT; // Dominator Tree for the current Loop... |
| 53 | DominanceFrontier *DF; // Current Dominance Frontier |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 54 | |
| 55 | virtual bool runOnFunction(Function &F); |
Owen Anderson | 6e047ab | 2006-05-26 21:19:17 +0000 | [diff] [blame] | 56 | bool visitSubloop(Loop* L); |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 57 | void processInstruction(Instruction* Instr, |
| 58 | const std::vector<BasicBlock*>& LoopBlocks, |
| 59 | const std::vector<BasicBlock*>& exitBlocks); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 60 | |
| 61 | /// This transformation requires natural loop information & requires that |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame] | 62 | /// loop preheaders be inserted into the CFG. It maintains both of these, |
| 63 | /// as well as the CFG. It also requires dominator information. |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 64 | /// |
| 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 66 | AU.setPreservesCFG(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 67 | AU.addRequiredID(LoopSimplifyID); |
| 68 | AU.addPreservedID(LoopSimplifyID); |
| 69 | AU.addRequired<LoopInfo>(); |
Owen Anderson | 1310e42 | 2006-05-27 18:47:11 +0000 | [diff] [blame] | 70 | AU.addRequired<DominatorTree>(); |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 71 | AU.addRequired<DominanceFrontier>(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 72 | } |
| 73 | private: |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 74 | std::set<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L, |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 75 | const std::vector<BasicBlock*>& LoopBlocks); |
| 76 | Instruction *getValueDominatingBlock(BasicBlock *BB, |
| 77 | std::map<BasicBlock*, Instruction*> PotDoms); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass"); |
| 81 | } |
| 82 | |
| 83 | FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); } |
| 84 | |
| 85 | bool LCSSA::runOnFunction(Function &F) { |
| 86 | bool changed = false; |
| 87 | LI = &getAnalysis<LoopInfo>(); |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 88 | DF = &getAnalysis<DominanceFrontier>(); |
| 89 | DT = &getAnalysis<DominatorTree>(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 90 | |
| 91 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) { |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 92 | changed |= visitSubloop(*I); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return changed; |
| 96 | } |
| 97 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 98 | bool LCSSA::visitSubloop(Loop* L) { |
| 99 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 100 | visitSubloop(*I); |
| 101 | |
| 102 | // Speed up queries by creating a sorted list of blocks |
| 103 | std::vector<BasicBlock*> LoopBlocks(L->block_begin(), L->block_end()); |
| 104 | std::sort(LoopBlocks.begin(), LoopBlocks.end()); |
| 105 | |
| 106 | std::set<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L, |
| 107 | LoopBlocks); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 108 | |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 109 | // If no values are affected, we can save a lot of work, since we know that |
| 110 | // nothing will be changed. |
| 111 | if (AffectedValues.empty()) |
| 112 | return false; |
| 113 | |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 114 | std::vector<BasicBlock*> exitBlocks; |
| 115 | L->getExitBlocks(exitBlocks); |
| 116 | |
Owen Anderson | 1310e42 | 2006-05-27 18:47:11 +0000 | [diff] [blame] | 117 | |
| 118 | // Iterate over all affected values for this loop and insert Phi nodes |
| 119 | // for them in the appropriate exit blocks |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 120 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 121 | for (std::set<Instruction*>::iterator I = AffectedValues.begin(), |
| 122 | E = AffectedValues.end(); I != E; ++I) { |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 123 | processInstruction(*I, LoopBlocks, exitBlocks); |
Owen Anderson | 1310e42 | 2006-05-27 18:47:11 +0000 | [diff] [blame] | 124 | } |
Owen Anderson | 8a8f278 | 2006-05-29 01:00:00 +0000 | [diff] [blame] | 125 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 126 | return true; // FIXME: Should be more intelligent in our return value. |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 129 | /// processInstruction - |
| 130 | void LCSSA::processInstruction(Instruction* Instr, |
| 131 | const std::vector<BasicBlock*>& LoopBlocks, |
| 132 | const std::vector<BasicBlock*>& exitBlocks) |
| 133 | { |
| 134 | ++NumLCSSA; // We are applying the transformation |
| 135 | |
| 136 | std::map<BasicBlock*, Instruction*> Phis; |
Owen Anderson | d00eacc | 2006-06-03 23:22:50 +0000 | [diff] [blame^] | 137 | |
| 138 | // Add the base instruction to the Phis list. This makes tracking down |
| 139 | // the dominating values easier when we're filling in Phi nodes. This will |
| 140 | // be removed later, before we perform use replacement. |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 141 | Phis[Instr->getParent()] = Instr; |
| 142 | |
| 143 | // Phi nodes that need to be IDF-processed |
| 144 | std::vector<PHINode*> workList; |
| 145 | |
| 146 | for (std::vector<BasicBlock*>::const_iterator BBI = exitBlocks.begin(), |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 147 | BBE = exitBlocks.end(); BBI != BBE; ++BBI) |
| 148 | if (DT->getNode(Instr->getParent())->dominates(DT->getNode(*BBI))) { |
| 149 | PHINode *phi = new PHINode(Instr->getType(), "lcssa", (*BBI)->begin()); |
| 150 | workList.push_back(phi); |
| 151 | Phis[*BBI] = phi; |
| 152 | } |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 153 | |
Owen Anderson | d00eacc | 2006-06-03 23:22:50 +0000 | [diff] [blame^] | 154 | // Phi nodes that need to have their incoming values filled. |
| 155 | std::vector<PHINode*> needIncomingValues; |
| 156 | |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 157 | // Calculate the IDF of these LCSSA Phi nodes, inserting new Phi's where |
| 158 | // necessary. Keep track of these new Phi's in Phis. |
| 159 | while (!workList.empty()) { |
| 160 | PHINode *CurPHI = workList.back(); |
| 161 | workList.pop_back(); |
| 162 | |
Owen Anderson | d00eacc | 2006-06-03 23:22:50 +0000 | [diff] [blame^] | 163 | // Even though we've removed this Phi from the work list, we still need |
| 164 | // to fill in its incoming values. |
| 165 | needIncomingValues.push_back(CurPHI); |
| 166 | |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 167 | // Get the current Phi's DF, and insert Phi nodes. Add these new |
| 168 | // nodes to our worklist. |
| 169 | DominanceFrontier::const_iterator it = DF->find(CurPHI->getParent()); |
| 170 | if (it != DF->end()) { |
| 171 | const DominanceFrontier::DomSetType &S = it->second; |
| 172 | for (DominanceFrontier::DomSetType::const_iterator P = S.begin(), |
| 173 | PE = S.end(); P != PE; ++P) { |
| 174 | if (Phis[*P] == 0) { |
| 175 | // Still doesn't have operands... |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 176 | PHINode *phi = new PHINode(Instr->getType(), "lcssa", (*P)->begin()); |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 177 | Phis[*P] = phi; |
| 178 | |
| 179 | workList.push_back(phi); |
| 180 | } |
| 181 | } |
| 182 | } |
Owen Anderson | d00eacc | 2006-06-03 23:22:50 +0000 | [diff] [blame^] | 183 | } |
| 184 | |
| 185 | // Fill in all Phis we've inserted that need their incoming values filled in. |
| 186 | for (std::vector<PHINode*>::iterator IVI = needIncomingValues.begin(), |
| 187 | IVE = needIncomingValues.end(); IVI != IVE; ++IVI) { |
| 188 | for (pred_iterator PI = pred_begin((*IVI)->getParent()), |
| 189 | E = pred_end((*IVI)->getParent()); PI != E; ++PI) |
| 190 | (*IVI)->addIncoming(getValueDominatingBlock(*PI, Phis), |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 191 | *PI); |
| 192 | } |
| 193 | |
| 194 | // Find all uses of the affected value, and replace them with the |
| 195 | // appropriate Phi. |
| 196 | std::vector<Instruction*> Uses; |
| 197 | for (Instruction::use_iterator UI = Instr->use_begin(), UE = Instr->use_end(); |
| 198 | UI != UE; ++UI) { |
| 199 | Instruction* use = cast<Instruction>(*UI); |
| 200 | // Don't need to update uses within the loop body |
| 201 | if (!std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), |
| 202 | use->getParent()) && |
| 203 | !(std::binary_search(exitBlocks.begin(), exitBlocks.end(), |
| 204 | use->getParent()) && isa<PHINode>(use))) |
| 205 | Uses.push_back(use); |
| 206 | } |
| 207 | |
Owen Anderson | d00eacc | 2006-06-03 23:22:50 +0000 | [diff] [blame^] | 208 | // Deliberately remove the initial instruction from Phis set. It would mess |
| 209 | // up use-replacement. |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 210 | Phis.erase(Instr->getParent()); |
| 211 | |
| 212 | for (std::vector<Instruction*>::iterator II = Uses.begin(), IE = Uses.end(); |
| 213 | II != IE; ++II) { |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 214 | if (PHINode* phi = dyn_cast<PHINode>(*II)) { |
| 215 | for (unsigned int i = 0; i < phi->getNumIncomingValues(); ++i) { |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 216 | Instruction* dominator = |
| 217 | getValueDominatingBlock(phi->getIncomingBlock(i), Phis); |
| 218 | |
| 219 | if (phi->getIncomingValue(i) == Instr) |
| 220 | phi->setIncomingValue(i, dominator); |
| 221 | } |
| 222 | } else { |
| 223 | (*II)->replaceUsesOfWith(Instr, |
| 224 | getValueDominatingBlock((*II)->getParent(), |
| 225 | Phis)); |
| 226 | } |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 230 | /// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that |
| 231 | /// are used by instructions outside of it. |
| 232 | std::set<Instruction*> LCSSA::getLoopValuesUsedOutsideLoop(Loop *L, |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 233 | const std::vector<BasicBlock*>& LoopBlocks) { |
| 234 | |
| 235 | // FIXME: For large loops, we may be able to avoid a lot of use-scanning |
| 236 | // by using dominance information. In particular, if a block does not |
| 237 | // dominate any of the loop exits, then none of the values defined in the |
| 238 | // block could be used outside the loop. |
| 239 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 240 | std::set<Instruction*> AffectedValues; |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 241 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 242 | BB != E; ++BB) { |
| 243 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I) |
| 244 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 245 | ++UI) { |
| 246 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 247 | if (!std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), UserBB)) { |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 248 | AffectedValues.insert(I); |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | return AffectedValues; |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 254 | } |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 255 | |
| 256 | Instruction *LCSSA::getValueDominatingBlock(BasicBlock *BB, |
| 257 | std::map<BasicBlock*, Instruction*> PotDoms) { |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 258 | DominatorTree::Node* bbNode = DT->getNode(BB); |
| 259 | while (bbNode != 0) { |
| 260 | std::map<BasicBlock*, Instruction*>::iterator I = |
| 261 | PotDoms.find(bbNode->getBlock()); |
| 262 | if (I != PotDoms.end()) { |
| 263 | return (*I).second; |
| 264 | } |
| 265 | bbNode = bbNode->getIDom(); |
| 266 | } |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 267 | |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 268 | assert(0 && "No dominating value found."); |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |