Skip to content

Commit d0f472c

Browse files
authored
SimplifyIndVar: teach widenLoopCompare about samesign (llvm#125851)
Proof: https://alive2.llvm.org/ce/z/NVXaeo
1 parent 7ebacf3 commit d0f472c

File tree

3 files changed

+719
-64
lines changed

3 files changed

+719
-64
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;

0 commit comments

Comments
 (0)