Privatize the first of the value maps.
[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
31 template<class ValType, class TypeClass, class ConstantClass,
32          bool HasLargeKey = false  /*true for arrays and structs*/ >
33 class ContextValueMap;
34
35 namespace llvm {
36 template<class ValType>
37 struct ConstantTraits;
38
39 class ConstantInt;
40 class ConstantFP;
41 class MDString;
42 class MDNode;
43 class LLVMContext;
44 class Type;
45 class Value;
46
47 struct DenseMapAPIntKeyInfo {
48   struct KeyTy {
49     APInt val;
50     const Type* type;
51     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
52     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
53     bool operator==(const KeyTy& that) const {
54       return type == that.type && this->val == that.val;
55     }
56     bool operator!=(const KeyTy& that) const {
57       return !this->operator==(that);
58     }
59   };
60   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
61   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
62   static unsigned getHashValue(const KeyTy &Key) {
63     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
64       Key.val.getHashValue();
65   }
66   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
67     return LHS == RHS;
68   }
69   static bool isPod() { return false; }
70 };
71
72 struct DenseMapAPFloatKeyInfo {
73   struct KeyTy {
74     APFloat val;
75     KeyTy(const APFloat& V) : val(V){}
76     KeyTy(const KeyTy& that) : val(that.val) {}
77     bool operator==(const KeyTy& that) const {
78       return this->val.bitwiseIsEqual(that.val);
79     }
80     bool operator!=(const KeyTy& that) const {
81       return !this->operator==(that);
82     }
83   };
84   static inline KeyTy getEmptyKey() { 
85     return KeyTy(APFloat(APFloat::Bogus,1));
86   }
87   static inline KeyTy getTombstoneKey() { 
88     return KeyTy(APFloat(APFloat::Bogus,2)); 
89   }
90   static unsigned getHashValue(const KeyTy &Key) {
91     return Key.val.getHashValue();
92   }
93   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
94     return LHS == RHS;
95   }
96   static bool isPod() { return false; }
97 };
98
99 class LLVMContextImpl {
100   sys::SmartRWMutex<true> ConstantsLock;
101   
102   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
103                    DenseMapAPIntKeyInfo> IntMapTy;
104   IntMapTy IntConstants;
105   
106   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
107                    DenseMapAPFloatKeyInfo> FPMapTy;
108   FPMapTy FPConstants;
109   
110   StringMap<MDString*> MDStringCache;
111   
112   FoldingSet<MDNode> MDNodeSet;
113   
114   ContextValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
115   
116   LLVMContext &Context;
117   ConstantInt *TheTrueVal;
118   ConstantInt *TheFalseVal;
119   
120   LLVMContextImpl();
121   LLVMContextImpl(const LLVMContextImpl&);
122 public:
123   LLVMContextImpl(LLVMContext &C);
124   ~LLVMContextImpl();
125   
126   /// Return a ConstantInt with the specified value and an implied Type. The
127   /// type is the integer type that corresponds to the bit width of the value.
128   ConstantInt *getConstantInt(const APInt &V);
129   
130   ConstantFP *getConstantFP(const APFloat &V);
131   
132   MDString *getMDString(const char *StrBegin, const char *StrEnd);
133   
134   MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
135   
136   ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
137   
138   ConstantInt *getTrue() {
139     if (TheTrueVal)
140       return TheTrueVal;
141     else
142       return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
143   }
144   
145   ConstantInt *getFalse() {
146     if (TheFalseVal)
147       return TheFalseVal;
148     else
149       return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
150   }
151   
152   void erase(MDString *M);
153   void erase(MDNode *M);
154   void erase(ConstantAggregateZero *Z);
155 };
156
157 }
158
159 #endif