25class StructuralHashImpl {
32 static constexpr stable_hash BlockHeaderHash = 45798;
33 static constexpr stable_hash FunctionHeaderHash = 0x62642d6b6b2d6b72;
34 static constexpr stable_hash GlobalHeaderHash = 23456;
41 std::unique_ptr<IndexInstrMap> IndexInstruction =
nullptr;
44 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap =
nullptr;
47 DenseMap<const Value *, int> ValueToId;
50 SmallVector<stable_hash> Hashes;
58 StructuralHashImpl() =
delete;
59 explicit StructuralHashImpl(
bool DetailedHash,
61 : DetailedHash(DetailedHash), IgnoreOp(IgnoreOp) {
63 IndexInstruction = std::make_unique<IndexInstrMap>();
64 IndexOperandHashMap = std::make_unique<IndexOperandHashMapType>();
69 SmallVector<stable_hash> Hashes;
71 auto RawVals = ArrayRef<uint64_t>(
I.getRawData(),
I.getNumWords());
72 Hashes.
append(RawVals.begin(), RawVals.end());
77 return hashAPInt(
F.bitcastToAPInt());
80 static stable_hash hashGlobalVariable(
const GlobalVariable &GVar) {
82 return hashGlobalValue(&GVar);
95 "__cfstring",
"__cstring",
"__objc_classrefs",
96 "__objc_methname",
"__objc_selrefs",
105 return hashGlobalValue(&GVar);
108 static stable_hash hashGlobalValue(
const GlobalValue *GV) {
119 SmallVector<stable_hash> Hashes;
121 Type *Ty =
C->getType();
124 if (
C->isNullValue()) {
140 if (Seq->isString()) {
146 switch (
C->getValueID()) {
147 case Value::ConstantIntVal: {
152 case Value::ConstantFPVal: {
157 case Value::ConstantArrayVal:
158 case Value::ConstantStructVal:
159 case Value::ConstantVectorVal:
160 case Value::ConstantExprVal: {
161 for (
const auto &
Op :
C->operands()) {
167 case Value::BlockAddressVal: {
173 case Value::DSOLocalEquivalentVal: {
175 auto H = hashGlobalValue(Equiv->getGlobalValue());
189 return hashConstant(
C);
192 SmallVector<stable_hash> Hashes;
197 auto [It, WasInserted] = ValueToId.try_emplace(V, ValueToId.size());
204 SmallVector<stable_hash> Hashes;
210 stable_hash hashInstruction(
const Instruction &Inst) {
211 SmallVector<stable_hash> Hashes;
222 Hashes.
emplace_back(ComparisonInstruction->getPredicate());
224 unsigned InstIdx = 0;
225 if (IndexInstruction) {
226 InstIdx = IndexInstruction->size();
227 IndexInstruction->try_emplace(InstIdx,
const_cast<Instruction *
>(&Inst));
231 auto OpndHash = hashOperand(
Op);
232 if (IgnoreOp && IgnoreOp(&Inst, OpndIdx)) {
233 assert(IndexOperandHashMap);
234 IndexOperandHashMap->try_emplace({InstIdx, OpndIdx}, OpndHash);
258 void update(
const Function &
F) {
260 if (
F.isDeclaration())
263 SmallVector<stable_hash> Hashes;
270 SmallVector<const BasicBlock *, 8> BBs;
271 SmallPtrSet<const BasicBlock *, 16> VisitedBBs;
277 VisitedBBs.
insert(BBs[0]);
278 while (!BBs.
empty()) {
282 for (
auto &Inst : *BB)
286 if (VisitedBBs.
insert(Succ).second)
294 void update(
const GlobalVariable &GV) {
300 SmallVector<stable_hash> Hashes;
309 void update(
const Module &M) {
310 for (
const GlobalVariable &GV :
M.globals())
312 for (
const Function &
F : M)
316 uint64_t getHash()
const {
return Hash; }
318 std::unique_ptr<IndexInstrMap> getIndexInstrMap() {
319 return std::move(IndexInstruction);
322 std::unique_ptr<IndexOperandHashMapType> getIndexPairOpndHashMap() {
323 return std::move(IndexOperandHashMap);
330 StructuralHashImpl
H(DetailedHash);
336 return StructuralHashImpl::hashGlobalVariable(GVar);
340 StructuralHashImpl
H(DetailedHash);
348 StructuralHashImpl
H(
true, IgnoreOp);
351 H.getIndexPairOpndHashMap());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
Function * getFunction() const
StringRef getSection() const
Get the custom section of this global if it has one.
bool hasSection() const
Check if this global has a custom object file section.
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
A Module instance is used to store all the information related to an LLVM module.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
TypeID getTypeID() const
Return the type id for the type.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
@ C
The default llvm calling convention, compatible with C.
@ BasicBlock
Various leaf nodes.
static constexpr StringLiteral SectionNames[SectionKindsNum]
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
std::function< bool(const Instruction *, unsigned)> IgnoreOperandFunc
A function that takes an instruction and an operand index and returns true if the operand should be i...
FunctionAddr VTableAddr Value
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI FunctionHashInfo StructuralHashWithDifferences(const Function &F, IgnoreOperandFunc IgnoreOp)
Computes a structural hash of a given function, considering the structure and content of the function...
auto successors(const MachineBasicBlock *BB)
uint64_t stable_hash
An opaque object representing a stable hash code.
stable_hash stable_hash_name(StringRef Name)
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
stable_hash stable_hash_combine(ArrayRef< stable_hash > Buffer)
LLVM_ABI stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.