Skip to content

Commit fbc7fcf

Browse files
committed
[ValueTracking] Use knownbits interface for determining if div/rem are safe to speculate
This just replaces the exact constant requirements with known-bits which can prove better results. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D149423
1 parent 824e9bb commit fbc7fcf

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6035,29 +6035,28 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
60356035
case Instruction::UDiv:
60366036
case Instruction::URem: {
60376037
// x / y is undefined if y == 0.
6038-
const APInt *V;
6039-
if (match(Inst->getOperand(1), m_APInt(V)))
6040-
return *V != 0;
6041-
return false;
6038+
const DataLayout &DL = Inst->getModule()->getDataLayout();
6039+
return isKnownNonZero(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
60426040
}
60436041
case Instruction::SDiv:
60446042
case Instruction::SRem: {
60456043
// x / y is undefined if y == 0 or x == INT_MIN and y == -1
6046-
const APInt *Numerator, *Denominator;
6047-
if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6048-
return false;
6044+
const DataLayout &DL = Inst->getModule()->getDataLayout();
6045+
KnownBits KnownDenominator =
6046+
computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
60496047
// We cannot hoist this division if the denominator is 0.
6050-
if (*Denominator == 0)
6048+
if (!KnownDenominator.isNonZero())
60516049
return false;
6050+
60526051
// It's safe to hoist if the denominator is not 0 or -1.
6053-
if (!Denominator->isAllOnes())
6052+
if (!KnownDenominator.Zero.isZero())
60546053
return true;
6055-
// At this point we know that the denominator is -1. It is safe to hoist as
6054+
6055+
// At this point denominator may be -1. It is safe to hoist as
60566056
// long we know that the numerator is not INT_MIN.
6057-
if (match(Inst->getOperand(0), m_APInt(Numerator)))
6058-
return !Numerator->isMinSignedValue();
6059-
// The numerator *might* be MinSignedValue.
6060-
return false;
6057+
KnownBits KnownNumerator =
6058+
computeKnownBits(Inst->getOperand(0), DL, /*Depth*/ 0, AC, CtxI, DT);
6059+
return !KnownNumerator.getSignedMinValue().isMinSignedValue();
60616060
}
60626061
case Instruction::Load: {
60636062
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]]
5455
; CHECK-NEXT: br label [[LOOP:%.*]]
5556
; CHECK: loop:
5657
; 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]]
7778
; CHECK-NEXT: br label [[LOOP:%.*]]
7879
; CHECK: loop:
7980
; 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]]
120121
; CHECK-NEXT: br label [[LOOP:%.*]]
121122
; CHECK: loop:
122123
; 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)