]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - lib/IR/LLVMContextImpl.h
a009262f80ebeea76c9db8fc6588750d555be860
[opencl/llvm.git] / lib / IR / LLVMContextImpl.h
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 //===----------------------------------------------------------------------===//
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"
34 #include <vector>
36 namespace llvm {
38 class ConstantInt;
39 class ConstantFP;
40 class DiagnosticInfoOptimizationRemark;
41 class DiagnosticInfoOptimizationRemarkMissed;
42 class DiagnosticInfoOptimizationRemarkAnalysis;
43 class GCStrategy;
44 class LLVMContext;
45 class Type;
46 class Value;
48 struct DenseMapAPIntKeyInfo {
49   static inline APInt getEmptyKey() {
50     APInt V(nullptr, 0);
51     V.VAL = 0;
52     return V;
53   }
54   static inline APInt getTombstoneKey() {
55     APInt V(nullptr, 0);
56     V.VAL = 1;
57     return V;
58   }
59   static unsigned getHashValue(const APInt &Key) {
60     return static_cast<unsigned>(hash_value(Key));
61   }
62   static bool isEqual(const APInt &LHS, const APInt &RHS) {
63     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
64   }
65 };
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));
72   }
73   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
74     return LHS.bitwiseIsEqual(RHS);
75   }
76 };
78 struct AnonStructTypeKeyInfo {
79   struct KeyTy {
80     ArrayRef<Type*> ETypes;
81     bool isPacked;
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)
88         return false;
89       if (ETypes != that.ETypes)
90         return false;
91       return true;
92     }
93     bool operator!=(const KeyTy& that) const {
94       return !this->operator==(that);
95     }
96   };
97   static inline StructType* getEmptyKey() {
98     return DenseMapInfo<StructType*>::getEmptyKey();
99   }
100   static inline StructType* getTombstoneKey() {
101     return DenseMapInfo<StructType*>::getTombstoneKey();
102   }
103   static unsigned getHashValue(const KeyTy& Key) {
104     return hash_combine(hash_combine_range(Key.ETypes.begin(),
105                                            Key.ETypes.end()),
106                         Key.isPacked);
107   }
108   static unsigned getHashValue(const StructType *ST) {
109     return getHashValue(KeyTy(ST));
110   }
111   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
112     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
113       return false;
114     return LHS == KeyTy(RHS);
115   }
116   static bool isEqual(const StructType *LHS, const StructType *RHS) {
117     return LHS == RHS;
118   }
119 };
121 struct FunctionTypeKeyInfo {
122   struct KeyTy {
123     const Type *ReturnType;
124     ArrayRef<Type*> Params;
125     bool isVarArg;
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)
133         return false;
134       if (isVarArg != that.isVarArg)
135         return false;
136       if (Params != that.Params)
137         return false;
138       return true;
139     }
140     bool operator!=(const KeyTy& that) const {
141       return !this->operator==(that);
142     }
143   };
144   static inline FunctionType* getEmptyKey() {
145     return DenseMapInfo<FunctionType*>::getEmptyKey();
146   }
147   static inline FunctionType* getTombstoneKey() {
148     return DenseMapInfo<FunctionType*>::getTombstoneKey();
149   }
150   static unsigned getHashValue(const KeyTy& Key) {
151     return hash_combine(Key.ReturnType,
152                         hash_combine_range(Key.Params.begin(),
153                                            Key.Params.end()),
154                         Key.isVarArg);
155   }
156   static unsigned getHashValue(const FunctionType *FT) {
157     return getHashValue(KeyTy(FT));
158   }
159   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
160     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
161       return false;
162     return LHS == KeyTy(RHS);
163   }
164   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
165     return LHS == RHS;
166   }
167 };
169 /// \brief Structure for hashing arbitrary MDNode operands.
170 class MDNodeOpsKey {
171   ArrayRef<Metadata *> RawOps;
172   ArrayRef<MDOperand> Ops;
174   unsigned Hash;
176 protected:
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())
187       return false;
189     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
190     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
191                           : compareOps(RawOps, RHS, Offset);
192   }
194   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
196 private:
197   template <class T>
198   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
199     if (Ops.size() != RHS->getNumOperands() - Offset)
200       return false;
201     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
202   }
204   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
206 public:
207   unsigned getHash() const { return Hash; }
208 };
210 /// \brief DenseMapInfo for MDTuple.
211 ///
212 /// Note that we don't need the is-function-local bit, since that's implicit in
213 /// the operands.
214 struct MDTupleInfo {
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())
221         return false;
222       return compareOps(RHS);
223     }
225     static unsigned calculateHash(MDTuple *N) {
226       return MDNodeOpsKey::calculateHash(N);
227     }
228   };
229   static inline MDTuple *getEmptyKey() {
230     return DenseMapInfo<MDTuple *>::getEmptyKey();
231   }
232   static inline MDTuple *getTombstoneKey() {
233     return DenseMapInfo<MDTuple *>::getTombstoneKey();
234   }
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) {
238     return LHS == RHS;
239   }
240   static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
241     return LHS == RHS;
242   }
243 };
245 /// \brief DenseMapInfo for MDLocation.
246 struct MDLocationInfo {
247   struct KeyTy {
248     unsigned Line;
249     unsigned Column;
250     Metadata *Scope;
251     Metadata *InlinedAt;
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())
262         return false;
263       return Line == RHS->getLine() && Column == RHS->getColumn() &&
264              Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
265     }
266   };
267   static inline MDLocation *getEmptyKey() {
268     return DenseMapInfo<MDLocation *>::getEmptyKey();
269   }
270   static inline MDLocation *getTombstoneKey() {
271     return DenseMapInfo<MDLocation *>::getTombstoneKey();
272   }
273   static unsigned getHashValue(const KeyTy &Key) {
274     return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
275   }
276   static unsigned getHashValue(const MDLocation *U) {
277     return getHashValue(KeyTy(U));
278   }
279   static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
280     return LHS == RHS;
281   }
282   static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
283     return LHS == RHS;
284   }
285 };
287 /// \brief DenseMapInfo for GenericDebugNode.
288 struct GenericDebugNodeInfo {
289   struct KeyTy : MDNodeOpsKey {
290     unsigned Tag;
291     MDString *Header;
292     KeyTy(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
293         : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
294     KeyTy(GenericDebugNode *N)
295         : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
297     bool operator==(const GenericDebugNode *RHS) const {
298       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
299         return false;
300       return Tag == RHS->getTag() && Header == RHS->getHeader() &&
301              compareOps(RHS, 1);
302     }
304     static unsigned calculateHash(GenericDebugNode *N) {
305       return MDNodeOpsKey::calculateHash(N, 1);
306     }
307   };
308   static inline GenericDebugNode *getEmptyKey() {
309     return DenseMapInfo<GenericDebugNode *>::getEmptyKey();
310   }
311   static inline GenericDebugNode *getTombstoneKey() {
312     return DenseMapInfo<GenericDebugNode *>::getTombstoneKey();
313   }
314   static unsigned getHashValue(const KeyTy &Key) {
315     return hash_combine(Key.getHash(), Key.Tag, Key.Header);
316   }
317   static unsigned getHashValue(const GenericDebugNode *U) {
318     return hash_combine(U->getHash(), U->getTag(), U->getHeader());
319   }
320   static bool isEqual(const KeyTy &LHS, const GenericDebugNode *RHS) {
321     return LHS == RHS;
322   }
323   static bool isEqual(const GenericDebugNode *LHS,
324                       const GenericDebugNode *RHS) {
325     return LHS == RHS;
326   }
327 };
329 class LLVMContextImpl {
330 public:
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;
334   
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;
349   FPMapTy FPConstants;
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<GenericDebugNode *, GenericDebugNodeInfo> GenericDebugNodes;
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;
373   
374   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
375   StructConstantsTy StructConstants;
376   
377   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
378   VectorConstantsTy VectorConstants;
379   
380   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
382   DenseMap<Type*, UndefValue*> UVConstants;
383   
384   StringMap<ConstantDataSequential*> CDSConstants;
386   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
387     BlockAddresses;
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;
400   
401   /// TypeAllocator - All dynamically allocated types are allocated from this.
402   /// They live forever until the context is torn down.
403   BumpPtrAllocator TypeAllocator;
404   
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;
413     
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;
425   
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
433   /// context.
434   DenseMap<const Instruction *, MDMapTy> MetadataStore;
435   
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
453   /// Use.
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);
470   
471   LLVMContextImpl(LLVMContext &C);
472   ~LLVMContextImpl();
474   /// Destroy the ConstantArrays if they are not used.
475   void dropTriviallyDeadConstantArrays();
476 };
480 #endif