1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file declares LLVMContextImpl, the opaque implementation
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
17 
18 #include "ConstantsContext.h"
19 #include "LeaksContext.h"
20 #include "TypesContext.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Metadata.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/Support/ValueHandle.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/FoldingSet.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/StringMap.h"
33 #include <vector>
34 
35 namespace llvm {
36 
37 class ConstantInt;
38 class ConstantFP;
39 class LLVMContext;
40 class Type;
41 class Value;
42 
43 struct DenseMapAPIntKeyInfo {
44   struct KeyTy {
45     APInt val;
46     const Type* type;
KeyTyDenseMapAPIntKeyInfo::KeyTy47     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
KeyTyDenseMapAPIntKeyInfo::KeyTy48     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
49     bool operator==(const KeyTy& that) const {
50       return type == that.type && this->val == that.val;
51     }
52     bool operator!=(const KeyTy& that) const {
53       return !this->operator==(that);
54     }
55   };
getEmptyKeyDenseMapAPIntKeyInfo56   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
getTombstoneKeyDenseMapAPIntKeyInfo57   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
getHashValueDenseMapAPIntKeyInfo58   static unsigned getHashValue(const KeyTy &Key) {
59     return DenseMapInfo<void*>::getHashValue(Key.type) ^
60       Key.val.getHashValue();
61   }
isEqualDenseMapAPIntKeyInfo62   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
63     return LHS == RHS;
64   }
65 };
66 
67 struct DenseMapAPFloatKeyInfo {
68   struct KeyTy {
69     APFloat val;
KeyTyDenseMapAPFloatKeyInfo::KeyTy70     KeyTy(const APFloat& V) : val(V){}
KeyTyDenseMapAPFloatKeyInfo::KeyTy71     KeyTy(const KeyTy& that) : val(that.val) {}
72     bool operator==(const KeyTy& that) const {
73       return this->val.bitwiseIsEqual(that.val);
74     }
75     bool operator!=(const KeyTy& that) const {
76       return !this->operator==(that);
77     }
78   };
getEmptyKeyDenseMapAPFloatKeyInfo79   static inline KeyTy getEmptyKey() {
80     return KeyTy(APFloat(APFloat::Bogus,1));
81   }
getTombstoneKeyDenseMapAPFloatKeyInfo82   static inline KeyTy getTombstoneKey() {
83     return KeyTy(APFloat(APFloat::Bogus,2));
84   }
getHashValueDenseMapAPFloatKeyInfo85   static unsigned getHashValue(const KeyTy &Key) {
86     return Key.val.getHashValue();
87   }
isEqualDenseMapAPFloatKeyInfo88   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
89     return LHS == RHS;
90   }
91 };
92 
93 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
94 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
95 class DebugRecVH : public CallbackVH {
96   /// Ctx - This is the LLVM Context being referenced.
97   LLVMContextImpl *Ctx;
98 
99   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
100   /// this reference lives in.  If this is zero, then it represents a
101   /// non-canonical entry that has no DenseMap value.  This can happen due to
102   /// RAUW.
103   int Idx;
104 public:
DebugRecVH(MDNode * n,LLVMContextImpl * ctx,int idx)105   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
106     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
107 
get()108   MDNode *get() const {
109     return cast_or_null<MDNode>(getValPtr());
110   }
111 
112   virtual void deleted();
113   virtual void allUsesReplacedWith(Value *VNew);
114 };
115 
116 class LLVMContextImpl {
117 public:
118   void *InlineAsmDiagHandler, *InlineAsmDiagContext;
119 
120   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
121                          DenseMapAPIntKeyInfo> IntMapTy;
122   IntMapTy IntConstants;
123 
124   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
125                          DenseMapAPFloatKeyInfo> FPMapTy;
126   FPMapTy FPConstants;
127 
128   StringMap<MDString*> MDStringCache;
129 
130   FoldingSet<MDNode> MDNodeSet;
131   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
132   // aren't in the MDNodeSet, but they're still shared between objects, so no
133   // one object can destroy them.  This set allows us to at least destroy them
134   // on Context destruction.
135   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
136 
137   ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
138 
139   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
140     ConstantArray, true /*largekey*/> ArrayConstantsTy;
141   ArrayConstantsTy ArrayConstants;
142 
143   typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
144     ConstantStruct, true /*largekey*/> StructConstantsTy;
145   StructConstantsTy StructConstants;
146 
147   typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
148                             ConstantVector> VectorConstantsTy;
149   VectorConstantsTy VectorConstants;
150 
151   ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
152   ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
153 
154   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
155   ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
156 
157   ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
158 
159   ConstantInt *TheTrueVal;
160   ConstantInt *TheFalseVal;
161 
162   LeakDetectorImpl<Value> LLVMObjects;
163 
164   // Basic type instances.
165   const Type VoidTy;
166   const Type LabelTy;
167   const Type FloatTy;
168   const Type DoubleTy;
169   const Type MetadataTy;
170   const Type X86_FP80Ty;
171   const Type FP128Ty;
172   const Type PPC_FP128Ty;
173   const IntegerType Int1Ty;
174   const IntegerType Int8Ty;
175   const IntegerType Int16Ty;
176   const IntegerType Int32Ty;
177   const IntegerType Int64Ty;
178 
179   // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
180   // for types as they are needed.  Because resolution of types must invalidate
181   // all of the abstract type descriptions, we keep them in a seperate map to
182   // make this easy.
183   TypePrinting ConcreteTypeDescriptions;
184   TypePrinting AbstractTypeDescriptions;
185 
186   TypeMap<ArrayValType, ArrayType> ArrayTypes;
187   TypeMap<VectorValType, VectorType> VectorTypes;
188   TypeMap<PointerValType, PointerType> PointerTypes;
189   TypeMap<FunctionValType, FunctionType> FunctionTypes;
190   TypeMap<StructValType, StructType> StructTypes;
191   TypeMap<IntegerValType, IntegerType> IntegerTypes;
192 
193   // Opaque types are not structurally uniqued, so don't use TypeMap.
194   typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
195   OpaqueTypesTy OpaqueTypes;
196 
197   /// Used as an abstract type that will never be resolved.
198   OpaqueType *const AlwaysOpaqueTy;
199 
200 
201   /// ValueHandles - This map keeps track of all of the value handles that are
202   /// watching a Value*.  The Value::HasValueHandle bit is used to know
203   // whether or not a value has an entry in this map.
204   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
205   ValueHandlesTy ValueHandles;
206 
207   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
208   StringMap<unsigned> CustomMDKindNames;
209 
210   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
211   typedef SmallVector<MDPairTy, 2> MDMapTy;
212 
213   /// MetadataStore - Collection of per-instruction metadata used in this
214   /// context.
215   DenseMap<const Instruction *, MDMapTy> MetadataStore;
216 
217   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
218   /// entry with no "inlined at" element.
219   DenseMap<MDNode*, int> ScopeRecordIdx;
220 
221   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
222   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
223   /// the MDNode is RAUW'd.
224   std::vector<DebugRecVH> ScopeRecords;
225 
226   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
227   /// scope/inlined-at pair.
228   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
229 
230   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
231   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
232   /// to date.
233   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
234 
235   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
236   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
237 
238   LLVMContextImpl(LLVMContext &C);
239   ~LLVMContextImpl();
240 };
241 
242 }
243 
244 #endif
245