Skip to content

[HashRecognize] Tighten pre-conditions for analysis #144757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions llvm/lib/Analysis/HashRecognize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,14 @@ std::variant<PolynomialInfo, ErrBits, StringRef>
HashRecognize::recognizeCRC() const {
if (!L.isInnermost())
return "Loop is not innermost";
unsigned TC = SE.getSmallConstantMaxTripCount(&L);
if (!TC || TC > 256)
return "Unable to find a small constant trip count";
BasicBlock *Latch = L.getLoopLatch();
BasicBlock *Exit = L.getExitBlock();
const PHINode *IndVar = L.getCanonicalInductionVariable();
if (!Latch || !Exit || !IndVar)
if (!Latch || !Exit || !IndVar || L.getNumBlocks() != 1)
return "Loop not in canonical form";
unsigned TC = SE.getSmallConstantTripCount(&L);
if (!TC || TC > 256 || TC % 8)
return "Unable to find a small constant byte-multiple trip count";

auto R = getRecurrences(Latch, IndVar, L);
if (!R)
Expand Down
62 changes: 57 additions & 5 deletions llvm/test/Analysis/HashRecognize/cyclic-redundancy-check.ll
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ exit: ; preds = %loop
define i16 @not.crc.non.const.tc(i16 %crc.init, i32 %loop.limit) {
; CHECK-LABEL: 'not.crc.non.const.tc'
; CHECK-NEXT: Did not find a hash algorithm
; CHECK-NEXT: Reason: Unable to find a small constant trip count
; CHECK-NEXT: Reason: Unable to find a small constant byte-multiple trip count
;
entry:
br label %loop
Expand All @@ -404,8 +404,31 @@ exit: ; preds = %loop
ret i16 %crc.next
}

define i16 @not.crc.non.canonical.loop(i16 %crc.init) {
; CHECK-LABEL: 'not.crc.non.canonical.loop'
define i16 @not.crc.non.canonical.not.multiple.8(i16 %crc.init) {
; CHECK-LABEL: 'not.crc.non.canonical.not.multiple.8'
; CHECK-NEXT: Did not find a hash algorithm
; CHECK-NEXT: Reason: Unable to find a small constant byte-multiple trip count
;
entry:
br label %loop

loop: ; preds = %loop, %entry
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
%crc = phi i16 [ %crc.init, %entry ], [ %crc.next, %loop ]
%crc.shl = shl i16 %crc, 1
%crc.xor = xor i16 %crc.shl, 4129
%check.sb = icmp slt i16 %crc, 0
%crc.next = select i1 %check.sb, i16 %crc.xor, i16 %crc.shl
%iv.next = add nuw nsw i32 %iv, 1
%exit.cond = icmp samesign eq i32 %iv, 3
br i1 %exit.cond, label %exit, label %loop

exit: ; preds = %loop
ret i16 %crc.next
}

define i16 @not.crc.non.canonical.loop.countdown(i16 %crc.init) {
; CHECK-LABEL: 'not.crc.non.canonical.loop.countdown'
; CHECK-NEXT: Did not find a hash algorithm
; CHECK-NEXT: Reason: Loop not in canonical form
;
Expand All @@ -427,10 +450,39 @@ exit: ; preds = %loop
ret i16 %crc.next
}

define i16 @not.crc.non.canonical.loop.multiple.blocks(i16 %crc.init) {
; CHECK-LABEL: 'not.crc.non.canonical.loop.multiple.blocks'
; CHECK-NEXT: Did not find a hash algorithm
; CHECK-NEXT: Reason: Loop not in canonical form
;
entry:
br label %loop

loop: ; preds = %loop, %entry
%iv = phi i32 [ 0, %entry ], [ %iv.next, %continue ]
%crc = phi i16 [ %crc.init, %entry ], [ %crc.next, %continue ]
%check.sb = icmp slt i16 %crc, 0
%crc.shl = shl i16 %crc, 1
br i1 %check.sb, label %xor, label %continue

xor:
%crc.xor = xor i16 %crc.shl, 4129
br label %continue

continue:
%crc.next = phi i16 [ %crc.xor, %xor ], [ %crc.shl, %loop ]
%iv.next = add nuw nsw i32 %iv, 1
%exit.cond = icmp samesign eq i32 %iv, 7
br i1 %exit.cond, label %exit, label %loop

exit: ; preds = %loop
ret i16 %crc.next
}

define i16 @not.crc.tc.limit(i16 %crc.init) {
; CHECK-LABEL: 'not.crc.tc.limit'
; CHECK-NEXT: Did not find a hash algorithm
; CHECK-NEXT: Reason: Unable to find a small constant trip count
; CHECK-NEXT: Reason: Unable to find a small constant byte-multiple trip count
;
entry:
br label %loop
Expand Down Expand Up @@ -617,7 +669,7 @@ loop: ; preds = %loop, %entry
%crc.xor = xor i16 %crc.lshr, -24575
%crc.next = select i1 %check.sb, i16 %crc.lshr, i16 %crc.xor
%iv.next = add nuw nsw i8 %iv, 1
%exit.cond = icmp samesign ult i8 %iv, 20
%exit.cond = icmp samesign ult i8 %iv, 31
br i1 %exit.cond, label %loop, label %exit

exit: ; preds = %loop
Expand Down
Loading