Skip to content

Commit 4b3ea33

Browse files
committed
[ValueTracking] Convert isKnownNonNegative() to use SimplifyQuery (NFC)
1 parent 10d6d5f commit 4b3ea33

File tree

6 files changed

+14
-19
lines changed

6 files changed

+14
-19
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,8 @@ bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0,
138138
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false);
139139

140140
/// Returns true if the give value is known to be non-negative.
141-
bool isKnownNonNegative(const Value *V, const DataLayout &DL,
142-
unsigned Depth = 0, AssumptionCache *AC = nullptr,
143-
const Instruction *CxtI = nullptr,
144-
const DominatorTree *DT = nullptr,
145-
bool UseInstrInfo = true);
141+
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
142+
unsigned Depth = 0);
146143

147144
/// Returns true if the given value is known be positive (i.e. non-negative
148145
/// and non-zero).

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,9 @@ bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
276276
V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
277277
}
278278

279-
bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
280-
unsigned Depth, AssumptionCache *AC,
281-
const Instruction *CxtI, const DominatorTree *DT,
282-
bool UseInstrInfo) {
283-
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
284-
return Known.isNonNegative();
279+
bool llvm::isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
280+
unsigned Depth) {
281+
return computeKnownBits(V, Depth, SQ).isNonNegative();
285282
}
286283

287284
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,
292289

293290
// TODO: We'd doing two recursive queries here. We should factor this such
294291
// that only a single query is needed.
295-
return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
292+
return isKnownNonNegative(V, SimplifyQuery(DL, DT, AC, CxtI, UseInstrInfo),
293+
Depth) &&
296294
isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
297295
}
298296

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
12311231
return &Zext;
12321232
}
12331233

1234-
if (isKnownNonNegative(Src, DL, 0, &AC, &Zext, &DT)) {
1234+
if (isKnownNonNegative(Src, SQ.getWithInstruction(&Zext))) {
12351235
Zext.setNonNeg();
12361236
return &Zext;
12371237
}
@@ -1389,7 +1389,7 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
13891389
unsigned DestBitSize = DestTy->getScalarSizeInBits();
13901390

13911391
// If the value being extended is zero or positive, use a zext instead.
1392-
if (isKnownNonNegative(Src, DL, 0, &AC, &Sext, &DT)) {
1392+
if (isKnownNonNegative(Src, SQ.getWithInstruction(&Sext))) {
13931393
auto CI = CastInst::Create(Instruction::ZExt, Src, DestTy);
13941394
CI->setNonNeg(true);
13951395
return CI;

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
15181518

15191519
if (KnownDividend.isNonNegative()) {
15201520
// If both operands are unsigned, turn this into a udiv.
1521-
if (isKnownNonNegative(Op1, DL, 0, &AC, &I, &DT)) {
1521+
if (isKnownNonNegative(Op1, SQ.getWithInstruction(&I))) {
15221522
auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
15231523
BO->setIsExact(I.isExact());
15241524
return BO;

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ static bool hoistGEP(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
24932493
// handle both offsets being non-negative.
24942494
const DataLayout &DL = GEP->getModule()->getDataLayout();
24952495
auto NonNegative = [&](Value *V) {
2496-
return isKnownNonNegative(V, DL, 0, AC, GEP, DT);
2496+
return isKnownNonNegative(V, SimplifyQuery(DL, DT, AC, GEP));
24972497
};
24982498
bool IsInBounds = Src->isInBounds() && GEP->isInBounds() &&
24992499
all_of(Src->indices(), NonNegative) &&

llvm/lib/Transforms/Scalar/NaryReassociate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,20 @@ bool NaryReassociatePass::requiresSignExtension(Value *Index,
359359
GetElementPtrInst *
360360
NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
361361
unsigned I, Type *IndexedType) {
362+
SimplifyQuery SQ(*DL, DT, AC, GEP);
362363
Value *IndexToSplit = GEP->getOperand(I + 1);
363364
if (SExtInst *SExt = dyn_cast<SExtInst>(IndexToSplit)) {
364365
IndexToSplit = SExt->getOperand(0);
365366
} else if (ZExtInst *ZExt = dyn_cast<ZExtInst>(IndexToSplit)) {
366367
// zext can be treated as sext if the source is non-negative.
367-
if (isKnownNonNegative(ZExt->getOperand(0), *DL, 0, AC, GEP, DT))
368+
if (isKnownNonNegative(ZExt->getOperand(0), SQ))
368369
IndexToSplit = ZExt->getOperand(0);
369370
}
370371

371372
if (AddOperator *AO = dyn_cast<AddOperator>(IndexToSplit)) {
372373
// If the I-th index needs sext and the underlying add is not equipped with
373374
// nsw, we cannot split the add because
374375
// sext(LHS + RHS) != sext(LHS) + sext(RHS).
375-
SimplifyQuery SQ(*DL, DT, AC, GEP);
376376
if (requiresSignExtension(IndexToSplit, GEP) &&
377377
computeOverflowForSignedAdd(AO, SQ) != OverflowResult::NeverOverflows)
378378
return nullptr;
@@ -402,7 +402,7 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
402402
IndexExprs.push_back(SE->getSCEV(Index));
403403
// Replace the I-th index with LHS.
404404
IndexExprs[I] = SE->getSCEV(LHS);
405-
if (isKnownNonNegative(LHS, *DL, 0, AC, GEP, DT) &&
405+
if (isKnownNonNegative(LHS, SimplifyQuery(*DL, DT, AC, GEP)) &&
406406
DL->getTypeSizeInBits(LHS->getType()).getFixedValue() <
407407
DL->getTypeSizeInBits(GEP->getOperand(I)->getType())
408408
.getFixedValue()) {

0 commit comments

Comments
 (0)