|
| 1 | +#ifndef LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H |
| 2 | +#define LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H |
| 3 | + |
| 4 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 5 | + |
| 6 | +namespace llvm { |
| 7 | + |
| 8 | +/// An object wrapping several components of a basic block hash. The combined |
| 9 | +/// (blended) hash is represented and stored as one uint64_t, while individual |
| 10 | +/// components are of smaller size (e.g., uint16_t or uint8_t). |
| 11 | +struct BlendedBlockHash { |
| 12 | +private: |
| 13 | + static uint64_t combineHashes(uint16_t Hash1, uint16_t Hash2, uint16_t Hash3, |
| 14 | + uint16_t Hash4) { |
| 15 | + uint64_t Hash = 0; |
| 16 | + |
| 17 | + Hash |= uint64_t(Hash4); |
| 18 | + Hash <<= 16; |
| 19 | + |
| 20 | + Hash |= uint64_t(Hash3); |
| 21 | + Hash <<= 16; |
| 22 | + |
| 23 | + Hash |= uint64_t(Hash2); |
| 24 | + Hash <<= 16; |
| 25 | + |
| 26 | + Hash |= uint64_t(Hash1); |
| 27 | + |
| 28 | + return Hash; |
| 29 | + } |
| 30 | + |
| 31 | + static void parseHashes(uint64_t Hash, uint16_t &Hash1, uint16_t &Hash2, |
| 32 | + uint16_t &Hash3, uint16_t &Hash4) { |
| 33 | + Hash1 = Hash & 0xffff; |
| 34 | + Hash >>= 16; |
| 35 | + |
| 36 | + Hash2 = Hash & 0xffff; |
| 37 | + Hash >>= 16; |
| 38 | + |
| 39 | + Hash3 = Hash & 0xffff; |
| 40 | + Hash >>= 16; |
| 41 | + |
| 42 | + Hash4 = Hash & 0xffff; |
| 43 | + Hash >>= 16; |
| 44 | + } |
| 45 | + |
| 46 | +public: |
| 47 | + explicit BlendedBlockHash() {} |
| 48 | + |
| 49 | + explicit BlendedBlockHash(uint64_t CombinedHash) { |
| 50 | + parseHashes(CombinedHash, Offset, OpcodeHash, InstrHash, NeighborHash); |
| 51 | + } |
| 52 | + |
| 53 | + /// Combine the blended hash into uint64_t. |
| 54 | + uint64_t combine() const { |
| 55 | + return combineHashes(Offset, OpcodeHash, InstrHash, NeighborHash); |
| 56 | + } |
| 57 | + |
| 58 | + /// Compute a distance between two given blended hashes. The smaller the |
| 59 | + /// distance, the more similar two blocks are. For identical basic blocks, |
| 60 | + /// the distance is zero. |
| 61 | + uint64_t distance(const BlendedBlockHash &BBH) const { |
| 62 | + assert(OpcodeHash == BBH.OpcodeHash && |
| 63 | + "incorrect blended hash distance computation"); |
| 64 | + uint64_t Dist = 0; |
| 65 | + // Account for NeighborHash |
| 66 | + Dist += NeighborHash == BBH.NeighborHash ? 0 : 1; |
| 67 | + Dist <<= 16; |
| 68 | + // Account for InstrHash |
| 69 | + Dist += InstrHash == BBH.InstrHash ? 0 : 1; |
| 70 | + Dist <<= 16; |
| 71 | + // Account for Offset |
| 72 | + Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset); |
| 73 | + return Dist; |
| 74 | + } |
| 75 | + |
| 76 | + /// The offset of the basic block from the function start. |
| 77 | + uint16_t Offset{0}; |
| 78 | + /// (Loose) Hash of the basic block instructions, excluding operands. |
| 79 | + uint16_t OpcodeHash{0}; |
| 80 | + /// (Strong) Hash of the basic block instructions, including opcodes and |
| 81 | + /// operands. |
| 82 | + uint16_t InstrHash{0}; |
| 83 | + /// Hash of the (loose) basic block together with (loose) hashes of its |
| 84 | + /// successors and predecessors. |
| 85 | + uint16_t NeighborHash{0}; |
| 86 | +}; |
| 87 | + |
| 88 | +class MachineBlockHashInfo : public MachineFunctionPass { |
| 89 | + DenseMap<unsigned, uint64_t> MBBHashInfo; |
| 90 | + |
| 91 | +public: |
| 92 | + static char ID; |
| 93 | + MachineBlockHashInfo(); |
| 94 | + |
| 95 | + StringRef getPassName() const override { return "Basic Block Hash Compute"; } |
| 96 | + |
| 97 | + void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 98 | + |
| 99 | + bool runOnMachineFunction(MachineFunction &F) override; |
| 100 | + |
| 101 | + uint64_t getMBBHash(const MachineBasicBlock &MBB); |
| 102 | +}; |
| 103 | + |
| 104 | +} // end namespace llvm |
| 105 | + |
| 106 | +#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H |
0 commit comments