LLVM 23.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/// An object wrapping several components of a basic block hash. The combined
22/// (blended) hash is represented and stored as one uint64_t, while individual
23/// components are of smaller size (e.g., uint16_t or uint8_t).
25public:
26 explicit BlendedBlockHash(uint16_t Offset, uint16_t OpcodeHash,
27 uint16_t InstrHash, uint16_t NeighborHash)
28 : Offset(Offset), OpcodeHash(OpcodeHash), InstrHash(InstrHash),
29 NeighborHash(NeighborHash) {}
30
31 explicit BlendedBlockHash(uint64_t CombinedHash) {
32 Offset = CombinedHash & 0xffff;
33 CombinedHash >>= 16;
34 OpcodeHash = CombinedHash & 0xffff;
35 CombinedHash >>= 16;
36 InstrHash = CombinedHash & 0xffff;
37 CombinedHash >>= 16;
38 NeighborHash = CombinedHash & 0xffff;
39 }
40
41 /// Combine the blended hash into uint64_t.
42 uint64_t combine() const {
43 uint64_t Hash = 0;
44 Hash |= uint64_t(NeighborHash);
45 Hash <<= 16;
46 Hash |= uint64_t(InstrHash);
47 Hash <<= 16;
48 Hash |= uint64_t(OpcodeHash);
49 Hash <<= 16;
50 Hash |= uint64_t(Offset);
51 return Hash;
52 }
53
54 /// Compute a distance between two given blended hashes. The smaller the
55 /// distance, the more similar two blocks are. For identical basic blocks,
56 /// the distance is zero.
57 /// Since OpcodeHash is highly stable, we consider a match good only if
58 /// the OpcodeHashes are identical. Mismatched OpcodeHashes lead to low
59 /// matching accuracy, and poor matches undermine the quality of final
60 /// inference. Notably, during inference, we also consider the matching
61 /// ratio of basic blocks. For MachineFunctions with a low matching
62 /// ratio, we directly skip optimization to reduce the impact of
63 /// mismatches. This ensures even very poor profiles won’t cause negative
64 /// optimization.
65 /// In the context of matching, we consider NeighborHash to be more
66 /// important. This is especially true when accounting for inlining
67 /// scenarios, where the position of a basic block in the control
68 /// flow graph is more critical.
70 assert(OpcodeHash == BBH.OpcodeHash &&
71 "incorrect blended hash distance computation");
72 uint64_t Dist = 0;
73 // Account for NeighborHash
74 Dist += NeighborHash == BBH.NeighborHash ? 0 : 1;
75 Dist <<= 16;
76 // Account for InstrHash
77 Dist += InstrHash == BBH.InstrHash ? 0 : 1;
78 Dist <<= 16;
79 // Account for Offset
80 Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset);
81 return Dist;
82 }
83
84 uint16_t getOpcodeHash() const { return OpcodeHash; }
85
86private:
87 /// The offset of the basic block from the function start.
89 /// Hash of the basic block instructions, excluding operands.
90 uint16_t OpcodeHash{0};
91 /// Hash of the basic block instructions, including opcodes and
92 /// operands.
93 uint16_t InstrHash{0};
94 /// OpcodeHash of the basic block together with OpcodeHashes of its
95 /// successors and predecessors.
96 uint16_t NeighborHash{0};
97};
98
99/// Result object for MachineBlockHashInfo.
108
110 : public AnalysisInfoMixin<MachineBlockHashInfoAnalysis> {
112 static AnalysisKey Key;
113
114public:
117};
118
119/// Printer pass for the \c MachineBlockHashInfoAnalysis results.
121 : public PassInfoMixin<MachineBlockHashInfoPrinterPass> {
122 raw_ostream &OS;
123
124public:
128 static bool isRequired() { return true; }
129};
130
131/// Legacy MachineFunctionPass for MachineBlockHashInfo.
134
135public:
136 static char ID;
138
139 StringRef getPassName() const override { return "Basic Block Hash Compute"; }
140
141 void getAnalysisUsage(AnalysisUsage &AU) const override;
142
143 bool runOnMachineFunction(MachineFunction &F) override;
144
146};
147
148} // end namespace llvm
149
150#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file defines the DenseMap class.
#define F(x, y, z)
Definition MD5.cpp:54
Represent the analysis usage information of a pass.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Result object for MachineBlockHashInfo.
uint64_t getMBBHash(const MachineBasicBlock &MBB) const
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
uint64_t getMBBHash(const MachineBasicBlock &MBB) const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:532
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
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 to automatically provide informational APIs needed for passes.
Definition PassManager.h:70