Factor some of the constants+context related code out into a separate header, to...
[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 "ConstantsContext.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/System/RWMutex.h"
23 #include "llvm/ADT/APFloat.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/StringMap.h"
28 #include <vector>
29
30 namespace llvm {
31
32 class ConstantInt;
33 class ConstantFP;
34 class MDString;
35 class MDNode;
36 struct LLVMContext;
37 class Type;
38 class Value;
39
40 struct DenseMapAPIntKeyInfo {
41   struct KeyTy {
42     APInt val;
43     const Type* type;
44     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
45     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
46     bool operator==(const KeyTy& that) const {
47       return type == that.type && this->val == that.val;
48     }
49     bool operator!=(const KeyTy& that) const {
50       return !this->operator==(that);
51     }
52   };
53   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
54   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
55   static unsigned getHashValue(const KeyTy &Key) {
56     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
57       Key.val.getHashValue();
58   }
59   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
60     return LHS == RHS;
61   }
62   static bool isPod() { return false; }
63 };
64
65 struct DenseMapAPFloatKeyInfo {
66   struct KeyTy {
67     APFloat val;
68     KeyTy(const APFloat& V) : val(V){}
69     KeyTy(const KeyTy& that) : val(that.val) {}
70     bool operator==(const KeyTy& that) const {
71       return this->val.bitwiseIsEqual(that.val);
72     }
73     bool operator!=(const KeyTy& that) const {
74       return !this->operator==(that);
75     }
76   };
77   static inline KeyTy getEmptyKey() { 
78     return KeyTy(APFloat(APFloat::Bogus,1));
79   }
80   static inline KeyTy getTombstoneKey() { 
81     return KeyTy(APFloat(APFloat::Bogus,2)); 
82   }
83   static unsigned getHashValue(const KeyTy &Key) {
84     return Key.val.getHashValue();
85   }
86   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
87     return LHS == RHS;
88   }
89   static bool isPod() { return false; }
90 };
91
92 struct LLVMContextImpl {
93   sys::SmartRWMutex<true> ConstantsLock;
94   
95   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
96                          DenseMapAPIntKeyInfo> IntMapTy;
97   IntMapTy IntConstants;
98   
99   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
100                          DenseMapAPFloatKeyInfo> FPMapTy;
101   FPMapTy FPConstants;
102   
103   StringMap<MDString*> MDStringCache;
104   
105   FoldingSet<MDNode> MDNodeSet;
106   
107   ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
108   
109   typedef ValueMap<std::vector<Constant*>, ArrayType, 
110     ConstantArray, true /*largekey*/> ArrayConstantsTy;
111   ArrayConstantsTy ArrayConstants;
112   
113   typedef ValueMap<std::vector<Constant*>, StructType,
114                    ConstantStruct, true /*largekey*/> StructConstantsTy;
115   StructConstantsTy StructConstants;
116   
117   typedef ValueMap<std::vector<Constant*>, VectorType,
118                    ConstantVector> VectorConstantsTy;
119   VectorConstantsTy VectorConstants;
120   
121   ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
122   
123   ValueMap<char, Type, UndefValue> UndefValueConstants;
124   
125   ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
126   
127   ConstantInt *TheTrueVal;
128   ConstantInt *TheFalseVal;
129   
130   LLVMContextImpl() : TheTrueVal(0), TheFalseVal(0) { }
131 };
132
133 }
134
135 #endif