llvm.org GIT mirror llvm / c4f3d51
Make LoopDeletion work on loops with multiple edges, as long as the incoming values from all of the loop's exiting blocks are equal. Patch by Andrew Clinton. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126253 91177308-0d34-0410-b5e6-96231b3b80d8 Cameron Zwarich 8 years ago
2 changed file(s) with 41 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
7777 SmallVector& exitingBlocks,
7878 SmallVector& exitBlocks,
7979 bool &Changed, BasicBlock *Preheader) {
80 BasicBlock* exitingBlock = exitingBlocks[0];
8180 BasicBlock* exitBlock = exitBlocks[0];
8281
8382 // Make sure that all PHI entries coming from the loop are loop invariant.
8786 // of the loop.
8887 BasicBlock::iterator BI = exitBlock->begin();
8988 while (PHINode* P = dyn_cast(BI)) {
90 Value* incoming = P->getIncomingValueForBlock(exitingBlock);
89 Value* incoming = P->getIncomingValueForBlock(exitingBlocks[0]);
90
91 // Make sure all exiting blocks produce the same incoming value for the exit
92 // block. If there are different incoming values for different exiting
93 // blocks, then it is impossible to statically determine which value should
94 // be used.
95 for (unsigned i = 1; i < exitingBlocks.size(); ++i) {
96 if (incoming != P->getIncomingValueForBlock(exitingBlocks[i]))
97 return false;
98 }
99
91100 if (Instruction* I = dyn_cast(incoming))
92101 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator()))
93102 return false;
94
103
95104 ++BI;
96105 }
97106
146155 if (exitBlocks.size() != 1)
147156 return false;
148157
149 // Loops with multiple exits are too complicated to handle correctly.
150 if (exitingBlocks.size() != 1)
151 return false;
152
153158 // Finally, we have to check that the loop really is dead.
154159 bool Changed = false;
155160 if (!IsLoopDead(L, exitingBlocks, exitBlocks, Changed, preheader))
165170 // Now that we know the removal is safe, remove the loop by changing the
166171 // branch from the preheader to go to the single exit block.
167172 BasicBlock* exitBlock = exitBlocks[0];
168 BasicBlock* exitingBlock = exitingBlocks[0];
169173
170174 // Because we're deleting a large chunk of code at once, the sequence in which
171175 // we remove things is very important to avoid invalidation issues. Don't
182186
183187 // Rewrite phis in the exit block to get their inputs from
184188 // the preheader instead of the exiting block.
189 BasicBlock* exitingBlock = exitingBlocks[0];
185190 BasicBlock::iterator BI = exitBlock->begin();
186191 while (PHINode* P = dyn_cast(BI)) {
187192 P->replaceUsesOfWith(exitingBlock, preheader);
193 for (unsigned i = 1; i < exitingBlocks.size(); ++i)
194 P->removeIncomingValue(exitingBlocks[i]);
188195 ++BI;
189196 }
190197
0 ; RUN: opt < %s -loop-deletion -S | FileCheck %s
1
2 ; Checks whether dead loops with multiple exits can be eliminated
3
4 ; CHECK: entry:
5 ; CHECK-NEXT: br label %return
6
7 ; CHECK: return:
8 ; CHECK-NEXT: ret void
9
10 define void @foo(i64 %n, i64 %m) nounwind {
11 entry:
12 br label %bb
13
14 bb:
15 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb2 ]
16 %t0 = add i64 %x.0, 1
17 %t1 = icmp slt i64 %x.0, %n
18 br i1 %t1, label %bb2, label %return
19 bb2:
20 %t2 = icmp slt i64 %x.0, %m
21 br i1 %t1, label %bb, label %return
22
23 return:
24 ret void
25 }