Skip to content

Commit 0202f3b

Browse files
committed
SimplifyIndVar: teach widenLoopCompare about samesign
There is still some way to go to optimize optimally with samesign.
1 parent 469a756 commit 0202f3b

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,8 @@ bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
16141614
// (A) == icmp slt i32 sext(%narrow), sext(%val)
16151615
// == icmp slt i32 zext(%narrow), sext(%val)
16161616
bool IsSigned = getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1617-
if (!(DU.NeverNegative || IsSigned == Cmp->isSigned()))
1617+
bool CmpPreferredSign = Cmp->hasSameSign() ? IsSigned : Cmp->isSigned();
1618+
if (!DU.NeverNegative && IsSigned != CmpPreferredSign)
16181619
return false;
16191620

16201621
Value *Op = Cmp->getOperand(Cmp->getOperand(0) == DU.NarrowDef ? 1 : 0);
@@ -1627,7 +1628,7 @@ bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
16271628

16281629
// Widen the other operand of the compare, if necessary.
16291630
if (CastWidth < IVWidth) {
1630-
Value *ExtOp = createExtendInst(Op, WideType, Cmp->isSigned(), Cmp);
1631+
Value *ExtOp = createExtendInst(Op, WideType, CmpPreferredSign, Cmp);
16311632
DU.NarrowUse->replaceUsesOfWith(Op, ExtOp);
16321633
}
16331634
return true;

llvm/test/Transforms/IndVarSimplify/iv-ext-samesign.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ define i32 @iv_zext_zext_gt_slt(i32 %iter.count, ptr %ptr) {
7979
; CHECK-NEXT: [[INDVARS_IV_NEXT2]] = add nsw i64 [[INDVARS_IV1]], -1
8080
; CHECK-NEXT: [[GEP_OUTER:%.*]] = getelementptr double, ptr [[PTR]], i64 [[INDVARS_IV_NEXT2]]
8181
; CHECK-NEXT: store double poison, ptr [[GEP_OUTER]], align 8
82-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw i64 [[INDVARS_IV1]] to i32
83-
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i32 [[TMP1]], 1
82+
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i64 [[INDVARS_IV1]], 1
8483
; CHECK-NEXT: br i1 [[EXIT_COND_OUTER]], label %[[INNER_LOOP_PREHEADER:.*]], label %[[PH]]
8584
; CHECK: [[INNER_LOOP_PREHEADER]]:
8685
; CHECK-NEXT: br label %[[INNER_LOOP:.*]]
@@ -320,8 +319,7 @@ define i32 @iv_sext_sext_gt_slt(i32 %iter.count, ptr %ptr) {
320319
; CHECK-NEXT: [[INDVARS_IV_NEXT2]] = add nsw i64 [[INDVARS_IV1]], -1
321320
; CHECK-NEXT: [[GEP_OUTER:%.*]] = getelementptr double, ptr [[PTR]], i64 [[INDVARS_IV_NEXT2]]
322321
; CHECK-NEXT: store double poison, ptr [[GEP_OUTER]], align 8
323-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw i64 [[INDVARS_IV1]] to i32
324-
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i32 [[TMP1]], 1
322+
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i64 [[INDVARS_IV1]], 1
325323
; CHECK-NEXT: br i1 [[EXIT_COND_OUTER]], label %[[INNER_LOOP_PREHEADER:.*]], label %[[PH]]
326324
; CHECK: [[INNER_LOOP_PREHEADER]]:
327325
; CHECK-NEXT: br label %[[INNER_LOOP:.*]]
@@ -442,8 +440,7 @@ define i32 @iv_sext_sext_gt_lt(i32 %iter.count, ptr %ptr) {
442440
; CHECK-NEXT: [[INDVARS_IV_NEXT4]] = add nsw i64 [[INDVARS_IV3]], -1
443441
; CHECK-NEXT: [[GEP_OUTER:%.*]] = getelementptr double, ptr [[PTR]], i64 [[INDVARS_IV_NEXT4]]
444442
; CHECK-NEXT: store double poison, ptr [[GEP_OUTER]], align 8
445-
; CHECK-NEXT: [[TMP2:%.*]] = trunc nsw i64 [[INDVARS_IV3]] to i32
446-
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i32 [[TMP2]], 1
443+
; CHECK-NEXT: [[EXIT_COND_OUTER:%.*]] = icmp samesign ugt i64 [[INDVARS_IV3]], 1
447444
; CHECK-NEXT: br i1 [[EXIT_COND_OUTER]], label %[[INNER_LOOP_PREHEADER:.*]], label %[[PH]]
448445
; CHECK: [[INNER_LOOP_PREHEADER]]:
449446
; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[INDVARS_IV1]] to i64

0 commit comments

Comments
 (0)