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