Skip to content

Commit b40e4ce

Browse files
authored
[ValueTracking] Make Depth last default arg (NFC) (#142384)
Having a finite Depth (or recursion limit) for computeKnownBits is very limiting, but is currently a load-bearing necessity, as all KnownBits are recomputed on each call and there is no caching. As a prerequisite for an effort to remove the recursion limit altogether, either using a clever caching technique, or writing a easily-invalidable KnownBits analysis, make the Depth argument in APIs in ValueTracking uniformly the last argument with a default value. This would aid in removing the argument when the time comes, as many callers that currently pass 0 explicitly are now updated to omit the argument altogether.
1 parent cb4a407 commit b40e4ce

40 files changed

+860
-875
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,37 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
5454
/// same width as the vector element, and the bit is set only if it is true
5555
/// for all of the elements in the vector.
5656
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known,
57-
const DataLayout &DL, unsigned Depth = 0,
57+
const DataLayout &DL,
5858
AssumptionCache *AC = nullptr,
5959
const Instruction *CxtI = nullptr,
6060
const DominatorTree *DT = nullptr,
61-
bool UseInstrInfo = true);
61+
bool UseInstrInfo = true, unsigned Depth = 0);
6262

6363
/// Returns the known bits rather than passing by reference.
6464
LLVM_ABI KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
65-
unsigned Depth = 0,
6665
AssumptionCache *AC = nullptr,
6766
const Instruction *CxtI = nullptr,
6867
const DominatorTree *DT = nullptr,
69-
bool UseInstrInfo = true);
68+
bool UseInstrInfo = true,
69+
unsigned Depth = 0);
7070

7171
/// Returns the known bits rather than passing by reference.
7272
LLVM_ABI KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
73-
const DataLayout &DL, unsigned Depth = 0,
73+
const DataLayout &DL,
7474
AssumptionCache *AC = nullptr,
7575
const Instruction *CxtI = nullptr,
7676
const DominatorTree *DT = nullptr,
77-
bool UseInstrInfo = true);
77+
bool UseInstrInfo = true,
78+
unsigned Depth = 0);
7879

7980
LLVM_ABI KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
80-
unsigned Depth, const SimplifyQuery &Q);
81+
const SimplifyQuery &Q, unsigned Depth = 0);
8182

82-
LLVM_ABI KnownBits computeKnownBits(const Value *V, unsigned Depth,
83-
const SimplifyQuery &Q);
83+
LLVM_ABI KnownBits computeKnownBits(const Value *V, const SimplifyQuery &Q,
84+
unsigned Depth = 0);
8485

85-
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
86-
const SimplifyQuery &Q);
86+
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known,
87+
const SimplifyQuery &Q, unsigned Depth = 0);
8788

8889
/// Compute known bits from the range metadata.
8990
/// \p KnownZero the set of bits that are known to be zero
@@ -93,22 +94,22 @@ LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
9394

9495
/// Merge bits known from context-dependent facts into Known.
9596
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known,
96-
unsigned Depth,
97-
const SimplifyQuery &Q);
97+
const SimplifyQuery &Q,
98+
unsigned Depth = 0);
9899

99100
/// Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
100101
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I,
101102
const KnownBits &KnownLHS,
102103
const KnownBits &KnownRHS,
103-
unsigned Depth,
104-
const SimplifyQuery &SQ);
104+
const SimplifyQuery &SQ,
105+
unsigned Depth = 0);
105106

106107
/// Adjust \p Known for the given select \p Arm to include information from the
107108
/// select \p Cond.
108109
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond,
109110
Value *Arm, bool Invert,
110-
unsigned Depth,
111-
const SimplifyQuery &Q);
111+
const SimplifyQuery &Q,
112+
unsigned Depth = 0);
112113

