[llvm] 24f6747 - [InstCombine] foldAddWithConstant(): don't deal with non-immediate constants

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 7 09:50:51 PDT 2021


Author: Roman Lebedev
Date: 2021-04-07T19:50:19+03:00
New Revision: 24f67473dd1253f484e10fd5dfbed95489487b60

URL: https://github.com/llvm/llvm-project/commit/24f67473dd1253f484e10fd5dfbed95489487b60
DIFF: https://github.com/llvm/llvm-project/commit/24f67473dd1253f484e10fd5dfbed95489487b60.diff

LOG: [InstCombine] foldAddWithConstant(): don't deal with non-immediate constants

All of the code that handles general constant here (other than the more
restrictive APInt-dealing code) expects that it is an immediate,
because otherwise we won't actually fold the constants, and increase
instruction count. And it isn't obvious why we'd be okay with
increasing the number of constant expressions,
those still will have to be run..

But after 2829094a8e252d04f13aabdf6f416c42a06af695
this could also cause endless combine loops.
So actually properly restrict this code to immediates.

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 7b33e29c59de..70caf2fdf83e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -864,7 +864,7 @@ static Instruction *foldNoWrapAdd(BinaryOperator &Add,
 Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
   Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1);
   Constant *Op1C;
-  if (!match(Op1, m_Constant(Op1C)))
+  if (!match(Op1, m_ImmConstant(Op1C)))
     return nullptr;
 
   if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add))

diff  --git a/llvm/test/Transforms/InstCombine/sub-from-sub.ll b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
index 0669efe2dd0e..636b8518f08e 100644
--- a/llvm/test/Transforms/InstCombine/sub-from-sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
@@ -202,3 +202,14 @@ define i32 @constantexpr2(i32 %x, i8* %y) unnamed_addr {
   %r = sub i32 ptrtoint (i8* @g1 to i32), %i0
   ret i32 %r
 }
+
+define i64 @pr49870(i64 %x) {
+; CHECK-LABEL: @pr49870(
+; CHECK-NEXT:    [[I0:%.*]] = xor i64 [[X:%.*]], -1
+; CHECK-NEXT:    [[R:%.*]] = add i64 [[I0]], ptrtoint (i8* @g0 to i64)
+; CHECK-NEXT:    ret i64 [[R]]
+;
+  %i0 = xor i64 %x, -1
+  %r = add i64 %i0, ptrtoint (i8* @g0 to i64)
+  ret i64 %r
+}


        


More information about the llvm-commits mailing list