[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 09:12:50 PDT 2022
w2yehia updated this revision to Diff 432578.
w2yehia added a comment.
InstCombine tries to rewrite
%prod = mul nsw i64 %X, Scale
%acc = add nsw i64 %prod, Offset
%0 = alloca i8, i64 %acc, align 4
%1 = bitcast i8* %0 to i32*
Use ( %1 )
into
%prod = mul nsw i64 %X, Scale/4
%acc = add nsw i64 %prod, Offset/4
%0 = alloca i32, i64 %acc, align 4
Use (%0)
But it assumes Scale is unsigned.
For now, bail out on negative operands to avoid an incorrect transformation.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D126546/new/
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.432578.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220527/032d39a9/attachment-0001.bin>
More information about the llvm-commits
mailing list