Move the ConstantInt uniquing table into LLVMContextImpl. This exposed a number...
[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/APInt.h"
20 #include "llvm/ADT/DenseMap.h"
21
22 namespace llvm {
23
24 class ConstantInt;
25 class LLVMContext;
26 class Type;
27
28 struct DenseMapAPIntKeyInfo {
29   struct KeyTy {
30     APInt val;
31     const Type* type;
32     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
33     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
34     bool operator==(const KeyTy& that) const {
35       return type == that.type && this->val == that.val;
36     }
37     bool operator!=(const KeyTy& that) const {
38       return !this->operator==(that);
39     }
40   };
41   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
42   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
43   static unsigned getHashValue(const KeyTy &Key) {
44     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
45       Key.val.getHashValue();
46   }
47   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
48     return LHS == RHS;
49   }
50   static bool isPod() { return false; }
51 };
52
53 class LLVMContextImpl {
54   sys::SmartRWMutex<true> ConstantsLock;
55   
56   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
57                    DenseMapAPIntKeyInfo> IntMapTy;
58   IntMapTy IntConstants;
59   
60   LLVMContext &Context;
61   LLVMContextImpl();
62   LLVMContextImpl(const LLVMContextImpl&);
63 public:
64   LLVMContextImpl(LLVMContext &C) : Context(C) { }
65   
66   /// Return a ConstantInt with the specified value and an implied Type. The
67   /// type is the integer type that corresponds to the bit width of the value.
68   ConstantInt* getConstantInt(const APInt &V);
69 };
70
71 }
72
73 #endif