llvm.org GIT mirror llvm / 0d27ca1
Move hash computation code into a separate class and file. No functional change intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188028 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 6 years ago
3 changed file(s) with 191 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for DWARF4 hashing of DIEs.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "dwarfdebug"
14
15 #include "DIE.h"
16 #include "DIEHash.h"
17 #include "DwarfCompileUnit.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MD5.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 /// \brief Grabs the string in whichever attribute is passed in and returns
29 /// a reference to it.
30 static StringRef getDIEStringAttr(DIE *Die, uint16_t Attr) {
31 const SmallVectorImpl &Values = Die->getValues();
32 const DIEAbbrev &Abbrevs = Die->getAbbrev();
33
34 // Iterate through all the attributes until we find the one we're
35 // looking for, if we can't find it return an empty string.
36 for (size_t i = 0; i < Values.size(); ++i) {
37 if (Abbrevs.getData()[i].getAttribute() == Attr) {
38 DIEValue *V = Values[i];
39 assert(isa(V) && "String requested. Not a string.");
40 DIEString *S = cast(V);
41 return S->getString();
42 }
43 }
44 return StringRef("");
45 }
46
47 /// \brief Adds the string in \p Str to the hash. This also hashes
48 /// a trailing NULL with the string.
49 void DIEHash::addString(StringRef Str) {
50 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
51 Hash.update(Str);
52 Hash.update(makeArrayRef((uint8_t)'\0'));
53 }
54
55 // FIXME: The LEB128 routines are copied and only slightly modified out of
56 // LEB128.h.
57
58 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
59 void DIEHash::addULEB128(uint64_t Value) {
60 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
61 do {
62 uint8_t Byte = Value & 0x7f;
63 Value >>= 7;
64 if (Value != 0)
65 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
66 Hash.update(Byte);
67 } while (Value != 0);
68 }
69
70 /// \brief Including \p Parent adds the context of Parent to the hash..
71 void DIEHash::addParentContext(DIE *Parent) {
72
73 DEBUG(dbgs() << "Adding parent context to hash...\n");
74
75 // [7.27.2] For each surrounding type or namespace beginning with the
76 // outermost such construct...
77 SmallVector Parents;
78 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
79 Parents.push_back(Parent);
80 Parent = Parent->getParent();
81 }
82
83 // Reverse iterate over our list to go from the outermost construct to the
84 // innermost.
85 for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(),
86 E = Parents.rend();
87 I != E; ++I) {
88 DIE *Die = *I;
89
90 // ... Append the letter "C" to the sequence...
91 addULEB128('C');
92
93 // ... Followed by the DWARF tag of the construct...
94 addULEB128(Die->getTag());
95
96 // ... Then the name, taken from the DW_AT_name attribute.
97 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
98 DEBUG(dbgs() << "... adding context: " << Name << "\n");
99 if (!Name.empty())
100 addString(Name);
101 }
102 }
103
104 /// This is based on the type signature computation given in section 7.27 of the
105 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
106 /// with
107 /// the exception that we are hashing only the context and the name of the type.
108 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
109
110 // Add the contexts to the hash. We won't be computing the ODR hash for
111 // function local types so it's safe to use the generic context hashing
112 // algorithm here.
113 // FIXME: If we figure out how to account for linkage in some way we could
114 // actually do this with a slight modification to the parent hash algorithm.
115 DIE *Parent = Die->getParent();
116 if (Parent)
117 addParentContext(Parent);
118
119 // Add the current DIE information.
120
121 // Add the DWARF tag of the DIE.
122 addULEB128(Die->getTag());
123
124 // Add the name of the type to the hash.
125 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
126
127 // Now get the result.
128 MD5::MD5Result Result;
129 Hash.final(Result);
130
131 // ... take the least significant 8 bytes and return those. Our MD5
132 // implementation always returns its results in little endian, swap bytes
133 // appropriately.
134 return *reinterpret_cast(Result + 8);
135 }
0 //===-- llvm/CodeGen/DIEHash.h - Dwarf Hashing Framework -------*- C++ -*--===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for DWARF4 hashing of DIEs.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/MD5.h"
14
15 namespace llvm {
16
17 class CompileUnit;
18
19 /// \brief An object containing the capability of hashing and adding hash
20 /// attributes onto a DIE.
21 class DIEHash {
22 public:
23 /// \brief Initializes. The hash is default initialized.
24 DIEHash() {}
25
26 /// \brief Computes the ODR signature
27 uint64_t computeDIEODRSignature(DIE *Die);
28
29 // Helper routines to process parts of a DIE.
30 private:
31 /// \brief Adds the parent context of \param Die to the hash.
32 void addParentContext(DIE *Die);
33
34 // Routines that add DIEValues to the hash.
35 private:
36 /// \brief Encodes and adds \param Value to the hash as a ULEB128.
37 void addULEB128(uint64_t Value);
38
39 /// \brief Adds \param Str to the hash and includes a NULL byte.
40 void addString(StringRef Str);
41
42 private:
43 MD5 Hash;
44 };
45 }
1313 #define DEBUG_TYPE "dwarfdebug"
1414 #include "DwarfDebug.h"
1515 #include "DIE.h"
16 #include "DIEHash.h"
1617 #include "DwarfAccelTable.h"
1718 #include "DwarfCompileUnit.h"
1819 #include "llvm/ADT/STLExtras.h"
961962 DeleteContainerSeconds(DeadFnScopeMap);
962963 }
963964
964 // Type Signature [7.27] computation code.
965 typedef ArrayRef HashValue;
965 // Type Signature [7.27] and ODR Hash code.
966966
967967 /// \brief Grabs the string in whichever attribute is passed in and returns
968968 /// a reference to it. Returns "" if the attribute doesn't exist.
973973 return S->getString();
974974
975975 return StringRef("");
976 }
977
978 /// \brief Adds the string in \p Str to the hash in \p Hash. This also hashes
979 /// a trailing NULL with the string.
980 static void addStringToHash(MD5 &Hash, StringRef Str) {
981 DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
982 Hash.update(Str);
983 Hash.update(makeArrayRef((uint8_t)'\0'));
984 }
985
986 // FIXME: These are copied and only slightly modified out of LEB128.h.
987
988 /// \brief Adds the unsigned in \p N to the hash in \p Hash. This also encodes
989 /// the unsigned as a ULEB128.
990 static void addULEB128ToHash(MD5 &Hash, uint64_t Value) {
991 DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
992 do {
993 uint8_t Byte = Value & 0x7f;
994 Value >>= 7;
995 if (Value != 0)
996 Byte |= 0x80; // Mark this byte to show that more bytes will follow.
997 Hash.update(Byte);
998 } while (Value != 0);
999 }
1000
1001 /// \brief Including \p Parent adds the context of Parent to \p Hash.
1002 static void addParentContextToHash(MD5 &Hash, DIE *Parent) {
1003
1004 DEBUG(dbgs() << "Adding parent context to hash...\n");
1005
1006 // [7.27.2] For each surrounding type or namespace beginning with the
1007 // outermost such construct...
1008 SmallVector Parents;
1009 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
1010 Parents.push_back(Parent);
1011 Parent = Parent->getParent();
1012 }
1013
1014 // Reverse iterate over our list to go from the outermost construct to the
1015 // innermost.
1016 for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(),
1017 E = Parents.rend();
1018 I != E; ++I) {
1019 DIE *Die = *I;
1020
1021 // ... Append the letter "C" to the sequence...
1022 addULEB128ToHash(Hash, 'C');
1023
1024 // ... Followed by the DWARF tag of the construct...
1025 addULEB128ToHash(Hash, Die->getTag());
1026
1027 // ... Then the name, taken from the DW_AT_name attribute.
1028 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
1029 DEBUG(dbgs() << "... adding context: " << Name << "\n");
1030 if (!Name.empty())
1031 addStringToHash(Hash, Name);
1032 }
1033 }
1034
1035 /// This is based on the type signature computation given in section 7.27 of the
1036 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE with
1037 /// the exception that we are hashing only the context and the name of the type.
1038 static void addDIEODRSignature(MD5 &Hash, CompileUnit *CU, DIE *Die) {
1039
1040 // Add the contexts to the hash. We won't be computing the ODR hash for
1041 // function local types so it's safe to use the generic context hashing
1042 // algorithm here.
1043 // FIXME: If we figure out how to account for linkage in some way we could
1044 // actually do this with a slight modification to the parent hash algorithm.
1045 DIE *Parent = Die->getParent();
1046 if (Parent)
1047 addParentContextToHash(Hash, Parent);
1048
1049 // Add the current DIE information.
1050
1051 // Add the DWARF tag of the DIE.
1052 addULEB128ToHash(Hash, Die->getTag());
1053
1054 // Add the name of the type to the hash.
1055 addStringToHash(Hash, getDIEStringAttr(Die, dwarf::DW_AT_name));
1056
1057 // Now get the result.
1058 MD5::MD5Result Result;
1059 Hash.final(Result);
1060
1061 // ... take the least significant 8 bytes and store those as the attribute.
1062 // Our MD5 implementation always returns its results in little endian, swap
1063 // bytes appropriately.
1064 uint64_t Signature = *reinterpret_cast(Result + 8);
1065
1066 // FIXME: This should be added onto the type unit, not the type, but this
1067 // works as an intermediate stage.
1068 CU->addUInt(Die, dwarf::DW_AT_GNU_odr_signature, dwarf::DW_FORM_data8,
1069 Signature);
1070976 }
1071977
1072978 /// Return true if the current DIE is contained within an anonymous namespace.
1089995 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1090996 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1091997 !isContainedInAnonNamespace(Die);
1092 }
998 }
1093999
10941000 void DwarfDebug::finalizeModuleInfo() {
10951001 // Collect info for variables that were optimized out.
11101016 // out type.
11111017 // FIXME: Do type splitting.
11121018 for (unsigned i = 0, e = TypeUnits.size(); i != e; ++i) {
1113 MD5 Hash;
11141019 DIE *Die = TypeUnits[i];
1020 DIEHash Hash;
11151021 // If we've requested ODR hashes and it's applicable for an ODR hash then
11161022 // add the ODR signature now.
1023 // FIXME: This should be added onto the type unit, not the type, but this
1024 // works as an intermediate stage.
11171025 if (GenerateODRHash && shouldAddODRHash(CUMap.begin()->second, Die))
1118 addDIEODRSignature(Hash, CUMap.begin()->second, Die);
1026 CUMap.begin()->second->addUInt(Die, dwarf::DW_AT_GNU_odr_signature,
1027 dwarf::DW_FORM_data8,
1028 Hash.computeDIEODRSignature(Die));
11191029 }
11201030
11211031 // Compute DIE offsets and sizes.