Skip to content

Commit a6c8e27

Browse files
committed
[IndVars] Generate zext nneg when locally obvious
zext nneg was recently added to the IR in #67982. This patch teaches SimplifyIndVars to prefer zext nneg over *both* sext and plain zext, when a local SCEV query indicates the source is non-negative. The choice to prefer zext nneg over sext looks slightly aggressive here, but probably isn't so much in practice. For cases where we'd "remember" the range fact, instcombine would convert the sext into a zext nneg anyways. The only cases where this produces a different result overall are when SCEV knows a non-local fact, and it doesn't get materialized into the IR. Those are exactly the cases where using zext nneg are most useful. We do run the risk of e.g. a missing combine - since we haven't updated most of them yet - but that seems like a manageable risk. Note that there are much deeper algorithmic changes we could make to this code to exploit zext nneg, but this seemed like a reasonable and low risk starting point.
1 parent 1e39575 commit a6c8e27

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,15 @@ Value *WidenIV::createExtendInst(Value *NarrowOper, Type *WideType,
12011201
L = L->getParentLoop())
12021202
Builder.SetInsertPoint(L->getLoopPreheader()->getTerminator());
12031203

1204+
// If we know the operand is never negative, prefer zext nneg.
1205+
// For constant expressions, fall back to plain sext or zext.
1206+
if (SE->isKnownNonNegative(SE->getSCEV(NarrowOper))) {
1207+
auto *Res = Builder.CreateZExt(NarrowOper, WideType);
1208+
if (auto *I = dyn_cast<Instruction>(Res))
1209+
I->setNonNeg(true);
1210+
return Res;
1211+
}
1212+
12041213
return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) :
12051214
Builder.CreateZExt(NarrowOper, WideType);
12061215
}
@@ -1686,6 +1695,16 @@ bool WidenIV::widenWithVariantUse(WidenIV::NarrowIVDefUse DU) {
16861695
auto ExtendedOp = [&](Value * V)->Value * {
16871696
if (V == NarrowUse)
16881697
return WideBO;
1698+
1699+
// If we know the operand is never negative, prefer zext nneg.
1700+
// For constant expressions, fall back to plain sext or zext.
1701+
if (SE->isKnownNonNegative(SE->getSCEV(V))) {
1702+
auto *Res = Builder.CreateZExt(V, WideBO->getType());
1703+
if (auto *I = dyn_cast<Instruction>(Res))
1704+
I->setNonNeg(true);
1705+
return Res;
1706+
}
1707+
16891708
if (ExtKind == ExtendKind::Zero)
16901709
return Builder.CreateZExt(V, WideBO->getType());
16911710
else

llvm/test/Analysis/ScalarEvolution/guards.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ define void @test_2(i32 %n, ptr %len_buf) {
5757
; CHECK-SAME: (i32 [[N:%.*]], ptr [[LEN_BUF:%.*]]) {
5858
; CHECK-NEXT: entry:
5959
; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[LEN_BUF]], align 4, !range [[RNG1:![0-9]+]]
60-
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[LEN]] to i64
60+
; CHECK-NEXT: [[TMP0:%.*]] = zext nneg i32 [[LEN]] to i64
6161
; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[N]] to i64
6262
; CHECK-NEXT: br label [[LOOP:%.*]]
6363
; CHECK: loop:

llvm/test/Transforms/IndVarSimplify/X86/pr59615.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ define void @test() {
1717
; CHECK-NEXT: ret void
1818
; CHECK: bb8:
1919
; CHECK-NEXT: [[VAR9:%.*]] = load atomic i32, ptr addrspace(1) poison unordered, align 8, !range [[RNG0]], !invariant.load !1, !noundef !1
20-
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[VAR9]] to i64
20+
; CHECK-NEXT: [[TMP0:%.*]] = zext nneg i32 [[VAR9]] to i64
2121
; CHECK-NEXT: [[VAR10:%.*]] = icmp ult i64 [[INDVARS_IV]], [[TMP0]]
2222
; CHECK-NEXT: br i1 [[VAR10]], label [[BB12]], label [[BB11:%.*]]
2323
; CHECK: bb11:

llvm/test/Transforms/IndVarSimplify/post-inc-range.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ define void @test_range_metadata(ptr %array_length_ptr, ptr %base,
120120
; CHECK: for.body:
121121
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_INC:%.*]] ], [ [[TMP0]], [[FOR_BODY_LR_PH:%.*]] ]
122122
; CHECK-NEXT: [[ARRAY_LENGTH:%.*]] = load i32, ptr [[ARRAY_LENGTH_PTR:%.*]], align 4, !range [[RNG0:![0-9]+]]
123-
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[ARRAY_LENGTH]] to i64
123+
; CHECK-NEXT: [[TMP2:%.*]] = zext nneg i32 [[ARRAY_LENGTH]] to i64
124124
; CHECK-NEXT: [[WITHIN_LIMITS:%.*]] = icmp ult i64 [[INDVARS_IV]], [[TMP2]]
125125
; CHECK-NEXT: br i1 [[WITHIN_LIMITS]], label [[CONTINUE:%.*]], label [[FOR_END:%.*]]
126126
; CHECK: continue:

llvm/test/Transforms/LoopFlatten/widen-iv2.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ define dso_local i32 @fn1() local_unnamed_addr #0 {
3939
; CHECK-NEXT: [[INDVAR:%.*]] = phi i64 [ [[INDVAR_NEXT:%.*]], [[FOR_BODY3_US]] ], [ 0, [[FOR_COND1_PREHEADER_US]] ]
4040
; CHECK-NEXT: [[J_014_US:%.*]] = phi i32 [ 0, [[FOR_COND1_PREHEADER_US]] ], [ [[INC_US:%.*]], [[FOR_BODY3_US]] ]
4141
; CHECK-NEXT: [[TMP7:%.*]] = add nsw i64 [[INDVAR]], [[TMP5]]
42-
; CHECK-NEXT: [[TMP8:%.*]] = sext i32 [[J_014_US]] to i64
42+
; CHECK-NEXT: [[TMP8:%.*]] = zext nneg i32 [[J_014_US]] to i64
4343
; CHECK-NEXT: [[TMP9:%.*]] = add nsw i64 [[TMP8]], [[TMP5]]
4444
; CHECK-NEXT: [[ADD_US:%.*]] = add nsw i32 [[J_014_US]], [[MUL_US]]
4545
; CHECK-NEXT: [[IDXPROM_US:%.*]] = sext i32 [[ADD_US]] to i64

llvm/test/Transforms/LoopFlatten/widen-iv3.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ define i16 @foo() {
1818
; CHECK-NEXT: [[SUM_012:%.*]] = phi i16 [ 0, [[ENTRY]] ], [ [[ADD5_LCSSA:%.*]], [[FOR_COND_CLEANUP3]] ]
1919
; CHECK-NEXT: [[TMP0:%.*]] = mul nsw i32 [[INDVAR2]], 16
2020
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i16 [[I_013]], 16
21-
; CHECK-NEXT: [[TMP1:%.*]] = zext i16 [[MUL]] to i32
21+
; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i16 [[MUL]] to i32
2222
; CHECK-NEXT: br label [[FOR_BODY4:%.*]]
2323
; CHECK: for.cond.cleanup:
2424
; CHECK-NEXT: [[ADD5_LCSSA_LCSSA:%.*]] = phi i16 [ [[ADD5_LCSSA]], [[FOR_COND_CLEANUP3]] ]

0 commit comments

Comments
 (0)