1 //===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
18 #include "ConstantsContext.h"
19 #include "TypesContext.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/System/Mutex.h"
24 #include "llvm/System/RWMutex.h"
25 #include "llvm/ADT/APFloat.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/FoldingSet.h"
29 #include "llvm/ADT/StringMap.h"
42 struct DenseMapAPIntKeyInfo {
46 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
47 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
48 bool operator==(const KeyTy& that) const {
49 return type == that.type && this->val == that.val;
51 bool operator!=(const KeyTy& that) const {
52 return !this->operator==(that);
55 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
56 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
57 static unsigned getHashValue(const KeyTy &Key) {
58 return DenseMapInfo<void*>::getHashValue(Key.type) ^
59 Key.val.getHashValue();
61 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
64 static bool isPod() { return false; }
67 struct DenseMapAPFloatKeyInfo {
70 KeyTy(const APFloat& V) : val(V){}
71 KeyTy(const KeyTy& that) : val(that.val) {}
72 bool operator==(const KeyTy& that) const {
73 return this->val.bitwiseIsEqual(that.val);
75 bool operator!=(const KeyTy& that) const {
76 return !this->operator==(that);
79 static inline KeyTy getEmptyKey() {
80 return KeyTy(APFloat(APFloat::Bogus,1));
82 static inline KeyTy getTombstoneKey() {
83 return KeyTy(APFloat(APFloat::Bogus,2));
85 static unsigned getHashValue(const KeyTy &Key) {
86 return Key.val.getHashValue();
88 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
91 static bool isPod() { return false; }
94 class LLVMContextImpl {
96 sys::SmartRWMutex<true> ConstantsLock;
98 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
99 DenseMapAPIntKeyInfo> IntMapTy;
100 IntMapTy IntConstants;
102 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
103 DenseMapAPFloatKeyInfo> FPMapTy;
106 StringMap<MDString*> MDStringCache;
108 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
110 typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/>
115 typedef ValueMap<std::vector<Constant*>, ArrayType,
116 ConstantArray, true /*largekey*/> ArrayConstantsTy;
117 ArrayConstantsTy ArrayConstants;
119 typedef ValueMap<std::vector<Constant*>, StructType,
120 ConstantStruct, true /*largekey*/> StructConstantsTy;
121 StructConstantsTy StructConstants;
123 typedef ValueMap<std::vector<Constant*>, VectorType,
124 ConstantVector> VectorConstantsTy;
125 VectorConstantsTy VectorConstants;
127 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
129 ValueMap<char, Type, UndefValue> UndefValueConstants;
131 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
133 ConstantInt *TheTrueVal;
134 ConstantInt *TheFalseVal;
136 // Lock used for guarding access to the type maps.
137 sys::SmartMutex<true> TypeMapLock;
139 TypeMap<ArrayValType, ArrayType> ArrayTypes;
140 TypeMap<VectorValType, VectorType> VectorTypes;
141 TypeMap<PointerValType, PointerType> PointerTypes;
142 TypeMap<FunctionValType, FunctionType> FunctionTypes;
143 TypeMap<StructValType, StructType> StructTypes;
144 TypeMap<IntegerValType, IntegerType> IntegerTypes;
149 const Type *DoubleTy;
150 const Type *MetadataTy;
151 const Type *X86_FP80Ty;
153 const Type *PPC_FP128Ty;
155 const IntegerType *Int1Ty;
156 const IntegerType *Int8Ty;
157 const IntegerType *Int16Ty;
158 const IntegerType *Int32Ty;
159 const IntegerType *Int64Ty;
161 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
162 VoidTy(new Type(C, Type::VoidTyID)),
163 LabelTy(new Type(C, Type::LabelTyID)),
164 FloatTy(new Type(C, Type::FloatTyID)),
165 DoubleTy(new Type(C, Type::DoubleTyID)),
166 MetadataTy(new Type(C, Type::MetadataTyID)),
167 X86_FP80Ty(new Type(C, Type::X86_FP80TyID)),
168 FP128Ty(new Type(C, Type::FP128TyID)),
169 PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)),
170 Int1Ty(new IntegerType(C, 1)),
171 Int8Ty(new IntegerType(C, 8)),
172 Int16Ty(new IntegerType(C, 16)),
173 Int32Ty(new IntegerType(C, 32)),
174 Int64Ty(new IntegerType(C, 64)) { }
177 // In principle, we should delete the member types here. However,
178 // this causes destruction order issues with the types in the TypeMaps.
179 // For now, just leak this, which is at least not a regression from the
180 // previous behavior, though still undesirable.