blob: 3a5f7c17a44ef9637682823fbc8f947b09c77209 [file] [log] [blame]
Owen Anderson001dbfe2009-07-16 18:04:31 +00001//===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
Owen Anderson2bc29dc2009-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 Anderson52170072009-06-30 17:06:46 +00009//
10// This file declares LLVMContextImpl, the opaque implementation
11// of LLVMContext.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson2bc29dc2009-06-30 00:48:55 +000014
15#ifndef LLVM_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_H
17
Owen Anderson001dbfe2009-07-16 18:04:31 +000018#include "llvm/System/RWMutex.h"
Owen Anderson914e50c2009-07-16 19:05:41 +000019#include "llvm/ADT/APFloat.h"
Owen Anderson001dbfe2009-07-16 18:04:31 +000020#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/DenseMap.h"
Owen Andersonaad3fb72009-07-16 22:11:26 +000022#include "llvm/ADT/StringMap.h"
Owen Anderson2bc29dc2009-06-30 00:48:55 +000023
Owen Anderson001dbfe2009-07-16 18:04:31 +000024namespace llvm {
25
26class ConstantInt;
Owen Anderson914e50c2009-07-16 19:05:41 +000027class ConstantFP;
Owen Andersonaad3fb72009-07-16 22:11:26 +000028class MDString;
Owen Anderson001dbfe2009-07-16 18:04:31 +000029class LLVMContext;
30class Type;
31
32struct DenseMapAPIntKeyInfo {
33 struct KeyTy {
34 APInt val;
35 const Type* type;
36 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
37 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
38 bool operator==(const KeyTy& that) const {
39 return type == that.type && this->val == that.val;
40 }
41 bool operator!=(const KeyTy& that) const {
42 return !this->operator==(that);
43 }
44 };
45 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
46 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
47 static unsigned getHashValue(const KeyTy &Key) {
48 return DenseMapInfo<void*>::getHashValue(Key.type) ^
49 Key.val.getHashValue();
50 }
51 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
52 return LHS == RHS;
53 }
54 static bool isPod() { return false; }
55};
56
Owen Anderson914e50c2009-07-16 19:05:41 +000057struct DenseMapAPFloatKeyInfo {
58 struct KeyTy {
59 APFloat val;
60 KeyTy(const APFloat& V) : val(V){}
61 KeyTy(const KeyTy& that) : val(that.val) {}
62 bool operator==(const KeyTy& that) const {
63 return this->val.bitwiseIsEqual(that.val);
64 }
65 bool operator!=(const KeyTy& that) const {
66 return !this->operator==(that);
67 }
68 };
69 static inline KeyTy getEmptyKey() {
70 return KeyTy(APFloat(APFloat::Bogus,1));
71 }
72 static inline KeyTy getTombstoneKey() {
73 return KeyTy(APFloat(APFloat::Bogus,2));
74 }
75 static unsigned getHashValue(const KeyTy &Key) {
76 return Key.val.getHashValue();
77 }
78 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
79 return LHS == RHS;
80 }
81 static bool isPod() { return false; }
82};
83
Owen Anderson001dbfe2009-07-16 18:04:31 +000084class LLVMContextImpl {
85 sys::SmartRWMutex<true> ConstantsLock;
86
87 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
88 DenseMapAPIntKeyInfo> IntMapTy;
89 IntMapTy IntConstants;
90
Owen Anderson914e50c2009-07-16 19:05:41 +000091 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
92 DenseMapAPFloatKeyInfo> FPMapTy;
93 FPMapTy FPConstants;
94
Owen Andersonaad3fb72009-07-16 22:11:26 +000095 StringMap<MDString*> MDStringCache;
96
Owen Anderson001dbfe2009-07-16 18:04:31 +000097 LLVMContext &Context;
98 LLVMContextImpl();
99 LLVMContextImpl(const LLVMContextImpl&);
100public:
101 LLVMContextImpl(LLVMContext &C) : Context(C) { }
102
103 /// Return a ConstantInt with the specified value and an implied Type. The
104 /// type is the integer type that corresponds to the bit width of the value.
Owen Anderson914e50c2009-07-16 19:05:41 +0000105 ConstantInt *getConstantInt(const APInt &V);
106
107 ConstantFP *getConstantFP(const APFloat &V);
Owen Andersonaad3fb72009-07-16 22:11:26 +0000108
109 MDString *getMDString(const char *StrBegin, const char *StrEnd);
110
111
112 void erase(MDString *M);
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000113};
114
115}
116
Owen Anderson52170072009-06-30 17:06:46 +0000117#endif