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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 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 (...) |
Dan Gohman | b5650eb | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 15 | // if (c) if (c) |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 16 | // X1 = ... X1 = ... |
| 17 | // else else |
| 18 | // X2 = ... X2 = ... |
| 19 | // X3 = phi(X1, X2) X3 = phi(X1, X2) |
Dan Gohman | 2ad7e73 | 2008-06-03 00:57:21 +0000 | [diff] [blame] | 20 | // ... = X3 + 4 X4 = phi(X3) |
| 21 | // ... = X4 + 4 |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 756c22c | 2014-02-10 19:39:35 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/AliasAnalysis.h" |
Devang Patel | 4cd1413 | 2007-07-13 23:57:11 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/LoopPass.h" |
| 35 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Function.h" |
| 39 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | aa0ab63 | 2014-03-04 12:09:19 +0000 | [diff] [blame] | 40 | #include "llvm/IR/PredIteratorCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include "llvm/Pass.h" |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "lcssa" |
| 47 | |
Chris Lattner | 45f966d | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 48 | STATISTIC(NumLCSSA, "Number of live out of a loop variables"); |
| 49 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 50 | /// Return true if the specified block is in the list. |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 51 | static bool isExitBlock(BasicBlock *BB, |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 52 | const SmallVectorImpl<BasicBlock *> &ExitBlocks) { |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 54 | if (ExitBlocks[i] == BB) |
| 55 | return true; |
| 56 | return false; |
| 57 | } |
| 58 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 59 | /// Given an instruction in the loop, check to see if it has any uses that are |
| 60 | /// outside the current loop. If so, insert LCSSA PHI nodes and rewrite the |
| 61 | /// uses. |
| 62 | static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT, |
| 63 | const SmallVectorImpl<BasicBlock *> &ExitBlocks, |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 64 | PredIteratorCache &PredCache, LoopInfo *LI) { |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 65 | SmallVector<Use *, 16> UsesToRewrite; |
| 66 | |
| 67 | BasicBlock *InstBB = Inst.getParent(); |
| 68 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 69 | for (Use &U : Inst.uses()) { |
| 70 | Instruction *User = cast<Instruction>(U.getUser()); |
| 71 | BasicBlock *UserBB = User->getParent(); |
| 72 | if (PHINode *PN = dyn_cast<PHINode>(User)) |
| 73 | UserBB = PN->getIncomingBlock(U); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 74 | |
| 75 | if (InstBB != UserBB && !L.contains(UserBB)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 76 | UsesToRewrite.push_back(&U); |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 77 | } |
Gabor Greif | 4247949 | 2010-07-09 14:29:14 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 79 | // If there are no uses outside the loop, exit with no change. |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 80 | if (UsesToRewrite.empty()) |
| 81 | return false; |
| 82 | |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 83 | ++NumLCSSA; // We are applying the transformation |
Chris Lattner | 5a2bc78 | 2006-08-02 00:06:09 +0000 | [diff] [blame] | 84 | |
Dan Gohman | 7eaf50e | 2009-06-26 00:31:13 +0000 | [diff] [blame] | 85 | // Invoke instructions are special in that their result value is not available |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 86 | // along their unwind edge. The code below tests to see whether DomBB |
| 87 | // dominates |
Dan Gohman | 7eaf50e | 2009-06-26 00:31:13 +0000 | [diff] [blame] | 88 | // the value, so adjust DomBB to the normal destination block, which is |
| 89 | // effectively where the value is first usable. |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 90 | BasicBlock *DomBB = Inst.getParent(); |
| 91 | if (InvokeInst *Inv = dyn_cast<InvokeInst>(&Inst)) |
Dan Gohman | 7eaf50e | 2009-06-26 00:31:13 +0000 | [diff] [blame] | 92 | DomBB = Inv->getNormalDest(); |
| 93 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 94 | DomTreeNode *DomNode = DT.getNode(DomBB); |
Chris Lattner | 5a2bc78 | 2006-08-02 00:06:09 +0000 | [diff] [blame] | 95 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 96 | SmallVector<PHINode *, 16> AddedPHIs; |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 97 | SmallVector<PHINode *, 8> PostProcessPHIs; |
Cameron Zwarich | 0b8cdfb | 2011-03-15 07:41:25 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 99 | SSAUpdater SSAUpdate; |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 100 | SSAUpdate.Initialize(Inst.getType(), Inst.getName()); |
| 101 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 102 | // Insert the LCSSA phi's into all of the exit blocks dominated by the |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 103 | // value, and add them to the Phi's map. |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 104 | for (SmallVectorImpl<BasicBlock *>::const_iterator BBI = ExitBlocks.begin(), |
| 105 | BBE = ExitBlocks.end(); |
| 106 | BBI != BBE; ++BBI) { |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 107 | BasicBlock *ExitBB = *BBI; |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 108 | if (!DT.dominates(DomNode, DT.getNode(ExitBB))) |
| 109 | continue; |
| 110 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 111 | // If we already inserted something for this BB, don't reprocess it. |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 112 | if (SSAUpdate.HasValueForBlock(ExitBB)) |
| 113 | continue; |
| 114 | |
| 115 | PHINode *PN = PHINode::Create(Inst.getType(), PredCache.GetNumPreds(ExitBB), |
| 116 | Inst.getName() + ".lcssa", ExitBB->begin()); |
Chris Lattner | 984d6e1 | 2006-10-31 18:56:48 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 118 | // Add inputs from inside the loop for this PHI. |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 119 | for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) { |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 120 | PN->addIncoming(&Inst, *PI); |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 121 | |
| 122 | // If the exit block has a predecessor not within the loop, arrange for |
Dan Gohman | f324dd6 | 2009-11-09 18:59:22 +0000 | [diff] [blame] | 123 | // the incoming value use corresponding to that predecessor to be |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 124 | // rewritten in terms of a different LCSSA PHI. |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 125 | if (!L.contains(*PI)) |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 126 | UsesToRewrite.push_back( |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 127 | &PN->getOperandUse(PN->getOperandNumForIncomingValue( |
| 128 | PN->getNumIncomingValues() - 1))); |
Dan Gohman | c146c780 | 2009-11-09 18:28:24 +0000 | [diff] [blame] | 129 | } |
Cameron Zwarich | 0b8cdfb | 2011-03-15 07:41:25 +0000 | [diff] [blame] | 130 | |
| 131 | AddedPHIs.push_back(PN); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 133 | // Remember that this phi makes the value alive in this block. |
| 134 | SSAUpdate.AddAvailableValue(ExitBB, PN); |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 135 | |
| 136 | // LoopSimplify might fail to simplify some loops (e.g. when indirect |
| 137 | // branches are involved). In such situations, it might happen that an exit |
| 138 | // for Loop L1 is the header of a disjoint Loop L2. Thus, when we create |
| 139 | // PHIs in such an exit block, we are also inserting PHIs into L2's header. |
| 140 | // This could break LCSSA form for L2 because these inserted PHIs can also |
| 141 | // have uses outside of L2. Remember all PHIs in such situation as to |
| 142 | // revisit than later on. FIXME: Remove this if indirectbr support into |
| 143 | // LoopSimplify gets improved. |
| 144 | if (auto *OtherLoop = LI->getLoopFor(ExitBB)) |
| 145 | if (!L.contains(OtherLoop)) |
| 146 | PostProcessPHIs.push_back(PN); |
Owen Anderson | 0ac3369 | 2006-06-12 07:10:16 +0000 | [diff] [blame] | 147 | } |
Benjamin Kramer | 8682ac1 | 2012-10-31 10:01:29 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 149 | // Rewrite all uses outside the loop in terms of the new PHIs we just |
| 150 | // inserted. |
| 151 | for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) { |
| 152 | // If this use is in an exit block, rewrite to use the newly inserted PHI. |
| 153 | // This is required for correctness because SSAUpdate doesn't handle uses in |
| 154 | // the same block. It assumes the PHI we inserted is at the end of the |
| 155 | // block. |
| 156 | Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser()); |
| 157 | BasicBlock *UserBB = User->getParent(); |
| 158 | if (PHINode *PN = dyn_cast<PHINode>(User)) |
| 159 | UserBB = PN->getIncomingBlock(*UsesToRewrite[i]); |
| 160 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 161 | if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) { |
Benjamin Kramer | ede2fe3bfd | 2012-10-31 16:30:03 +0000 | [diff] [blame] | 162 | // Tell the VHs that the uses changed. This updates SCEV's caches. |
| 163 | if (UsesToRewrite[i]->get()->hasValueHandle()) |
| 164 | ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin()); |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 165 | UsesToRewrite[i]->set(UserBB->begin()); |
Chris Lattner | 5a2bc78 | 2006-08-02 00:06:09 +0000 | [diff] [blame] | 166 | continue; |
Owen Anderson | cd76fa0 | 2006-06-01 06:05:47 +0000 | [diff] [blame] | 167 | } |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 169 | // Otherwise, do full PHI insertion. |
| 170 | SSAUpdate.RewriteUse(*UsesToRewrite[i]); |
Chris Lattner | 5a2bc78 | 2006-08-02 00:06:09 +0000 | [diff] [blame] | 171 | } |
Cameron Zwarich | 0b8cdfb | 2011-03-15 07:41:25 +0000 | [diff] [blame] | 172 | |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 173 | // Post process PHI instructions that were inserted into another disjoint loop |
| 174 | // and update their exits properly. |
| 175 | for (auto *I : PostProcessPHIs) { |
| 176 | if (I->use_empty()) |
| 177 | continue; |
| 178 | |
| 179 | BasicBlock *PHIBB = I->getParent(); |
| 180 | Loop *OtherLoop = LI->getLoopFor(PHIBB); |
| 181 | SmallVector<BasicBlock *, 8> EBs; |
| 182 | OtherLoop->getExitBlocks(EBs); |
| 183 | if (EBs.empty()) |
| 184 | continue; |
| 185 | |
| 186 | // Recurse and re-process each PHI instruction. FIXME: we should really |
| 187 | // convert this entire thing to a worklist approach where we process a |
| 188 | // vector of instructions... |
| 189 | processInstruction(*OtherLoop, *I, DT, EBs, PredCache, LI); |
| 190 | } |
| 191 | |
Cameron Zwarich | 0b8cdfb | 2011-03-15 07:41:25 +0000 | [diff] [blame] | 192 | // Remove PHI nodes that did not have any uses rewritten. |
| 193 | for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) { |
Cameron Zwarich | dbb2739 | 2011-03-15 18:42:33 +0000 | [diff] [blame] | 194 | if (AddedPHIs[i]->use_empty()) |
Cameron Zwarich | 0b8cdfb | 2011-03-15 07:41:25 +0000 | [diff] [blame] | 195 | AddedPHIs[i]->eraseFromParent(); |
| 196 | } |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 71d353d | 2009-10-11 02:53:37 +0000 | [diff] [blame] | 198 | return true; |
Owen Anderson | dad8c57 | 2006-05-31 20:55:06 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 5a2bc78 | 2006-08-02 00:06:09 +0000 | [diff] [blame] | 200 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 201 | /// Return true if the specified block dominates at least |
| 202 | /// one of the blocks in the specified list. |
| 203 | static bool |
| 204 | blockDominatesAnExit(BasicBlock *BB, |
| 205 | DominatorTree &DT, |
| 206 | const SmallVectorImpl<BasicBlock *> &ExitBlocks) { |
| 207 | DomTreeNode *DomNode = DT.getNode(BB); |
| 208 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 209 | if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i]))) |
| 210 | return true; |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 215 | bool llvm::formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, |
| 216 | ScalarEvolution *SE) { |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 217 | bool Changed = false; |
| 218 | |
| 219 | // Get the set of exiting blocks. |
| 220 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 221 | L.getExitBlocks(ExitBlocks); |
| 222 | |
| 223 | if (ExitBlocks.empty()) |
| 224 | return false; |
| 225 | |
| 226 | PredIteratorCache PredCache; |
| 227 | |
| 228 | // Look at all the instructions in the loop, checking to see if they have uses |
| 229 | // outside the loop. If so, rewrite those uses. |
| 230 | for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end(); |
| 231 | BBI != BBE; ++BBI) { |
| 232 | BasicBlock *BB = *BBI; |
| 233 | |
| 234 | // For large loops, avoid use-scanning by using dominance information: In |
| 235 | // particular, if a block does not dominate any of the loop exits, then none |
| 236 | // of the values defined in the block could be used outside the loop. |
| 237 | if (!blockDominatesAnExit(BB, DT, ExitBlocks)) |
| 238 | continue; |
| 239 | |
| 240 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 241 | // Reject two common cases fast: instructions with no uses (like stores) |
| 242 | // and instructions with one use that is in the same block as this. |
| 243 | if (I->use_empty() || |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 244 | (I->hasOneUse() && I->user_back()->getParent() == BB && |
| 245 | !isa<PHINode>(I->user_back()))) |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 246 | continue; |
| 247 | |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 248 | Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache, LI); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // If we modified the code, remove any caches about the loop from SCEV to |
| 253 | // avoid dangling entries. |
| 254 | // FIXME: This is a big hammer, can we clear the cache more selectively? |
| 255 | if (SE && Changed) |
| 256 | SE->forgetLoop(&L); |
| 257 | |
| 258 | assert(L.isLCSSAForm(DT)); |
| 259 | |
| 260 | return Changed; |
| 261 | } |
| 262 | |
Chandler Carruth | d84f776 | 2014-01-28 01:25:38 +0000 | [diff] [blame] | 263 | /// Process a loop nest depth first. |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 264 | bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, |
Chandler Carruth | d84f776 | 2014-01-28 01:25:38 +0000 | [diff] [blame] | 265 | ScalarEvolution *SE) { |
| 266 | bool Changed = false; |
| 267 | |
| 268 | // Recurse depth-first through inner loops. |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 269 | for (Loop::iterator I = L.begin(), E = L.end(); I != E; ++I) |
| 270 | Changed |= formLCSSARecursively(**I, DT, LI, SE); |
Chandler Carruth | d84f776 | 2014-01-28 01:25:38 +0000 | [diff] [blame] | 271 | |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 272 | Changed |= formLCSSA(L, DT, LI, SE); |
Chandler Carruth | d84f776 | 2014-01-28 01:25:38 +0000 | [diff] [blame] | 273 | return Changed; |
| 274 | } |
| 275 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 276 | namespace { |
| 277 | struct LCSSA : public FunctionPass { |
| 278 | static char ID; // Pass identification, replacement for typeid |
| 279 | LCSSA() : FunctionPass(ID) { |
| 280 | initializeLCSSAPass(*PassRegistry::getPassRegistry()); |
| 281 | } |
| 282 | |
| 283 | // Cached analysis information for the current function. |
| 284 | DominatorTree *DT; |
| 285 | LoopInfo *LI; |
| 286 | ScalarEvolution *SE; |
| 287 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 288 | bool runOnFunction(Function &F) override; |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 289 | |
| 290 | /// This transformation requires natural loop information & requires that |
| 291 | /// loop preheaders be inserted into the CFG. It maintains both of these, |
| 292 | /// as well as the CFG. It also requires dominator information. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 293 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 294 | AU.setPreservesCFG(); |
| 295 | |
| 296 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 297 | AU.addRequired<LoopInfo>(); |
| 298 | AU.addPreservedID(LoopSimplifyID); |
Chandler Carruth | 756c22c | 2014-02-10 19:39:35 +0000 | [diff] [blame] | 299 | AU.addPreserved<AliasAnalysis>(); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 300 | AU.addPreserved<ScalarEvolution>(); |
| 301 | } |
| 302 | |
| 303 | private: |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 304 | void verifyAnalysis() const override; |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 305 | }; |
| 306 | } |
| 307 | |
| 308 | char LCSSA::ID = 0; |
| 309 | INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false) |
| 310 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 311 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 312 | INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false) |
| 313 | |
| 314 | Pass *llvm::createLCSSAPass() { return new LCSSA(); } |
| 315 | char &llvm::LCSSAID = LCSSA::ID; |
| 316 | |
| 317 | |
| 318 | /// Process all loops in the function, inner-most out. |
| 319 | bool LCSSA::runOnFunction(Function &F) { |
| 320 | bool Changed = false; |
| 321 | LI = &getAnalysis<LoopInfo>(); |
| 322 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 323 | SE = getAnalysisIfAvailable<ScalarEvolution>(); |
| 324 | |
| 325 | // Simplify each loop nest in the function. |
| 326 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
Bruno Cardoso Lopes | bad65c3 | 2014-12-22 22:35:46 +0000 | [diff] [blame^] | 327 | Changed |= formLCSSARecursively(**I, *DT, LI, SE); |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 328 | |
| 329 | return Changed; |
| 330 | } |
| 331 | |
Chandler Carruth | 8765cf7 | 2014-01-25 04:07:24 +0000 | [diff] [blame] | 332 | static void verifyLoop(Loop &L, DominatorTree &DT) { |
| 333 | // Recurse depth-first through inner loops. |
| 334 | for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI) |
| 335 | verifyLoop(**LI, DT); |
| 336 | |
| 337 | // Check the special guarantees that LCSSA makes. |
| 338 | //assert(L.isLCSSAForm(DT) && "LCSSA form not preserved!"); |
| 339 | } |
| 340 | |
| 341 | void LCSSA::verifyAnalysis() const { |
| 342 | // Verify each loop nest in the function, assuming LI still points at that |
| 343 | // function's loop info. |
| 344 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 345 | verifyLoop(**I, *DT); |
| 346 | } |