Skip to content

Commit d430015

Browse files
committed
Revert "[ValueTracking] Remove by-ref computeKnownBits() overloads (NFC)"
This reverts commit b5743d4. This causes some minor compile-time impact. Revert for now, better to do the change more gradually.
1 parent 4d6fc88 commit d430015

File tree

12 files changed

+105
-48
lines changed

12 files changed

+105
-48
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,43 @@ class Value;
4646

4747
constexpr unsigned MaxAnalysisRecursionDepth = 6;
4848

49-
/// Determine which bits of V are known to be either zero or one.
49+
/// Determine which bits of V are known to be either zero or one and return
50+
/// them in the KnownZero/KnownOne bit sets.
5051
///
5152
/// This function is defined on values with integer type, values with pointer
5253
/// type, and vectors of integers. In the case
5354
/// where V is a vector, the known zero and known one values are the
5455
/// same width as the vector element, and the bit is set only if it is true
5556
/// for all of the elements in the vector.
56-
KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
57-
unsigned Depth = 0, AssumptionCache *AC = nullptr,
58-
const Instruction *CxtI = nullptr,
59-
const DominatorTree *DT = nullptr,
60-
bool UseInstrInfo = true);
61-
62-
/// Determine which bits of V are known to be either zero or one.
57+
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL,
58+
unsigned Depth = 0, AssumptionCache *AC = nullptr,
59+
const Instruction *CxtI = nullptr,
60+
const DominatorTree *DT = nullptr,
61+
bool UseInstrInfo = true);
62+
63+
/// Determine which bits of V are known to be either zero or one and return
64+
/// them in the KnownZero/KnownOne bit sets.
6365
///
6466
/// This function is defined on values with integer type, values with pointer
6567
/// type, and vectors of integers. In the case
6668
/// where V is a vector, the known zero and known one values are the
6769
/// same width as the vector element, and the bit is set only if it is true
6870
/// for all of the demanded elements in the vector.
71+
void computeKnownBits(const Value *V, const APInt &DemandedElts,
72+
KnownBits &Known, const DataLayout &DL,
73+
unsigned Depth = 0, AssumptionCache *AC = nullptr,
74+
const Instruction *CxtI = nullptr,
75+
const DominatorTree *DT = nullptr,
76+
bool UseInstrInfo = true);
77+
78+
/// Returns the known bits rather than passing by reference.
79+
KnownBits computeKnownBits(const Value *V, const DataLayout &DL,
80+
unsigned Depth = 0, AssumptionCache *AC = nullptr,
81+
const Instruction *CxtI = nullptr,
82+
const DominatorTree *DT = nullptr,
83+
bool UseInstrInfo = true);
84+
85+
/// Returns the known bits rather than passing by reference.
6986
KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
7087
const DataLayout &DL, unsigned Depth = 0,
7188
AssumptionCache *AC = nullptr,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
466466
/// methods should return the value returned by this function.
467467
virtual Instruction *eraseInstFromFunction(Instruction &I) = 0;
468468

469+
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
470+
const Instruction *CxtI) const {
471+
llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
472+
}
473+
469474
KnownBits computeKnownBits(const Value *V, unsigned Depth,
470475
const Instruction *CxtI) const {
471476
return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);

llvm/lib/Analysis/DemandedBits.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ void DemandedBits::determineLiveOperandBits(
7070
KnownBitsComputed = true;
7171

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

75-
if (V2)
76-
Known2 = computeKnownBits(V2, DL, 0, &AC, UserI, &DT);
76+
if (V2) {
77+
Known2 = KnownBits(BitWidth);
78+
computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
79+
}
7780
};
7881

7982
switch (UserI->getOpcode()) {

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7722,8 +7722,9 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
77227722
unsigned LZ = A.countl_zero();
77237723
unsigned TZ = A.countr_zero();
77247724
unsigned BitWidth = A.getBitWidth();
7725-
KnownBits Known =
7726-
computeKnownBits(BO->LHS, getDataLayout(), 0, &AC, nullptr, &DT);
7725+
KnownBits Known(BitWidth);
7726+
computeKnownBits(BO->LHS, Known, getDataLayout(),
7727+
0, &AC, nullptr, &DT);
77277728

77287729
APInt EffectiveMask =
77297730
APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
159159
computeKnownBits(V, DemandedElts, Known, Depth, Q);
160160
}
161161