113114
/// Return true if LHS and RHS have no common bits set.
114115
LLVM_ABI bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
@@ -121,14 +122,16 @@ LLVM_ABI bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
121122
/// vectors of integers. If 'OrZero' is set, then return true if the given
122123
/// value is either a power of two or zero.
123124
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
124-
bool OrZero = false, unsigned Depth = 0,
125+
bool OrZero = false,
125126
AssumptionCache *AC = nullptr,
126127
const Instruction *CxtI = nullptr,
127128
const DominatorTree *DT = nullptr,
128-
bool UseInstrInfo = true);
129+
bool UseInstrInfo = true,
130+
unsigned Depth = 0);
129131

130132
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
131-
unsigned Depth, const SimplifyQuery &Q);
133+
const SimplifyQuery &Q,
134+
unsigned Depth = 0);
132135

133136
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI);
134137

@@ -196,21 +199,21 @@ LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask,
196199
/// sign bits for the vector element with the mininum number of known sign
197200
/// bits.
198201
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL,
199-
unsigned Depth = 0,
200202
AssumptionCache *AC = nullptr,
201203
const Instruction *CxtI = nullptr,
202204
const DominatorTree *DT = nullptr,
203-
bool UseInstrInfo = true);
205+
bool UseInstrInfo = true,
206+
unsigned Depth = 0);
204207

205208
/// Get the upper bound on bit size for this Value \p Op as a signed integer.
206209
/// i.e. x == sext(trunc(x to MaxSignificantBits) to bitwidth(x)).
207210
/// Similar to the APInt::getSignificantBits function.
208211
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op,
209212
const DataLayout &DL,
210-
unsigned Depth = 0,
211213
AssumptionCache *AC = nullptr,
212214
const Instruction *CxtI = nullptr,
213-
const DominatorTree *DT = nullptr);
215+
const DominatorTree *DT = nullptr,
216+
unsigned Depth = 0);
214217

215218
/// Map a call instruction to an intrinsic ID. Libcalls which have equivalent
216219
/// intrinsics are treated as-if they were intrinsics.
@@ -236,36 +239,36 @@ LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
236239
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V,
237240
const APInt &DemandedElts,
238241
FPClassTest InterestedClasses,
239-
unsigned Depth,
240-
const SimplifyQuery &SQ);
242+
const SimplifyQuery &SQ,
243+
unsigned Depth = 0);
241244

242245
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V,
243246
FPClassTest InterestedClasses,
244-
unsigned Depth,
245-
const SimplifyQuery &SQ);
247+
const SimplifyQuery &SQ,
248+
unsigned Depth = 0);
246249

247250
LLVM_ABI KnownFPClass computeKnownFPClass(
248251
const Value *V, const DataLayout &DL,
249-
FPClassTest InterestedClasses = fcAllFlags, unsigned Depth = 0,
252+
FPClassTest InterestedClasses = fcAllFlags,
250253
const TargetLibraryInfo *TLI = nullptr, AssumptionCache *AC = nullptr,
251254
const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr,
252-
bool UseInstrInfo = true);
255+
bool UseInstrInfo = true, unsigned Depth = 0);
253256

254257
/// Wrapper to account for known fast math flags at the use instruction.
255258
LLVM_ABI KnownFPClass computeKnownFPClass(
256259
const Value *V, const APInt &DemandedElts, FastMathFlags FMF,
257-
FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ);
260+
FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth = 0);
258261

259262
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, FastMathFlags FMF,
260263
FPClassTest InterestedClasses,
261-
unsigned Depth,
262-
const SimplifyQuery &SQ);
264+
const SimplifyQuery &SQ,
265+
unsigned Depth = 0);
263266

264267
/// Return true if we can prove that the specified FP value is never equal to
265268
/// -0.0. Users should use caution when considering PreserveSign
266269
/// denormal-fp-math.
267-
LLVM_ABI bool cannotBeNegativeZero(const Value *V, unsigned Depth,
268-
const SimplifyQuery &SQ);
270+
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ,
271+
unsigned Depth = 0);
269272

270273
/// Return true if we can prove that the specified FP value is either NaN or
271274
/// never less than -0.0.
@@ -275,30 +278,32 @@ LLVM_ABI bool cannotBeNegativeZero(const Value *V, unsigned Depth,
275278
/// -0 --> true
276279
/// x > +0 --> true
277280
/// x < -0 --> false
278-
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, unsigned Depth,
279-
const SimplifyQuery &SQ);
281+
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V,
282+
const SimplifyQuery &SQ,
283+
unsigned Depth = 0);
280284

