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