162+
void llvm::computeKnownBits(const Value *V, KnownBits &Known,
163+
const DataLayout &DL, unsigned Depth,
164+
AssumptionCache *AC, const Instruction *CxtI,
165+
const DominatorTree *DT, bool UseInstrInfo) {
166+
::computeKnownBits(
167+
V, Known, Depth,
168+
SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
169+
}
170+
171+
void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
172+
KnownBits &Known, const DataLayout &DL,
173+
unsigned Depth, AssumptionCache *AC,
174+
const Instruction *CxtI, const DominatorTree *DT,
175+
bool UseInstrInfo) {
176+
::computeKnownBits(
177+
V, DemandedElts, Known, Depth,
178+
SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
179+
}
180+
162181
static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
163182
unsigned Depth, const SimplifyQuery &Q);
164183

@@ -231,9 +250,11 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
231250
match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
232251
return true;
233252
}
234-
235-
KnownBits LHSKnown = ::computeKnownBits(LHS, 0, SQ);
236-
KnownBits RHSKnown = ::computeKnownBits(RHS, 0, SQ);
253+
IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
254+
KnownBits LHSKnown(IT->getBitWidth());
255+
KnownBits RHSKnown(IT->getBitWidth());
256+
::computeKnownBits(LHS, LHSKnown, 0, SQ);
257+
::computeKnownBits(RHS, RHSKnown, 0, SQ);
237258
return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown);
238259
}
239260

@@ -8119,8 +8140,9 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
81198140
// If X & C == 0 then (X | C) == X +_{nuw} C
81208141
if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
81218142
match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
8122-
KnownBits Known = computeKnownBits(X, DL, Depth + 1, /*AC*/ nullptr,
8123-
/*CxtI*/ nullptr, /*DT*/ nullptr);
8143+
KnownBits Known(CA->getBitWidth());
8144+
computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
8145+
/*CxtI*/ nullptr, /*DT*/ nullptr);
81248146
if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
81258147
return true;
81268148
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12147,7 +12147,9 @@ MaybeAlign SelectionDAG::InferPtrAlign(SDValue Ptr) const {
1214712147
const GlobalValue *GV = nullptr;
1214812148
int64_t GVOffset = 0;
1214912149
if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12150-
KnownBits Known = llvm::computeKnownBits(GV, getDataLayout());
12150+
unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12151+
KnownBits Known(PtrWidth);
12152+
llvm::computeKnownBits(GV, Known, getDataLayout());
1215112153
unsigned AlignBits = Known.countMinTrailingZeros();
1215212154
if (AlignBits)
1215312155
return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);

llvm/lib/Target/Hexagon/HexagonLoopIdiomRecognition.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,8 @@ bool PolynomialMultiplyRecognize::highBitsAreZero(Value *V,
12701270
if (!T)
12711271
return false;
12721272

1273-
KnownBits Known = computeKnownBits(V, DL);
1273+
KnownBits Known(T->getBitWidth());
1274+
computeKnownBits(V, Known, DL);
12741275
return Known.countMinLeadingZeros() >= IterCount;
12751276
}
12761277

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ static Instruction *foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC) {
642642
return CastInst::Create(Instruction::ZExt, NarrowPop, Ty);
643643
}
644644

645-
KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
645+
KnownBits Known(BitWidth);
646+
IC.computeKnownBits(Op0, Known, 0, &II);
646647

647648
// If all bits are zero except for exactly one fixed bit, then the result
648649
// must be 0 or 1, and we can get that answer by shifting to LSB:
@@ -2874,7 +2875,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
28742875

