Privatize the MDString uniquing 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/System/RWMutex.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/StringMap.h"
23
24 namespace llvm {
25
26 class ConstantInt;
27 class ConstantFP;
28 class MDString;
29 class LLVMContext;
30 class Type;
31
32 struct DenseMapAPIntKeyInfo {
33   struct KeyTy {
34     APInt val;
35     const Type* type;
36     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
37     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
38     bool operator==(const KeyTy& that) const {
39       return type == that.type && this->val == that.val;
40     }
41     bool operator!=(const KeyTy& that) const {
42       return !this->operator==(that);
43     }
44   };
45   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
46   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
47   static unsigned getHashValue(const KeyTy &Key) {
48     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
49       Key.val.getHashValue();
50   }
51   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
52     return LHS == RHS;
53   }
54   static bool isPod() { return false; }
55 };
56
57 struct DenseMapAPFloatKeyInfo {
58   struct KeyTy {
59     APFloat val;
60     KeyTy(const APFloat& V) : val(V){}
61     KeyTy(const KeyTy& that) : val(that.val) {}
62     bool operator==(const KeyTy& that) const {
63       return this->val.bitwiseIsEqual(that.val);
64     }
65     bool operator!=(const KeyTy& that) const {
66       return !this->operator==(that);
67     }
68   };
69   static inline KeyTy getEmptyKey() { 
70     return KeyTy(APFloat(APFloat::Bogus,1));
71   }
72   static inline KeyTy getTombstoneKey() { 
73     return KeyTy(APFloat(APFloat::Bogus,2)); 
74   }
75   static unsigned getHashValue(const KeyTy &Key) {
76     return Key.val.getHashValue();
77   }
78   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
79     return LHS == RHS;
80   }
81   static bool isPod() { return false; }
82 };
83
84 class LLVMContextImpl {
85   sys::SmartRWMutex<true> ConstantsLock;
86   
87   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
88                    DenseMapAPIntKeyInfo> IntMapTy;
89   IntMapTy IntConstants;
90   
91   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
92                    DenseMapAPFloatKeyInfo> FPMapTy;
93   FPMapTy FPConstants;
94   
95   StringMap<MDString*> MDStringCache;
96   
97   LLVMContext &Context;
98   LLVMContextImpl();
99   LLVMContextImpl(const LLVMContextImpl&);
100 public:
101   LLVMContextImpl(LLVMContext &C) : Context(C) { }
102   
103   /// Return a ConstantInt with the specified value and an implied Type. The
104   /// type is the integer type that corresponds to the bit width of the value.
105   ConstantInt *getConstantInt(const APInt &V);
106   
107   ConstantFP *getConstantFP(const APFloat &V);
108   
109   MDString *getMDString(const char *StrBegin, const char *StrEnd);
110   
111   
112   void erase(MDString *M);
113 };
114
115 }
116
117 #endif