Skip to content

Commit 967e84e

Browse files
committed
[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.
1 parent 4b7f145 commit 967e84e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,11 @@ static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
823823
if (Instr->getType()->isVectorTy())
824824
return false;
825825

826-
ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0));
827-
ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1));
826+
ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0),
827+
/*UndefAllowed*/ false);
828+
// Allow undef for RHS, as we can assume it is division by zero UB.
829+
ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1),
830+
/*UndefAllowed*/ true);
828831
if (expandUDivOrURem(Instr, XCR, YCR))
829832
return true;
830833

@@ -951,8 +954,11 @@ static bool processSDivOrSRem(BinaryOperator *Instr, LazyValueInfo *LVI) {
951954
if (Instr->getType()->isVectorTy())
952955
return false;
953956

954-
ConstantRange LCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0));
955-
ConstantRange RCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1));
957+
ConstantRange LCR =
958+
LVI->getConstantRangeAtUse(Instr->getOperandUse(0), /*AllowUndef*/ false);
959+
// Allow undef for RHS, as we can assume it is division by zero UB.
960+
ConstantRange RCR =
961+
LVI->getConstantRangeAtUse(Instr->getOperandUse(1), /*AlloweUndef*/ true);
956962
if (Instr->getOpcode() == Instruction::SDiv)
957963
if (processSDiv(Instr, LCR, RCR, LVI))
958964
return true;

llvm/test/Transforms/CorrelatedValuePropagation/urem.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ else:
394394
ret void
395395
}
396396

397-
; FIXME: This is a miscompile.
398397
define i8 @urem_undef_range_op1(i8 %x) {
399398
; CHECK-LABEL: @urem_undef_range_op1(
400399
; CHECK-NEXT: entry:
@@ -408,7 +407,8 @@ define i8 @urem_undef_range_op1(i8 %x) {
408407
; CHECK-NEXT: br label [[JOIN]]
409408
; CHECK: join:
410409
; CHECK-NEXT: [[PHI:%.*]] = phi i8 [ 1, [[CASE1]] ], [ 2, [[CASE2]] ], [ undef, [[ENTRY:%.*]] ]
411-
; CHECK-NEXT: ret i8 [[PHI]]
410+
; CHECK-NEXT: [[RES:%.*]] = urem i8 [[PHI]], 3
411+
; CHECK-NEXT: ret i8 [[RES]]
412412
;
413413
entry:
414414
switch i8 %x, label %join [

0 commit comments

Comments
 (0)