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