forked from OSchip/llvm-project
[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
This commit is contained in:
parent
664e1784cd
commit
5060f5682b
|
@ -2048,6 +2048,15 @@ m_Neg(const ValTy &V) {
|
||||||
return m_Sub(m_ZeroInt(), V);
|
return m_Sub(m_ZeroInt(), V);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Matches a 'Neg' as 'sub nsw 0, V'.
|
||||||
|
template <typename ValTy>
|
||||||
|
inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
|
||||||
|
Instruction::Sub,
|
||||||
|
OverflowingBinaryOperator::NoSignedWrap>
|
||||||
|
m_NSWNeg(const ValTy &V) {
|
||||||
|
return m_NSWSub(m_ZeroInt(), V);
|
||||||
|
}
|
||||||
|
|
||||||
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
|
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
|
||||||
template <typename ValTy>
|
template <typename ValTy>
|
||||||
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
|
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
|
||||||
|
|
|
@ -3719,6 +3719,27 @@ Value *InstCombinerImpl::foldUnsignedMultiplicationOverflowCheck(ICmpInst &I) {
|
||||||
return Res;
|
return Res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Instruction *foldICmpXNegX(ICmpInst &I) {
|
||||||
|
CmpInst::Predicate Pred;
|
||||||
|
Value *X;
|
||||||
|
if (!match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X))))
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
CmpInst::Predicate NewPred;
|
||||||
|
Constant *NewRHS;
|
||||||
|
switch (Pred) {
|
||||||
|
case ICmpInst::ICMP_SGT:
|
||||||
|
NewPred = ICmpInst::ICMP_SLT;
|
||||||
|
NewRHS = Constant::getNullValue(X->getType());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ICmpInst::Create(Instruction::ICmp, NewPred, X, NewRHS, I.getName());
|
||||||
|
}
|
||||||
|
|
||||||
/// Try to fold icmp (binop), X or icmp X, (binop).
|
/// Try to fold icmp (binop), X or icmp X, (binop).
|
||||||
/// TODO: A large part of this logic is duplicated in InstSimplify's
|
/// TODO: A large part of this logic is duplicated in InstSimplify's
|
||||||
/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
|
/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
|
||||||
|
@ -3734,6 +3755,9 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
|
||||||
if (!BO0 && !BO1)
|
if (!BO0 && !BO1)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
if (Instruction *NewICmp = foldICmpXNegX(I))
|
||||||
|
return NewICmp;
|
||||||
|
|
||||||
const CmpInst::Predicate Pred = I.getPredicate();
|
const CmpInst::Predicate Pred = I.getPredicate();
|
||||||
Value *X;
|
Value *X;
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,7 @@ declare void @use8(i8)
|
||||||
|
|
||||||
define i1 @t0(i8 %x) {
|
define i1 @t0(i8 %x) {
|
||||||
; CHECK-LABEL: @t0(
|
; CHECK-LABEL: @t0(
|
||||||
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
|
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
|
||||||
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[NEG_X]], [[X]]
|
|
||||||
; CHECK-NEXT: ret i1 [[CMP]]
|
; CHECK-NEXT: ret i1 [[CMP]]
|
||||||
;
|
;
|
||||||
%neg_x = sub nsw i8 0, %x
|
%neg_x = sub nsw i8 0, %x
|
||||||
|
@ -18,8 +17,7 @@ define i1 @t0(i8 %x) {
|
||||||
define i1 @t0_commutative() {
|
define i1 @t0_commutative() {
|
||||||
; CHECK-LABEL: @t0_commutative(
|
; CHECK-LABEL: @t0_commutative(
|
||||||
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
|
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
|
||||||
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X]]
|
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], 0
|
||||||
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], [[NEG_X]]
|
|
||||||
; CHECK-NEXT: ret i1 [[CMP]]
|
; CHECK-NEXT: ret i1 [[CMP]]
|
||||||
;
|
;
|
||||||
%x = call i8 @gen8()
|
%x = call i8 @gen8()
|
||||||
|
@ -32,7 +30,7 @@ define i1 @t0_extrause(i8 %x) {
|
||||||
; CHECK-LABEL: @t0_extrause(
|
; CHECK-LABEL: @t0_extrause(
|
||||||
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
|
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
|
||||||
; CHECK-NEXT: call void @use8(i8 [[NEG_X]])
|
; CHECK-NEXT: call void @use8(i8 [[NEG_X]])
|
||||||
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[NEG_X]], [[X]]
|
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], 0
|
||||||
; CHECK-NEXT: ret i1 [[CMP]]
|
; CHECK-NEXT: ret i1 [[CMP]]
|
||||||
;
|
;
|
||||||
%neg_x = sub nsw i8 0, %x
|
%neg_x = sub nsw i8 0, %x
|
||||||
|
|
Loading…
Reference in New Issue