1d410692b6aa245eaf4fca2fcba27d763c17dfc5
[oota-llvm.git] / lib / IR / LLVMContextImpl.h
1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file declares LLVMContextImpl, the opaque implementation 
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
17
18 #include "AttributeImpl.h"
19 #include "ConstantsContext.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DebugInfoMetadata.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include <vector>
36
37 namespace llvm {
38
39 class ConstantInt;
40 class ConstantFP;
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
44 class GCStrategy;
45 class LLVMContext;
46 class Type;
47 class Value;
48
49 struct DenseMapAPIntKeyInfo {
50   static inline APInt getEmptyKey() {
51     APInt V(nullptr, 0);
52     V.VAL = 0;
53     return V;
54   }
55   static inline APInt getTombstoneKey() {
56     APInt V(nullptr, 0);
57     V.VAL = 1;
58     return V;
59   }
60   static unsigned getHashValue(const APInt &Key) {
61     return static_cast<unsigned>(hash_value(Key));
62   }
63   static bool isEqual(const APInt &LHS, const APInt &RHS) {
64     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
65   }
66 };
67
68 struct DenseMapAPFloatKeyInfo {
69   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
70   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
71   static unsigned getHashValue(const APFloat &Key) {
72     return static_cast<unsigned>(hash_value(Key));
73   }
74   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
75     return LHS.bitwiseIsEqual(RHS);
76   }
77 };
78
79 struct AnonStructTypeKeyInfo {
80   struct KeyTy {
81     ArrayRef<Type*> ETypes;
82     bool isPacked;
83     KeyTy(const ArrayRef<Type*>& E, bool P) :
84       ETypes(E), isPacked(P) {}
85     KeyTy(const StructType *ST)
86         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
87     bool operator==(const KeyTy& that) const {
88       if (isPacked != that.isPacked)
89         return false;
90       if (ETypes != that.ETypes)
91         return false;
92       return true;
93     }
94     bool operator!=(const KeyTy& that) const {
95       return !this->operator==(that);
96     }
97   };
98   static inline StructType* getEmptyKey() {
99     return DenseMapInfo<StructType*>::getEmptyKey();
100   }
101   static inline StructType* getTombstoneKey() {
102     return DenseMapInfo<StructType*>::getTombstoneKey();
103   }
104   static unsigned getHashValue(const KeyTy& Key) {
105     return hash_combine(hash_combine_range(Key.ETypes.begin(),
106                                            Key.ETypes.end()),
107                         Key.isPacked);
108   }
109   static unsigned getHashValue(const StructType *ST) {
110     return getHashValue(KeyTy(ST));
111   }
112   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
113     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
114       return false;
115     return LHS == KeyTy(RHS);
116   }
117   static bool isEqual(const StructType *LHS, const StructType *RHS) {
118     return LHS == RHS;
119   }
120 };
121
122 struct FunctionTypeKeyInfo {
123   struct KeyTy {
124     const Type *ReturnType;
125     ArrayRef<Type*> Params;
126     bool isVarArg;
127     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
128       ReturnType(R), Params(P), isVarArg(V) {}
129     KeyTy(const FunctionType *FT)
130         : ReturnType(FT->getReturnType()), Params(FT->params()),
131           isVarArg(FT->isVarArg()) {}
132     bool operator==(const KeyTy& that) const {
133       if (ReturnType != that.ReturnType)
134         return false;
135       if (isVarArg != that.isVarArg)
136         return false;
137       if (Params != that.Params)
138         return false;
139       return true;
140     }
141     bool operator!=(const KeyTy& that) const {
142       return !this->operator==(that);
143     }
144   };
145   static inline FunctionType* getEmptyKey() {
146     return DenseMapInfo<FunctionType*>::getEmptyKey();
147   }
148   static inline FunctionType* getTombstoneKey() {
149     return DenseMapInfo<FunctionType*>::getTombstoneKey();
150   }
151   static unsigned getHashValue(const KeyTy& Key) {
152     return hash_combine(Key.ReturnType,
153                         hash_combine_range(Key.Params.begin(),
154                                            Key.Params.end()),
155                         Key.isVarArg);
156   }
157   static unsigned getHashValue(const FunctionType *FT) {
158     return getHashValue(KeyTy(FT));
159   }
160   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
161     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
162       return false;
163     return LHS == KeyTy(RHS);
164   }
165   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
166     return LHS == RHS;
167   }
168 };
169
170 /// \brief Structure for hashing arbitrary MDNode operands.
171 class MDNodeOpsKey {
172   ArrayRef<Metadata *> RawOps;
173   ArrayRef<MDOperand> Ops;
174
175   unsigned Hash;
176
177 protected:
178   MDNodeOpsKey(ArrayRef<Metadata *> Ops)
179       : RawOps(Ops), Hash(calculateHash(Ops)) {}
180
181   template <class NodeTy>
182   MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
183       : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
184
185   template <class NodeTy>
186   bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
187     if (getHash() != RHS->getHash())
188       return false;
189
190     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
191     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
192                           : compareOps(RawOps, RHS, Offset);
193   }
194
195   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
196
197 private:
198   template <class T>
199   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
200     if (Ops.size() != RHS->getNumOperands() - Offset)
201       return false;
202     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
203   }
204
205   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
206
207 public:
208   unsigned getHash() const { return Hash; }
209 };
210
211 template <class NodeTy> struct MDNodeKeyImpl;
212 template <class NodeTy> struct MDNodeInfo;
213
214 /// \brief DenseMapInfo for MDTuple.
215 ///
216 /// Note that we don't need the is-function-local bit, since that's implicit in
217 /// the operands.
218 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
219   MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
220   MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
221
222   bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
223
224   unsigned getHashValue() const { return getHash(); }
225
226   static unsigned calculateHash(MDTuple *N) {
227     return MDNodeOpsKey::calculateHash(N);
228   }
229 };
230
231 /// \brief DenseMapInfo for DILocation.
232 template <> struct MDNodeKeyImpl<DILocation> {
233   unsigned Line;
234   unsigned Column;
235   Metadata *Scope;
236   Metadata *InlinedAt;
237
238   MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
239                 Metadata *InlinedAt)
240       : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
241
242   MDNodeKeyImpl(const DILocation *L)
243       : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
244         InlinedAt(L->getRawInlinedAt()) {}
245
246   bool isKeyOf(const DILocation *RHS) const {
247     return Line == RHS->getLine() && Column == RHS->getColumn() &&
248            Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt();
249   }
250   unsigned getHashValue() const {
251     return hash_combine(Line, Column, Scope, InlinedAt);
252   }
253 };
254
255 /// \brief DenseMapInfo for GenericDINode.
256 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
257   unsigned Tag;
258   StringRef Header;
259   MDNodeKeyImpl(unsigned Tag, StringRef Header, ArrayRef<Metadata *> DwarfOps)
260       : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
261   MDNodeKeyImpl(const GenericDINode *N)
262       : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
263
264   bool isKeyOf(const GenericDINode *RHS) const {
265     return Tag == RHS->getTag() && Header == RHS->getHeader() &&
266            compareOps(RHS, 1);
267   }
268
269   unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
270
271   static unsigned calculateHash(GenericDINode *N) {
272     return MDNodeOpsKey::calculateHash(N, 1);
273   }
274 };
275
276 template <> struct MDNodeKeyImpl<DISubrange> {
277   int64_t Count;
278   int64_t LowerBound;
279
280   MDNodeKeyImpl(int64_t Count, int64_t LowerBound)
281       : Count(Count), LowerBound(LowerBound) {}
282   MDNodeKeyImpl(const DISubrange *N)
283       : Count(N->getCount()), LowerBound(N->getLowerBound()) {}
284
285   bool isKeyOf(const DISubrange *RHS) const {
286     return Count == RHS->getCount() && LowerBound == RHS->getLowerBound();
287   }
288   unsigned getHashValue() const { return hash_combine(Count, LowerBound); }
289 };
290
291 template <> struct MDNodeKeyImpl<DIEnumerator> {
292   int64_t Value;
293   StringRef Name;
294
295   MDNodeKeyImpl(int64_t Value, StringRef Name) : Value(Value), Name(Name) {}
296   MDNodeKeyImpl(const DIEnumerator *N)
297       : Value(N->getValue()), Name(N->getName()) {}
298
299   bool isKeyOf(const DIEnumerator *RHS) const {
300     return Value == RHS->getValue() && Name == RHS->getName();
301   }
302   unsigned getHashValue() const { return hash_combine(Value, Name); }
303 };
304
305 template <> struct MDNodeKeyImpl<DIBasicType> {
306   unsigned Tag;
307   StringRef Name;
308   uint64_t SizeInBits;
309   uint64_t AlignInBits;
310   unsigned Encoding;
311
312   MDNodeKeyImpl(unsigned Tag, StringRef Name, uint64_t SizeInBits,
313                 uint64_t AlignInBits, unsigned Encoding)
314       : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
315         Encoding(Encoding) {}
316   MDNodeKeyImpl(const DIBasicType *N)
317       : Tag(N->getTag()), Name(N->getName()), SizeInBits(N->getSizeInBits()),
318         AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
319
320   bool isKeyOf(const DIBasicType *RHS) const {
321     return Tag == RHS->getTag() && Name == RHS->getName() &&
322            SizeInBits == RHS->getSizeInBits() &&
323            AlignInBits == RHS->getAlignInBits() &&
324            Encoding == RHS->getEncoding();
325   }
326   unsigned getHashValue() const {
327     return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
328   }
329 };
330
331 template <> struct MDNodeKeyImpl<DIDerivedType> {
332   unsigned Tag;
333   StringRef Name;
334   Metadata *File;
335   unsigned Line;
336   Metadata *Scope;
337   Metadata *BaseType;
338   uint64_t SizeInBits;
339   uint64_t AlignInBits;
340   uint64_t OffsetInBits;
341   unsigned Flags;
342   Metadata *ExtraData;
343
344   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
345                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
346                 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
347                 Metadata *ExtraData)
348       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
349         BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
350         OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {}
351   MDNodeKeyImpl(const DIDerivedType *N)
352       : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()),
353         Line(N->getLine()), Scope(N->getRawScope()),
354         BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
355         AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
356         Flags(N->getFlags()), ExtraData(N->getRawExtraData()) {}
357
358   bool isKeyOf(const DIDerivedType *RHS) const {
359     return Tag == RHS->getTag() && Name == RHS->getName() &&
360            File == RHS->getRawFile() && Line == RHS->getLine() &&
361            Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
362            SizeInBits == RHS->getSizeInBits() &&
363            AlignInBits == RHS->getAlignInBits() &&
364            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
365            ExtraData == RHS->getRawExtraData();
366   }
367   unsigned getHashValue() const {
368     return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
369                         AlignInBits, OffsetInBits, Flags, ExtraData);
370   }
371 };
372
373 template <> struct MDNodeKeyImpl<DICompositeType> {
374   unsigned Tag;
375   StringRef Name;
376   Metadata *File;
377   unsigned Line;
378   Metadata *Scope;
379   Metadata *BaseType;
380   uint64_t SizeInBits;
381   uint64_t AlignInBits;
382   uint64_t OffsetInBits;
383   unsigned Flags;
384   Metadata *Elements;
385   unsigned RuntimeLang;
386   Metadata *VTableHolder;
387   Metadata *TemplateParams;
388   StringRef Identifier;
389
390   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
391                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
392                 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
393                 Metadata *Elements, unsigned RuntimeLang,
394                 Metadata *VTableHolder, Metadata *TemplateParams,
395                 StringRef Identifier)
396       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
397         BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
398         OffsetInBits(OffsetInBits), Flags(Flags), Elements(Elements),
399         RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
400         TemplateParams(TemplateParams), Identifier(Identifier) {}
401   MDNodeKeyImpl(const DICompositeType *N)
402       : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()),
403         Line(N->getLine()), Scope(N->getRawScope()),
404         BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
405         AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
406         Flags(N->getFlags()), Elements(N->getRawElements()),
407         RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
408         TemplateParams(N->getRawTemplateParams()),
409         Identifier(N->getIdentifier()) {}
410
411   bool isKeyOf(const DICompositeType *RHS) const {
412     return Tag == RHS->getTag() && Name == RHS->getName() &&
413            File == RHS->getRawFile() && Line == RHS->getLine() &&
414            Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
415            SizeInBits == RHS->getSizeInBits() &&
416            AlignInBits == RHS->getAlignInBits() &&
417            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
418            Elements == RHS->getRawElements() &&
419            RuntimeLang == RHS->getRuntimeLang() &&
420            VTableHolder == RHS->getRawVTableHolder() &&
421            TemplateParams == RHS->getRawTemplateParams() &&
422            Identifier == RHS->getIdentifier();
423   }
424   unsigned getHashValue() const {
425     return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
426                         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
427                         VTableHolder, TemplateParams, Identifier);
428   }
429 };
430
431 template <> struct MDNodeKeyImpl<DISubroutineType> {
432   unsigned Flags;
433   Metadata *TypeArray;
434
435   MDNodeKeyImpl(int64_t Flags, Metadata *TypeArray)
436       : Flags(Flags), TypeArray(TypeArray) {}
437   MDNodeKeyImpl(const DISubroutineType *N)
438       : Flags(N->getFlags()), TypeArray(N->getRawTypeArray()) {}
439
440   bool isKeyOf(const DISubroutineType *RHS) const {
441     return Flags == RHS->getFlags() && TypeArray == RHS->getRawTypeArray();
442   }
443   unsigned getHashValue() const { return hash_combine(Flags, TypeArray); }
444 };
445
446 template <> struct MDNodeKeyImpl<DIFile> {
447   StringRef Filename;
448   StringRef Directory;
449
450   MDNodeKeyImpl(StringRef Filename, StringRef Directory)
451       : Filename(Filename), Directory(Directory) {}
452   MDNodeKeyImpl(const DIFile *N)
453       : Filename(N->getFilename()), Directory(N->getDirectory()) {}
454
455   bool isKeyOf(const DIFile *RHS) const {
456     return Filename == RHS->getFilename() && Directory == RHS->getDirectory();
457   }
458   unsigned getHashValue() const { return hash_combine(Filename, Directory); }
459 };
460
461 template <> struct MDNodeKeyImpl<DISubprogram> {
462   Metadata *Scope;
463   StringRef Name;
464   StringRef LinkageName;
465   Metadata *File;
466   unsigned Line;
467   Metadata *Type;
468   bool IsLocalToUnit;
469   bool IsDefinition;
470   unsigned ScopeLine;
471   Metadata *ContainingType;
472   unsigned Virtuality;
473   unsigned VirtualIndex;
474   unsigned Flags;
475   bool IsOptimized;
476   Metadata *Function;
477   Metadata *TemplateParams;
478   Metadata *Declaration;
479   Metadata *Variables;
480
481   MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
482                 Metadata *File, unsigned Line, Metadata *Type,
483                 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
484                 Metadata *ContainingType, unsigned Virtuality,
485                 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
486                 Metadata *Function, Metadata *TemplateParams,
487                 Metadata *Declaration, Metadata *Variables)
488       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
489         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
490         IsDefinition(IsDefinition), ScopeLine(ScopeLine),
491         ContainingType(ContainingType), Virtuality(Virtuality),
492         VirtualIndex(VirtualIndex), Flags(Flags), IsOptimized(IsOptimized),
493         Function(Function), TemplateParams(TemplateParams),
494         Declaration(Declaration), Variables(Variables) {}
495   MDNodeKeyImpl(const DISubprogram *N)
496       : Scope(N->getRawScope()), Name(N->getName()),
497         LinkageName(N->getLinkageName()), File(N->getRawFile()),
498         Line(N->getLine()), Type(N->getRawType()),
499         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
500         ScopeLine(N->getScopeLine()), ContainingType(N->getRawContainingType()),
501         Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()),
502         Flags(N->getFlags()), IsOptimized(N->isOptimized()),
503         Function(N->getRawFunction()),
504         TemplateParams(N->getRawTemplateParams()),
505         Declaration(N->getRawDeclaration()), Variables(N->getRawVariables()) {}
506
507   bool isKeyOf(const DISubprogram *RHS) const {
508     return Scope == RHS->getRawScope() && Name == RHS->getName() &&
509            LinkageName == RHS->getLinkageName() && File == RHS->getRawFile() &&
510            Line == RHS->getLine() && Type == RHS->getRawType() &&
511            IsLocalToUnit == RHS->isLocalToUnit() &&
512            IsDefinition == RHS->isDefinition() &&
513            ScopeLine == RHS->getScopeLine() &&
514            ContainingType == RHS->getRawContainingType() &&
515            Virtuality == RHS->getVirtuality() &&
516            VirtualIndex == RHS->getVirtualIndex() && Flags == RHS->getFlags() &&
517            IsOptimized == RHS->isOptimized() &&
518            Function == RHS->getRawFunction() &&
519            TemplateParams == RHS->getRawTemplateParams() &&
520            Declaration == RHS->getRawDeclaration() &&
521            Variables == RHS->getRawVariables();
522   }
523   unsigned getHashValue() const {
524     return hash_combine(Scope, Name, LinkageName, File, Line, Type,
525                         IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
526                         Virtuality, VirtualIndex, Flags, IsOptimized, Function,
527                         TemplateParams, Declaration, Variables);
528   }
529 };
530
531 template <> struct MDNodeKeyImpl<DILexicalBlock> {
532   Metadata *Scope;
533   Metadata *File;
534   unsigned Line;
535   unsigned Column;
536
537   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
538       : Scope(Scope), File(File), Line(Line), Column(Column) {}
539   MDNodeKeyImpl(const DILexicalBlock *N)
540       : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
541         Column(N->getColumn()) {}
542
543   bool isKeyOf(const DILexicalBlock *RHS) const {
544     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
545            Line == RHS->getLine() && Column == RHS->getColumn();
546   }
547   unsigned getHashValue() const {
548     return hash_combine(Scope, File, Line, Column);
549   }
550 };
551
552 template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
553   Metadata *Scope;
554   Metadata *File;
555   unsigned Discriminator;
556
557   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
558       : Scope(Scope), File(File), Discriminator(Discriminator) {}
559   MDNodeKeyImpl(const DILexicalBlockFile *N)
560       : Scope(N->getRawScope()), File(N->getRawFile()),
561         Discriminator(N->getDiscriminator()) {}
562
563   bool isKeyOf(const DILexicalBlockFile *RHS) const {
564     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
565            Discriminator == RHS->getDiscriminator();
566   }
567   unsigned getHashValue() const {
568     return hash_combine(Scope, File, Discriminator);
569   }
570 };
571
572 template <> struct MDNodeKeyImpl<DINamespace> {
573   Metadata *Scope;
574   Metadata *File;
575   StringRef Name;
576   unsigned Line;
577
578   MDNodeKeyImpl(Metadata *Scope, Metadata *File, StringRef Name, unsigned Line)
579       : Scope(Scope), File(File), Name(Name), Line(Line) {}
580   MDNodeKeyImpl(const DINamespace *N)
581       : Scope(N->getRawScope()), File(N->getRawFile()), Name(N->getName()),
582         Line(N->getLine()) {}
583
584   bool isKeyOf(const DINamespace *RHS) const {
585     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
586            Name == RHS->getName() && Line == RHS->getLine();
587   }
588   unsigned getHashValue() const {
589     return hash_combine(Scope, File, Name, Line);
590   }
591 };
592
593 template <> struct MDNodeKeyImpl<DIModule> {
594   Metadata *Scope;
595   StringRef Name;
596   StringRef ConfigurationMacros;
597   StringRef IncludePath;
598   StringRef ISysRoot;
599   MDNodeKeyImpl(Metadata *Scope, StringRef Name,
600                 StringRef ConfigurationMacros,
601                 StringRef IncludePath,
602                 StringRef ISysRoot)
603     : Scope(Scope), Name(Name), ConfigurationMacros(ConfigurationMacros),
604       IncludePath(IncludePath), ISysRoot(ISysRoot) {}
605   MDNodeKeyImpl(const DIModule *N)
606     : Scope(N->getRawScope()), Name(N->getName()),
607       ConfigurationMacros(N->getConfigurationMacros()),
608       IncludePath(N->getIncludePath()), ISysRoot(N->getISysRoot()) {}
609
610   bool isKeyOf(const DIModule *RHS) const {
611     return Scope == RHS->getRawScope() && Name == RHS->getName() &&
612            ConfigurationMacros == RHS->getConfigurationMacros() &&
613            IncludePath == RHS->getIncludePath() &&
614            ISysRoot == RHS->getISysRoot();
615   }
616   unsigned getHashValue() const {
617     return hash_combine(Scope, Name,
618                         ConfigurationMacros, IncludePath, ISysRoot);
619   }
620 };
621
622 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
623   StringRef Name;
624   Metadata *Type;
625
626   MDNodeKeyImpl(StringRef Name, Metadata *Type) : Name(Name), Type(Type) {}
627   MDNodeKeyImpl(const DITemplateTypeParameter *N)
628       : Name(N->getName()), Type(N->getRawType()) {}
629
630   bool isKeyOf(const DITemplateTypeParameter *RHS) const {
631     return Name == RHS->getName() && Type == RHS->getRawType();
632   }
633   unsigned getHashValue() const { return hash_combine(Name, Type); }
634 };
635
636 template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
637   unsigned Tag;
638   StringRef Name;
639   Metadata *Type;
640   Metadata *Value;
641
642   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *Type, Metadata *Value)
643       : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
644   MDNodeKeyImpl(const DITemplateValueParameter *N)
645       : Tag(N->getTag()), Name(N->getName()), Type(N->getRawType()),
646         Value(N->getValue()) {}
647
648   bool isKeyOf(const DITemplateValueParameter *RHS) const {
649     return Tag == RHS->getTag() && Name == RHS->getName() &&
650            Type == RHS->getRawType() && Value == RHS->getValue();
651   }
652   unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
653 };
654
655 template <> struct MDNodeKeyImpl<DIGlobalVariable> {
656   Metadata *Scope;
657   StringRef Name;
658   StringRef LinkageName;
659   Metadata *File;
660   unsigned Line;
661   Metadata *Type;
662   bool IsLocalToUnit;
663   bool IsDefinition;
664   Metadata *Variable;
665   Metadata *StaticDataMemberDeclaration;
666
667   MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
668                 Metadata *File, unsigned Line, Metadata *Type,
669                 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
670                 Metadata *StaticDataMemberDeclaration)
671       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
672         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
673         IsDefinition(IsDefinition), Variable(Variable),
674         StaticDataMemberDeclaration(StaticDataMemberDeclaration) {}
675   MDNodeKeyImpl(const DIGlobalVariable *N)
676       : Scope(N->getRawScope()), Name(N->getName()),
677         LinkageName(N->getLinkageName()), File(N->getRawFile()),
678         Line(N->getLine()), Type(N->getRawType()),
679         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
680         Variable(N->getRawVariable()),
681         StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()) {}
682
683   bool isKeyOf(const DIGlobalVariable *RHS) const {
684     return Scope == RHS->getRawScope() && Name == RHS->getName() &&
685            LinkageName == RHS->getLinkageName() && File == RHS->getRawFile() &&
686            Line == RHS->getLine() && Type == RHS->getRawType() &&
687            IsLocalToUnit == RHS->isLocalToUnit() &&
688            IsDefinition == RHS->isDefinition() &&
689            Variable == RHS->getRawVariable() &&
690            StaticDataMemberDeclaration ==
691                RHS->getRawStaticDataMemberDeclaration();
692   }
693   unsigned getHashValue() const {
694     return hash_combine(Scope, Name, LinkageName, File, Line, Type,
695                         IsLocalToUnit, IsDefinition, Variable,
696                         StaticDataMemberDeclaration);
697   }
698 };
699
700 template <> struct MDNodeKeyImpl<DILocalVariable> {
701   Metadata *Scope;
702   StringRef Name;
703   Metadata *File;
704   unsigned Line;
705   Metadata *Type;
706   unsigned Arg;
707   unsigned Flags;
708
709   MDNodeKeyImpl(Metadata *Scope, StringRef Name, Metadata *File, unsigned Line,
710                 Metadata *Type, unsigned Arg, unsigned Flags)
711       : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
712         Flags(Flags) {}
713   MDNodeKeyImpl(const DILocalVariable *N)
714       : Scope(N->getRawScope()), Name(N->getName()), File(N->getRawFile()),
715         Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
716         Flags(N->getFlags()) {}
717
718   bool isKeyOf(const DILocalVariable *RHS) const {
719     return Scope == RHS->getRawScope() && Name == RHS->getName() &&
720            File == RHS->getRawFile() && Line == RHS->getLine() &&
721            Type == RHS->getRawType() && Arg == RHS->getArg() &&
722            Flags == RHS->getFlags();
723   }
724   unsigned getHashValue() const {
725     return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
726   }
727 };
728
729 template <> struct MDNodeKeyImpl<DIExpression> {
730   ArrayRef<uint64_t> Elements;
731
732   MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
733   MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
734
735   bool isKeyOf(const DIExpression *RHS) const {
736     return Elements == RHS->getElements();
737   }
738   unsigned getHashValue() const {
739     return hash_combine_range(Elements.begin(), Elements.end());
740   }
741 };
742
743 template <> struct MDNodeKeyImpl<DIObjCProperty> {
744   StringRef Name;
745   Metadata *File;
746   unsigned Line;
747   StringRef GetterName;
748   StringRef SetterName;
749   unsigned Attributes;
750   Metadata *Type;
751
752   MDNodeKeyImpl(StringRef Name, Metadata *File, unsigned Line,
753                 StringRef GetterName, StringRef SetterName, unsigned Attributes,
754                 Metadata *Type)
755       : Name(Name), File(File), Line(Line), GetterName(GetterName),
756         SetterName(SetterName), Attributes(Attributes), Type(Type) {}
757   MDNodeKeyImpl(const DIObjCProperty *N)
758       : Name(N->getName()), File(N->getRawFile()), Line(N->getLine()),
759         GetterName(N->getGetterName()), SetterName(N->getSetterName()),
760         Attributes(N->getAttributes()), Type(N->getRawType()) {}
761
762   bool isKeyOf(const DIObjCProperty *RHS) const {
763     return Name == RHS->getName() && File == RHS->getRawFile() &&
764            Line == RHS->getLine() && GetterName == RHS->getGetterName() &&
765            SetterName == RHS->getSetterName() &&
766            Attributes == RHS->getAttributes() && Type == RHS->getRawType();
767   }
768   unsigned getHashValue() const {
769     return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
770                         Type);
771   }
772 };
773
774 template <> struct MDNodeKeyImpl<DIImportedEntity> {
775   unsigned Tag;
776   Metadata *Scope;
777   Metadata *Entity;
778   unsigned Line;
779   StringRef Name;
780
781   MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line,
782                 StringRef Name)
783       : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {}
784   MDNodeKeyImpl(const DIImportedEntity *N)
785       : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
786         Line(N->getLine()), Name(N->getName()) {}
787
788   bool isKeyOf(const DIImportedEntity *RHS) const {
789     return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
790            Entity == RHS->getRawEntity() && Line == RHS->getLine() &&
791            Name == RHS->getName();
792   }
793   unsigned getHashValue() const {
794     return hash_combine(Tag, Scope, Entity, Line, Name);
795   }
796 };
797
798 /// \brief DenseMapInfo for MDNode subclasses.
799 template <class NodeTy> struct MDNodeInfo {
800   typedef MDNodeKeyImpl<NodeTy> KeyTy;
801   static inline NodeTy *getEmptyKey() {
802     return DenseMapInfo<NodeTy *>::getEmptyKey();
803   }
804   static inline NodeTy *getTombstoneKey() {
805     return DenseMapInfo<NodeTy *>::getTombstoneKey();
806   }
807   static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
808   static unsigned getHashValue(const NodeTy *N) {
809     return KeyTy(N).getHashValue();
810   }
811   static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
812     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
813       return false;
814     return LHS.isKeyOf(RHS);
815   }
816   static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
817     return LHS == RHS;
818   }
819 };
820
821 #define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo<CLASS> CLASS##Info;
822 #include "llvm/IR/Metadata.def"
823
824 /// \brief Map-like storage for metadata attachments.
825 class MDAttachmentMap {
826   SmallVector<std::pair<unsigned, TrackingMDNodeRef>, 2> Attachments;
827
828 public:
829   bool empty() const { return Attachments.empty(); }
830   size_t size() const { return Attachments.size(); }
831
832   /// \brief Get a particular attachment (if any).
833   MDNode *lookup(unsigned ID) const;
834
835   /// \brief Set an attachment to a particular node.
836   ///
837   /// Set the \c ID attachment to \c MD, replacing the current attachment at \c
838   /// ID (if anyway).
839   void set(unsigned ID, MDNode &MD);
840
841   /// \brief Remove an attachment.
842   ///
843   /// Remove the attachment at \c ID, if any.
844   void erase(unsigned ID);
845
846   /// \brief Copy out all the attachments.
847   ///
848   /// Copies all the current attachments into \c Result, sorting by attachment
849   /// ID.  This function does \em not clear \c Result.
850   void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
851
852   /// \brief Erase matching attachments.
853   ///
854   /// Erases all attachments matching the \c shouldRemove predicate.
855   template <class PredTy> void remove_if(PredTy shouldRemove) {
856     Attachments.erase(
857         std::remove_if(Attachments.begin(), Attachments.end(), shouldRemove),
858         Attachments.end());
859   }
860 };
861
862 class LLVMContextImpl {
863 public:
864   /// OwnedModules - The set of modules instantiated in this context, and which
865   /// will be automatically deleted if this context is deleted.
866   SmallPtrSet<Module*, 4> OwnedModules;
867   
868   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
869   void *InlineAsmDiagContext;
870
871   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
872   void *DiagnosticContext;
873   bool RespectDiagnosticFilters;
874
875   LLVMContext::YieldCallbackTy YieldCallback;
876   void *YieldOpaqueHandle;
877
878   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
879   IntMapTy IntConstants;
880
881   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
882   FPMapTy FPConstants;
883
884   FoldingSet<AttributeImpl> AttrsSet;
885   FoldingSet<AttributeSetImpl> AttrsLists;
886   FoldingSet<AttributeSetNode> AttrsSetNodes;
887
888   StringMap<MDString> MDStringCache;
889   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
890   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
891
892   DenseMap<const Value*, ValueName*> ValueNames;
893
894 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
895   DenseSet<CLASS *, CLASS##Info> CLASS##s;
896 #include "llvm/IR/Metadata.def"
897
898   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
899   // aren't in the MDNodeSet, but they're still shared between objects, so no
900   // one object can destroy them.  This set allows us to at least destroy them
901   // on Context destruction.
902   SmallPtrSet<MDNode *, 1> DistinctMDNodes;
903
904   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
905
906   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
907   ArrayConstantsTy ArrayConstants;
908   
909   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
910   StructConstantsTy StructConstants;
911   
912   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
913   VectorConstantsTy VectorConstants;
914   
915   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
916
917   DenseMap<Type*, UndefValue*> UVConstants;
918   
919   StringMap<ConstantDataSequential*> CDSConstants;
920
921   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
922     BlockAddresses;
923   ConstantUniqueMap<ConstantExpr> ExprConstants;
924
925   ConstantUniqueMap<InlineAsm> InlineAsms;
926
927   ConstantInt *TheTrueVal;
928   ConstantInt *TheFalseVal;
929
930   // Basic type instances.
931   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy, TokenTy;
932   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
933   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
934
935   
936   /// TypeAllocator - All dynamically allocated types are allocated from this.
937   /// They live forever until the context is torn down.
938   BumpPtrAllocator TypeAllocator;
939   
940   DenseMap<unsigned, IntegerType*> IntegerTypes;
941
942   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
943   FunctionTypeSet FunctionTypes;
944   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
945   StructTypeSet AnonStructTypes;
946   StringMap<StructType*> NamedStructTypes;
947   unsigned NamedStructTypesUniqueID;
948     
949   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
950   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
951   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
952   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
953
954
955   /// ValueHandles - This map keeps track of all of the value handles that are
956   /// watching a Value*.  The Value::HasValueHandle bit is used to know
957   /// whether or not a value has an entry in this map.
958   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
959   ValueHandlesTy ValueHandles;
960   
961   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
962   StringMap<unsigned> CustomMDKindNames;
963
964   /// Collection of per-instruction metadata used in this context.
965   DenseMap<const Instruction *, MDAttachmentMap> InstructionMetadata;
966
967   /// Collection of per-function metadata used in this context.
968   DenseMap<const Function *, MDAttachmentMap> FunctionMetadata;
969
970   /// DiscriminatorTable - This table maps file:line locations to an
971   /// integer representing the next DWARF path discriminator to assign to
972   /// instructions in different blocks at the same location.
973   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
974
975   /// \brief Mapping from a function to its prefix data, which is stored as the
976   /// operand of an unparented ReturnInst so that the prefix data has a Use.
977   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
978   PrefixDataMapTy PrefixDataMap;
979
980   /// \brief Mapping from a function to its prologue data, which is stored as
981   /// the operand of an unparented ReturnInst so that the prologue data has a
982   /// Use.
983   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
984   PrologueDataMapTy PrologueDataMap;
985
986   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
987   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
988
989   /// \brief A set of interned tags for operand bundles.  The StringMap maps
990   /// bundle tags to their IDs.
991   ///
992   /// \see LLVMContext::getOperandBundleTagID
993   StringMap<uint32_t> BundleTagCache;
994
995   StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
996   void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
997   uint32_t getOperandBundleTagID(StringRef Tag) const;
998
999   LLVMContextImpl(LLVMContext &C);
1000   ~LLVMContextImpl();
1001
1002   /// Destroy the ConstantArrays if they are not used.
1003   void dropTriviallyDeadConstantArrays();
1004 };
1005
1006 }
1007
1008 #endif