[llvm] 4b3ea33 - [ValueTracking] Convert isKnownNonNegative() to use SimplifyQuery (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 29 01:53:01 PST 2023
Author: Nikita Popov
Date: 2023-11-29T10:52:52+01:00
New Revision: 4b3ea337ad188d5c3ff05bf64cc61c3355e66376
URL: https://github.com/llvm/llvm-project/commit/4b3ea337ad188d5c3ff05bf64cc61c3355e66376
DIFF: https://github.com/llvm/llvm-project/commit/4b3ea337ad188d5c3ff05bf64cc61c3355e66376.diff
LOG: [ValueTracking] Convert isKnownNonNegative() to use SimplifyQuery (NFC)
Added:
Modified:
llvm/include/llvm/Analysis/ValueTracking.h
llvm/lib/Analysis/ValueTracking.cpp
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/lib/Transforms/Scalar/LICM.cpp
llvm/lib/Transforms/Scalar/NaryReassociate.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 0c5b62499b253e7..4670f42db0c3b34 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -138,11 +138,8 @@ bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0,
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false);
/// Returns true if the give value is known to be non-negative.
-bool isKnownNonNegative(const Value *V, const DataLayout &DL,
- unsigned Depth = 0, AssumptionCache *AC = nullptr,
- const Instruction *CxtI = nullptr,
- const DominatorTree *DT = nullptr,
- bool UseInstrInfo = true);
+bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth = 0);
/// Returns true if the given value is known be positive (i.e. non-negative
/// and non-zero).
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index ccb5ae3ba0a11dd..1bd6f06a49c973b 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -276,12 +276,9 @@ bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
}
-bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
- unsigned Depth, AssumptionCache *AC,
- const Instruction *CxtI, const DominatorTree *DT,
- bool UseInstrInfo) {
- KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
- return Known.isNonNegative();
+bool llvm::isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
+ unsigned Depth) {
+ return computeKnownBits(V, Depth, SQ).isNonNegative();
}
bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
@@ -292,7 +289,8 @@ bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
// TODO: We'd doing two recursive queries here. We should factor this such
// that only a single query is needed.
- return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
+ return isKnownNonNegative(V, SimplifyQuery(DL, DT, AC, CxtI, UseInstrInfo),
+ Depth) &&
isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index d2c6ffe9001d78b..6629ca840a67c01 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1231,7 +1231,7 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
return &Zext;
}
- if (isKnownNonNegative(Src, DL, 0, &AC, &Zext, &DT)) {
+ if (isKnownNonNegative(Src, SQ.getWithInstruction(&Zext))) {
Zext.setNonNeg();
return &Zext;
}
@@ -1389,7 +1389,7 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
unsigned DestBitSize = DestTy->getScalarSizeInBits();
// If the value being extended is zero or positive, use a zext instead.
- if (isKnownNonNegative(Src, DL, 0, &AC, &Sext, &DT)) {
+ if (isKnownNonNegative(Src, SQ.getWithInstruction(&Sext))) {
auto CI = CastInst::Create(Instruction::ZExt, Src, DestTy);
CI->setNonNeg(true);
return CI;
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 40156726c7038b9..3b45138a8bebb06 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1518,7 +1518,7 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
if (KnownDividend.isNonNegative()) {
// If both operands are unsigned, turn this into a udiv.
- if (isKnownNonNegative(Op1, DL, 0, &AC, &I, &DT)) {
+ if (isKnownNonNegative(Op1, SQ.getWithInstruction(&I))) {
auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
BO->setIsExact(I.isExact());
return BO;
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 35a9e3c9b6000fa..a49ba6980d57d2c 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -2493,7 +2493,7 @@ static bool hoistGEP(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
// handle both offsets being non-negative.
const DataLayout &DL = GEP->getModule()->getDataLayout();
auto NonNegative = [&](Value *V) {
- return isKnownNonNegative(V, DL, 0, AC, GEP, DT);
+ return isKnownNonNegative(V, SimplifyQuery(DL, DT, AC, GEP));
};
bool IsInBounds = Src->isInBounds() && GEP->isInBounds() &&
all_of(Src->indices(), NonNegative) &&
diff --git a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
index 021b03e42403cd7..7fe1a222021ebee 100644
--- a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
@@ -359,12 +359,13 @@ bool NaryReassociatePass::requiresSignExtension(Value *Index,
GetElementPtrInst *
NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
unsigned I, Type *IndexedType) {
+ SimplifyQuery SQ(*DL, DT, AC, GEP);
Value *IndexToSplit = GEP->getOperand(I + 1);
if (SExtInst *SExt = dyn_cast<SExtInst>(IndexToSplit)) {
IndexToSplit = SExt->getOperand(0);
} else if (ZExtInst *ZExt = dyn_cast<ZExtInst>(IndexToSplit)) {
// zext can be treated as sext if the source is non-negative.
- if (isKnownNonNegative(ZExt->getOperand(0), *DL, 0, AC, GEP, DT))
+ if (isKnownNonNegative(ZExt->getOperand(0), SQ))
IndexToSplit = ZExt->getOperand(0);
}
@@ -372,7 +373,6 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
// If the I-th index needs sext and the underlying add is not equipped with
// nsw, we cannot split the add because
// sext(LHS + RHS) != sext(LHS) + sext(RHS).
- SimplifyQuery SQ(*DL, DT, AC, GEP);
if (requiresSignExtension(IndexToSplit, GEP) &&
computeOverflowForSignedAdd(AO, SQ) != OverflowResult::NeverOverflows)
return nullptr;
@@ -402,7 +402,7 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
IndexExprs.push_back(SE->getSCEV(Index));
// Replace the I-th index with LHS.
IndexExprs[I] = SE->getSCEV(LHS);
- if (isKnownNonNegative(LHS, *DL, 0, AC, GEP, DT) &&
+ if (isKnownNonNegative(LHS, SimplifyQuery(*DL, DT, AC, GEP)) &&
DL->getTypeSizeInBits(LHS->getType()).getFixedValue() <
DL->getTypeSizeInBits(GEP->getOperand(I)->getType())
.getFixedValue()) {
More information about the llvm-commits
mailing list