LLVM 23.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"
20
21using namespace llvm;
22
23// Frozen mixer; the block hashes computed below are serialized into BB
24// section profile data, so this function's exact output is part of the
25// on-disk format. Do not change without versioning that format.
26static constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
27 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
28 uint64_t a = (low ^ high) * kMul;
29 a ^= (a >> 47);
30 uint64_t b = (high ^ a) * kMul;
31 b ^= (b >> 47);
32 b *= kMul;
33 return b;
34}
35
36static uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands) {
37 uint64_t Hash = 0;
38 for (const MachineInstr &MI : MBB) {
39 if (MI.isMetaInstruction() || MI.isTerminator())
40 continue;
41 Hash = hash_16_bytes(Hash, MI.getOpcode());
42 if (HashOperands) {
43 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
44 Hash = hash_16_bytes(Hash, stableHashValue(MI.getOperand(i)));
45 }
46 }
47 }
48 return Hash;
49}
50
51/// Fold a 64-bit integer to a 16-bit one.
52static constexpr uint16_t fold_64_to_16(const uint64_t Value) {
53 uint16_t Res = static_cast<uint16_t>(Value);
54 Res ^= static_cast<uint16_t>(Value >> 16);
55 Res ^= static_cast<uint16_t>(Value >> 32);
56 Res ^= static_cast<uint16_t>(Value >> 48);
57 return Res;
58}
59
60static_assert(hash_16_bytes(1, 2) == 9684580150926652833ull,
61 "Hash function must be stable");
62static_assert(hash_16_bytes(-1, -2) == 7819786907124864172ull,
63 "Hash function must be stable");
64static_assert(fold_64_to_16(1) == 1, "Fold function must be stable");
65static_assert(fold_64_to_16(12345678) == 25074, "Fold function must be stable");
66
67INITIALIZE_PASS(MachineBlockHashInfo, "machine-block-hash",
68 "Machine Block Hash Analysis", true, true)
69
71
73
78
85
87
89 const MachineFunction &F) {
91 uint16_t Offset = 0;
92 // Initialize hash components
93 for (const MachineBasicBlock &MBB : F) {
94 auto &HashInfo = HashInfos[&MBB];
95 // offset of the machine basic block
96 HashInfo.Offset = Offset;
97 Offset += MBB.size();
98 // Hashing opcodes
99 HashInfo.OpcodeHash = hashBlock(MBB, /*HashOperands=*/false);
100 // Hash complete instructions
101 HashInfo.InstrHash = hashBlock(MBB, /*HashOperands=*/true);
102 }
103
104 // Initialize neighbor hash
105 for (const MachineBasicBlock &MBB : F) {
106 auto &HashInfo = HashInfos[&MBB];
107 uint64_t Hash = HashInfo.OpcodeHash;
108 // Append hashes of successors
109 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
110 uint64_t SuccHash = HashInfos[SuccMBB].OpcodeHash;
111 Hash = hash_16_bytes(Hash, SuccHash);
112 }
113 // Append hashes of predecessors
114 for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
115 uint64_t PredHash = HashInfos[PredMBB].OpcodeHash;
116 Hash = hash_16_bytes(Hash, PredHash);
117 }
118 HashInfo.NeighborHash = Hash;
119 }
120
121 // Assign hashes
122 for (const MachineBasicBlock &MBB : F) {
123 const auto &HashInfo = HashInfos[&MBB];
124 BlendedBlockHash BlendedHash(fold_64_to_16(HashInfo.Offset),
125 fold_64_to_16(HashInfo.OpcodeHash),
126 fold_64_to_16(HashInfo.InstrHash),
127 fold_64_to_16(HashInfo.NeighborHash));
128 MBBHashInfo[&MBB] = BlendedHash.combine();
129 }
130}
131
134 auto it = MBBHashInfo.find(&MBB);
135 return it->second;
136}
137
142
144 return Result.getMBBHash(MBB);
145}
146
150
151AnalysisKey MachineBlockHashInfoAnalysis::Key;
152
158
162 auto &MBHI = MFAM.getResult<MachineBlockHashInfoAnalysis>(MF);
163 OS << "Machine Block Hash Info for function: " << MF.getName() << "\n";
164 for (const auto &MBB : MF) {
165 OS << " BB#" << MBB.getNumber() << ": "
166 << format_hex(MBHI.getMBBHash(MBB), 16) << "\n";
167 }
168 return PreservedAnalyses::all();
169}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
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.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Result object for MachineBlockHashInfo.
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:557
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:191
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.