llvm.org GIT mirror llvm / 3d69cf5
[LPM] Make LCSSA a utility with a FunctionPass that applies it to all the loops in a function, and teach LICM to work in the presance of LCSSA. Previously, LCSSA was a loop pass. That made passes requiring it also be loop passes and unable to depend on function analysis passes easily. It also caused outer loops to have a different "canonical" form from inner loops during analysis. Instead, we go into LCSSA form and preserve it through the loop pass manager run. Note that this has the same problem as LoopSimplify that prevents enabling its verification -- loop passes which run at the end of the loop pass manager and don't preserve these are valid, but the subsequent loop pass runs of outer loops that do preserve this pass trigger too much verification and fail because the inner loop no longer verifies. The other problem this exposed is that LICM was completely unable to handle LCSSA form. It didn't preserve it and it actually would give up on moving instructions in many cases when they were used by an LCSSA phi node. I've taught LICM to support detecting LCSSA-form PHI nodes and to hoist and sink around them. This may actually let LICM fire significantly more because we put everything into LCSSA form to rotate the loop before running LICM. =/ Now LICM should handle that fine and preserve it correctly. The down side is that LICM has to require LCSSA in order to preserve it. This is just a fact of life for LCSSA. It's entirely possible we should completely remove LCSSA from the optimizer. The test updates are essentially accomodating LCSSA phi nodes in the output of LICM, and the fact that we now completely sink every instruction in ashr-crash below the loop bodies prior to unrolling. With this change, LCSSA is computed only three times in the pass pipeline. One of them could be removed (and potentially a SCEV run and a separate LoopPassManager entirely!) if we had a LoopPass variant of InstCombine that ran InstCombine on the loop body but refused to combine away LCSSA PHI nodes. Currently, this also prevents loop unrolling from being in the same loop pass manager is rotate, LICM, and unswitch. There is one thing that I *really* don't like -- preserving LCSSA in LICM is quite expensive. We end up having to re-run LCSSA twice for some loops after LICM runs because LICM can undo LCSSA both in the current loop and the parent loop. I don't really see good solutions to this other than to completely move away from LCSSA and using tools like SSAUpdater instead. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200067 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 5 years ago
6 changed file(s) with 299 addition(s) and 183 deletion(s). Raw diff Collapse all Expand all
3333 bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,
3434 AliasAnalysis *AA = 0, ScalarEvolution *SE = 0);
3535
36 /// \brief Put loop into LCSSA form.
37 ///
38 /// Looks at all instructions in the loop which have uses outside of the
39 /// current loop. For each, an LCSSA PHI node is inserted and the uses outside
40 /// the loop are rewritten to use this node.
41 ///
42 /// LoopInfo and DominatorTree are required and preserved.
43 ///
44 /// If ScalarEvolution is passed in, it will be preserved.
45 ///
46 /// Returns true if any modifications are made to the loop.
47 bool formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE = 0);
48
3649 }
3750
3851 #endif
5353 #include "llvm/Support/raw_ostream.h"
5454 #include "llvm/Target/TargetLibraryInfo.h"
5555 #include "llvm/Transforms/Utils/Local.h"
56 #include "llvm/Transforms/Utils/LoopUtils.h"
5657 #include "llvm/Transforms/Utils/SSAUpdater.h"
5758 #include
5859 using namespace llvm;
8485 AU.addRequired();
8586 AU.addRequired();
8687 AU.addRequiredID(LoopSimplifyID);
88 AU.addPreservedID(LoopSimplifyID);
89 AU.addRequiredID(LCSSAID);
90 AU.addPreservedID(LCSSAID);
8791 AU.addRequired();
8892 AU.addPreserved();
8993 AU.addPreserved();
90 AU.addPreservedID(LoopSimplifyID);
9194 AU.addRequired();
9295 }
9396
192195 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
193196 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
194197 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
198 INITIALIZE_PASS_DEPENDENCY(LCSSA)
199 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
195200 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
196201 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
197202 INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false)
280285 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
281286 I != E; ++I)
282287 PromoteAliasSet(*I, ExitBlocks, InsertPts);
288 }
289
290 // If we have successfully changed the loop, re-form LCSSA and also re-form
291 // LCSSA in the parent loop as hoisting or sinking may have broken it.
292 if (Changed) {
293 formLCSSA(*L, *DT, getAnalysisIfAvailable());
294 if (Loop *ParentL = L->getParentLoop())
295 formLCSSA(*ParentL, *DT, getAnalysisIfAvailable());
283296 }
284297
285298 // Clear out loops state information for the next iteration
450463 return isSafeToExecuteUnconditionally(I);
451464 }
452465
466 /// \brief Returns true if a PHINode is a trivially replaceable with an
467 /// Instruction.
468 ///
469 /// This is true when all incoming values are that instruction. This pattern
470 /// occurs most often with LCSSA PHI nodes.
471 static bool isTriviallyReplacablePHI(PHINode &PN, Instruction &I) {
472 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
473 if (PN.getIncomingValue(i) != &I)
474 return false;
475
476 return true;
477 }
478
453479 /// isNotUsedInLoop - Return true if the only users of this instruction are
454480 /// outside of the loop. If this is true, we can sink the instruction to the
455481 /// exit blocks of the loop.
458484 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) {
459485 Instruction *User = cast(*UI);
460486 if (PHINode *PN = dyn_cast(User)) {
461 // PHI node uses occur in predecessor blocks!
487 // A PHI node where all of the incoming values are this instruction are
488 // special -- they can just be RAUW'ed with the instruction and thus
489 // don't require a use in the predecessor. This is a particular important
490 // special case because it is the pattern found in LCSSA form.
491 if (isTriviallyReplacablePHI(*PN, I)) {
492 if (CurLoop->contains(PN))
493 return false;
494 else
495 continue;
496 }
497
498 // Otherwise, PHI node uses occur in predecessor blocks if the incoming
499 // values. Check for such a use being inside the loop.
462500 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
463501 if (PN->getIncomingValue(i) == &I)
464502 if (CurLoop->contains(PN->getIncomingBlock(i)))
465503 return false;
466 } else if (CurLoop->contains(User)) {
504
505 continue;
506 }
507
508 if (CurLoop->contains(User))
467509 return false;
468 }
469510 }
470511 return true;
471512 }
472513
514 static BasicBlock::iterator
515 replaceTrivialPHIsAndGetInsertionPt(BasicBlock &BB, Instruction &I) {
516 BasicBlock::iterator II = BB.begin();
517 while (PHINode *PN = dyn_cast(II)) {
518 ++II;
519 if (isTriviallyReplacablePHI(*PN, I)) {
520 PN->replaceAllUsesWith(&I);
521 PN->eraseFromParent();
522 }
523 }
524 if (isa(II))
525 ++II;
526
527 return II;
528 }
473529
474530 /// sink - When an instruction is found to only be used outside of the loop,
475531 /// this function moves it to the exit blocks and patches up SSA form as needed.
501557 I.replaceAllUsesWith(UndefValue::get(I.getType()));
502558 I.eraseFromParent();
503559 } else {
560 // Look for any LCSSA PHI nodes for this instruction in the exit blocks
561 // and replace them.
562 BasicBlock::iterator II =
563 replaceTrivialPHIsAndGetInsertionPt(*ExitBlocks[0], I);
564
504565 // Move the instruction to the start of the exit block, after any PHI
505566 // nodes in it.
506 I.moveBefore(ExitBlocks[0]->getFirstInsertionPt());
567 I.moveBefore(II);
507568
508569 // This instruction is no longer in the AST for the current loop, because
509570 // we just sunk it out of the loop. If we just sunk it into an outer
545606 if (!DT->dominates(InstOrigBB, ExitBlock))
546607 continue;
547608
548 // Insert the code after the last PHI node.
549 BasicBlock::iterator InsertPt = ExitBlock->getFirstInsertionPt();
609 // Look for any LCSSA PHI nodes for this instruction in the exit blocks
610 // and replace them. Then get the insertion point after the last PHI.
611 BasicBlock::iterator InsertPt =
612 replaceTrivialPHIsAndGetInsertionPt(*ExitBlock, I);
550613
551614 // If this is the first exit block processed, just move the original
552615 // instruction, otherwise clone the original instruction and insert
3838 #include "llvm/IR/Instructions.h"
3939 #include "llvm/Pass.h"
4040 #include "llvm/Support/PredIteratorCache.h"
41 #include "llvm/Transforms/Utils/LoopUtils.h"
4142 #include "llvm/Transforms/Utils/SSAUpdater.h"
4243 using namespace llvm;
4344
4445 STATISTIC(NumLCSSA, "Number of live out of a loop variables");
4546
46 namespace {
47 struct LCSSA : public LoopPass {
48 static char ID; // Pass identification, replacement for typeid
49 LCSSA() : LoopPass(ID) {
50 initializeLCSSAPass(*PassRegistry::getPassRegistry());
51 }
52
53 // Cached analysis information for the current function.
54 DominatorTree *DT;
55 LoopInfo *LI;
56 ScalarEvolution *SE;
57 PredIteratorCache PredCache;
58 Loop *L;
59
60 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
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
69 AU.addRequired();
70 AU.addRequired();
71 AU.addPreservedID(LoopSimplifyID);
72 AU.addPreserved();
73 }
74 private:
75 bool ProcessInstruction(Instruction *Inst,
76 const SmallVectorImpl &ExitBlocks);
77
78 /// verifyAnalysis() - Verify loop nest.
79 virtual void verifyAnalysis() const {
80 // Check the special guarantees that LCSSA makes.
81 assert(L->isLCSSAForm(*DT) && "LCSSA form not preserved!");
82 }
83 };
84 }
85
86 char LCSSA::ID = 0;
87 INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
88 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
89 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
90 INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
91
92 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
93 char &llvm::LCSSAID = LCSSA::ID;
94
95
96 /// BlockDominatesAnExit - Return true if the specified block dominates at least
97 /// one of the blocks in the specified list.
98 static bool BlockDominatesAnExit(BasicBlock *BB,
99 const SmallVectorImpl &ExitBlocks,
100 DominatorTree *DT) {
101 DomTreeNode *DomNode = DT->getNode(BB);
102 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
103 if (DT->dominates(DomNode, DT->getNode(ExitBlocks[i])))
104 return true;
105
106 return false;
107 }
108
109
110 /// runOnFunction - Process all loops in the function, inner-most out.
111 bool LCSSA::runOnLoop(Loop *TheLoop, LPPassManager &LPM) {
112 L = TheLoop;
113
114 DT = &getAnalysis().getDomTree();
115 LI = &getAnalysis();
116 SE = getAnalysisIfAvailable();
117
118 // Get the set of exiting blocks.
119 SmallVector ExitBlocks;
120 L->getExitBlocks(ExitBlocks);
121
122 if (ExitBlocks.empty())
123 return false;
124
125 // Look at all the instructions in the loop, checking to see if they have uses
126 // outside the loop. If so, rewrite those uses.
127 bool MadeChange = false;
128
129 for (Loop::block_iterator BBI = L->block_begin(), E = L->block_end();
130 BBI != E; ++BBI) {
131 BasicBlock *BB = *BBI;
132
133 // For large loops, avoid use-scanning by using dominance information: In
134 // particular, if a block does not dominate any of the loop exits, then none
135 // of the values defined in the block could be used outside the loop.
136 if (!BlockDominatesAnExit(BB, ExitBlocks, DT))
137 continue;
138
139 for (BasicBlock::iterator I = BB->begin(), E = BB->end();
140 I != E; ++I) {
141 // Reject two common cases fast: instructions with no uses (like stores)
142 // and instructions with one use that is in the same block as this.
143 if (I->use_empty() ||
144 (I->hasOneUse() && I->use_back()->getParent() == BB &&
145 !isa(I->use_back())))
146 continue;
147
148 MadeChange |= ProcessInstruction(I, ExitBlocks);
149 }
150 }
151
152 // If we modified the code, remove any caches about the loop from SCEV to
153 // avoid dangling entries.
154 // FIXME: This is a big hammer, can we clear the cache more selectively?
155 if (SE && MadeChange)
156 SE->forgetLoop(L);
157
158 assert(L->isLCSSAForm(*DT));
159 PredCache.clear();
160
161 return MadeChange;
162 }
163
164 /// isExitBlock - Return true if the specified block is in the list.
47 /// Return true if the specified block is in the list.
16548 static bool isExitBlock(BasicBlock *BB,
166 const SmallVectorImpl*> &ExitBlocks) {
49 const SmallVectorImpl *> &ExitBlocks) {
16750 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
16851 if (ExitBlocks[i] == BB)
16952 return true;
17053 return false;
17154 }
17255
173 /// ProcessInstruction - Given an instruction in the loop, check to see if it
174 /// has any uses that are outside the current loop. If so, insert LCSSA PHI
175 /// nodes and rewrite the uses.
176 bool LCSSA::ProcessInstruction(Instruction *Inst,
177 const SmallVectorImpl &ExitBlocks) {
178 SmallVector UsesToRewrite;
179
180 BasicBlock *InstBB = Inst->getParent();
181
182 for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
183 UI != E; ++UI) {
56 /// Given an instruction in the loop, check to see if it has any uses that are
57 /// outside the current loop. If so, insert LCSSA PHI nodes and rewrite the
58 /// uses.
59 static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
60 const SmallVectorImpl &ExitBlocks,
61 PredIteratorCache &PredCache) {
62 SmallVector UsesToRewrite;
63
64 BasicBlock *InstBB = Inst.getParent();
65
66 for (Value::use_iterator UI = Inst.use_begin(), E = Inst.use_end(); UI != E;
67 ++UI) {
18468 User *U = *UI;
18569 BasicBlock *UserBB = cast(U)->getParent();
18670 if (PHINode *PN = dyn_cast(U))
18771 UserBB = PN->getIncomingBlock(UI);
188
189 if (InstBB != UserBB && !L->contains(UserBB))
72
73 if (InstBB != UserBB && !L.contains(UserBB))
19074 UsesToRewrite.push_back(&UI.getUse());
19175 }
19276
19377 // If there are no uses outside the loop, exit with no change.
194 if (UsesToRewrite.empty()) return false;
195
78 if (UsesToRewrite.empty())
79 return false;
80
19681 ++NumLCSSA; // We are applying the transformation
19782
19883 // Invoke instructions are special in that their result value is not available
199 // along their unwind edge. The code below tests to see whether DomBB dominates
84 // along their unwind edge. The code below tests to see whether DomBB
85 // dominates
20086 // the value, so adjust DomBB to the normal destination block, which is
20187 // effectively where the value is first usable.
202 BasicBlock *DomBB = Inst->getParent();
203 if (InvokeInst *Inv = dyn_cast(Inst))
88 BasicBlock *DomBB = Inst.getParent();
89 if (InvokeInst *Inv = dyn_cast(&Inst))
20490 DomBB = Inv->getNormalDest();
20591
206 DomTreeNode *DomNode = DT->getNode(DomBB);
207
208 SmallVector AddedPHIs;
92 DomTreeNode *DomNode = DT.getNode(DomBB);
93
94 SmallVector AddedPHIs;
20995
21096 SSAUpdater SSAUpdate;
211 SSAUpdate.Initialize(Inst->getType(), Inst->getName());
212
97 SSAUpdate.Initialize(Inst.getType(), Inst.getName());
98
21399 // Insert the LCSSA phi's into all of the exit blocks dominated by the
214100 // value, and add them to the Phi's map.
215 for (SmallVectorImpl::const_iterator BBI = ExitBlocks.begin(),
216 BBE = ExitBlocks.end(); BBI != BBE; ++BBI) {
101 for (SmallVectorImpl::const_iterator BBI = ExitBlocks.begin(),
102 BBE = ExitBlocks.end();
103 BBI != BBE; ++BBI) {
217104 BasicBlock *ExitBB = *BBI;
218 if (!DT->dominates(DomNode, DT->getNode(ExitBB))) continue;
219
105 if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
106 continue;
107
220108 // If we already inserted something for this BB, don't reprocess it.
221 if (SSAUpdate.HasValueForBlock(ExitBB)) continue;
222
223 PHINode *PN = PHINode::Create(Inst->getType(),
224 PredCache.GetNumPreds(ExitBB),
225 Inst->getName()+".lcssa",
226 ExitBB->begin());
109 if (SSAUpdate.HasValueForBlock(ExitBB))
110 continue;
111
112 PHINode *PN = PHINode::Create(Inst.getType(), PredCache.GetNumPreds(ExitBB),
113 Inst.getName() + ".lcssa", ExitBB->begin());
227114
228115 // Add inputs from inside the loop for this PHI.
229116 for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
230 PN->addIncoming(Inst, *PI);
117 PN->addIncoming(&Inst, *PI);
231118
232119 // If the exit block has a predecessor not within the loop, arrange for
233120 // the incoming value use corresponding to that predecessor to be
234121 // rewritten in terms of a different LCSSA PHI.
235 if (!L->contains(*PI))
122 if (!L.contains(*PI))
236123 UsesToRewrite.push_back(
237 &PN->getOperandUse(
238 PN->getOperandNumForIncomingValue(PN->getNumIncomingValues()-1)));
124 &PN->getOperandUse(PN->getOperandNumForIncomingValue(
125 PN->getNumIncomingValues() - 1)));
239126 }
240127
241128 AddedPHIs.push_back(PN);
242
129
243130 // Remember that this phi makes the value alive in this block.
244131 SSAUpdate.AddAvailableValue(ExitBB, PN);
245132 }
256143 if (PHINode *PN = dyn_cast(User))
257144 UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
258145
259 if (isa(UserBB->begin()) &&
260 isExitBlock(UserBB, ExitBlocks)) {
146 if (isa(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
261147 // Tell the VHs that the uses changed. This updates SCEV's caches.
262148 if (UsesToRewrite[i]->get()->hasValueHandle())
263149 ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
264150 UsesToRewrite[i]->set(UserBB->begin());
265151 continue;
266152 }
267
153
268154 // Otherwise, do full PHI insertion.
269155 SSAUpdate.RewriteUse(*UsesToRewrite[i]);
270156 }
274160 if (AddedPHIs[i]->use_empty())
275161 AddedPHIs[i]->eraseFromParent();
276162 }
277
163
278164 return true;
279165 }
280166
167 /// Return true if the specified block dominates at least
168 /// one of the blocks in the specified list.
169 static bool
170 blockDominatesAnExit(BasicBlock *BB,
171 DominatorTree &DT,
172 const SmallVectorImpl &ExitBlocks) {
173 DomTreeNode *DomNode = DT.getNode(BB);
174 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
175 if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i])))
176 return true;
177
178 return false;
179 }
180
181 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE) {
182 bool Changed = false;
183
184 // Get the set of exiting blocks.
185 SmallVector ExitBlocks;
186 L.getExitBlocks(ExitBlocks);
187
188 if (ExitBlocks.empty())
189 return false;
190
191 PredIteratorCache PredCache;
192
193 // Look at all the instructions in the loop, checking to see if they have uses
194 // outside the loop. If so, rewrite those uses.
195 for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end();
196 BBI != BBE; ++BBI) {
197 BasicBlock *BB = *BBI;
198
199 // For large loops, avoid use-scanning by using dominance information: In
200 // particular, if a block does not dominate any of the loop exits, then none
201 // of the values defined in the block could be used outside the loop.
202 if (!blockDominatesAnExit(BB, DT, ExitBlocks))
203 continue;
204
205 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
206 // Reject two common cases fast: instructions with no uses (like stores)
207 // and instructions with one use that is in the same block as this.
208 if (I->use_empty() ||
209 (I->hasOneUse() && I->use_back()->getParent() == BB &&
210 !isa(I->use_back())))
211 continue;
212
213 Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache);
214 }
215 }
216
217 // If we modified the code, remove any caches about the loop from SCEV to
218 // avoid dangling entries.
219 // FIXME: This is a big hammer, can we clear the cache more selectively?
220 if (SE && Changed)
221 SE->forgetLoop(&L);
222
223 assert(L.isLCSSAForm(DT));
224
225 return Changed;
226 }
227
228 namespace {
229 struct LCSSA : public FunctionPass {
230 static char ID; // Pass identification, replacement for typeid
231 LCSSA() : FunctionPass(ID) {
232 initializeLCSSAPass(*PassRegistry::getPassRegistry());
233 }
234
235 // Cached analysis information for the current function.
236 DominatorTree *DT;
237 LoopInfo *LI;
238 ScalarEvolution *SE;
239
240 virtual bool runOnFunction(Function &F);
241
242 /// This transformation requires natural loop information & requires that
243 /// loop preheaders be inserted into the CFG. It maintains both of these,
244 /// as well as the CFG. It also requires dominator information.
245 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
246 AU.setPreservesCFG();
247
248 AU.addRequired();
249 AU.addRequired();
250 AU.addPreservedID(LoopSimplifyID);
251 AU.addPreserved();
252 }
253
254 private:
255 bool processLoop(Loop &L);
256
257 virtual void verifyAnalysis() const;
258 };
259 }
260
261 char LCSSA::ID = 0;
262 INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
263 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
264 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
265 INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
266
267 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
268 char &llvm::LCSSAID = LCSSA::ID;
269
270
271 /// Process all loops in the function, inner-most out.
272 bool LCSSA::runOnFunction(Function &F) {
273 bool Changed = false;
274 LI = &getAnalysis();
275 DT = &getAnalysis().getDomTree();
276 SE = getAnalysisIfAvailable();
277
278 // Simplify each loop nest in the function.
279 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
280 Changed |= processLoop(**I);
281
282 return Changed;
283 }
284
285 /// Process a loop nest depth first.
286 bool LCSSA::processLoop(Loop &L) {
287 bool Changed = false;
288
289 // Recurse depth-first through inner loops.
290 for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
291 Changed |= processLoop(**LI);
292
293 Changed |= formLCSSA(L, *DT, SE);
294 return Changed;
295 }
296
297 static void verifyLoop(Loop &L, DominatorTree &DT) {
298 // Recurse depth-first through inner loops.
299 for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
300 verifyLoop(**LI, DT);
301
302 // Check the special guarantees that LCSSA makes.
303 //assert(L.isLCSSAForm(DT) && "LCSSA form not preserved!");
304 }
305
306 void LCSSA::verifyAnalysis() const {
307 // Verify each loop nest in the function, assuming LI still points at that
308 // function's loop info.
309 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
310 verifyLoop(**I, *DT);
311 }
2323 Out:
2424 ret void
2525 ; CHECK: Out:
26 ; CHECK-NEXT: store i32 %x2, i32* @X
26 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %x2
27 ; CHECK-NEXT: store i32 %[[LCSSAPHI]], i32* @X
2728 ; CHECK-NEXT: ret void
2829
2930 }
4748 Exit: ; preds = %Loop
4849 ret void
4950 ; CHECK: Exit:
50 ; CHECK-NEXT: store i32 %V, i32* getelementptr inbounds (i32* @X, i64 1)
51 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %V
52 ; CHECK-NEXT: store i32 %[[LCSSAPHI]], i32* getelementptr inbounds (i32* @X, i64 1)
5153 ; CHECK-NEXT: ret void
5254 }
5355
141143 Out:
142144 ret void
143145 ; CHECK: Out:
144 ; CHECK-NEXT: store i32 %x2, i32* @X
146 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %x2
147 ; CHECK-NEXT: store i32 %[[LCSSAPHI]], i32* @X
145148 ; CHECK-NEXT: ret void
146149
147150 }
177180 ; CHECK: for.body.lr.ph:
178181 ; CHECK-NEXT: %gi.promoted = load i32* %gi, align 4, !tbaa !0
179182 ; CHECK: for.cond.for.end_crit_edge:
180 ; CHECK-NEXT: store i32 %inc, i32* %gi, align 4, !tbaa !0
183 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %inc
184 ; CHECK-NEXT: store i32 %[[LCSSAPHI]], i32* %gi, align 4, !tbaa !0
181185 }
182186
183187 !0 = metadata !{metadata !4, metadata !4, i64 0}
7575 ret i32 %tmp.7
7676 ; CHECK-LABEL: @test4(
7777 ; CHECK: Out:
78 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
78 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %N_addr.0.pn
79 ; CHECK-NEXT: mul i32 %N, %[[LCSSAPHI]]
7980 ; CHECK-NEXT: sub i32 %tmp.6, %N
8081 ; CHECK-NEXT: ret i32
8182 }
151152 ret i32 %tmp.7
152153 ; CHECK-LABEL: @test7(
153154 ; CHECK: Out1:
154 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
155 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %N_addr.0.pn
156 ; CHECK-NEXT: mul i32 %N, %[[LCSSAPHI]]
155157 ; CHECK-NEXT: sub i32 %tmp.6, %N
156158 ; CHECK-NEXT: ret
157159 ; CHECK: Out2:
158 ; CHECK-NEXT: mul i32 %N, %N_addr.0.pn
159 ; CHECK-NEXT: sub i32 %tmp.6
160 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %N_addr.0.pn
161 ; CHECK-NEXT: mul i32 %N, %[[LCSSAPHI]]
162 ; CHECK-NEXT: sub i32 %tmp.6.le, %N
160163 ; CHECK-NEXT: ret
161164 }
162165
182185 ; CHECK: exit1:
183186 ; CHECK-NEXT: ret i32 0
184187 ; CHECK: exit2:
185 ; CHECK-NEXT: %V = add i32 %X, 1
188 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %X
189 ; CHECK-NEXT: %V = add i32 %[[LCSSAPHI]], 1
186190 ; CHECK-NEXT: ret i32 %V
187191 }
188192
228232
229233 ; CHECK-LABEL: @test10(
230234 ; CHECK: Out:
231 ; CHECK-NEXT: %tmp.6 = sdiv i32 %N, %N_addr.0.pn
235 ; CHECK-NEXT: %[[LCSSAPHI:.*]] = phi i32 [ %N_addr.0.pn
236 ; CHECK-NEXT: %tmp.6 = sdiv i32 %N, %[[LCSSAPHI]]
232237 ; CHECK-NEXT: ret i32 %tmp.6
233238 }
234239
2222 @b = common global i32 0, align 4
2323
2424 ; Check that the def-use chain that leads to the bad SCEV is still
25 ; there, and part of it is hoisted to the entry block.
25 ; there.
2626 ;
2727 ; CHECK-LABEL: @foo
2828 ; CHECK-LABEL: entry:
29 ; CHECK: %cmp4
3029 ; CHECK-LABEL: for.cond1.preheader:
3130 ; CHECK-LABEL: for.body3:
32 ; CHECK: %1 = zext i1 %cmp4 to i32
33 ; CHECK: %xor = xor i32 %1, 1
31 ; CHECK: %cmp4
32 ; CHECK: %conv = zext i1 %cmp4 to i32
33 ; CHECK: %xor = xor i32 %conv6, 1
3434 define void @foo() {
3535 entry:
3636 br label %for.cond