LLVM 24.0.0git
MachineBlockHashInfo.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/MachineBlockHashInfo.cpp---------------------*- 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
16#include "llvm/CodeGen/Passes.h"
21
22using namespace llvm;
23
25 "emit-bb-hash",
27 "Emit the hash of basic block in the SHT_LLVM_BB_ADDR_MAP section."),
28 cl::init(false));
29
31
32// Frozen mixer; the block hashes computed below are serialized into BB
33// section profile data, so this function's exact output is part of the
34// on-disk format. Do not change without versioning that format.
35static constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
36 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
37 uint64_t a = (low ^ high) * kMul;
38 a ^= (a >> 47);
39 uint64_t b = (high ^ a) * kMul;
40 b ^= (b >> 47);
41 b *= kMul;
42 return b;
43}
44
45static uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands) {
46 uint64_t Hash = 0;
47 for (const MachineInstr &MI : MBB) {
48 if (MI.isMetaInstruction() || MI.isTerminator())
49 continue;
50 Hash = hash_16_bytes(Hash, MI.getOpcode());
51 if (HashOperands) {
52 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
53 Hash = hash_16_bytes(Hash, stableHashValue(MI.getOperand(i)));
54 }
55 }
56 }
57 return Hash;
58}
59
60/// Fold a 64-bit integer to a 16-bit one.
61static constexpr uint16_t fold_64_to_16(const uint64_t Value) {
62 uint16_t Res = static_cast<uint16_t>(Value);
63 Res ^= static_cast<uint16_t>(Value >> 16);
64 Res ^= static_cast<uint16_t>(Value >> 32);
65 Res ^= static_cast<uint16_t>(Value >> 48);
66 return Res;
67}
68
69static_assert(hash_16_bytes(1, 2) == 9684580150926652833ull,
70 "Hash function must be stable");
71static_assert(hash_16_bytes(-1, -2) == 7819786907124864172ull,
72 "Hash function must be stable");
73static_assert(fold_64_to_16(1) == 1, "Fold function must be stable");
74static_assert(fold_64_to_16(12345678) == 25074, "Fold function must be stable");
75
76INITIALIZE_PASS(MachineBlockHashInfo, "machine-block-hash",
77 "Machine Block Hash Analysis", true, true)
78
80
82
87
89 uint64_t Offset;
90 uint64_t OpcodeHash;
91 uint64_t InstrHash;
92 uint64_t NeighborHash;
93};
94
96
98 const MachineFunction &F) {
100 uint16_t Offset = 0;
101 // Initialize hash components
102 for (const MachineBasicBlock &MBB : F) {
103 auto &HashInfo = HashInfos[&MBB];
104 // offset of the machine basic block
105 HashInfo.Offset = Offset;
106 Offset += MBB.size();
107 // Hashing opcodes
108 HashInfo.OpcodeHash = hashBlock(MBB, /*HashOperands=*/false);
109 // Hash complete instructions
110 HashInfo.InstrHash = hashBlock(MBB, /*HashOperands=*/true);
111 }
112
113 // Initialize neighbor hash
114 for (const MachineBasicBlock &MBB : F) {
115 auto &HashInfo = HashInfos[&MBB];
116 uint64_t Hash = HashInfo.OpcodeHash;
117 // Append hashes of successors
118 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
119 uint64_t SuccHash = HashInfos[SuccMBB].OpcodeHash;
120 Hash = hash_16_bytes(Hash, SuccHash);
121 }
122 // Append hashes of predecessors
123 for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
124 uint64_t PredHash = HashInfos[PredMBB].OpcodeHash;
125 Hash = hash_16_bytes(Hash, PredHash);
126 }
127 HashInfo.NeighborHash = Hash;
128 }
129
130 // Assign hashes
131 for (const MachineBasicBlock &MBB : F) {
132 const auto &HashInfo = HashInfos[&MBB];
133 BlendedBlockHash BlendedHash(fold_64_to_16(HashInfo.Offset),
134 fold_64_to_16(HashInfo.OpcodeHash),
135 fold_64_to_16(HashInfo.InstrHash),
136 fold_64_to_16(HashInfo.NeighborHash));
137 MBBHashInfo[&MBB] = BlendedHash.combine();
138 }
139}
140
141uint64_t
143 auto it = MBBHashInfo.find(&MBB);
144 return it->second;
145}
146
151
153 return Result.getMBBHash(MBB);
154}
155
159
160AnalysisKey MachineBlockHashInfoAnalysis::Key;
161
167
171 auto &MBHI = MFAM.getResult<MachineBlockHashInfoAnalysis>(MF);
172 OS << "Machine Block Hash Info for function: " << MF.getName() << "\n";
173 for (const auto &MBB : MF) {
174 OS << " BB#" << MBB.getNumber() << ": "
175 << format_hex(MBHI.getMBBHash(MBB), 18) << "\n";
176 }
177 return PreservedAnalyses::all();
178}
unsigned uint64_t
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
static cl::opt< bool > EmitBBHash("emit-bb-hash", cl::desc("Emit the hash of basic block in the SHT_LLVM_BB_ADDR_MAP section."), cl::init(false))
static constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high)
static constexpr uint16_t fold_64_to_16(const uint64_t Value)
Fold a 64-bit integer to a 16-bit one.
static uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
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
Legacy MachineFunctionPass for MachineBlockHashInfo.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM Value Representation.
Definition Value.h:75
initializer< Ty > init(const Ty &Val)
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
LLVM_ABI stable_hash stableHashValue(const MachineOperand &MO)
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:164
LLVM_ABI MachineFunctionPass * createMachineBlockHashInfoPass()
createMachineBlockHashInfoPass - This pass computes basic block hashes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
An object wrapping several components of a basic block hash.
uint64_t combine() const
Combine the blended hash into uint64_t.