aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ValueTracking.cpp
diff options
context:
space:
mode:
authorNoah Goldstein <goldstein.w.n@gmail.com>2023-04-30 10:16:55 -0500
committerNoah Goldstein <goldstein.w.n@gmail.com>2023-04-30 10:40:46 -0500
commitfbc7fcf5ae26fad7db3e7c861fc6447e02b39cf0 (patch)
treef2c0ccff37d04158038948b2f4376f994d38e8cf /llvm/lib/Analysis/ValueTracking.cpp
parent824e9bb2e33655734eb3e9169ba5c4964cc1861c (diff)
downloadllvm-fbc7fcf5ae26fad7db3e7c861fc6447e02b39cf0.zip
llvm-fbc7fcf5ae26fad7db3e7c861fc6447e02b39cf0.tar.gz
llvm-fbc7fcf5ae26fad7db3e7c861fc6447e02b39cf0.tar.bz2
[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
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp27
1 files changed, 13 insertions, 14 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index a5259ba7..9972a43 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -6035,29 +6035,28 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
case Instruction::UDiv:
case Instruction::URem: {
// x / y is undefined if y == 0.
- const APInt *V;
- if (match(Inst->getOperand(1), m_APInt(V)))
- return *V != 0;
- return false;
+ const DataLayout &DL = Inst->getModule()->getDataLayout();
+ return isKnownNonZero(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
}
case Instruction::SDiv:
case Instruction::SRem: {
// x / y is undefined if y == 0 or x == INT_MIN and y == -1
- const APInt *Numerator, *Denominator;
- if (!match(Inst->getOperand(1), m_APInt(Denominator)))
- return false;
+ const DataLayout &DL = Inst->getModule()->getDataLayout();
+ KnownBits KnownDenominator =
+ computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
// We cannot hoist this division if the denominator is 0.
- if (*Denominator == 0)
+ if (!KnownDenominator.isNonZero())
return false;
+
// It's safe to hoist if the denominator is not 0 or -1.
- if (!Denominator->isAllOnes())
+ if (!KnownDenominator.Zero.isZero())
return true;
- // At this point we know that the denominator is -1. It is safe to hoist as
+
+ // At this point denominator may be -1. It is safe to hoist as
// long we know that the numerator is not INT_MIN.
- if (match(Inst->getOperand(0), m_APInt(Numerator)))
- return !Numerator->isMinSignedValue();
- // The numerator *might* be MinSignedValue.
- return false;
+ KnownBits KnownNumerator =
+ computeKnownBits(Inst->getOperand(0), DL, /*Depth*/ 0, AC, CtxI, DT);
+ return !KnownNumerator.getSignedMinValue().isMinSignedValue();
}
case Instruction::Load: {
const LoadInst *LI = dyn_cast<LoadInst>(Inst);