|
| 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" |
| 19 | +#include "llvm/Analysis/LoopAnalysisManager.h" |
| 20 | +#include "llvm/Analysis/ScalarEvolution.h" |
| 21 | +#include "llvm/IR/PassManager.h" |
| 22 | +#include "llvm/IR/Value.h" |
| 23 | +#include "llvm/Support/KnownBits.h" |
| 24 | +#include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 25 | + |
| 26 | +namespace llvm { |
| 27 | +/// A tuple of bits that are expected to be zero, number N of them expected to |
| 28 | +/// be zero, with a boolean indicating whether it's the top or bottom N bits |
| 29 | +/// expected to be zero. |
| 30 | +using ErrBits = std::tuple<KnownBits, unsigned, bool>; |
| 31 | + |
| 32 | +/// A custom std::array with 256 entries, that also has a print function. |
| 33 | +struct CRCTable : public std::array<APInt, 256> { |
| 34 | + void print(raw_ostream &OS) const; |
| 35 | +}; |
| 36 | + |
| 37 | +/// The structure that is returned when a polynomial algorithm was recognized by |
| 38 | +/// the analysis. Currently, only the CRC algorithm is recognized. |
| 39 | +struct PolynomialInfo { |
| 40 | + unsigned TripCount; |
| 41 | + const Value *LHS; |
| 42 | + APInt RHS; |
| 43 | + const Value *ComputedValue; |
| 44 | + bool ByteOrderSwapped; |
| 45 | + const Value *LHSAux; |
| 46 | + PolynomialInfo(unsigned TripCount, const Value *LHS, const APInt &RHS, |
| 47 | + const Value *ComputedValue, bool ByteOrderSwapped, |
| 48 | + const Value *LHSAux = nullptr); |
| 49 | +}; |
| 50 | + |
| 51 | +/// The analysis. |
| 52 | +class HashRecognize { |
| 53 | + const Loop &L; |
| 54 | + ScalarEvolution &SE; |
| 55 | + |
| 56 | +public: |
| 57 | + HashRecognize(const Loop &L, ScalarEvolution &SE); |
| 58 | + |
| 59 | + // The main analysis entry point. |
| 60 | + std::variant<PolynomialInfo, ErrBits, StringRef> recognizeCRC() const; |
| 61 | + |
| 62 | + // Auxilary entry point after analysis to interleave the generating polynomial |
| 63 | + // and return a 256-entry CRC table. |
| 64 | + CRCTable genSarwateTable(const APInt &GenPoly, bool ByteOrderSwapped) const; |
| 65 | + |
| 66 | + void print(raw_ostream &OS) const; |
| 67 | +}; |
| 68 | + |
| 69 | +class HashRecognizePrinterPass |
| 70 | + : public PassInfoMixin<HashRecognizePrinterPass> { |
| 71 | + raw_ostream &OS; |
| 72 | + |
| 73 | +public: |
| 74 | + explicit HashRecognizePrinterPass(raw_ostream &OS) : OS(OS) {} |
| 75 | + PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, |
| 76 | + LoopStandardAnalysisResults &AR, LPMUpdater &); |
| 77 | +}; |
| 78 | + |
| 79 | +class HashRecognizeAnalysis : public AnalysisInfoMixin<HashRecognizeAnalysis> { |
| 80 | + friend AnalysisInfoMixin<HashRecognizeAnalysis>; |
| 81 | + static AnalysisKey Key; |
| 82 | + |
| 83 | +public: |
| 84 | + using Result = HashRecognize; |
| 85 | + Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR); |
| 86 | +}; |
| 87 | +} // namespace llvm |
| 88 | + |
| 89 | +#endif |
0 commit comments