blob: 2e49514c98be383d6d9765567a2c98fa3a41d509 [file] [log] [blame]
Eric Christopher45731982013-08-08 23:45:55 +00001//===-- llvm/CodeGen/DIEHash.h - Dwarf Hashing Framework -------*- C++ -*--===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Eric Christopher45731982013-08-08 23:45:55 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for DWARF4 hashing of DIEs.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DIEHASH_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_DIEHASH_H
Eric Christopher5c38b652014-03-07 22:40:30 +000015
David Blaikie980d4992013-10-21 18:59:40 +000016#include "llvm/ADT/DenseMap.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/CodeGen/DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000018#include "llvm/Support/MD5.h"
19
20namespace llvm {
21
Eric Christopher420569b2014-02-20 02:50:45 +000022class AsmPrinter;
Eric Christopher45731982013-08-08 23:45:55 +000023class CompileUnit;
24
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000025/// An object containing the capability of hashing and adding hash
Eric Christopher45731982013-08-08 23:45:55 +000026/// attributes onto a DIE.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000027class DIEHash {
Eric Christopherd29614f2013-08-13 01:21:55 +000028 // Collection of all attributes used in hashing a particular DIE.
29 struct DIEAttrs {
David Blaikie74fa8032017-05-23 18:27:09 +000030#define HANDLE_DIE_HASH_ATTR(NAME) DIEValue NAME;
31#include "DIEHashAttributes.def"
Eric Christopherd29614f2013-08-13 01:21:55 +000032 };
33
Eric Christopher45731982013-08-08 23:45:55 +000034public:
Craig Topper353eda42014-04-24 06:44:33 +000035 DIEHash(AsmPrinter *A = nullptr) : AP(A) {}
Eric Christopher420569b2014-02-20 02:50:45 +000036
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000037 /// Computes the CU signature.
Mehdi Amini96ab48f2017-05-29 06:32:34 +000038 uint64_t computeCUSignature(StringRef DWOName, const DIE &Die);
Eric Christopherd29614f2013-08-13 01:21:55 +000039
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000040 /// Computes the type signature.
David Blaikie2aee7be2013-10-24 18:29:03 +000041 uint64_t computeTypeSignature(const DIE &Die);
Eric Christopher25b7adc2013-09-03 21:57:57 +000042
Eric Christopher45731982013-08-08 23:45:55 +000043 // Helper routines to process parts of a DIE.
Eric Christopherd29614f2013-08-13 01:21:55 +000044private:
Simon Pilgrim3cb30562018-07-18 09:07:54 +000045 /// Adds the parent context of \param Parent to the hash.
Fangrui Songcb0bab82018-07-16 18:51:40 +000046 void addParentContext(const DIE &Parent);
Eric Christopherd29614f2013-08-13 01:21:55 +000047
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000048 /// Adds the attributes of \param Die to the hash.
David Blaikie2aee7be2013-10-24 18:29:03 +000049 void addAttributes(const DIE &Die);
Eric Christopherd29614f2013-08-13 01:21:55 +000050
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000051 /// Computes the full DWARF4 7.27 hash of the DIE.
David Blaikie2aee7be2013-10-24 18:29:03 +000052 void computeHash(const DIE &Die);
Eric Christopherd29614f2013-08-13 01:21:55 +000053
Eric Christopher45731982013-08-08 23:45:55 +000054 // Routines that add DIEValues to the hash.
Eric Christopher5c38b652014-03-07 22:40:30 +000055public:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000056 /// Adds \param Value to the hash.
Eric Christopher5c38b652014-03-07 22:40:30 +000057 void update(uint8_t Value) { Hash.update(Value); }
58
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000059 /// Encodes and adds \param Value to the hash as a ULEB128.
Eric Christopher45731982013-08-08 23:45:55 +000060 void addULEB128(uint64_t Value);
61
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000062 /// Encodes and adds \param Value to the hash as a SLEB128.
David Blaikie6316ca42013-10-16 23:36:20 +000063 void addSLEB128(int64_t Value);
64
Eric Christopher5c38b652014-03-07 22:40:30 +000065private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000066 /// Adds \param Str to the hash and includes a NULL byte.
Eric Christopher45731982013-08-08 23:45:55 +000067 void addString(StringRef Str);
Eric Christopherd29614f2013-08-13 01:21:55 +000068
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000069 /// Collects the attributes of DIE \param Die into the \param Attrs
Eric Christopherd29614f2013-08-13 01:21:55 +000070 /// structure.
David Blaikie2aee7be2013-10-24 18:29:03 +000071 void collectAttributes(const DIE &Die, DIEAttrs &Attrs);
Eric Christopherd29614f2013-08-13 01:21:55 +000072
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000073 /// Hashes the attributes in \param Attrs in order.
David Blaikie6cf58c82013-10-21 22:36:50 +000074 void hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag);
Eric Christopherd29614f2013-08-13 01:21:55 +000075
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000076 /// Hashes the data in a block like DIEValue, e.g. DW_FORM_block or
Eric Christopher420569b2014-02-20 02:50:45 +000077 /// DW_FORM_exprloc.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000078 void hashBlockData(const DIE::const_value_range &Values);
Eric Christopher420569b2014-02-20 02:50:45 +000079
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000080 /// Hashes the contents pointed to in the .debug_loc section.
Eric Christopher4f17ee02014-03-08 00:29:41 +000081 void hashLocList(const DIELocList &LocList);
82
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000083 /// Hashes an individual attribute.
Benjamin Kramer1afc1de2016-06-17 20:41:14 +000084 void hashAttribute(const DIEValue &Value, dwarf::Tag Tag);
Eric Christopherd29614f2013-08-13 01:21:55 +000085
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000086 /// Hashes an attribute that refers to another DIE.
David Blaikieafcb9652013-10-24 17:51:43 +000087 void hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
David Blaikie2aee7be2013-10-24 18:29:03 +000088 const DIE &Entry);
David Blaikieafcb9652013-10-24 17:51:43 +000089
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000090 /// Hashes a reference to a named type in such a way that is
David Blaikieafcb9652013-10-24 17:51:43 +000091 /// independent of whether that type is described by a declaration or a
92 /// definition.
David Blaikie2aee7be2013-10-24 18:29:03 +000093 void hashShallowTypeReference(dwarf::Attribute Attribute, const DIE &Entry,
94 StringRef Name);
David Blaikieafcb9652013-10-24 17:51:43 +000095
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000096 /// Hashes a reference to a previously referenced type DIE.
Eric Christophera07e4f52013-11-19 09:28:34 +000097 void hashRepeatedTypeReference(dwarf::Attribute Attribute,
98 unsigned DieNumber);
David Blaikieafcb9652013-10-24 17:51:43 +000099
David Blaikie65cc9692013-10-25 18:38:43 +0000100 void hashNestedType(const DIE &Die, StringRef Name);
101
Eric Christopher45731982013-08-08 23:45:55 +0000102private:
103 MD5 Hash;
Eric Christopher420569b2014-02-20 02:50:45 +0000104 AsmPrinter *AP;
David Blaikie2aee7be2013-10-24 18:29:03 +0000105 DenseMap<const DIE *, unsigned> Numbering;
Eric Christopher45731982013-08-08 23:45:55 +0000106};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000107}
Eric Christopher5c38b652014-03-07 22:40:30 +0000108
109#endif