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/LLVMContext.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/System/RWMutex.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/StringMap.h"
37 struct DenseMapAPIntKeyInfo {
41 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
42 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
43 bool operator==(const KeyTy& that) const {
44 return type == that.type && this->val == that.val;
46 bool operator!=(const KeyTy& that) const {
47 return !this->operator==(that);
50 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
51 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
52 static unsigned getHashValue(const KeyTy &Key) {
53 return DenseMapInfo<void*>::getHashValue(Key.type) ^
54 Key.val.getHashValue();
56 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
59 static bool isPod() { return false; }
62 struct DenseMapAPFloatKeyInfo {
65 KeyTy(const APFloat& V) : val(V){}
66 KeyTy(const KeyTy& that) : val(that.val) {}
67 bool operator==(const KeyTy& that) const {
68 return this->val.bitwiseIsEqual(that.val);
70 bool operator!=(const KeyTy& that) const {
71 return !this->operator==(that);
74 static inline KeyTy getEmptyKey() {
75 return KeyTy(APFloat(APFloat::Bogus,1));
77 static inline KeyTy getTombstoneKey() {
78 return KeyTy(APFloat(APFloat::Bogus,2));
80 static unsigned getHashValue(const KeyTy &Key) {
81 return Key.val.getHashValue();
83 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
86 static bool isPod() { return false; }
89 class LLVMContextImpl {
90 sys::SmartRWMutex<true> ConstantsLock;
92 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
93 DenseMapAPIntKeyInfo> IntMapTy;
94 IntMapTy IntConstants;
96 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
97 DenseMapAPFloatKeyInfo> FPMapTy;
100 StringMap<MDString*> MDStringCache;
102 FoldingSet<MDNode> MDNodeSet;
104 LLVMContext &Context;
105 ConstantInt *TheTrueVal;
106 ConstantInt *TheFalseVal;
109 LLVMContextImpl(const LLVMContextImpl&);
111 LLVMContextImpl(LLVMContext &C) : Context(C), TheTrueVal(0), TheFalseVal(0) {}
113 /// Return a ConstantInt with the specified value and an implied Type. The
114 /// type is the integer type that corresponds to the bit width of the value.
115 ConstantInt *getConstantInt(const APInt &V);
117 ConstantFP *getConstantFP(const APFloat &V);
119 MDString *getMDString(const char *StrBegin, const char *StrEnd);
121 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
123 ConstantInt *getConstantIntTrue() {
127 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
130 ConstantInt *getConstantIntFalse() {
134 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
137 void erase(MDString *M);
138 void erase(MDNode *M);