| Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 1 | //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===// | 
| Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// | 
| Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // | 
|  | 10 | //  This file declares LLVMContextImpl, the opaque implementation | 
|  | 11 | //  of LLVMContext. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
| Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 |  | 
|  | 15 | #ifndef LLVM_LLVMCONTEXT_IMPL_H | 
|  | 16 | #define LLVM_LLVMCONTEXT_IMPL_H | 
|  | 17 |  | 
| Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" | 
| Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 19 | #include "ConstantsContext.h" | 
| Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 20 | #include "LeaksContext.h" | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" | 
| Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" | 
| Jeffrey Yasskin | cd3706c | 2010-03-21 22:08:41 +0000 | [diff] [blame] | 23 | #include "llvm/Metadata.h" | 
| Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ValueHandle.h" | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/APFloat.h" | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/APInt.h" | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ArrayRef.h" | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" | 
| Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/FoldingSet.h" | 
| Chris Lattner | 1910c9c | 2012-01-23 08:42:38 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/OwningPtr.h" | 
| Jeffrey Yasskin | 28f2448 | 2009-12-17 19:55:06 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" | 
| Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringMap.h" | 
| Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 33 | #include <vector> | 
| Owen Anderson | 39ede7b | 2009-07-21 20:13:12 +0000 | [diff] [blame] | 34 |  | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 35 | namespace llvm { | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 36 |  | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 37 | class ConstantInt; | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 38 | class ConstantFP; | 
| Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 39 | class LLVMContext; | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 40 | class Type; | 
| Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 41 | class Value; | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | struct DenseMapAPIntKeyInfo { | 
|  | 44 | struct KeyTy { | 
|  | 45 | APInt val; | 
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 46 | Type* type; | 
|  | 47 | KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {} | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 48 | KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} | 
|  | 49 | bool operator==(const KeyTy& that) const { | 
|  | 50 | return type == that.type && this->val == that.val; | 
|  | 51 | } | 
|  | 52 | bool operator!=(const KeyTy& that) const { | 
|  | 53 | return !this->operator==(that); | 
|  | 54 | } | 
|  | 55 | }; | 
|  | 56 | static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } | 
|  | 57 | static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } | 
|  | 58 | static unsigned getHashValue(const KeyTy &Key) { | 
|  | 59 | return DenseMapInfo<void*>::getHashValue(Key.type) ^ | 
|  | 60 | Key.val.getHashValue(); | 
|  | 61 | } | 
|  | 62 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { | 
|  | 63 | return LHS == RHS; | 
|  | 64 | } | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 65 | }; | 
|  | 66 |  | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 67 | struct DenseMapAPFloatKeyInfo { | 
|  | 68 | struct KeyTy { | 
|  | 69 | APFloat val; | 
|  | 70 | KeyTy(const APFloat& V) : val(V){} | 
|  | 71 | KeyTy(const KeyTy& that) : val(that.val) {} | 
|  | 72 | bool operator==(const KeyTy& that) const { | 
|  | 73 | return this->val.bitwiseIsEqual(that.val); | 
|  | 74 | } | 
|  | 75 | bool operator!=(const KeyTy& that) const { | 
|  | 76 | return !this->operator==(that); | 
|  | 77 | } | 
|  | 78 | }; | 
|  | 79 | static inline KeyTy getEmptyKey() { | 
|  | 80 | return KeyTy(APFloat(APFloat::Bogus,1)); | 
|  | 81 | } | 
|  | 82 | static inline KeyTy getTombstoneKey() { | 
|  | 83 | return KeyTy(APFloat(APFloat::Bogus,2)); | 
|  | 84 | } | 
|  | 85 | static unsigned getHashValue(const KeyTy &Key) { | 
|  | 86 | return Key.val.getHashValue(); | 
|  | 87 | } | 
|  | 88 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { | 
|  | 89 | return LHS == RHS; | 
|  | 90 | } | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 91 | }; | 
|  | 92 |  | 
| Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 93 | /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps | 
|  | 94 | /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp. | 
|  | 95 | class DebugRecVH : public CallbackVH { | 
|  | 96 | /// Ctx - This is the LLVM Context being referenced. | 
|  | 97 | LLVMContextImpl *Ctx; | 
|  | 98 |  | 
|  | 99 | /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that | 
|  | 100 | /// this reference lives in.  If this is zero, then it represents a | 
|  | 101 | /// non-canonical entry that has no DenseMap value.  This can happen due to | 
|  | 102 | /// RAUW. | 
|  | 103 | int Idx; | 
|  | 104 | public: | 
|  | 105 | DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx) | 
|  | 106 | : CallbackVH(n), Ctx(ctx), Idx(idx) {} | 
|  | 107 |  | 
|  | 108 | MDNode *get() const { | 
|  | 109 | return cast_or_null<MDNode>(getValPtr()); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | virtual void deleted(); | 
|  | 113 | virtual void allUsesReplacedWith(Value *VNew); | 
|  | 114 | }; | 
|  | 115 |  | 
| Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 116 | class LLVMContextImpl { | 
|  | 117 | public: | 
| Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 118 | /// OwnedModules - The set of modules instantiated in this context, and which | 
|  | 119 | /// will be automatically deleted if this context is deleted. | 
|  | 120 | SmallPtrSet<Module*, 4> OwnedModules; | 
|  | 121 |  | 
| Chris Lattner | b0e3608 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 122 | LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler; | 
|  | 123 | void *InlineAsmDiagContext; | 
| Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 124 |  | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 125 | typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, | 
| Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 126 | DenseMapAPIntKeyInfo> IntMapTy; | 
| Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 127 | IntMapTy IntConstants; | 
|  | 128 |  | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 129 | typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, | 
| Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 130 | DenseMapAPFloatKeyInfo> FPMapTy; | 
| Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 131 | FPMapTy FPConstants; | 
|  | 132 |  | 
| Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 133 | StringMap<MDString*> MDStringCache; | 
|  | 134 |  | 
| Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 135 | FoldingSet<MDNode> MDNodeSet; | 
| Jeffrey Yasskin | 2cc2476 | 2010-03-13 01:26:15 +0000 | [diff] [blame] | 136 | // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they | 
|  | 137 | // aren't in the MDNodeSet, but they're still shared between objects, so no | 
|  | 138 | // one object can destroy them.  This set allows us to at least destroy them | 
|  | 139 | // on Context destruction. | 
|  | 140 | SmallPtrSet<MDNode*, 1> NonUniquedMDNodes; | 
| Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 141 |  | 
| Chris Lattner | 1910c9c | 2012-01-23 08:42:38 +0000 | [diff] [blame] | 142 | typedef DenseMap<Type*, OwningPtr<ConstantAggregateZero> > CAZMapTy; | 
|  | 143 | CAZMapTy CAZConstants; | 
| Owen Anderson | 13234f8 | 2009-08-10 18:16:08 +0000 | [diff] [blame] | 144 |  | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 145 | typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, | 
|  | 146 | ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy; | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 147 | ArrayConstantsTy ArrayConstants; | 
| Owen Anderson | 39ede7b | 2009-07-21 20:13:12 +0000 | [diff] [blame] | 148 |  | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 149 | typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, | 
|  | 150 | StructType, ConstantStruct, true /*largekey*/> StructConstantsTy; | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 151 | StructConstantsTy StructConstants; | 
| Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 152 |  | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 153 | typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>, | 
|  | 154 | VectorType, ConstantVector> VectorConstantsTy; | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 155 | VectorConstantsTy VectorConstants; | 
| Owen Anderson | 0348a13 | 2009-07-24 00:36:24 +0000 | [diff] [blame] | 156 |  | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 157 | ConstantUniqueMap<char, char, PointerType, ConstantPointerNull> | 
|  | 158 | NullPtrConstants; | 
|  | 159 | ConstantUniqueMap<char, char, Type, UndefValue> UndefValueConstants; | 
| Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 160 |  | 
| Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 161 | DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses; | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 162 | ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> | 
|  | 163 | ExprConstants; | 
| Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 164 |  | 
| Jay Foad | c365eea | 2011-06-22 08:50:06 +0000 | [diff] [blame] | 165 | ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType, | 
|  | 166 | InlineAsm> InlineAsms; | 
| Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 167 |  | 
| Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 168 | ConstantInt *TheTrueVal; | 
|  | 169 | ConstantInt *TheFalseVal; | 
|  | 170 |  | 
| Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 171 | LeakDetectorImpl<Value> LLVMObjects; | 
|  | 172 |  | 
| Dan Gohman | 97d2cb8 | 2009-08-25 16:00:35 +0000 | [diff] [blame] | 173 | // Basic type instances. | 
| Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 174 | Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; | 
| Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 175 | Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; | 
|  | 176 | IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty; | 
| Dan Gohman | 97d2cb8 | 2009-08-25 16:00:35 +0000 | [diff] [blame] | 177 |  | 
| Chris Lattner | 07bd69c | 2011-07-15 05:49:15 +0000 | [diff] [blame] | 178 |  | 
|  | 179 | /// TypeAllocator - All dynamically allocated types are allocated from this. | 
|  | 180 | /// They live forever until the context is torn down. | 
|  | 181 | BumpPtrAllocator TypeAllocator; | 
|  | 182 |  | 
| Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 183 | DenseMap<unsigned, IntegerType*> IntegerTypes; | 
|  | 184 |  | 
|  | 185 | // TODO: Optimize FunctionTypes/AnonStructTypes! | 
|  | 186 | std::map<std::vector<Type*>, FunctionType*> FunctionTypes; | 
|  | 187 | std::map<std::vector<Type*>, StructType*> AnonStructTypes; | 
|  | 188 | StringMap<StructType*> NamedStructTypes; | 
|  | 189 | unsigned NamedStructTypesUniqueID; | 
|  | 190 |  | 
|  | 191 | DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes; | 
|  | 192 | DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes; | 
|  | 193 | DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0 | 
|  | 194 | DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes; | 
| Jeffrey Yasskin | c660b23 | 2010-02-11 06:41:30 +0000 | [diff] [blame] | 195 |  | 
| Jeffrey Yasskin | 28f2448 | 2009-12-17 19:55:06 +0000 | [diff] [blame] | 196 |  | 
| Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 197 | /// ValueHandles - This map keeps track of all of the value handles that are | 
|  | 198 | /// watching a Value*.  The Value::HasValueHandle bit is used to know | 
|  | 199 | // whether or not a value has an entry in this map. | 
|  | 200 | typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; | 
|  | 201 | ValueHandlesTy ValueHandles; | 
|  | 202 |  | 
| Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 203 | /// CustomMDKindNames - Map to hold the metadata string to ID mapping. | 
|  | 204 | StringMap<unsigned> CustomMDKindNames; | 
|  | 205 |  | 
|  | 206 | typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; | 
|  | 207 | typedef SmallVector<MDPairTy, 2> MDMapTy; | 
|  | 208 |  | 
|  | 209 | /// MetadataStore - Collection of per-instruction metadata used in this | 
|  | 210 | /// context. | 
|  | 211 | DenseMap<const Instruction *, MDMapTy> MetadataStore; | 
|  | 212 |  | 
| Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 213 | /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope | 
|  | 214 | /// entry with no "inlined at" element. | 
|  | 215 | DenseMap<MDNode*, int> ScopeRecordIdx; | 
|  | 216 |  | 
|  | 217 | /// ScopeRecords - These are the actual mdnodes (in a value handle) for an | 
|  | 218 | /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if | 
|  | 219 | /// the MDNode is RAUW'd. | 
|  | 220 | std::vector<DebugRecVH> ScopeRecords; | 
|  | 221 |  | 
|  | 222 | /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an | 
|  | 223 | /// scope/inlined-at pair. | 
|  | 224 | DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx; | 
|  | 225 |  | 
|  | 226 | /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles) | 
|  | 227 | /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up | 
|  | 228 | /// to date. | 
|  | 229 | std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords; | 
|  | 230 |  | 
|  | 231 | int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); | 
|  | 232 | int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); | 
|  | 233 |  | 
| Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 234 | LLVMContextImpl(LLVMContext &C); | 
|  | 235 | ~LLVMContextImpl(); | 
| Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 236 | }; | 
|  | 237 |  | 
|  | 238 | } | 
|  | 239 |  | 
| Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 240 | #endif |