Skip to content

Commit a41485a

Browse files
artagnonpfusik
andcommitted
[HashRecognize] Fix bug; forbid stray uses
Co-authored-by: Piotr Fusik <[email protected]>
1 parent c0270eb commit a41485a

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ HashRecognize::recognizeCRC() const {
591591
if (isBigEndianBitShift(SimpleRecurrence.BO, SE) != ByteOrderSwapped)
592592
return "Loop with non-unit bitshifts";
593593

594+
// Ensure that the PHIs have exactly two uses:
595+
// the bit-shift, and the XOR (or a cast feeding into the XOR).
596+
if (!ConditionalRecurrence.Phi->hasNUses(2) ||
597+
!SimpleRecurrence.Phi->hasNUses(2))
598+
return "Recurrences have stray uses";
599+
594600
// Check that the SelectInst ConditionalRecurrence.Step is conditional on
595601
// the XOR of SimpleRecurrence.Phi and ConditionalRecurrence.Phi.
596602
if (!isConditionalOnXorOfPHIs(cast<SelectInst>(ConditionalRecurrence.Step),

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ exit: ; preds = %loop
923923
define i16 @not.crc.dead.msg.no.use(i8 %msg, i16 %checksum) {
924924
; CHECK-LABEL: 'not.crc.dead.msg.no.use'
925925
; CHECK-NEXT: Did not find a hash algorithm
926-
; CHECK-NEXT: Reason: Recurrences not intertwined with XOR
926+
; CHECK-NEXT: Reason: Recurrences have stray uses
927927
;
928928
entry:
929929
br label %loop
@@ -979,7 +979,7 @@ exit: ; preds = %loop
979979
define i16 @not.crc.dead.msg.xor.notin.select.chain(i16 %msg, i16 %checksum) {
980980
; CHECK-LABEL: 'not.crc.dead.msg.xor.notin.select.chain'
981981
; CHECK-NEXT: Did not find a hash algorithm
982-
; CHECK-NEXT: Reason: Recurrences not intertwined with XOR
982+
; CHECK-NEXT: Reason: Recurrences have stray uses
983983
;
984984
entry:
985985
br label %loop
@@ -1004,6 +1004,64 @@ exit: ; preds = %loop
10041004
ret i16 %crc.next
10051005
}
10061006

1007+
define i16 @not.crc.bad.xor.crc.data(i16 %msg, i16 %checksum) {
1008+
; CHECK-LABEL: 'not.crc.bad.xor.crc.data'
1009+
; CHECK-NEXT: Did not find a hash algorithm
1010+
; CHECK-NEXT: Reason: Recurrences have stray uses
1011+
;
1012+
entry:
1013+
br label %loop
1014+
1015+
loop: ; preds = %loop, %entry
1016+
%iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ]
1017+
%crc = phi i16 [ %checksum, %entry ], [ %crc.next, %loop ]
1018+
%data = phi i16 [ %msg, %entry ], [ %data.next, %loop ]
1019+
%xor.crc.data = xor i16 %crc, %data
1020+
%mul.corrupt = mul i16 %xor.crc.data, 0
1021+
%xor.crc.data.corrupt = xor i16 %mul.corrupt, %crc
1022+
%and.crc.data = and i16 %xor.crc.data.corrupt, 1
1023+
%data.next = lshr i16 %data, 1
1024+
%check.sb = icmp eq i16 %and.crc.data, 0
1025+
%crc.lshr = lshr i16 %crc, 1
1026+
%crc.xor = xor i16 %crc.lshr, -24575
1027+
%crc.next = select i1 %check.sb, i16 %crc.lshr, i16 %crc.xor
1028+
%iv.next = add nuw nsw i8 %iv, 1
1029+
%exit.cond = icmp samesign ult i8 %iv, 15
1030+
br i1 %exit.cond, label %loop, label %exit
1031+
1032+
exit: ; preds = %loop
1033+
ret i16 %crc.next
1034+
}
1035+
1036+
define i16 @not.crc.dead.msg.or.zero(i16 %msg, i16 %checksum) {
1037+
; CHECK-LABEL: 'not.crc.dead.msg.or.zero'
1038+
; CHECK-NEXT: Did not find a hash algorithm
1039+
; CHECK-NEXT: Reason: Recurrences have stray uses
1040+
;
1041+
entry:
1042+
br label %loop
1043+
1044+
loop: ; preds = %loop, %entry
1045+
%iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ]
1046+
%crc = phi i16 [ %checksum, %entry ], [ %crc.next, %loop ]
1047+
%data = phi i16 [ %msg, %entry ], [ %data.next, %loop ]
1048+
%xor.crc.data = xor i16 %crc, %data
1049+
%mul.corrupt = mul i16 %xor.crc.data, 0
1050+
%or.crc.data.corrupt = or i16 %mul.corrupt, %crc
1051+
%and.crc.data = and i16 %or.crc.data.corrupt, 1
1052+
%data.next = lshr i16 %data, 1
1053+
%check.sb = icmp eq i16 %and.crc.data, 0
1054+
%crc.lshr = lshr i16 %crc, 1
1055+
%crc.xor = xor i16 %crc.lshr, -24575
1056+
%crc.next = select i1 %check.sb, i16 %crc.lshr, i16 %crc.xor
1057+
%iv.next = add nuw nsw i8 %iv, 1
1058+
%exit.cond = icmp samesign ult i8 %iv, 15
1059+
br i1 %exit.cond, label %loop, label %exit
1060+
1061+
exit: ; preds = %loop
1062+
ret i16 %crc.next
1063+
}
1064+
10071065
define i16 @not.crc.unknown.value(i16 %msg, i16 %checksum, i16 %corrupt) {
10081066
; CHECK-LABEL: 'not.crc.unknown.value'
10091067
; CHECK-NEXT: Did not find a hash algorithm

0 commit comments

Comments
 (0)