Skip to content

Commit 6071273

Browse files
committed
[IndVars] Teach replaceCongruentIVs to avoid scrambling induction variables
replaceCongruentIVs analysis is based on ScalarEvolution; this makes comparing different PHIs and performing the replacement straightforward. However, it can have some side-effects: it isn't aware whether an induction variable is in canonical form, so it can perform replacements which obscure the meaning of the IR. In test22 in widen-loop-comp.ll, the resulting loop can't be analyzed by ScalarEvolution at all. My attempted solution is to restrict the transform: don't try to replace induction variables using PHI nodes that don't represent simple induction variables. I'm not sure if this is the best solution; suggestions welcome. Differential Revision: https://reviews.llvm.org/D121950
1 parent 1159266 commit 6071273

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,11 +1652,17 @@ SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
16521652
OrigPhiRef = Phi;
16531653
if (Phi->getType()->isIntegerTy() && TTI &&
16541654
TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) {
1655-
// This phi can be freely truncated to the narrowest phi type. Map the
1656-
// truncated expression to it so it will be reused for narrow types.
1657-
const SCEV *TruncExpr =
1658-
SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType());
1659-
ExprToIVMap[TruncExpr] = Phi;
1655+
// Make sure we only rewrite using simple induction variables;
1656+
// otherwise, we can make the trip count of a loop unanalyzable
1657+
// to SCEV.
1658+
const SCEV *PhiExpr = SE.getSCEV(Phi);
1659+
if (isa<SCEVAddRecExpr>(PhiExpr)) {
1660+
// This phi can be freely truncated to the narrowest phi type. Map the
1661+
// truncated expression to it so it will be reused for narrow types.
1662+
const SCEV *TruncExpr =
1663+
SE.getTruncateExpr(PhiExpr, Phis.back()->getType());
1664+
ExprToIVMap[TruncExpr] = Phi;
1665+
}
16601666
}
16611667
continue;
16621668
}

llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -856,15 +856,15 @@ define i32 @test16_unsigned_pos1(i32 %start, ptr %p, ptr %q, i32 %x) {
856856
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[START:%.*]] to i64
857857
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i64 [[TMP0]], -1
858858
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[X:%.*]] to i64
859-
; CHECK-NEXT: [[ICMP_USER_WIDE5:%.*]] = icmp ult i64 [[TMP1]], [[TMP2]]
859+
; CHECK-NEXT: [[ICMP_USER_WIDE_FIRST_ITER:%.*]] = icmp ult i64 [[TMP1]], [[TMP2]]
860860
; CHECK-NEXT: br label [[LOOP:%.*]]
861861
; CHECK: loop:
862862
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
863863
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[INDVARS_IV]], 0
864864
; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[INDVARS_IV]], -1
865865
; CHECK-NEXT: br i1 [[COND]], label [[EXIT:%.*]], label [[GUARDED:%.*]]
866866
; CHECK: guarded:
867-
; CHECK-NEXT: br i1 [[ICMP_USER_WIDE5]], label [[BACKEDGE]], label [[SIDE_EXIT:%.*]]
867+
; CHECK-NEXT: br i1 [[ICMP_USER_WIDE_FIRST_ITER]], label [[BACKEDGE]], label [[SIDE_EXIT:%.*]]
868868
; CHECK: backedge:
869869
; CHECK-NEXT: [[STORE_ADDR:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[TMP3]]
870870
; CHECK-NEXT: store i32 1, ptr [[STORE_ADDR]], align 4
@@ -1196,15 +1196,15 @@ define i32 @test16_signed_neg(i32 %start, ptr %p, ptr %q, i32 %x) {
11961196
; CHECK-LABEL: @test16_signed_neg(
11971197
; CHECK-NEXT: entry:
11981198
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[START:%.*]], -1
1199-
; CHECK-NEXT: [[ICMP_USER3:%.*]] = icmp ult i32 [[TMP0]], [[X:%.*]]
1199+
; CHECK-NEXT: [[ICMP_USER_FIRST_ITER:%.*]] = icmp ult i32 [[TMP0]], [[X:%.*]]
12001200
; CHECK-NEXT: br label [[LOOP:%.*]]
12011201
; CHECK: loop:
12021202
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[START]], [[ENTRY:%.*]] ], [ [[IV_NEXT_1:%.*]], [[BACKEDGE:%.*]] ]
12031203
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[IV]], 0
12041204
; CHECK-NEXT: [[FOO:%.*]] = add i32 [[IV]], -1
12051205
; CHECK-NEXT: br i1 [[COND]], label [[EXIT:%.*]], label [[GUARDED:%.*]]
12061206
; CHECK: guarded:
1207-
; CHECK-NEXT: br i1 [[ICMP_USER3]], label [[BACKEDGE]], label [[SIDE_EXIT:%.*]]
1207+
; CHECK-NEXT: br i1 [[ICMP_USER_FIRST_ITER]], label [[BACKEDGE]], label [[SIDE_EXIT:%.*]]
12081208
; CHECK: backedge:
12091209
; CHECK-NEXT: [[INDEX:%.*]] = sext i32 [[FOO]] to i64
12101210
; CHECK-NEXT: [[STORE_ADDR:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
@@ -1440,15 +1440,17 @@ exit: ; preds = %loop
14401440
ret void
14411441
}
14421442

1443+
; Don't perform replacement here; SCEV won't recognize the new PHI as an
1444+
; induction variable.
14431445
define void @test22(ptr %ptr) {
14441446
; CHECK-LABEL: @test22(
14451447
; CHECK-NEXT: entry:
14461448
; CHECK-NEXT: store i16 0, ptr [[PTR:%.*]], align 4
14471449
; CHECK-NEXT: br label [[LOOP:%.*]]
14481450
; CHECK: loop:
1449-
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
1450-
; CHECK-NEXT: [[INDVARS:%.*]] = trunc i32 [[IV]] to i16
1451-
; CHECK-NEXT: [[VAL_INC:%.*]] = add i16 [[INDVARS]], 1
1451+
; CHECK-NEXT: [[VAL:%.*]] = phi i16 [ [[VAL_INC:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
1452+
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY]] ]
1453+
; CHECK-NEXT: [[VAL_INC]] = add i16 [[VAL]], 1
14521454
; CHECK-NEXT: store i16 [[VAL_INC]], ptr [[PTR]], align 4
14531455
; CHECK-NEXT: [[IV_WIDE:%.*]] = zext i32 [[IV]] to i64
14541456
; CHECK-NEXT: call void @foo(i64 [[IV_WIDE]])

0 commit comments

Comments
 (0)