[InstCombine] 'C-(C2-X) --> X+(C-C2)' constant-fold

It looks this fold was already partially happening, indirectly
via some other folds, but with one-use limitation.
No other fold here has that restriction.

https://rise4fun.com/Alive/ftR

llvm-svn: 362217
This commit is contained in:
Roman Lebedev 2019-05-31 09:47:16 +00:00
parent 886c4ef35a
commit 39390d8317
2 changed files with 8 additions and 3 deletions

View File

@ -1609,8 +1609,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
if (Instruction *R = foldOpIntoPhi(I, PN))
return R;
// C-(X+C2) --> (C-C2)-X
Constant *C2;
// C-(C2-X) --> X+(C-C2)
if (match(Op1, m_Sub(m_Constant(C2), m_Value(X))))
return BinaryOperator::CreateAdd(X, ConstantExpr::getSub(C, C2));
// C-(X+C2) --> (C-C2)-X
if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
}

View File

@ -485,7 +485,7 @@ define i32 @const_sub_const_sub_extrause(i32 %arg) {
; CHECK-LABEL: @const_sub_const_sub_extrause(
; CHECK-NEXT: [[T0:%.*]] = sub i32 8, [[ARG:%.*]]
; CHECK-NEXT: call void @use(i32 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = sub i32 2, [[T0]]
; CHECK-NEXT: [[T1:%.*]] = add i32 [[ARG]], -6
; CHECK-NEXT: ret i32 [[T1]]
;
%t0 = sub i32 8, %arg
@ -508,7 +508,7 @@ define <4 x i32> @vec_const_sub_const_sub_extrause(<4 x i32> %arg) {
; CHECK-LABEL: @vec_const_sub_const_sub_extrause(
; CHECK-NEXT: [[T0:%.*]] = sub <4 x i32> <i32 8, i32 8, i32 8, i32 8>, [[ARG:%.*]]
; CHECK-NEXT: call void @vec_use(<4 x i32> [[T0]])
; CHECK-NEXT: [[T1:%.*]] = sub <4 x i32> <i32 2, i32 2, i32 2, i32 2>, [[T0]]
; CHECK-NEXT: [[T1:%.*]] = add <4 x i32> [[ARG]], <i32 -6, i32 -6, i32 -6, i32 -6>
; CHECK-NEXT: ret <4 x i32> [[T1]]
;
%t0 = sub <4 x i32> <i32 8, i32 8, i32 8, i32 8>, %arg