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