[llvm] 2ad0cc9 - [DivRemPairs] Strip division's poison generating flag

Juneyoung Lee via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 18 13:01:09 PST 2023


Author: Juneyoung Lee
Date: 2023-02-18T21:00:55Z
New Revision: 2ad0cc9186d4952b3009e1b8c3c606f0b13ea38a

URL: https://github.com/llvm/llvm-project/commit/2ad0cc9186d4952b3009e1b8c3c606f0b13ea38a
DIFF: https://github.com/llvm/llvm-project/commit/2ad0cc9186d4952b3009e1b8c3c606f0b13ea38a.diff

LOG: [DivRemPairs] Strip division's poison generating flag

Given this transformation: X % Y -> X - (X / Y) * Y

This patch strips off the poison-generating flag of X / Y such as exact, because it may make the optimized form result poison whereas X % Y does not.

The issue was reported here: https://github.com/llvm/llvm-project/issues/60748

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D144333

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/DivRemPairs.cpp
    llvm/test/Transforms/DivRemPairs/AArch64/div-rem-pairs.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
index 303951643a0b8..2b06e8fe511a1 100644
--- a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
+++ b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
@@ -371,6 +371,10 @@ static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
       Mul->insertAfter(RemInst);
       Sub->insertAfter(Mul);
 
+      // If DivInst has the exact flag, remove it. Otherwise this optimization
+      // may replace a well-defined value 'X % Y' with poison.
+      DivInst->dropPoisonGeneratingFlags();
+
       // If X can be undef, X should be frozen first.
       // For example, let's assume that Y = 1 & X = undef:
       //   %div = sdiv undef, 1 // %div = undef

diff  --git a/llvm/test/Transforms/DivRemPairs/AArch64/div-rem-pairs.ll b/llvm/test/Transforms/DivRemPairs/AArch64/div-rem-pairs.ll
index f7e0eaa9c9a14..b787f218026ea 100644
--- a/llvm/test/Transforms/DivRemPairs/AArch64/div-rem-pairs.ll
+++ b/llvm/test/Transforms/DivRemPairs/AArch64/div-rem-pairs.ll
@@ -5,7 +5,7 @@ define i8 @f(ptr %p, i8 %x, i8 %y) {
 ; CHECK-LABEL: @f(
 ; CHECK-NEXT:    [[X_FROZEN:%.*]] = freeze i8 [[X:%.*]]
 ; CHECK-NEXT:    [[Y_FROZEN:%.*]] = freeze i8 [[Y:%.*]]
-; CHECK-NEXT:    [[XDIVY:%.*]] = udiv exact i8 [[X_FROZEN]], [[Y_FROZEN]]
+; CHECK-NEXT:    [[XDIVY:%.*]] = udiv i8 [[X_FROZEN]], [[Y_FROZEN]]
 ; CHECK-NEXT:    [[TMP1:%.*]] = mul i8 [[XDIVY]], [[Y_FROZEN]]
 ; CHECK-NEXT:    [[XMODY_DECOMPOSED:%.*]] = sub i8 [[X_FROZEN]], [[TMP1]]
 ; CHECK-NEXT:    store i8 [[XMODY_DECOMPOSED]], ptr [[P:%.*]], align 1


        


More information about the llvm-commits mailing list