1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Reland [InstCombine] Fold ((X - Y) - Z) to X - (Y + Z) (PR49858)

This reverts commit a547b4e26b311e417cd51100e379693f51a3f448,
relanding commit 31d219d2997fed1b7dc97e0adf170d5aaf65883e,
which was reverted because there was a conflicting inverse transform,
which was causing an endless combine loop, which has now been adjusted.

Original commit message:

https://alive2.llvm.org/ce/z/67w-wQ

We prefer `add`s over `sub`, and this particular xform
allows further folds to happen:

Fixes https://bugs.llvm.org/show_bug.cgi?id=49858
This commit is contained in:
Roman Lebedev 2021-04-07 11:04:57 +03:00
parent 692350022e
commit 4792a641c6
3 changed files with 14 additions and 8 deletions

View File

@ -1802,6 +1802,12 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
return BinaryOperator::CreateSub(XZ, YW);
}
// ((X - Y) - Op1) --> X - (Y + Op1)
if (match(Op0, m_OneUse(m_Sub(m_Value(X), m_Value(Y))))) {
Value *Add = Builder.CreateAdd(Y, Op1);
return BinaryOperator::CreateSub(X, Add);
}
auto m_AddRdx = [](Value *&Vec) {
return m_OneUse(m_Intrinsic<Intrinsic::vector_reduce_add>(m_Value(Vec)));
};

View File

@ -305,9 +305,9 @@ define i32 @nabs_canonical_8(i32 %a) {
define i32 @nabs_canonical_9(i32 %a, i32 %b) {
; CHECK-LABEL: @nabs_canonical_9(
; CHECK-NEXT: [[T1:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[T2:%.*]] = sub i32 [[B]], [[A]]
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.abs.i32(i32 [[T1]], i1 false)
; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[T2]], [[TMP1]]
; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[TMP1]], [[A]]
; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[B]], [[TMP2]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%t1 = sub i32 %a, %b

View File

@ -8,8 +8,8 @@ declare void @use8(i8)
; Basic test
define i8 @t0(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @t0(
; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%i0 = sub i8 %x, %y
@ -20,8 +20,8 @@ define i8 @t0(i8 %x, i8 %y, i8 %z) {
; No flags are propagated
define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @t1_flags(
; CHECK-NEXT: [[O0:%.*]] = sub nuw nsw i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub nuw nsw i8 [[O0]], [[Z:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%o0 = sub nuw nsw i8 %x, %y
@ -47,8 +47,8 @@ define i8 @n2(i8 %x, i8 %y, i8 %z) {
define i8 @t3_c0(i8 %y, i8 %z) {
; CHECK-LABEL: @t3_c0(
; CHECK-NEXT: [[I0:%.*]] = sub i8 42, [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = sub i8 42, [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%i0 = sub i8 42, %y