1//===-- llvm/CodeGen/DIEHash.h - Dwarf Hashing Framework -------*- 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// This file contains support for DWARF4 hashing of DIEs.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIEHASH_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_DIEHASH_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/CodeGen/DIE.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/MD5.h"
20
21namespace llvm {
22
23class AsmPrinter;
24
25/// An object containing the capability of hashing and adding hash
26/// attributes onto a DIE.
27class DIEHash {
28 // Collection of all attributes used in hashing a particular DIE.
29 struct DIEAttrs {
30#define HANDLE_DIE_HASH_ATTR(NAME) DIEValue NAME;
31#include "DIEHashAttributes.def"
32 };
33
34public:
35 DIEHash(AsmPrinter *A = nullptr, DwarfCompileUnit *CU = nullptr)
36 : AP(A), CU(CU) {}
37
38 /// Computes the CU signature.
39 uint64_t computeCUSignature(StringRef DWOName, const DIE &Die);
40
41 /// Computes the type signature.
42 LLVM_ABI_FOR_TEST uint64_t computeTypeSignature(const DIE &Die);
43
44 // Helper routines to process parts of a DIE.
45private:
46 /// Adds the parent context of \param Parent to the hash.
47 void addParentContext(const DIE &Parent);
48
49 /// Adds the attributes of \param Die to the hash.
50 void addAttributes(const DIE &Die);
51
52 /// Computes the full DWARF4 7.27 hash of the DIE.
53 void computeHash(const DIE &Die);
54
55 // Routines that add DIEValues to the hash.
56public:
57 /// Adds \param Value to the hash.
58 void update(uint8_t Value) { Hash.update(Data: Value); }
59
60 /// Encodes and adds \param Value to the hash as a ULEB128.
61 void addULEB128(uint64_t Value);
62
63 /// Encodes and adds \param Value to the hash as a SLEB128.
64 void addSLEB128(int64_t Value);
65
66 void hashRawTypeReference(const DIE &Entry);
67
68private:
69 /// Adds \param Str to the hash and includes a NULL byte.
70 void addString(StringRef Str);
71
72 /// Collects the attributes of DIE \param Die into the \param Attrs
73 /// structure.
74 void collectAttributes(const DIE &Die, DIEAttrs &Attrs);
75
76 /// Hashes the attributes in \param Attrs in order.
77 void hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag);
78
79 /// Hashes the data in a block like DIEValue, e.g. DW_FORM_block or
80 /// DW_FORM_exprloc.
81 void hashBlockData(const DIE::const_value_range &Values);
82
83 /// Hashes the contents pointed to in the .debug_loc section.
84 void hashLocList(const DIELocList &LocList);
85
86 /// Hashes an individual attribute.
87 void hashAttribute(const DIEValue &Value, dwarf::Tag Tag);
88
89 /// Hashes an attribute that refers to another DIE.
90 void hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
91 const DIE &Entry);
92
93 /// Hashes a reference to a named type in such a way that is
94 /// independent of whether that type is described by a declaration or a
95 /// definition.
96 void hashShallowTypeReference(dwarf::Attribute Attribute, const DIE &Entry,
97 StringRef Name);
98
99 /// Hashes a reference to a previously referenced type DIE.
100 void hashRepeatedTypeReference(dwarf::Attribute Attribute,
101 unsigned DieNumber);
102
103 void hashNestedType(const DIE &Die, StringRef Name);
104
105private:
106 MD5 Hash;
107 AsmPrinter *AP;
108 DwarfCompileUnit *CU;
109 DenseMap<const DIE *, unsigned> Numbering;
110};
111}
112
113#endif
114