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 "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/DenseSet.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"
40 class DiagnosticInfoOptimizationRemark;
41 class DiagnosticInfoOptimizationRemarkMissed;
42 class DiagnosticInfoOptimizationRemarkAnalysis;
48 struct DenseMapAPIntKeyInfo {
49 static inline APInt getEmptyKey() {
54 static inline APInt getTombstoneKey() {
59 static unsigned getHashValue(const APInt &Key) {
60 return static_cast<unsigned>(hash_value(Key));
62 static bool isEqual(const APInt &LHS, const APInt &RHS) {
63 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
67 struct DenseMapAPFloatKeyInfo {
68 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
69 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
70 static unsigned getHashValue(const APFloat &Key) {
71 return static_cast<unsigned>(hash_value(Key));
73 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
74 return LHS.bitwiseIsEqual(RHS);
78 struct AnonStructTypeKeyInfo {
80 ArrayRef<Type*> ETypes;
82 KeyTy(const ArrayRef<Type*>& E, bool P) :
83 ETypes(E), isPacked(P) {}
84 KeyTy(const StructType *ST)
85 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
86 bool operator==(const KeyTy& that) const {
87 if (isPacked != that.isPacked)
89 if (ETypes != that.ETypes)
93 bool operator!=(const KeyTy& that) const {
94 return !this->operator==(that);
97 static inline StructType* getEmptyKey() {
98 return DenseMapInfo<StructType*>::getEmptyKey();
100 static inline StructType* getTombstoneKey() {
101 return DenseMapInfo<StructType*>::getTombstoneKey();
103 static unsigned getHashValue(const KeyTy& Key) {
104 return hash_combine(hash_combine_range(Key.ETypes.begin(),
108 static unsigned getHashValue(const StructType *ST) {
109 return getHashValue(KeyTy(ST));
111 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
112 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
114 return LHS == KeyTy(RHS);
116 static bool isEqual(const StructType *LHS, const StructType *RHS) {
121 struct FunctionTypeKeyInfo {
123 const Type *ReturnType;
124 ArrayRef<Type*> Params;
126 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
127 ReturnType(R), Params(P), isVarArg(V) {}
128 KeyTy(const FunctionType *FT)
129 : ReturnType(FT->getReturnType()), Params(FT->params()),
130 isVarArg(FT->isVarArg()) {}
131 bool operator==(const KeyTy& that) const {
132 if (ReturnType != that.ReturnType)
134 if (isVarArg != that.isVarArg)
136 if (Params != that.Params)
140 bool operator!=(const KeyTy& that) const {
141 return !this->operator==(that);
144 static inline FunctionType* getEmptyKey() {
145 return DenseMapInfo<FunctionType*>::getEmptyKey();
147 static inline FunctionType* getTombstoneKey() {
148 return DenseMapInfo<FunctionType*>::getTombstoneKey();
150 static unsigned getHashValue(const KeyTy& Key) {
151 return hash_combine(Key.ReturnType,
152 hash_combine_range(Key.Params.begin(),
156 static unsigned getHashValue(const FunctionType *FT) {
157 return getHashValue(KeyTy(FT));
159 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
160 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
162 return LHS == KeyTy(RHS);
164 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
169 /// \brief Structure for hashing arbitrary MDNode operands.
171 ArrayRef<Metadata *> RawOps;
172 ArrayRef<MDOperand> Ops;
177 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
178 : RawOps(Ops), Hash(calculateHash(Ops)) {}
180 template <class NodeTy>
181 MDNodeOpsKey(NodeTy *N, unsigned Offset = 0)
182 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
184 template <class NodeTy>
185 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
186 if (getHash() != RHS->getHash())
189 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
190 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
191 : compareOps(RawOps, RHS, Offset);
194 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
198 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
199 if (Ops.size() != RHS->getNumOperands() - Offset)
201 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
204 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
207 unsigned getHash() const { return Hash; }
210 /// \brief DenseMapInfo for MDTuple.
212 /// Note that we don't need the is-function-local bit, since that's implicit in
215 struct KeyTy : MDNodeOpsKey {
216 KeyTy(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
217 KeyTy(MDTuple *N) : MDNodeOpsKey(N) {}
219 bool operator==(const MDTuple *RHS) const {
220 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
222 return compareOps(RHS);
225 static unsigned calculateHash(MDTuple *N) {
226 return MDNodeOpsKey::calculateHash(N);
229 static inline MDTuple *getEmptyKey() {
230 return DenseMapInfo<MDTuple *>::getEmptyKey();
232 static inline MDTuple *getTombstoneKey() {
233 return DenseMapInfo<MDTuple *>::getTombstoneKey();
235 static unsigned getHashValue(const KeyTy &Key) { return Key.getHash(); }
236 static unsigned getHashValue(const MDTuple *U) { return U->getHash(); }
237 static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
240 static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
245 /// \brief DenseMapInfo for MDLocation.
246 struct MDLocationInfo {
253 KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
254 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
256 KeyTy(const MDLocation *L)
257 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
258 InlinedAt(L->getInlinedAt()) {}
260 bool operator==(const MDLocation *RHS) const {
261 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
263 return Line == RHS->getLine() && Column == RHS->getColumn() &&
264 Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
267 static inline MDLocation *getEmptyKey() {
268 return DenseMapInfo<MDLocation *>::getEmptyKey();
270 static inline MDLocation *getTombstoneKey() {
271 return DenseMapInfo<MDLocation *>::getTombstoneKey();
273 static unsigned getHashValue(const KeyTy &Key) {
274 return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
276 static unsigned getHashValue(const MDLocation *U) {
277 return getHashValue(KeyTy(U));
279 static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
282 static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
287 /// \brief DenseMapInfo for GenericDwarfNode.
288 struct GenericDwarfNodeInfo {
289 struct KeyTy : MDNodeOpsKey {
292 KeyTy(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
293 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
294 KeyTy(GenericDwarfNode *N)
295 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
297 bool operator==(const GenericDwarfNode *RHS) const {
298 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
300 return Tag == RHS->getTag() && Header == RHS->getHeader() &&
304 static unsigned calculateHash(GenericDwarfNode *N) {
305 return MDNodeOpsKey::calculateHash(N, 1);
308 static inline GenericDwarfNode *getEmptyKey() {
309 return DenseMapInfo<GenericDwarfNode *>::getEmptyKey();
311 static inline GenericDwarfNode *getTombstoneKey() {
312 return DenseMapInfo<GenericDwarfNode *>::getTombstoneKey();
314 static unsigned getHashValue(const KeyTy &Key) {
315 return hash_combine(Key.getHash(), Key.Tag, Key.Header);
317 static unsigned getHashValue(const GenericDwarfNode *U) {
318 return hash_combine(U->getHash(), U->getTag(), U->getHeader());
320 static bool isEqual(const KeyTy &LHS, const GenericDwarfNode *RHS) {
323 static bool isEqual(const GenericDwarfNode *LHS,
324 const GenericDwarfNode *RHS) {
329 class LLVMContextImpl {
331 /// OwnedModules - The set of modules instantiated in this context, and which
332 /// will be automatically deleted if this context is deleted.
333 SmallPtrSet<Module*, 4> OwnedModules;
335 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
336 void *InlineAsmDiagContext;
338 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
339 void *DiagnosticContext;
340 bool RespectDiagnosticFilters;
342 LLVMContext::YieldCallbackTy YieldCallback;
343 void *YieldOpaqueHandle;
345 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
346 IntMapTy IntConstants;
348 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
351 FoldingSet<AttributeImpl> AttrsSet;
352 FoldingSet<AttributeSetImpl> AttrsLists;
353 FoldingSet<AttributeSetNode> AttrsSetNodes;
355 StringMap<MDString> MDStringCache;
356 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
357 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
359 DenseSet<MDTuple *, MDTupleInfo> MDTuples;
360 DenseSet<MDLocation *, MDLocationInfo> MDLocations;
361 DenseSet<GenericDwarfNode *, GenericDwarfNodeInfo> GenericDwarfNodes;
363 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
364 // aren't in the MDNodeSet, but they're still shared between objects, so no
365 // one object can destroy them. This set allows us to at least destroy them
366 // on Context destruction.
367 SmallPtrSet<MDNode *, 1> DistinctMDNodes;
369 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
371 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
372 ArrayConstantsTy ArrayConstants;
374 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
375 StructConstantsTy StructConstants;
377 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
378 VectorConstantsTy VectorConstants;
380 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
382 DenseMap<Type*, UndefValue*> UVConstants;
384 StringMap<ConstantDataSequential*> CDSConstants;
386 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
388 ConstantUniqueMap<ConstantExpr> ExprConstants;
390 ConstantUniqueMap<InlineAsm> InlineAsms;
392 ConstantInt *TheTrueVal;
393 ConstantInt *TheFalseVal;
395 // Basic type instances.
396 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
397 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
398 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
401 /// TypeAllocator - All dynamically allocated types are allocated from this.
402 /// They live forever until the context is torn down.
403 BumpPtrAllocator TypeAllocator;
405 DenseMap<unsigned, IntegerType*> IntegerTypes;
407 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
408 FunctionTypeSet FunctionTypes;
409 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
410 StructTypeSet AnonStructTypes;
411 StringMap<StructType*> NamedStructTypes;
412 unsigned NamedStructTypesUniqueID;
414 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
415 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
416 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
417 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
420 /// ValueHandles - This map keeps track of all of the value handles that are
421 /// watching a Value*. The Value::HasValueHandle bit is used to know
422 /// whether or not a value has an entry in this map.
423 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
424 ValueHandlesTy ValueHandles;
426 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
427 StringMap<unsigned> CustomMDKindNames;
429 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
430 typedef SmallVector<MDPairTy, 2> MDMapTy;
432 /// MetadataStore - Collection of per-instruction metadata used in this
434 DenseMap<const Instruction *, MDMapTy> MetadataStore;
436 /// DiscriminatorTable - This table maps file:line locations to an
437 /// integer representing the next DWARF path discriminator to assign to
438 /// instructions in different blocks at the same location.
439 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
441 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
442 /// requested in this context
443 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
444 IntrinsicIDCacheTy IntrinsicIDCache;
446 /// \brief Mapping from a function to its prefix data, which is stored as the
447 /// operand of an unparented ReturnInst so that the prefix data has a Use.
448 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
449 PrefixDataMapTy PrefixDataMap;
451 /// \brief Mapping from a function to its prologue data, which is stored as
452 /// the operand of an unparented ReturnInst so that the prologue data has a
454 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
455 PrologueDataMapTy PrologueDataMap;
457 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
458 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
460 /// An owning list of all GCStrategies which have been created
461 SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
462 /// A helper map to speedup lookups into the above list
463 StringMap<GCStrategy*> GCStrategyMap;
465 /// Lookup the GCStrategy object associated with the given gc name. If one
466 /// can't be found, returns nullptr. The lifetime of the returned objects
467 /// is dictated by the lifetime of the associated context. No caller should
468 /// attempt to delete the returned objects.
469 GCStrategy *getGCStrategy(const StringRef Name);
471 LLVMContextImpl(LLVMContext &C);
474 /// Destroy the ConstantArrays if they are not used.
475 void dropTriviallyDeadConstantArrays();