LLVM API Documentation

LLVMContextImpl.h
Go to the documentation of this file.
00001 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file declares LLVMContextImpl, the opaque implementation 
00011 //  of LLVMContext.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
00016 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
00017 
00018 #include "AttributeImpl.h"
00019 #include "ConstantsContext.h"
00020 #include "llvm/ADT/APFloat.h"
00021 #include "llvm/ADT/APInt.h"
00022 #include "llvm/ADT/ArrayRef.h"
00023 #include "llvm/ADT/DenseMap.h"
00024 #include "llvm/ADT/DenseSet.h"
00025 #include "llvm/ADT/FoldingSet.h"
00026 #include "llvm/ADT/Hashing.h"
00027 #include "llvm/ADT/SmallPtrSet.h"
00028 #include "llvm/ADT/StringMap.h"
00029 #include "llvm/IR/Constants.h"
00030 #include "llvm/IR/DebugInfoMetadata.h"
00031 #include "llvm/IR/DerivedTypes.h"
00032 #include "llvm/IR/LLVMContext.h"
00033 #include "llvm/IR/Metadata.h"
00034 #include "llvm/IR/ValueHandle.h"
00035 #include <vector>
00036 
00037 namespace llvm {
00038 
00039 class ConstantInt;
00040 class ConstantFP;
00041 class DiagnosticInfoOptimizationRemark;
00042 class DiagnosticInfoOptimizationRemarkMissed;
00043 class DiagnosticInfoOptimizationRemarkAnalysis;
00044 class GCStrategy;
00045 class LLVMContext;
00046 class Type;
00047 class Value;
00048 
00049 struct DenseMapAPIntKeyInfo {
00050   static inline APInt getEmptyKey() {
00051     APInt V(nullptr, 0);
00052     V.VAL = 0;
00053     return V;
00054   }
00055   static inline APInt getTombstoneKey() {
00056     APInt V(nullptr, 0);
00057     V.VAL = 1;
00058     return V;
00059   }
00060   static unsigned getHashValue(const APInt &Key) {
00061     return static_cast<unsigned>(hash_value(Key));
00062   }
00063   static bool isEqual(const APInt &LHS, const APInt &RHS) {
00064     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
00065   }
00066 };
00067 
00068 struct DenseMapAPFloatKeyInfo {
00069   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
00070   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
00071   static unsigned getHashValue(const APFloat &Key) {
00072     return static_cast<unsigned>(hash_value(Key));
00073   }
00074   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
00075     return LHS.bitwiseIsEqual(RHS);
00076   }
00077 };
00078 
00079 struct AnonStructTypeKeyInfo {
00080   struct KeyTy {
00081     ArrayRef<Type*> ETypes;
00082     bool isPacked;
00083     KeyTy(const ArrayRef<Type*>& E, bool P) :
00084       ETypes(E), isPacked(P) {}
00085     KeyTy(const StructType *ST)
00086         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
00087     bool operator==(const KeyTy& that) const {
00088       if (isPacked != that.isPacked)
00089         return false;
00090       if (ETypes != that.ETypes)
00091         return false;
00092       return true;
00093     }
00094     bool operator!=(const KeyTy& that) const {
00095       return !this->operator==(that);
00096     }
00097   };
00098   static inline StructType* getEmptyKey() {
00099     return DenseMapInfo<StructType*>::getEmptyKey();
00100   }
00101   static inline StructType* getTombstoneKey() {
00102     return DenseMapInfo<StructType*>::getTombstoneKey();
00103   }
00104   static unsigned getHashValue(const KeyTy& Key) {
00105     return hash_combine(hash_combine_range(Key.ETypes.begin(),
00106                                            Key.ETypes.end()),
00107                         Key.isPacked);
00108   }
00109   static unsigned getHashValue(const StructType *ST) {
00110     return getHashValue(KeyTy(ST));
00111   }
00112   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
00113     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00114       return false;
00115     return LHS == KeyTy(RHS);
00116   }
00117   static bool isEqual(const StructType *LHS, const StructType *RHS) {
00118     return LHS == RHS;
00119   }
00120 };
00121 
00122 struct FunctionTypeKeyInfo {
00123   struct KeyTy {
00124     const Type *ReturnType;
00125     ArrayRef<Type*> Params;
00126     bool isVarArg;
00127     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
00128       ReturnType(R), Params(P), isVarArg(V) {}
00129     KeyTy(const FunctionType *FT)
00130         : ReturnType(FT->getReturnType()), Params(FT->params()),
00131           isVarArg(FT->isVarArg()) {}
00132     bool operator==(const KeyTy& that) const {
00133       if (ReturnType != that.ReturnType)
00134         return false;
00135       if (isVarArg != that.isVarArg)
00136         return false;
00137       if (Params != that.Params)
00138         return false;
00139       return true;
00140     }
00141     bool operator!=(const KeyTy& that) const {
00142       return !this->operator==(that);
00143     }
00144   };
00145   static inline FunctionType* getEmptyKey() {
00146     return DenseMapInfo<FunctionType*>::getEmptyKey();
00147   }
00148   static inline FunctionType* getTombstoneKey() {
00149     return DenseMapInfo<FunctionType*>::getTombstoneKey();
00150   }
00151   static unsigned getHashValue(const KeyTy& Key) {
00152     return hash_combine(Key.ReturnType,
00153                         hash_combine_range(Key.Params.begin(),
00154                                            Key.Params.end()),
00155                         Key.isVarArg);
00156   }
00157   static unsigned getHashValue(const FunctionType *FT) {
00158     return getHashValue(KeyTy(FT));
00159   }
00160   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
00161     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00162       return false;
00163     return LHS == KeyTy(RHS);
00164   }
00165   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
00166     return LHS == RHS;
00167   }
00168 };
00169 
00170 /// \brief Structure for hashing arbitrary MDNode operands.
00171 class MDNodeOpsKey {
00172   ArrayRef<Metadata *> RawOps;
00173   ArrayRef<MDOperand> Ops;
00174 
00175   unsigned Hash;
00176 
00177 protected:
00178   MDNodeOpsKey(ArrayRef<Metadata *> Ops)
00179       : RawOps(Ops), Hash(calculateHash(Ops)) {}
00180 
00181   template <class NodeTy>
00182   MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
00183       : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
00184 
00185   template <class NodeTy>
00186   bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
00187     if (getHash() != RHS->getHash())
00188       return false;
00189 
00190     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
00191     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
00192                           : compareOps(RawOps, RHS, Offset);
00193   }
00194 
00195   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
00196 
00197 private:
00198   template <class T>
00199   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
00200     if (Ops.size() != RHS->getNumOperands() - Offset)
00201       return false;
00202     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
00203   }
00204 
00205   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
00206 
00207 public:
00208   unsigned getHash() const { return Hash; }
00209 };
00210 
00211 template <class NodeTy> struct MDNodeKeyImpl;
00212 template <class NodeTy> struct MDNodeInfo;
00213 
00214 /// \brief DenseMapInfo for MDTuple.
00215 ///
00216 /// Note that we don't need the is-function-local bit, since that's implicit in
00217 /// the operands.
00218 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
00219   MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
00220   MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
00221 
00222   bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
00223 
00224   unsigned getHashValue() const { return getHash(); }
00225 
00226   static unsigned calculateHash(MDTuple *N) {
00227     return MDNodeOpsKey::calculateHash(N);
00228   }
00229 };
00230 
00231 /// \brief DenseMapInfo for MDLocation.
00232 template <> struct MDNodeKeyImpl<MDLocation> {
00233   unsigned Line;
00234   unsigned Column;
00235   Metadata *Scope;
00236   Metadata *InlinedAt;
00237 
00238   MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
00239                 Metadata *InlinedAt)
00240       : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
00241 
00242   MDNodeKeyImpl(const MDLocation *L)
00243       : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
00244         InlinedAt(L->getInlinedAt()) {}
00245 
00246   bool isKeyOf(const MDLocation *RHS) const {
00247     return Line == RHS->getLine() && Column == RHS->getColumn() &&
00248            Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
00249   }
00250   unsigned getHashValue() const {
00251     return hash_combine(Line, Column, Scope, InlinedAt);
00252   }
00253 };
00254 
00255 /// \brief DenseMapInfo for GenericDebugNode.
00256 template <> struct MDNodeKeyImpl<GenericDebugNode> : MDNodeOpsKey {
00257   unsigned Tag;
00258   StringRef Header;
00259   MDNodeKeyImpl(unsigned Tag, StringRef Header, ArrayRef<Metadata *> DwarfOps)
00260       : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
00261   MDNodeKeyImpl(const GenericDebugNode *N)
00262       : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
00263 
00264   bool isKeyOf(const GenericDebugNode *RHS) const {
00265     return Tag == RHS->getTag() && Header == RHS->getHeader() &&
00266            compareOps(RHS, 1);
00267   }
00268 
00269   unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
00270 
00271   static unsigned calculateHash(GenericDebugNode *N) {
00272     return MDNodeOpsKey::calculateHash(N, 1);
00273   }
00274 };
00275 
00276 template <> struct MDNodeKeyImpl<MDSubrange> {
00277   int64_t Count;
00278   int64_t Lo;
00279 
00280   MDNodeKeyImpl(int64_t Count, int64_t Lo) : Count(Count), Lo(Lo) {}
00281   MDNodeKeyImpl(const MDSubrange *N) : Count(N->getCount()), Lo(N->getLo()) {}
00282 
00283   bool isKeyOf(const MDSubrange *RHS) const {
00284     return Count == RHS->getCount() && Lo == RHS->getLo();
00285   }
00286   unsigned getHashValue() const { return hash_combine(Count, Lo); }
00287 };
00288 
00289 template <> struct MDNodeKeyImpl<MDEnumerator> {
00290   int64_t Value;
00291   StringRef Name;
00292 
00293   MDNodeKeyImpl(int64_t Value, StringRef Name) : Value(Value), Name(Name) {}
00294   MDNodeKeyImpl(const MDEnumerator *N)
00295       : Value(N->getValue()), Name(N->getName()) {}
00296 
00297   bool isKeyOf(const MDEnumerator *RHS) const {
00298     return Value == RHS->getValue() && Name == RHS->getName();
00299   }
00300   unsigned getHashValue() const { return hash_combine(Value, Name); }
00301 };
00302 
00303 template <> struct MDNodeKeyImpl<MDBasicType> {
00304   unsigned Tag;
00305   StringRef Name;
00306   uint64_t SizeInBits;
00307   uint64_t AlignInBits;
00308   unsigned Encoding;
00309 
00310   MDNodeKeyImpl(unsigned Tag, StringRef Name, uint64_t SizeInBits,
00311                 uint64_t AlignInBits, unsigned Encoding)
00312       : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
00313         Encoding(Encoding) {}
00314   MDNodeKeyImpl(const MDBasicType *N)
00315       : Tag(N->getTag()), Name(N->getName()), SizeInBits(N->getSizeInBits()),
00316         AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
00317 
00318   bool isKeyOf(const MDBasicType *RHS) const {
00319     return Tag == RHS->getTag() && Name == RHS->getName() &&
00320            SizeInBits == RHS->getSizeInBits() &&
00321            AlignInBits == RHS->getAlignInBits() &&
00322            Encoding == RHS->getEncoding();
00323   }
00324   unsigned getHashValue() const {
00325     return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
00326   }
00327 };
00328 
00329 template <> struct MDNodeKeyImpl<MDDerivedType> {
00330   unsigned Tag;
00331   StringRef Name;
00332   Metadata *File;
00333   unsigned Line;
00334   Metadata *Scope;
00335   Metadata *BaseType;
00336   uint64_t SizeInBits;
00337   uint64_t AlignInBits;
00338   uint64_t OffsetInBits;
00339   unsigned Flags;
00340   Metadata *ExtraData;
00341 
00342   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
00343                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
00344                 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
00345                 Metadata *ExtraData)
00346       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
00347         BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
00348         OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {}
00349   MDNodeKeyImpl(const MDDerivedType *N)
00350       : Tag(N->getTag()), Name(N->getName()), File(N->getFile()),
00351         Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()),
00352         SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()),
00353         OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()),
00354         ExtraData(N->getExtraData()) {}
00355 
00356   bool isKeyOf(const MDDerivedType *RHS) const {
00357     return Tag == RHS->getTag() && Name == RHS->getName() &&
00358            File == RHS->getFile() && Line == RHS->getLine() &&
00359            Scope == RHS->getScope() && BaseType == RHS->getBaseType() &&
00360            SizeInBits == RHS->getSizeInBits() &&
00361            AlignInBits == RHS->getAlignInBits() &&
00362            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
00363            ExtraData == RHS->getExtraData();
00364   }
00365   unsigned getHashValue() const {
00366     return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
00367                         AlignInBits, OffsetInBits, Flags, ExtraData);
00368   }
00369 };
00370 
00371 template <> struct MDNodeKeyImpl<MDCompositeType> {
00372   unsigned Tag;
00373   StringRef Name;
00374   Metadata *File;
00375   unsigned Line;
00376   Metadata *Scope;
00377   Metadata *BaseType;
00378   uint64_t SizeInBits;
00379   uint64_t AlignInBits;
00380   uint64_t OffsetInBits;
00381   unsigned Flags;
00382   Metadata *Elements;
00383   unsigned RuntimeLang;
00384   Metadata *VTableHolder;
00385   Metadata *TemplateParams;
00386   StringRef Identifier;
00387 
00388   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
00389                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
00390                 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
00391                 Metadata *Elements, unsigned RuntimeLang,
00392                 Metadata *VTableHolder, Metadata *TemplateParams,
00393                 StringRef Identifier)
00394       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
00395         BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
00396         OffsetInBits(OffsetInBits), Flags(Flags), Elements(Elements),
00397         RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
00398         TemplateParams(TemplateParams), Identifier(Identifier) {}
00399   MDNodeKeyImpl(const MDCompositeType *N)
00400       : Tag(N->getTag()), Name(N->getName()), File(N->getFile()),
00401         Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()),
00402         SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()),
00403         OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()),
00404         Elements(N->getElements()), RuntimeLang(N->getRuntimeLang()),
00405         VTableHolder(N->getVTableHolder()),
00406         TemplateParams(N->getTemplateParams()), Identifier(N->getIdentifier()) {
00407   }
00408 
00409   bool isKeyOf(const MDCompositeType *RHS) const {
00410     return Tag == RHS->getTag() && Name == RHS->getName() &&
00411            File == RHS->getFile() && Line == RHS->getLine() &&
00412            Scope == RHS->getScope() && BaseType == RHS->getBaseType() &&
00413            SizeInBits == RHS->getSizeInBits() &&
00414            AlignInBits == RHS->getAlignInBits() &&
00415            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
00416            Elements == RHS->getElements() &&
00417            RuntimeLang == RHS->getRuntimeLang() &&
00418            VTableHolder == RHS->getVTableHolder() &&
00419            TemplateParams == RHS->getTemplateParams() &&
00420            Identifier == RHS->getIdentifier();
00421   }
00422   unsigned getHashValue() const {
00423     return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
00424                         AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
00425                         VTableHolder, TemplateParams, Identifier);
00426   }
00427 };
00428 
00429 template <> struct MDNodeKeyImpl<MDSubroutineType> {
00430   unsigned Flags;
00431   Metadata *TypeArray;
00432 
00433   MDNodeKeyImpl(int64_t Flags, Metadata *TypeArray)
00434       : Flags(Flags), TypeArray(TypeArray) {}
00435   MDNodeKeyImpl(const MDSubroutineType *N)
00436       : Flags(N->getFlags()), TypeArray(N->getTypeArray()) {}
00437 
00438   bool isKeyOf(const MDSubroutineType *RHS) const {
00439     return Flags == RHS->getFlags() && TypeArray == RHS->getTypeArray();
00440   }
00441   unsigned getHashValue() const { return hash_combine(Flags, TypeArray); }
00442 };
00443 
00444 template <> struct MDNodeKeyImpl<MDFile> {
00445   StringRef Filename;
00446   StringRef Directory;
00447 
00448   MDNodeKeyImpl(StringRef Filename, StringRef Directory)
00449       : Filename(Filename), Directory(Directory) {}
00450   MDNodeKeyImpl(const MDFile *N)
00451       : Filename(N->getFilename()), Directory(N->getDirectory()) {}
00452 
00453   bool isKeyOf(const MDFile *RHS) const {
00454     return Filename == RHS->getFilename() && Directory == RHS->getDirectory();
00455   }
00456   unsigned getHashValue() const { return hash_combine(Filename, Directory); }
00457 };
00458 
00459 template <> struct MDNodeKeyImpl<MDCompileUnit> {
00460   unsigned SourceLanguage;
00461   Metadata *File;
00462   StringRef Producer;
00463   bool IsOptimized;
00464   StringRef Flags;
00465   unsigned RuntimeVersion;
00466   StringRef SplitDebugFilename;
00467   unsigned EmissionKind;
00468   Metadata *EnumTypes;
00469   Metadata *RetainedTypes;
00470   Metadata *Subprograms;
00471   Metadata *GlobalVariables;
00472   Metadata *ImportedEntities;
00473 
00474   MDNodeKeyImpl(unsigned SourceLanguage, Metadata *File, StringRef Producer,
00475                 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
00476                 StringRef SplitDebugFilename, unsigned EmissionKind,
00477                 Metadata *EnumTypes, Metadata *RetainedTypes,
00478                 Metadata *Subprograms, Metadata *GlobalVariables,
00479                 Metadata *ImportedEntities)
00480       : SourceLanguage(SourceLanguage), File(File), Producer(Producer),
00481         IsOptimized(IsOptimized), Flags(Flags), RuntimeVersion(RuntimeVersion),
00482         SplitDebugFilename(SplitDebugFilename), EmissionKind(EmissionKind),
00483         EnumTypes(EnumTypes), RetainedTypes(RetainedTypes),
00484         Subprograms(Subprograms), GlobalVariables(GlobalVariables),
00485         ImportedEntities(ImportedEntities) {}
00486   MDNodeKeyImpl(const MDCompileUnit *N)
00487       : SourceLanguage(N->getSourceLanguage()), File(N->getFile()),
00488         Producer(N->getProducer()), IsOptimized(N->isOptimized()),
00489         Flags(N->getFlags()), RuntimeVersion(N->getRuntimeVersion()),
00490         SplitDebugFilename(N->getSplitDebugFilename()),
00491         EmissionKind(N->getEmissionKind()), EnumTypes(N->getEnumTypes()),
00492         RetainedTypes(N->getRetainedTypes()), Subprograms(N->getSubprograms()),
00493         GlobalVariables(N->getGlobalVariables()),
00494         ImportedEntities(N->getImportedEntities()) {}
00495 
00496   bool isKeyOf(const MDCompileUnit *RHS) const {
00497     return SourceLanguage == RHS->getSourceLanguage() &&
00498            File == RHS->getFile() && Producer == RHS->getProducer() &&
00499            IsOptimized == RHS->isOptimized() && Flags == RHS->getFlags() &&
00500            RuntimeVersion == RHS->getRuntimeVersion() &&
00501            SplitDebugFilename == RHS->getSplitDebugFilename() &&
00502            EmissionKind == RHS->getEmissionKind() &&
00503            EnumTypes == RHS->getEnumTypes() &&
00504            RetainedTypes == RHS->getRetainedTypes() &&
00505            Subprograms == RHS->getSubprograms() &&
00506            GlobalVariables == RHS->getGlobalVariables() &&
00507            ImportedEntities == RHS->getImportedEntities();
00508   }
00509   unsigned getHashValue() const {
00510     return hash_combine(SourceLanguage, File, Producer, IsOptimized, Flags,
00511                         RuntimeVersion, SplitDebugFilename, EmissionKind,
00512                         EnumTypes, RetainedTypes, Subprograms, GlobalVariables,
00513                         ImportedEntities);
00514   }
00515 };
00516 
00517 template <> struct MDNodeKeyImpl<MDSubprogram> {
00518   Metadata *Scope;
00519   StringRef Name;
00520   StringRef LinkageName;
00521   Metadata *File;
00522   unsigned Line;
00523   Metadata *Type;
00524   bool IsLocalToUnit;
00525   bool IsDefinition;
00526   unsigned ScopeLine;
00527   Metadata *ContainingType;
00528   unsigned Virtuality;
00529   unsigned VirtualIndex;
00530   unsigned Flags;
00531   bool IsOptimized;
00532   Metadata *Function;
00533   Metadata *TemplateParams;
00534   Metadata *Declaration;
00535   Metadata *Variables;
00536 
00537   MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
00538                 Metadata *File, unsigned Line, Metadata *Type,
00539                 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
00540                 Metadata *ContainingType, unsigned Virtuality,
00541                 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
00542                 Metadata *Function, Metadata *TemplateParams,
00543                 Metadata *Declaration, Metadata *Variables)
00544       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
00545         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
00546         IsDefinition(IsDefinition), ScopeLine(ScopeLine),
00547         ContainingType(ContainingType), Virtuality(Virtuality),
00548         VirtualIndex(VirtualIndex), Flags(Flags), IsOptimized(IsOptimized),
00549         Function(Function), TemplateParams(TemplateParams),
00550         Declaration(Declaration), Variables(Variables) {}
00551   MDNodeKeyImpl(const MDSubprogram *N)
00552       : Scope(N->getScope()), Name(N->getName()),
00553         LinkageName(N->getLinkageName()), File(N->getFile()),
00554         Line(N->getLine()), Type(N->getType()),
00555         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
00556         ScopeLine(N->getScopeLine()), ContainingType(N->getContainingType()),
00557         Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()),
00558         Flags(N->getFlags()), IsOptimized(N->isOptimized()),
00559         Function(N->getFunction()), TemplateParams(N->getTemplateParams()),
00560         Declaration(N->getDeclaration()), Variables(N->getVariables()) {}
00561 
00562   bool isKeyOf(const MDSubprogram *RHS) const {
00563     return Scope == RHS->getScope() && Name == RHS->getName() &&
00564            LinkageName == RHS->getLinkageName() && File == RHS->getFile() &&
00565            Line == RHS->getLine() && Type == RHS->getType() &&
00566            IsLocalToUnit == RHS->isLocalToUnit() &&
00567            IsDefinition == RHS->isDefinition() &&
00568            ScopeLine == RHS->getScopeLine() &&
00569            ContainingType == RHS->getContainingType() &&
00570            Virtuality == RHS->getVirtuality() &&
00571            VirtualIndex == RHS->getVirtualIndex() && Flags == RHS->getFlags() &&
00572            IsOptimized == RHS->isOptimized() &&
00573            Function == RHS->getFunction() &&
00574            TemplateParams == RHS->getTemplateParams() &&
00575            Declaration == RHS->getDeclaration() &&
00576            Variables == RHS->getVariables();
00577   }
00578   unsigned getHashValue() const {
00579     return hash_combine(Scope, Name, LinkageName, File, Line, Type,
00580                         IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
00581                         Virtuality, VirtualIndex, Flags, IsOptimized, Function,
00582                         TemplateParams, Declaration, Variables);
00583   }
00584 };
00585 
00586 template <> struct MDNodeKeyImpl<MDLexicalBlock> {
00587   Metadata *Scope;
00588   Metadata *File;
00589   unsigned Line;
00590   unsigned Column;
00591 
00592   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
00593       : Scope(Scope), File(File), Line(Line), Column(Column) {}
00594   MDNodeKeyImpl(const MDLexicalBlock *N)
00595       : Scope(N->getScope()), File(N->getFile()), Line(N->getLine()),
00596         Column(N->getColumn()) {}
00597 
00598   bool isKeyOf(const MDLexicalBlock *RHS) const {
00599     return Scope == RHS->getScope() && File == RHS->getFile() &&
00600            Line == RHS->getLine() && Column == RHS->getColumn();
00601   }
00602   unsigned getHashValue() const {
00603     return hash_combine(Scope, File, Line, Column);
00604   }
00605 };
00606 
00607 template <> struct MDNodeKeyImpl<MDLexicalBlockFile> {
00608   Metadata *Scope;
00609   Metadata *File;
00610   unsigned Discriminator;
00611 
00612   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
00613       : Scope(Scope), File(File), Discriminator(Discriminator) {}
00614   MDNodeKeyImpl(const MDLexicalBlockFile *N)
00615       : Scope(N->getScope()), File(N->getFile()),
00616         Discriminator(N->getDiscriminator()) {}
00617 
00618   bool isKeyOf(const MDLexicalBlockFile *RHS) const {
00619     return Scope == RHS->getScope() && File == RHS->getFile() &&
00620            Discriminator == RHS->getDiscriminator();
00621   }
00622   unsigned getHashValue() const {
00623     return hash_combine(Scope, File, Discriminator);
00624   }
00625 };
00626 
00627 template <> struct MDNodeKeyImpl<MDNamespace> {
00628   Metadata *Scope;
00629   Metadata *File;
00630   StringRef Name;
00631   unsigned Line;
00632 
00633   MDNodeKeyImpl(Metadata *Scope, Metadata *File, StringRef Name, unsigned Line)
00634       : Scope(Scope), File(File), Name(Name), Line(Line) {}
00635   MDNodeKeyImpl(const MDNamespace *N)
00636       : Scope(N->getScope()), File(N->getFile()), Name(N->getName()),
00637         Line(N->getLine()) {}
00638 
00639   bool isKeyOf(const MDNamespace *RHS) const {
00640     return Scope == RHS->getScope() && File == RHS->getFile() &&
00641            Name == RHS->getName() && Line == RHS->getLine();
00642   }
00643   unsigned getHashValue() const {
00644     return hash_combine(Scope, File, Name, Line);
00645   }
00646 };
00647 
00648 template <> struct MDNodeKeyImpl<MDTemplateTypeParameter> {
00649   StringRef Name;
00650   Metadata *Type;
00651 
00652   MDNodeKeyImpl(StringRef Name, Metadata *Type) : Name(Name), Type(Type) {}
00653   MDNodeKeyImpl(const MDTemplateTypeParameter *N)
00654       : Name(N->getName()), Type(N->getType()) {}
00655 
00656   bool isKeyOf(const MDTemplateTypeParameter *RHS) const {
00657     return Name == RHS->getName() && Type == RHS->getType();
00658   }
00659   unsigned getHashValue() const { return hash_combine(Name, Type); }
00660 };
00661 
00662 template <> struct MDNodeKeyImpl<MDTemplateValueParameter> {
00663   unsigned Tag;
00664   StringRef Name;
00665   Metadata *Type;
00666   Metadata *Value;
00667 
00668   MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *Type, Metadata *Value)
00669       : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
00670   MDNodeKeyImpl(const MDTemplateValueParameter *N)
00671       : Tag(N->getTag()), Name(N->getName()), Type(N->getType()),
00672         Value(N->getValue()) {}
00673 
00674   bool isKeyOf(const MDTemplateValueParameter *RHS) const {
00675     return Tag == RHS->getTag() && Name == RHS->getName() &&
00676            Type == RHS->getType() && Value == RHS->getValue();
00677   }
00678   unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
00679 };
00680 
00681 template <> struct MDNodeKeyImpl<MDGlobalVariable> {
00682   Metadata *Scope;
00683   StringRef Name;
00684   StringRef LinkageName;
00685   Metadata *File;
00686   unsigned Line;
00687   Metadata *Type;
00688   bool IsLocalToUnit;
00689   bool IsDefinition;
00690   Metadata *Variable;
00691   Metadata *StaticDataMemberDeclaration;
00692 
00693   MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
00694                 Metadata *File, unsigned Line, Metadata *Type,
00695                 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
00696                 Metadata *StaticDataMemberDeclaration)
00697       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
00698         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
00699         IsDefinition(IsDefinition), Variable(Variable),
00700         StaticDataMemberDeclaration(StaticDataMemberDeclaration) {}
00701   MDNodeKeyImpl(const MDGlobalVariable *N)
00702       : Scope(N->getScope()), Name(N->getName()),
00703         LinkageName(N->getLinkageName()), File(N->getFile()),
00704         Line(N->getLine()), Type(N->getType()),
00705         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
00706         Variable(N->getVariable()),
00707         StaticDataMemberDeclaration(N->getStaticDataMemberDeclaration()) {}
00708 
00709   bool isKeyOf(const MDGlobalVariable *RHS) const {
00710     return Scope == RHS->getScope() && Name == RHS->getName() &&
00711            LinkageName == RHS->getLinkageName() && File == RHS->getFile() &&
00712            Line == RHS->getLine() && Type == RHS->getType() &&
00713            IsLocalToUnit == RHS->isLocalToUnit() &&
00714            IsDefinition == RHS->isDefinition() &&
00715            Variable == RHS->getVariable() &&
00716            StaticDataMemberDeclaration == RHS->getStaticDataMemberDeclaration();
00717   }
00718   unsigned getHashValue() const {
00719     return hash_combine(Scope, Name, LinkageName, File, Line, Type,
00720                         IsLocalToUnit, IsDefinition, Variable,
00721                         StaticDataMemberDeclaration);
00722   }
00723 };
00724 
00725 template <> struct MDNodeKeyImpl<MDLocalVariable> {
00726   unsigned Tag;
00727   Metadata *Scope;
00728   StringRef Name;
00729   Metadata *File;
00730   unsigned Line;
00731   Metadata *Type;
00732   unsigned Arg;
00733   unsigned Flags;
00734   Metadata *InlinedAt;
00735 
00736   MDNodeKeyImpl(unsigned Tag, Metadata *Scope, StringRef Name, Metadata *File,
00737                 unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
00738                 Metadata *InlinedAt)
00739       : Tag(Tag), Scope(Scope), Name(Name), File(File), Line(Line), Type(Type),
00740         Arg(Arg), Flags(Flags), InlinedAt(InlinedAt) {}
00741   MDNodeKeyImpl(const MDLocalVariable *N)
00742       : Tag(N->getTag()), Scope(N->getScope()), Name(N->getName()),
00743         File(N->getFile()), Line(N->getLine()), Type(N->getType()),
00744         Arg(N->getArg()), Flags(N->getFlags()), InlinedAt(N->getInlinedAt()) {}
00745 
00746   bool isKeyOf(const MDLocalVariable *RHS) const {
00747     return Tag == RHS->getTag() && Scope == RHS->getScope() &&
00748            Name == RHS->getName() && File == RHS->getFile() &&
00749            Line == RHS->getLine() && Type == RHS->getType() &&
00750            Arg == RHS->getArg() && Flags == RHS->getFlags() &&
00751            InlinedAt == RHS->getInlinedAt();
00752   }
00753   unsigned getHashValue() const {
00754     return hash_combine(Tag, Scope, Name, File, Line, Type, Arg, Flags,
00755                         InlinedAt);
00756   }
00757 };
00758 
00759 template <> struct MDNodeKeyImpl<MDExpression> {
00760   ArrayRef<uint64_t> Elements;
00761 
00762   MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
00763   MDNodeKeyImpl(const MDExpression *N) : Elements(N->getElements()) {}
00764 
00765   bool isKeyOf(const MDExpression *RHS) const {
00766     return Elements == RHS->getElements();
00767   }
00768   unsigned getHashValue() const {
00769     return hash_combine_range(Elements.begin(), Elements.end());
00770   }
00771 };
00772 
00773 template <> struct MDNodeKeyImpl<MDObjCProperty> {
00774   StringRef Name;
00775   Metadata *File;
00776   unsigned Line;
00777   StringRef GetterName;
00778   StringRef SetterName;
00779   unsigned Attributes;
00780   Metadata *Type;
00781 
00782   MDNodeKeyImpl(StringRef Name, Metadata *File, unsigned Line,
00783                 StringRef GetterName, StringRef SetterName, unsigned Attributes,
00784                 Metadata *Type)
00785       : Name(Name), File(File), Line(Line), GetterName(GetterName),
00786         SetterName(SetterName), Attributes(Attributes), Type(Type) {}
00787   MDNodeKeyImpl(const MDObjCProperty *N)
00788       : Name(N->getName()), File(N->getFile()), Line(N->getLine()),
00789         GetterName(N->getGetterName()), SetterName(N->getSetterName()),
00790         Attributes(N->getAttributes()), Type(N->getType()) {}
00791 
00792   bool isKeyOf(const MDObjCProperty *RHS) const {
00793     return Name == RHS->getName() && File == RHS->getFile() &&
00794            Line == RHS->getLine() && GetterName == RHS->getGetterName() &&
00795            SetterName == RHS->getSetterName() &&
00796            Attributes == RHS->getAttributes() && Type == RHS->getType();
00797   }
00798   unsigned getHashValue() const {
00799     return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
00800                         Type);
00801   }
00802 };
00803 
00804 template <> struct MDNodeKeyImpl<MDImportedEntity> {
00805   unsigned Tag;
00806   Metadata *Scope;
00807   Metadata *Entity;
00808   unsigned Line;
00809   StringRef Name;
00810 
00811   MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line,
00812                 StringRef Name)
00813       : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {}
00814   MDNodeKeyImpl(const MDImportedEntity *N)
00815       : Tag(N->getTag()), Scope(N->getScope()), Entity(N->getEntity()),
00816         Line(N->getLine()), Name(N->getName()) {}
00817 
00818   bool isKeyOf(const MDImportedEntity *RHS) const {
00819     return Tag == RHS->getTag() && Scope == RHS->getScope() &&
00820            Entity == RHS->getEntity() && Line == RHS->getLine() &&
00821            Name == RHS->getName();
00822   }
00823   unsigned getHashValue() const {
00824     return hash_combine(Tag, Scope, Entity, Line, Name);
00825   }
00826 };
00827 
00828 /// \brief DenseMapInfo for MDNode subclasses.
00829 template <class NodeTy> struct MDNodeInfo {
00830   typedef MDNodeKeyImpl<NodeTy> KeyTy;
00831   static inline NodeTy *getEmptyKey() {
00832     return DenseMapInfo<NodeTy *>::getEmptyKey();
00833   }
00834   static inline NodeTy *getTombstoneKey() {
00835     return DenseMapInfo<NodeTy *>::getTombstoneKey();
00836   }
00837   static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
00838   static unsigned getHashValue(const NodeTy *N) {
00839     return KeyTy(N).getHashValue();
00840   }
00841   static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
00842     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00843       return false;
00844     return LHS.isKeyOf(RHS);
00845   }
00846   static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
00847     return LHS == RHS;
00848   }
00849 };
00850 
00851 #define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo<CLASS> CLASS##Info;
00852 #include "llvm/IR/Metadata.def"
00853 
00854 class LLVMContextImpl {
00855 public:
00856   /// OwnedModules - The set of modules instantiated in this context, and which
00857   /// will be automatically deleted if this context is deleted.
00858   SmallPtrSet<Module*, 4> OwnedModules;
00859   
00860   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
00861   void *InlineAsmDiagContext;
00862 
00863   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
00864   void *DiagnosticContext;
00865   bool RespectDiagnosticFilters;
00866 
00867   LLVMContext::YieldCallbackTy YieldCallback;
00868   void *YieldOpaqueHandle;
00869 
00870   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
00871   IntMapTy IntConstants;
00872 
00873   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
00874   FPMapTy FPConstants;
00875 
00876   FoldingSet<AttributeImpl> AttrsSet;
00877   FoldingSet<AttributeSetImpl> AttrsLists;
00878   FoldingSet<AttributeSetNode> AttrsSetNodes;
00879 
00880   StringMap<MDString> MDStringCache;
00881   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
00882   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
00883 
00884 #define HANDLE_MDNODE_LEAF(CLASS) DenseSet<CLASS *, CLASS##Info> CLASS##s;
00885 #include "llvm/IR/Metadata.def"
00886 
00887   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
00888   // aren't in the MDNodeSet, but they're still shared between objects, so no
00889   // one object can destroy them.  This set allows us to at least destroy them
00890   // on Context destruction.
00891   SmallPtrSet<MDNode *, 1> DistinctMDNodes;
00892 
00893   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
00894 
00895   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
00896   ArrayConstantsTy ArrayConstants;
00897   
00898   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
00899   StructConstantsTy StructConstants;
00900   
00901   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
00902   VectorConstantsTy VectorConstants;
00903   
00904   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
00905 
00906   DenseMap<Type*, UndefValue*> UVConstants;
00907   
00908   StringMap<ConstantDataSequential*> CDSConstants;
00909 
00910   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
00911     BlockAddresses;
00912   ConstantUniqueMap<ConstantExpr> ExprConstants;
00913 
00914   ConstantUniqueMap<InlineAsm> InlineAsms;
00915 
00916   ConstantInt *TheTrueVal;
00917   ConstantInt *TheFalseVal;
00918 
00919   // Basic type instances.
00920   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
00921   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
00922   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
00923 
00924   
00925   /// TypeAllocator - All dynamically allocated types are allocated from this.
00926   /// They live forever until the context is torn down.
00927   BumpPtrAllocator TypeAllocator;
00928   
00929   DenseMap<unsigned, IntegerType*> IntegerTypes;
00930 
00931   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
00932   FunctionTypeSet FunctionTypes;
00933   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
00934   StructTypeSet AnonStructTypes;
00935   StringMap<StructType*> NamedStructTypes;
00936   unsigned NamedStructTypesUniqueID;
00937     
00938   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
00939   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
00940   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
00941   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
00942 
00943 
00944   /// ValueHandles - This map keeps track of all of the value handles that are
00945   /// watching a Value*.  The Value::HasValueHandle bit is used to know
00946   /// whether or not a value has an entry in this map.
00947   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
00948   ValueHandlesTy ValueHandles;
00949   
00950   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
00951   StringMap<unsigned> CustomMDKindNames;
00952 
00953   typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
00954   typedef SmallVector<MDPairTy, 2> MDMapTy;
00955 
00956   /// MetadataStore - Collection of per-instruction metadata used in this
00957   /// context.
00958   DenseMap<const Instruction *, MDMapTy> MetadataStore;
00959   
00960   /// DiscriminatorTable - This table maps file:line locations to an
00961   /// integer representing the next DWARF path discriminator to assign to
00962   /// instructions in different blocks at the same location.
00963   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
00964 
00965   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
00966   /// requested in this context
00967   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
00968   IntrinsicIDCacheTy IntrinsicIDCache;
00969 
00970   /// \brief Mapping from a function to its prefix data, which is stored as the
00971   /// operand of an unparented ReturnInst so that the prefix data has a Use.
00972   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
00973   PrefixDataMapTy PrefixDataMap;
00974 
00975   /// \brief Mapping from a function to its prologue data, which is stored as
00976   /// the operand of an unparented ReturnInst so that the prologue data has a
00977   /// Use.
00978   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
00979   PrologueDataMapTy PrologueDataMap;
00980 
00981   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
00982   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
00983 
00984   LLVMContextImpl(LLVMContext &C);
00985   ~LLVMContextImpl();
00986 
00987   /// Destroy the ConstantArrays if they are not used.
00988   void dropTriviallyDeadConstantArrays();
00989 };
00990 
00991 }
00992 
00993 #endif