1 //===----------------- LLVMContextImpl.h - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
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 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/StringMap.h"
35 struct DenseMapAPIntKeyInfo {
39 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
40 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
41 bool operator==(const KeyTy& that) const {
42 return type == that.type && this->val == that.val;
44 bool operator!=(const KeyTy& that) const {
45 return !this->operator==(that);
48 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
49 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
50 static unsigned getHashValue(const KeyTy &Key) {
51 return DenseMapInfo<void*>::getHashValue(Key.type) ^
52 Key.val.getHashValue();
54 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
57 static bool isPod() { return false; }
60 struct DenseMapAPFloatKeyInfo {
63 KeyTy(const APFloat& V) : val(V){}
64 KeyTy(const KeyTy& that) : val(that.val) {}
65 bool operator==(const KeyTy& that) const {
66 return this->val.bitwiseIsEqual(that.val);
68 bool operator!=(const KeyTy& that) const {
69 return !this->operator==(that);
72 static inline KeyTy getEmptyKey() {
73 return KeyTy(APFloat(APFloat::Bogus,1));
75 static inline KeyTy getTombstoneKey() {
76 return KeyTy(APFloat(APFloat::Bogus,2));
78 static unsigned getHashValue(const KeyTy &Key) {
79 return Key.val.getHashValue();
81 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
84 static bool isPod() { return false; }
87 class LLVMContextImpl {
88 sys::SmartRWMutex<true> ConstantsLock;
90 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
91 DenseMapAPIntKeyInfo> IntMapTy;
92 IntMapTy IntConstants;
94 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
95 DenseMapAPFloatKeyInfo> FPMapTy;
98 StringMap<MDString*> MDStringCache;
100 FoldingSet<MDNode> MDNodeSet;
102 LLVMContext &Context;
104 LLVMContextImpl(const LLVMContextImpl&);
106 LLVMContextImpl(LLVMContext &C) : Context(C) { }
108 /// Return a ConstantInt with the specified value and an implied Type. The
109 /// type is the integer type that corresponds to the bit width of the value.
110 ConstantInt *getConstantInt(const APInt &V);
112 ConstantFP *getConstantFP(const APFloat &V);
114 MDString *getMDString(const char *StrBegin, const char *StrEnd);
116 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
118 void erase(MDString *M);
119 void erase(MDNode *M);