Privatize the ConstantStruct table.
[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 "llvm/LLVMContext.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/System/Mutex.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"
29 #include <map>
30 #include <vector>
31
32 template<class ValType, class TypeClass, class ConstantClass,
33          bool HasLargeKey = false  /*true for arrays and structs*/ >
34 class ValueMap;
35
36 namespace llvm {
37 template<class ValType>
38 struct ConstantTraits;
39
40 class ConstantInt;
41 class ConstantFP;
42 class MDString;
43 class MDNode;
44 class LLVMContext;
45 class Type;
46 class Value;
47
48 struct DenseMapAPIntKeyInfo {
49   struct KeyTy {
50     APInt val;
51     const Type* type;
52     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
53     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
54     bool operator==(const KeyTy& that) const {
55       return type == that.type && this->val == that.val;
56     }
57     bool operator!=(const KeyTy& that) const {
58       return !this->operator==(that);
59     }
60   };
61   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
62   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
63   static unsigned getHashValue(const KeyTy &Key) {
64     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
65       Key.val.getHashValue();
66   }
67   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
68     return LHS == RHS;
69   }
70   static bool isPod() { return false; }
71 };
72
73 struct DenseMapAPFloatKeyInfo {
74   struct KeyTy {
75     APFloat val;
76     KeyTy(const APFloat& V) : val(V){}
77     KeyTy(const KeyTy& that) : val(that.val) {}
78     bool operator==(const KeyTy& that) const {
79       return this->val.bitwiseIsEqual(that.val);
80     }
81     bool operator!=(const KeyTy& that) const {
82       return !this->operator==(that);
83     }
84   };
85   static inline KeyTy getEmptyKey() { 
86     return KeyTy(APFloat(APFloat::Bogus,1));
87   }
88   static inline KeyTy getTombstoneKey() { 
89     return KeyTy(APFloat(APFloat::Bogus,2)); 
90   }
91   static unsigned getHashValue(const KeyTy &Key) {
92     return Key.val.getHashValue();
93   }
94   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
95     return LHS == RHS;
96   }
97   static bool isPod() { return false; }
98 };
99
100 class LLVMContextImpl {
101   sys::SmartRWMutex<true> ConstantsLock;
102   
103   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
104                    DenseMapAPIntKeyInfo> IntMapTy;
105   IntMapTy IntConstants;
106   
107   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
108                    DenseMapAPFloatKeyInfo> FPMapTy;
109   FPMapTy FPConstants;
110   
111   StringMap<MDString*> MDStringCache;
112   
113   FoldingSet<MDNode> MDNodeSet;
114   
115   ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
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   LLVMContext &Context;
126   ConstantInt *TheTrueVal;
127   ConstantInt *TheFalseVal;
128   
129   LLVMContextImpl();
130   LLVMContextImpl(const LLVMContextImpl&);
131 public:
132   LLVMContextImpl(LLVMContext &C);
133   ~LLVMContextImpl();
134   
135   /// Return a ConstantInt with the specified value and an implied Type. The
136   /// type is the integer type that corresponds to the bit width of the value.
137   ConstantInt *getConstantInt(const APInt &V);
138   
139   ConstantFP *getConstantFP(const APFloat &V);
140   
141   MDString *getMDString(const char *StrBegin, unsigned StrLength);
142   
143   MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
144   
145   ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
146   
147   Constant *getConstantArray(const ArrayType *Ty,
148                              const std::vector<Constant*> &V);
149   
150   Constant *getConstantStruct(const StructType *Ty, 
151                               const std::vector<Constant*> &V);
152   
153   ConstantInt *getTrue() {
154     if (TheTrueVal)
155       return TheTrueVal;
156     else
157       return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
158   }
159   
160   ConstantInt *getFalse() {
161     if (TheFalseVal)
162       return TheFalseVal;
163     else
164       return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
165   }
166   
167   void erase(MDString *M);
168   void erase(MDNode *M);
169   void erase(ConstantAggregateZero *Z);
170   void erase(ConstantArray *C);
171   void erase(ConstantStruct *S);
172   
173   // RAUW helpers
174   
175   Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
176                                              Value *To, Use *U);
177   Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
178                                         Value *To, Use *U);
179 };
180
181 }
182
183 #endif