Skip to content

Commit f688f24

Browse files
committed
[HashRecognize] Address latest review
1 parent f57af30 commit f688f24

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,31 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I,
182182
Value *L, *R, *TV, *FV;
183183
if (match(I, m_Select(m_ICmp(Pred, m_Value(L), m_Value(R)), m_Value(TV),
184184
m_Value(FV)))) {
185-
KnownBits KnownL = compute(L, KnownPhis).zextOrTrunc(BitWidth);
186-
KnownBits KnownR = compute(R, KnownPhis).zextOrTrunc(BitWidth);
187-
KnownBits KnownTV = compute(TV, KnownPhis);
188-
KnownBits KnownFV = compute(FV, KnownPhis);
189-
auto LCR = ConstantRange::fromKnownBits(KnownL, false);
190-
auto RCR = ConstantRange::fromKnownBits(KnownR, false);
191-
192185
// We need to check LCR against [0, 2) in the little-endian case, because
193186
// the RCR check is insufficient: it is simply [0, 1).
194-
auto CheckLCR = ConstantRange(APInt::getZero(BitWidth), APInt(BitWidth, 2));
195-
if (!ByteOrderSwapped && LCR != CheckLCR) {
196-
ErrStr = "Bad LHS of significant-bit-check";
197-
return {BitWidth};
187+
if (!ByteOrderSwapped) {
188+
KnownBits KnownL = compute(L, KnownPhis).zextOrTrunc(BitWidth);
189+
auto LCR = ConstantRange::fromKnownBits(KnownL, false);
190+
auto CheckLCR =
191+
ConstantRange(APInt::getZero(BitWidth), APInt(BitWidth, 2));
192+
if (LCR != CheckLCR) {
193+
ErrStr = "Bad LHS of significant-bit-check";
194+
return {BitWidth};
195+
}
198196
}
199197

200198
// Check that the predication is on (most|least) significant bit.
199+
KnownBits KnownR = compute(R, KnownPhis).zextOrTrunc(BitWidth);
200+
auto RCR = ConstantRange::fromKnownBits(KnownR, false);
201201
auto AllowedR = ConstantRange::makeAllowedICmpRegion(Pred, RCR);
202-
auto InverseR = ConstantRange::makeAllowedICmpRegion(
203-
CmpInst::getInversePredicate(Pred), RCR);
204202
ConstantRange LSBRange(APInt::getZero(BitWidth), APInt(BitWidth, 1));
205203
ConstantRange MSBRange(APInt::getZero(BitWidth),
206204
APInt::getSignedMinValue(BitWidth));
207205
const ConstantRange &CheckRCR = ByteOrderSwapped ? MSBRange : LSBRange;
208206
if (AllowedR == CheckRCR)
209-
return KnownTV;
207+
return compute(TV, KnownPhis);
210208
if (AllowedR.inverse() == CheckRCR)
211-
return KnownFV;
209+
return compute(FV, KnownPhis);
212210

213211
ErrStr = "Bad RHS of significant-bit-check";
214212
return {BitWidth};
@@ -233,9 +231,8 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I,
233231
/// Compute the KnownBits of Value \p V.
234232
KnownBits ValueEvolution::compute(const Value *V,
235233
const KnownPhiMap &KnownPhis) {
236-
const APInt *C;
237-
if (match(V, m_APInt(C)))
238-
return KnownBits::makeConstant(*C);
234+
if (auto *CI = dyn_cast<ConstantInt>(V))
235+
return KnownBits::makeConstant(CI->getValue());
239236

240237
if (auto *I = dyn_cast<Instruction>(V))
241238
return computeInstr(I, KnownPhis);
@@ -260,7 +257,7 @@ ValueEvolution::computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions) {
260257
KnownPhis.emplace_or_assign(Phi, KnownAtIter);
261258
}
262259
}
263-
return KnownPhis;
260+
return hasError() ? std::nullopt : std::make_optional(KnownPhis);
264261
}
265262

266263
/// A structure that can hold either a Simple Recurrence or a Conditional
@@ -375,11 +372,11 @@ RecurrenceInfo::digRecurrence(Instruction *V,
375372
/// where %tv and %fv ultimately end up using %rec via the same %BO instruction,
376373
/// after digging through the use-def chain.
377374
///
378-
/// \p ExtraConst is relevant if \p BOWithConstOpToMatch is supplied: when
379-
/// digging the use-def chain, a BinOp with opcode \p BOWithConstOpToMatch is
380-
/// matched, and \p ExtraConst is a constant operand of that BinOp. This
381-
/// peculiarity exists, because in a CRC algorithm, the \p BOWithConstOpToMatch
382-
/// is an XOR, and the \p ExtraConst ends up being the generating polynomial.
375+
/// ExtraConst is relevant if \p BOWithConstOpToMatch is supplied: when digging
376+
/// the use-def chain, a BinOp with opcode \p BOWithConstOpToMatch is matched,
377+
/// and ExtraConst is a constant operand of that BinOp. This peculiarity exists,
378+
/// because in a CRC algorithm, the \p BOWithConstOpToMatch is an XOR, and the
379+
/// ExtraConst ends up being the generating polynomial.
383380
bool RecurrenceInfo::matchConditionalRecurrence(
384381
const PHINode *P, Instruction::BinaryOps BOWithConstOpToMatch) {
385382
Phi = P;
@@ -447,9 +444,11 @@ PolynomialInfo::PolynomialInfo(unsigned TripCount, const Value *LHS,
447444
: TripCount(TripCount), LHS(LHS), RHS(RHS), ComputedValue(ComputedValue),
448445
ByteOrderSwapped(ByteOrderSwapped), LHSAux(LHSAux) {}
449446

450-
/// In big-endian case, checks that the bottom N bits against CheckFn, and that
451-
/// the rest are unknown. In little-endian case, checks that the top N bits
452-
/// against CheckFn, and that the rest are unknown.
447+
/// In the big-endian case, checks the bottom N bits against CheckFn, and that
448+
/// the rest are unknown. In the little-endian case, checks the top N bits
449+
/// against CheckFn, and that the rest are unknown. Callers usually call this
450+
/// function with N = TripCount, and CheckFn checking that the remainder bits of
451+
/// the CRC polynomial division are zero.
453452
static bool checkExtractBits(const KnownBits &Known, unsigned N,
454453
function_ref<bool(const KnownBits &)> CheckFn,
455454
bool ByteOrderSwapped) {
@@ -555,7 +554,7 @@ HashRecognize::recognizeCRC() const {
555554
if (!L.isInnermost())
556555
return "Loop is not innermost";
557556
unsigned TC = SE.getSmallConstantMaxTripCount(&L);
558-
if (!TC)
557+
if (!TC || TC > 256)
559558
return "Unable to find a small constant trip count";
560559
BasicBlock *Latch = L.getLoopLatch();
561560
BasicBlock *Exit = L.getExitBlock();
@@ -600,7 +599,9 @@ HashRecognize::recognizeCRC() const {
600599
const APInt &GenPoly = *ConditionalRecurrence.ExtraConst;
601600

602601
// PhiEvolutions are pairs of PHINodes along with their incoming value from
603-
// within the loop, which we term as their step.
602+
// within the loop, which we term as their step. Note that in the case of a
603+
// Simple Recurrence, Step is an operand of the BO, while in a Conditional
604+
// Recurrence, it is a SelectInst.
604605
SmallVector<PhiStepPair, 2> PhiEvolutions;
605606
PhiEvolutions.emplace_back(ConditionalRecurrence.Phi, ComputedValue);
606607
if (SimpleRecurrence)

0 commit comments

Comments
 (0)