[llvm] [LICM] Drop NSW/NUW flags on reassociated integer Mul instructions (PR #85645)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 18 07:12:27 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Danila Malyutin (danilaml)

<details>
<summary>Changes</summary>

After reassociation NSW/NUW may no longer be valid.

Fixes #<!-- -->85448

---
Full diff: https://github.com/llvm/llvm-project/pull/85645.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/LICM.cpp (+4-1) 
- (added) llvm/test/Transforms/LICM/pr85448.ll (+29) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 40bc16f9b57516..53c6026bfb20df 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -2741,13 +2741,16 @@ static bool hoistMulAddAssociation(Instruction &I, Loop &L,
   IRBuilder<> Builder(Preheader->getTerminator());
   for (auto *U : Changes) {
     assert(L.isLoopInvariant(U->get()));
-    Instruction *Ins = cast<Instruction>(U->getUser());
+    BinaryOperator *Ins = cast<BinaryOperator>(U->getUser());
     Value *Mul;
     if (I.getType()->isIntOrIntVectorTy())
       Mul = Builder.CreateMul(U->get(), Factor, "factor.op.mul");
     else
       Mul = Builder.CreateFMulFMF(U->get(), Factor, Ins, "factor.op.fmul");
     U->set(Mul);
+    // We cannot guarantee the validity of NSW/NUW flags after reassociation.
+    Ins->setHasNoSignedWrap(false);
+    Ins->setHasNoUnsignedWrap(false);
   }
   I.replaceAllUsesWith(VariantOp);
   eraseInstruction(I, SafetyInfo, MSSAU);
diff --git a/llvm/test/Transforms/LICM/pr85448.ll b/llvm/test/Transforms/LICM/pr85448.ll
new file mode 100644
index 00000000000000..acff3766e7fca3
--- /dev/null
+++ b/llvm/test/Transforms/LICM/pr85448.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt -passes='loop-mssa(licm)' -S %s | FileCheck %s
+
+; Check that NSW/NUW flags are correctly dropped after reassociation
+
+define void @test(i32 %a) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: i32 [[A:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    [[VAL:%.*]] = phi i32 [ 1, [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[TMP0:%.*]] = or i32 [[VAL]], [[A]]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i32 [[TMP0]], -589824
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 null(i32 [[TMP1]])
+; CHECK-NEXT:    br label [[LOOP]]
+;
+entry:
+  br label %loop
+
+loop:                              ; preds = %loop, %entry
+  %val = phi i32 [ 1, %loop ], [ 0, %entry ]
+  %0 = or i32 %val, %a
+  %1 = mul nuw i32 %0, 65536
+  %sext = mul nsw i32 %1, -9
+  %2 = call i32 null(i32 %sext)
+  br label %loop
+}
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/85645


More information about the llvm-commits mailing list