llvm.org GIT mirror llvm / 469af3f
[Loop Deletion] Delete loops that are never executed Summary: Currently, loop deletion deletes loop where the only values that are used outside the loop are loop-invariant. This patch adds logic to delete loops where the loop is proven to be never executed (i.e. the only predecessor of the loop preheader has a constant conditional branch as terminator, and the preheader is not the taken target). This will remove loops that become dead after loop-unswitching generates constant conditional branches. The next steps are: 1. moving the loop deletion implementation to LoopUtils. 2. Add logic in loop-simplifyCFG which will support changing conditional constant branches to unconditional branches. If loops become unreachable in this process, they can be removed using `deleteDeadLoop` function. Reviewers: chandlerc, efriedma, sanjoy, reames Reviewed by: sanjoy Subscribers: mzolotukhin, llvm-commits Differential Revision: https://reviews.llvm.org/D32494 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302015 91177308-0d34-0410-b5e6-96231b3b80d8 Anna Thomas 2 years ago
2 changed file(s) with 427 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
1919 #include "llvm/Analysis/GlobalsModRef.h"
2020 #include "llvm/Analysis/LoopPass.h"
2121 #include "llvm/IR/Dominators.h"
22 #include "llvm/IR/PatternMatch.h"
2223 #include "llvm/Transforms/Scalar.h"
2324 #include "llvm/Transforms/Scalar/LoopPassManager.h"
2425 #include "llvm/Transforms/Utils/LoopUtils.h"
2829
2930 STATISTIC(NumDeleted, "Number of loops deleted");
3031
32 /// This function deletes dead loops. The caller of this function needs to
33 /// guarantee that the loop is infact dead. Here we handle two kinds of dead
34 /// loop. The first kind (\p isLoopDead) is where only invariant values from
35 /// within the loop are used outside of it. The second kind (\p
36 /// isLoopNeverExecuted) is where the loop is provably never executed. We can
37 /// always remove never executed loops since they will not cause any
38 /// difference to program behaviour.
39 ///
40 /// This also updates the relevant analysis information in \p DT, \p SE, and \p
41 /// LI. It also updates the loop PM if an updater struct is provided.
42 // TODO: This function will be used by loop-simplifyCFG as well. So, move this
43 // to LoopUtils.cpp
44 static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
45 LoopInfo &LI, bool LoopIsNeverExecuted,
46 LPMUpdater *Updater = nullptr);
3147 /// Determines if a loop is dead.
3248 ///
3349 /// This assumes that we've already checked for unique exit and exiting blocks,
8399 return true;
84100 }
85101
102 /// This function returns true if there is no viable path from the
103 /// entry block to the header of \p L. Right now, it only does
104 /// a local search to save compile time.
105 static bool isLoopNeverExecuted(Loop *L) {
106 using namespace PatternMatch;
107
108 auto *Preheader = L->getLoopPreheader();
109 // TODO: We can relax this constraint, since we just need a loop
110 // predecessor.
111 assert(Preheader && "Needs preheader!");
112
113 if (Preheader == &Preheader->getParent()->getEntryBlock())
114 return false;
115 // All predecessors of the preheader should have a constant conditional
116 // branch, with the loop's preheader as not-taken.
117 for (auto *Pred: predecessors(Preheader)) {
118 BasicBlock *Taken, *NotTaken;
119 ConstantInt *Cond;
120 if (!match(Pred->getTerminator(),
121 m_Br(m_ConstantInt(Cond), Taken, NotTaken)))
122 return false;
123 if (!Cond->getZExtValue())
124 std::swap(Taken, NotTaken);
125 if (Taken == Preheader)
126 return false;
127 }
128 assert(!pred_empty(Preheader) &&
129 "Preheader should have predecessors at this point!");
130 // All the predecessors have the loop preheader as not-taken target.
131 return true;
132 }
133
86134 /// Remove a loop if it is dead.
87135 ///
88136 /// A loop is considered dead if it does not impact the observable behavior of
89137 /// the program other than finite running time. This never removes a loop that
90 /// might be infinite, as doing so could change the halting/non-halting nature
91 /// of a program.
138 /// might be infinite (unless it is never executed), as doing so could change
139 /// the halting/non-halting nature of a program.
92140 ///
93141 /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in
94142 /// order to make various safety checks work.
96144 /// \returns true if any changes were made. This may mutate the loop even if it
97145 /// is unable to delete it due to hoisting trivially loop invariant
98146 /// instructions out of the loop.
99 ///
100 /// This also updates the relevant analysis information in \p DT, \p SE, and \p
101 /// LI. It also updates the loop PM if an updater struct is provided.
102147 static bool deleteLoopIfDead(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
103148 LoopInfo &LI, LPMUpdater *Updater = nullptr) {
104149 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
118163 if (L->begin() != L->end())
119164 return false;
120165
166
167 BasicBlock *ExitBlock = L->getUniqueExitBlock();
168
169 if (ExitBlock && isLoopNeverExecuted(L)) {
170 deleteDeadLoop(L, DT, SE, LI, true /* LoopIsNeverExecuted */, Updater);
171 ++NumDeleted;
172 return true;
173 }
174
175 // The remaining checks below are for a loop being dead because all statements
176 // in the loop are invariant.
121177 SmallVector ExitingBlocks;
122178 L->getExitingBlocks(ExitingBlocks);
123179
125181 // be in the situation of needing to be able to solve statically which exit
126182 // block will be branched to, or trying to preserve the branching logic in
127183 // a loop invariant manner.
128 BasicBlock *ExitBlock = L->getUniqueExitBlock();
129184 if (!ExitBlock)
130185 return false;
131186
140195 if (isa(S))
141196 return Changed;
142197
198 deleteDeadLoop(L, DT, SE, LI, false /* LoopIsNeverExecuted */, Updater);
199 ++NumDeleted;
200
201 return true;
202 }
203
204 static void deleteDeadLoop(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
205 LoopInfo &LI, bool LoopIsNeverExecuted,
206 LPMUpdater *Updater) {
207 assert(L->isLCSSAForm(DT) && "Expected LCSSA!");
208 auto *Preheader = L->getLoopPreheader();
209 assert(Preheader && "Preheader should exist!");
210
143211 // Now that we know the removal is safe, remove the loop by changing the
144212 // branch from the preheader to go to the single exit block.
145213 //
155223 // to determine what it needs to clean up.
156224 SE.forgetLoop(L);
157225
226 auto *ExitBlock = L->getUniqueExitBlock();
227 assert(ExitBlock && "Should have a unique exit block!");
228
158229 // Connect the preheader directly to the exit block.
159 TerminatorInst *TI = Preheader->getTerminator();
160 TI->replaceUsesOfWith(L->getHeader(), ExitBlock);
161
162 // Rewrite phis in the exit block to get their inputs from
163 // the preheader instead of the exiting block.
230 // Even when the loop is never executed, we cannot remove the edge from the
231 // source block to the exit block. Consider the case where the unexecuted loop
232 // branches back to an outer loop. If we deleted the loop and removed the edge
233 // coming to this inner loop, this will break the outer loop structure (by
234 // deleting the backedge of the outer loop). If the outer loop is indeed a
235 // non-loop, it will be deleted in a future iteration of loop deletion pass.
236 Preheader->getTerminator()->replaceUsesOfWith(L->getHeader(), ExitBlock);
237
238 SmallVector ExitingBlocks;
239 L->getExitingBlocks(ExitingBlocks);
240 // Rewrite phis in the exit block to get their inputs from the Preheader
241 // instead of the exiting block.
164242 BasicBlock *ExitingBlock = ExitingBlocks[0];
165243 BasicBlock::iterator BI = ExitBlock->begin();
166244 while (PHINode *P = dyn_cast(BI)) {
167245 int j = P->getBasicBlockIndex(ExitingBlock);
168246 assert(j >= 0 && "Can't find exiting block in exit block's phi node!");
247 if (LoopIsNeverExecuted)
248 P->setIncomingValue(j, UndefValue::get(P->getType()));
169249 P->setIncomingBlock(j, Preheader);
170250 for (unsigned i = 1; i < ExitingBlocks.size(); ++i)
171251 P->removeIncomingValue(ExitingBlocks[i]);
210290
211291 // The last step is to update LoopInfo now that we've eliminated this loop.
212292 LI.markAsRemoved(L);
213 ++NumDeleted;
214
215 return true;
216293 }
217294
218295 PreservedAnalyses LoopDeletionPass::run(Loop &L, LoopAnalysisManager &AM,
253330 bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &) {
254331 if (skipLoop(L))
255332 return false;
256
257333 DominatorTree &DT = getAnalysis().getDomTree();
258334 ScalarEvolution &SE = getAnalysis().getSE();
259335 LoopInfo &LI = getAnalysis().getLoopInfo();
0 ; RUN: opt < %s -loop-deletion -verify-dom-info -S | FileCheck %s
1
2 ; Checking that we can delete loops that are never executed.
3 ; We do not change the constant conditional branch statement (where the not-taken target
4 ; is the loop) to an unconditional one.
5
6 ; delete the infinite loop because it is never executed.
7 define void @test1(i64 %n, i64 %m) nounwind {
8 ; CHECK-LABEL: test1
9 ; CHECK-LABEL: entry:
10 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
11 ; CHECK-NOT: bb:
12 entry:
13 br i1 true, label %return, label %bb
14
15 bb:
16 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb ]
17 %t0 = add i64 %x.0, 1
18 %t1 = icmp slt i64 %x.0, %n
19 %t3 = icmp sgt i64 %x.0, %m
20 %t4 = and i1 %t1, %t3
21 br i1 true, label %bb, label %return
22
23 return:
24 ret void
25 }
26
27 ; FIXME: We can delete this infinite loop. Currently we do not,
28 ; because the infinite loop has no exit block.
29 define void @test2(i64 %n, i64 %m) nounwind {
30 ; CHECK-LABEL: test2
31 ; CHECK-LABEL: entry:
32 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
33 ; CHECK-LABEL: bb:
34 ; CHECK: br label %bb
35 entry:
36 br i1 true, label %return, label %bb
37
38 bb:
39 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb ]
40 %t0 = add i64 %x.0, 1
41 %t1 = icmp slt i64 %x.0, %n
42 %t3 = icmp sgt i64 %x.0, %m
43 %t4 = and i1 %t1, %t3
44 br label %bb
45
46 return:
47 ret void
48 }
49
50 ; There are multiple exiting blocks and a single exit block.
51 ; Since it is a never executed loop, we do not care about the values
52 ; from different exiting paths and we can
53 ; delete the loop.
54 define i64 @test3(i64 %n, i64 %m, i64 %maybe_zero) nounwind {
55
56 ; CHECK-NOT: bb:
57 ; CHECK-NOT: bb2:
58 ; CHECK-NOT: bb3:
59 ; CHECK-LABEL: return.loopexit:
60 ; CHECK-NEXT: %x.lcssa.ph = phi i64 [ undef, %bb.preheader ]
61 ; CHECK-NEXT: br label %return
62 ; CHECK-LABEL: return:
63 ; CHECK-NEXT: %x.lcssa = phi i64 [ 20, %entry ], [ %x.lcssa.ph, %return.loopexit ]
64 ; CHECK-NEXT: ret i64 %x.lcssa
65 entry:
66 br i1 false, label %bb, label %return
67
68 bb:
69 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb3 ]
70 %t0 = add i64 %x.0, 1
71 %t1 = icmp slt i64 %x.0, %n
72 br i1 %t1, label %bb2, label %return
73
74 bb2:
75 %t2 = icmp slt i64 %x.0, %m
76 %unused1 = udiv i64 42, %maybe_zero
77 br i1 %t2, label %bb3, label %return
78
79 bb3:
80 %t3 = icmp slt i64 %x.0, %m
81 %unused2 = sdiv i64 42, %maybe_zero
82 br i1 %t3, label %bb, label %return
83
84 return:
85 ; the only valid value fo x.lcssa is 20.
86 %x.lcssa = phi i64 [ 12, %bb ], [ 14, %bb2 ], [ 16, %bb3 ], [20, %entry ]
87 ret i64 %x.lcssa
88 }
89
90 ; Cannot delete the loop, since it may be executed at runtime.
91 define void @test4(i64 %n, i64 %m, i1 %cond) {
92 ; CHECK-LABEL: test4
93 ; CHECK-LABEL: bb:
94 entry:
95 br i1 %cond, label %looppred1, label %looppred2
96
97 looppred1:
98 br i1 true, label %return, label %bb
99
100 looppred2:
101 br i1 false, label %return, label %bb
102
103 bb:
104 %x.0 = phi i64 [ 0, %looppred1 ], [ 1, %looppred2 ], [ %t0, %bb ]
105 %t0 = add i64 %x.0, 1
106 %t1 = icmp slt i64 %x.0, %n
107 %t3 = icmp sgt i64 %x.0, %m
108 %t4 = and i1 %t1, %t3
109 br i1 true, label %bb, label %return
110
111 return:
112 ret void
113 }
114
115 ; multiple constant conditional branches with loop not-taken in all cases.
116 define void @test5(i64 %n, i64 %m, i1 %cond) nounwind {
117 ; CHECK-LABEL: test5
118 ; CHECK-LABEL: looppred1:
119 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
120 ; CHECK-LABEL: looppred2:
121 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
122 ; CHECK-NOT: bb:
123 entry:
124 br i1 %cond, label %looppred1, label %looppred2
125
126 looppred1:
127 br i1 true, label %return, label %bb
128
129 looppred2:
130 br i1 true, label %return, label %bb
131
132 bb:
133 %x.0 = phi i64 [ 0, %looppred1 ], [ 1, %looppred2 ], [ %t0, %bb ]
134 %t0 = add i64 %x.0, 1
135 %t1 = icmp slt i64 %x.0, %n
136 %t3 = icmp sgt i64 %x.0, %m
137 %t4 = and i1 %t1, %t3
138 br i1 true, label %bb, label %return
139
140 return:
141 ret void
142 }
143
144 ; Don't delete this infinite loop because the loop
145 ; is executable at runtime.
146 define void @test6(i64 %n, i64 %m) nounwind {
147 ; CHECK-LABEL: test6
148 ; CHECK-LABEL: entry:
149 ; CHECK-NEXT: br i1 true, label %bb.preheader, label %bb.preheader
150 ; CHECK: bb:
151 entry:
152 br i1 true, label %bb, label %bb
153
154 bb:
155 %x.0 = phi i64 [ 0, %entry ], [ 0, %entry ], [ %t0, %bb ]
156 %t0 = add i64 %x.0, 1
157 %t1 = icmp slt i64 %x.0, %n
158 %t3 = icmp sgt i64 %x.0, %m
159 %t4 = and i1 %t1, %t3
160 br i1 true, label %bb, label %return
161
162 return:
163 ret void
164 }
165
166 declare i64 @foo(i64)
167 ; The loop L2 is never executed and is a subloop, with an
168 ; exit block that branches back to parent loop.
169 ; Here we can delete loop L2, while L1 still exists.
170 define i64 @test7(i64 %n) {
171 ; CHECK-LABEL: test7
172 ; CHECK-LABEL: L1:
173 ; CHECK: br i1 true, label %L1Latch, label %L2.preheader
174 ; CHECK-LABEL: L2.preheader:
175 ; CHECK-NEXT: br label %L1Latch.loopexit
176 ; CHECK-LABEL: L1Latch.loopexit:
177 ; CHECK: br label %L1Latch
178 ; CHECK-LABEL: L1Latch:
179 ; CHECK-NEXT: %y = phi i64 [ %y.next, %L1 ], [ %y.L2.lcssa, %L1Latch.loopexit ]
180 ; CHECK: br i1 %cond2, label %exit, label %L1
181 entry:
182 br label %L1
183
184 L1:
185 %y.next = phi i64 [ 0, %entry ], [ %y.add, %L1Latch ]
186 br i1 true, label %L1Latch, label %L2
187
188 L2:
189 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
190 %x.next = add i64 %x, 1
191 %y.L2 = call i64 @foo(i64 %x.next)
192 %cond = icmp slt i64 %x.next, %n
193 br i1 %cond, label %L2, label %L1Latch
194
195 L1Latch:
196 %y = phi i64 [ %y.next, %L1 ], [ %y.L2, %L2 ]
197 %y.add = add i64 %y, %n
198 %cond2 = icmp eq i64 %y.add, 42
199 br i1 %cond2, label %exit, label %L1
200
201 exit:
202 ret i64 %y.add
203 }
204
205
206 ; Show recursive deletion of loops. Since we start with subloops and progress outward
207 ; to parent loop, we first delete the loop L2. Now loop L1 becomes a non-loop since it's backedge
208 ; from L2's preheader to L1's exit block is never taken. So, L1 gets deleted as well.
209 define void @test8(i64 %n) {
210 ; CHECK-LABEL: test8
211 ; CHECK-LABEL: entry:
212 ; CHECK-NEXT: br label %exit
213 ; CHECK-LABEL: exit:
214 ; CHECK-NEXT: ret void
215 entry:
216 br label %L1
217
218 L1:
219 br i1 true, label %exit, label %L2
220
221 L2:
222 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
223 %x.next = add i64 %x, 1
224 %y.L2 = call i64 @foo(i64 %x.next)
225 %cond = icmp slt i64 %x.next, %n
226 br i1 %cond, label %L2, label %L1
227
228 exit:
229 ret void
230 }
231
232
233 ; Delete a loop (L2) which has subloop (L3).
234 ; Here we delete loop L2, but leave L3 as is.
235 ; FIXME: Can delete L3 as well, by iteratively going backward through the single
236 ; predecessor of L3 until we reach L1's block that guarantees L3 is never
237 ; executed.
238 define void @test9(i64 %n) {
239 ; CHECK-LABEL: test9
240 ; CHECK-LABEL: L2.preheader:
241 ; CHECK-NEXT: br label %L3.preheader
242 ; CHECK-NOT: L2:
243 ; CHECK-LABEL: L3.preheader:
244 ; CHECK-NEXT: %y.L2.lcssa = phi i64 [ undef, %L2.preheader ]
245 ; CHECK-NEXT: br label %L3
246 ; CHECK-LABEL: L3:
247 ; CHECK: br i1 %cond2, label %L3, label %L1.loopexit
248 entry:
249 br label %L1
250
251 L1:
252 br i1 true, label %exit, label %L2
253
254 L2:
255 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
256 %x.next = add i64 %x, 1
257 %y.L2 = call i64 @foo(i64 %x.next)
258 %cond = icmp slt i64 %x.next, %n
259 br i1 %cond, label %L2, label %L3
260
261 L3:
262 %cond2 = icmp slt i64 %y.L2, %n
263 br i1 %cond2, label %L3, label %L1
264
265 exit:
266 ret void
267 }
268
269 ; We cannot delete L3 because of call within it.
270 ; Since L3 is not deleted, and entirely contained within L2, L2 is also not
271 ; deleted.
272 ; FIXME: We can delete unexecutable loops having
273 ; subloops contained entirely within them.
274 define void @test10(i64 %n) {
275 ; CHECK-LABEL: test10
276 ; CHECK: L2:
277 ; CHECK: L3:
278 entry:
279 br label %L1
280
281 L1:
282 br i1 true, label %exit, label %L2
283
284 L2:
285 %x = phi i64 [ 0, %L1 ], [ %x.next, %L3 ]
286 %x.next = add i64 %x, 1
287 %y.L2 = call i64 @foo(i64 %x.next)
288 %cond = icmp slt i64 %x.next, %n
289 br i1 %cond, label %L1, label %L3
290
291 L3:
292 %y.L3 = phi i64 [ %y.L2, %L2 ], [ %y.L3.next, %L3 ]
293 %y.L3.next = add i64 %y.L3, 1
294 %dummy = call i64 @foo(i64 %y.L3.next)
295 %cond2 = icmp slt i64 %y.L3, %n
296 br i1 %cond2, label %L3, label %L2
297
298 exit:
299 ret void
300 }
301
302 ; same as test10, but L3 does not contain call.
303 ; So, in the first iteration, all statements of L3 are made invariant, and L3 is
304 ; deleted.
305 ; In the next iteration, since L2 is never executed and has no subloops, we delete
306 ; L2 as well. Finally, the outermost loop L1 is deleted.
307 define void @test11(i64 %n) {
308 ; CHECK-LABEL: test11
309 ; CHECK-LABEL: entry:
310 ; CHECK-NEXT: br label %exit
311 ; CHECK-LABEL: exit:
312 ; CHECK-NEXT: ret void
313 entry:
314 br label %L1
315
316 L1:
317 br i1 true, label %exit, label %L2
318
319 L2:
320 %x = phi i64 [ 0, %L1 ], [ %x.next, %L3 ]
321 %x.next = add i64 %x, 1
322 %y.L2 = call i64 @foo(i64 %x.next)
323 %cond = icmp slt i64 %x.next, %n
324 br i1 %cond, label %L1, label %L3
325
326 L3:
327 %y.L3 = phi i64 [ %y.L2, %L2 ], [ %y.L3.next, %L3 ]
328 %y.L3.next = add i64 %y.L3, 1
329 %cond2 = icmp slt i64 %y.L3, %n
330 br i1 %cond2, label %L3, label %L2
331
332 exit:
333 ret void
334 }
335