Skip to content

Commit 588eb93

Browse files
committed
[HashRecognize] Address review
1 parent 7538b13 commit 588eb93

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class ValueEvolution {
9090
const bool ByteOrderSwapped;
9191
APInt GenPoly;
9292
StringRef ErrStr;
93-
KnownPhiMap KnownPhis;
9493

9594
KnownBits computeBinOp(const BinaryOperator *I);
9695
KnownBits computeInstr(const Instruction *I);
@@ -107,8 +106,10 @@ class ValueEvolution {
107106
// Given a list of PHI nodes along with their incoming value from within the
108107
// loop, and the trip-count of the loop, computeEvolutions
109108
// computes the KnownBits of each of the PHI nodes on the final iteration.
110-
std::optional<KnownPhiMap>
111-
computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions);
109+
bool computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions);
110+
111+
// The resulting KnownBits for each PHI node.
112+
KnownPhiMap KnownPhis;
112113
};
113114

114115
ValueEvolution::ValueEvolution(unsigned TripCount, bool ByteOrderSwapped)
@@ -184,7 +185,8 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I) {
184185
// We need to check LCR against [0, 2) in the little-endian case, because
185186
// the RCR check is insufficient: it is simply [0, 1).
186187
if (!ByteOrderSwapped) {
187-
KnownBits KnownL = compute(L).zextOrTrunc(BitWidth);
188+
KnownBits KnownL = compute(L);
189+
BitWidth = KnownL.getBitWidth();
188190
auto LCR = ConstantRange::fromKnownBits(KnownL, false);
189191
auto CheckLCR =
190192
ConstantRange(APInt::getZero(BitWidth), APInt(BitWidth, 2));
@@ -195,13 +197,13 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I) {
195197
}
196198

197199
// Check that the predication is on (most|least) significant bit.
198-
KnownBits KnownR = compute(R).zextOrTrunc(BitWidth);
200+
KnownBits KnownR = compute(R);
201+
BitWidth = KnownR.getBitWidth();
199202
auto RCR = ConstantRange::fromKnownBits(KnownR, false);
200203
auto AllowedR = ConstantRange::makeAllowedICmpRegion(Pred, RCR);
201-
ConstantRange LSBRange(APInt::getZero(BitWidth), APInt(BitWidth, 1));
202-
ConstantRange MSBRange(APInt::getZero(BitWidth),
203-
APInt::getSignedMinValue(BitWidth));
204-
const ConstantRange &CheckRCR = ByteOrderSwapped ? MSBRange : LSBRange;
204+
ConstantRange CheckRCR(APInt::getZero(BitWidth),
205+
ByteOrderSwapped ? APInt::getSignedMinValue(BitWidth)
206+
: APInt(BitWidth, 1));
205207
if (AllowedR == CheckRCR)
206208
return compute(TV);
207209
if (AllowedR.inverse() == CheckRCR)
@@ -242,19 +244,18 @@ KnownBits ValueEvolution::compute(const Value *V) {
242244

243245
// Takes every PHI-step pair in PhiEvolutions, and computes KnownBits on the
244246
// final iteration, using KnownBits from the previous iteration.
245-
std::optional<KnownPhiMap>
246-
ValueEvolution::computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions) {
247+
bool ValueEvolution::computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions) {
247248
for (unsigned I = 0; I < TripCount; ++I) {
248249
for (auto [Phi, Step] : PhiEvolutions) {
249250
KnownBits KnownAtIter = computeInstr(Step);
250251
if (KnownAtIter.getBitWidth() < I + 1) {
251252
ErrStr = "Loop iterations exceed bitwidth of result";
252-
return std::nullopt;
253+
return false;
253254
}
254255
KnownPhis.emplace_or_assign(Phi, KnownAtIter);
255256
}
256257
}
257-
return hasError() ? std::nullopt : std::make_optional(KnownPhis);
258+
return !hasError();
258259
}
259260

260261
/// A structure that can hold either a Simple Recurrence or a Conditional
@@ -605,12 +606,10 @@ HashRecognize::recognizeCRC() const {
605606
PhiEvolutions.emplace_back(SimpleRecurrence.Phi, SimpleRecurrence.BO);
606607

607608
ValueEvolution VE(TC, *ByteOrderSwapped);
608-
std::optional<KnownPhiMap> KnownPhis = VE.computeEvolutions(PhiEvolutions);
609-
610-
if (VE.hasError())
609+
if (!VE.computeEvolutions(PhiEvolutions))
611610
return VE.getError();
611+
KnownBits ResultBits = VE.KnownPhis.at(ConditionalRecurrence.Phi);
612612

613-
KnownBits ResultBits = KnownPhis->at(ConditionalRecurrence.Phi);
614613
auto IsZero = [](const KnownBits &K) { return K.isZero(); };
615614
if (!checkExtractBits(ResultBits, TC, IsZero, *ByteOrderSwapped))
616615
return ErrBits(ResultBits, TC, *ByteOrderSwapped);

0 commit comments

Comments
 (0)