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/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
51
56
63
66 uint16_t Offset = 0;
67 // Initialize hash components
68 for (const MachineBasicBlock &MBB : F) {
69 // offset of the machine basic block
70 HashInfos[&MBB].Offset = Offset;
71 Offset += MBB.size();
72 // Hashing opcodes
73 HashInfos[&MBB].OpcodeHash = hashBlock(MBB, /*HashOperands=*/false);
74 // Hash complete instructions
75 HashInfos[&MBB].InstrHash = hashBlock(MBB, /*HashOperands=*/true);
76 }
77
78 // Initialize neighbor hash
79 for (const MachineBasicBlock &MBB : F) {
80 uint64_t Hash = HashInfos[&MBB].OpcodeHash;
81 // Append hashes of successors
82 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
83 uint64_t SuccHash = HashInfos[SuccMBB].OpcodeHash;
84 Hash = hashing::detail::hash_16_bytes(Hash, SuccHash);
85 }
86 // Append hashes of predecessors
87 for (const MachineBasicBlock *PredMBB : MBB.predecessors()) {
88 uint64_t PredHash = HashInfos[PredMBB].OpcodeHash;
89 Hash = hashing::detail::hash_16_bytes(Hash, PredHash);
90 }
91 HashInfos[&MBB].NeighborHash = Hash;
92 }
93
94 // Assign hashes
95 for (const MachineBasicBlock &MBB : F) {
96 const auto &HashInfo = HashInfos[&MBB];
97 BlendedBlockHash BlendedHash(fold_64_to_16(HashInfo.Offset),
98 fold_64_to_16(HashInfo.OpcodeHash),
99 fold_64_to_16(HashInfo.InstrHash),
100 fold_64_to_16(HashInfo.NeighborHash));
101 MBBHashInfo[&MBB] = BlendedHash.combine();
102 }
103
104 return false;
105}
106
108 return MBBHashInfo[&MBB];
109}
110
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
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.
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.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
hash_code hash_value(const FixedPointSemantics &Val)
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.