LLVM  14.0.0git
MachineStableHash.cpp
Go to the documentation of this file.
1 //===- lib/CodeGen/MachineStableHash.cpp ----------------------------------===//
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 // Stable hashing for MachineInstr and MachineOperand. Useful or getting a
10 // hash across runs, modules, etc.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/Loads.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Instructions.h"
35 #include "llvm/MC/MCDwarf.h"
38 
39 #define DEBUG_TYPE "machine-stable-hash"
40 
41 using namespace llvm;
42 
43 STATISTIC(StableHashBailingMachineBasicBlock,
44  "Number of encountered unsupported MachineOperands that were "
45  "MachineBasicBlocks while computing stable hashes");
46 STATISTIC(StableHashBailingConstantPoolIndex,
47  "Number of encountered unsupported MachineOperands that were "
48  "ConstantPoolIndex while computing stable hashes");
49 STATISTIC(StableHashBailingTargetIndexNoName,
50  "Number of encountered unsupported MachineOperands that were "
51  "TargetIndex with no name");
52 STATISTIC(StableHashBailingGlobalAddress,
53  "Number of encountered unsupported MachineOperands that were "
54  "GlobalAddress while computing stable hashes");
55 STATISTIC(StableHashBailingBlockAddress,
56  "Number of encountered unsupported MachineOperands that were "
57  "BlockAddress while computing stable hashes");
58 STATISTIC(StableHashBailingMetadataUnsupported,
59  "Number of encountered unsupported MachineOperands that were "
60  "Metadata of an unsupported kind while computing stable hashes");
61 
63  switch (MO.getType()) {
66  const MachineRegisterInfo &MRI = MO.getParent()->getMF()->getRegInfo();
67  return MRI.getVRegDef(MO.getReg())->getOpcode();
68  }
69 
70  // Register operands don't have target flags.
71  return stable_hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(),
72  MO.isDef());
74  return stable_hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
77  auto Val = MO.isCImm() ? MO.getCImm()->getValue()
79  auto ValHash =
80  stable_hash_combine_array(Val.getRawData(), Val.getNumWords());
81  return hash_combine(MO.getType(), MO.getTargetFlags(), ValHash);
82  }
83 
85  StableHashBailingMachineBasicBlock++;
86  return 0;
88  StableHashBailingConstantPoolIndex++;
89  return 0;
91  StableHashBailingBlockAddress++;
92  return 0;
94  StableHashBailingMetadataUnsupported++;
95  return 0;
97  StableHashBailingGlobalAddress++;
98  return 0;
100  if (const char *Name = MO.getTargetIndexName())
101  return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
103  MO.getOffset());
104  StableHashBailingTargetIndexNoName++;
105  return 0;
106  }
107 
110  return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
111  MO.getIndex());
112 
114  return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
116 
119  return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
120 
122  std::vector<llvm::stable_hash> ShuffleMaskHashes;
123 
125  MO.getShuffleMask(), std::back_inserter(ShuffleMaskHashes),
126  [](int S) -> llvm::stable_hash { return llvm::stable_hash(S); });
127 
128  return hash_combine(MO.getType(), MO.getTargetFlags(),
129  stable_hash_combine_array(ShuffleMaskHashes.data(),
130  ShuffleMaskHashes.size()));
131  }
133  auto SymbolName = MO.getMCSymbol()->getName();
134  return hash_combine(MO.getType(), MO.getTargetFlags(),
136  }
138  return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
139  MO.getCFIIndex());
141  return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
142  MO.getIntrinsicID());
144  return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
145  MO.getPredicate());
146  }
147  llvm_unreachable("Invalid machine operand type");
148 }
149 
150 /// A stable hash value for machine instructions.
151 /// Returns 0 if no stable hash could be computed.
152 /// The hashing and equality testing functions ignore definitions so this is
153 /// useful for CSE, etc.
155  bool HashConstantPoolIndices,
156  bool HashMemOperands) {
157  // Build up a buffer of hash code components.
158  SmallVector<stable_hash, 16> HashComponents;
159  HashComponents.reserve(MI.getNumOperands() + MI.getNumMemOperands() + 2);
160  HashComponents.push_back(MI.getOpcode());
161  HashComponents.push_back(MI.getFlags());
162  for (const MachineOperand &MO : MI.operands()) {
163  if (!HashVRegs && MO.isReg() && MO.isDef() &&
164  Register::isVirtualRegister(MO.getReg()))
165  continue; // Skip virtual register defs.
166 
167  if (MO.isCPI()) {
168  HashComponents.push_back(stable_hash_combine(
169  MO.getType(), MO.getTargetFlags(), MO.getIndex()));
170  continue;
171  }
172 
173  stable_hash StableHash = stableHashValue(MO);
174  if (!StableHash)
175  return 0;
176  HashComponents.push_back(StableHash);
177  }
178 
179  for (const auto *Op : MI.memoperands()) {
180  if (!HashMemOperands)
181  break;
182  HashComponents.push_back(static_cast<unsigned>(Op->getSize()));
183  HashComponents.push_back(static_cast<unsigned>(Op->getFlags()));
184  HashComponents.push_back(static_cast<unsigned>(Op->getOffset()));
185  HashComponents.push_back(static_cast<unsigned>(Op->getSuccessOrdering()));
186  HashComponents.push_back(static_cast<unsigned>(Op->getAddrSpace()));
187  HashComponents.push_back(static_cast<unsigned>(Op->getSyncScopeID()));
188  HashComponents.push_back(static_cast<unsigned>(Op->getBaseAlign().value()));
189  HashComponents.push_back(static_cast<unsigned>(Op->getFailureOrdering()));
190  }
191 
192  return stable_hash_combine_range(HashComponents.begin(),
193  HashComponents.end());
194 }
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
MCDwarf.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm::stable_hash_combine_string
stable_hash stable_hash_combine_string(const StringRef &S)
Definition: StableHashing.h:99
MachineInstr.h
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::stable_hash_combine_range
stable_hash stable_hash_combine_range(InputIteratorT First, InputIteratorT Last)
Compute a stable_hash for a sequence of values.
Definition: StableHashing.h:84
llvm::stable_hash_combine
stable_hash stable_hash_combine(stable_hash A, stable_hash B)
Definition: StableHashing.h:51
llvm::MachineOperand::MO_ShuffleMask
@ MO_ShuffleMask
Other IR Constant for ISel (shuffle masks)
Definition: MachineOperand.h:71
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
Loads.h
llvm::MachineOperand::MO_RegisterLiveOut
@ MO_RegisterLiveOut
Mask of live-out registers.
Definition: MachineOperand.h:65
llvm::stable_hash_combine_array
stable_hash stable_hash_combine_array(const stable_hash *P, size_t C)
Definition: StableHashing.h:92
llvm::MachineOperand::getIntrinsicID
Intrinsic::ID getIntrinsicID() const
Definition: MachineOperand.h:583
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:133
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
Statistic.h
MachineJumpTableInfo.h
TargetInstrInfo.h
llvm::MachineOperand::MO_CFIIndex
@ MO_CFIIndex
MCCFIInstruction index.
Definition: MachineOperand.h:68
llvm::MachineOperand::getTargetIndexName
const char * getTargetIndexName() const
getTargetIndexName - If this MachineOperand is a TargetIndex that has a name, attempt to get the name...
Definition: MachineOperand.cpp:428
llvm::MachineOperand::isCImm
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
Definition: MachineOperand.h:325
llvm::MachineInstr::getMF
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
Definition: MachineInstr.cpp:666
llvm::ConstantFP::getValueAPF
const APFloat & getValueAPF() const
Definition: Constants.h:297
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
llvm::MachineOperand::getMCSymbol
MCSymbol * getMCSymbol() const
Definition: MachineOperand.h:573
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
MachineRegisterInfo.h
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:644
TargetMachine.h
StableHashing.h
Constants.h
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::MachineOperand::getRegMask
const uint32_t * getRegMask() const
getRegMask - Returns a bit mask of registers preserved by this RegMask operand.
Definition: MachineOperand.h:630
llvm::MachineOperand::MO_FrameIndex
@ MO_FrameIndex
Abstract Stack Frame Index.
Definition: MachineOperand.h:57
llvm::stableHashValue
stable_hash stableHashValue(const MachineOperand &MO)
Definition: MachineStableHash.cpp:62
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
IRPrintingPasses.h
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:198
MachineStableHash.h
llvm::APFloat::bitcastToAPInt
APInt bitcastToAPInt() const
Definition: APFloat.h:1130
llvm::MachineRegisterInfo::getVRegDef
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Definition: MachineRegisterInfo.cpp:400
llvm::MachineOperand::getParent
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Definition: MachineOperand.h:238
llvm::MachineOperand::MO_Metadata
@ MO_Metadata
Metadata reference (for debug info)
Definition: MachineOperand.h:66
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MachineOperand::getCImm
const ConstantInt * getCImm() const
Definition: MachineOperand.h:542
uint64_t
MemoryLocation.h
llvm::MachineOperand::MO_Predicate
@ MO_Predicate
Generic predicate for ISel.
Definition: MachineOperand.h:70
MIRPrinter.h
StringExtras.h
llvm::MachineOperand::MO_MCSymbol
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
Definition: MachineOperand.h:67
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:219
llvm::MachineOperand::getFPImm
const ConstantFP * getFPImm() const
Definition: MachineOperand.h:547
llvm::Register::isVirtualRegister
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:71
llvm::MachineOperand::getPredicate
unsigned getPredicate() const
Definition: MachineOperand.h:588
MIRFormatter.h
llvm::MachineOperand::getShuffleMask
ArrayRef< int > getShuffleMask() const
Definition: MachineOperand.h:593
llvm::MachineOperand::MO_TargetIndex
@ MO_TargetIndex
Target-dependent index+offset operand.
Definition: MachineOperand.h:59
llvm::MachineOperand::MO_FPImmediate
@ MO_FPImmediate
Floating-point immediate operand.
Definition: MachineOperand.h:55
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:360
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MachineOperand::MO_CImmediate
@ MO_CImmediate
Immediate >64bit operand.
Definition: MachineOperand.h:54
llvm::MachineInstr::getOpcode
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:489
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::transform
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition: STLExtras.h:1648
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
llvm::MachineOperand::isDef
bool isDef() const
Definition: MachineOperand.h:375
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MachineOperand::MO_IntrinsicID
@ MO_IntrinsicID
Intrinsic ID for ISel.
Definition: MachineOperand.h:69
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::MachineOperand::getSubReg
unsigned getSubReg() const
Definition: MachineOperand.h:365
FoldingSet.h
llvm::AMDGPU::HSAMD::Kernel::Key::SymbolName
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Definition: AMDGPUMetadata.h:381
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:324
MachineFrameInfo.h
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:557
llvm::MachineOperand::getCFIIndex
unsigned getCFIIndex() const
Definition: MachineOperand.h:578
Instructions.h
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:605
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:608
TargetIntrinsicInfo.h
ModuleSlotTracker.h
MachineOperand.h
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
TargetRegisterInfo.h
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58