1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00

[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:
Roman Lebedev 2020-08-06 10:45:32 +03:00
parent 95a05d24ff
commit 3b33b0cf64
3 changed files with 36 additions and 5 deletions

View File

@ -2048,6 +2048,15 @@ m_Neg(const ValTy &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'.
template <typename ValTy>
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>

View File

@ -3719,6 +3719,27 @@ Value *InstCombinerImpl::foldUnsignedMultiplicationOverflowCheck(ICmpInst &I) {
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).
/// TODO: A large part of this logic is duplicated in InstSimplify's
/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
@ -3734,6 +3755,9 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
if (!BO0 && !BO1)
return nullptr;
if (Instruction *NewICmp = foldICmpXNegX(I))
return NewICmp;
const CmpInst::Predicate Pred = I.getPredicate();
Value *X;

View File

@ -6,8 +6,7 @@ declare void @use8(i8)
define i1 @t0(i8 %x) {
; CHECK-LABEL: @t0(
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[NEG_X]], [[X]]
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%neg_x = sub nsw i8 0, %x
@ -18,8 +17,7 @@ define i1 @t0(i8 %x) {
define i1 @t0_commutative() {
; CHECK-LABEL: @t0_commutative(
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X]]
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], [[NEG_X]]
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%x = call i8 @gen8()
@ -32,7 +30,7 @@ define i1 @t0_extrause(i8 %x) {
; CHECK-LABEL: @t0_extrause(
; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[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]]
;
%neg_x = sub nsw i8 0, %x