28752876
// If there is a dominating assume with the same condition as this one,
28762877
// then this one is redundant, and should be removed.
2877-
KnownBits Known = computeKnownBits(IIOperand, 0, II);
2878+
KnownBits Known(1);
2879+
computeKnownBits(IIOperand, Known, 0, II);
28782880
if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
28792881
return eraseInstFromFunction(*II);
28802882

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3749,7 +3749,8 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
37493749
// The motivation for this call into value tracking is to take advantage of
37503750
// the assumption cache, so make sure that is populated.
37513751
if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
3752-
KnownBits Known = computeKnownBits(CondVal, 0, &SI);
3752+
KnownBits Known(1);
3753+
computeKnownBits(CondVal, Known, 0, &SI);
37533754
if (Known.One.isOne())
37543755
return replaceInstUsesWith(SI, TrueVal);
37553756
if (Known.Zero.isOne())

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
119119
"Value *V, DemandedMask and Known must have same BitWidth");
120120

121121
if (isa<Constant>(V)) {
122-
Known = computeKnownBits(V, Depth, CxtI);
122+
computeKnownBits(V, Known, Depth, CxtI);
123123
return nullptr;
124124
}
125125

@@ -132,7 +132,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
132132

133133
Instruction *I = dyn_cast<Instruction>(V);
134134
if (!I) {
135-
Known = computeKnownBits(V, Depth, CxtI);
135+
computeKnownBits(V, Known, Depth, CxtI);
136136
return nullptr; // Only analyze instructions.
137137
}
138138

@@ -184,7 +184,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
184184

185185
switch (I->getOpcode()) {
186186
default:
187-
Known = computeKnownBits(I, Depth, CxtI);
187+
computeKnownBits(I, Known, Depth, CxtI);
188188
break;
189189
case Instruction::And: {
190190
// If either the LHS or the RHS are Zero, the result is zero.
@@ -598,7 +598,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
598598
return InsertNewInstWith(And1, I->getIterator());
599599
}
600600

601-
Known = computeKnownBits(I, Depth, CxtI);
601+
computeKnownBits(I, Known, Depth, CxtI);
602602
break;
603603
}
604604
case Instruction::Shl: {
@@ -660,7 +660,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
660660
return I;
661661
}
662662
}
663-
Known = computeKnownBits(I, Depth, CxtI);
663+
computeKnownBits(I, Known, Depth, CxtI);
664664
}
665665
break;
666666
}
@@ -712,7 +712,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
712712
if (ShiftAmt)
713713
Known.Zero.setHighBits(ShiftAmt); // high bits known zero.
714714
} else {
715-
Known = computeKnownBits(I, Depth, CxtI);
715+
computeKnownBits(I, Known, Depth, CxtI);
716716
}
717717
break;
718718
}
@@ -775,7 +775,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
775775
Known.One |= HighBits;
776776
}
777777
} else {
778-
Known = computeKnownBits(I, Depth, CxtI);
778+
computeKnownBits(I, Known, Depth, CxtI);
779779
}
780780
break;
781781
}
@@ -797,7 +797,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
797797
Known = KnownBits::udiv(LHSKnown, KnownBits::makeConstant(*SA),
798798
cast<BinaryOperator>(I)->isExact());
799799
} else {
800-
Known = computeKnownBits(I, Depth, CxtI);
800+
computeKnownBits(I, Known, Depth, CxtI);
801801
}
802802
break;
803803
}
@@ -837,7 +837,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
837837
}
838838
}
839839

840-
Known = computeKnownBits(I, Depth, CxtI);
840+
computeKnownBits(I, Known, Depth, CxtI);
841841
break;
842842
}
843843
case Instruction::URem: {
@@ -977,7 +977,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
977977
}
978978

979979
if (!KnownBitsComputed)
980-
Known = computeKnownBits(V, Depth, CxtI);
980+
computeKnownBits(V, Known, Depth, CxtI);
981981
break;
982982
}
983983
}
@@ -1007,8 +1007,8 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10071007
// this instruction has a simpler value in that context.
10081008
switch (I->getOpcode()) {
10091009
case Instruction::And: {
1010-
RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, CxtI);
1011-
LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, CxtI);
1010+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
1011+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
10121012
Known = LHSKnown & RHSKnown;
10131013
computeKnownBitsFromAssume(I, Known, Depth, SQ.getWithInstruction(CxtI));
10141014

