Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 7 additions & 14 deletions llvm/include/llvm/Analysis/HashRecognize.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ struct PolynomialInfo {
// Set to true in the case of big-endian.
bool ByteOrderSwapped;

// A function_ref to generate the Sarwate lookup-table, which can be used to
// optimize CRC in the absence of target-specific instructions.
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable;

// An optional auxiliary checksum that augments the LHS. In the case of CRC,
// it is XOR'ed with the LHS, so that the computation's final remainder is
// zero.
Value *LHSAux;

PolynomialInfo(unsigned TripCount, Value *LHS, const APInt &RHS,
Value *ComputedValue, bool ByteOrderSwapped,
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable,
Value *LHSAux = nullptr);
};

Expand All @@ -84,12 +89,9 @@ class HashRecognize {
public:
HashRecognize(const Loop &L, ScalarEvolution &SE);

// The main analysis entry point.
// The main analysis entry points.
std::variant<PolynomialInfo, ErrBits, StringRef> recognizeCRC() const;

// Auxilary entry point after analysis to interleave the generating polynomial
// and return a 256-entry CRC table.
CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped) const;
std::optional<PolynomialInfo> getResult() const;

void print(raw_ostream &OS) const;

Expand All @@ -107,15 +109,6 @@ class HashRecognizePrinterPass
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &);
};

class HashRecognizeAnalysis : public AnalysisInfoMixin<HashRecognizeAnalysis> {
friend AnalysisInfoMixin<HashRecognizeAnalysis>;
static AnalysisKey Key;

public:
using Result = HashRecognize;
Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
};
} // namespace llvm

#endif
33 changes: 17 additions & 16 deletions llvm/lib/Analysis/HashRecognize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,13 @@ getRecurrences(BasicBlock *LoopLatch, const PHINode *IndVar, const Loop &L) {
return std::make_pair(SimpleRecurrence, ConditionalRecurrence);
}

PolynomialInfo::PolynomialInfo(unsigned TripCount, Value *LHS, const APInt &RHS,
Value *ComputedValue, bool ByteOrderSwapped,
Value *LHSAux)
PolynomialInfo::PolynomialInfo(
unsigned TripCount, Value *LHS, const APInt &RHS, Value *ComputedValue,
bool ByteOrderSwapped,
function_ref<CRCTable(const APInt &, bool)> GenSarwateTable, Value *LHSAux)
: TripCount(TripCount), LHS(LHS), RHS(RHS), ComputedValue(ComputedValue),
ByteOrderSwapped(ByteOrderSwapped), LHSAux(LHSAux) {}
ByteOrderSwapped(ByteOrderSwapped), GenSarwateTable(GenSarwateTable),
LHSAux(LHSAux) {}

/// In the big-endian case, checks the bottom N bits against CheckFn, and that
/// the rest are unknown. In the little-endian case, checks the top N bits
Expand All @@ -471,8 +473,7 @@ static bool checkExtractBits(const KnownBits &Known, unsigned N,
/// Generate a lookup table of 256 entries by interleaving the generating
/// polynomial. The optimization technique of table-lookup for CRC is also
/// called the Sarwate algorithm.
CRCTable HashRecognize::genSarwateTable(const APInt &GenPoly,
bool ByteOrderSwapped) const {
static CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped) {
unsigned BW = GenPoly.getBitWidth();
CRCTable Table;
Table[0] = APInt::getZero(BW);
Expand Down Expand Up @@ -625,7 +626,7 @@ HashRecognize::recognizeCRC() const {

Value *LHSAux = SimpleRecurrence ? SimpleRecurrence.Start : nullptr;
return PolynomialInfo(TC, ConditionalRecurrence.Start, GenPoly, ComputedValue,
*ByteOrderSwapped, LHSAux);
*ByteOrderSwapped, genSarwateTable, LHSAux);
}

void CRCTable::print(raw_ostream &OS) const {
Expand Down Expand Up @@ -679,27 +680,27 @@ void HashRecognize::print(raw_ostream &OS) const {
OS << "\n";
}
OS.indent(2) << "Computed CRC lookup table:\n";
genSarwateTable(Info.RHS, Info.ByteOrderSwapped).print(OS);
Info.GenSarwateTable(Info.RHS, Info.ByteOrderSwapped).print(OS);
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void HashRecognize::dump() const { print(dbgs()); }
#endif

std::optional<PolynomialInfo> HashRecognize::getResult() const {
auto Res = HashRecognize(L, SE).recognizeCRC();
if (std::holds_alternative<PolynomialInfo>(Res))
return std::get<PolynomialInfo>(Res);
return std::nullopt;
}

HashRecognize::HashRecognize(const Loop &L, ScalarEvolution &SE)
: L(L), SE(SE) {}

PreservedAnalyses HashRecognizePrinterPass::run(Loop &L,
LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR,
LPMUpdater &) {
AM.getResult<HashRecognizeAnalysis>(L, AR).print(OS);
HashRecognize(L, AR.SE).print(OS);
return PreservedAnalyses::all();
}

HashRecognize HashRecognizeAnalysis::run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR) {
return {L, AR.SE};
}

AnalysisKey HashRecognizeAnalysis::Key;
1 change: 0 additions & 1 deletion llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ LOOPNEST_PASS("no-op-loopnest", NoOpLoopNestPass())
#define LOOP_ANALYSIS(NAME, CREATE_PASS)
#endif
LOOP_ANALYSIS("ddg", DDGAnalysis())
LOOP_ANALYSIS("hash-recognize", HashRecognizeAnalysis())
LOOP_ANALYSIS("iv-users", IVUsersAnalysis())
LOOP_ANALYSIS("no-op-loop", NoOpLoopAnalysis())
LOOP_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
Expand Down