Skip to content

Commit a695d6b

Browse files
authored
[HashRecognize] Check TC against bitwidth of LHSAux (#144881)
The trip-count of a CRC algorithm can legitimately be greater than the bitwidth of the result: what it cannot exceed is the bitwidth of the data, or LHSAux. crc8.le.tc16 is now successfully recognized as a CRC algorithm.
1 parent 77f0f81 commit a695d6b

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,10 @@ KnownBits ValueEvolution::compute(const Value *V) {
247247
}
248248

249249
bool ValueEvolution::computeEvolutions(ArrayRef<PhiStepPair> PhiEvolutions) {
250-
for (unsigned I = 0; I < TripCount; ++I) {
251-
for (auto [Phi, Step] : PhiEvolutions) {
252-
KnownBits KnownAtIter = computeInstr(Step);
253-
if (KnownAtIter.getBitWidth() < I + 1) {
254-
ErrStr = "Loop iterations exceed bitwidth of result";
255-
return false;
256-
}
257-
KnownPhis.emplace_or_assign(Phi, KnownAtIter);
258-
}
259-
}
250+
for (unsigned I = 0; I < TripCount; ++I)
251+
for (auto [Phi, Step] : PhiEvolutions)
252+
KnownPhis.emplace_or_assign(Phi, computeInstr(Step));
253+
260254
return ErrStr.empty();
261255
}
262256

@@ -605,6 +599,13 @@ HashRecognize::recognizeCRC() const {
605599
return "Recurrences not intertwined with XOR";
606600
}
607601

602+
// Make sure that the TC doesn't exceed the bitwidth of LHSAux, or LHS.
603+
Value *LHS = ConditionalRecurrence.Start;
604+
Value *LHSAux = SimpleRecurrence ? SimpleRecurrence.Start : nullptr;
605+
if (TC > (LHSAux ? LHSAux->getType()->getIntegerBitWidth()
606+
: LHS->getType()->getIntegerBitWidth()))
607+
return "Loop iterations exceed bitwidth of data";
608+
608609
// Make sure that the computed value is used in the exit block: this should be
609610
// true even if it is only really used in an outer loop's exit block, since
610611
// the loop is in LCSSA form.
@@ -633,13 +634,13 @@ HashRecognize::recognizeCRC() const {
633634
return VE.getError();
634635
KnownBits ResultBits = VE.KnownPhis.at(ConditionalRecurrence.Phi);
635636

637+
unsigned N = std::min(TC, ResultBits.getBitWidth());
636638
auto IsZero = [](const KnownBits &K) { return K.isZero(); };
637-
if (!checkExtractBits(ResultBits, TC, IsZero, *ByteOrderSwapped))
639+
if (!checkExtractBits(ResultBits, N, IsZero, *ByteOrderSwapped))
638640
return ErrBits(ResultBits, TC, *ByteOrderSwapped);
639641

640-
Value *LHSAux = SimpleRecurrence ? SimpleRecurrence.Start : nullptr;
641-
return PolynomialInfo(TC, ConditionalRecurrence.Start, GenPoly, ComputedValue,
642-
*ByteOrderSwapped, LHSAux);
642+
return PolynomialInfo(TC, LHS, GenPoly, ComputedValue, *ByteOrderSwapped,
643+
LHSAux);
643644
}
644645

645646
void CRCTable::print(raw_ostream &OS) const {

llvm/test/Analysis/HashRecognize/cyclic-redundancy-check.ll

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,28 @@ exit: ; preds = %loop
146146

147147
define i8 @crc8.le.tc16(i16 %msg, i8 %checksum) {
148148
; CHECK-LABEL: 'crc8.le.tc16'
149-
; CHECK-NEXT: Did not find a hash algorithm
150-
; CHECK-NEXT: Reason: Loop iterations exceed bitwidth of result
149+
; CHECK-NEXT: Found little-endian CRC-8 loop with trip count 16
150+
; CHECK-NEXT: Initial CRC: i8 %checksum
151+
; CHECK-NEXT: Generating polynomial: 29
152+
; CHECK-NEXT: Computed CRC: %crc.next = select i1 %check.sb, i8 %crc.lshr, i8 %crc.xor
153+
; CHECK-NEXT: Auxiliary data: i16 %msg
154+
; CHECK-NEXT: Computed CRC lookup table:
155+
; CHECK-NEXT: 0 9 18 27 31 22 13 4 5 12 23 30 26 19 8 1
156+
; CHECK-NEXT: 10 3 24 17 21 28 7 14 15 6 29 20 16 25 2 11
157+
; CHECK-NEXT: 20 29 6 15 11 2 25 16 17 24 3 10 14 7 28 21
158+
; CHECK-NEXT: 30 23 12 5 1 8 19 26 27 18 9 0 4 13 22 31
159+
; CHECK-NEXT: 19 26 1 8 12 5 30 23 22 31 4 13 9 0 27 18
160+
; CHECK-NEXT: 25 16 11 2 6 15 20 29 28 21 14 7 3 10 17 24
161+
; CHECK-NEXT: 7 14 21 28 24 17 10 3 2 11 16 25 29 20 15 6
162+
; CHECK-NEXT: 13 4 31 22 18 27 0 9 8 1 26 19 23 30 5 12
163+
; CHECK-NEXT: 29 20 15 6 2 11 16 25 24 17 10 3 7 14 21 28
164+
; CHECK-NEXT: 23 30 5 12 8 1 26 19 18 27 0 9 13 4 31 22
165+
; CHECK-NEXT: 9 0 27 18 22 31 4 13 12 5 30 23 19 26 1 8
166+
; CHECK-NEXT: 3 10 17 24 28 21 14 7 6 15 20 29 25 16 11 2
167+
; CHECK-NEXT: 14 7 28 21 17 24 3 10 11 2 25 16 20 29 6 15
168+
; CHECK-NEXT: 4 13 22 31 27 18 9 0 1 8 19 26 30 23 12 5
169+
; CHECK-NEXT: 26 19 8 1 5 12 23 30 31 22 13 4 0 9 18 27
170+
; CHECK-NEXT: 16 25 2 11 15 6 29 20 21 28 7 14 10 3 24 17
151171
;
152172
entry:
153173
br label %loop
@@ -676,27 +696,51 @@ exit: ; preds = %loop
676696
ret i16 %crc.next
677697
}
678698

679-
define i16 @not.crc.excess.tc(i16 %msg, i16 %checksum) {
699+
define i16 @not.crc.excess.tc(i8 %msg, i16 %checksum) {
680700
; CHECK-LABEL: 'not.crc.excess.tc'
681701
; CHECK-NEXT: Did not find a hash algorithm
682-
; CHECK-NEXT: Reason: Loop iterations exceed bitwidth of result
702+
; CHECK-NEXT: Reason: Loop iterations exceed bitwidth of data
683703
;
684704
entry:
685705
br label %loop
686706

687707
loop: ; preds = %loop, %entry
688708
%iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ]
689709
%crc = phi i16 [ %checksum, %entry ], [ %crc.next, %loop ]
690-
%data = phi i16 [ %msg, %entry ], [ %data.next, %loop ]
691-
%xor.crc.data = xor i16 %crc, %data
692-
%and.crc.data = and i16 %xor.crc.data, 1
693-
%data.next = lshr i16 %data, 1
694-
%check.sb = icmp eq i16 %and.crc.data, 0
710+
%data = phi i8 [ %msg, %entry ], [ %data.next, %loop ]
711+
%crc.trunc = trunc i16 %crc to i8
712+
%xor.crc.data = xor i8 %crc.trunc, %data
713+
%and.crc.data = and i8 %xor.crc.data, 1
714+
%data.next = lshr i8 %data, 1
715+
%check.sb = icmp eq i8 %and.crc.data, 0
695716
%crc.lshr = lshr i16 %crc, 1
696717
%crc.xor = xor i16 %crc.lshr, -24575
697718
%crc.next = select i1 %check.sb, i16 %crc.lshr, i16 %crc.xor
698719
%iv.next = add nuw nsw i8 %iv, 1
699-
%exit.cond = icmp samesign ult i8 %iv, 31
720+
%exit.cond = icmp samesign ult i8 %iv, 15
721+
br i1 %exit.cond, label %loop, label %exit
722+
723+
exit: ; preds = %loop
724+
ret i16 %crc.next
725+
}
726+
727+
define i16 @not.crc.init.arg.excess.tc(i16 %crc.init) {
728+
; CHECK-LABEL: 'not.crc.init.arg.excess.tc'
729+
; CHECK-NEXT: Did not find a hash algorithm
730+
; CHECK-NEXT: Reason: Loop iterations exceed bitwidth of data
731+
;
732+
entry:
733+
br label %loop
734+
735+
loop: ; preds = %loop, %entry
736+
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
737+
%crc = phi i16 [ %crc.init, %entry ], [ %crc.next, %loop ]
738+
%crc.shl = shl i16 %crc, 1
739+
%crc.xor = xor i16 %crc.shl, 4129
740+
%check.sb = icmp slt i16 %crc, 0
741+
%crc.next = select i1 %check.sb, i16 %crc.xor, i16 %crc.shl
742+
%iv.next = add nuw nsw i32 %iv, 1
743+
%exit.cond = icmp samesign ult i32 %iv, 31
700744
br i1 %exit.cond, label %loop, label %exit
701745

702746
exit: ; preds = %loop

0 commit comments

Comments
 (0)