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