Skip to content

Commit 2bbbf15

Browse files
committed
[ValueTracking] Expand isKnown{Negative,Positive} APIs; NFC
1) Add support for `DemandedElts`. 2) Add private API that also returns the already computed KnownBits.
1 parent 4956118 commit 2bbbf15

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,21 @@ bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
145145
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ,
146146
unsigned Depth = 0);
147147

148+
/// Returns true if the given value is known be positive (i.e. non-negative
149+
/// and non-zero) for DemandedElts.
150+
bool isKnownPositive(const Value *V, const APInt &DemandedElts,
151+
const SimplifyQuery &SQ, unsigned Depth = 0);
152+
148153
/// Returns true if the given value is known be negative (i.e. non-positive
149154
/// and non-zero).
150155
bool isKnownNegative(const Value *V, const SimplifyQuery &DL,
151156
unsigned Depth = 0);
152157

158+
/// Returns true if the given value is known be negative (i.e. non-positive
159+
/// and non-zero) for DemandedElts.
160+
bool isKnownNegative(const Value *V, const APInt &DemandedElts,
161+
const SimplifyQuery &DL, unsigned Depth = 0);
162+
153163
/// Return true if the given values are known to be non-equal when defined.
154164
/// Supports scalar integer types only.
155165
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL,

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,52 @@ bool llvm::isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,
289289
return computeKnownBits(V, Depth, SQ).isNonNegative();
290290
}
291291

292-
bool llvm::isKnownPositive(const Value *V, const SimplifyQuery &SQ,
293-
unsigned Depth) {
292+
static bool isKnownPositive(const Value *V, const APInt &DemandedElts,
293+
KnownBits &Known, const SimplifyQuery &SQ,
294+
unsigned Depth) {
294295
if (auto *CI = dyn_cast<ConstantInt>(V))
295296
return CI->getValue().isStrictlyPositive();
296297

297298
// If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
298299
// this updated.
299-
KnownBits Known = computeKnownBits(V, Depth, SQ);
300+
Known = computeKnownBits(V, DemandedElts, Depth, SQ);
300301
return Known.isNonNegative() &&
301-
(Known.isNonZero() || ::isKnownNonZero(V, Depth, SQ));
302+
(Known.isNonZero() || ::isKnownNonZero(V, DemandedElts, Depth, SQ));
303+
}
304+
305+
bool llvm::isKnownPositive(const Value *V, const APInt &DemandedElts,
306+
const SimplifyQuery &SQ, unsigned Depth) {
307+
KnownBits Known(getBitWidth(V->getType(), SQ.DL));
308+
return ::isKnownPositive(V, DemandedElts, Known, SQ, Depth);
309+
}
310+
311+
bool llvm::isKnownPositive(const Value *V, const SimplifyQuery &SQ,
312+
unsigned Depth) {
313+
auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
314+
APInt DemandedElts =
315+
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
316+
return isKnownPositive(V, DemandedElts, SQ, Depth);
317+
}
318+
319+
static bool isKnownNegative(const Value *V, const APInt &DemandedElts,
320+
KnownBits &Known, const SimplifyQuery &SQ,
321+
unsigned Depth) {
322+
Known = computeKnownBits(V, DemandedElts, Depth, SQ);
323+
return Known.isNegative();
324+
}
325+
326+
bool llvm::isKnownNegative(const Value *V, const APInt &DemandedElts,
327+
const SimplifyQuery &SQ, unsigned Depth) {
328+
KnownBits Known(getBitWidth(V->getType(), SQ.DL));
329+
return ::isKnownNegative(V, DemandedElts, Known, SQ, Depth);
302330
}
303331

304332
bool llvm::isKnownNegative(const Value *V, const SimplifyQuery &SQ,
305333
unsigned Depth) {
306-
return computeKnownBits(V, Depth, SQ).isNegative();
334+
auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
335+
APInt DemandedElts =
336+
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
337+
return isKnownNegative(V, DemandedElts, SQ, Depth);
307338
}
308339

309340
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,

0 commit comments

Comments
 (0)