Skip to content

Commit 5060f56

Browse files
committed
[InstCombine] (-NSW x) s> x --> x s< 0 (PR39480)
Name: (-x) s> x --> x s< 0 %neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN %r = icmp sgt i8 %neg_x, %x => %r = icmp slt i8 %x, 0 https://rise4fun.com/Alive/ZslD https://bugs.llvm.org/show_bug.cgi?id=39480
1 parent 664e178 commit 5060f56

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,15 @@ m_Neg(const ValTy &V) {
20482048
return m_Sub(m_ZeroInt(), V);
20492049
}
20502050

2051+
/// Matches a 'Neg' as 'sub nsw 0, V'.
2052+
template <typename ValTy>
2053+
inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
2054+
Instruction::Sub,
2055+
OverflowingBinaryOperator::NoSignedWrap>
2056+
m_NSWNeg(const ValTy &V) {
2057+
return m_NSWSub(m_ZeroInt(), V);
2058+
}
2059+
20512060
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
20522061
template <typename ValTy>
20532062
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,6 +3719,27 @@ Value *InstCombinerImpl::foldUnsignedMultiplicationOverflowCheck(ICmpInst &I) {
37193719
return Res;
37203720
}
37213721

3722+
Instruction *foldICmpXNegX(ICmpInst &I) {
3723+
CmpInst::Predicate Pred;
3724+
Value *X;
3725+
if (!match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X))))
3726+
return nullptr;
3727+
3728+
CmpInst::Predicate NewPred;
3729+
Constant *NewRHS;
3730+
switch (Pred) {
3731+
case ICmpInst::ICMP_SGT:
3732+
NewPred = ICmpInst::ICMP_SLT;
3733+
NewRHS = Constant::getNullValue(X->getType());
3734+
break;
3735+
3736+
default:
3737+
return nullptr;
3738+
}
3739+
3740+
return ICmpInst::Create(Instruction::ICmp, NewPred, X, NewRHS, I.getName());
3741+
}
3742+
37223743
/// Try to fold icmp (binop), X or icmp X, (binop).
37233744
/// TODO: A large part of this logic is duplicated in InstSimplify's
37243745
/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
@@ -3734,6 +3755,9 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
37343755
if (!BO0 && !BO1)
37353756
return nullptr;
37363757

3758+
if (Instruction *NewICmp = foldICmpXNegX(I))
3759+
return NewICmp;
3760+
37373761
const CmpInst::Predicate Pred = I.getPredicate();
37383762
Value *X;
37393763

llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ declare void @use8(i8)
66

77
define i1 @t0(i8 %x) {
88
; CHECK-LABEL: @t0(
9-
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
10-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[NEG_X]], [[X]]
9+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
1110
; CHECK-NEXT: ret i1 [[CMP]]
1211
;
1312
%neg_x = sub nsw i8 0, %x
@@ -18,8 +17,7 @@ define i1 @t0(i8 %x) {
1817
define i1 @t0_commutative() {
1918
; CHECK-LABEL: @t0_commutative(
2019
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
21-
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X]]
22-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], [[NEG_X]]
20+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], 0
2321
; CHECK-NEXT: ret i1 [[CMP]]
2422
;
2523
%x = call i8 @gen8()
@@ -32,7 +30,7 @@ define i1 @t0_extrause(i8 %x) {
3230
; CHECK-LABEL: @t0_extrause(
3331
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
3432
; CHECK-NEXT: call void @use8(i8 [[NEG_X]])
35-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[NEG_X]], [[X]]
33+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], 0
3634
; CHECK-NEXT: ret i1 [[CMP]]
3735
;
3836
%neg_x = sub nsw i8 0, %x

0 commit comments

Comments
 (0)