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/DerivedTypes.h"
00031 #include "llvm/IR/LLVMContext.h"
00032 #include "llvm/IR/Metadata.h"
00033 #include "llvm/IR/ValueHandle.h"
00034 #include <vector>
00035 
00036 namespace llvm {
00037 
00038 class ConstantInt;
00039 class ConstantFP;
00040 class DiagnosticInfoOptimizationRemark;
00041 class DiagnosticInfoOptimizationRemarkMissed;
00042 class DiagnosticInfoOptimizationRemarkAnalysis;
00043 class GCStrategy;
00044 class LLVMContext;
00045 class Type;
00046 class Value;
00047 
00048 struct DenseMapAPIntKeyInfo {
00049   static inline APInt getEmptyKey() {
00050     APInt V(nullptr, 0);
00051     V.VAL = 0;
00052     return V;
00053   }
00054   static inline APInt getTombstoneKey() {
00055     APInt V(nullptr, 0);
00056     V.VAL = 1;
00057     return V;
00058   }
00059   static unsigned getHashValue(const APInt &Key) {
00060     return static_cast<unsigned>(hash_value(Key));
00061   }
00062   static bool isEqual(const APInt &LHS, const APInt &RHS) {
00063     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
00064   }
00065 };
00066 
00067 struct DenseMapAPFloatKeyInfo {
00068   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
00069   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
00070   static unsigned getHashValue(const APFloat &Key) {
00071     return static_cast<unsigned>(hash_value(Key));
00072   }
00073   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
00074     return LHS.bitwiseIsEqual(RHS);
00075   }
00076 };
00077 
00078 struct AnonStructTypeKeyInfo {
00079   struct KeyTy {
00080     ArrayRef<Type*> ETypes;
00081     bool isPacked;
00082     KeyTy(const ArrayRef<Type*>& E, bool P) :
00083       ETypes(E), isPacked(P) {}
00084     KeyTy(const StructType *ST)
00085         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
00086     bool operator==(const KeyTy& that) const {
00087       if (isPacked != that.isPacked)
00088         return false;
00089       if (ETypes != that.ETypes)
00090         return false;
00091       return true;
00092     }
00093     bool operator!=(const KeyTy& that) const {
00094       return !this->operator==(that);
00095     }
00096   };
00097   static inline StructType* getEmptyKey() {
00098     return DenseMapInfo<StructType*>::getEmptyKey();
00099   }
00100   static inline StructType* getTombstoneKey() {
00101     return DenseMapInfo<StructType*>::getTombstoneKey();
00102   }
00103   static unsigned getHashValue(const KeyTy& Key) {
00104     return hash_combine(hash_combine_range(Key.ETypes.begin(),
00105                                            Key.ETypes.end()),
00106                         Key.isPacked);
00107   }
00108   static unsigned getHashValue(const StructType *ST) {
00109     return getHashValue(KeyTy(ST));
00110   }
00111   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
00112     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00113       return false;
00114     return LHS == KeyTy(RHS);
00115   }
00116   static bool isEqual(const StructType *LHS, const StructType *RHS) {
00117     return LHS == RHS;
00118   }
00119 };
00120 
00121 struct FunctionTypeKeyInfo {
00122   struct KeyTy {
00123     const Type *ReturnType;
00124     ArrayRef<Type*> Params;
00125     bool isVarArg;
00126     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
00127       ReturnType(R), Params(P), isVarArg(V) {}
00128     KeyTy(const FunctionType *FT)
00129         : ReturnType(FT->getReturnType()), Params(FT->params()),
00130           isVarArg(FT->isVarArg()) {}
00131     bool operator==(const KeyTy& that) const {
00132       if (ReturnType != that.ReturnType)
00133         return false;
00134       if (isVarArg != that.isVarArg)
00135         return false;
00136       if (Params != that.Params)
00137         return false;
00138       return true;
00139     }
00140     bool operator!=(const KeyTy& that) const {
00141       return !this->operator==(that);
00142     }
00143   };
00144   static inline FunctionType* getEmptyKey() {
00145     return DenseMapInfo<FunctionType*>::getEmptyKey();
00146   }
00147   static inline FunctionType* getTombstoneKey() {
00148     return DenseMapInfo<FunctionType*>::getTombstoneKey();
00149   }
00150   static unsigned getHashValue(const KeyTy& Key) {
00151     return hash_combine(Key.ReturnType,
00152                         hash_combine_range(Key.Params.begin(),
00153                                            Key.Params.end()),
00154                         Key.isVarArg);
00155   }
00156   static unsigned getHashValue(const FunctionType *FT) {
00157     return getHashValue(KeyTy(FT));
00158   }
00159   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
00160     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00161       return false;
00162     return LHS == KeyTy(RHS);
00163   }
00164   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
00165     return LHS == RHS;
00166   }
00167 };
00168 
00169 /// \brief Structure for hashing arbitrary MDNode operands.
00170 class MDNodeOpsKey {
00171   ArrayRef<Metadata *> RawOps;
00172   ArrayRef<MDOperand> Ops;
00173 
00174   unsigned Hash;
00175 
00176 protected:
00177   MDNodeOpsKey(ArrayRef<Metadata *> Ops)
00178       : RawOps(Ops), Hash(calculateHash(Ops)) {}
00179 
00180   template <class NodeTy>
00181   MDNodeOpsKey(NodeTy *N, unsigned Offset = 0)
00182       : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
00183 
00184   template <class NodeTy>
00185   bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
00186     if (getHash() != RHS->getHash())
00187       return false;
00188 
00189     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
00190     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
00191                           : compareOps(RawOps, RHS, Offset);
00192   }
00193 
00194   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
00195 
00196 private:
00197   template <class T>
00198   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
00199     if (Ops.size() != RHS->getNumOperands() - Offset)
00200       return false;
00201     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
00202   }
00203 
00204   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
00205 
00206 public:
00207   unsigned getHash() const { return Hash; }
00208 };
00209 
00210 /// \brief DenseMapInfo for MDTuple.
00211 ///
00212 /// Note that we don't need the is-function-local bit, since that's implicit in
00213 /// the operands.
00214 struct MDTupleInfo {
00215   struct KeyTy : MDNodeOpsKey {
00216     KeyTy(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
00217     KeyTy(MDTuple *N) : MDNodeOpsKey(N) {}
00218 
00219     bool operator==(const MDTuple *RHS) const {
00220       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00221         return false;
00222       return compareOps(RHS);
00223     }
00224 
00225     static unsigned calculateHash(MDTuple *N) {
00226       return MDNodeOpsKey::calculateHash(N);
00227     }
00228   };
00229   static inline MDTuple *getEmptyKey() {
00230     return DenseMapInfo<MDTuple *>::getEmptyKey();
00231   }
00232   static inline MDTuple *getTombstoneKey() {
00233     return DenseMapInfo<MDTuple *>::getTombstoneKey();
00234   }
00235   static unsigned getHashValue(const KeyTy &Key) { return Key.getHash(); }
00236   static unsigned getHashValue(const MDTuple *U) { return U->getHash(); }
00237   static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
00238     return LHS == RHS;
00239   }
00240   static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
00241     return LHS == RHS;
00242   }
00243 };
00244 
00245 /// \brief DenseMapInfo for MDLocation.
00246 struct MDLocationInfo {
00247   struct KeyTy {
00248     unsigned Line;
00249     unsigned Column;
00250     Metadata *Scope;
00251     Metadata *InlinedAt;
00252 
00253     KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
00254         : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
00255 
00256     KeyTy(const MDLocation *L)
00257         : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
00258           InlinedAt(L->getInlinedAt()) {}
00259 
00260     bool operator==(const MDLocation *RHS) const {
00261       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00262         return false;
00263       return Line == RHS->getLine() && Column == RHS->getColumn() &&
00264              Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
00265     }
00266   };
00267   static inline MDLocation *getEmptyKey() {
00268     return DenseMapInfo<MDLocation *>::getEmptyKey();
00269   }
00270   static inline MDLocation *getTombstoneKey() {
00271     return DenseMapInfo<MDLocation *>::getTombstoneKey();
00272   }
00273   static unsigned getHashValue(const KeyTy &Key) {
00274     return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
00275   }
00276   static unsigned getHashValue(const MDLocation *U) {
00277     return getHashValue(KeyTy(U));
00278   }
00279   static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
00280     return LHS == RHS;
00281   }
00282   static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
00283     return LHS == RHS;
00284   }
00285 };
00286 
00287 /// \brief DenseMapInfo for GenericDebugNode.
00288 struct GenericDebugNodeInfo {
00289   struct KeyTy : MDNodeOpsKey {
00290     unsigned Tag;
00291     StringRef Header;
00292     KeyTy(unsigned Tag, StringRef Header, ArrayRef<Metadata *> DwarfOps)
00293         : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
00294     KeyTy(GenericDebugNode *N)
00295         : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
00296 
00297     bool operator==(const GenericDebugNode *RHS) const {
00298       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00299         return false;
00300       return Tag == RHS->getTag() && Header == RHS->getHeader() &&
00301              compareOps(RHS, 1);
00302     }
00303 
00304     static unsigned calculateHash(GenericDebugNode *N) {
00305       return MDNodeOpsKey::calculateHash(N, 1);
00306     }
00307   };
00308   static inline GenericDebugNode *getEmptyKey() {
00309     return DenseMapInfo<GenericDebugNode *>::getEmptyKey();
00310   }
00311   static inline GenericDebugNode *getTombstoneKey() {
00312     return DenseMapInfo<GenericDebugNode *>::getTombstoneKey();
00313   }
00314   static unsigned getHashValue(const KeyTy &Key) {
00315     return hash_combine(Key.getHash(), Key.Tag, Key.Header);
00316   }
00317   static unsigned getHashValue(const GenericDebugNode *U) {
00318     return hash_combine(U->getHash(), U->getTag(), U->getHeader());
00319   }
00320   static bool isEqual(const KeyTy &LHS, const GenericDebugNode *RHS) {
00321     return LHS == RHS;
00322   }
00323   static bool isEqual(const GenericDebugNode *LHS,
00324                       const GenericDebugNode *RHS) {
00325     return LHS == RHS;
00326   }
00327 };
00328 
00329 class LLVMContextImpl {
00330 public:
00331   /// OwnedModules - The set of modules instantiated in this context, and which
00332   /// will be automatically deleted if this context is deleted.
00333   SmallPtrSet<Module*, 4> OwnedModules;
00334   
00335   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
00336   void *InlineAsmDiagContext;
00337 
00338   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
00339   void *DiagnosticContext;
00340   bool RespectDiagnosticFilters;
00341 
00342   LLVMContext::YieldCallbackTy YieldCallback;
00343   void *YieldOpaqueHandle;
00344 
00345   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
00346   IntMapTy IntConstants;
00347 
00348   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
00349   FPMapTy FPConstants;
00350 
00351   FoldingSet<AttributeImpl> AttrsSet;
00352   FoldingSet<AttributeSetImpl> AttrsLists;
00353   FoldingSet<AttributeSetNode> AttrsSetNodes;
00354 
00355   StringMap<MDString> MDStringCache;
00356   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
00357   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
00358 
00359   DenseSet<MDTuple *, MDTupleInfo> MDTuples;
00360   DenseSet<MDLocation *, MDLocationInfo> MDLocations;
00361   DenseSet<GenericDebugNode *, GenericDebugNodeInfo> GenericDebugNodes;
00362 
00363   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
00364   // aren't in the MDNodeSet, but they're still shared between objects, so no
00365   // one object can destroy them.  This set allows us to at least destroy them
00366   // on Context destruction.
00367   SmallPtrSet<MDNode *, 1> DistinctMDNodes;
00368 
00369   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
00370 
00371   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
00372   ArrayConstantsTy ArrayConstants;
00373   
00374   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
00375   StructConstantsTy StructConstants;
00376   
00377   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
00378   VectorConstantsTy VectorConstants;
00379   
00380   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
00381 
00382   DenseMap<Type*, UndefValue*> UVConstants;
00383   
00384   StringMap<ConstantDataSequential*> CDSConstants;
00385 
00386   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
00387     BlockAddresses;
00388   ConstantUniqueMap<ConstantExpr> ExprConstants;
00389 
00390   ConstantUniqueMap<InlineAsm> InlineAsms;
00391 
00392   ConstantInt *TheTrueVal;
00393   ConstantInt *TheFalseVal;
00394 
00395   // Basic type instances.
00396   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
00397   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
00398   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
00399 
00400   
00401   /// TypeAllocator - All dynamically allocated types are allocated from this.
00402   /// They live forever until the context is torn down.
00403   BumpPtrAllocator TypeAllocator;
00404   
00405   DenseMap<unsigned, IntegerType*> IntegerTypes;
00406 
00407   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
00408   FunctionTypeSet FunctionTypes;
00409   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
00410   StructTypeSet AnonStructTypes;
00411   StringMap<StructType*> NamedStructTypes;
00412   unsigned NamedStructTypesUniqueID;
00413     
00414   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
00415   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
00416   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
00417   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
00418 
00419 
00420   /// ValueHandles - This map keeps track of all of the value handles that are
00421   /// watching a Value*.  The Value::HasValueHandle bit is used to know
00422   /// whether or not a value has an entry in this map.
00423   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
00424   ValueHandlesTy ValueHandles;
00425   
00426   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
00427   StringMap<unsigned> CustomMDKindNames;
00428 
00429   typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
00430   typedef SmallVector<MDPairTy, 2> MDMapTy;
00431 
00432   /// MetadataStore - Collection of per-instruction metadata used in this
00433   /// context.
00434   DenseMap<const Instruction *, MDMapTy> MetadataStore;
00435   
00436   /// DiscriminatorTable - This table maps file:line locations to an
00437   /// integer representing the next DWARF path discriminator to assign to
00438   /// instructions in different blocks at the same location.
00439   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
00440 
00441   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
00442   /// requested in this context
00443   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
00444   IntrinsicIDCacheTy IntrinsicIDCache;
00445 
00446   /// \brief Mapping from a function to its prefix data, which is stored as the
00447   /// operand of an unparented ReturnInst so that the prefix data has a Use.
00448   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
00449   PrefixDataMapTy PrefixDataMap;
00450 
00451   /// \brief Mapping from a function to its prologue data, which is stored as
00452   /// the operand of an unparented ReturnInst so that the prologue data has a
00453   /// Use.
00454   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
00455   PrologueDataMapTy PrologueDataMap;
00456 
00457   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
00458   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
00459 
00460   LLVMContextImpl(LLVMContext &C);
00461   ~LLVMContextImpl();
00462 
00463   /// Destroy the ConstantArrays if they are not used.
00464   void dropTriviallyDeadConstantArrays();
00465 };
00466 
00467 }
00468 
00469 #endif