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_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_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/DenseSet.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/Hashing.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/ValueHandle.h"
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
48 struct DenseMapAPIntKeyInfo {
49 static inline APInt getEmptyKey() { return APInt(nullptr, 0); }
50 static inline APInt getTombstoneKey() {
51 return APInt(reinterpret_cast<uint64_t *>(sizeof(uint64_t)), 0);
53 static unsigned getHashValue(const APInt &Key) {
54 return static_cast<unsigned>(hash_value(Key));
56 static bool isEqual(const APInt &LHS, const APInt &RHS) {
57 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
61 struct DenseMapAPFloatKeyInfo {
62 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
63 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
64 static unsigned getHashValue(const APFloat &Key) {
65 return static_cast<unsigned>(hash_value(Key));
67 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
68 return LHS.bitwiseIsEqual(RHS);
72 struct AnonStructTypeKeyInfo {
74 ArrayRef<Type*> ETypes;
76 KeyTy(const ArrayRef<Type*>& E, bool P) :
77 ETypes(E), isPacked(P) {}
78 KeyTy(const StructType *ST)
79 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
80 bool operator==(const KeyTy& that) const {
81 if (isPacked != that.isPacked)
83 if (ETypes != that.ETypes)
87 bool operator!=(const KeyTy& that) const {
88 return !this->operator==(that);
91 static inline StructType* getEmptyKey() {
92 return DenseMapInfo<StructType*>::getEmptyKey();
94 static inline StructType* getTombstoneKey() {
95 return DenseMapInfo<StructType*>::getTombstoneKey();
97 static unsigned getHashValue(const KeyTy& Key) {
98 return hash_combine(hash_combine_range(Key.ETypes.begin(),
102 static unsigned getHashValue(const StructType *ST) {
103 return getHashValue(KeyTy(ST));
105 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
106 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
108 return LHS == KeyTy(RHS);
110 static bool isEqual(const StructType *LHS, const StructType *RHS) {
115 struct FunctionTypeKeyInfo {
117 const Type *ReturnType;
118 ArrayRef<Type*> Params;
120 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
121 ReturnType(R), Params(P), isVarArg(V) {}
122 KeyTy(const FunctionType *FT)
123 : ReturnType(FT->getReturnType()), Params(FT->params()),
124 isVarArg(FT->isVarArg()) {}
125 bool operator==(const KeyTy& that) const {
126 if (ReturnType != that.ReturnType)
128 if (isVarArg != that.isVarArg)
130 if (Params != that.Params)
134 bool operator!=(const KeyTy& that) const {
135 return !this->operator==(that);
138 static inline FunctionType* getEmptyKey() {
139 return DenseMapInfo<FunctionType*>::getEmptyKey();
141 static inline FunctionType* getTombstoneKey() {
142 return DenseMapInfo<FunctionType*>::getTombstoneKey();
144 static unsigned getHashValue(const KeyTy& Key) {
145 return hash_combine(Key.ReturnType,
146 hash_combine_range(Key.Params.begin(),
150 static unsigned getHashValue(const FunctionType *FT) {
151 return getHashValue(KeyTy(FT));
153 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
154 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
156 return LHS == KeyTy(RHS);
158 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
163 /// \brief DenseMapInfo for GenericMDNode.
165 /// Note that we don't need the is-function-local bit, since that's implicit in
167 struct GenericMDNodeInfo {
169 ArrayRef<Value *> Ops;
172 KeyTy(ArrayRef<Value *> Ops)
173 : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
175 KeyTy(GenericMDNode *N, SmallVectorImpl<Value *> &Storage) {
176 Storage.resize(N->getNumOperands());
177 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
178 Storage[I] = N->getOperand(I);
180 Hash = hash_combine_range(Ops.begin(), Ops.end());
183 bool operator==(const GenericMDNode *RHS) const {
184 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
186 if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
188 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
189 if (Ops[I] != RHS->getOperand(I))
194 static inline GenericMDNode *getEmptyKey() {
195 return DenseMapInfo<GenericMDNode *>::getEmptyKey();
197 static inline GenericMDNode *getTombstoneKey() {
198 return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
200 static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
201 static unsigned getHashValue(const GenericMDNode *U) {
204 static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
207 static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
212 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
213 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
214 class DebugRecVH : public CallbackVH {
215 /// Ctx - This is the LLVM Context being referenced.
216 LLVMContextImpl *Ctx;
218 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
219 /// this reference lives in. If this is zero, then it represents a
220 /// non-canonical entry that has no DenseMap value. This can happen due to
224 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
225 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
227 MDNode *get() const {
228 return cast_or_null<MDNode>(getValPtr());
231 void deleted() override;
232 void allUsesReplacedWith(Value *VNew) override;
235 class LLVMContextImpl {
237 /// OwnedModules - The set of modules instantiated in this context, and which
238 /// will be automatically deleted if this context is deleted.
239 SmallPtrSet<Module*, 4> OwnedModules;
241 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
242 void *InlineAsmDiagContext;
244 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
245 void *DiagnosticContext;
246 bool RespectDiagnosticFilters;
248 LLVMContext::YieldCallbackTy YieldCallback;
249 void *YieldOpaqueHandle;
251 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
252 IntMapTy IntConstants;
254 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
257 FoldingSet<AttributeImpl> AttrsSet;
258 FoldingSet<AttributeSetImpl> AttrsLists;
259 FoldingSet<AttributeSetNode> AttrsSetNodes;
261 StringMap<MDString> MDStringCache;
263 DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
265 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
266 // aren't in the MDNodeSet, but they're still shared between objects, so no
267 // one object can destroy them. This set allows us to at least destroy them
268 // on Context destruction.
269 SmallPtrSet<GenericMDNode *, 1> NonUniquedMDNodes;
271 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
273 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
274 ArrayConstantsTy ArrayConstants;
276 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
277 StructConstantsTy StructConstants;
279 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
280 VectorConstantsTy VectorConstants;
282 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
284 DenseMap<Type*, UndefValue*> UVConstants;
286 StringMap<ConstantDataSequential*> CDSConstants;
288 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
290 ConstantUniqueMap<ConstantExpr> ExprConstants;
292 ConstantUniqueMap<InlineAsm> InlineAsms;
294 ConstantInt *TheTrueVal;
295 ConstantInt *TheFalseVal;
297 LeakDetectorImpl<Value> LLVMObjects;
299 // Basic type instances.
300 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
301 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
302 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
305 /// TypeAllocator - All dynamically allocated types are allocated from this.
306 /// They live forever until the context is torn down.
307 BumpPtrAllocator TypeAllocator;
309 DenseMap<unsigned, IntegerType*> IntegerTypes;
311 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
312 FunctionTypeMap FunctionTypes;
313 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
314 StructTypeMap AnonStructTypes;
315 StringMap<StructType*> NamedStructTypes;
316 unsigned NamedStructTypesUniqueID;
318 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
319 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
320 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
321 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
324 /// ValueHandles - This map keeps track of all of the value handles that are
325 /// watching a Value*. The Value::HasValueHandle bit is used to know
326 /// whether or not a value has an entry in this map.
327 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
328 ValueHandlesTy ValueHandles;
330 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
331 StringMap<unsigned> CustomMDKindNames;
333 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
334 typedef SmallVector<MDPairTy, 2> MDMapTy;
336 /// MetadataStore - Collection of per-instruction metadata used in this
338 DenseMap<const Instruction *, MDMapTy> MetadataStore;
340 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
341 /// entry with no "inlined at" element.
342 DenseMap<MDNode*, int> ScopeRecordIdx;
344 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
345 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
346 /// the MDNode is RAUW'd.
347 std::vector<DebugRecVH> ScopeRecords;
349 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
350 /// scope/inlined-at pair.
351 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
353 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
354 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
356 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
358 /// DiscriminatorTable - This table maps file:line locations to an
359 /// integer representing the next DWARF path discriminator to assign to
360 /// instructions in different blocks at the same location.
361 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
363 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
364 /// requested in this context
365 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
366 IntrinsicIDCacheTy IntrinsicIDCache;
368 /// \brief Mapping from a function to its prefix data, which is stored as the
369 /// operand of an unparented ReturnInst so that the prefix data has a Use.
370 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
371 PrefixDataMapTy PrefixDataMap;
373 /// \brief Mapping from a function to its prologue data, which is stored as
374 /// the operand of an unparented ReturnInst so that the prologue data has a
376 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
377 PrologueDataMapTy PrologueDataMap;
379 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
380 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
382 LLVMContextImpl(LLVMContext &C);