Skip to content

Commit cd3bfc1

Browse files
[SCEV] Preserve LCSSA when reusing dominating variable
This seems to just be a missing case in SCEV. This came up when looking at making LSR preserve LCSSA for the NewPM. Regression test of that nature has been added. Reviewers: fhahn, nikic, arsenm Pull Request: llvm/llvm-project#192831
1 parent 5ad66bd commit cd3bfc1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,8 @@ SCEVExpander::expandAddRecExprLiterally(SCEVUseT<const SCEVAddRecExpr *> S) {
12371237
// We have decided to reuse an induction variable of a dominating loop. Apply
12381238
// truncation and/or inversion of the step.
12391239
if (TruncTy) {
1240+
if (TruncTy != Result->getType() || InvertStep)
1241+
Result = fixupLCSSAFormFor(Result);
12401242
// Truncate the result.
12411243
if (TruncTy != Result->getType())
12421244
Result = Builder.CreateTrunc(Result, TruncTy);

llvm/test/Transforms/LoopStrengthReduce/X86/lcssa-preservation-regression.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,43 @@ loop1.header: ; preds = %loop1.latc
161161
loop1.latch: ; preds = %loop1.header
162162
br i1 false, label %funcexit, label %loop1.header
163163
}
164+
165+
define i32 @regression4() {
166+
; CHECK-LABEL: define i32 @regression4() {
167+
; CHECK-NEXT: [[ENTRY:.*:]]
168+
; CHECK-NEXT: br label %[[WHILE_COND_OUTER:.*]]
169+
; CHECK: [[WHILE_COND_OUTER_LOOPEXIT:.*]]:
170+
; CHECK-NEXT: br label %[[WHILE_COND_OUTER]]
171+
; CHECK: [[WHILE_COND_OUTER]]:
172+
; CHECK-NEXT: br label %[[WHILE_COND:.*]]
173+
; CHECK: [[WHILE_COND]]:
174+
; CHECK-NEXT: br i1 false, label %[[WHILE_COND]], label %[[FOR_BODY_PREHEADER:.*]]
175+
; CHECK: [[FOR_BODY_PREHEADER]]:
176+
; CHECK-NEXT: br label %[[FOR_BODY:.*]]
177+
; CHECK: [[FOR_BODY]]:
178+
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i32 [ 0, %[[FOR_BODY_PREHEADER]] ], [ [[LSR_IV_NEXT:%.*]], %[[FOR_BODY]] ]
179+
; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i32 [[LSR_IV]], 0
180+
; CHECK-NEXT: [[LSR_IV_NEXT]] = add i32 [[LSR_IV]], -1
181+
; CHECK-NEXT: br i1 false, label %[[WHILE_COND_OUTER_LOOPEXIT]], label %[[FOR_BODY]]
182+
;
183+
entry:
184+
br label %while.cond.outer
185+
186+
while.cond.outer: ; preds = %for.body, %entry
187+
%phi = phi i32 [ 0, %entry ], [ %add, %for.body ]
188+
br label %while.cond
189+
190+
while.cond: ; preds = %while.cond, %while.cond.outer
191+
%phi3 = phi i32 [ 0, %while.cond.outer ], [ %add5, %while.cond ]
192+
%phi4 = phi i32 [ 0, %while.cond.outer ], [ %add, %while.cond ]
193+
%add = add i32 %phi4, 1
194+
%add5 = add i32 %phi3, 1
195+
br i1 false, label %while.cond, label %for.body
196+
197+
for.body: ; preds = %for.body, %while.cond
198+
%phi7 = phi i32 [ %add8, %for.body ], [ 0, %while.cond ]
199+
%add8 = add i32 %phi7, 1
200+
%sub = sub i32 0, %phi3
201+
%icmp = icmp eq i32 %phi7, %sub
202+
br i1 false, label %while.cond.outer, label %for.body
203+
}

0 commit comments

Comments
 (0)