Skip to content

Commit 358cdb4

Browse files
committed
Revert "[ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate"
Appears to be causing out-of-tree test failures. Reverting while the issue is investigated. This reverts commit fbc7fcf.
1 parent d4db528 commit 358cdb4

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6035,28 +6035,29 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
60356035
case Instruction::UDiv:
60366036
case Instruction::URem: {
60376037
// x / y is undefined if y == 0.
6038-
const DataLayout &DL = Inst->getModule()->getDataLayout();
6039-
return isKnownNonZero(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
6038+
const APInt *V;
6039+
if (match(Inst->getOperand(1), m_APInt(V)))
6040+
return *V != 0;
6041+
return false;
60406042
}
60416043
case Instruction::SDiv:
60426044
case Instruction::SRem: {
60436045
// x / y is undefined if y == 0 or x == INT_MIN and y == -1
6044-
const DataLayout &DL = Inst->getModule()->getDataLayout();
6045-
KnownBits KnownDenominator =
6046-
computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
6046+
const APInt *Numerator, *Denominator;
6047+
if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6048+
return false;
60476049
// We cannot hoist this division if the denominator is 0.
6048-
if (!KnownDenominator.isNonZero())
6050+
if (*Denominator == 0)
60496051
return false;
6050-
60516052
// It's safe to hoist if the denominator is not 0 or -1.
6052-
if (!KnownDenominator.Zero.isZero())
6053+
if (!Denominator->isAllOnes())
60536054
return true;
6054-
6055-
// At this point denominator may be -1. It is safe to hoist as
6055+
// At this point we know that the denominator is -1. It is safe to hoist as
60566056
// long we know that the numerator is not INT_MIN.
6057-
KnownBits KnownNumerator =
6058-
computeKnownBits(Inst->getOperand(0), DL, /*Depth*/ 0, AC, CtxI, DT);
6059-
return !KnownNumerator.getSignedMinValue().isMinSignedValue();
6057+
if (match(Inst->getOperand(0), m_APInt(Numerator)))
6058+
return !Numerator->isMinSignedValue();
6059+
// The numerator *might* be MinSignedValue.
6060+
return false;
60606061
}
60616062
case Instruction::Load: {
60626063
const LoadInst *LI = dyn_cast<LoadInst>(Inst);

llvm/test/Transforms/LICM/speculate-div.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ define void @sdiv_ok(i16 %n, i16 %xx) {
5151
; CHECK-NEXT: entry:
5252
; CHECK-NEXT: [[XO:%.*]] = or i16 [[XX:%.*]], 1
5353
; CHECK-NEXT: [[X:%.*]] = and i16 [[XO]], 123
54-
; CHECK-NEXT: [[DIV:%.*]] = sdiv i16 [[N:%.*]], [[X]]
5554
; CHECK-NEXT: br label [[LOOP:%.*]]
5655
; CHECK: loop:
5756
; CHECK-NEXT: call void @maythrow()
57+
; CHECK-NEXT: [[DIV:%.*]] = sdiv i16 [[N:%.*]], [[X]]
5858
; CHECK-NEXT: call void @use(i16 [[DIV]])
5959
; CHECK-NEXT: br label [[LOOP]]
6060
;
@@ -74,10 +74,10 @@ define void @srem_ok2(i16 %nn, i16 %xx) {
7474
; CHECK-NEXT: entry:
7575
; CHECK-NEXT: [[N:%.*]] = and i16 [[NN:%.*]], 123
7676
; CHECK-NEXT: [[X:%.*]] = or i16 [[XX:%.*]], 1
77-
; CHECK-NEXT: [[DIV:%.*]] = srem i16 [[N]], [[X]]
7877
; CHECK-NEXT: br label [[LOOP:%.*]]
7978
; CHECK: loop:
8079
; CHECK-NEXT: call void @maythrow()
80+
; CHECK-NEXT: [[DIV:%.*]] = srem i16 [[N]], [[X]]
8181
; CHECK-NEXT: call void @use(i16 [[DIV]])
8282
; CHECK-NEXT: br label [[LOOP]]
8383
;
@@ -117,10 +117,10 @@ define void @udiv_ok(i16 %n, i16 %xx) {
117117
; CHECK-LABEL: @udiv_ok(
118118
; CHECK-NEXT: entry:
119119
; CHECK-NEXT: [[X:%.*]] = or i16 [[XX:%.*]], 1
120-
; CHECK-NEXT: [[DIV:%.*]] = udiv i16 [[N:%.*]], [[X]]
121120
; CHECK-NEXT: br label [[LOOP:%.*]]
122121
; CHECK: loop:
123122
; CHECK-NEXT: call void @maythrow()
123+
; CHECK-NEXT: [[DIV:%.*]] = udiv i16 [[N:%.*]], [[X]]
124124
; CHECK-NEXT: call void @use(i16 [[DIV]])
125125
; CHECK-NEXT: br label [[LOOP]]
126126
;

0 commit comments

Comments
 (0)