[llvm] 93d1d94 - [InstCombine] Restrict "C-(X+C2) --> (C-C2)-X" fold to immediate constants

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 7 02:08:14 PDT 2021


Author: Roman Lebedev
Date: 2021-04-07T12:06:24+03:00
New Revision: 93d1d94b745b940c9ad0adf20322eb16ed624ef3

URL: https://github.com/llvm/llvm-project/commit/93d1d94b745b940c9ad0adf20322eb16ed624ef3
DIFF: https://github.com/llvm/llvm-project/commit/93d1d94b745b940c9ad0adf20322eb16ed624ef3.diff

LOG: [InstCombine] Restrict "C-(X+C2) --> (C-C2)-X" fold to immediate constants

I.e., if any/all of the consants is an expression, don't do it.
Since those constants won't reduce into an immediate,
but would be left as an constant expression, they could cause
endless combine loops after 31d219d2997fed1b7dc97e0adf170d5aaf65883e
added an inverse transformation.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/sub-from-sub.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index bacb8689892a..98cd7af4bf0c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1726,12 +1726,13 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
   if (Instruction *R = factorizeMathWithShlOps(I, Builder))
     return R;
 
-  if (Constant *C = dyn_cast<Constant>(Op0)) {
+  Constant *C;
+  if (match(Op0, m_ImmConstant(C))) {
     Value *X;
     Constant *C2;
 
     // C-(X+C2) --> (C-C2)-X
-    if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
+    if (match(Op1, m_Add(m_Value(X), m_ImmConstant(C2))))
       return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
   }
 

diff  --git a/llvm/test/Transforms/InstCombine/sub-from-sub.ll b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
index 0d5885a740f0..4d5ba0ea4dbc 100644
--- a/llvm/test/Transforms/InstCombine/sub-from-sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
@@ -174,7 +174,8 @@ define i8 @t12_c1_c2_exrause(i8 %x, i8 %z) {
 @g1 = external global i8, align 1
 define i32 @constantexpr0(i32 %x, i8* %y) unnamed_addr {
 ; CHECK-LABEL: @constantexpr0(
-; CHECK-NEXT:    [[R:%.*]] = sub i32 sub (i32 0, i32 ptrtoint (i8* @g0 to i32)), [[X:%.*]]
+; CHECK-NEXT:    [[I0:%.*]] = add i32 [[X:%.*]], ptrtoint (i8* @g0 to i32)
+; CHECK-NEXT:    [[R:%.*]] = sub i32 0, [[I0]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %i0 = add i32 %x, ptrtoint (i8* @g0 to i32)
@@ -183,7 +184,8 @@ define i32 @constantexpr0(i32 %x, i8* %y) unnamed_addr {
 }
 define i32 @constantexpr1(i32 %x, i8* %y) unnamed_addr {
 ; CHECK-LABEL: @constantexpr1(
-; CHECK-NEXT:    [[R:%.*]] = sub i32 sub (i32 ptrtoint (i8* @g1 to i32), i32 42), [[X:%.*]]
+; CHECK-NEXT:    [[I0:%.*]] = add i32 [[X:%.*]], 42
+; CHECK-NEXT:    [[R:%.*]] = sub i32 ptrtoint (i8* @g1 to i32), [[I0]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %i0 = add i32 %x, 42
@@ -192,7 +194,8 @@ define i32 @constantexpr1(i32 %x, i8* %y) unnamed_addr {
 }
 define i32 @constantexpr2(i32 %x, i8* %y) unnamed_addr {
 ; CHECK-LABEL: @constantexpr2(
-; CHECK-NEXT:    [[R:%.*]] = sub i32 sub (i32 ptrtoint (i8* @g1 to i32), i32 ptrtoint (i8* @g0 to i32)), [[X:%.*]]
+; CHECK-NEXT:    [[I0:%.*]] = add i32 [[X:%.*]], ptrtoint (i8* @g0 to i32)
+; CHECK-NEXT:    [[R:%.*]] = sub i32 ptrtoint (i8* @g1 to i32), [[I0]]
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %i0 = add i32 %x, ptrtoint (i8* @g0 to i32)


        


More information about the llvm-commits mailing list