1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- 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 "AttributeImpl.h"
19 #include "ConstantsContext.h"
20 #include "LeaksContext.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/IR/ValueHandle.h"
44 struct DenseMapAPIntKeyInfo {
48 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
49 bool operator==(const KeyTy& that) const {
50 return type == that.type && this->val == that.val;
52 bool operator!=(const KeyTy& that) const {
53 return !this->operator==(that);
55 friend hash_code hash_value(const KeyTy &Key) {
56 return hash_combine(Key.type, Key.val);
59 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
60 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
61 static unsigned getHashValue(const KeyTy &Key) {
62 return static_cast<unsigned>(hash_value(Key));
64 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
69 struct DenseMapAPFloatKeyInfo {
72 KeyTy(const APFloat& V) : val(V){}
73 bool operator==(const KeyTy& that) const {
74 return this->val.bitwiseIsEqual(that.val);
76 bool operator!=(const KeyTy& that) const {
77 return !this->operator==(that);
79 friend hash_code hash_value(const KeyTy &Key) {
80 return hash_combine(Key.val);
83 static inline KeyTy getEmptyKey() {
84 return KeyTy(APFloat(APFloat::Bogus,1));
86 static inline KeyTy getTombstoneKey() {
87 return KeyTy(APFloat(APFloat::Bogus,2));
89 static unsigned getHashValue(const KeyTy &Key) {
90 return static_cast<unsigned>(hash_value(Key));
92 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
97 struct AnonStructTypeKeyInfo {
99 ArrayRef<Type*> ETypes;
101 KeyTy(const ArrayRef<Type*>& E, bool P) :
102 ETypes(E), isPacked(P) {}
103 KeyTy(const StructType* ST) :
104 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
105 isPacked(ST->isPacked()) {}
106 bool operator==(const KeyTy& that) const {
107 if (isPacked != that.isPacked)
109 if (ETypes != that.ETypes)
113 bool operator!=(const KeyTy& that) const {
114 return !this->operator==(that);
117 static inline StructType* getEmptyKey() {
118 return DenseMapInfo<StructType*>::getEmptyKey();
120 static inline StructType* getTombstoneKey() {
121 return DenseMapInfo<StructType*>::getTombstoneKey();
123 static unsigned getHashValue(const KeyTy& Key) {
124 return hash_combine(hash_combine_range(Key.ETypes.begin(),
128 static unsigned getHashValue(const StructType *ST) {
129 return getHashValue(KeyTy(ST));
131 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
132 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
134 return LHS == KeyTy(RHS);
136 static bool isEqual(const StructType *LHS, const StructType *RHS) {
141 struct FunctionTypeKeyInfo {
143 const Type *ReturnType;
144 ArrayRef<Type*> Params;
146 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
147 ReturnType(R), Params(P), isVarArg(V) {}
148 KeyTy(const FunctionType* FT) :
149 ReturnType(FT->getReturnType()),
150 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
151 isVarArg(FT->isVarArg()) {}
152 bool operator==(const KeyTy& that) const {
153 if (ReturnType != that.ReturnType)
155 if (isVarArg != that.isVarArg)
157 if (Params != that.Params)
161 bool operator!=(const KeyTy& that) const {
162 return !this->operator==(that);
165 static inline FunctionType* getEmptyKey() {
166 return DenseMapInfo<FunctionType*>::getEmptyKey();
168 static inline FunctionType* getTombstoneKey() {
169 return DenseMapInfo<FunctionType*>::getTombstoneKey();
171 static unsigned getHashValue(const KeyTy& Key) {
172 return hash_combine(Key.ReturnType,
173 hash_combine_range(Key.Params.begin(),
177 static unsigned getHashValue(const FunctionType *FT) {
178 return getHashValue(KeyTy(FT));
180 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
181 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
183 return LHS == KeyTy(RHS);
185 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
190 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
191 // shortcut to avoid comparing all operands.
192 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
193 static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
194 unsigned IDHash, FoldingSetNodeID &TempID) {
195 assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
196 // First, check if the cached hashes match. If they don't we can skip the
197 // expensive operand walk.
198 if (X.Hash != IDHash)
201 // If they match we have to compare the operands.
205 static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
206 return X.Hash; // Return cached hash.
210 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
211 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
212 class DebugRecVH : public CallbackVH {
213 /// Ctx - This is the LLVM Context being referenced.
214 LLVMContextImpl *Ctx;
216 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
217 /// this reference lives in. If this is zero, then it represents a
218 /// non-canonical entry that has no DenseMap value. This can happen due to
222 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
223 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
225 MDNode *get() const {
226 return cast_or_null<MDNode>(getValPtr());
229 void deleted() override;
230 void allUsesReplacedWith(Value *VNew) override;
233 class LLVMContextImpl {
235 /// OwnedModules - The set of modules instantiated in this context, and which
236 /// will be automatically deleted if this context is deleted.
237 SmallPtrSet<Module*, 4> OwnedModules;
239 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
240 void *InlineAsmDiagContext;
242 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
243 void *DiagnosticContext;
245 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *,
246 DenseMapAPIntKeyInfo> IntMapTy;
247 IntMapTy IntConstants;
249 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
250 DenseMapAPFloatKeyInfo> FPMapTy;
253 FoldingSet<AttributeImpl> AttrsSet;
254 FoldingSet<AttributeSetImpl> AttrsLists;
255 FoldingSet<AttributeSetNode> AttrsSetNodes;
257 StringMap<Value*> MDStringCache;
259 FoldingSet<MDNode> MDNodeSet;
261 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
262 // aren't in the MDNodeSet, but they're still shared between objects, so no
263 // one object can destroy them. This set allows us to at least destroy them
264 // on Context destruction.
265 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
267 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
269 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
270 ArrayConstantsTy ArrayConstants;
272 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
273 StructConstantsTy StructConstants;
275 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
276 VectorConstantsTy VectorConstants;
278 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
280 DenseMap<Type*, UndefValue*> UVConstants;
282 StringMap<ConstantDataSequential*> CDSConstants;
284 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
286 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
289 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
290 InlineAsm> InlineAsms;
292 ConstantInt *TheTrueVal;
293 ConstantInt *TheFalseVal;
295 LeakDetectorImpl<Value> LLVMObjects;
297 // Basic type instances.
298 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
299 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
300 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
303 /// TypeAllocator - All dynamically allocated types are allocated from this.
304 /// They live forever until the context is torn down.
305 BumpPtrAllocator TypeAllocator;
307 DenseMap<unsigned, IntegerType*> IntegerTypes;
309 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
310 FunctionTypeMap FunctionTypes;
311 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
312 StructTypeMap AnonStructTypes;
313 StringMap<StructType*> NamedStructTypes;
314 unsigned NamedStructTypesUniqueID;
316 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
317 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
318 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
319 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
322 /// ValueHandles - This map keeps track of all of the value handles that are
323 /// watching a Value*. The Value::HasValueHandle bit is used to know
324 /// whether or not a value has an entry in this map.
325 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
326 ValueHandlesTy ValueHandles;
328 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
329 StringMap<unsigned> CustomMDKindNames;
331 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
332 typedef SmallVector<MDPairTy, 2> MDMapTy;
334 /// MetadataStore - Collection of per-instruction metadata used in this
336 DenseMap<const Instruction *, MDMapTy> MetadataStore;
338 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
339 /// entry with no "inlined at" element.
340 DenseMap<MDNode*, int> ScopeRecordIdx;
342 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
343 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
344 /// the MDNode is RAUW'd.
345 std::vector<DebugRecVH> ScopeRecords;
347 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
348 /// scope/inlined-at pair.
349 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
351 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
352 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
354 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
356 /// DiscriminatorTable - This table maps file:line locations to an
357 /// integer representing the next DWARF path discriminator to assign to
358 /// instructions in different blocks at the same location.
359 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
361 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
362 /// requested in this context
363 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
364 IntrinsicIDCacheTy IntrinsicIDCache;
366 /// \brief Mapping from a function to its prefix data, which is stored as the
367 /// operand of an unparented ReturnInst so that the prefix data has a Use.
368 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
369 PrefixDataMapTy PrefixDataMap;
371 /// \brief Return true if the given pass name should emit optimization
373 bool optimizationRemarksEnabledFor(const char *PassName) const;
375 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
376 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
378 LLVMContextImpl(LLVMContext &C);