281285
/// Return true if the floating-point scalar value is not an infinity or if
282286
/// the floating-point vector value has no infinities. Return false if a value
283287
/// could ever be infinity.
284-
LLVM_ABI bool isKnownNeverInfinity(const Value *V, unsigned Depth,
285-
const SimplifyQuery &SQ);
288+
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ,
289+
unsigned Depth = 0);
286290

287291
/// Return true if the floating-point value can never contain a NaN or infinity.
288-
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, unsigned Depth,
289-
const SimplifyQuery &SQ);
292+
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ,
293+
unsigned Depth = 0);
290294

291295
/// Return true if the floating-point scalar value is not a NaN or if the
292296
/// floating-point vector value has no NaN elements. Return false if a value
293297
/// could ever be NaN.
294-
LLVM_ABI bool isKnownNeverNaN(const Value *V, unsigned Depth,
295-
const SimplifyQuery &SQ);
298+
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ,
299+
unsigned Depth = 0);
296300

297301
/// Return false if we can prove that the specified FP value's sign bit is 0.
298302
/// Return true if we can prove that the specified FP value's sign bit is 1.
299303
/// Otherwise return std::nullopt.
300-
LLVM_ABI std::optional<bool>
301-
computeKnownFPSignBit(const Value *V, unsigned Depth, const SimplifyQuery &SQ);
304+
LLVM_ABI std::optional<bool> computeKnownFPSignBit(const Value *V,
305+
const SimplifyQuery &SQ,
306+
unsigned Depth = 0);
302307

303308
/// Return true if the sign bit of the FP value can be ignored by the user when
304309
/// the value is zero.

