mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-19 11:41:53 +00:00
[InstCombine] add nsw/nuw X, signbit --> or X, signbit
Changing to 'or' (rather than 'xor' when no wrapping flags are set) allows icmp simplifies to happen as expected. Differential Revision: https://reviews.llvm.org/D29729 llvm-svn: 295574
This commit is contained in:
parent
ab7638b75f
commit
ee4ec69fbc
@ -1044,9 +1044,16 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
|
|||||||
|
|
||||||
const APInt *Val;
|
const APInt *Val;
|
||||||
if (match(RHS, m_APInt(Val))) {
|
if (match(RHS, m_APInt(Val))) {
|
||||||
// X + (signbit) --> X ^ signbit
|
if (Val->isSignBit()) {
|
||||||
if (Val->isSignBit())
|
// If wrapping is not allowed, then the addition must set the sign bit:
|
||||||
|
// X + (signbit) --> X | signbit
|
||||||
|
if (I.hasNoSignedWrap() || I.hasNoUnsignedWrap())
|
||||||
|
return BinaryOperator::CreateOr(LHS, RHS);
|
||||||
|
|
||||||
|
// If wrapping is allowed, then the addition flips the sign bit of LHS:
|
||||||
|
// X + (signbit) --> X ^ signbit
|
||||||
return BinaryOperator::CreateXor(LHS, RHS);
|
return BinaryOperator::CreateXor(LHS, RHS);
|
||||||
|
}
|
||||||
|
|
||||||
// Is this add the last step in a convoluted sext?
|
// Is this add the last step in a convoluted sext?
|
||||||
Value *X;
|
Value *X;
|
||||||
|
@ -267,24 +267,22 @@ define i32 @xor_sign_bit(i32 %x) {
|
|||||||
ret i32 %add
|
ret i32 %add
|
||||||
}
|
}
|
||||||
|
|
||||||
; Lose no-wrap info by converting to xor? %x is known non-negative
|
; No-wrap info allows converting the add to 'or'.
|
||||||
; here, but not after converting to xor.
|
|
||||||
|
|
||||||
define i8 @add_nsw_signbit(i8 %x) {
|
define i8 @add_nsw_signbit(i8 %x) {
|
||||||
; CHECK-LABEL: @add_nsw_signbit(
|
; CHECK-LABEL: @add_nsw_signbit(
|
||||||
; CHECK-NEXT: [[Y:%.*]] = xor i8 %x, -128
|
; CHECK-NEXT: [[Y:%.*]] = or i8 %x, -128
|
||||||
; CHECK-NEXT: ret i8 [[Y]]
|
; CHECK-NEXT: ret i8 [[Y]]
|
||||||
;
|
;
|
||||||
%y = add nsw i8 %x, -128
|
%y = add nsw i8 %x, -128
|
||||||
ret i8 %y
|
ret i8 %y
|
||||||
}
|
}
|
||||||
|
|
||||||
; Lose no-wrap info by converting to xor? %x is known non-negative
|
; No-wrap info allows converting the add to 'or'.
|
||||||
; (x < 128 unsigned), but not after converting to xor.
|
|
||||||
|
|
||||||
define i8 @add_nuw_signbit(i8 %x) {
|
define i8 @add_nuw_signbit(i8 %x) {
|
||||||
; CHECK-LABEL: @add_nuw_signbit(
|
; CHECK-LABEL: @add_nuw_signbit(
|
||||||
; CHECK-NEXT: [[Y:%.*]] = xor i8 %x, -128
|
; CHECK-NEXT: [[Y:%.*]] = or i8 %x, -128
|
||||||
; CHECK-NEXT: ret i8 [[Y]]
|
; CHECK-NEXT: ret i8 [[Y]]
|
||||||
;
|
;
|
||||||
%y = add nuw i8 %x, 128
|
%y = add nuw i8 %x, 128
|
||||||
|
@ -223,24 +223,22 @@ define i1 @nsw_slt5_ov(i8 %a) {
|
|||||||
ret i1 %c
|
ret i1 %c
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME: InstCombine should not lose wrapping information by changing the add to xor.
|
; InstCombine should not thwart this opportunity to simplify completely.
|
||||||
|
|
||||||
define i1 @slt_zero_add_nsw_signbit(i8 %x) {
|
define i1 @slt_zero_add_nsw_signbit(i8 %x) {
|
||||||
; CHECK-LABEL: @slt_zero_add_nsw_signbit(
|
; CHECK-LABEL: @slt_zero_add_nsw_signbit(
|
||||||
; CHECK-NEXT: [[Z:%.*]] = icmp sgt i8 %x, -1
|
; CHECK-NEXT: ret i1 true
|
||||||
; CHECK-NEXT: ret i1 [[Z]]
|
|
||||||
;
|
;
|
||||||
%y = add nsw i8 %x, -128
|
%y = add nsw i8 %x, -128
|
||||||
%z = icmp slt i8 %y, 0
|
%z = icmp slt i8 %y, 0
|
||||||
ret i1 %z
|
ret i1 %z
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME: InstCombine should not lose wrapping information by changing the add to xor.
|
; InstCombine should not thwart this opportunity to simplify completely.
|
||||||
|
|
||||||
define i1 @slt_zero_add_nuw_signbit(i8 %x) {
|
define i1 @slt_zero_add_nuw_signbit(i8 %x) {
|
||||||
; CHECK-LABEL: @slt_zero_add_nuw_signbit(
|
; CHECK-LABEL: @slt_zero_add_nuw_signbit(
|
||||||
; CHECK-NEXT: [[Z:%.*]] = icmp sgt i8 %x, -1
|
; CHECK-NEXT: ret i1 true
|
||||||
; CHECK-NEXT: ret i1 [[Z]]
|
|
||||||
;
|
;
|
||||||
%y = add nuw i8 %x, 128
|
%y = add nuw i8 %x, 128
|
||||||
%z = icmp slt i8 %y, 0
|
%z = icmp slt i8 %y, 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user