LLVM API Documentation
00001 //===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares LLVMContextImpl, the opaque implementation 00011 // of LLVMContext. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_LLVMCONTEXT_IMPL_H 00016 #define LLVM_LLVMCONTEXT_IMPL_H 00017 00018 #include "ConstantsContext.h" 00019 #include "LeaksContext.h" 00020 #include "TypesContext.h" 00021 #include "llvm/LLVMContext.h" 00022 #include "llvm/Constants.h" 00023 #include "llvm/DerivedTypes.h" 00024 #include "llvm/System/Mutex.h" 00025 #include "llvm/System/RWMutex.h" 00026 #include "llvm/Assembly/Writer.h" 00027 #include "llvm/ADT/APFloat.h" 00028 #include "llvm/ADT/APInt.h" 00029 #include "llvm/ADT/DenseMap.h" 00030 #include "llvm/ADT/FoldingSet.h" 00031 #include "llvm/ADT/StringMap.h" 00032 #include <vector> 00033 00034 namespace llvm { 00035 00036 class ConstantInt; 00037 class ConstantFP; 00038 class MDString; 00039 class MDNode; 00040 class LLVMContext; 00041 class Type; 00042 class Value; 00043 00044 struct DenseMapAPIntKeyInfo { 00045 struct KeyTy { 00046 APInt val; 00047 const Type* type; 00048 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {} 00049 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {} 00050 bool operator==(const KeyTy& that) const { 00051 return type == that.type && this->val == that.val; 00052 } 00053 bool operator!=(const KeyTy& that) const { 00054 return !this->operator==(that); 00055 } 00056 }; 00057 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } 00058 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } 00059 static unsigned getHashValue(const KeyTy &Key) { 00060 return DenseMapInfo<void*>::getHashValue(Key.type) ^ 00061 Key.val.getHashValue(); 00062 } 00063 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00064 return LHS == RHS; 00065 } 00066 static bool isPod() { return false; } 00067 }; 00068 00069 struct DenseMapAPFloatKeyInfo { 00070 struct KeyTy { 00071 APFloat val; 00072 KeyTy(const APFloat& V) : val(V){} 00073 KeyTy(const KeyTy& that) : val(that.val) {} 00074 bool operator==(const KeyTy& that) const { 00075 return this->val.bitwiseIsEqual(that.val); 00076 } 00077 bool operator!=(const KeyTy& that) const { 00078 return !this->operator==(that); 00079 } 00080 }; 00081 static inline KeyTy getEmptyKey() { 00082 return KeyTy(APFloat(APFloat::Bogus,1)); 00083 } 00084 static inline KeyTy getTombstoneKey() { 00085 return KeyTy(APFloat(APFloat::Bogus,2)); 00086 } 00087 static unsigned getHashValue(const KeyTy &Key) { 00088 return Key.val.getHashValue(); 00089 } 00090 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00091 return LHS == RHS; 00092 } 00093 static bool isPod() { return false; } 00094 }; 00095 00096 class LLVMContextImpl { 00097 public: 00098 sys::SmartRWMutex<true> ConstantsLock; 00099 00100 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 00101 DenseMapAPIntKeyInfo> IntMapTy; 00102 IntMapTy IntConstants; 00103 00104 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 00105 DenseMapAPFloatKeyInfo> FPMapTy; 00106 FPMapTy FPConstants; 00107 00108 StringMap<MDString*> MDStringCache; 00109 00110 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants; 00111 00112 typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/> 00113 MDNodeMapTy; 00114 00115 MDNodeMapTy MDNodes; 00116 00117 typedef ValueMap<std::vector<Constant*>, ArrayType, 00118 ConstantArray, true /*largekey*/> ArrayConstantsTy; 00119 ArrayConstantsTy ArrayConstants; 00120 00121 typedef ValueMap<std::vector<Constant*>, StructType, 00122 ConstantStruct, true /*largekey*/> StructConstantsTy; 00123 StructConstantsTy StructConstants; 00124 00125 typedef ValueMap<std::vector<Constant*>, VectorType, 00126 ConstantVector> VectorConstantsTy; 00127 VectorConstantsTy VectorConstants; 00128 00129 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants; 00130 00131 ValueMap<char, Type, UndefValue> UndefValueConstants; 00132 00133 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants; 00134 00135 ConstantInt *TheTrueVal; 00136 ConstantInt *TheFalseVal; 00137 00138 // Lock used for guarding access to the leak detector 00139 sys::SmartMutex<true> LLVMObjectsLock; 00140 LeakDetectorImpl<Value> LLVMObjects; 00141 00142 // Lock used for guarding access to the type maps. 00143 sys::SmartMutex<true> TypeMapLock; 00144 00145 // Recursive lock used for guarding access to AbstractTypeUsers. 00146 // NOTE: The true template parameter means this will no-op when we're not in 00147 // multithreaded mode. 00148 sys::SmartMutex<true> AbstractTypeUsersLock; 00149 00150 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions 00151 // for types as they are needed. Because resolution of types must invalidate 00152 // all of the abstract type descriptions, we keep them in a seperate map to 00153 // make this easy. 00154 TypePrinting ConcreteTypeDescriptions; 00155 TypePrinting AbstractTypeDescriptions; 00156 00157 TypeMap<ArrayValType, ArrayType> ArrayTypes; 00158 TypeMap<VectorValType, VectorType> VectorTypes; 00159 TypeMap<PointerValType, PointerType> PointerTypes; 00160 TypeMap<FunctionValType, FunctionType> FunctionTypes; 00161 TypeMap<StructValType, StructType> StructTypes; 00162 TypeMap<IntegerValType, IntegerType> IntegerTypes; 00163 00164 const Type *VoidTy; 00165 const Type *LabelTy; 00166 const Type *FloatTy; 00167 const Type *DoubleTy; 00168 const Type *MetadataTy; 00169 const Type *X86_FP80Ty; 00170 const Type *FP128Ty; 00171 const Type *PPC_FP128Ty; 00172 00173 const IntegerType *Int1Ty; 00174 const IntegerType *Int8Ty; 00175 const IntegerType *Int16Ty; 00176 const IntegerType *Int32Ty; 00177 const IntegerType *Int64Ty; 00178 00179 /// ValueHandles - This map keeps track of all of the value handles that are 00180 /// watching a Value*. The Value::HasValueHandle bit is used to know 00181 // whether or not a value has an entry in this map. 00182 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; 00183 ValueHandlesTy ValueHandles; 00184 00185 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), 00186 VoidTy(new Type(C, Type::VoidTyID)), 00187 LabelTy(new Type(C, Type::LabelTyID)), 00188 FloatTy(new Type(C, Type::FloatTyID)), 00189 DoubleTy(new Type(C, Type::DoubleTyID)), 00190 MetadataTy(new Type(C, Type::MetadataTyID)), 00191 X86_FP80Ty(new Type(C, Type::X86_FP80TyID)), 00192 FP128Ty(new Type(C, Type::FP128TyID)), 00193 PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)), 00194 Int1Ty(new IntegerType(C, 1)), 00195 Int8Ty(new IntegerType(C, 8)), 00196 Int16Ty(new IntegerType(C, 16)), 00197 Int32Ty(new IntegerType(C, 32)), 00198 Int64Ty(new IntegerType(C, 64)) { } 00199 00200 ~LLVMContextImpl() { 00201 // In principle, we should delete the member types here. However, 00202 // this causes destruction order issues with the types in the TypeMaps. 00203 // For now, just leak this, which is at least not a regression from the 00204 // previous behavior, though still undesirable. 00205 #if 0 00206 delete VoidTy; 00207 delete LabelTy; 00208 delete FloatTy; 00209 delete DoubleTy; 00210 delete MetadataTy; 00211 delete X86_FP80Ty; 00212 delete FP128Ty; 00213 delete PPC_FP128Ty; 00214 00215 delete Int1Ty; 00216 delete Int8Ty; 00217 delete Int16Ty; 00218 delete Int32Ty; 00219 delete Int64Ty; 00220 #endif 00221 } 00222 }; 00223 00224 } 00225 00226 #endif