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/RWMutex.h"
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/ADT/StringMap.h"
41 struct DenseMapAPIntKeyInfo
{
45 KeyTy(const APInt
& V
, const Type
* Ty
) : val(V
), type(Ty
) {}
46 KeyTy(const KeyTy
& that
) : val(that
.val
), type(that
.type
) {}
47 bool operator==(const KeyTy
& that
) const {
48 return type
== that
.type
&& this->val
== that
.val
;
50 bool operator!=(const KeyTy
& that
) const {
51 return !this->operator==(that
);
54 static inline KeyTy
getEmptyKey() { return KeyTy(APInt(1,0), 0); }
55 static inline KeyTy
getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
56 static unsigned getHashValue(const KeyTy
&Key
) {
57 return DenseMapInfo
<void*>::getHashValue(Key
.type
) ^
58 Key
.val
.getHashValue();
60 static bool isEqual(const KeyTy
&LHS
, const KeyTy
&RHS
) {
63 static bool isPod() { return false; }
66 struct DenseMapAPFloatKeyInfo
{
69 KeyTy(const APFloat
& V
) : val(V
){}
70 KeyTy(const KeyTy
& that
) : val(that
.val
) {}
71 bool operator==(const KeyTy
& that
) const {
72 return this->val
.bitwiseIsEqual(that
.val
);
74 bool operator!=(const KeyTy
& that
) const {
75 return !this->operator==(that
);
78 static inline KeyTy
getEmptyKey() {
79 return KeyTy(APFloat(APFloat::Bogus
,1));
81 static inline KeyTy
getTombstoneKey() {
82 return KeyTy(APFloat(APFloat::Bogus
,2));
84 static unsigned getHashValue(const KeyTy
&Key
) {
85 return Key
.val
.getHashValue();
87 static bool isEqual(const KeyTy
&LHS
, const KeyTy
&RHS
) {
90 static bool isPod() { return false; }
93 class LLVMContextImpl
{
95 sys::SmartRWMutex
<true> ConstantsLock
;
97 typedef DenseMap
<DenseMapAPIntKeyInfo::KeyTy
, ConstantInt
*,
98 DenseMapAPIntKeyInfo
> IntMapTy
;
99 IntMapTy IntConstants
;
101 typedef DenseMap
<DenseMapAPFloatKeyInfo::KeyTy
, ConstantFP
*,
102 DenseMapAPFloatKeyInfo
> FPMapTy
;
105 StringMap
<MDString
*> MDStringCache
;
107 ValueMap
<char, Type
, ConstantAggregateZero
> AggZeroConstants
;
109 typedef ValueMap
<std::vector
<Value
*>, Type
, MDNode
, true /*largekey*/>
114 typedef ValueMap
<std::vector
<Constant
*>, ArrayType
,
115 ConstantArray
, true /*largekey*/> ArrayConstantsTy
;
116 ArrayConstantsTy ArrayConstants
;
118 typedef ValueMap
<std::vector
<Constant
*>, StructType
,
119 ConstantStruct
, true /*largekey*/> StructConstantsTy
;
120 StructConstantsTy StructConstants
;
122 typedef ValueMap
<std::vector
<Constant
*>, VectorType
,
123 ConstantVector
> VectorConstantsTy
;
124 VectorConstantsTy VectorConstants
;
126 ValueMap
<char, PointerType
, ConstantPointerNull
> NullPtrConstants
;
128 ValueMap
<char, Type
, UndefValue
> UndefValueConstants
;
130 ValueMap
<ExprMapKeyType
, Type
, ConstantExpr
> ExprConstants
;
132 ConstantInt
*TheTrueVal
;
133 ConstantInt
*TheFalseVal
;
135 TypeMap
<ArrayValType
, ArrayType
> ArrayTypes
;
136 TypeMap
<VectorValType
, VectorType
> VectorTypes
;
137 TypeMap
<PointerValType
, PointerType
> PointerTypes
;
138 TypeMap
<FunctionValType
, FunctionType
> FunctionTypes
;
139 TypeMap
<StructValType
, StructType
> StructTypes
;
140 TypeMap
<IntegerValType
, IntegerType
> IntegerTypes
;
145 const Type
*DoubleTy
;
146 const Type
*MetadataTy
;
147 const Type
*X86_FP80Ty
;
149 const Type
*PPC_FP128Ty
;
151 const IntegerType
*Int1Ty
;
152 const IntegerType
*Int8Ty
;
153 const IntegerType
*Int16Ty
;
154 const IntegerType
*Int32Ty
;
155 const IntegerType
*Int64Ty
;
157 LLVMContextImpl(LLVMContext
&C
) : TheTrueVal(0), TheFalseVal(0),
158 VoidTy(new Type(C
, Type::VoidTyID
)),
159 LabelTy(new Type(C
, Type::LabelTyID
)),
160 FloatTy(new Type(C
, Type::FloatTyID
)),
161 DoubleTy(new Type(C
, Type::DoubleTyID
)),
162 MetadataTy(new Type(C
, Type::MetadataTyID
)),
163 X86_FP80Ty(new Type(C
, Type::X86_FP80TyID
)),
164 FP128Ty(new Type(C
, Type::FP128TyID
)),
165 PPC_FP128Ty(new Type(C
, Type::PPC_FP128TyID
)),
166 Int1Ty(new IntegerType(C
, 1)),
167 Int8Ty(new IntegerType(C
, 8)),
168 Int16Ty(new IntegerType(C
, 16)),
169 Int32Ty(new IntegerType(C
, 32)),
170 Int64Ty(new IntegerType(C
, 64)) { }
173 // In principle, we should delete the member types here. However,
174 // this causes destruction order issues with the types in the TypeMaps.
175 // For now, just leak this, which is at least not a regression from the
176 // previous behavior, though still undesirable.