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/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/System/Mutex.h"
23 #include "llvm/System/RWMutex.h"
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/ADT/StringMap.h"
31 template<class ValType, class TypeClass, class ConstantClass,
32 bool HasLargeKey = false /*true for arrays and structs*/ >
33 class ContextValueMap;
36 template<class ValType>
37 struct ConstantTraits;
47 struct DenseMapAPIntKeyInfo {
51 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
52 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
53 bool operator==(const KeyTy& that) const {
54 return type == that.type && this->val == that.val;
56 bool operator!=(const KeyTy& that) const {
57 return !this->operator==(that);
60 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
61 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
62 static unsigned getHashValue(const KeyTy &Key) {
63 return DenseMapInfo<void*>::getHashValue(Key.type) ^
64 Key.val.getHashValue();
66 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
69 static bool isPod() { return false; }
72 struct DenseMapAPFloatKeyInfo {
75 KeyTy(const APFloat& V) : val(V){}
76 KeyTy(const KeyTy& that) : val(that.val) {}
77 bool operator==(const KeyTy& that) const {
78 return this->val.bitwiseIsEqual(that.val);
80 bool operator!=(const KeyTy& that) const {
81 return !this->operator==(that);
84 static inline KeyTy getEmptyKey() {
85 return KeyTy(APFloat(APFloat::Bogus,1));
87 static inline KeyTy getTombstoneKey() {
88 return KeyTy(APFloat(APFloat::Bogus,2));
90 static unsigned getHashValue(const KeyTy &Key) {
91 return Key.val.getHashValue();
93 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
96 static bool isPod() { return false; }
99 class LLVMContextImpl {
100 sys::SmartRWMutex<true> ConstantsLock;
102 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
103 DenseMapAPIntKeyInfo> IntMapTy;
104 IntMapTy IntConstants;
106 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
107 DenseMapAPFloatKeyInfo> FPMapTy;
110 StringMap<MDString*> MDStringCache;
112 FoldingSet<MDNode> MDNodeSet;
114 ContextValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
116 LLVMContext &Context;
117 ConstantInt *TheTrueVal;
118 ConstantInt *TheFalseVal;
121 LLVMContextImpl(const LLVMContextImpl&);
123 LLVMContextImpl(LLVMContext &C);
126 /// Return a ConstantInt with the specified value and an implied Type. The
127 /// type is the integer type that corresponds to the bit width of the value.
128 ConstantInt *getConstantInt(const APInt &V);
130 ConstantFP *getConstantFP(const APFloat &V);
132 MDString *getMDString(const char *StrBegin, const char *StrEnd);
134 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
136 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
138 ConstantInt *getTrue() {
142 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
145 ConstantInt *getFalse() {
149 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
152 void erase(MDString *M);
153 void erase(MDNode *M);
154 void erase(ConstantAggregateZero *Z);