Owen Anderson | 3fb4aab | 2009-08-23 04:24:24 +0000 | [diff] [blame] | 1 | //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class --------------===// |
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 | |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 18 | #include "ConstantsContext.h" |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 19 | #include "LeaksContext.h" |
Owen Anderson | 542cffc | 2009-08-04 23:33:01 +0000 | [diff] [blame] | 20 | #include "TypesContext.h" |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 22 | #include "llvm/Metadata.h" |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Owen Anderson | 9801560 | 2009-08-17 17:59:35 +0000 | [diff] [blame] | 25 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/APFloat.h" |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/APInt.h" |
| 29 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/FoldingSet.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; |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 39 | class MDString; |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 40 | class MDNode; |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 41 | class LLVMContext; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 42 | class Type; |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 43 | class Value; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 44 | |
| 45 | struct DenseMapAPIntKeyInfo { |
| 46 | struct KeyTy { |
| 47 | APInt val; |
| 48 | const Type* type; |
| 49 | KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} |
| 50 | KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} |
| 51 | bool operator==(const KeyTy& that) const { |
| 52 | return type == that.type && this->val == that.val; |
| 53 | } |
| 54 | bool operator!=(const KeyTy& that) const { |
| 55 | return !this->operator==(that); |
| 56 | } |
| 57 | }; |
| 58 | static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } |
| 59 | static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } |
| 60 | static unsigned getHashValue(const KeyTy &Key) { |
| 61 | return DenseMapInfo<void*>::getHashValue(Key.type) ^ |
| 62 | Key.val.getHashValue(); |
| 63 | } |
| 64 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 65 | return LHS == RHS; |
| 66 | } |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 69 | struct DenseMapAPFloatKeyInfo { |
| 70 | struct KeyTy { |
| 71 | APFloat val; |
| 72 | KeyTy(const APFloat& V) : val(V){} |
| 73 | KeyTy(const KeyTy& that) : val(that.val) {} |
| 74 | bool operator==(const KeyTy& that) const { |
| 75 | return this->val.bitwiseIsEqual(that.val); |
| 76 | } |
| 77 | bool operator!=(const KeyTy& that) const { |
| 78 | return !this->operator==(that); |
| 79 | } |
| 80 | }; |
| 81 | static inline KeyTy getEmptyKey() { |
| 82 | return KeyTy(APFloat(APFloat::Bogus,1)); |
| 83 | } |
| 84 | static inline KeyTy getTombstoneKey() { |
| 85 | return KeyTy(APFloat(APFloat::Bogus,2)); |
| 86 | } |
| 87 | static unsigned getHashValue(const KeyTy &Key) { |
| 88 | return Key.val.getHashValue(); |
| 89 | } |
| 90 | static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { |
| 91 | return LHS == RHS; |
| 92 | } |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 95 | class LLVMContextImpl { |
| 96 | public: |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 97 | typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 98 | DenseMapAPIntKeyInfo> IntMapTy; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 99 | IntMapTy IntConstants; |
| 100 | |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 101 | typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 102 | DenseMapAPFloatKeyInfo> FPMapTy; |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 103 | FPMapTy FPConstants; |
| 104 | |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 105 | StringMap<MDString*> MDStringCache; |
| 106 | |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 107 | FoldingSet<MDNode> MDNodeSet; |
| 108 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 109 | ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants; |
Owen Anderson | 13234f8 | 2009-08-10 18:16:08 +0000 | [diff] [blame] | 110 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 111 | typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType, |
Owen Anderson | 3d34492 | 2009-07-21 20:55:28 +0000 | [diff] [blame] | 112 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 113 | ArrayConstantsTy ArrayConstants; |
Owen Anderson | 39ede7b | 2009-07-21 20:13:12 +0000 | [diff] [blame] | 114 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 115 | typedef ConstantUniqueMap<std::vector<Constant*>, StructType, |
| 116 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 117 | StructConstantsTy StructConstants; |
Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame^] | 119 | typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion> |
| 120 | UnionConstantsTy; |
| 121 | UnionConstantsTy UnionConstants; |
| 122 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 123 | typedef ConstantUniqueMap<std::vector<Constant*>, VectorType, |
| 124 | ConstantVector> VectorConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 125 | VectorConstantsTy VectorConstants; |
Owen Anderson | 0348a13 | 2009-07-24 00:36:24 +0000 | [diff] [blame] | 126 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 127 | ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 128 | |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 129 | ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants; |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 131 | DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses; |
Jeffrey Yasskin | f6ee7be | 2009-10-27 23:45:55 +0000 | [diff] [blame] | 132 | ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 133 | |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 134 | ConstantInt *TheTrueVal; |
| 135 | ConstantInt *TheFalseVal; |
| 136 | |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 137 | LeakDetectorImpl<Value> LLVMObjects; |
| 138 | |
Dan Gohman | 97d2cb8 | 2009-08-25 16:00:35 +0000 | [diff] [blame] | 139 | // Basic type instances. |
| 140 | const Type VoidTy; |
| 141 | const Type LabelTy; |
| 142 | const Type FloatTy; |
| 143 | const Type DoubleTy; |
| 144 | const Type MetadataTy; |
| 145 | const Type X86_FP80Ty; |
| 146 | const Type FP128Ty; |
| 147 | const Type PPC_FP128Ty; |
| 148 | const IntegerType Int1Ty; |
| 149 | const IntegerType Int8Ty; |
| 150 | const IntegerType Int16Ty; |
| 151 | const IntegerType Int32Ty; |
| 152 | const IntegerType Int64Ty; |
| 153 | |
Owen Anderson | 9801560 | 2009-08-17 17:59:35 +0000 | [diff] [blame] | 154 | // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions |
| 155 | // for types as they are needed. Because resolution of types must invalidate |
| 156 | // all of the abstract type descriptions, we keep them in a seperate map to |
| 157 | // make this easy. |
| 158 | TypePrinting ConcreteTypeDescriptions; |
| 159 | TypePrinting AbstractTypeDescriptions; |
| 160 | |
Owen Anderson | 542cffc | 2009-08-04 23:33:01 +0000 | [diff] [blame] | 161 | TypeMap<ArrayValType, ArrayType> ArrayTypes; |
Owen Anderson | d918649 | 2009-08-04 23:47:44 +0000 | [diff] [blame] | 162 | TypeMap<VectorValType, VectorType> VectorTypes; |
Owen Anderson | e565995 | 2009-08-05 00:15:12 +0000 | [diff] [blame] | 163 | TypeMap<PointerValType, PointerType> PointerTypes; |
Owen Anderson | 4b5c761 | 2009-08-05 18:13:27 +0000 | [diff] [blame] | 164 | TypeMap<FunctionValType, FunctionType> FunctionTypes; |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 165 | TypeMap<StructValType, StructType> StructTypes; |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame^] | 166 | TypeMap<UnionValType, UnionType> UnionTypes; |
Owen Anderson | a42ac69 | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 167 | TypeMap<IntegerValType, IntegerType> IntegerTypes; |
Dan Gohman | 97d2cb8 | 2009-08-25 16:00:35 +0000 | [diff] [blame] | 168 | |
Jeffrey Yasskin | 28f2448 | 2009-12-17 19:55:06 +0000 | [diff] [blame] | 169 | // Opaque types are not structurally uniqued, so don't use TypeMap. |
| 170 | typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy; |
| 171 | OpaqueTypesTy OpaqueTypes; |
Jeffrey Yasskin | c660b23 | 2010-02-11 06:41:30 +0000 | [diff] [blame] | 172 | |
| 173 | /// Used as an abstract type that will never be resolved. |
| 174 | OpaqueType *const AlwaysOpaqueTy; |
| 175 | |
Jeffrey Yasskin | 28f2448 | 2009-12-17 19:55:06 +0000 | [diff] [blame] | 176 | |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 177 | /// ValueHandles - This map keeps track of all of the value handles that are |
| 178 | /// watching a Value*. The Value::HasValueHandle bit is used to know |
| 179 | // whether or not a value has an entry in this map. |
| 180 | typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; |
| 181 | ValueHandlesTy ValueHandles; |
| 182 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 183 | /// CustomMDKindNames - Map to hold the metadata string to ID mapping. |
| 184 | StringMap<unsigned> CustomMDKindNames; |
| 185 | |
| 186 | typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; |
| 187 | typedef SmallVector<MDPairTy, 2> MDMapTy; |
| 188 | |
| 189 | /// MetadataStore - Collection of per-instruction metadata used in this |
| 190 | /// context. |
| 191 | DenseMap<const Instruction *, MDMapTy> MetadataStore; |
| 192 | |
| 193 | |
Owen Anderson | a42ac69 | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 194 | LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), |
Dan Gohman | 97d2cb8 | 2009-08-25 16:00:35 +0000 | [diff] [blame] | 195 | VoidTy(C, Type::VoidTyID), |
| 196 | LabelTy(C, Type::LabelTyID), |
| 197 | FloatTy(C, Type::FloatTyID), |
| 198 | DoubleTy(C, Type::DoubleTyID), |
| 199 | MetadataTy(C, Type::MetadataTyID), |
| 200 | X86_FP80Ty(C, Type::X86_FP80TyID), |
| 201 | FP128Ty(C, Type::FP128TyID), |
| 202 | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
| 203 | Int1Ty(C, 1), |
| 204 | Int8Ty(C, 8), |
| 205 | Int16Ty(C, 16), |
| 206 | Int32Ty(C, 32), |
Jeffrey Yasskin | c660b23 | 2010-02-11 06:41:30 +0000 | [diff] [blame] | 207 | Int64Ty(C, 64), |
| 208 | AlwaysOpaqueTy(new OpaqueType(C)) { |
| 209 | // Make sure the AlwaysOpaqueTy stays alive as long as the Context. |
| 210 | AlwaysOpaqueTy->addRef(); |
| 211 | OpaqueTypes.insert(AlwaysOpaqueTy); |
| 212 | } |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 213 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 214 | ~LLVMContextImpl() { |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 215 | ExprConstants.freeConstants(); |
| 216 | ArrayConstants.freeConstants(); |
| 217 | StructConstants.freeConstants(); |
| 218 | VectorConstants.freeConstants(); |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 219 | AggZeroConstants.freeConstants(); |
| 220 | NullPtrConstants.freeConstants(); |
| 221 | UndefValueConstants.freeConstants(); |
Devang Patel | 5ffc385 | 2009-09-10 22:36:12 +0000 | [diff] [blame] | 222 | for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 223 | I != E; ++I) { |
| 224 | if (I->second->use_empty()) |
| 225 | delete I->second; |
| 226 | } |
Devang Patel | 5ffc385 | 2009-09-10 22:36:12 +0000 | [diff] [blame] | 227 | for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 228 | I != E; ++I) { |
| 229 | if (I->second->use_empty()) |
| 230 | delete I->second; |
| 231 | } |
Devang Patel | c07f1f4 | 2010-02-11 20:58:56 +0000 | [diff] [blame] | 232 | MDNodeSet.clear(); |
Jeffrey Yasskin | c660b23 | 2010-02-11 06:41:30 +0000 | [diff] [blame] | 233 | AlwaysOpaqueTy->dropRef(); |
Jeffrey Yasskin | 28f2448 | 2009-12-17 19:55:06 +0000 | [diff] [blame] | 234 | for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end(); |
| 235 | I != E; ++I) { |
| 236 | (*I)->AbstractTypeUsers.clear(); |
| 237 | delete *I; |
| 238 | } |
Torok Edwin | d18e668 | 2009-08-31 16:14:59 +0000 | [diff] [blame] | 239 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | } |
| 243 | |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 244 | #endif |