llvm.org GIT mirror llvm / 045a03d
[LoopDeletion] Clear SCEV loop dispositions `Loop::makeLoopInvariant` can hoist instructions out of loops, so loop dispositions for the loop it operated on may need to be cleared. We can be smarter here (especially around how `forgetLoopDispositions` is implemented), but let's be correct first. Fixes PR27570. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268406 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 3 years ago
2 changed file(s) with 84 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
4343 }
4444
4545 private:
46 bool isLoopDead(Loop *L, SmallVectorImpl &exitingBlocks,
47 SmallVectorImpl &exitBlocks,
48 bool &Changed, BasicBlock *Preheader);
49
46 bool isLoopDead(Loop *L, ScalarEvolution &SE,
47 SmallVectorImpl &exitingBlocks,
48 SmallVectorImpl &exitBlocks, bool &Changed,
49 BasicBlock *Preheader);
5050 };
5151 }
5252
6464 /// isLoopDead - Determined if a loop is dead. This assumes that we've already
6565 /// checked for unique exit and exiting blocks, and that the code is in LCSSA
6666 /// form.
67 bool LoopDeletion::isLoopDead(Loop *L,
67 bool LoopDeletion::isLoopDead(Loop *L, ScalarEvolution &SE,
6868 SmallVectorImpl &exitingBlocks,
6969 SmallVectorImpl &exitBlocks,
7070 bool &Changed, BasicBlock *Preheader) {
7676 // sufficient to guarantee that no loop-variant values are used outside
7777 // of the loop.
7878 BasicBlock::iterator BI = exitBlock->begin();
79 bool AllEntriesInvariant = true;
80 bool AllOutgoingValuesSame = true;
7981 while (PHINode *P = dyn_cast(BI)) {
8082 Value *incoming = P->getIncomingValueForBlock(exitingBlocks[0]);
8183
8486 // blocks, then it is impossible to statically determine which value should
8587 // be used.
8688 for (unsigned i = 1, e = exitingBlocks.size(); i < e; ++i) {
87 if (incoming != P->getIncomingValueForBlock(exitingBlocks[i]))
88 return false;
89 }
89 if (incoming != P->getIncomingValueForBlock(exitingBlocks[i])) {
90 AllOutgoingValuesSame = false;
91 break;
92 }
93 }
94
95 if (!AllOutgoingValuesSame)
96 break;
9097
9198 if (Instruction *I = dyn_cast(incoming))
92 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator()))
93 return false;
99 if (!L->makeLoopInvariant(I, Changed, Preheader->getTerminator())) {
100 AllEntriesInvariant = false;
101 break;
102 }
94103
95104 ++BI;
96105 }
106
107 if (Changed)
108 SE.forgetLoopDispositions(L);
109
110 if (!AllEntriesInvariant || !AllOutgoingValuesSame)
111 return false;
97112
98113 // Make sure that no instructions in the block have potential side-effects.
99114 // This includes instructions that could write to memory, and loads that are
152167 if (exitBlocks.size() != 1)
153168 return false;
154169
170 ScalarEvolution &SE = getAnalysis().getSE();
171
155172 // Finally, we have to check that the loop really is dead.
156173 bool Changed = false;
157 if (!isLoopDead(L, exitingBlocks, exitBlocks, Changed, preheader))
174 if (!isLoopDead(L, SE, exitingBlocks, exitBlocks, Changed, preheader))
158175 return Changed;
159176
160177 // Don't remove loops for which we can't solve the trip count.
161178 // They could be infinite, in which case we'd be changing program behavior.
162 ScalarEvolution &SE = getAnalysis().getSE();
163179 const SCEV *S = SE.getMaxBackedgeTakenCount(L);
164180 if (isa(S))
165181 return Changed;
0 ; RUN: opt -S -analyze -scalar-evolution -loop-deletion -scalar-evolution < %s | FileCheck %s --check-prefix=SCEV-EXPRS
1 ; RUN: opt -S -loop-deletion < %s | FileCheck %s --check-prefix=IR-AFTER-TRANSFORM
2 ; RUN: opt -S -indvars -loop-deletion -indvars < %s | FileCheck %s --check-prefix=ORIGINAL-CRASH
3
4 ; Checking for a crash. Loop-deletion would change the loop
5 ; disposition of an instruction, but not update SCEV.
6
7 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
8 target triple = "x86_64-apple-macosx10.11.0"
9
10 define void @pr27570() {
11 ; IR-AFTER-TRANSFORM-LABEL: @pr27570(
12 ; ORIGINAL-CRASH: @pr27570(
13 entry:
14 br label %for.cond
15
16 for.cond: ; preds = %for.cond14, %entry
17 %f.0 = phi i32 [ 20, %entry ], [ 0, %for.cond14 ]
18 br label %for.body
19
20 for.body: ; preds = %for.inc11, %for.cond
21 ; IR-AFTER-TRANSFORM: for.body:
22 ; IR-AFTER-TRANSFORM: %cmp = icmp eq i32 %val, -1
23 ; IR-AFTER-TRANSFORM: %conv7 = zext i1 %cmp to i32
24 ; IR-AFTER-TRANSFORM: for.body6:
25
26 ; SCEV-EXPRS: %conv7 = zext i1 %cmp to i32
27 ; SCEV-EXPRS: %conv7 = zext i1 %cmp to i32
28 ; SCEV-EXPRS-NEXT: --> {{.*}} LoopDispositions: { %for.body: Variant, %for.cond: Variant, %for.body6: Invariant }
29 %val = phi i32 [ -20, %for.cond ], [ %inc12, %for.inc11 ]
30 %g.040 = phi i32 [ -20, %for.cond ], [ %and.lcssa, %for.inc11 ]
31 br label %for.body6
32
33 for.body6: ; preds = %for.body6, %for.body
34 %h.039 = phi i32 [ 1, %for.body ], [ %inc, %for.body6 ]
35 %g.138 = phi i32 [ %g.040, %for.body ], [ %and, %for.body6 ]
36 %cmp = icmp eq i32 %val, -1
37 %conv7 = zext i1 %cmp to i32
38 %add.i = add nsw i32 %conv7, %h.039
39 %sext = shl i32 %add.i, 24
40 %conv8 = ashr exact i32 %sext, 24
41 %cmp9 = icmp eq i32 %conv8, %f.0
42 %conv10 = zext i1 %cmp9 to i32
43 %and = and i32 %conv10, %g.138
44 %inc = add i32 %h.039, 1
45 br i1 undef, label %for.inc11, label %for.body6
46
47 for.inc11: ; preds = %for.body6
48 %and.lcssa = phi i32 [ %and, %for.body6 ]
49 %inc12 = add nsw i32 %val, 1
50 %tobool = icmp eq i32 %inc12, 0
51 br i1 %tobool, label %for.cond14, label %for.body
52
53 for.cond14: ; preds = %for.cond14, %for.inc11
54 br i1 undef, label %for.cond, label %for.cond14
55 }