[PATCH] D126546: decomposeSimpleLinearExpr should bail out on negative operands.

wael yehia via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 27 07:59:42 PDT 2022


w2yehia created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
w2yehia requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

https://reviews.llvm.org/D126546

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  llvm/test/Transforms/InstCombine/neg-alloca.ll


Index: llvm/test/Transforms/InstCombine/neg-alloca.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/neg-alloca.ll
@@ -0,0 +1,29 @@
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+ at test = external global [1 x i32], align 16
+
+define void @foo() {
+  br label %b0
+
+b0:
+  %1 = phi i64 [ 1, %0 ], [ %9, %b0 ]
+; Currently we cannot handle expressions of the form Offset - X * Scale.
+; CHECK: mul nsw i64 %1, -4
+; CHECK: alloca i8
+  %2 = mul nsw i64 %1, -4
+  %3 = add nsw i64 %2, 24
+  %4 = alloca i8, i64 %3, align 4
+  %5 = bitcast i8* %4 to i32*
+  store i32 undef, i32* %5, align 4
+
+  %6 = load i8, i8* %4, align 4
+  %7 = getelementptr inbounds [1 x i32], [1 x i32]* @test, i64 0, i8 %6
+  %8 = load i32, i32* %7, align 4
+  store i32 %8, i32* undef, align 4
+
+  %9 = add nuw nsw i64 %1, 1
+  br i1 undef, label %bend, label %b0
+
+bend:
+  ret void
+}
+
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -43,7 +43,8 @@
       return Val;
     }
 
-    if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
+    ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1));
+    if (RHS && !RHS->isNegative()) {
       if (I->getOpcode() == Instruction::Shl) {
         // This is a value scaled by '1 << the shift amt'.
         Scale = UINT64_C(1) << RHS->getZExtValue();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126546.432561.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220527/1beb29a5/attachment.bin>


More information about the llvm-commits mailing list