llvm/include/llvm/Analysis/WithCache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
namespace llvm {
2424
struct SimplifyQuery;
25-
LLVM_ABI KnownBits computeKnownBits(const Value *V, unsigned Depth,
26-
const SimplifyQuery &Q);
25+
LLVM_ABI KnownBits computeKnownBits(const Value *V, const SimplifyQuery &Q,
26+
unsigned Depth);
2727

2828
template <typename Arg> class WithCache {
2929
static_assert(std::is_pointer_v<Arg>, "WithCache requires a pointer type!");
@@ -45,7 +45,7 @@ template <typename Arg> class WithCache {
4545
mutable KnownBits Known;
4646

4747
void calculateKnownBits(const SimplifyQuery &Q) const {
48-
Known = computeKnownBits(Pointer.getPointer(), 0, Q);
48+
Known = computeKnownBits(Pointer.getPointer(), Q, 0);
4949
Pointer.setInt(true);
5050
}
5151

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -430,36 +430,39 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
430430
/// methods should return the value returned by this function.
431431
virtual Instruction *eraseInstFromFunction(Instruction &I) = 0;
432432

433-
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
434-
const Instruction *CxtI) const {
435-
llvm::computeKnownBits(V, Known, Depth, SQ.getWithInstruction(CxtI));
433+
void computeKnownBits(const Value *V, KnownBits &Known,
434+
const Instruction *CxtI, unsigned Depth = 0) const {
435+
llvm::computeKnownBits(V, Known, SQ.getWithInstruction(CxtI), Depth);
436436
}
437437

438-
KnownBits computeKnownBits(const Value *V, unsigned Depth,
439-
const Instruction *CxtI) const {
440-
return llvm::computeKnownBits(V, Depth, SQ.getWithInstruction(CxtI));
438+
KnownBits computeKnownBits(const Value *V, const Instruction *CxtI,
439+
unsigned Depth = 0) const {
440+
return llvm::computeKnownBits(V, SQ.getWithInstruction(CxtI), Depth);
441441
}
442442

443443
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
444-
unsigned Depth = 0,
445-
const Instruction *CxtI = nullptr) {
446-
return llvm::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
447-
SQ.getWithInstruction(CxtI));
444+
const Instruction *CxtI = nullptr,
445+
unsigned Depth = 0) {
446+
return llvm::isKnownToBeAPowerOfTwo(V, OrZero, SQ.getWithInstruction(CxtI),
447+
Depth);
448448
}
449449

450-
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
451-
const Instruction *CxtI = nullptr) const {
450+
bool MaskedValueIsZero(const Value *V, const APInt &Mask,
451+
const Instruction *CxtI = nullptr,
452+
unsigned Depth = 0) const {
452453
return llvm::MaskedValueIsZero(V, Mask, SQ.getWithInstruction(CxtI), Depth);
453454
}
454455

455-
unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
456-
const Instruction *CxtI = nullptr) const {
457-
return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
456+
unsigned ComputeNumSignBits(const Value *Op,
457+
const Instruction *CxtI = nullptr,
458+
unsigned Depth = 0) const {
459+
return llvm::ComputeNumSignBits(Op, DL, &AC, CxtI, &DT, Depth);
458460
}
459461

460-
unsigned ComputeMaxSignificantBits(const Value *Op, unsigned Depth = 0,
461-
const Instruction *CxtI = nullptr) const {
462-
return llvm::ComputeMaxSignificantBits(Op, DL, Depth, &AC, CxtI, &DT);
462+
unsigned ComputeMaxSignificantBits(const Value *Op,
463+
const Instruction *CxtI = nullptr,
464+
unsigned Depth = 0) const {
465+
return llvm::ComputeMaxSignificantBits(Op, DL, &AC, CxtI, &DT, Depth);
463466
}
464467

465468
OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
@@ -507,12 +510,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
507510

508511
virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
509512
const APInt &DemandedMask, KnownBits &Known,
510-
unsigned Depth, const SimplifyQuery &Q) = 0;
513+
const SimplifyQuery &Q,
514+
unsigned Depth = 0) = 0;
511515

512516
bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
513517
const APInt &DemandedMask, KnownBits &Known) {
514518
return SimplifyDemandedBits(I, OpNo, DemandedMask, Known,
515-
/*Depth=*/0, SQ.getWithInstruction(I));
519+
SQ.getWithInstruction(I));
516520
}
517521

518522
virtual Value *

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,7 @@ AliasResult BasicAAResult::aliasGEP(
12611261

12621262
ConstantRange CR = computeConstantRange(Index.Val.V, /* ForSigned */ false,
12631263
true, &AC, Index.CxtI);
1264-
KnownBits Known =
1265-
computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT);
1264+
KnownBits Known = computeKnownBits(Index.Val.V, DL, &AC, Index.CxtI, DT);
12661265
CR = CR.intersectWith(
12671266
ConstantRange::fromKnownBits(Known, /* Signed */ true),
12681267
ConstantRange::Signed);

llvm/lib/Analysis/DemandedBits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ void DemandedBits::determineLiveOperandBits(
7070

7171
const DataLayout &DL = UserI->getDataLayout();
7272
Known = KnownBits(BitWidth);
73-
computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
73+
computeKnownBits(V1, Known, DL, &AC, UserI, &DT);
7474

7575
if (V2) {
7676
Known2 = KnownBits(BitWidth);
77-
computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
77+
computeKnownBits(V2, Known2, DL, &AC, UserI, &DT);
7878
}
7979
};
8080

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
111111
// If demanded bits wasn't able to limit the bit width, we can try to use
112112
// value tracking instead. This can be the case, for example, if the value
113113
// may be negative.
114-
auto NumSignBits = ComputeNumSignBits(Exit, DL, 0, AC, nullptr, DT);
114+
auto NumSignBits = ComputeNumSignBits(Exit, DL, AC, nullptr, DT);
115115
auto NumTypeBits = DL.getTypeSizeInBits(Exit->getType());
116116
MaxBitWidth = NumTypeBits - NumSignBits;
117117
KnownBits Bits = computeKnownBits(Exit, DL);

0 commit comments

Comments
 (0)