Skip to content

Commit f8e7b53

Browse files
committed
[IndVars, RLEV] Support rewriting exit values in loops without known exits (prep work)
This is a prepatory patch for future work on support exit value rewriting in loops with a mixture of computable and non-computable exit counts. The intention is to be "mostly NFC" - i.e. not enable any interesting new transforms - but in practice, there are some small output changes. The test differences are caused by cases wherewhere getSCEVAtScope can simplify a single entry phi without needing any knowledge of the loop. llvm-svn: 367485
1 parent 04bdb25 commit f8e7b53

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ bool IndVarSimplify::rewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) {
630630
// and varies predictably *inside* the loop. Evaluate the value it
631631
// contains when the loop exits, if possible.
632632
const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
633-
if (!SE->isLoopInvariant(ExitValue, L) ||
633+
if (isa<SCEVCouldNotCompute>(ExitValue) ||
634+
!SE->isLoopInvariant(ExitValue, L) ||
634635
!isSafeToExpand(ExitValue, *SE))
635636
continue;
636637

@@ -804,7 +805,7 @@ bool IndVarSimplify::canLoopBeDeleted(
804805
L->getExitingBlocks(ExitingBlocks);
805806
SmallVector<BasicBlock *, 8> ExitBlocks;
806807
L->getUniqueExitBlocks(ExitBlocks);
807-
if (ExitBlocks.size() > 1 || ExitingBlocks.size() > 1)
808+
if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
808809
return false;
809810

810811
BasicBlock *ExitBlock = ExitBlocks[0];
@@ -2774,14 +2775,11 @@ bool IndVarSimplify::run(Loop *L) {
27742775
Rewriter.disableCanonicalMode();
27752776
Changed |= simplifyAndExtend(L, Rewriter, LI);
27762777

2777-
// Check to see if this loop has a computable loop-invariant execution count.
2778-
// If so, this means that we can compute the final value of any expressions
2778+
// Check to see if we can compute the final value of any expressions
27792779
// that are recurrent in the loop, and substitute the exit values from the
2780-
// loop into any instructions outside of the loop that use the final values of
2781-
// the current expressions.
2782-
//
2783-
if (ReplaceExitValue != NeverRepl &&
2784-
!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
2780+
// loop into any instructions outside of the loop that use the final values
2781+
// of the current expressions.
2782+
if (ReplaceExitValue != NeverRepl)
27852783
Changed |= rewriteLoopExitValues(L, Rewriter);
27862784

27872785
// Eliminate redundant IV cycles.

llvm/test/Transforms/IndVarSimplify/lftr-pr20680.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ define void @f() {
6262
; CHECK: cond.false.us:
6363
; CHECK-NEXT: br label [[COND_END_US]]
6464
; CHECK: cond.end.us:
65-
; CHECK-NEXT: [[COND_US:%.*]] = phi i32 [ [[DIV]], [[COND_FALSE_US]] ], [ [[INDVARS_IV]], [[FOR_BODY3_US]] ]
6665
; CHECK-NEXT: [[TMP6:%.*]] = load i32, i32* @b, align 4
6766
; CHECK-NEXT: [[CMP91_US:%.*]] = icmp slt i32 [[TMP6]], 1
6867
; CHECK-NEXT: br i1 [[CMP91_US]], label [[FOR_INC_LR_PH_US:%.*]], label [[FOR_COND2_LOOPEXIT_US:%.*]]
@@ -79,10 +78,9 @@ define void @f() {
7978
; CHECK-NEXT: store i32 1, i32* @b, align 4
8079
; CHECK-NEXT: br label [[FOR_COND2_LOOPEXIT_US]]
8180
; CHECK: for.cond2.for.inc13_crit_edge.us-lcssa.us.us-lcssa:
82-
; CHECK-NEXT: [[COND_LCSSA_PH_US_PH:%.*]] = phi i32 [ [[COND_US]], [[FOR_COND2_LOOPEXIT_US]] ]
8381
; CHECK-NEXT: br label [[FOR_COND2_FOR_INC13_CRIT_EDGE_US_LCSSA_US]]
8482
; CHECK: for.cond2.for.inc13_crit_edge.us-lcssa.us:
85-
; CHECK-NEXT: [[COND_LCSSA_PH_US:%.*]] = phi i32 [ [[COND_LCSSA_PH_US_PH]], [[FOR_COND2_FOR_INC13_CRIT_EDGE_US_LCSSA_US_US_LCSSA]] ], [ [[DIV]], [[FOR_COND2_FOR_INC13_CRIT_EDGE_US_LCSSA_US_US_LCSSA_US]] ]
83+
; CHECK-NEXT: [[COND_LCSSA_PH_US:%.*]] = phi i32 [ [[DIV]], [[FOR_COND2_FOR_INC13_CRIT_EDGE_US_LCSSA_US_US_LCSSA]] ], [ [[DIV]], [[FOR_COND2_FOR_INC13_CRIT_EDGE_US_LCSSA_US_US_LCSSA_US]] ]
8684
; CHECK-NEXT: br label [[FOR_COND2_FOR_INC13_CRIT_EDGE:%.*]]
8785
; CHECK: for.body3.lr.ph.split:
8886
; CHECK-NEXT: br i1 [[TOBOOL]], label [[FOR_BODY3_LR_PH_SPLIT_SPLIT_US:%.*]], label [[FOR_BODY3_LR_PH_SPLIT_FOR_BODY3_LR_PH_SPLIT_SPLIT_CRIT_EDGE:%.*]]

llvm/test/Transforms/IndVarSimplify/pr38674.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ define i32 @test_02(i32 %x) {
9292
; CHECK: for.end:
9393
; CHECK-NEXT: br i1 false, label [[FOR_COND4_PREHEADER]], label [[FOR_END9:%.*]]
9494
; CHECK: for.end9:
95-
; CHECK-NEXT: [[INC11:%.*]] = add nuw nsw i32 0, [[X]]
9695
; CHECK-NEXT: br i1 false, label [[FOR_COND1_PREHEADER]], label [[RETURN_LOOPEXIT3:%.*]]
9796
; CHECK: return.loopexit:
9897
; CHECK-NEXT: unreachable
9998
; CHECK: return.loopexit3:
100-
; CHECK-NEXT: [[INC11_LCSSA:%.*]] = phi i32 [ [[INC11]], [[FOR_END9]] ]
10199
; CHECK-NEXT: br label [[RETURN:%.*]]
102100
; CHECK: return:
103-
; CHECK-NEXT: ret i32 [[INC11_LCSSA]]
101+
; CHECK-NEXT: ret i32 [[X]]
104102
;
105103
entry:
106104
br label %for.cond1.preheader

0 commit comments

Comments
 (0)