LLVM 22.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/CodeGen/Passes.h"
17
18using namespace llvm;
19
20uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands) {
21 uint64_t Hash = 0;
22 for (const MachineInstr &MI : MBB) {
23 if (MI.isMetaInstruction() || MI.isTerminator())
24 continue;
25 Hash = hashing::detail::hash_16_bytes(Hash, MI.getOpcode());
26 if (HashOperands) {
27 for (unsigned i = 0; i < MI.getNumOperands(); i++) {
28 Hash =
29 hashing::detail::hash_16_bytes(Hash, hash_value(MI.getOperand(i)));
30 }
31 }
32 }
33 return Hash;
34}
35
36/// Fold a 64-bit integer to a 16-bit one.
38 uint16_t Res = static_cast<uint16_t>(Value);
39 Res ^= static_cast<uint16_t>(Value >> 16);
40 Res ^= static_cast<uint16_t>(Value >> 32);
41 Res ^= static_cast<uint16_t>(Value >> 48);
42 return Res;
43}
44
45INITIALIZE_PASS(MachineBlockHashInfo, "machine-block-hash",
46 "Machine Block Hash Analysis", true, true)
47
49
52}
53
58
65
68 uint16_t Offset = 0;
69 // Initialize hash components
70 for (const MachineBasicBlock &MBB : F) {
71 // offset of the machine basic block
72 HashInfos[&MBB].Offset = Offset;
73 Offset += MBB.size();
74 // Hashing opcodes
75 HashInfos[&MBB].OpcodeHash = hashBlock(MBB, /*HashOperands=*/false);
76 // Hash complete instructions
77 HashInfos[&MBB].InstrHash = hashBlock(MBB, /*HashOperands=*/true);
78 }
79
80 // Initialize neighbor hash
81 for (const MachineBasicBlock &MBB : F) {
82 uint64_t Hash = HashInfos[&MBB].OpcodeHash;
83 // Append hashes of successors
84 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
85 uint64_t SuccHash = HashInfos[SuccMBB].OpcodeHash;
86 Hash = hashing::detail::hash_16_bytes(Hash, SuccHash);
87 }
88 // Append hashes of predecessors
89 for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
90 uint64_t PredHash = HashInfos[PredMBB].OpcodeHash;
91 Hash = hashing::detail::hash_16_bytes(Hash, PredHash);
92 }
93 HashInfos[&MBB].NeighborHash = Hash;
94 }
95
96 // Assign hashes
97 for (const MachineBasicBlock &MBB : F) {
98 const auto &HashInfo = HashInfos[&MBB];
99 BlendedBlockHash BlendedHash(fold_64_to_16(HashInfo.Offset),
100 fold_64_to_16(HashInfo.OpcodeHash),
101 fold_64_to_16(HashInfo.InstrHash),
102 fold_64_to_16(HashInfo.NeighborHash));
103 MBBHashInfo[&MBB] = BlendedHash.combine();
104 }
105
106 return false;
107}
108
110 return MBBHashInfo[&MBB];
111}
112
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands)
uint16_t fold_64_to_16(const uint64_t Value)
Fold a 64-bit integer to a 16-bit one.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
uint64_t getMBBHash(const MachineBasicBlock &MBB)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
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.
Representation of each machine instruction.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM Value Representation.
Definition Value.h:75
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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:477
hash_code hash_value(const FixedPointSemantics &Val)
LLVM_ABI void initializeMachineBlockHashInfoPass(PassRegistry &)
LLVM_ABI MachineFunctionPass * createMachineBlockHashInfoPass()
createMachineBlockHashInfoPass - This pass computes basic block hashes.
An object wrapping several components of a basic block hash.
uint64_t combine() const
Combine the blended hash into uint64_t.