LLVM 24.0.0git
MachineBlockHashInfo.h
Go to the documentation of this file.
1//===- llvm/CodeGen/MachineBlockHashInfo.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// Compute the hashes of basic blocks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
14#define LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
15
16#include "llvm/ADT/DenseMap.h"
18
19namespace llvm {
20
21/// Returns whether -emit-bb-hash is set.
23
24/// An object wrapping several components of a basic block hash. The combined
25/// (blended) hash is represented and stored as one uint64_t, while individual
26/// components are of smaller size (e.g., uint16_t or uint8_t).
28public:
29 explicit BlendedBlockHash(uint16_t Offset, uint16_t OpcodeHash,
30 uint16_t InstrHash, uint16_t NeighborHash)
31 : Offset(Offset), OpcodeHash(OpcodeHash), InstrHash(InstrHash),
32 NeighborHash(NeighborHash) {}
33
34 explicit BlendedBlockHash(uint64_t CombinedHash) {
35 Offset = CombinedHash & 0xffff;
36 CombinedHash >>= 16;
37 OpcodeHash = CombinedHash & 0xffff;
38 CombinedHash >>= 16;
39 InstrHash = CombinedHash & 0xffff;
40 CombinedHash >>= 16;
41 NeighborHash = CombinedHash & 0xffff;
42 }
43
44 /// Combine the blended hash into uint64_t.
45 uint64_t combine() const {
46 uint64_t Hash = 0;
47 Hash |= uint64_t(NeighborHash);
48 Hash <<= 16;
49 Hash |= uint64_t(InstrHash);
50 Hash <<= 16;
51 Hash |= uint64_t(OpcodeHash);
52 Hash <<= 16;
53 Hash |= uint64_t(Offset);
54 return Hash;
55 }
56
57 /// Compute a distance between two given blended hashes. The smaller the
58 /// distance, the more similar two blocks are. For identical basic blocks,
59 /// the distance is zero.
60 /// Since OpcodeHash is highly stable, we consider a match good only if
61 /// the OpcodeHashes are identical. Mismatched OpcodeHashes lead to low
62 /// matching accuracy, and poor matches undermine the quality of final
63 /// inference. Notably, during inference, we also consider the matching
64 /// ratio of basic blocks. For MachineFunctions with a low matching
65 /// ratio, we directly skip optimization to reduce the impact of
66 /// mismatches. This ensures even very poor profiles won’t cause negative
67 /// optimization.
68 /// In the context of matching, we consider NeighborHash to be more
69 /// important. This is especially true when accounting for inlining
70 /// scenarios, where the position of a basic block in the control
71 /// flow graph is more critical.
73 assert(OpcodeHash == BBH.OpcodeHash &&
74 "incorrect blended hash distance computation");
75 uint64_t Dist = 0;
76 // Account for NeighborHash
77 Dist += NeighborHash == BBH.NeighborHash ? 0 : 1;
78 Dist <<= 16;
79 // Account for InstrHash
80 Dist += InstrHash == BBH.InstrHash ? 0 : 1;
81 Dist <<= 16;
82 // Account for Offset
83 Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset);
84 return Dist;
85 }
86
87 uint16_t getOpcodeHash() const { return OpcodeHash; }
88
89private:
90 /// The offset of the basic block from the function start.
92 /// Hash of the basic block instructions, excluding operands.
93 uint16_t OpcodeHash{0};
94 /// Hash of the basic block instructions, including opcodes and
95 /// operands.
96 uint16_t InstrHash{0};
97 /// OpcodeHash of the basic block together with OpcodeHashes of its
98 /// successors and predecessors.
99 uint16_t NeighborHash{0};
100};
101
102/// Result object for MachineBlockHashInfo.
111
113 : public AnalysisInfoMixin<MachineBlockHashInfoAnalysis> {
115 static AnalysisKey Key;
116
117public:
121};
122
123/// Printer pass for the \c MachineBlockHashInfoAnalysis results.
125 : public RequiredPassInfoMixin<MachineBlockHashInfoPrinterPass> {
126 raw_ostream &OS;
127
128public:
132};
133
134/// Legacy MachineFunctionPass for MachineBlockHashInfo.
137
138public:
139 static char ID;
141
142 StringRef getPassName() const override { return "Basic Block Hash Compute"; }
143
144 void getAnalysisUsage(AnalysisUsage &AU) const override;
145
146 bool runOnMachineFunction(MachineFunction &F) override;
147
148 uint64_t getMBBHash(const MachineBasicBlock &MBB) const;
149
151};
152
153} // end namespace llvm
154
155#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock & MBB
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
Represent the analysis usage information of a pass.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Result object for MachineBlockHashInfo.
LLVM_ABI uint64_t getMBBHash(const MachineBasicBlock &MBB) const
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
MachineBlockHashInfoResult & getMBHI()
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
@ Offset
Definition DWP.cpp:577
LLVM_ABI bool shouldEmitBBHash()
Returns whether -emit-bb-hash is set.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
uint64_t distance(const BlendedBlockHash &BBH) const
Compute a distance between two given blended hashes.
BlendedBlockHash(uint16_t Offset, uint16_t OpcodeHash, uint16_t InstrHash, uint16_t NeighborHash)
uint64_t combine() const
Combine the blended hash into uint64_t.
BlendedBlockHash(uint64_t CombinedHash)
A CRTP mix-in for passes that should not be skipped.