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
14#include "llvm/ADT/Hashing.h"
17#include "llvm/CodeGen/Passes.h"
21
22using namespace llvm;
23
24static uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands) {
25 uint64_t Hash = 0;
26 for (const MachineInstr &MI : MBB) {
27 if (MI.isMetaInstruction() || MI.isTerminator())
28 continue;
29 Hash = hashing::detail::hash_16_bytes(Hash, MI.getOpcode());
30 if (HashOperands) {
31 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
33 Hash, stableHashValue(MI.getOperand(i)));
34 }
35 }
36 }
37 return Hash;
38}
39
40/// Fold a 64-bit integer to a 16-bit one.
41static constexpr uint16_t fold_64_to_16(const uint64_t Value) {
42 uint16_t Res = static_cast<uint16_t>(Value);
43 Res ^= static_cast<uint16_t>(Value >> 16);
44 Res ^= static_cast<uint16_t>(Value >> 32);
45 Res ^= static_cast<uint16_t>(Value >> 48);
46 return Res;
47}
48
49// Keep stable to serialize data.
50static_assert(hashing::detail::hash_16_bytes(1, 2) == 9684580150926652833ull,
51 "Hash function must be stable");
52static_assert(hashing::detail::hash_16_bytes(-1, -2) == 7819786907124864172ull,
53 "Hash function must be stable");
54static_assert(fold_64_to_16(1) == 1, "Fold function must be stable");
55static_assert(fold_64_to_16(12345678) == 25074, "Fold function must be stable");
56
57INITIALIZE_PASS(MachineBlockHashInfo, "machine-block-hash",
58 "Machine Block Hash Analysis", true, true)
59
61
63
68
75
77
79 const MachineFunction &F) {
81 uint16_t Offset = 0;
82 // Initialize hash components
83 for (const MachineBasicBlock &MBB : F) {
84 auto &HashInfo = HashInfos[&MBB];
85 // offset of the machine basic block
86 HashInfo.Offset = Offset + MBB.size();
87 // Hashing opcodes
88 HashInfo.OpcodeHash = hashBlock(MBB, /*HashOperands=*/false);
89 // Hash complete instructions
90 HashInfo.InstrHash = hashBlock(MBB, /*HashOperands=*/true);
91 }
92
93 // Initialize neighbor hash
94 for (const MachineBasicBlock &MBB : F) {
95 auto &HashInfo = HashInfos[&MBB];
96 uint64_t Hash = HashInfo.OpcodeHash;
97 // Append hashes of successors
98 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
99 uint64_t SuccHash = HashInfos[SuccMBB].OpcodeHash;
100 Hash = hashing::detail::hash_16_bytes(Hash, SuccHash);
101 }
102 // Append hashes of predecessors
103 for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
104 uint64_t PredHash = HashInfos[PredMBB].OpcodeHash;
105 Hash = hashing::detail::hash_16_bytes(Hash, PredHash);
106 }
107 HashInfo.NeighborHash = Hash;
108 }
109
110 // Assign hashes
111 for (const MachineBasicBlock &MBB : F) {
112 const auto &HashInfo = HashInfos[&MBB];
113 BlendedBlockHash BlendedHash(fold_64_to_16(HashInfo.Offset),
114 fold_64_to_16(HashInfo.OpcodeHash),
115 fold_64_to_16(HashInfo.InstrHash),
116 fold_64_to_16(HashInfo.NeighborHash));
117 MBBHashInfo[&MBB] = BlendedHash.combine();
118 }
119}
120
123 auto it = MBBHashInfo.find(&MBB);
124 return it->second;
125}
126
131
133 return Result.getMBBHash(MBB);
134}
135
139
140AnalysisKey MachineBlockHashInfoAnalysis::Key;
141
147
151 auto &MBHI = MFAM.getResult<MachineBlockHashInfoAnalysis>(MF);
152 OS << "Machine Block Hash Info for function: " << MF.getName() << "\n";
153 for (const auto &MBB : MF) {
154 OS << " BB#" << MBB.getNumber() << ": "
155 << format_hex(MBHI.getMBBHash(MBB), 16) << "\n";
156 }
157 return PreservedAnalyses::all();
158}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
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
constexpr uint64_t hash_16_bytes(uint64_t low, uint64_t high)
Definition Hashing.h:171
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
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.