AsmPrinter: Remove DIEHash::AttrEntry, NFC
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DIEHash.cpp
1 //===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for DWARF4 hashing of DIEs.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ByteStreamer.h"
15 #include "DIEHash.h"
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/Dwarf.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/MD5.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "dwarfdebug"
30
31 /// \brief Grabs the string in whichever attribute is passed in and returns
32 /// a reference to it.
33 static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
34   const auto &Values = Die.getValues();
35
36   // Iterate through all the attributes until we find the one we're
37   // looking for, if we can't find it return an empty string.
38   for (size_t i = 0; i < Values.size(); ++i) {
39     if (Values[i].getAttribute() == Attr)
40       return Values[i].getDIEString().getString();
41   }
42   return StringRef("");
43 }
44
45 /// \brief Adds the string in \p Str to the hash. This also hashes
46 /// a trailing NULL with the string.
47 void DIEHash::addString(StringRef Str) {
48   DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
49   Hash.update(Str);
50   Hash.update(makeArrayRef((uint8_t)'\0'));
51 }
52
53 // FIXME: The LEB128 routines are copied and only slightly modified out of
54 // LEB128.h.
55
56 /// \brief Adds the unsigned in \p Value to the hash encoded as a ULEB128.
57 void DIEHash::addULEB128(uint64_t Value) {
58   DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
59   do {
60     uint8_t Byte = Value & 0x7f;
61     Value >>= 7;
62     if (Value != 0)
63       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
64     Hash.update(Byte);
65   } while (Value != 0);
66 }
67
68 void DIEHash::addSLEB128(int64_t Value) {
69   DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
70   bool More;
71   do {
72     uint8_t Byte = Value & 0x7f;
73     Value >>= 7;
74     More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
75               ((Value == -1) && ((Byte & 0x40) != 0))));
76     if (More)
77       Byte |= 0x80; // Mark this byte to show that more bytes will follow.
78     Hash.update(Byte);
79   } while (More);
80 }
81
82 /// \brief Including \p Parent adds the context of Parent to the hash..
83 void DIEHash::addParentContext(const DIE &Parent) {
84
85   DEBUG(dbgs() << "Adding parent context to hash...\n");
86
87   // [7.27.2] For each surrounding type or namespace beginning with the
88   // outermost such construct...
89   SmallVector<const DIE *, 1> Parents;
90   const DIE *Cur = &Parent;
91   while (Cur->getParent()) {
92     Parents.push_back(Cur);
93     Cur = Cur->getParent();
94   }
95   assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
96          Cur->getTag() == dwarf::DW_TAG_type_unit);
97
98   // Reverse iterate over our list to go from the outermost construct to the
99   // innermost.
100   for (SmallVectorImpl<const DIE *>::reverse_iterator I = Parents.rbegin(),
101                                                       E = Parents.rend();
102        I != E; ++I) {
103     const DIE &Die = **I;
104
105     // ... Append the letter "C" to the sequence...
106     addULEB128('C');
107
108     // ... Followed by the DWARF tag of the construct...
109     addULEB128(Die.getTag());
110
111     // ... Then the name, taken from the DW_AT_name attribute.
112     StringRef Name = getDIEStringAttr(Die, dwarf::DW_AT_name);
113     DEBUG(dbgs() << "... adding context: " << Name << "\n");
114     if (!Name.empty())
115       addString(Name);
116   }
117 }
118
119 // Collect all of the attributes for a particular DIE in single structure.
120 void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
121   const SmallVectorImpl<DIEValue> &Values = Die.getValues();
122
123 #define COLLECT_ATTR(NAME)                                                     \
124   case dwarf::NAME:                                                            \
125     Attrs.NAME = Values[i];                                                    \
126     break
127
128   for (size_t i = 0, e = Values.size(); i != e; ++i) {
129     DEBUG(dbgs() << "Attribute: "
130                  << dwarf::AttributeString(Values[i].getAttribute())
131                  << " added.\n");
132     switch (Values[i].getAttribute()) {
133       COLLECT_ATTR(DW_AT_name);
134       COLLECT_ATTR(DW_AT_accessibility);
135       COLLECT_ATTR(DW_AT_address_class);
136       COLLECT_ATTR(DW_AT_allocated);
137       COLLECT_ATTR(DW_AT_artificial);
138       COLLECT_ATTR(DW_AT_associated);
139       COLLECT_ATTR(DW_AT_binary_scale);
140       COLLECT_ATTR(DW_AT_bit_offset);
141       COLLECT_ATTR(DW_AT_bit_size);
142       COLLECT_ATTR(DW_AT_bit_stride);
143       COLLECT_ATTR(DW_AT_byte_size);
144       COLLECT_ATTR(DW_AT_byte_stride);
145       COLLECT_ATTR(DW_AT_const_expr);
146       COLLECT_ATTR(DW_AT_const_value);
147       COLLECT_ATTR(DW_AT_containing_type);
148       COLLECT_ATTR(DW_AT_count);
149       COLLECT_ATTR(DW_AT_data_bit_offset);
150       COLLECT_ATTR(DW_AT_data_location);
151       COLLECT_ATTR(DW_AT_data_member_location);
152       COLLECT_ATTR(DW_AT_decimal_scale);
153       COLLECT_ATTR(DW_AT_decimal_sign);
154       COLLECT_ATTR(DW_AT_default_value);
155       COLLECT_ATTR(DW_AT_digit_count);
156       COLLECT_ATTR(DW_AT_discr);
157       COLLECT_ATTR(DW_AT_discr_list);
158       COLLECT_ATTR(DW_AT_discr_value);
159       COLLECT_ATTR(DW_AT_encoding);
160       COLLECT_ATTR(DW_AT_enum_class);
161       COLLECT_ATTR(DW_AT_endianity);
162       COLLECT_ATTR(DW_AT_explicit);
163       COLLECT_ATTR(DW_AT_is_optional);
164       COLLECT_ATTR(DW_AT_location);
165       COLLECT_ATTR(DW_AT_lower_bound);
166       COLLECT_ATTR(DW_AT_mutable);
167       COLLECT_ATTR(DW_AT_ordering);
168       COLLECT_ATTR(DW_AT_picture_string);
169       COLLECT_ATTR(DW_AT_prototyped);
170       COLLECT_ATTR(DW_AT_small);
171       COLLECT_ATTR(DW_AT_segment);
172       COLLECT_ATTR(DW_AT_string_length);
173       COLLECT_ATTR(DW_AT_threads_scaled);
174       COLLECT_ATTR(DW_AT_upper_bound);
175       COLLECT_ATTR(DW_AT_use_location);
176       COLLECT_ATTR(DW_AT_use_UTF8);
177       COLLECT_ATTR(DW_AT_variable_parameter);
178       COLLECT_ATTR(DW_AT_virtuality);
179       COLLECT_ATTR(DW_AT_visibility);
180       COLLECT_ATTR(DW_AT_vtable_elem_location);
181       COLLECT_ATTR(DW_AT_type);
182     default:
183       break;
184     }
185   }
186 }
187
188 void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
189                                        const DIE &Entry, StringRef Name) {
190   // append the letter 'N'
191   addULEB128('N');
192
193   // the DWARF attribute code (DW_AT_type or DW_AT_friend),
194   addULEB128(Attribute);
195
196   // the context of the tag,
197   if (const DIE *Parent = Entry.getParent())
198     addParentContext(*Parent);
199
200   // the letter 'E',
201   addULEB128('E');
202
203   // and the name of the type.
204   addString(Name);
205
206   // Currently DW_TAG_friends are not used by Clang, but if they do become so,
207   // here's the relevant spec text to implement:
208   //
209   // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
210   // the context is omitted and the name to be used is the ABI-specific name
211   // of the subprogram (e.g., the mangled linker name).
212 }
213
214 void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
215                                         unsigned DieNumber) {
216   // a) If T is in the list of [previously hashed types], use the letter
217   // 'R' as the marker
218   addULEB128('R');
219
220   addULEB128(Attribute);
221
222   // and use the unsigned LEB128 encoding of [the index of T in the
223   // list] as the attribute value;
224   addULEB128(DieNumber);
225 }
226
227 void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
228                            const DIE &Entry) {
229   assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
230                                         "tags. Add support here when there's "
231                                         "a use case");
232   // Step 5
233   // If the tag in Step 3 is one of [the below tags]
234   if ((Tag == dwarf::DW_TAG_pointer_type ||
235        Tag == dwarf::DW_TAG_reference_type ||
236        Tag == dwarf::DW_TAG_rvalue_reference_type ||
237        Tag == dwarf::DW_TAG_ptr_to_member_type) &&
238       // and the referenced type (via the [below attributes])
239       // FIXME: This seems overly restrictive, and causes hash mismatches
240       // there's a decl/def difference in the containing type of a
241       // ptr_to_member_type, but it's what DWARF says, for some reason.
242       Attribute == dwarf::DW_AT_type) {
243     // ... has a DW_AT_name attribute,
244     StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
245     if (!Name.empty()) {
246       hashShallowTypeReference(Attribute, Entry, Name);
247       return;
248     }
249   }
250
251   unsigned &DieNumber = Numbering[&Entry];
252   if (DieNumber) {
253     hashRepeatedTypeReference(Attribute, DieNumber);
254     return;
255   }
256
257   // otherwise, b) use the letter 'T' as the marker, ...
258   addULEB128('T');
259
260   addULEB128(Attribute);
261
262   // ... process the type T recursively by performing Steps 2 through 7, and
263   // use the result as the attribute value.
264   DieNumber = Numbering.size();
265   computeHash(Entry);
266 }
267
268 // Hash all of the values in a block like set of values. This assumes that
269 // all of the data is going to be added as integers.
270 void DIEHash::hashBlockData(const SmallVectorImpl<DIEValue> &Values) {
271   for (auto I = Values.begin(), E = Values.end(); I != E; ++I)
272     Hash.update((uint64_t)I->getDIEInteger().getValue());
273 }
274
275 // Hash the contents of a loclistptr class.
276 void DIEHash::hashLocList(const DIELocList &LocList) {
277   HashingByteStreamer Streamer(*this);
278   DwarfDebug &DD = *AP->getDwarfDebug();
279   const DebugLocStream &Locs = DD.getDebugLocs();
280   for (const auto &Entry : Locs.getEntries(Locs.getList(LocList.getValue())))
281     DD.emitDebugLocEntry(Streamer, Entry);
282 }
283
284 // Hash an individual attribute \param Attr based on the type of attribute and
285 // the form.
286 void DIEHash::hashAttribute(DIEValue Value, dwarf::Tag Tag) {
287   dwarf::Attribute Attribute = Value.getAttribute();
288
289   // Other attribute values use the letter 'A' as the marker, and the value
290   // consists of the form code (encoded as an unsigned LEB128 value) followed by
291   // the encoding of the value according to the form code. To ensure
292   // reproducibility of the signature, the set of forms used in the signature
293   // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
294   // DW_FORM_string, and DW_FORM_block.
295
296   switch (Value.getType()) {
297   case DIEValue::isNone:
298     llvm_unreachable("Expected valid DIEValue");
299
300     // 7.27 Step 3
301     // ... An attribute that refers to another type entry T is processed as
302     // follows:
303   case DIEValue::isEntry:
304     hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
305     break;
306   case DIEValue::isInteger: {
307     addULEB128('A');
308     addULEB128(Attribute);
309     switch (Value.getForm()) {
310     case dwarf::DW_FORM_data1:
311     case dwarf::DW_FORM_data2:
312     case dwarf::DW_FORM_data4:
313     case dwarf::DW_FORM_data8:
314     case dwarf::DW_FORM_udata:
315     case dwarf::DW_FORM_sdata:
316       addULEB128(dwarf::DW_FORM_sdata);
317       addSLEB128((int64_t)Value.getDIEInteger().getValue());
318       break;
319     // DW_FORM_flag_present is just flag with a value of one. We still give it a
320     // value so just use the value.
321     case dwarf::DW_FORM_flag_present:
322     case dwarf::DW_FORM_flag:
323       addULEB128(dwarf::DW_FORM_flag);
324       addULEB128((int64_t)Value.getDIEInteger().getValue());
325       break;
326     default:
327       llvm_unreachable("Unknown integer form!");
328     }
329     break;
330   }
331   case DIEValue::isString:
332     addULEB128('A');
333     addULEB128(Attribute);
334     addULEB128(dwarf::DW_FORM_string);
335     addString(Value.getDIEString().getString());
336     break;
337   case DIEValue::isBlock:
338   case DIEValue::isLoc:
339   case DIEValue::isLocList:
340     addULEB128('A');
341     addULEB128(Attribute);
342     addULEB128(dwarf::DW_FORM_block);
343     if (Value.getType() == DIEValue::isBlock) {
344       addULEB128(Value.getDIEBlock().ComputeSize(AP));
345       hashBlockData(Value.getDIEBlock().getValues());
346     } else if (Value.getType() == DIEValue::isLoc) {
347       addULEB128(Value.getDIELoc().ComputeSize(AP));
348       hashBlockData(Value.getDIELoc().getValues());
349     } else {
350       // We could add the block length, but that would take
351       // a bit of work and not add a lot of uniqueness
352       // to the hash in some way we could test.
353       hashLocList(Value.getDIELocList());
354     }
355     break;
356     // FIXME: It's uncertain whether or not we should handle this at the moment.
357   case DIEValue::isExpr:
358   case DIEValue::isLabel:
359   case DIEValue::isDelta:
360   case DIEValue::isTypeSignature:
361     llvm_unreachable("Add support for additional value types.");
362   }
363 }
364
365 // Go through the attributes from \param Attrs in the order specified in 7.27.4
366 // and hash them.
367 void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
368 #define ADD_ATTR(ATTR)                                                         \
369   {                                                                            \
370     if (ATTR)                                                                  \
371       hashAttribute(ATTR, Tag);                                                \
372   }
373
374   ADD_ATTR(Attrs.DW_AT_name);
375   ADD_ATTR(Attrs.DW_AT_accessibility);
376   ADD_ATTR(Attrs.DW_AT_address_class);
377   ADD_ATTR(Attrs.DW_AT_allocated);
378   ADD_ATTR(Attrs.DW_AT_artificial);
379   ADD_ATTR(Attrs.DW_AT_associated);
380   ADD_ATTR(Attrs.DW_AT_binary_scale);
381   ADD_ATTR(Attrs.DW_AT_bit_offset);
382   ADD_ATTR(Attrs.DW_AT_bit_size);
383   ADD_ATTR(Attrs.DW_AT_bit_stride);
384   ADD_ATTR(Attrs.DW_AT_byte_size);
385   ADD_ATTR(Attrs.DW_AT_byte_stride);
386   ADD_ATTR(Attrs.DW_AT_const_expr);
387   ADD_ATTR(Attrs.DW_AT_const_value);
388   ADD_ATTR(Attrs.DW_AT_containing_type);
389   ADD_ATTR(Attrs.DW_AT_count);
390   ADD_ATTR(Attrs.DW_AT_data_bit_offset);
391   ADD_ATTR(Attrs.DW_AT_data_location);
392   ADD_ATTR(Attrs.DW_AT_data_member_location);
393   ADD_ATTR(Attrs.DW_AT_decimal_scale);
394   ADD_ATTR(Attrs.DW_AT_decimal_sign);
395   ADD_ATTR(Attrs.DW_AT_default_value);
396   ADD_ATTR(Attrs.DW_AT_digit_count);
397   ADD_ATTR(Attrs.DW_AT_discr);
398   ADD_ATTR(Attrs.DW_AT_discr_list);
399   ADD_ATTR(Attrs.DW_AT_discr_value);
400   ADD_ATTR(Attrs.DW_AT_encoding);
401   ADD_ATTR(Attrs.DW_AT_enum_class);
402   ADD_ATTR(Attrs.DW_AT_endianity);
403   ADD_ATTR(Attrs.DW_AT_explicit);
404   ADD_ATTR(Attrs.DW_AT_is_optional);
405   ADD_ATTR(Attrs.DW_AT_location);
406   ADD_ATTR(Attrs.DW_AT_lower_bound);
407   ADD_ATTR(Attrs.DW_AT_mutable);
408   ADD_ATTR(Attrs.DW_AT_ordering);
409   ADD_ATTR(Attrs.DW_AT_picture_string);
410   ADD_ATTR(Attrs.DW_AT_prototyped);
411   ADD_ATTR(Attrs.DW_AT_small);
412   ADD_ATTR(Attrs.DW_AT_segment);
413   ADD_ATTR(Attrs.DW_AT_string_length);
414   ADD_ATTR(Attrs.DW_AT_threads_scaled);
415   ADD_ATTR(Attrs.DW_AT_upper_bound);
416   ADD_ATTR(Attrs.DW_AT_use_location);
417   ADD_ATTR(Attrs.DW_AT_use_UTF8);
418   ADD_ATTR(Attrs.DW_AT_variable_parameter);
419   ADD_ATTR(Attrs.DW_AT_virtuality);
420   ADD_ATTR(Attrs.DW_AT_visibility);
421   ADD_ATTR(Attrs.DW_AT_vtable_elem_location);
422   ADD_ATTR(Attrs.DW_AT_type);
423
424   // FIXME: Add the extended attributes.
425 }
426
427 // Add all of the attributes for \param Die to the hash.
428 void DIEHash::addAttributes(const DIE &Die) {
429   DIEAttrs Attrs = {};
430   collectAttributes(Die, Attrs);
431   hashAttributes(Attrs, Die.getTag());
432 }
433
434 void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
435   // 7.27 Step 7
436   // ... append the letter 'S',
437   addULEB128('S');
438
439   // the tag of C,
440   addULEB128(Die.getTag());
441
442   // and the name.
443   addString(Name);
444 }
445
446 // Compute the hash of a DIE. This is based on the type signature computation
447 // given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
448 // flattened description of the DIE.
449 void DIEHash::computeHash(const DIE &Die) {
450   // Append the letter 'D', followed by the DWARF tag of the DIE.
451   addULEB128('D');
452   addULEB128(Die.getTag());
453
454   // Add each of the attributes of the DIE.
455   addAttributes(Die);
456
457   // Then hash each of the children of the DIE.
458   for (auto &C : Die.getChildren()) {
459     // 7.27 Step 7
460     // If C is a nested type entry or a member function entry, ...
461     if (isType(C->getTag()) || C->getTag() == dwarf::DW_TAG_subprogram) {
462       StringRef Name = getDIEStringAttr(*C, dwarf::DW_AT_name);
463       // ... and has a DW_AT_name attribute
464       if (!Name.empty()) {
465         hashNestedType(*C, Name);
466         continue;
467       }
468     }
469     computeHash(*C);
470   }
471
472   // Following the last (or if there are no children), append a zero byte.
473   Hash.update(makeArrayRef((uint8_t)'\0'));
474 }
475
476 /// This is based on the type signature computation given in section 7.27 of the
477 /// DWARF4 standard. It is the md5 hash of a flattened description of the DIE
478 /// with the exception that we are hashing only the context and the name of the
479 /// type.
480 uint64_t DIEHash::computeDIEODRSignature(const DIE &Die) {
481
482   // Add the contexts to the hash. We won't be computing the ODR hash for
483   // function local types so it's safe to use the generic context hashing
484   // algorithm here.
485   // FIXME: If we figure out how to account for linkage in some way we could
486   // actually do this with a slight modification to the parent hash algorithm.
487   if (const DIE *Parent = Die.getParent())
488     addParentContext(*Parent);
489
490   // Add the current DIE information.
491
492   // Add the DWARF tag of the DIE.
493   addULEB128(Die.getTag());
494
495   // Add the name of the type to the hash.
496   addString(getDIEStringAttr(Die, dwarf::DW_AT_name));
497
498   // Now get the result.
499   MD5::MD5Result Result;
500   Hash.final(Result);
501
502   // ... take the least significant 8 bytes and return those. Our MD5
503   // implementation always returns its results in little endian, swap bytes
504   // appropriately.
505   return support::endian::read64le(Result + 8);
506 }
507
508 /// This is based on the type signature computation given in section 7.27 of the
509 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
510 /// with the inclusion of the full CU and all top level CU entities.
511 // TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
512 uint64_t DIEHash::computeCUSignature(const DIE &Die) {
513   Numbering.clear();
514   Numbering[&Die] = 1;
515
516   // Hash the DIE.
517   computeHash(Die);
518
519   // Now return the result.
520   MD5::MD5Result Result;
521   Hash.final(Result);
522
523   // ... take the least significant 8 bytes and return those. Our MD5
524   // implementation always returns its results in little endian, swap bytes
525   // appropriately.
526   return support::endian::read64le(Result + 8);
527 }
528
529 /// This is based on the type signature computation given in section 7.27 of the
530 /// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
531 /// with the inclusion of additional forms not specifically called out in the
532 /// standard.
533 uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
534   Numbering.clear();
535   Numbering[&Die] = 1;
536
537   if (const DIE *Parent = Die.getParent())
538     addParentContext(*Parent);
539
540   // Hash the DIE.
541   computeHash(Die);
542
543   // Now return the result.
544   MD5::MD5Result Result;
545   Hash.final(Result);
546
547   // ... take the least significant 8 bytes and return those. Our MD5
548   // implementation always returns its results in little endian, swap bytes
549   // appropriately.
550   return support::endian::read64le(Result + 8);
551 }