265201434d866d0a8adcd0a91142e30cfe4b3587
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.h
1 //===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
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 //===----------------------------------------------------------------------===//
9 //
10 //  This file declares LLVMContextImpl, the opaque implementation 
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
17
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"
30 #include <vector>
31
32 namespace llvm {
33
34 class ConstantInt;
35 class ConstantFP;
36 class MDString;
37 class MDNode;
38 class LLVMContext;
39 class Type;
40 class Value;
41
42 struct DenseMapAPIntKeyInfo {
43   struct KeyTy {
44     APInt val;
45     const Type* type;
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;
50     }
51     bool operator!=(const KeyTy& that) const {
52       return !this->operator==(that);
53     }
54   };
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();
60   }
61   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
62     return LHS == RHS;
63   }
64   static bool isPod() { return false; }
65 };
66
67 struct DenseMapAPFloatKeyInfo {
68   struct KeyTy {
69     APFloat val;
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);
74     }
75     bool operator!=(const KeyTy& that) const {
76       return !this->operator==(that);
77     }
78   };
79   static inline KeyTy getEmptyKey() { 
80     return KeyTy(APFloat(APFloat::Bogus,1));
81   }
82   static inline KeyTy getTombstoneKey() { 
83     return KeyTy(APFloat(APFloat::Bogus,2)); 
84   }
85   static unsigned getHashValue(const KeyTy &Key) {
86     return Key.val.getHashValue();
87   }
88   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
89     return LHS == RHS;
90   }
91   static bool isPod() { return false; }
92 };
93
94 class LLVMContextImpl {
95 public:
96   sys::SmartRWMutex<true> ConstantsLock;
97   
98   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
99                          DenseMapAPIntKeyInfo> IntMapTy;
100   IntMapTy IntConstants;
101   
102   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
103                          DenseMapAPFloatKeyInfo> FPMapTy;
104   FPMapTy FPConstants;
105   
106   StringMap<MDString*> MDStringCache;
107   
108   ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
109
110   typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/> 
111   MDNodeMapTy;
112
113   MDNodeMapTy MDNodes;
114   
115   typedef ValueMap<std::vector<Constant*>, ArrayType, 
116     ConstantArray, true /*largekey*/> ArrayConstantsTy;
117   ArrayConstantsTy ArrayConstants;
118   
119   typedef ValueMap<std::vector<Constant*>, StructType,
120                    ConstantStruct, true /*largekey*/> StructConstantsTy;
121   StructConstantsTy StructConstants;
122   
123   typedef ValueMap<std::vector<Constant*>, VectorType,
124                    ConstantVector> VectorConstantsTy;
125   VectorConstantsTy VectorConstants;
126   
127   ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
128   
129   ValueMap<char, Type, UndefValue> UndefValueConstants;
130   
131   ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
132   
133   ConstantInt *TheTrueVal;
134   ConstantInt *TheFalseVal;
135   
136   // Lock used for guarding access to the type maps.
137   sys::SmartMutex<true> TypeMapLock;
138   
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;
145   
146   const Type *VoidTy;
147   const Type *LabelTy;
148   const Type *FloatTy;
149   const Type *DoubleTy;
150   const Type *MetadataTy;
151   const Type *X86_FP80Ty;
152   const Type *FP128Ty;
153   const Type *PPC_FP128Ty;
154   
155   const IntegerType *Int1Ty;
156   const IntegerType *Int8Ty;
157   const IntegerType *Int16Ty;
158   const IntegerType *Int32Ty;
159   const IntegerType *Int64Ty;
160   
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)) { }
175   
176   ~LLVMContextImpl() {
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.
181 #if 0
182     delete VoidTy;
183     delete LabelTy;
184     delete FloatTy;
185     delete DoubleTy;
186     delete MetadataTy;
187     delete X86_FP80Ty;
188     delete FP128Ty;
189     delete PPC_FP128Ty;
190     
191     delete Int1Ty;
192     delete Int8Ty;
193     delete Int16Ty;
194     delete Int32Ty;
195     delete Int64Ty;
196 #endif
197   }
198 };
199
200 }
201
202 #endif