Use standard LLVM-style headers.
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.h
1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class --------------===//
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 "LeaksContext.h"
20 #include "TypesContext.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/System/Mutex.h"
25 #include "llvm/System/RWMutex.h"
26 #include "llvm/Assembly/Writer.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/FoldingSet.h"
31 #include "llvm/ADT/StringMap.h"
32 #include <vector>
33
34 namespace llvm {
35
36 class ConstantInt;
37 class ConstantFP;
38 class MDString;
39 class MDNode;
40 class LLVMContext;
41 class Type;
42 class Value;
43
44 struct DenseMapAPIntKeyInfo {
45   struct KeyTy {
46     APInt val;
47     const Type* type;
48     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
49     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
50     bool operator==(const KeyTy& that) const {
51       return type == that.type && this->val == that.val;
52     }
53     bool operator!=(const KeyTy& that) const {
54       return !this->operator==(that);
55     }
56   };
57   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
58   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
59   static unsigned getHashValue(const KeyTy &Key) {
60     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
61       Key.val.getHashValue();
62   }
63   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
64     return LHS == RHS;
65   }
66   static bool isPod() { return false; }
67 };
68
69 struct DenseMapAPFloatKeyInfo {
70   struct KeyTy {
71     APFloat val;
72     KeyTy(const APFloat& V) : val(V){}
73     KeyTy(const KeyTy& that) : val(that.val) {}
74     bool operator==(const KeyTy& that) const {
75       return this->val.bitwiseIsEqual(that.val);
76     }
77     bool operator!=(const KeyTy& that) const {
78       return !this->operator==(that);
79     }
80   };
81   static inline KeyTy getEmptyKey() { 
82     return KeyTy(APFloat(APFloat::Bogus,1));
83   }
84   static inline KeyTy getTombstoneKey() { 
85     return KeyTy(APFloat(APFloat::Bogus,2)); 
86   }
87   static unsigned getHashValue(const KeyTy &Key) {
88     return Key.val.getHashValue();
89   }
90   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
91     return LHS == RHS;
92   }
93   static bool isPod() { return false; }
94 };
95
96 class LLVMContextImpl {
97 public:
98   sys::SmartRWMutex<true> ConstantsLock;
99   
100   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
101                          DenseMapAPIntKeyInfo> IntMapTy;
102   IntMapTy IntConstants;
103   
104   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
105                          DenseMapAPFloatKeyInfo> FPMapTy;
106   FPMapTy FPConstants;
107   
108   StringMap<MDString*> MDStringCache;
109   
110   ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
111
112   typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/> 
113   MDNodeMapTy;
114
115   MDNodeMapTy MDNodes;
116   
117   typedef ValueMap<std::vector<Constant*>, ArrayType, 
118     ConstantArray, true /*largekey*/> ArrayConstantsTy;
119   ArrayConstantsTy ArrayConstants;
120   
121   typedef ValueMap<std::vector<Constant*>, StructType,
122                    ConstantStruct, true /*largekey*/> StructConstantsTy;
123   StructConstantsTy StructConstants;
124   
125   typedef ValueMap<std::vector<Constant*>, VectorType,
126                    ConstantVector> VectorConstantsTy;
127   VectorConstantsTy VectorConstants;
128   
129   ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
130   
131   ValueMap<char, Type, UndefValue> UndefValueConstants;
132   
133   ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
134   
135   ConstantInt *TheTrueVal;
136   ConstantInt *TheFalseVal;
137   
138   // Lock used for guarding access to the leak detector
139   sys::SmartMutex<true> LLVMObjectsLock;
140   LeakDetectorImpl<Value> LLVMObjects;
141   
142   // Lock used for guarding access to the type maps.
143   sys::SmartMutex<true> TypeMapLock;
144   
145   // Recursive lock used for guarding access to AbstractTypeUsers.
146   // NOTE: The true template parameter means this will no-op when we're not in
147   // multithreaded mode.
148   sys::SmartMutex<true> AbstractTypeUsersLock;
149
150   // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
151   // for types as they are needed.  Because resolution of types must invalidate
152   // all of the abstract type descriptions, we keep them in a seperate map to 
153   // make this easy.
154   TypePrinting ConcreteTypeDescriptions;
155   TypePrinting AbstractTypeDescriptions;
156   
157   TypeMap<ArrayValType, ArrayType> ArrayTypes;
158   TypeMap<VectorValType, VectorType> VectorTypes;
159   TypeMap<PointerValType, PointerType> PointerTypes;
160   TypeMap<FunctionValType, FunctionType> FunctionTypes;
161   TypeMap<StructValType, StructType> StructTypes;
162   TypeMap<IntegerValType, IntegerType> IntegerTypes;
163   
164   const Type *VoidTy;
165   const Type *LabelTy;
166   const Type *FloatTy;
167   const Type *DoubleTy;
168   const Type *MetadataTy;
169   const Type *X86_FP80Ty;
170   const Type *FP128Ty;
171   const Type *PPC_FP128Ty;
172   
173   const IntegerType *Int1Ty;
174   const IntegerType *Int8Ty;
175   const IntegerType *Int16Ty;
176   const IntegerType *Int32Ty;
177   const IntegerType *Int64Ty;
178   
179   /// ValueHandles - This map keeps track of all of the value handles that are
180   /// watching a Value*.  The Value::HasValueHandle bit is used to know
181   // whether or not a value has an entry in this map.
182   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
183   ValueHandlesTy ValueHandles;
184   
185   LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
186     VoidTy(new Type(C, Type::VoidTyID)),
187     LabelTy(new Type(C, Type::LabelTyID)),
188     FloatTy(new Type(C, Type::FloatTyID)),
189     DoubleTy(new Type(C, Type::DoubleTyID)),
190     MetadataTy(new Type(C, Type::MetadataTyID)),
191     X86_FP80Ty(new Type(C, Type::X86_FP80TyID)),
192     FP128Ty(new Type(C, Type::FP128TyID)),
193     PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)),
194     Int1Ty(new IntegerType(C, 1)),
195     Int8Ty(new IntegerType(C, 8)),
196     Int16Ty(new IntegerType(C, 16)),
197     Int32Ty(new IntegerType(C, 32)),
198     Int64Ty(new IntegerType(C, 64)) { }
199   
200   ~LLVMContextImpl() {
201     // In principle, we should delete the member types here.  However,
202     // this causes destruction order issues with the types in the TypeMaps.
203     // For now, just leak this, which is at least not a regression from the
204     // previous behavior, though still undesirable.
205 #if 0
206     delete VoidTy;
207     delete LabelTy;
208     delete FloatTy;
209     delete DoubleTy;
210     delete MetadataTy;
211     delete X86_FP80Ty;
212     delete FP128Ty;
213     delete PPC_FP128Ty;
214     
215     delete Int1Ty;
216     delete Int8Ty;
217     delete Int16Ty;
218     delete Int32Ty;
219     delete Int64Ty;
220 #endif
221   }
222 };
223
224 }
225
226 #endif