[llvm] 002da67 - [InstCombine] Require ImmConstant in shift of shift fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 05:56:14 PST 2023


Author: Nikita Popov
Date: 2023-11-13T14:56:06+01:00
New Revision: 002da67d01b230c5a358d5d8b10841fa04ac5e4a

URL: https://github.com/llvm/llvm-project/commit/002da67d01b230c5a358d5d8b10841fa04ac5e4a
DIFF: https://github.com/llvm/llvm-project/commit/002da67d01b230c5a358d5d8b10841fa04ac5e4a.diff

LOG: [InstCombine] Require ImmConstant in shift of shift fold

This fixes an infinite loop reported at:
https://github.com/llvm/llvm-project/commit/82f68a992b9f89036042d57a5f6345cb2925b2c1#commitcomment-132406739

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
    llvm/test/Transforms/InstCombine/shift-shift.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 1aac93731474291..f3f4183efbc7895 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -760,7 +760,7 @@ Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1,
   // (C2 >> X) >> C1 --> (C2 >> C1) >> X
   Constant *C2;
   Value *X;
-  if (match(Op0, m_BinOp(I.getOpcode(), m_Constant(C2), m_Value(X))))
+  if (match(Op0, m_BinOp(I.getOpcode(), m_ImmConstant(C2), m_Value(X))))
     return BinaryOperator::Create(
         I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), C2, C1), X);
 

diff  --git a/llvm/test/Transforms/InstCombine/shift-shift.ll b/llvm/test/Transforms/InstCombine/shift-shift.ll
index 4dc287d3979bba0..4270a2c2ba205ed 100644
--- a/llvm/test/Transforms/InstCombine/shift-shift.ll
+++ b/llvm/test/Transforms/InstCombine/shift-shift.ll
@@ -732,3 +732,15 @@ define <2 x i8> @lshr_shl_demand5_nonuniform_vec_both(<2 x i8> %x) {
   %r = and <2 x i8> %shl, <i8 -4, i8 -16>
   ret <2 x i8> %r
 }
+
+ at g = external global i8, align 8
+
+define i64 @ashr_ashr_constexpr() {
+; CHECK-LABEL: @ashr_ashr_constexpr(
+; CHECK-NEXT:    [[SHR2:%.*]] = ashr exact i64 ptrtoint (ptr @g to i64), 3
+; CHECK-NEXT:    ret i64 [[SHR2]]
+;
+  %shr = ashr i64 ptrtoint (ptr @g to i64), 1
+  %shr2 = ashr i64 %shr, 2
+  ret i64 %shr2
+}


        


More information about the llvm-commits mailing list