Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 1 | //===----------------- LLVMContextImpl.h - Implementation ------*- 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 | |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 18 | #include "ConstantsContext.h" |
Owen Anderson | 542cffc | 2009-08-04 23:33:01 +0000 | [diff] [blame] | 19 | #include "TypesContext.h" |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 20 | #include "llvm/LLVMContext.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" |
Owen Anderson | 286b3af | 2009-08-17 17:34:27 +0000 | [diff] [blame] | 23 | #include "llvm/System/Mutex.h" |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 24 | #include "llvm/System/RWMutex.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" |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringMap.h" |
Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 31 | #include <vector> |
Owen Anderson | 39ede7b | 2009-07-21 20:13:12 +0000 | [diff] [blame] | 32 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 33 | namespace llvm { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 34 | |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 35 | class ConstantInt; |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 36 | class ConstantFP; |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 37 | class MDString; |
Owen Anderson | 4118dde | 2009-07-16 23:44:30 +0000 | [diff] [blame] | 38 | class MDNode; |
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; |
| 46 | const Type* type; |
| 47 | KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} |
| 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 | } |
| 65 | static bool isPod() { return false; } |
| 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 | } |
| 92 | static bool isPod() { return false; } |
| 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 | sys::SmartRWMutex<true> ConstantsLock; |
| 98 | |
| 99 | typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 100 | DenseMapAPIntKeyInfo> IntMapTy; |
Owen Anderson | 20b34ac | 2009-07-16 18:04:31 +0000 | [diff] [blame] | 101 | IntMapTy IntConstants; |
| 102 | |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 103 | typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 104 | DenseMapAPFloatKeyInfo> FPMapTy; |
Owen Anderson | c277dc4 | 2009-07-16 19:05:41 +0000 | [diff] [blame] | 105 | FPMapTy FPConstants; |
| 106 | |
Owen Anderson | 69ab416 | 2009-07-16 22:11:26 +0000 | [diff] [blame] | 107 | StringMap<MDString*> MDStringCache; |
| 108 | |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 109 | ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants; |
Owen Anderson | 13234f8 | 2009-08-10 18:16:08 +0000 | [diff] [blame] | 110 | |
Devang Patel | c5aa8c6 | 2009-08-11 06:31:57 +0000 | [diff] [blame] | 111 | typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/> |
| 112 | MDNodeMapTy; |
| 113 | |
| 114 | MDNodeMapTy MDNodes; |
Owen Anderson | 3d34492 | 2009-07-21 20:55:28 +0000 | [diff] [blame] | 115 | |
| 116 | typedef ValueMap<std::vector<Constant*>, ArrayType, |
| 117 | ConstantArray, true /*largekey*/> ArrayConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 118 | ArrayConstantsTy ArrayConstants; |
Owen Anderson | 39ede7b | 2009-07-21 20:13:12 +0000 | [diff] [blame] | 119 | |
Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 120 | typedef ValueMap<std::vector<Constant*>, StructType, |
| 121 | ConstantStruct, true /*largekey*/> StructConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 122 | StructConstantsTy StructConstants; |
Owen Anderson | 909f600 | 2009-07-23 23:25:33 +0000 | [diff] [blame] | 123 | |
Owen Anderson | 0348a13 | 2009-07-24 00:36:24 +0000 | [diff] [blame] | 124 | typedef ValueMap<std::vector<Constant*>, VectorType, |
| 125 | ConstantVector> VectorConstantsTy; |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 126 | VectorConstantsTy VectorConstants; |
Owen Anderson | 0348a13 | 2009-07-24 00:36:24 +0000 | [diff] [blame] | 127 | |
Owen Anderson | c8c3026 | 2009-07-31 22:45:43 +0000 | [diff] [blame] | 128 | ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; |
| 129 | |
| 130 | ValueMap<char, Type, UndefValue> UndefValueConstants; |
| 131 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 132 | ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; |
| 133 | |
Owen Anderson | 2ad5217 | 2009-07-21 02:47:59 +0000 | [diff] [blame] | 134 | ConstantInt *TheTrueVal; |
| 135 | ConstantInt *TheFalseVal; |
| 136 | |
Owen Anderson | 286b3af | 2009-08-17 17:34:27 +0000 | [diff] [blame] | 137 | // Lock used for guarding access to the type maps. |
| 138 | sys::SmartMutex<true> TypeMapLock; |
| 139 | |
Owen Anderson | 9801560 | 2009-08-17 17:59:35 +0000 | [diff] [blame^] | 140 | // Recursive lock used for guarding access to AbstractTypeUsers. |
| 141 | // NOTE: The true template parameter means this will no-op when we're not in |
| 142 | // multithreaded mode. |
| 143 | sys::SmartMutex<true> AbstractTypeUsersLock; |
| 144 | |
| 145 | // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions |
| 146 | // for types as they are needed. Because resolution of types must invalidate |
| 147 | // all of the abstract type descriptions, we keep them in a seperate map to |
| 148 | // make this easy. |
| 149 | TypePrinting ConcreteTypeDescriptions; |
| 150 | TypePrinting AbstractTypeDescriptions; |
| 151 | |
Owen Anderson | 542cffc | 2009-08-04 23:33:01 +0000 | [diff] [blame] | 152 | TypeMap<ArrayValType, ArrayType> ArrayTypes; |
Owen Anderson | d918649 | 2009-08-04 23:47:44 +0000 | [diff] [blame] | 153 | TypeMap<VectorValType, VectorType> VectorTypes; |
Owen Anderson | e565995 | 2009-08-05 00:15:12 +0000 | [diff] [blame] | 154 | TypeMap<PointerValType, PointerType> PointerTypes; |
Owen Anderson | 4b5c761 | 2009-08-05 18:13:27 +0000 | [diff] [blame] | 155 | TypeMap<FunctionValType, FunctionType> FunctionTypes; |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 156 | TypeMap<StructValType, StructType> StructTypes; |
Owen Anderson | a42ac69 | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 157 | TypeMap<IntegerValType, IntegerType> IntegerTypes; |
Owen Anderson | 542cffc | 2009-08-04 23:33:01 +0000 | [diff] [blame] | 158 | |
Owen Anderson | a42ac69 | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 159 | const Type *VoidTy; |
| 160 | const Type *LabelTy; |
| 161 | const Type *FloatTy; |
| 162 | const Type *DoubleTy; |
| 163 | const Type *MetadataTy; |
| 164 | const Type *X86_FP80Ty; |
| 165 | const Type *FP128Ty; |
| 166 | const Type *PPC_FP128Ty; |
| 167 | |
| 168 | const IntegerType *Int1Ty; |
| 169 | const IntegerType *Int8Ty; |
| 170 | const IntegerType *Int16Ty; |
| 171 | const IntegerType *Int32Ty; |
| 172 | const IntegerType *Int64Ty; |
| 173 | |
| 174 | LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), |
| 175 | VoidTy(new Type(C, Type::VoidTyID)), |
| 176 | LabelTy(new Type(C, Type::LabelTyID)), |
| 177 | FloatTy(new Type(C, Type::FloatTyID)), |
| 178 | DoubleTy(new Type(C, Type::DoubleTyID)), |
| 179 | MetadataTy(new Type(C, Type::MetadataTyID)), |
| 180 | X86_FP80Ty(new Type(C, Type::X86_FP80TyID)), |
| 181 | FP128Ty(new Type(C, Type::FP128TyID)), |
| 182 | PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)), |
| 183 | Int1Ty(new IntegerType(C, 1)), |
| 184 | Int8Ty(new IntegerType(C, 8)), |
| 185 | Int16Ty(new IntegerType(C, 16)), |
| 186 | Int32Ty(new IntegerType(C, 32)), |
| 187 | Int64Ty(new IntegerType(C, 64)) { } |
| 188 | |
| 189 | ~LLVMContextImpl() { |
| 190 | // In principle, we should delete the member types here. However, |
| 191 | // this causes destruction order issues with the types in the TypeMaps. |
| 192 | // For now, just leak this, which is at least not a regression from the |
| 193 | // previous behavior, though still undesirable. |
| 194 | #if 0 |
| 195 | delete VoidTy; |
| 196 | delete LabelTy; |
| 197 | delete FloatTy; |
| 198 | delete DoubleTy; |
| 199 | delete MetadataTy; |
| 200 | delete X86_FP80Ty; |
| 201 | delete FP128Ty; |
| 202 | delete PPC_FP128Ty; |
| 203 | |
| 204 | delete Int1Ty; |
| 205 | delete Int8Ty; |
| 206 | delete Int16Ty; |
| 207 | delete Int32Ty; |
| 208 | delete Int64Ty; |
| 209 | #endif |
| 210 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } |
| 214 | |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 215 | #endif |