From: Roman Lebedev Date: Fri, 31 May 2019 09:47:16 +0000 (+0000) Subject: [InstCombine] 'C-(C2-X) --> X+(C-C2)' constant-fold X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9c291f745045eb6c3879e5a63e7efaae4d691a4f;p=llvm [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362217 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/lib/Transforms/InstCombine/InstCombineAddSub.cpp index d422b07d49f..e6b32ba13a4 100644 --- a/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -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); } diff --git a/test/Transforms/InstCombine/addsub-constant-folding.ll b/test/Transforms/InstCombine/addsub-constant-folding.ll index 66c1e6e9168..2d9918737d2 100644 --- a/test/Transforms/InstCombine/addsub-constant-folding.ll +++ b/test/Transforms/InstCombine/addsub-constant-folding.ll @@ -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> , [[ARG:%.*]] ; CHECK-NEXT: call void @vec_use(<4 x i32> [[T0]]) -; CHECK-NEXT: [[T1:%.*]] = sub <4 x i32> , [[T0]] +; CHECK-NEXT: [[T1:%.*]] = add <4 x i32> [[ARG]], ; CHECK-NEXT: ret <4 x i32> [[T1]] ; %t0 = sub <4 x i32> , %arg