25class StructuralHashImpl {
32 static constexpr stable_hash BlockHeaderHash = 45798;
33 static constexpr stable_hash FunctionHeaderHash = 0x62642d6b6b2d6b72;
34 static constexpr stable_hash GlobalHeaderHash = 23456;
40 bool SourceQualifyLocalGlobals;
44 std::optional<stable_hash> SourceNameHash;
48 std::unique_ptr<IndexInstrMap> IndexInstruction =
nullptr;
51 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap =
nullptr;
54 DenseMap<const Value *, int> ValueToId;
57 SmallVector<stable_hash> Hashes;
65 StructuralHashImpl() =
delete;
66 explicit StructuralHashImpl(
bool DetailedHash,
68 bool SourceQualifyLocalGlobals =
false)
69 : DetailedHash(DetailedHash), IgnoreOp(IgnoreOp),
70 SourceQualifyLocalGlobals(SourceQualifyLocalGlobals) {
72 IndexInstruction = std::make_unique<IndexInstrMap>();
73 IndexOperandHashMap = std::make_unique<IndexOperandHashMapType>();
78 SmallVector<stable_hash> Hashes;
80 auto RawVals = ArrayRef<uint64_t>(
I.getRawData(),
I.getNumWords());
81 Hashes.
append(RawVals.begin(), RawVals.end());
86 return hashAPInt(
F.bitcastToAPInt());
89 stable_hash hashGlobalName(
const GlobalVariable &GVar) {
91 if (!GlobalHash || !SourceQualifyLocalGlobals || !GVar.
hasLocalLinkage())
93 if (!SourceNameHash) {
103 stable_hash hashGlobalVariable(
const GlobalVariable &GVar) {
105 return hashGlobalName(GVar);
118 "__cfstring",
"__cstring",
"__objc_classrefs",
119 "__objc_methname",
"__objc_selrefs",
128 return hashGlobalName(GVar);
131 static stable_hash hashGlobalValue(
const GlobalValue *GV) {
142 SmallVector<stable_hash> Hashes;
144 Type *Ty =
C->getType();
147 if (
C->isNullValue()) {
163 if (Seq->isString()) {
169 switch (
C->getValueID()) {
170 case Value::ConstantIntVal: {
175 case Value::ConstantFPVal: {
180 case Value::ConstantArrayVal:
181 case Value::ConstantStructVal:
182 case Value::ConstantVectorVal:
183 case Value::ConstantExprVal: {
184 for (
const auto &
Op :
C->operands()) {
190 case Value::BlockAddressVal: {
196 case Value::DSOLocalEquivalentVal: {
198 auto H = hashGlobalValue(Equiv->getGlobalValue());
212 return hashConstant(
C);
215 SmallVector<stable_hash> Hashes;
220 auto [It, WasInserted] = ValueToId.try_emplace(V, ValueToId.size());
227 SmallVector<stable_hash> Hashes;
234 SmallVector<stable_hash> Hashes;
245 Hashes.
emplace_back(ComparisonInstruction->getPredicate());
247 unsigned InstIdx = 0;
248 if (IndexInstruction) {
249 InstIdx = IndexInstruction->size();
250 IndexInstruction->try_emplace(InstIdx,
const_cast<Instruction *
>(&Inst));
254 auto OpndHash = hashOperand(
Op);
255 if (IgnoreOp && IgnoreOp(&Inst, OpndIdx)) {
256 assert(IndexOperandHashMap);
257 IndexOperandHashMap->try_emplace({InstIdx, OpndIdx}, OpndHash);
283 if (
F.isDeclaration())
286 SmallVector<stable_hash> Hashes;
293 SmallVector<const BasicBlock *, 8> BBs;
294 SmallPtrSet<const BasicBlock *, 16> VisitedBBs;
300 VisitedBBs.
insert(BBs[0]);
301 while (!BBs.
empty()) {
305 for (
auto &Inst : *BB)
309 if (VisitedBBs.
insert(Succ).second)
317 void update(
const GlobalVariable &GV) {
323 SmallVector<stable_hash> Hashes;
332 void update(
const Module &M) {
333 for (
const GlobalVariable &GV :
M.globals())
339 uint64_t getHash()
const {
return Hash; }
341 std::unique_ptr<IndexInstrMap> getIndexInstrMap() {
342 return std::move(IndexInstruction);
345 std::unique_ptr<IndexOperandHashMapType> getIndexPairOpndHashMap() {
346 return std::move(IndexOperandHashMap);
353 StructuralHashImpl
H(DetailedHash);
359 bool SourceQualifyLocalGlobals) {
360 StructuralHashImpl
H(
false,
nullptr,
361 SourceQualifyLocalGlobals);
362 return H.hashGlobalVariable(GVar);
366 StructuralHashImpl
H(DetailedHash);
374 bool SourceQualifyLocalGlobals) {
375 StructuralHashImpl
H(
true, IgnoreOp,
376 SourceQualifyLocalGlobals);
379 H.getIndexPairOpndHashMap());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
Module.h This file contains the declarations for the Module class.
static stable_hash hashInstruction(const MachineInstr &MI)
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...
bool hasLocalLinkage() const
Module * getParent()
Get the module that this global value is contained inside of...
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.
const std::string & getSourceFileName() const
Get the module's original source file name.
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.
constexpr bool empty() const
Check if the string is empty.
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.
@ 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...
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.
auto successors(const MachineBasicBlock *BB)
uint64_t stable_hash
An opaque object representing a stable hash code.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
stable_hash stable_hash_name(StringRef Name)
LLVM_ABI FunctionHashInfo StructuralHashWithDifferences(const Function &F, IgnoreOperandFunc IgnoreOp, bool SourceQualifyLocalGlobals=false)
Computes a structural hash of a given function, considering the structure and content of the function...
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.