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

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri May 31 02:47:16 PDT 2019


Author: lebedevri
Date: Fri May 31 02:47:16 2019
New Revision: 362217

URL: http://llvm.org/viewvc/llvm-project?rev=362217&view=rev
Log:
[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

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/trunk/test/Transforms/InstCombine/addsub-constant-folding.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=362217&r1=362216&r2=362217&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Fri May 31 02:47:16 2019
@@ -1609,8 +1609,13 @@ Instruction *InstCombiner::visitSub(Bina
       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);
   }

Modified: llvm/trunk/test/Transforms/InstCombine/addsub-constant-folding.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/addsub-constant-folding.ll?rev=362217&r1=362216&r2=362217&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/addsub-constant-folding.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/addsub-constant-folding.ll Fri May 31 02:47:16 2019
@@ -485,7 +485,7 @@ define i32 @const_sub_const_sub_extrause
 ; 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_su
 ; 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




More information about the llvm-commits mailing list