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() {
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 DenseMapInfo for GenericMDNode.
171 /// Note that we don't need the is-function-local bit, since that's implicit in
173 struct GenericMDNodeInfo {
175 ArrayRef<Value *> Ops;
178 KeyTy(ArrayRef<Value *> Ops)
179 : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
181 KeyTy(GenericMDNode *N, SmallVectorImpl<Value *> &Storage) {
182 Storage.resize(N->getNumOperands());
183 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
184 Storage[I] = N->getOperand(I);
186 Hash = hash_combine_range(Ops.begin(), Ops.end());
189 bool operator==(const GenericMDNode *RHS) const {
190 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
192 if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
194 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
195 if (Ops[I] != RHS->getOperand(I))
200 static inline GenericMDNode *getEmptyKey() {
201 return DenseMapInfo<GenericMDNode *>::getEmptyKey();
203 static inline GenericMDNode *getTombstoneKey() {
204 return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
206 static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
207 static unsigned getHashValue(const GenericMDNode *U) {
210 static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
213 static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
218 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
219 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
220 class DebugRecVH : public CallbackVH {
221 /// Ctx - This is the LLVM Context being referenced.
222 LLVMContextImpl *Ctx;
224 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
225 /// this reference lives in. If this is zero, then it represents a
226 /// non-canonical entry that has no DenseMap value. This can happen due to
230 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
231 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
233 MDNode *get() const {
234 return cast_or_null<MDNode>(getValPtr());
237 void deleted() override;
238 void allUsesReplacedWith(Value *VNew) override;
241 class LLVMContextImpl {
243 /// OwnedModules - The set of modules instantiated in this context, and which
244 /// will be automatically deleted if this context is deleted.
245 SmallPtrSet<Module*, 4> OwnedModules;
247 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
248 void *InlineAsmDiagContext;
250 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
251 void *DiagnosticContext;
252 bool RespectDiagnosticFilters;
254 LLVMContext::YieldCallbackTy YieldCallback;
255 void *YieldOpaqueHandle;
257 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
258 IntMapTy IntConstants;
260 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
263 FoldingSet<AttributeImpl> AttrsSet;
264 FoldingSet<AttributeSetImpl> AttrsLists;
265 FoldingSet<AttributeSetNode> AttrsSetNodes;
267 StringMap<MDString> MDStringCache;
269 DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
271 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
272 // aren't in the MDNodeSet, but they're still shared between objects, so no
273 // one object can destroy them. This set allows us to at least destroy them
274 // on Context destruction.
275 SmallPtrSet<GenericMDNode *, 1> NonUniquedMDNodes;
277 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
279 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
280 ArrayConstantsTy ArrayConstants;
282 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
283 StructConstantsTy StructConstants;
285 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
286 VectorConstantsTy VectorConstants;
288 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
290 DenseMap<Type*, UndefValue*> UVConstants;
292 StringMap<ConstantDataSequential*> CDSConstants;
294 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
296 ConstantUniqueMap<ConstantExpr> ExprConstants;
298 ConstantUniqueMap<InlineAsm> InlineAsms;
300 ConstantInt *TheTrueVal;
301 ConstantInt *TheFalseVal;
303 LeakDetectorImpl<Value> LLVMObjects;
305 // Basic type instances.
306 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
307 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
308 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
311 /// TypeAllocator - All dynamically allocated types are allocated from this.
312 /// They live forever until the context is torn down.
313 BumpPtrAllocator TypeAllocator;
315 DenseMap<unsigned, IntegerType*> IntegerTypes;
317 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
318 FunctionTypeMap FunctionTypes;
319 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
320 StructTypeMap AnonStructTypes;
321 StringMap<StructType*> NamedStructTypes;
322 unsigned NamedStructTypesUniqueID;
324 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
325 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
326 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
327 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
330 /// ValueHandles - This map keeps track of all of the value handles that are
331 /// watching a Value*. The Value::HasValueHandle bit is used to know
332 /// whether or not a value has an entry in this map.
333 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
334 ValueHandlesTy ValueHandles;
336 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
337 StringMap<unsigned> CustomMDKindNames;
339 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
340 typedef SmallVector<MDPairTy, 2> MDMapTy;
342 /// MetadataStore - Collection of per-instruction metadata used in this
344 DenseMap<const Instruction *, MDMapTy> MetadataStore;
346 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
347 /// entry with no "inlined at" element.
348 DenseMap<MDNode*, int> ScopeRecordIdx;
350 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
351 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
352 /// the MDNode is RAUW'd.
353 std::vector<DebugRecVH> ScopeRecords;
355 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
356 /// scope/inlined-at pair.
357 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
359 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
360 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
362 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
364 /// DiscriminatorTable - This table maps file:line locations to an
365 /// integer representing the next DWARF path discriminator to assign to
366 /// instructions in different blocks at the same location.
367 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
369 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
370 /// requested in this context
371 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
372 IntrinsicIDCacheTy IntrinsicIDCache;
374 /// \brief Mapping from a function to its prefix data, which is stored as the
375 /// operand of an unparented ReturnInst so that the prefix data has a Use.
376 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
377 PrefixDataMapTy PrefixDataMap;
379 /// \brief Mapping from a function to its prologue data, which is stored as
380 /// the operand of an unparented ReturnInst so that the prologue data has a
382 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
383 PrologueDataMapTy PrologueDataMap;
385 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
386 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
388 LLVMContextImpl(LLVMContext &C);