Skip to content

Commit 08d6c2d

Browse files
committed
[HashRecognize] Forbid sexts
1 parent 4e8df09 commit 08d6c2d

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,14 @@ m_ZExtOrSExtOrSelf(const OpTy &Op) {
22832283
return m_CombineOr(m_ZExtOrSExt(Op), Op);
22842284
}
22852285

2286+
template <typename OpTy>
2287+
inline match_combine_or<match_combine_or<CastInst_match<OpTy, ZExtInst>,
2288+
CastInst_match<OpTy, TruncInst>>,
2289+
OpTy>
2290+
m_ZExtOrTruncOrSelf(const OpTy &Op) {
2291+
return m_CombineOr(m_CombineOr(m_ZExt(Op), m_Trunc(Op)), Op);
2292+
}
2293+
22862294
template <typename OpTy>
22872295
inline CastInst_match<OpTy, UIToFPInst> m_UIToFP(const OpTy &Op) {
22882296
return CastInst_match<OpTy, UIToFPInst>(Op);

llvm/lib/Analysis/HashRecognize.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ isSignificantBitCheckWellFormed(const RecurrenceInfo &ConditionalRecurrence,
178178

179179
// Match predicate with or without a SimpleRecurrence (the corresponding data
180180
// is LHSAux).
181-
auto MatchPred =
182-
m_CombineOr(m_Specific(ConditionalRecurrence.Phi),
183-
m_c_Xor(m_CastOrSelf(m_Specific(ConditionalRecurrence.Phi)),
184-
m_CastOrSelf(m_Specific(SimpleRecurrence.Phi))));
181+
auto MatchPred = m_CombineOr(
182+
m_Specific(ConditionalRecurrence.Phi),
183+
m_c_Xor(m_ZExtOrTruncOrSelf(m_Specific(ConditionalRecurrence.Phi)),
184+
m_ZExtOrTruncOrSelf(m_Specific(SimpleRecurrence.Phi))));
185185
bool LWellFormed = ByteOrderSwapped ? match(L, MatchPred)
186186
: match(L, m_c_And(MatchPred, m_One()));
187187
if (!LWellFormed)
@@ -401,8 +401,8 @@ static bool isConditionalOnXorOfPHIs(const SelectInst *SI, const PHINode *P1,
401401
continue;
402402

403403
// If we match an XOR of the two PHIs ignoring casts, we're done.
404-
if (match(I, m_c_Xor(m_CastOrSelf(m_Specific(P1)),
405-
m_CastOrSelf(m_Specific(P2)))))
404+
if (match(I, m_c_Xor(m_ZExtOrTruncOrSelf(m_Specific(P1)),
405+
m_ZExtOrTruncOrSelf(m_Specific(P2)))))
406406
return true;
407407

408408
// Continue along the use-def chain.

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,53 @@ exit: ; preds = %loop
427427
ret i32 %crc.next
428428
}
429429

430+
define i16 @crc16.be.tc8.zext.data(i8 %msg, i16 %checksum) {
431+
; CHECK-LABEL: 'crc16.be.tc8.zext.data'
432+
; CHECK-NEXT: Found big-endian CRC-16 loop with trip count 8
433+
; CHECK-NEXT: Initial CRC: i16 %checksum
434+
; CHECK-NEXT: Generating polynomial: 258
435+
; CHECK-NEXT: Computed CRC: %crc.next = select i1 %check.sb, i16 %crc.shl, i16 %crc.xor
436+
; CHECK-NEXT: Auxiliary data: i8 %msg
437+
; CHECK-NEXT: Computed CRC lookup table:
438+
; CHECK-NEXT: 0 258 516 774 1032 1290 1548 1806 2064 2322 2580 2838 3096 3354 3612 3870
439+
; CHECK-NEXT: 4128 4386 4644 4902 5160 5418 5676 5934 6192 6450 6708 6966 7224 7482 7740 7998
440+
; CHECK-NEXT: 8256 8514 8772 9030 9288 9546 9804 10062 10320 10578 10836 11094 11352 11610 11868 12126
441+
; CHECK-NEXT: 12384 12642 12900 13158 13416 13674 13932 14190 14448 14706 14964 15222 15480 15738 15996 16254
442+
; CHECK-NEXT: 16512 16770 17028 17286 17544 17802 18060 18318 18576 18834 19092 19350 19608 19866 20124 20382
443+
; CHECK-NEXT: 20640 20898 21156 21414 21672 21930 22188 22446 22704 22962 23220 23478 23736 23994 24252 24510
444+
; CHECK-NEXT: 24768 25026 25284 25542 25800 26058 26316 26574 26832 27090 27348 27606 27864 28122 28380 28638
445+
; CHECK-NEXT: 28896 29154 29412 29670 29928 30186 30444 30702 30960 31218 31476 31734 31992 32250 32508 32766
446+
; CHECK-NEXT: 33024 32770 33540 33286 34056 33802 34572 34318 35088 34834 35604 35350 36120 35866 36636 36382
447+
; CHECK-NEXT: 37152 36898 37668 37414 38184 37930 38700 38446 39216 38962 39732 39478 40248 39994 40764 40510
448+
; CHECK-NEXT: 41280 41026 41796 41542 42312 42058 42828 42574 43344 43090 43860 43606 44376 44122 44892 44638
449+
; CHECK-NEXT: 45408 45154 45924 45670 46440 46186 46956 46702 47472 47218 47988 47734 48504 48250 49020 48766
450+
; CHECK-NEXT: 49536 49282 50052 49798 50568 50314 51084 50830 51600 51346 52116 51862 52632 52378 53148 52894
451+
; CHECK-NEXT: 53664 53410 54180 53926 54696 54442 55212 54958 55728 55474 56244 55990 56760 56506 57276 57022
452+
; CHECK-NEXT: 57792 57538 58308 58054 58824 58570 59340 59086 59856 59602 60372 60118 60888 60634 61404 61150
453+
; CHECK-NEXT: 61920 61666 62436 62182 62952 62698 63468 63214 63984 63730 64500 64246 65016 64762 65532 65278
454+
;
455+
entry:
456+
br label %loop
457+
458+
loop: ; preds = %loop, %entry
459+
%iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ]
460+
%data = phi i8 [ %msg, %entry ], [ %data.next, %loop ]
461+
%crc = phi i16 [ %checksum, %entry ], [ %crc.next, %loop ]
462+
%data.ext = zext i8 %data to i16
463+
%xor.crc.data = xor i16 %crc, %data.ext
464+
%check.sb = icmp sge i16 %xor.crc.data, 0
465+
%crc.shl = shl i16 %crc, 1
466+
%crc.xor = xor i16 %crc.shl, 258
467+
%crc.next = select i1 %check.sb, i16 %crc.shl, i16 %crc.xor
468+
%data.next = shl i8 %data, 1
469+
%iv.next = add nuw nsw i8 %iv, 1
470+
%exit.cond = icmp samesign ult i8 %iv, 7
471+
br i1 %exit.cond, label %loop, label %exit
472+
473+
exit: ; preds = %loop
474+
ret i16 %crc.next
475+
}
476+
430477
; Negative tests
431478

432479
define i16 @not.crc.non.const.tc(i16 %crc.init, i32 %loop.limit) {
@@ -1320,6 +1367,34 @@ exit: ; preds = %loop
13201367
ret i16 %crc.next
13211368
}
13221369

1370+
define i16 @not.crc.bad.cast.sext(i8 %msg, i16 %checksum) {
1371+
; CHECK-LABEL: 'not.crc.bad.cast.sext'
1372+
; CHECK-NEXT: Did not find a hash algorithm
1373+
; CHECK-NEXT: Reason: Recurrences not intertwined with XOR
1374+
;
1375+
entry:
1376+
br label %loop
1377+
1378+
loop: ; preds = %loop, %entry
1379+
%iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ]
1380+
%data = phi i8 [ %msg, %entry ], [ %data.next, %loop ]
1381+
%crc = phi i16 [ %checksum, %entry ], [ %crc.next, %loop ]
1382+
%data.ext = sext i8 %data to i16
1383+
%xor.crc.data = xor i16 %crc, %data.ext
1384+
%check.sb = icmp sge i16 %xor.crc.data, 0
1385+
%crc.shl = shl i16 %crc, 1
1386+
%crc.xor = xor i16 %crc.shl, 258
1387+
%crc.next = select i1 %check.sb, i16 %crc.shl, i16 %crc.xor
1388+
%data.next = shl i8 %data, 1
1389+
%iv.next = add nuw nsw i8 %iv, 1
1390+
%exit.cond = icmp samesign ult i8 %iv, 7
1391+
br i1 %exit.cond, label %loop, label %exit
1392+
1393+
exit: ; preds = %loop
1394+
ret i16 %crc.next
1395+
}
1396+
1397+
13231398
define i16 @not.crc.sb.check.patternmatch.fail(i16 %crc.init) {
13241399
; CHECK-LABEL: 'not.crc.sb.check.patternmatch.fail'
13251400
; CHECK-NEXT: Did not find a hash algorithm

0 commit comments

Comments
 (0)