@@ -1027,8 +1027,8 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10271027
break;
10281028
}
10291029
case Instruction::Or: {
1030-
RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, CxtI);
1031-
LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, CxtI);
1030+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
1031+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
10321032
Known = LHSKnown | RHSKnown;
10331033
computeKnownBitsFromAssume(I, Known, Depth, SQ.getWithInstruction(CxtI));
10341034

@@ -1049,8 +1049,8 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10491049
break;
10501050
}
10511051
case Instruction::Xor: {
1052-
RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, CxtI);
1053-
LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, CxtI);
1052+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
1053+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
10541054
Known = LHSKnown ^ RHSKnown;
10551055
computeKnownBitsFromAssume(I, Known, Depth, SQ.getWithInstruction(CxtI));
10561056

@@ -1075,11 +1075,11 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10751075

10761076
// If an operand adds zeros to every bit below the highest demanded bit,
10771077
// that operand doesn't change the result. Return the other side.
1078-
RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, CxtI);
1078+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
10791079
if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
10801080
return I->getOperand(0);
10811081

1082-
LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, CxtI);
1082+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
10831083
if (DemandedFromOps.isSubsetOf(LHSKnown.Zero))
10841084
return I->getOperand(1);
10851085

@@ -1094,19 +1094,19 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10941094

10951095
// If an operand subtracts zeros from every bit below the highest demanded
10961096
// bit, that operand doesn't change the result. Return the other side.
1097-
RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, CxtI);
1097+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
10981098
if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
10991099
return I->getOperand(0);
11001100

11011101
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1102-
LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, CxtI);
1102+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
11031103
Known = KnownBits::computeForAddSub(/*Add*/ false, NSW, LHSKnown, RHSKnown);
11041104
computeKnownBitsFromAssume(I, Known, Depth, SQ.getWithInstruction(CxtI));
11051105
break;
11061106
}
11071107
case Instruction::AShr: {
11081108
// Compute the Known bits to simplify things downstream.
1109-
Known = computeKnownBits(I, Depth, CxtI);
1109+
computeKnownBits(I, Known, Depth, CxtI);
11101110

11111111
// If this user is only demanding bits that we know, return the known
11121112
// constant.
@@ -1133,7 +1133,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
11331133
}
11341134
default:
11351135
// Compute the Known bits to simplify things downstream.
1136-
Known = computeKnownBits(I, Depth, CxtI);
1136+
computeKnownBits(I, Known, Depth, CxtI);
11371137

11381138
// If this user is only demanding bits that we know, return the known
11391139
// constant.

llvm/lib/Transforms/Utils/BypassSlowDivision.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ ValueRange FastDivInsertionTask::getValueRange(Value *V,
234234
unsigned HiBits = LongLen - ShortLen;
235235

236236
const DataLayout &DL = SlowDivOrRem->getModule()->getDataLayout();
237-
KnownBits Known = computeKnownBits(V, DL);
237+
KnownBits Known(LongLen);
238+
239+
computeKnownBits(V, Known, DL);
238240

239241
if (Known.countMinLeadingZeros() >= HiBits)
240242
return VALRNG_KNOWN_SHORT;

llvm/lib/Transforms/Vectorize/LoadStoreVectorizer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,9 @@ std::optional<APInt> Vectorizer::getConstantOffsetComplexAddrs(
12621262
if (!Safe) {
12631263
// When computing known bits, use the GEPs as context instructions, since
12641264
// they likely are in the same BB as the load/store.
1265-
KnownBits Known = computeKnownBits((IdxDiff.sge(0) ? ValA : OpB), DL, 0,
1266-
&AC, ContextInst, &DT);
1265+
KnownBits Known(BitWidth);
1266+
computeKnownBits((IdxDiff.sge(0) ? ValA : OpB), Known, DL, 0, &AC,
1267+
ContextInst, &DT);
12671268
APInt BitsAllowedToBeSet = Known.Zero.zext(IdxDiff.getBitWidth());
12681269
if (Signed)
12691270
BitsAllowedToBeSet.clearBit(BitWidth - 1);

0 commit comments

Comments
 (0)