blob: f7038ace035d6b4b1c958e026fe8d0dec099452e [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001//===- LLVMContextImpl.h - The LLVMContextImpl opaque class -----*- C++ -*-===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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
Owen Anderson8e66e0b2009-06-30 00:48:55 +00006//
7//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00008//
Fangrui Songf78650a2018-07-30 19:41:25 +00009// This file declares LLVMContextImpl, the opaque implementation
Owen Anderson36f62e52009-06-30 17:06:46 +000010// of LLVMContext.
11//
12//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000013
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
15#define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
Owen Anderson8e66e0b2009-06-30 00:48:55 +000016
Bill Wendling4607f4b2012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Owen Andersonafd0c4c2009-08-04 22:41:48 +000018#include "ConstantsContext.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000019#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000020#include "llvm/ADT/APInt.h"
Jay Foadc365eea2011-06-22 08:50:06 +000021#include "llvm/ADT/ArrayRef.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000022#include "llvm/ADT/DenseMap.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000023#include "llvm/ADT/DenseMapInfo.h"
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +000024#include "llvm/ADT/DenseSet.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000025#include "llvm/ADT/FoldingSet.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000026#include "llvm/ADT/Hashing.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000027#include "llvm/ADT/Optional.h"
28#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskin28f24482009-12-17 19:55:06 +000029#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000030#include "llvm/ADT/SmallVector.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000031#include "llvm/ADT/StringMap.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000032#include "llvm/ADT/StringRef.h"
Reid Kleckner443423e2017-01-10 23:23:58 +000033#include "llvm/ADT/StringSet.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000034#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000036#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Metadata.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000040#include "llvm/IR/TrackingMDRef.h"
41#include "llvm/Support/Allocator.h"
42#include "llvm/Support/Casting.h"
Adam Nemeta62b7e12016-09-27 20:55:07 +000043#include "llvm/Support/YAMLTraits.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000044#include <algorithm>
45#include <cassert>
46#include <cstddef>
47#include <cstdint>
48#include <memory>
49#include <string>
50#include <utility>
Duncan P. N. Exon Smith3eef9d12016-04-19 23:59:13 +000051#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000052
Owen Anderson20b34ac2009-07-16 18:04:31 +000053namespace llvm {
Owen Andersonedb4a702009-07-24 23:12:02 +000054
Owen Andersonc277dc42009-07-16 19:05:41 +000055class ConstantFP;
Eugene Zelenkode6cce22017-06-19 22:05:08 +000056class ConstantInt;
Owen Anderson20b34ac2009-07-16 18:04:31 +000057class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000058class Value;
Eugene Zelenkode6cce22017-06-19 22:05:08 +000059class ValueHandleBase;
Owen Anderson20b34ac2009-07-16 18:04:31 +000060
Benjamin Kramer079b96e2013-09-11 18:05:11 +000061struct DenseMapAPIntKeyInfo {
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000062 static inline APInt getEmptyKey() {
63 APInt V(nullptr, 0);
Craig Topperb339c6d2017-05-03 15:46:24 +000064 V.U.VAL = 0;
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000065 return V;
66 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +000067
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000068 static inline APInt getTombstoneKey() {
69 APInt V(nullptr, 0);
Craig Topperb339c6d2017-05-03 15:46:24 +000070 V.U.VAL = 1;
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000071 return V;
72 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +000073
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000074 static unsigned getHashValue(const APInt &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000075 return static_cast<unsigned>(hash_value(Key));
Owen Anderson20b34ac2009-07-16 18:04:31 +000076 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +000077
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000078 static bool isEqual(const APInt &LHS, const APInt &RHS) {
79 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
80 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000081};
82
Benjamin Kramer079b96e2013-09-11 18:05:11 +000083struct DenseMapAPFloatKeyInfo {
Stephan Bergmann17c7f702016-12-14 11:57:17 +000084 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); }
85 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus(), 2); }
Eugene Zelenkode6cce22017-06-19 22:05:08 +000086
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000087 static unsigned getHashValue(const APFloat &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000088 return static_cast<unsigned>(hash_value(Key));
Owen Andersonc277dc42009-07-16 19:05:41 +000089 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +000090
Benjamin Kramer8e5dc532014-12-06 13:12:56 +000091 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
92 return LHS.bitwiseIsEqual(RHS);
93 }
Owen Andersonc277dc42009-07-16 19:05:41 +000094};
95
Benjamin Kramer079b96e2013-09-11 18:05:11 +000096struct AnonStructTypeKeyInfo {
Jay Foad529776c2012-02-23 09:17:40 +000097 struct KeyTy {
98 ArrayRef<Type*> ETypes;
99 bool isPacked;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000100
Jay Foad529776c2012-02-23 09:17:40 +0000101 KeyTy(const ArrayRef<Type*>& E, bool P) :
102 ETypes(E), isPacked(P) {}
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000103
Rafael Espindola334b73f2014-11-21 18:53:05 +0000104 KeyTy(const StructType *ST)
105 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000106
Jay Foad529776c2012-02-23 09:17:40 +0000107 bool operator==(const KeyTy& that) const {
108 if (isPacked != that.isPacked)
109 return false;
110 if (ETypes != that.ETypes)
111 return false;
112 return true;
113 }
114 bool operator!=(const KeyTy& that) const {
115 return !this->operator==(that);
116 }
117 };
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000118
Jay Foad529776c2012-02-23 09:17:40 +0000119 static inline StructType* getEmptyKey() {
120 return DenseMapInfo<StructType*>::getEmptyKey();
121 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000122
Jay Foad529776c2012-02-23 09:17:40 +0000123 static inline StructType* getTombstoneKey() {
124 return DenseMapInfo<StructType*>::getTombstoneKey();
125 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000126
Jay Foad529776c2012-02-23 09:17:40 +0000127 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000128 return hash_combine(hash_combine_range(Key.ETypes.begin(),
129 Key.ETypes.end()),
130 Key.isPacked);
Jay Foad529776c2012-02-23 09:17:40 +0000131 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000132
Jay Foad529776c2012-02-23 09:17:40 +0000133 static unsigned getHashValue(const StructType *ST) {
134 return getHashValue(KeyTy(ST));
135 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000136
Jay Foad529776c2012-02-23 09:17:40 +0000137 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
138 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
139 return false;
140 return LHS == KeyTy(RHS);
141 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000142
Jay Foad529776c2012-02-23 09:17:40 +0000143 static bool isEqual(const StructType *LHS, const StructType *RHS) {
144 return LHS == RHS;
145 }
146};
147
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000148struct FunctionTypeKeyInfo {
Jay Foad529776c2012-02-23 09:17:40 +0000149 struct KeyTy {
150 const Type *ReturnType;
151 ArrayRef<Type*> Params;
152 bool isVarArg;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000153
Jay Foad529776c2012-02-23 09:17:40 +0000154 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
155 ReturnType(R), Params(P), isVarArg(V) {}
Rafael Espindolae973fd42014-11-21 19:03:35 +0000156 KeyTy(const FunctionType *FT)
157 : ReturnType(FT->getReturnType()), Params(FT->params()),
158 isVarArg(FT->isVarArg()) {}
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000159
Jay Foad529776c2012-02-23 09:17:40 +0000160 bool operator==(const KeyTy& that) const {
161 if (ReturnType != that.ReturnType)
162 return false;
163 if (isVarArg != that.isVarArg)
164 return false;
165 if (Params != that.Params)
166 return false;
167 return true;
168 }
169 bool operator!=(const KeyTy& that) const {
170 return !this->operator==(that);
171 }
172 };
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000173
Jay Foad529776c2012-02-23 09:17:40 +0000174 static inline FunctionType* getEmptyKey() {
175 return DenseMapInfo<FunctionType*>::getEmptyKey();
176 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000177
Jay Foad529776c2012-02-23 09:17:40 +0000178 static inline FunctionType* getTombstoneKey() {
179 return DenseMapInfo<FunctionType*>::getTombstoneKey();
180 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000181
Jay Foad529776c2012-02-23 09:17:40 +0000182 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000183 return hash_combine(Key.ReturnType,
184 hash_combine_range(Key.Params.begin(),
185 Key.Params.end()),
186 Key.isVarArg);
Jay Foad529776c2012-02-23 09:17:40 +0000187 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000188
Jay Foad529776c2012-02-23 09:17:40 +0000189 static unsigned getHashValue(const FunctionType *FT) {
190 return getHashValue(KeyTy(FT));
191 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000192
Jay Foad529776c2012-02-23 09:17:40 +0000193 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
194 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
195 return false;
196 return LHS == KeyTy(RHS);
197 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000198
Jay Foad529776c2012-02-23 09:17:40 +0000199 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
200 return LHS == RHS;
201 }
202};
203
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000204/// Structure for hashing arbitrary MDNode operands.
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000205class MDNodeOpsKey {
206 ArrayRef<Metadata *> RawOps;
207 ArrayRef<MDOperand> Ops;
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000208 unsigned Hash;
209
210protected:
211 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
212 : RawOps(Ops), Hash(calculateHash(Ops)) {}
213
214 template <class NodeTy>
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000215 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000216 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000217
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000218 template <class NodeTy>
219 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000220 if (getHash() != RHS->getHash())
221 return false;
222
223 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000224 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
225 : compareOps(RawOps, RHS, Offset);
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000226 }
227
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000228 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000229
230private:
231 template <class T>
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000232 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
233 if (Ops.size() != RHS->getNumOperands() - Offset)
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000234 return false;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000235 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000236 }
237
238 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
239
240public:
241 unsigned getHash() const { return Hash; }
242};
243
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000244template <class NodeTy> struct MDNodeKeyImpl;
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000245
Duncan P. N. Exon Smithf2291272016-04-16 23:42:04 +0000246/// Configuration point for MDNodeInfo::isEqual().
247template <class NodeTy> struct MDNodeSubsetEqualImpl {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000248 using KeyTy = MDNodeKeyImpl<NodeTy>;
249
Duncan P. N. Exon Smithf2291272016-04-16 23:42:04 +0000250 static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) {
251 return false;
252 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000253
Duncan P. N. Exon Smithf2291272016-04-16 23:42:04 +0000254 static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) {
255 return false;
256 }
257};
258
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000259/// DenseMapInfo for MDTuple.
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000260///
261/// Note that we don't need the is-function-local bit, since that's implicit in
262/// the operands.
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000263template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
264 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
265 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
Duncan P. N. Exon Smithf39c3b82014-11-17 23:28:21 +0000266
Duncan P. N. Exon Smith9c26d802015-02-05 00:51:35 +0000267 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000268
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000269 unsigned getHashValue() const { return getHash(); }
270
271 static unsigned calculateHash(MDTuple *N) {
272 return MDNodeOpsKey::calculateHash(N);
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000273 }
274};
275
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000276/// DenseMapInfo for DILocation.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000277template <> struct MDNodeKeyImpl<DILocation> {
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000278 unsigned Line;
279 unsigned Column;
280 Metadata *Scope;
281 Metadata *InlinedAt;
Calixte Denizeteb7f6022018-09-20 08:53:06 +0000282 bool ImplicitCode;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000283
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000284 MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
Calixte Denizeteb7f6022018-09-20 08:53:06 +0000285 Metadata *InlinedAt, bool ImplicitCode)
286 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt),
287 ImplicitCode(ImplicitCode) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000288 MDNodeKeyImpl(const DILocation *L)
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +0000289 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
Calixte Denizeteb7f6022018-09-20 08:53:06 +0000290 InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {}
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000291
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000292 bool isKeyOf(const DILocation *RHS) const {
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000293 return Line == RHS->getLine() && Column == RHS->getColumn() &&
Calixte Denizeteb7f6022018-09-20 08:53:06 +0000294 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() &&
295 ImplicitCode == RHS->isImplicitCode();
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000296 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000297
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000298 unsigned getHashValue() const {
Calixte Denizeteb7f6022018-09-20 08:53:06 +0000299 return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000300 }
301};
302
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000303/// DenseMapInfo for GenericDINode.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000304template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000305 unsigned Tag;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000306 MDString *Header;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000307
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000308 MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000309 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000310 MDNodeKeyImpl(const GenericDINode *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000311 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {}
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000312
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000313 bool isKeyOf(const GenericDINode *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000314 return Tag == RHS->getTag() && Header == RHS->getRawHeader() &&
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000315 compareOps(RHS, 1);
316 }
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000317
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000318 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
319
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000320 static unsigned calculateHash(GenericDINode *N) {
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000321 return MDNodeOpsKey::calculateHash(N, 1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000322 }
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +0000323};
324
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000325template <> struct MDNodeKeyImpl<DISubrange> {
Sander de Smalenfdf40912018-01-24 09:56:07 +0000326 Metadata *CountNode;
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +0000327 int64_t LowerBound;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000328
Sander de Smalenfdf40912018-01-24 09:56:07 +0000329 MDNodeKeyImpl(Metadata *CountNode, int64_t LowerBound)
330 : CountNode(CountNode), LowerBound(LowerBound) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000331 MDNodeKeyImpl(const DISubrange *N)
Sander de Smalenfdf40912018-01-24 09:56:07 +0000332 : CountNode(N->getRawCountNode()),
333 LowerBound(N->getLowerBound()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000334
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000335 bool isKeyOf(const DISubrange *RHS) const {
Sander de Smalenfdf40912018-01-24 09:56:07 +0000336 if (LowerBound != RHS->getLowerBound())
337 return false;
338
339 if (auto *RHSCount = RHS->getCount().dyn_cast<ConstantInt*>())
340 if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode))
341 if (RHSCount->getSExtValue() ==
342 cast<ConstantInt>(MD->getValue())->getSExtValue())
343 return true;
344
345 return CountNode == RHS->getRawCountNode();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000346 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000347
Sander de Smalenfdf40912018-01-24 09:56:07 +0000348 unsigned getHashValue() const {
349 if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode))
350 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(),
351 LowerBound);
352 return hash_combine(CountNode, LowerBound);
353 }
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000354};
355
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000356template <> struct MDNodeKeyImpl<DIEnumerator> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000357 int64_t Value;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000358 MDString *Name;
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000359 bool IsUnsigned;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000360
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000361 MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name)
362 : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000363 MDNodeKeyImpl(const DIEnumerator *N)
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000364 : Value(N->getValue()), Name(N->getRawName()),
365 IsUnsigned(N->isUnsigned()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000366
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000367 bool isKeyOf(const DIEnumerator *RHS) const {
Momchil Velikov08dc66e2018-02-12 16:10:09 +0000368 return Value == RHS->getValue() && IsUnsigned == RHS->isUnsigned() &&
369 Name == RHS->getRawName();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000370 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000371
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000372 unsigned getHashValue() const { return hash_combine(Value, Name); }
373};
374
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000375template <> struct MDNodeKeyImpl<DIBasicType> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000376 unsigned Tag;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000377 MDString *Name;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000378 uint64_t SizeInBits;
Victor Leschuk197aa312016-10-18 14:31:22 +0000379 uint32_t AlignInBits;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000380 unsigned Encoding;
Adrian Prantl55f42622018-08-14 19:35:34 +0000381 unsigned Flags;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000382
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000383 MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
Adrian Prantl55f42622018-08-14 19:35:34 +0000384 uint32_t AlignInBits, unsigned Encoding, unsigned Flags)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000385 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
Adrian Prantl55f42622018-08-14 19:35:34 +0000386 Encoding(Encoding), Flags(Flags) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000387 MDNodeKeyImpl(const DIBasicType *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000388 : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
Adrian Prantl55f42622018-08-14 19:35:34 +0000389 AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()), Flags(N->getFlags()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000390
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000391 bool isKeyOf(const DIBasicType *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000392 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000393 SizeInBits == RHS->getSizeInBits() &&
394 AlignInBits == RHS->getAlignInBits() &&
Adrian Prantl55f42622018-08-14 19:35:34 +0000395 Encoding == RHS->getEncoding() &&
396 Flags == RHS->getFlags();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000397 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000398
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000399 unsigned getHashValue() const {
400 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
401 }
402};
403
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000404template <> struct MDNodeKeyImpl<DIDerivedType> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000405 unsigned Tag;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000406 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000407 Metadata *File;
408 unsigned Line;
409 Metadata *Scope;
410 Metadata *BaseType;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000411 uint64_t SizeInBits;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000412 uint64_t OffsetInBits;
Victor Leschuk197aa312016-10-18 14:31:22 +0000413 uint32_t AlignInBits;
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000414 Optional<unsigned> DWARFAddressSpace;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000415 unsigned Flags;
416 Metadata *ExtraData;
417
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000418 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000419 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000420 uint32_t AlignInBits, uint64_t OffsetInBits,
421 Optional<unsigned> DWARFAddressSpace, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000422 Metadata *ExtraData)
423 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
Victor Leschuk197aa312016-10-18 14:31:22 +0000424 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000425 AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace),
426 Flags(Flags), ExtraData(ExtraData) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000427 MDNodeKeyImpl(const DIDerivedType *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000428 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000429 Line(N->getLine()), Scope(N->getRawScope()),
430 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
Victor Leschuk197aa312016-10-18 14:31:22 +0000431 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000432 DWARFAddressSpace(N->getDWARFAddressSpace()), Flags(N->getFlags()),
433 ExtraData(N->getRawExtraData()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000434
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000435 bool isKeyOf(const DIDerivedType *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000436 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000437 File == RHS->getRawFile() && Line == RHS->getLine() &&
438 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000439 SizeInBits == RHS->getSizeInBits() &&
440 AlignInBits == RHS->getAlignInBits() &&
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +0000441 OffsetInBits == RHS->getOffsetInBits() &&
442 DWARFAddressSpace == RHS->getDWARFAddressSpace() &&
443 Flags == RHS->getFlags() &&
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000444 ExtraData == RHS->getRawExtraData();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000445 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000446
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000447 unsigned getHashValue() const {
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000448 // If this is a member inside an ODR type, only hash the type and the name.
449 // Otherwise the hash will be stronger than
450 // MDNodeSubsetEqualImpl::isODRMember().
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000451 if (Tag == dwarf::DW_TAG_member && Name)
452 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
453 if (CT->getRawIdentifier())
454 return hash_combine(Name, Scope);
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000455
Mehdi Amini9bc362a2016-03-19 01:06:24 +0000456 // Intentionally computes the hash on a subset of the operands for
457 // performance reason. The subset has to be significant enough to avoid
458 // collision "most of the time". There is no correctness issue in case of
459 // collision because of the full check above.
Mehdi Amini53fc3892016-03-19 00:59:26 +0000460 return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000461 }
462};
463
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000464template <> struct MDNodeSubsetEqualImpl<DIDerivedType> {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000465 using KeyTy = MDNodeKeyImpl<DIDerivedType>;
466
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000467 static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) {
468 return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS);
469 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000470
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000471 static bool isSubsetEqual(const DIDerivedType *LHS, const DIDerivedType *RHS) {
472 return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(),
473 RHS);
474 }
475
476 /// Subprograms compare equal if they declare the same function in an ODR
477 /// type.
478 static bool isODRMember(unsigned Tag, const Metadata *Scope,
479 const MDString *Name, const DIDerivedType *RHS) {
480 // Check whether the LHS is eligible.
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000481 if (Tag != dwarf::DW_TAG_member || !Name)
482 return false;
483
484 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
485 if (!CT || !CT->getRawIdentifier())
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000486 return false;
487
488 // Compare to the RHS.
489 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
490 Scope == RHS->getRawScope();
491 }
492};
493
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000494template <> struct MDNodeKeyImpl<DICompositeType> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000495 unsigned Tag;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000496 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000497 Metadata *File;
498 unsigned Line;
499 Metadata *Scope;
500 Metadata *BaseType;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000501 uint64_t SizeInBits;
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000502 uint64_t OffsetInBits;
Victor Leschuk197aa312016-10-18 14:31:22 +0000503 uint32_t AlignInBits;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000504 unsigned Flags;
505 Metadata *Elements;
506 unsigned RuntimeLang;
507 Metadata *VTableHolder;
508 Metadata *TemplateParams;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000509 MDString *Identifier;
Adrian Prantl8c599212018-02-06 23:45:59 +0000510 Metadata *Discriminator;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000511
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000512 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +0000513 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
Victor Leschuk197aa312016-10-18 14:31:22 +0000514 uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000515 Metadata *Elements, unsigned RuntimeLang,
516 Metadata *VTableHolder, Metadata *TemplateParams,
Adrian Prantl8c599212018-02-06 23:45:59 +0000517 MDString *Identifier, Metadata *Discriminator)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000518 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
Victor Leschuk197aa312016-10-18 14:31:22 +0000519 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
520 AlignInBits(AlignInBits), Flags(Flags), Elements(Elements),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000521 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
Adrian Prantl8c599212018-02-06 23:45:59 +0000522 TemplateParams(TemplateParams), Identifier(Identifier),
523 Discriminator(Discriminator) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000524 MDNodeKeyImpl(const DICompositeType *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000525 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000526 Line(N->getLine()), Scope(N->getRawScope()),
527 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
Victor Leschuk197aa312016-10-18 14:31:22 +0000528 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000529 Flags(N->getFlags()), Elements(N->getRawElements()),
530 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
531 TemplateParams(N->getRawTemplateParams()),
Adrian Prantl8c599212018-02-06 23:45:59 +0000532 Identifier(N->getRawIdentifier()),
533 Discriminator(N->getRawDiscriminator()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000534
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000535 bool isKeyOf(const DICompositeType *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000536 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000537 File == RHS->getRawFile() && Line == RHS->getLine() &&
538 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000539 SizeInBits == RHS->getSizeInBits() &&
540 AlignInBits == RHS->getAlignInBits() &&
541 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000542 Elements == RHS->getRawElements() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000543 RuntimeLang == RHS->getRuntimeLang() &&
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +0000544 VTableHolder == RHS->getRawVTableHolder() &&
545 TemplateParams == RHS->getRawTemplateParams() &&
Adrian Prantl8c599212018-02-06 23:45:59 +0000546 Identifier == RHS->getRawIdentifier() &&
547 Discriminator == RHS->getRawDiscriminator();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000548 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000549
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000550 unsigned getHashValue() const {
Mehdi Amini9bc362a2016-03-19 01:06:24 +0000551 // Intentionally computes the hash on a subset of the operands for
552 // performance reason. The subset has to be significant enough to avoid
553 // collision "most of the time". There is no correctness issue in case of
554 // collision because of the full check above.
Mehdi Amini53fc3892016-03-19 00:59:26 +0000555 return hash_combine(Name, File, Line, BaseType, Scope, Elements,
556 TemplateParams);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000557 }
558};
559
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000560template <> struct MDNodeKeyImpl<DISubroutineType> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000561 unsigned Flags;
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000562 uint8_t CC;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000563 Metadata *TypeArray;
564
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000565 MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray)
566 : Flags(Flags), CC(CC), TypeArray(TypeArray) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000567 MDNodeKeyImpl(const DISubroutineType *N)
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000568 : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000569
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000570 bool isKeyOf(const DISubroutineType *RHS) const {
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000571 return Flags == RHS->getFlags() && CC == RHS->getCC() &&
572 TypeArray == RHS->getRawTypeArray();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000573 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000574
Reid Klecknerde3d8b52016-06-08 20:34:29 +0000575 unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); }
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000576};
577
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000578template <> struct MDNodeKeyImpl<DIFile> {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000579 MDString *Filename;
580 MDString *Directory;
Scott Linder71603842018-02-12 19:45:54 +0000581 Optional<DIFile::ChecksumInfo<MDString *>> Checksum;
Scott Linder16c7bda2018-02-23 23:01:06 +0000582 Optional<MDString *> Source;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000583
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000584 MDNodeKeyImpl(MDString *Filename, MDString *Directory,
Scott Linder16c7bda2018-02-23 23:01:06 +0000585 Optional<DIFile::ChecksumInfo<MDString *>> Checksum,
586 Optional<MDString *> Source)
587 : Filename(Filename), Directory(Directory), Checksum(Checksum),
588 Source(Source) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000589 MDNodeKeyImpl(const DIFile *N)
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000590 : Filename(N->getRawFilename()), Directory(N->getRawDirectory()),
Scott Linder16c7bda2018-02-23 23:01:06 +0000591 Checksum(N->getRawChecksum()), Source(N->getRawSource()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000592
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000593 bool isKeyOf(const DIFile *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000594 return Filename == RHS->getRawFilename() &&
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000595 Directory == RHS->getRawDirectory() &&
Scott Linder16c7bda2018-02-23 23:01:06 +0000596 Checksum == RHS->getRawChecksum() &&
597 Source == RHS->getRawSource();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000598 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000599
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000600 unsigned getHashValue() const {
Scott Linder16c7bda2018-02-23 23:01:06 +0000601 return hash_combine(
602 Filename, Directory, Checksum ? Checksum->Kind : 0,
603 Checksum ? Checksum->Value : nullptr, Source.getValueOr(nullptr));
Amjad Aboud7faeecc2016-12-25 10:12:09 +0000604 }
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000605};
606
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000607template <> struct MDNodeKeyImpl<DISubprogram> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000608 Metadata *Scope;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000609 MDString *Name;
610 MDString *LinkageName;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000611 Metadata *File;
612 unsigned Line;
613 Metadata *Type;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000614 unsigned ScopeLine;
615 Metadata *ContainingType;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000616 unsigned VirtualIndex;
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000617 int ThisAdjustment;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000618 unsigned Flags;
Paul Robinsoncda54212018-11-19 18:29:28 +0000619 unsigned SPFlags;
Adrian Prantl75819ae2016-04-15 15:57:41 +0000620 Metadata *Unit;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000621 Metadata *TemplateParams;
622 Metadata *Declaration;
Shiva Chen2c864552018-05-09 02:40:45 +0000623 Metadata *RetainedNodes;
Adrian Prantl1d12b882017-04-26 22:56:44 +0000624 Metadata *ThrownTypes;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000625
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000626 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000627 Metadata *File, unsigned Line, Metadata *Type,
Paul Robinsoncda54212018-11-19 18:29:28 +0000628 unsigned ScopeLine, Metadata *ContainingType,
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000629 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags,
Paul Robinsoncda54212018-11-19 18:29:28 +0000630 unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams,
Shiva Chen2c864552018-05-09 02:40:45 +0000631 Metadata *Declaration, Metadata *RetainedNodes,
Adrian Prantl1d12b882017-04-26 22:56:44 +0000632 Metadata *ThrownTypes)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000633 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
Paul Robinsoncda54212018-11-19 18:29:28 +0000634 Line(Line), Type(Type), ScopeLine(ScopeLine),
635 ContainingType(ContainingType), VirtualIndex(VirtualIndex),
636 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags),
637 Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration),
Shiva Chen2c864552018-05-09 02:40:45 +0000638 RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000639 MDNodeKeyImpl(const DISubprogram *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000640 : Scope(N->getRawScope()), Name(N->getRawName()),
641 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
Paul Robinsoncda54212018-11-19 18:29:28 +0000642 Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()),
643 ContainingType(N->getRawContainingType()),
644 VirtualIndex(N->getVirtualIndex()),
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000645 ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
Paul Robinsoncda54212018-11-19 18:29:28 +0000646 SPFlags(N->getSPFlags()), Unit(N->getRawUnit()),
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000647 TemplateParams(N->getRawTemplateParams()),
Paul Robinsoncda54212018-11-19 18:29:28 +0000648 Declaration(N->getRawDeclaration()),
649 RetainedNodes(N->getRawRetainedNodes()),
Adrian Prantl1d12b882017-04-26 22:56:44 +0000650 ThrownTypes(N->getRawThrownTypes()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000651
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000652 bool isKeyOf(const DISubprogram *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000653 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
654 LinkageName == RHS->getRawLinkageName() &&
655 File == RHS->getRawFile() && Line == RHS->getLine() &&
Paul Robinsoncda54212018-11-19 18:29:28 +0000656 Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() &&
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000657 ContainingType == RHS->getRawContainingType() &&
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000658 VirtualIndex == RHS->getVirtualIndex() &&
659 ThisAdjustment == RHS->getThisAdjustment() &&
Paul Robinsoncda54212018-11-19 18:29:28 +0000660 Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() &&
Reid Klecknerb5af11d2016-07-01 02:41:21 +0000661 Unit == RHS->getUnit() &&
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +0000662 TemplateParams == RHS->getRawTemplateParams() &&
663 Declaration == RHS->getRawDeclaration() &&
Shiva Chen2c864552018-05-09 02:40:45 +0000664 RetainedNodes == RHS->getRawRetainedNodes() &&
Adrian Prantl1d12b882017-04-26 22:56:44 +0000665 ThrownTypes == RHS->getRawThrownTypes();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000666 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000667
Paul Robinsoncda54212018-11-19 18:29:28 +0000668 bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; }
669
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000670 unsigned getHashValue() const {
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000671 // If this is a declaration inside an ODR type, only hash the type and the
672 // name. Otherwise the hash will be stronger than
673 // MDNodeSubsetEqualImpl::isDeclarationOfODRMember().
Paul Robinsoncda54212018-11-19 18:29:28 +0000674 if (!isDefinition() && LinkageName)
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000675 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
676 if (CT->getRawIdentifier())
677 return hash_combine(LinkageName, Scope);
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000678
Mehdi Amini9bc362a2016-03-19 01:06:24 +0000679 // Intentionally computes the hash on a subset of the operands for
680 // performance reason. The subset has to be significant enough to avoid
681 // collision "most of the time". There is no correctness issue in case of
682 // collision because of the full check above.
Mehdi Amini53fc3892016-03-19 00:59:26 +0000683 return hash_combine(Name, Scope, File, Type, Line);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000684 }
685};
686
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000687template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000688 using KeyTy = MDNodeKeyImpl<DISubprogram>;
689
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000690 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
Paul Robinsoncda54212018-11-19 18:29:28 +0000691 return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope,
Peter Collingbournee69e73c2017-02-06 21:23:03 +0000692 LHS.LinkageName, LHS.TemplateParams, RHS);
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000693 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000694
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000695 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
696 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
Peter Collingbournee69e73c2017-02-06 21:23:03 +0000697 LHS->getRawLinkageName(),
698 LHS->getRawTemplateParams(), RHS);
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000699 }
700
701 /// Subprograms compare equal if they declare the same function in an ODR
702 /// type.
703 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
704 const MDString *LinkageName,
Peter Collingbournee69e73c2017-02-06 21:23:03 +0000705 const Metadata *TemplateParams,
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000706 const DISubprogram *RHS) {
707 // Check whether the LHS is eligible.
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000708 if (IsDefinition || !Scope || !LinkageName)
709 return false;
710
711 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
712 if (!CT || !CT->getRawIdentifier())
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000713 return false;
714
715 // Compare to the RHS.
Peter Collingbournee69e73c2017-02-06 21:23:03 +0000716 // FIXME: We need to compare template parameters here to avoid incorrect
717 // collisions in mapMetadata when RF_MoveDistinctMDs and a ODR-DISubprogram
718 // has a non-ODR template parameter (i.e., a DICompositeType that does not
719 // have an identifier). Eventually we should decouple ODR logic from
720 // uniquing logic.
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000721 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
Peter Collingbournee69e73c2017-02-06 21:23:03 +0000722 LinkageName == RHS->getRawLinkageName() &&
723 TemplateParams == RHS->getRawTemplateParams();
Duncan P. N. Exon Smith05ebfd02016-04-17 02:30:20 +0000724 }
725};
726
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000727template <> struct MDNodeKeyImpl<DILexicalBlock> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000728 Metadata *Scope;
729 Metadata *File;
730 unsigned Line;
731 unsigned Column;
732
733 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
734 : Scope(Scope), File(File), Line(Line), Column(Column) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000735 MDNodeKeyImpl(const DILexicalBlock *N)
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000736 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000737 Column(N->getColumn()) {}
738
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000739 bool isKeyOf(const DILexicalBlock *RHS) const {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000740 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000741 Line == RHS->getLine() && Column == RHS->getColumn();
742 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000743
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000744 unsigned getHashValue() const {
745 return hash_combine(Scope, File, Line, Column);
746 }
747};
748
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000749template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000750 Metadata *Scope;
751 Metadata *File;
752 unsigned Discriminator;
753
754 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
755 : Scope(Scope), File(File), Discriminator(Discriminator) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000756 MDNodeKeyImpl(const DILexicalBlockFile *N)
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000757 : Scope(N->getRawScope()), File(N->getRawFile()),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000758 Discriminator(N->getDiscriminator()) {}
759
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000760 bool isKeyOf(const DILexicalBlockFile *RHS) const {
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +0000761 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000762 Discriminator == RHS->getDiscriminator();
763 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000764
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000765 unsigned getHashValue() const {
766 return hash_combine(Scope, File, Discriminator);
767 }
768};
769
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000770template <> struct MDNodeKeyImpl<DINamespace> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000771 Metadata *Scope;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000772 MDString *Name;
Adrian Prantldbfda632016-11-03 19:42:02 +0000773 bool ExportSymbols;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000774
Adrian Prantlfed4f392017-04-28 22:25:46 +0000775 MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols)
776 : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000777 MDNodeKeyImpl(const DINamespace *N)
Adrian Prantlfed4f392017-04-28 22:25:46 +0000778 : Scope(N->getRawScope()), Name(N->getRawName()),
779 ExportSymbols(N->getExportSymbols()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000780
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000781 bool isKeyOf(const DINamespace *RHS) const {
Adrian Prantlfed4f392017-04-28 22:25:46 +0000782 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
Adrian Prantldbfda632016-11-03 19:42:02 +0000783 ExportSymbols == RHS->getExportSymbols();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000784 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000785
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000786 unsigned getHashValue() const {
Adrian Prantlfed4f392017-04-28 22:25:46 +0000787 return hash_combine(Scope, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000788 }
789};
790
Adrian Prantlab1243f2015-06-29 23:03:47 +0000791template <> struct MDNodeKeyImpl<DIModule> {
792 Metadata *Scope;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000793 MDString *Name;
794 MDString *ConfigurationMacros;
795 MDString *IncludePath;
796 MDString *ISysRoot;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000797
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000798 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
799 MDString *IncludePath, MDString *ISysRoot)
800 : Scope(Scope), Name(Name), ConfigurationMacros(ConfigurationMacros),
801 IncludePath(IncludePath), ISysRoot(ISysRoot) {}
Adrian Prantlab1243f2015-06-29 23:03:47 +0000802 MDNodeKeyImpl(const DIModule *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000803 : Scope(N->getRawScope()), Name(N->getRawName()),
804 ConfigurationMacros(N->getRawConfigurationMacros()),
805 IncludePath(N->getRawIncludePath()), ISysRoot(N->getRawISysRoot()) {}
Adrian Prantlab1243f2015-06-29 23:03:47 +0000806
807 bool isKeyOf(const DIModule *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000808 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
809 ConfigurationMacros == RHS->getRawConfigurationMacros() &&
810 IncludePath == RHS->getRawIncludePath() &&
811 ISysRoot == RHS->getRawISysRoot();
Adrian Prantlab1243f2015-06-29 23:03:47 +0000812 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000813
Adrian Prantlab1243f2015-06-29 23:03:47 +0000814 unsigned getHashValue() const {
815 return hash_combine(Scope, Name,
816 ConfigurationMacros, IncludePath, ISysRoot);
817 }
818};
819
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000820template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000821 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000822 Metadata *Type;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000823
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000824 MDNodeKeyImpl(MDString *Name, Metadata *Type) : Name(Name), Type(Type) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000825 MDNodeKeyImpl(const DITemplateTypeParameter *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000826 : Name(N->getRawName()), Type(N->getRawType()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000827
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000828 bool isKeyOf(const DITemplateTypeParameter *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000829 return Name == RHS->getRawName() && Type == RHS->getRawType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000830 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000831
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000832 unsigned getHashValue() const { return hash_combine(Name, Type); }
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000833};
834
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000835template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000836 unsigned Tag;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000837 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000838 Metadata *Type;
839 Metadata *Value;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000840
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000841 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, Metadata *Value)
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000842 : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000843 MDNodeKeyImpl(const DITemplateValueParameter *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000844 : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()),
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000845 Value(N->getValue()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000846
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000847 bool isKeyOf(const DITemplateValueParameter *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000848 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000849 Type == RHS->getRawType() && Value == RHS->getValue();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000850 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000851
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +0000852 unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000853};
854
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000855template <> struct MDNodeKeyImpl<DIGlobalVariable> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000856 Metadata *Scope;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000857 MDString *Name;
858 MDString *LinkageName;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000859 Metadata *File;
860 unsigned Line;
861 Metadata *Type;
862 bool IsLocalToUnit;
863 bool IsDefinition;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000864 Metadata *StaticDataMemberDeclaration;
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000865 Metadata *TemplateParams;
Victor Leschuka37660c2016-10-26 21:32:29 +0000866 uint32_t AlignInBits;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000867
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000868 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000869 Metadata *File, unsigned Line, Metadata *Type,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000870 bool IsLocalToUnit, bool IsDefinition,
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000871 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
872 uint32_t AlignInBits)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000873 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
874 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000875 IsDefinition(IsDefinition),
Victor Leschuk2ede1262016-10-20 00:13:12 +0000876 StaticDataMemberDeclaration(StaticDataMemberDeclaration),
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000877 TemplateParams(TemplateParams), AlignInBits(AlignInBits) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000878 MDNodeKeyImpl(const DIGlobalVariable *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000879 : Scope(N->getRawScope()), Name(N->getRawName()),
880 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000881 Line(N->getLine()), Type(N->getRawType()),
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000882 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
Victor Leschuk2ede1262016-10-20 00:13:12 +0000883 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()),
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000884 TemplateParams(N->getRawTemplateParams()),
Victor Leschuk2ede1262016-10-20 00:13:12 +0000885 AlignInBits(N->getAlignInBits()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000886
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000887 bool isKeyOf(const DIGlobalVariable *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000888 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
889 LinkageName == RHS->getRawLinkageName() &&
890 File == RHS->getRawFile() && Line == RHS->getLine() &&
891 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000892 IsDefinition == RHS->isDefinition() &&
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +0000893 StaticDataMemberDeclaration ==
Victor Leschuk2ede1262016-10-20 00:13:12 +0000894 RHS->getRawStaticDataMemberDeclaration() &&
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000895 TemplateParams == RHS->getRawTemplateParams() &&
Victor Leschuk2ede1262016-10-20 00:13:12 +0000896 AlignInBits == RHS->getAlignInBits();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000897 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000898
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000899 unsigned getHashValue() const {
Victor Leschuk2ede1262016-10-20 00:13:12 +0000900 // We do not use AlignInBits in hashing function here on purpose:
901 // in most cases this param for local variable is zero (for function param
902 // it is always zero). This leads to lots of hash collisions and errors on
903 // cases with lots of similar variables.
904 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
905 // generated IR is random for each run and test fails with Align included.
906 // TODO: make hashing work fine with such situations
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000907 return hash_combine(Scope, Name, LinkageName, File, Line, Type,
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000908 IsLocalToUnit, IsDefinition, /* AlignInBits, */
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000909 StaticDataMemberDeclaration);
910 }
911};
912
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000913template <> struct MDNodeKeyImpl<DILocalVariable> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000914 Metadata *Scope;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000915 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000916 Metadata *File;
917 unsigned Line;
918 Metadata *Type;
919 unsigned Arg;
920 unsigned Flags;
Victor Leschuka37660c2016-10-26 21:32:29 +0000921 uint32_t AlignInBits;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000922
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000923 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line,
Victor Leschuk2ede1262016-10-20 00:13:12 +0000924 Metadata *Type, unsigned Arg, unsigned Flags,
Victor Leschuka37660c2016-10-26 21:32:29 +0000925 uint32_t AlignInBits)
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000926 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
Victor Leschuk2ede1262016-10-20 00:13:12 +0000927 Flags(Flags), AlignInBits(AlignInBits) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000928 MDNodeKeyImpl(const DILocalVariable *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000929 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000930 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
Victor Leschuk2ede1262016-10-20 00:13:12 +0000931 Flags(N->getFlags()), AlignInBits(N->getAlignInBits()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000932
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000933 bool isKeyOf(const DILocalVariable *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +0000934 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000935 File == RHS->getRawFile() && Line == RHS->getLine() &&
936 Type == RHS->getRawType() && Arg == RHS->getArg() &&
Victor Leschuk2ede1262016-10-20 00:13:12 +0000937 Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000938 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000939
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000940 unsigned getHashValue() const {
Victor Leschuk2ede1262016-10-20 00:13:12 +0000941 // We do not use AlignInBits in hashing function here on purpose:
942 // in most cases this param for local variable is zero (for function param
943 // it is always zero). This leads to lots of hash collisions and errors on
944 // cases with lots of similar variables.
945 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
946 // generated IR is random for each run and test fails with Align included.
947 // TODO: make hashing work fine with such situations
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +0000948 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000949 }
950};
951
Shiva Chen2c864552018-05-09 02:40:45 +0000952template <> struct MDNodeKeyImpl<DILabel> {
953 Metadata *Scope;
954 MDString *Name;
955 Metadata *File;
956 unsigned Line;
957
958 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line)
959 : Scope(Scope), Name(Name), File(File), Line(Line) {}
960 MDNodeKeyImpl(const DILabel *N)
961 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
962 Line(N->getLine()) {}
963
964 bool isKeyOf(const DILabel *RHS) const {
965 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
966 File == RHS->getRawFile() && Line == RHS->getLine();
967 }
968
969 /// Using name and line to get hash value. It should already be mostly unique.
970 unsigned getHashValue() const {
971 return hash_combine(Scope, Name, Line);
972 }
973};
974
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000975template <> struct MDNodeKeyImpl<DIExpression> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000976 ArrayRef<uint64_t> Elements;
977
978 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000979 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000980
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000981 bool isKeyOf(const DIExpression *RHS) const {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000982 return Elements == RHS->getElements();
983 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000984
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000985 unsigned getHashValue() const {
986 return hash_combine_range(Elements.begin(), Elements.end());
987 }
988};
989
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000990template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> {
991 Metadata *Variable;
992 Metadata *Expression;
993
994 MDNodeKeyImpl(Metadata *Variable, Metadata *Expression)
995 : Variable(Variable), Expression(Expression) {}
996 MDNodeKeyImpl(const DIGlobalVariableExpression *N)
997 : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {}
998
999 bool isKeyOf(const DIGlobalVariableExpression *RHS) const {
1000 return Variable == RHS->getRawVariable() &&
1001 Expression == RHS->getRawExpression();
1002 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001003
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001004 unsigned getHashValue() const { return hash_combine(Variable, Expression); }
1005};
1006
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001007template <> struct MDNodeKeyImpl<DIObjCProperty> {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001008 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001009 Metadata *File;
1010 unsigned Line;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001011 MDString *GetterName;
1012 MDString *SetterName;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001013 unsigned Attributes;
1014 Metadata *Type;
1015
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001016 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line,
1017 MDString *GetterName, MDString *SetterName, unsigned Attributes,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001018 Metadata *Type)
1019 : Name(Name), File(File), Line(Line), GetterName(GetterName),
1020 SetterName(SetterName), Attributes(Attributes), Type(Type) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001021 MDNodeKeyImpl(const DIObjCProperty *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001022 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()),
1023 GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()),
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001024 Attributes(N->getAttributes()), Type(N->getRawType()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001025
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001026 bool isKeyOf(const DIObjCProperty *RHS) const {
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001027 return Name == RHS->getRawName() && File == RHS->getRawFile() &&
1028 Line == RHS->getLine() && GetterName == RHS->getRawGetterName() &&
1029 SetterName == RHS->getRawSetterName() &&
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001030 Attributes == RHS->getAttributes() && Type == RHS->getRawType();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001031 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001032
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001033 unsigned getHashValue() const {
1034 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
1035 Type);
1036 }
1037};
1038
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001039template <> struct MDNodeKeyImpl<DIImportedEntity> {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001040 unsigned Tag;
1041 Metadata *Scope;
1042 Metadata *Entity;
Adrian Prantld63bfd22017-07-19 00:09:54 +00001043 Metadata *File;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001044 unsigned Line;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001045 MDString *Name;
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001046
Adrian Prantld63bfd22017-07-19 00:09:54 +00001047 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File,
1048 unsigned Line, MDString *Name)
1049 : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line),
1050 Name(Name) {}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001051 MDNodeKeyImpl(const DIImportedEntity *N)
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001052 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
Adrian Prantld63bfd22017-07-19 00:09:54 +00001053 File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()) {}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001054
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001055 bool isKeyOf(const DIImportedEntity *RHS) const {
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001056 return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
Adrian Prantld63bfd22017-07-19 00:09:54 +00001057 Entity == RHS->getRawEntity() && File == RHS->getFile() &&
1058 Line == RHS->getLine() && Name == RHS->getRawName();
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001059 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001060
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001061 unsigned getHashValue() const {
Adrian Prantld63bfd22017-07-19 00:09:54 +00001062 return hash_combine(Tag, Scope, Entity, File, Line, Name);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001063 }
1064};
1065
Amjad Abouda9bcf162015-12-10 12:56:35 +00001066template <> struct MDNodeKeyImpl<DIMacro> {
1067 unsigned MIType;
1068 unsigned Line;
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001069 MDString *Name;
1070 MDString *Value;
Amjad Abouda9bcf162015-12-10 12:56:35 +00001071
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001072 MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value)
Amjad Abouda9bcf162015-12-10 12:56:35 +00001073 : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
1074 MDNodeKeyImpl(const DIMacro *N)
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001075 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()),
1076 Value(N->getRawValue()) {}
Amjad Abouda9bcf162015-12-10 12:56:35 +00001077
1078 bool isKeyOf(const DIMacro *RHS) const {
1079 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
Mehdi Amini5d99c4e2016-03-19 01:02:34 +00001080 Name == RHS->getRawName() && Value == RHS->getRawValue();
Amjad Abouda9bcf162015-12-10 12:56:35 +00001081 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001082
Amjad Abouda9bcf162015-12-10 12:56:35 +00001083 unsigned getHashValue() const {
1084 return hash_combine(MIType, Line, Name, Value);
1085 }
1086};
1087
1088template <> struct MDNodeKeyImpl<DIMacroFile> {
1089 unsigned MIType;
1090 unsigned Line;
1091 Metadata *File;
1092 Metadata *Elements;
1093
1094 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
1095 Metadata *Elements)
1096 : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
1097 MDNodeKeyImpl(const DIMacroFile *N)
1098 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
1099 Elements(N->getRawElements()) {}
1100
1101 bool isKeyOf(const DIMacroFile *RHS) const {
1102 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
Amjad Aboud580498d2016-07-31 14:41:50 +00001103 File == RHS->getRawFile() && Elements == RHS->getRawElements();
Amjad Abouda9bcf162015-12-10 12:56:35 +00001104 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001105
Amjad Abouda9bcf162015-12-10 12:56:35 +00001106 unsigned getHashValue() const {
1107 return hash_combine(MIType, Line, File, Elements);
1108 }
1109};
1110
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001111/// DenseMapInfo for MDNode subclasses.
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001112template <class NodeTy> struct MDNodeInfo {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001113 using KeyTy = MDNodeKeyImpl<NodeTy>;
1114 using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>;
1115
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001116 static inline NodeTy *getEmptyKey() {
1117 return DenseMapInfo<NodeTy *>::getEmptyKey();
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001118 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001119
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001120 static inline NodeTy *getTombstoneKey() {
1121 return DenseMapInfo<NodeTy *>::getTombstoneKey();
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001122 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001123
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001124 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001125
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001126 static unsigned getHashValue(const NodeTy *N) {
1127 return KeyTy(N).getHashValue();
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001128 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001129
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001130 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
1131 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1132 return false;
Duncan P. N. Exon Smithf2291272016-04-16 23:42:04 +00001133 return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001134 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001135
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001136 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
Duncan P. N. Exon Smithf2291272016-04-16 23:42:04 +00001137 if (LHS == RHS)
1138 return true;
1139 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1140 return false;
1141 return SubsetEqualTy::isSubsetEqual(LHS, RHS);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001142 }
1143};
1144
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001145#define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>;
Duncan P. N. Exon Smith8af6cfc2015-02-04 22:08:30 +00001146#include "llvm/IR/Metadata.def"
1147
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001148/// Map-like storage for metadata attachments.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001149class MDAttachmentMap {
1150 SmallVector<std::pair<unsigned, TrackingMDNodeRef>, 2> Attachments;
1151
1152public:
1153 bool empty() const { return Attachments.empty(); }
1154 size_t size() const { return Attachments.size(); }
1155
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001156 /// Get a particular attachment (if any).
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001157 MDNode *lookup(unsigned ID) const;
1158
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001159 /// Set an attachment to a particular node.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001160 ///
1161 /// Set the \c ID attachment to \c MD, replacing the current attachment at \c
1162 /// ID (if anyway).
1163 void set(unsigned ID, MDNode &MD);
1164
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001165 /// Remove an attachment.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001166 ///
1167 /// Remove the attachment at \c ID, if any.
Benjamin Kramer0deb9a92018-05-31 13:29:58 +00001168 bool erase(unsigned ID);
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001169
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001170 /// Copy out all the attachments.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001171 ///
1172 /// Copies all the current attachments into \c Result, sorting by attachment
1173 /// ID. This function does \em not clear \c Result.
1174 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1175
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001176 /// Erase matching attachments.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001177 ///
1178 /// Erases all attachments matching the \c shouldRemove predicate.
1179 template <class PredTy> void remove_if(PredTy shouldRemove) {
David Majnemer2d006e72016-08-12 04:32:42 +00001180 Attachments.erase(llvm::remove_if(Attachments, shouldRemove),
1181 Attachments.end());
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001182 }
1183};
1184
Peter Collingbourne382d81c2016-06-01 01:17:57 +00001185/// Multimap-like storage for metadata attachments for globals. This differs
1186/// from MDAttachmentMap in that it allows multiple attachments per metadata
1187/// kind.
1188class MDGlobalAttachmentMap {
1189 struct Attachment {
1190 unsigned MDKind;
1191 TrackingMDNodeRef Node;
1192 };
1193 SmallVector<Attachment, 1> Attachments;
1194
1195public:
1196 bool empty() const { return Attachments.empty(); }
1197
1198 /// Appends all attachments with the given ID to \c Result in insertion order.
1199 /// If the global has no attachments with the given ID, or if ID is invalid,
1200 /// leaves Result unchanged.
Benjamin Kramer0deb9a92018-05-31 13:29:58 +00001201 void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const;
1202
1203 /// Returns the first attachment with the given ID or nullptr if no such
1204 /// attachment exists.
1205 MDNode *lookup(unsigned ID) const;
Peter Collingbourne382d81c2016-06-01 01:17:57 +00001206
1207 void insert(unsigned ID, MDNode &MD);
Benjamin Kramer0deb9a92018-05-31 13:29:58 +00001208 bool erase(unsigned ID);
Peter Collingbourne382d81c2016-06-01 01:17:57 +00001209
1210 /// Appends all attachments for the global to \c Result, sorting by attachment
1211 /// ID. Attachments with the same ID appear in insertion order. This function
1212 /// does \em not clear \c Result.
1213 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1214};
1215
Benjamin Kramer079b96e2013-09-11 18:05:11 +00001216class LLVMContextImpl {
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +00001217public:
Owen Anderson8e89e412010-09-08 18:03:32 +00001218 /// OwnedModules - The set of modules instantiated in this context, and which
1219 /// will be automatically deleted if this context is deleted.
1220 SmallPtrSet<Module*, 4> OwnedModules;
Fangrui Songf78650a2018-07-30 19:41:25 +00001221
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001222 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler = nullptr;
1223 void *InlineAsmDiagContext = nullptr;
Quentin Colombetb4c44d22013-12-17 17:47:22 +00001224
Vivek Pandyab5ab8952017-09-15 20:10:09 +00001225 std::unique_ptr<DiagnosticHandler> DiagHandler;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001226 bool RespectDiagnosticFilters = false;
Brian Gesiak44e5f6c2017-06-30 18:13:59 +00001227 bool DiagnosticsHotnessRequested = false;
Brian Gesiak4ef3daa2017-06-30 23:14:53 +00001228 uint64_t DiagnosticsHotnessThreshold = 0;
Adam Nemeta62b7e12016-09-27 20:55:07 +00001229 std::unique_ptr<yaml::Output> DiagnosticsOutputFile;
Quentin Colombetb4c44d22013-12-17 17:47:22 +00001230
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001231 LLVMContext::YieldCallbackTy YieldCallback = nullptr;
1232 void *YieldOpaqueHandle = nullptr;
Juergen Ributzka34390c72014-05-16 02:33:15 +00001233
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001234 using IntMapTy =
1235 DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>;
Owen Anderson20b34ac2009-07-16 18:04:31 +00001236 IntMapTy IntConstants;
NAKAMURA Takumifc3062f2014-12-06 05:57:06 +00001237
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001238 using FPMapTy =
1239 DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>;
Owen Andersonc277dc42009-07-16 19:05:41 +00001240 FPMapTy FPConstants;
Bill Wendlinge38b8042012-09-26 21:07:29 +00001241
Bill Wendling4607f4b2012-12-20 01:36:59 +00001242 FoldingSet<AttributeImpl> AttrsSet;
Reid Klecknerb5180542017-03-21 16:57:19 +00001243 FoldingSet<AttributeListImpl> AttrsLists;
Bill Wendlingd2e493b2013-01-24 00:06:56 +00001244 FoldingSet<AttributeSetNode> AttrsSetNodes;
Bill Wendlingf86efb92012-11-20 05:09:20 +00001245
Duncan P. N. Exon Smith3e0430e2016-04-06 06:41:54 +00001246 StringMap<MDString, BumpPtrAllocator> MDStringCache;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001247 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
1248 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
Bill Wendlinge38b8042012-09-26 21:07:29 +00001249
Owen Anderson7349ab92015-06-01 22:24:01 +00001250 DenseMap<const Value*, ValueName*> ValueNames;
1251
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00001252#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
1253 DenseSet<CLASS *, CLASS##Info> CLASS##s;
Duncan P. N. Exon Smith104e4022015-02-04 21:46:12 +00001254#include "llvm/IR/Metadata.def"
Bill Wendlinge38b8042012-09-26 21:07:29 +00001255
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +00001256 // Optional map for looking up composite types by identifier.
Duncan P. N. Exon Smithe8b555c2016-04-19 16:06:50 +00001257 Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap;
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +00001258
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +00001259 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
1260 // aren't in the MDNodeSet, but they're still shared between objects, so no
Duncan P. N. Exon Smith3eef9d12016-04-19 23:59:13 +00001261 // one object can destroy them. Keep track of them here so we can delete
1262 // them on context teardown.
1263 std::vector<MDNode *> DistinctMDNodes;
Duncan P. N. Exon Smith50846f82014-11-18 00:37:17 +00001264
Justin Lebar611c5c22016-10-10 16:26:13 +00001265 DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants;
Owen Anderson13234f82009-08-10 18:16:08 +00001266
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001267 using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>;
Owen Andersonedb4a702009-07-24 23:12:02 +00001268 ArrayConstantsTy ArrayConstants;
Fangrui Songf78650a2018-07-30 19:41:25 +00001269
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001270 using StructConstantsTy = ConstantUniqueMap<ConstantStruct>;
Owen Andersonedb4a702009-07-24 23:12:02 +00001271 StructConstantsTy StructConstants;
Fangrui Songf78650a2018-07-30 19:41:25 +00001272
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001273 using VectorConstantsTy = ConstantUniqueMap<ConstantVector>;
Owen Andersonedb4a702009-07-24 23:12:02 +00001274 VectorConstantsTy VectorConstants;
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001275
Justin Lebar611c5c22016-10-10 16:26:13 +00001276 DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants;
1277
1278 DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants;
1279
Chris Lattner3756b912012-01-23 22:57:10 +00001280 StringMap<ConstantDataSequential*> CDSConstants;
1281
Chandler Carruth6a936922014-01-19 02:13:50 +00001282 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
1283 BlockAddresses;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001284 ConstantUniqueMap<ConstantExpr> ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +00001285
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001286 ConstantUniqueMap<InlineAsm> InlineAsms;
1287
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001288 ConstantInt *TheTrueVal = nullptr;
1289 ConstantInt *TheFalseVal = nullptr;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001290
David Majnemer2dd41c52015-11-16 20:55:57 +00001291 std::unique_ptr<ConstantTokenNone> TheNoneToken;
David Majnemerf0f224d2015-11-11 21:57:16 +00001292
Dan Gohman97d2cb82009-08-25 16:00:35 +00001293 // Basic type instances.
David Majnemerb611e3f2015-08-14 05:09:07 +00001294 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy, TokenTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001295 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
Kit Barton72918022015-04-17 15:32:15 +00001296 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
Fangrui Songf78650a2018-07-30 19:41:25 +00001297
Chris Lattner07bd69c2011-07-15 05:49:15 +00001298 /// TypeAllocator - All dynamically allocated types are allocated from this.
1299 /// They live forever until the context is torn down.
1300 BumpPtrAllocator TypeAllocator;
Fangrui Songf78650a2018-07-30 19:41:25 +00001301
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001302 DenseMap<unsigned, IntegerType*> IntegerTypes;
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00001303
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001304 using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>;
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00001305 FunctionTypeSet FunctionTypes;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001306 using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>;
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00001307 StructTypeSet AnonStructTypes;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001308 StringMap<StructType*> NamedStructTypes;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001309 unsigned NamedStructTypesUniqueID = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00001310
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001311 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
1312 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
1313 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
1314 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +00001315
Owen Andersone8f21852009-08-18 18:28:58 +00001316 /// ValueHandles - This map keeps track of all of the value handles that are
1317 /// watching a Value*. The Value::HasValueHandle bit is used to know
Michael Ilseman516d7032013-03-01 18:48:54 +00001318 /// whether or not a value has an entry in this map.
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001319 using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>;
Owen Andersone8f21852009-08-18 18:28:58 +00001320 ValueHandlesTy ValueHandles;
Fangrui Songf78650a2018-07-30 19:41:25 +00001321
Chris Lattnera0566972009-12-29 09:01:33 +00001322 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
1323 StringMap<unsigned> CustomMDKindNames;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001324
Duncan P. N. Exon Smith391fc562015-04-24 20:16:42 +00001325 /// Collection of per-instruction metadata used in this context.
Duncan P. N. Exon Smithcbc28dc2015-04-24 20:36:25 +00001326 DenseMap<const Instruction *, MDAttachmentMap> InstructionMetadata;
Duncan P. N. Exon Smith391fc562015-04-24 20:16:42 +00001327
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001328 /// Collection of per-GlobalObject metadata used in this context.
Peter Collingbourne382d81c2016-06-01 01:17:57 +00001329 DenseMap<const GlobalObject *, MDGlobalAttachmentMap> GlobalObjectMetadata;
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +00001330
Reid Kleckner443423e2017-01-10 23:23:58 +00001331 /// Collection of per-GlobalObject sections used in this context.
1332 DenseMap<const GlobalObject *, StringRef> GlobalObjectSections;
1333
1334 /// Stable collection of section strings.
1335 StringSet<> SectionStrings;
1336
Diego Novillof5041ce2014-03-03 20:06:11 +00001337 /// DiscriminatorTable - This table maps file:line locations to an
1338 /// integer representing the next DWARF path discriminator to assign to
1339 /// instructions in different blocks at the same location.
1340 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
1341
Chris Lattner8cb2aeb2010-04-01 00:37:44 +00001342 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
1343 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
Philip Reames2b453952015-01-16 20:07:33 +00001344
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001345 /// A set of interned tags for operand bundles. The StringMap maps
Sanjoy Das9303c242015-09-24 19:14:18 +00001346 /// bundle tags to their IDs.
1347 ///
1348 /// \see LLVMContext::getOperandBundleTagID
1349 StringMap<uint32_t> BundleTagCache;
1350
1351 StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
1352 void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
1353 uint32_t getOperandBundleTagID(StringRef Tag) const;
1354
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001355 /// A set of interned synchronization scopes. The StringMap maps
1356 /// synchronization scope names to their respective synchronization scope IDs.
1357 StringMap<SyncScope::ID> SSC;
1358
1359 /// getOrInsertSyncScopeID - Maps synchronization scope name to
1360 /// synchronization scope ID. Every synchronization scope registered with
1361 /// LLVMContext has unique ID except pre-defined ones.
1362 SyncScope::ID getOrInsertSyncScopeID(StringRef SSN);
1363
1364 /// getSyncScopeNames - Populates client supplied SmallVector with
1365 /// synchronization scope names registered with LLVMContext. Synchronization
1366 /// scope names are ordered by increasing synchronization scope IDs.
1367 void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const;
1368
Mehdi Amini599ebf22016-01-08 02:28:20 +00001369 /// Maintain the GC name for each function.
1370 ///
1371 /// This saves allocating an additional word in Function for programs which
1372 /// do not use GC (i.e., most programs) at the cost of increased overhead for
1373 /// clients which do use GC.
1374 DenseMap<const Function*, std::string> GCNames;
1375
Mehdi Amini09b4a8d2016-03-10 01:28:54 +00001376 /// Flag to indicate if Value (other than GlobalValue) retains their name or
1377 /// not.
1378 bool DiscardValueNames = false;
1379
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001380 LLVMContextImpl(LLVMContext &C);
1381 ~LLVMContextImpl();
Manman Rendab999d2015-01-20 19:24:59 +00001382
1383 /// Destroy the ConstantArrays if they are not used.
1384 void dropTriviallyDeadConstantArrays();
Andrew Kayloraa641a52016-04-22 22:06:11 +00001385
Fedor Sergeevd29884c2018-04-05 10:29:37 +00001386 mutable OptPassGate *OPG = nullptr;
1387
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001388 /// Access the object which can disable optional passes and individual
Fedor Sergeevd29884c2018-04-05 10:29:37 +00001389 /// optimizations at compile time.
1390 OptPassGate &getOptPassGate() const;
1391
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001392 /// Set the object which can disable optional passes and individual
Fedor Sergeevd29884c2018-04-05 10:29:37 +00001393 /// optimizations at compile time.
1394 ///
1395 /// The lifetime of the object must be guaranteed to extend as long as the
1396 /// LLVMContext is used by compilation.
1397 void setOptPassGate(OptPassGate&);
Owen Anderson8e66e0b2009-06-30 00:48:55 +00001398};
1399
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001400} // end namespace llvm
Owen Anderson8e66e0b2009-06-30 00:48:55 +00001401
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001402#endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H