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"
32 template<class ValType, class TypeClass, class ConstantClass,
33 bool HasLargeKey = false /*true for arrays and structs*/ >
37 template<class ValType>
38 struct ConstantTraits;
48 struct DenseMapAPIntKeyInfo {
52 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
53 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
54 bool operator==(const KeyTy& that) const {
55 return type == that.type && this->val == that.val;
57 bool operator!=(const KeyTy& that) const {
58 return !this->operator==(that);
61 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
62 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
63 static unsigned getHashValue(const KeyTy &Key) {
64 return DenseMapInfo<void*>::getHashValue(Key.type) ^
65 Key.val.getHashValue();
67 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
70 static bool isPod() { return false; }
73 struct DenseMapAPFloatKeyInfo {
76 KeyTy(const APFloat& V) : val(V){}
77 KeyTy(const KeyTy& that) : val(that.val) {}
78 bool operator==(const KeyTy& that) const {
79 return this->val.bitwiseIsEqual(that.val);
81 bool operator!=(const KeyTy& that) const {
82 return !this->operator==(that);
85 static inline KeyTy getEmptyKey() {
86 return KeyTy(APFloat(APFloat::Bogus,1));
88 static inline KeyTy getTombstoneKey() {
89 return KeyTy(APFloat(APFloat::Bogus,2));
91 static unsigned getHashValue(const KeyTy &Key) {
92 return Key.val.getHashValue();
94 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
97 static bool isPod() { return false; }
100 class LLVMContextImpl {
101 sys::SmartRWMutex<true> ConstantsLock;
103 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
104 DenseMapAPIntKeyInfo> IntMapTy;
105 IntMapTy IntConstants;
107 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
108 DenseMapAPFloatKeyInfo> FPMapTy;
111 StringMap<MDString*> MDStringCache;
113 FoldingSet<MDNode> MDNodeSet;
115 ValueMap<char, Type, ConstantAggregateZero> *AggZeroConstants;
117 typedef ValueMap<std::vector<Constant*>, ArrayType,
118 ConstantArray, true /*largekey*/> ArrayConstantsTy;
119 ArrayConstantsTy *ArrayConstants;
121 typedef ValueMap<std::vector<Constant*>, StructType,
122 ConstantStruct, true /*largekey*/> StructConstantsTy;
123 StructConstantsTy *StructConstants;
125 typedef ValueMap<std::vector<Constant*>, VectorType,
126 ConstantVector> VectorConstantsTy;
127 VectorConstantsTy *VectorConstants;
129 LLVMContext &Context;
130 ConstantInt *TheTrueVal;
131 ConstantInt *TheFalseVal;
134 LLVMContextImpl(const LLVMContextImpl&);
136 LLVMContextImpl(LLVMContext &C);
139 /// Return a ConstantInt with the specified value and an implied Type. The
140 /// type is the integer type that corresponds to the bit width of the value.
141 ConstantInt *getConstantInt(const APInt &V);
143 ConstantFP *getConstantFP(const APFloat &V);
145 MDString *getMDString(const char *StrBegin, unsigned StrLength);
147 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
149 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
151 Constant *getConstantArray(const ArrayType *Ty,
152 const std::vector<Constant*> &V);
154 Constant *getConstantStruct(const StructType *Ty,
155 const std::vector<Constant*> &V);
157 Constant *getConstantVector(const VectorType *Ty,
158 const std::vector<Constant*> &V);
160 ConstantInt *getTrue() {
164 return (TheTrueVal = Context.getConstantInt(IntegerType::get(1), 1));
167 ConstantInt *getFalse() {
171 return (TheFalseVal = Context.getConstantInt(IntegerType::get(1), 0));
174 void erase(MDString *M);
175 void erase(MDNode *M);
176 void erase(ConstantAggregateZero *Z);
177 void erase(ConstantArray *C);
178 void erase(ConstantStruct *S);
179 void erase(ConstantVector *V);
183 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
185 Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,