llvm.org GIT mirror llvm / 377e832
DIEHash: Const correct and use references where non-null/non-rebound. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193363 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 7 years ago
4 changed file(s) with 66 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
8282 }
8383
8484 /// \brief Including \p Parent adds the context of Parent to the hash..
85 void DIEHash::addParentContext(DIE *Parent) {
85 void DIEHash::addParentContext(const DIE &Parent) {
8686
8787 DEBUG(dbgs() << "Adding parent context to hash...\n");
8888
8989 // [7.27.2] For each surrounding type or namespace beginning with the
9090 // outermost such construct...
91 SmallVector Parents;
92 while (Parent->getTag() != dwarf::DW_TAG_compile_unit) {
93 Parents.push_back(Parent);
94 Parent = Parent->getParent();
91 SmallVector Parents;
92 const DIE *Cur = &Parent;
93 while (Cur->getTag() != dwarf::DW_TAG_compile_unit) {
94 Parents.push_back(Cur);
95 Cur = Cur->getParent();
9596 }
9697
9798 // Reverse iterate over our list to go from the outermost construct to the
9899 // innermost.
99 for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(),
100 E = Parents.rend();
100 for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(),
101 E = Parents.rend();
101102 I != E; ++I) {
102 DIE *Die = *I;
103 const DIE &Die = **I;
103104
104105 // ... Append the letter "C" to the sequence...
105106 addULEB128('C');
106107
107108 // ... Followed by the DWARF tag of the construct...
108 addULEB128(Die->getTag());
109 addULEB128(Die.getTag());
109110
110111 // ... Then the name, taken from the DW_AT_name attribute.
111 StringRef Name = getDIEStringAttr(*Die, dwarf::DW_AT_name);
112 StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
112113 DEBUG(dbgs() << "... adding context: " << Name << "\n");
113114 if (!Name.empty())
114115 addString(Name);
116117 }
117118
118119 // Collect all of the attributes for a particular DIE in single structure.
119 void DIEHash::collectAttributes(DIE *Die, DIEAttrs &Attrs) {
120 const SmallVectorImpl &Values = Die->getValues();
121 const DIEAbbrev &Abbrevs = Die->getAbbrev();
120 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
121 const SmallVectorImpl &Values = Die.getValues();
122 const DIEAbbrev &Abbrevs = Die.getAbbrev();
122123
123124 #define COLLECT_ATTR(NAME) \
124125 case dwarf::NAME: \
195196 addULEB128(Attribute);
196197
197198 // the context of the tag,
198 if (DIE *Parent = Entry.getParent())
199 addParentContext(Parent);
199 if (const DIE *Parent = Entry.getParent())
200 addParentContext(*Parent);
200201
201202 // the letter 'E',
202203 addULEB128('E');
226227 }
227228
228229 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
229 DIE &Entry) {
230 const DIE &Entry) {
230231 assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
231232 "tags. Add support here when there's "
232233 "a use case");
263264 // ... process the type T recursively by performing Steps 2 through 7, and
264265 // use the result as the attribute value.
265266 DieNumber = Numbering.size();
266 computeHash(&Entry);
267 computeHash(Entry);
267268 }
268269
269270 // Hash an individual attribute \param Attr based on the type of attribute and
376377 }
377378
378379 // Add all of the attributes for \param Die to the hash.
379 void DIEHash::addAttributes(DIE *Die) {
380 void DIEHash::addAttributes(const DIE &Die) {
380381 DIEAttrs Attrs = {};
381382 collectAttributes(Die, Attrs);
382 hashAttributes(Attrs, Die->getTag());
383 hashAttributes(Attrs, Die.getTag());
383384 }
384385
385386 // Compute the hash of a DIE. This is based on the type signature computation
386387 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
387388 // flattened description of the DIE.
388 void DIEHash::computeHash(DIE *Die) {
389 void DIEHash::computeHash(const DIE &Die) {
389390 // Append the letter 'D', followed by the DWARF tag of the DIE.
390391 addULEB128('D');
391 addULEB128(Die->getTag());
392 addULEB128(Die.getTag());
392393
393394 // Add each of the attributes of the DIE.
394395 addAttributes(Die);
395396
396397 // Then hash each of the children of the DIE.
397 for (std::vector::const_iterator I = Die->getChildren().begin(),
398 E = Die->getChildren().end();
398 for (std::vector::const_iterator I = Die.getChildren().begin(),
399 E = Die.getChildren().end();
399400 I != E; ++I)
400 computeHash(*I);
401 computeHash(**I);
401402
402403 // Following the last (or if there are no children), append a zero byte.
403404 Hash.update(makeArrayRef((uint8_t)'\0'));
407408 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
408409 /// with the exception that we are hashing only the context and the name of the
409410 /// type.
410 uint64_t DIEHash::computeDIEODRSignature(DIE *Die) {
411 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
411412
412413 // Add the contexts to the hash. We won't be computing the ODR hash for
413414 // function local types so it's safe to use the generic context hashing
414415 // algorithm here.
415416 // FIXME: If we figure out how to account for linkage in some way we could
416417 // actually do this with a slight modification to the parent hash algorithm.
417 DIE *Parent = Die->getParent();
418 if (Parent)
419 addParentContext(Parent);
418 if (const DIE *Parent = Die.getParent())
419 addParentContext(*Parent);
420420
421421 // Add the current DIE information.
422422
423423 // Add the DWARF tag of the DIE.
424 addULEB128(Die->getTag());
424 addULEB128(Die.getTag());
425425
426426 // Add the name of the type to the hash.
427 addString(getDIEStringAttr(*Die, dwarf::DW_AT_name));
427 addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
428428
429429 // Now get the result.
430430 MD5::MD5Result Result;
440440 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
441441 /// with the inclusion of the full CU and all top level CU entities.
442442 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
443 uint64_t DIEHash::computeCUSignature(DIE *Die) {
443 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
444444 Numbering.clear();
445 Numbering[Die] = 1;
445 Numbering[&Die] = 1;
446446
447447 // Hash the DIE.
448448 computeHash(Die);
461461 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
462462 /// with the inclusion of additional forms not specifically called out in the
463463 /// standard.
464 uint64_t DIEHash::computeTypeSignature(DIE *Die) {
464 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
465465 Numbering.clear();
466 Numbering[Die] = 1;
467
468 if (DIE *Parent = Die->getParent())
469 addParentContext(Parent);
466 Numbering[&Die] = 1;
467
468 if (const DIE *Parent = Die.getParent())
469 addParentContext(*Parent);
470470
471471 // Hash the DIE.
472472 computeHash(Die);
8383
8484 public:
8585 /// \brief Computes the ODR signature.
86 uint64_t computeDIEODRSignature(DIE *Die);
86 uint64_t computeDIEODRSignature(const DIE &Die);
8787
8888 /// \brief Computes the CU signature.
89 uint64_t computeCUSignature(DIE *Die);
89 uint64_t computeCUSignature(const DIE &Die);
9090
9191 /// \brief Computes the type signature.
92 uint64_t computeTypeSignature(DIE *Die);
92 uint64_t computeTypeSignature(const DIE &Die);
9393
9494 // Helper routines to process parts of a DIE.
9595 private:
9696 /// \brief Adds the parent context of \param Die to the hash.
97 void addParentContext(DIE *Die);
97 void addParentContext(const DIE &Die);
9898
9999 /// \brief Adds the attributes of \param Die to the hash.
100 void addAttributes(DIE *Die);
100 void addAttributes(const DIE &Die);
101101
102102 /// \brief Computes the full DWARF4 7.27 hash of the DIE.
103 void computeHash(DIE *Die);
103 void computeHash(const DIE &Die);
104104
105105 // Routines that add DIEValues to the hash.
106106 private:
115115
116116 /// \brief Collects the attributes of DIE \param Die into the \param Attrs
117117 /// structure.
118 void collectAttributes(DIE *Die, DIEAttrs &Attrs);
118 void collectAttributes(const DIE &Die, DIEAttrs &Attrs);
119119
120120 /// \brief Hashes the attributes in \param Attrs in order.
121121 void hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag);
125125
126126 /// \brief Hashes an attribute that refers to another DIE.
127127 void hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
128 DIE &Entry);
128 const DIE &Entry);
129129
130130 /// \brief Hashes a reference to a named type in such a way that is
131131 /// independent of whether that type is described by a declaration or a
132132 /// definition.
133 void hashShallowTypeReference(dwarf::Attribute Attribute,
134 const DIE &Entry, StringRef Name);
133 void hashShallowTypeReference(dwarf::Attribute Attribute, const DIE &Entry,
134 StringRef Name);
135135
136136 /// \brief Hashes a reference to a previously referenced type DIE.
137137 void hashRepeatedTypeReference(dwarf::Attribute Attribute, unsigned DieNumber);
138138
139139 private:
140140 MD5 Hash;
141 DenseMap<DIE*, unsigned> Numbering;
141 DenseMap<const DIE *, unsigned> Numbering;
142142 };
143143 }
10611061 if (GenerateODRHash && shouldAddODRHash(CUMap.begin()->second, Die))
10621062 CUMap.begin()->second->addUInt(Die, dwarf::DW_AT_GNU_odr_signature,
10631063 dwarf::DW_FORM_data8,
1064 Hash.computeDIEODRSignature(Die));
1064 Hash.computeDIEODRSignature(*Die));
10651065 }
10661066
10671067 // Handle anything that needs to be done on a per-cu basis.
10791079 uint64_t ID = 0;
10801080 if (GenerateCUHash) {
10811081 DIEHash CUHash;
1082 ID = CUHash.computeCUSignature(TheCU->getCUDie());
1082 ID = CUHash.computeCUSignature(*TheCU->getCUDie());
10831083 }
10841084 // This should be a unique identifier when we want to build .dwp files.
10851085 TheCU->addUInt(TheCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
2121 DIE Die(dwarf::DW_TAG_base_type);
2222 DIEInteger Size(4);
2323 Die.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &Size);
24 uint64_t MD5Res = Hash.computeTypeSignature(&Die);
24 uint64_t MD5Res = Hash.computeTypeSignature(Die);
2525 ASSERT_EQ(0x1AFE116E83701108ULL, MD5Res);
2626 }
2727
3434 // Line and file number are ignored.
3535 Unnamed.addValue(dwarf::DW_AT_decl_file, dwarf::DW_FORM_data1, &One);
3636 Unnamed.addValue(dwarf::DW_AT_decl_line, dwarf::DW_FORM_data1, &One);
37 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
37 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
3838
3939 // The exact same hash GCC produces for this DIE.
4040 ASSERT_EQ(0x715305ce6cfd9ad1ULL, MD5Res);
4848 Foo.addValue(dwarf::DW_AT_name, dwarf::DW_FORM_strp, &FooStr);
4949 Foo.addValue(dwarf::DW_AT_byte_size, dwarf::DW_FORM_data1, &One);
5050
51 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
51 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
5252
5353 // The exact same hash GCC produces for this DIE.
5454 ASSERT_EQ(0xd566dbd2ca5265ffULL, MD5Res);
7474 Space->addChild(Foo);
7575 CU.addChild(Space);
7676
77 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
77 uint64_t MD5Res = DIEHash().computeTypeSignature(*Foo);
7878
7979 // The exact same hash GCC produces for this DIE.
8080 ASSERT_EQ(0x7b80381fd17f1e33ULL, MD5Res);
104104 DIEEntry IntRef(&Int);
105105 Member->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
106106
107 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
107 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
108108
109109 ASSERT_EQ(0x5646aa436b7e07c6ULL, MD5Res);
110110 }
142142 Mem1->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
143143 Mem2->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, &IntRef);
144144
145 uint64_t MD5Res = DIEHash().computeTypeSignature(&Unnamed);
145 uint64_t MD5Res = DIEHash().computeTypeSignature(Unnamed);
146146
147147 ASSERT_EQ(0x3a7dc3ed7b76b2f8ULL, MD5Res);
148148 }
164164
165165 Foo.addChild(Mem);
166166
167 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
167 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
168168
169169 ASSERT_EQ(0x73d8b25aef227b06ULL, MD5Res);
170170 }
193193
194194 Foo.addChild(Mem);
195195
196 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
196 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
197197
198198 ASSERT_EQ(0x74ea73862e8708d2ULL, MD5Res);
199199 }
226226
227227 Foo.addChild(Mem);
228228
229 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
229 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
230230
231231 ASSERT_EQ(0xa0b15f467ad4525bULL, MD5Res);
232232 }
259259
260260 Foo.addChild(Mem);
261261
262 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
262 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
263263
264264 ASSERT_EQ(0xad211c8c3b31e57ULL, MD5Res);
265265 }
288288
289289 Foo.addChild(Mem);
290290
291 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
291 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
292292
293293 ASSERT_EQ(0x852e0c9ff7c04ebULL, MD5Res);
294294 }
332332
333333 Foo.addChild(Mem);
334334
335 MD5ResDecl = DIEHash().computeTypeSignature(&Foo);
335 MD5ResDecl = DIEHash().computeTypeSignature(Foo);
336336 }
337337 uint64_t MD5ResDef;
338338 {
361361
362362 Foo.addChild(Mem);
363363
364 MD5ResDef = DIEHash().computeTypeSignature(&Foo);
364 MD5ResDef = DIEHash().computeTypeSignature(Foo);
365365 }
366366 ASSERT_EQ(MD5ResDef, MD5ResDecl);
367367 }
404404
405405 Foo.addChild(Mem);
406406
407 MD5ResDecl = DIEHash().computeTypeSignature(&Foo);
407 MD5ResDecl = DIEHash().computeTypeSignature(Foo);
408408 }
409409 uint64_t MD5ResDef;
410410 {
432432
433433 Foo.addChild(Mem);
434434
435 MD5ResDef = DIEHash().computeTypeSignature(&Foo);
435 MD5ResDef = DIEHash().computeTypeSignature(Foo);
436436 }
437437 // FIXME: This seems to be a bug in the DWARF type hashing specification that
438438 // only uses the brief name hashing for types referenced via DW_AT_type. In
472472
473473 Foo.addChild(Mem);
474474
475 uint64_t MD5Res = DIEHash().computeTypeSignature(&Foo);
475 uint64_t MD5Res = DIEHash().computeTypeSignature(Foo);
476476
477477 ASSERT_EQ(0x954e026f01c02529ULL, MD5Res);
478478 }