35
35
// following: in such fields, polynomial addition and subtraction are identical
36
36
// and equivalent to XOR, polynomial multiplication is an AND, and polynomial
37
37
// division is identity: the XOR and AND operations in unoptimized
38
- // implmentations are performed bit-wise, and can be optimized to be performed
38
+ // implementations are performed bit-wise, and can be optimized to be performed
39
39
// chunk-wise, by interleaving copies of the generating polynomial, and storing
40
40
// the pre-computed values in a table.
41
41
//
@@ -87,11 +87,10 @@ using PhiStepPair = std::pair<const PHINode *, const Instruction *>;
87
87
// / given trip count, and predication is specialized for a significant-bit
88
88
// / check.
89
89
class ValueEvolution {
90
- unsigned TripCount;
91
- bool ByteOrderSwapped;
90
+ const unsigned TripCount;
91
+ const bool ByteOrderSwapped;
92
92
APInt GenPoly;
93
93
StringRef ErrStr;
94
- unsigned AtIteration;
95
94
96
95
KnownBits computeBinOp (const BinaryOperator *I, const KnownPhiMap &KnownPhis);
97
96
KnownBits computeInstr (const Instruction *I, const KnownPhiMap &KnownPhis);
@@ -182,8 +181,8 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I,
182
181
if (const PHINode *P = dyn_cast<PHINode>(I))
183
182
return KnownPhis.lookup_or (P, BitWidth);
184
183
185
- // Compute the KnownBits for a Select(Cmp()), forcing it to take the take the
186
- // branch that is predicated on the (least|most)-significant-bit check.
184
+ // Compute the KnownBits for a Select(Cmp()), forcing it to take the branch
185
+ // that is predicated on the (least|most)-significant-bit check.
187
186
CmpPredicate Pred;
188
187
Value *L, *R, *TV, *FV;
189
188
if (match (I, m_Select (m_ICmp (Pred, m_Value (L), m_Value (R)), m_Value (TV),
@@ -239,8 +238,6 @@ KnownBits ValueEvolution::computeInstr(const Instruction *I,
239
238
// / Compute the KnownBits of Value \p V.
240
239
KnownBits ValueEvolution::compute (const Value *V,
241
240
const KnownPhiMap &KnownPhis) {
242
- unsigned BitWidth = V->getType ()->getScalarSizeInBits ();
243
-
244
241
const APInt *C;
245
242
if (match (V, m_APInt (C)))
246
243
return KnownBits::makeConstant (*C);
@@ -249,6 +246,7 @@ KnownBits ValueEvolution::compute(const Value *V,
249
246
return computeInstr (I, KnownPhis);
250
247
251
248
ErrStr = " Unknown Value" ;
249
+ unsigned BitWidth = V->getType ()->getScalarSizeInBits ();
252
250
return {BitWidth};
253
251
}
254
252
@@ -258,7 +256,6 @@ std::optional<KnownPhiMap>
258
256
ValueEvolution::computeEvolutions (ArrayRef<PhiStepPair> PhiEvolutions) {
259
257
KnownPhiMap KnownPhis;
260
258
for (unsigned I = 0 ; I < TripCount; ++I) {
261
- AtIteration = I;
262
259
for (auto [Phi, Step] : PhiEvolutions) {
263
260
KnownBits KnownAtIter = computeInstr (Step, KnownPhis);
264
261
if (KnownAtIter.getBitWidth () < I + 1 ) {
@@ -320,8 +317,8 @@ digRecurrence(Instruction *V, const PHINode *P, const Loop &L,
320
317
// / \p ExtraConst is relevant if \p BOWithConstOpToMatch is supplied: when
321
318
// / digging the use-def chain, a BinOp with opcode \p BOWithConstOpToMatch is
322
319
// / matched, and \p ExtraConst is a constant operand of that BinOp. This
323
- // / peculiary exists, because in a CRC algorithm, the \p BOWithConstOpToMatch is
324
- // / an XOR, and the \p ExtraConst ends up being the generating polynomial.
320
+ // / peculiarity exists, because in a CRC algorithm, the \p BOWithConstOpToMatch
321
+ // / is an XOR, and the \p ExtraConst ends up being the generating polynomial.
325
322
static bool matchConditionalRecurrence (
326
323
const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step,
327
324
const Loop &L, const APInt *&ExtraConst,
@@ -434,9 +431,9 @@ PolynomialInfo::PolynomialInfo(unsigned TripCount, const Value *LHS,
434
431
: TripCount(TripCount), LHS(LHS), RHS(RHS), ComputedValue(ComputedValue),
435
432
ByteOrderSwapped(ByteOrderSwapped), LHSAux(LHSAux) {}
436
433
437
- // / In big-endian case, checks that bottom N bits against CheckFn, and that the
438
- // / rest are unknown. In little-endian case, checks that the top N bits against
439
- // / CheckFn, and that the rest are unknown.
434
+ // / In big-endian case, checks that the bottom N bits against CheckFn, and that
435
+ // / the rest are unknown. In little-endian case, checks that the top N bits
436
+ // / against CheckFn, and that the rest are unknown.
440
437
static bool checkExtractBits (const KnownBits &Known, unsigned N,
441
438
function_ref<bool (const KnownBits &)> CheckFn,
442
439
bool ByteOrderSwapped) {
@@ -459,15 +456,14 @@ static bool checkExtractBits(const KnownBits &Known, unsigned N,
459
456
CRCTable HashRecognize::genSarwateTable (const APInt &GenPoly,
460
457
bool ByteOrderSwapped) const {
461
458
unsigned BW = GenPoly.getBitWidth ();
462
- unsigned MSB = 1 << (BW - 1 );
463
459
CRCTable Table;
464
460
Table[0 ] = APInt::getZero (BW);
465
461
466
462
if (ByteOrderSwapped) {
467
463
APInt CRCInit (BW, 1 );
468
464
for (unsigned I = 1 ; I < 256 ; I <<= 1 ) {
469
465
CRCInit = CRCInit.shl (1 ) ^
470
- (( CRCInit & MSB). isZero () ? APInt::getZero (BW) : GenPoly );
466
+ (CRCInit. isSignBitSet () ? GenPoly : APInt::getZero (BW));
471
467
for (unsigned J = 0 ; J < I; ++J)
472
468
Table[I + J] = CRCInit ^ Table[J];
473
469
}
@@ -476,8 +472,7 @@ CRCTable HashRecognize::genSarwateTable(const APInt &GenPoly,
476
472
477
473
APInt CRCInit (BW, 128 );
478
474
for (unsigned I = 128 ; I; I >>= 1 ) {
479
- CRCInit = CRCInit.lshr (1 ) ^
480
- ((CRCInit & 1 ).isZero () ? APInt::getZero (BW) : GenPoly);
475
+ CRCInit = CRCInit.lshr (1 ) ^ (CRCInit[0 ] ? GenPoly : APInt::getZero (BW));
481
476
for (unsigned J = 0 ; J < 256 ; J += (I << 1 ))
482
477
Table[I + J] = CRCInit ^ Table[J];
483
478
}
@@ -597,8 +592,6 @@ HashRecognize::recognizeCRC() const {
597
592
if (SimpleRecurrence)
598
593
PhiEvolutions.emplace_back (SimpleRecurrence->Phi , SimpleRecurrence->BO );
599
594
600
- const Value *LHSAux = SimpleRecurrence ? SimpleRecurrence->Start : nullptr ;
601
-
602
595
ValueEvolution VE (TC, ByteOrderSwapped);
603
596
std::optional<KnownPhiMap> KnownPhis = VE.computeEvolutions (PhiEvolutions);
604
597
@@ -610,6 +603,7 @@ HashRecognize::recognizeCRC() const {
610
603
if (!checkExtractBits (ResultBits, TC, IsZero, ByteOrderSwapped))
611
604
return ErrBits (ResultBits, TC, ByteOrderSwapped);
612
605
606
+ const Value *LHSAux = SimpleRecurrence ? SimpleRecurrence->Start : nullptr ;
613
607
return PolynomialInfo (TC, ConditionalRecurrence->Start , GenPoly,
614
608
ComputedValue, ByteOrderSwapped, LHSAux);
615
609
}
0 commit comments