LLVM 22.0.0git
HashRecognize.h
Go to the documentation of this file.
1//===- HashRecognize.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Interface for the HashRecognize analysis, which identifies hash functions
10// that can be optimized using a lookup-table or with target-specific
11// instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_HASHRECOGNIZE_H
16#define LLVM_ANALYSIS_HASHRECOGNIZE_H
17
18#include "llvm/ADT/APInt.h"
21#include "llvm/IR/PassManager.h"
22#include "llvm/IR/Value.h"
24#include <variant>
25
26namespace llvm {
27
28class LPMUpdater;
29
30/// A tuple of bits that are expected to be zero, number N of them expected to
31/// be zero, with a boolean indicating whether it's the top or bottom N bits
32/// expected to be zero.
33using ErrBits = std::tuple<KnownBits, unsigned, bool>;
34
35/// A custom std::array with 256 entries, that also has a print function.
36struct CRCTable : public std::array<APInt, 256> {
37 void print(raw_ostream &OS) const;
38
39#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
40 LLVM_DUMP_METHOD void dump() const;
41#endif
42};
43
44/// The structure that is returned when a polynomial algorithm was recognized by
45/// the analysis. Currently, only the CRC algorithm is recognized.
47 // The small constant trip-count of the analyzed loop.
48 unsigned TripCount;
49
50 // The LHS in a polynomial operation, or the initial variable of the
51 // computation, since all polynomial operations must have a constant RHS,
52 // which is the generating polynomial. It is the LHS of the polynomial
53 // division in the case of CRC. Since polynomial division is an XOR in
54 // GF(2^m), this variable must be XOR'ed with RHS in a loop to yield the
55 // ComputedValue.
57
58 // The generating polynomial, or the RHS of the polynomial division in the
59 // case of CRC.
61
62 // The final computed value. This is a remainder of a polynomial division in
63 // the case of CRC, which must be zero.
65
66 // Set to true in the case of big-endian.
68
69 // An optional auxiliary checksum that augments the LHS. In the case of CRC,
70 // it is XOR'ed with the LHS, so that the computation's final remainder is
71 // zero.
73
74 PolynomialInfo(unsigned TripCount, Value *LHS, const APInt &RHS,
76 Value *LHSAux = nullptr);
77};
78
79/// The analysis.
81 const Loop &L;
83
84public:
85 HashRecognize(const Loop &L, ScalarEvolution &SE);
86
87 // The main analysis entry points.
88 std::variant<PolynomialInfo, ErrBits, StringRef> recognizeCRC() const;
89 std::optional<PolynomialInfo> getResult() const;
90
91 // Auxilary entry point after analysis to interleave the generating polynomial
92 // and return a 256-entry CRC table.
93 static CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped);
94
95 void print(raw_ostream &OS) const;
96
97#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
98 LLVM_DUMP_METHOD void dump() const;
99#endif
100};
101
103 : public PassInfoMixin<HashRecognizePrinterPass> {
104 raw_ostream &OS;
105
106public:
110};
111} // namespace llvm
112
113#endif
This file implements a class to represent arbitrary precision integral constant values and operations...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
This header defines various interfaces for pass management in LLVM.
This header provides classes for managing per-loop analyses.
raw_pwrite_stream & OS
Class for arbitrary precision integers.
Definition: APInt.h:78
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &)
HashRecognizePrinterPass(raw_ostream &OS)
The analysis.
Definition: HashRecognize.h:80
static CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped)
Generate a lookup table of 256 entries by interleaving the generating polynomial.
std::optional< PolynomialInfo > getResult() const
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &OS) const
std::variant< PolynomialInfo, ErrBits, StringRef > recognizeCRC() const
The main entry point for analyzing a loop and recognizing the CRC algorithm.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:40
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
The main scalar evolution driver.
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::tuple< KnownBits, unsigned, bool > ErrBits
A tuple of bits that are expected to be zero, number N of them expected to be zero,...
Definition: HashRecognize.h:33
A custom std::array with 256 entries, that also has a print function.
Definition: HashRecognize.h:36
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &OS) const
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
The structure that is returned when a polynomial algorithm was recognized by the analysis.
Definition: HashRecognize.h:46