[llvm] 5d67d81 - [InstCombine] prevent crashing/assert on shift constant expression (PR44028)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 17 14:32:28 PST 2019


Author: Sanjay Patel
Date: 2019-11-17T17:31:09-05:00
New Revision: 5d67d81f484f935b709918ad99462e32efa3b17a

URL: https://github.com/llvm/llvm-project/commit/5d67d81f484f935b709918ad99462e32efa3b17a
DIFF: https://github.com/llvm/llvm-project/commit/5d67d81f484f935b709918ad99462e32efa3b17a.diff

LOG: [InstCombine] prevent crashing/assert on shift constant expression (PR44028)

The binary operator cast implies an instruction, but the matcher for shift does not:
https://bugs.llvm.org/show_bug.cgi?id=44028

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index f35a6ce2d937..fbff5dd4a8cd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -319,7 +319,8 @@ static Instruction *foldShiftOfShiftedLogic(BinaryOperator &I,
   // TODO: Remove the one-use check if the other logic operand (Y) is constant.
   Value *X, *Y;
   auto matchFirstShift = [&](Value *V) {
-    return match(V, m_OneUse(m_Shift(m_Value(X), m_APInt(C0)))) &&
+    return !isa<ConstantExpr>(V) &&
+           match(V, m_OneUse(m_Shift(m_Value(X), m_APInt(C0)))) &&
            cast<BinaryOperator>(V)->getOpcode() == ShiftOpcode &&
            (*C0 + *C1).ult(Ty->getScalarSizeInBits());
   };

diff  --git a/llvm/test/Transforms/InstCombine/shift-logic.ll b/llvm/test/Transforms/InstCombine/shift-logic.ll
index 453463f46fc4..d0d06a387271 100644
--- a/llvm/test/Transforms/InstCombine/shift-logic.ll
+++ b/llvm/test/Transforms/InstCombine/shift-logic.ll
@@ -169,3 +169,20 @@ define i32 @lshr_or_extra_use(i32 %x, i32 %y, i32* %p) {
   %sh1 = lshr i32 %r, 7
   ret i32 %sh1
 }
+
+; Avoid crashing on constant expressions.
+
+ at g = external global i32
+
+define i32 @PR44028(i32 %x) {
+; CHECK-LABEL: @PR44028(
+; CHECK-NEXT:    [[SH1:%.*]] = ashr exact i32 [[X:%.*]], 16
+; CHECK-NEXT:    [[T0:%.*]] = xor i32 [[SH1]], shl (i32 ptrtoint (i32* @g to i32), i32 16)
+; CHECK-NEXT:    [[T27:%.*]] = ashr exact i32 [[T0]], 16
+; CHECK-NEXT:    ret i32 [[T27]]
+;
+  %sh1 = ashr exact i32 %x, 16
+  %t0 = xor i32 %sh1, shl (i32 ptrtoint (i32* @g to i32), i32 16)
+  %t27 = ashr exact i32 %t0, 16
+  ret i32 %t27
+}


        


More information about the llvm-commits mailing list