[llvm] 967e84e - [CVP] Don't use undef range for LHS of div/rem transforms

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 12 03:06:28 PST 2023


Author: Nikita Popov
Date: 2023-12-12T12:06:19+01:00
New Revision: 967e84eee3076bc6f306c70d07e3a922e3c1c3a6

URL: https://github.com/llvm/llvm-project/commit/967e84eee3076bc6f306c70d07e3a922e3c1c3a6
DIFF: https://github.com/llvm/llvm-project/commit/967e84eee3076bc6f306c70d07e3a922e3c1c3a6.diff

LOG: [CVP] Don't use undef range for LHS of div/rem transforms

Using it for RHS is fine, as undef is UB in that case.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
    llvm/test/Transforms/CorrelatedValuePropagation/urem.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 8bc05be97dae1..e6d8f89122ff6 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -823,8 +823,11 @@ static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
   if (Instr->getType()->isVectorTy())
     return false;
 
-  ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0));
-  ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1));
+  ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0),
+                                                 /*UndefAllowed*/ false);
+  // Allow undef for RHS, as we can assume it is division by zero UB.
+  ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1),
+                                                 /*UndefAllowed*/ true);
   if (expandUDivOrURem(Instr, XCR, YCR))
     return true;
 
@@ -951,8 +954,11 @@ static bool processSDivOrSRem(BinaryOperator *Instr, LazyValueInfo *LVI) {
   if (Instr->getType()->isVectorTy())
     return false;
 
-  ConstantRange LCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0));
-  ConstantRange RCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1));
+  ConstantRange LCR =
+      LVI->getConstantRangeAtUse(Instr->getOperandUse(0), /*AllowUndef*/ false);
+  // Allow undef for RHS, as we can assume it is division by zero UB.
+  ConstantRange RCR =
+      LVI->getConstantRangeAtUse(Instr->getOperandUse(1), /*AlloweUndef*/ true);
   if (Instr->getOpcode() == Instruction::SDiv)
     if (processSDiv(Instr, LCR, RCR, LVI))
       return true;

diff  --git a/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll b/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
index 9eed770e665d2..ec6461e29f191 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
@@ -394,7 +394,6 @@ else:
   ret void
 }
 
-; FIXME: This is a miscompile.
 define i8 @urem_undef_range_op1(i8 %x) {
 ; CHECK-LABEL: @urem_undef_range_op1(
 ; CHECK-NEXT:  entry:
@@ -408,7 +407,8 @@ define i8 @urem_undef_range_op1(i8 %x) {
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
 ; CHECK-NEXT:    [[PHI:%.*]] = phi i8 [ 1, [[CASE1]] ], [ 2, [[CASE2]] ], [ undef, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    ret i8 [[PHI]]
+; CHECK-NEXT:    [[RES:%.*]] = urem i8 [[PHI]], 3
+; CHECK-NEXT:    ret i8 [[RES]]
 ;
 entry:
   switch i8 %x, label %join [


        


More information about the llvm-commits mailing list