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