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*/ >
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 ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
116 typedef ValueMap<std::vector<Constant*>, ArrayType,
117 ConstantArray, true /*largekey*/> ArrayConstantsTy;
118 ArrayConstantsTy *ArrayConstants;
120 LLVMContext &Context;
121 ConstantInt *TheTrueVal;
122 ConstantInt *TheFalseVal;
125 LLVMContextImpl(const LLVMContextImpl&);
127 LLVMContextImpl(LLVMContext &C);
130 /// Return a ConstantInt with the specified value and an implied Type. The
131 /// type is the integer type that corresponds to the bit width of the value.
132 ConstantInt *getConstantInt(const APInt &V);
134 ConstantFP *getConstantFP(const APFloat &V);
136 MDString *getMDString(const char *StrBegin, const char *StrEnd);
138 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
140 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
142 Constant *getConstantArray(const ArrayType *Ty,
143 const std::vector<Constant*> &V);
145 ConstantInt *getTrue() {
149 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
152 ConstantInt *getFalse() {
156 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
159 void erase(MDString *M);
160 void erase(MDNode *M);
161 void erase(ConstantAggregateZero *Z);
162 void erase(ConstantArray *C);
166 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,