blob: 27bd45133937d7913b93c37f3e4f4501135793bc [file] [log] [blame]
Owen Anderson20b34ac2009-07-16 18:04:31 +00001//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file declares LLVMContextImpl, the opaque implementation
11// of LLVMContext.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#ifndef LLVM_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_H
17
Owen Anderson20b34ac2009-07-16 18:04:31 +000018#include "llvm/System/RWMutex.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000019#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000020#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/DenseMap.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000022
Owen Anderson20b34ac2009-07-16 18:04:31 +000023namespace llvm {
24
25class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000026class ConstantFP;
Owen Anderson20b34ac2009-07-16 18:04:31 +000027class LLVMContext;
28class Type;
29
30struct 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
Owen Andersonc277dc42009-07-16 19:05:41 +000055struct 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
Owen Anderson20b34ac2009-07-16 18:04:31 +000082class LLVMContextImpl {
83 sys::SmartRWMutex<true> ConstantsLock;
84
85 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
86 DenseMapAPIntKeyInfo> IntMapTy;
87 IntMapTy IntConstants;
88
Owen Andersonc277dc42009-07-16 19:05:41 +000089 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
90 DenseMapAPFloatKeyInfo> FPMapTy;
91 FPMapTy FPConstants;
92
Owen Anderson20b34ac2009-07-16 18:04:31 +000093 LLVMContext &Context;
94 LLVMContextImpl();
95 LLVMContextImpl(const LLVMContextImpl&);
96public:
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.
Owen Andersonc277dc42009-07-16 19:05:41 +0000101 ConstantInt *getConstantInt(const APInt &V);
102
103 ConstantFP *getConstantFP(const APFloat &V);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000104};
105
106}
107
Owen Anderson36f62e52009-06-30 17:06:46 +0000108#endif