Skip to content

Commit

Permalink
SimplifyIndVar: teach widenLoopCompare about samesign (llvm#125851)
Browse files Browse the repository at this point in the history
  • Loading branch information
artagnon authored and Icohedron committed Feb 11, 2025
1 parent e376737 commit 1b1cc21
Show file tree
Hide file tree
Showing 3 changed files with 719 additions and 64 deletions.
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ bool WidenIV::widenLoopCompare(WidenIV::NarrowIVDefUse DU) {
// (A) == icmp slt i32 sext(%narrow), sext(%val)
// == icmp slt i32 zext(%narrow), sext(%val)
bool IsSigned = getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
if (!(DU.NeverNegative || IsSigned == Cmp->isSigned()))
bool CmpPreferredSign = Cmp->hasSameSign() ? IsSigned : Cmp->isSigned();
if (!DU.NeverNegative && IsSigned != CmpPreferredSign)
return false;

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

// Widen the other operand of the compare, if necessary.
if (CastWidth < IVWidth) {
Value *ExtOp = createExtendInst(Op, WideType, Cmp->isSigned(), Cmp);
Value *ExtOp = createExtendInst(Op, WideType, CmpPreferredSign, Cmp);
DU.NarrowUse->replaceUsesOfWith(Op, ExtOp);
}
return true;
Expand Down
Loading

0 comments on commit 1b1cc21

Please sign in to comment.