Reapply "LLVMContext: Store APInt/APFloat directly into the ConstantInt/FP DenseMaps."
[oota-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 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
17
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"
35 #include <vector>
36
37 namespace llvm {
38
39 class ConstantInt;
40 class ConstantFP;
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
44 class LLVMContext;
45 class Type;
46 class Value;
47
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 };
66
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 };
77
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 };
120
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 };
168
169 /// \brief DenseMapInfo for GenericMDNode.
170 ///
171 /// Note that we don't need the is-function-local bit, since that's implicit in
172 /// the operands.
173 struct GenericMDNodeInfo {
174   struct KeyTy {
175     ArrayRef<Value *> Ops;
176     unsigned Hash;
177
178     KeyTy(ArrayRef<Value *> Ops)
179         : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
180
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);
185       Ops = Storage;
186       Hash = hash_combine_range(Ops.begin(), Ops.end());
187     }
188
189     bool operator==(const GenericMDNode *RHS) const {
190       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
191         return false;
192       if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
193         return false;
194       for (unsigned I = 0, E = Ops.size(); I != E; ++I)
195         if (Ops[I] != RHS->getOperand(I))
196           return false;
197       return true;
198     }
199   };
200   static inline GenericMDNode *getEmptyKey() {
201     return DenseMapInfo<GenericMDNode *>::getEmptyKey();
202   }
203   static inline GenericMDNode *getTombstoneKey() {
204     return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
205   }
206   static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
207   static unsigned getHashValue(const GenericMDNode *U) {
208     return U->getHash();
209   }
210   static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
211     return LHS == RHS;
212   }
213   static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
214     return LHS == RHS;
215   }
216 };
217
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;
223   
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
227   /// RAUW.
228   int Idx;
229 public:
230   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
231     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
232   
233   MDNode *get() const {
234     return cast_or_null<MDNode>(getValPtr());
235   }
236
237   void deleted() override;
238   void allUsesReplacedWith(Value *VNew) override;
239 };
240   
241 class LLVMContextImpl {
242 public:
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;
246   
247   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
248   void *InlineAsmDiagContext;
249
250   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
251   void *DiagnosticContext;
252   bool RespectDiagnosticFilters;
253
254   LLVMContext::YieldCallbackTy YieldCallback;
255   void *YieldOpaqueHandle;
256
257   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
258   IntMapTy IntConstants;
259
260   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
261   FPMapTy FPConstants;
262
263   FoldingSet<AttributeImpl> AttrsSet;
264   FoldingSet<AttributeSetImpl> AttrsLists;
265   FoldingSet<AttributeSetNode> AttrsSetNodes;
266
267   StringMap<MDString> MDStringCache;
268
269   DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
270
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;
276
277   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
278
279   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
280   ArrayConstantsTy ArrayConstants;
281   
282   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
283   StructConstantsTy StructConstants;
284   
285   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
286   VectorConstantsTy VectorConstants;
287   
288   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
289
290   DenseMap<Type*, UndefValue*> UVConstants;
291   
292   StringMap<ConstantDataSequential*> CDSConstants;
293
294   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
295     BlockAddresses;
296   ConstantUniqueMap<ConstantExpr> ExprConstants;
297
298   ConstantUniqueMap<InlineAsm> InlineAsms;
299
300   ConstantInt *TheTrueVal;
301   ConstantInt *TheFalseVal;
302   
303   LeakDetectorImpl<Value> LLVMObjects;
304   
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;
309
310   
311   /// TypeAllocator - All dynamically allocated types are allocated from this.
312   /// They live forever until the context is torn down.
313   BumpPtrAllocator TypeAllocator;
314   
315   DenseMap<unsigned, IntegerType*> IntegerTypes;
316   
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;
323     
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;
328
329
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;
335   
336   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
337   StringMap<unsigned> CustomMDKindNames;
338   
339   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
340   typedef SmallVector<MDPairTy, 2> MDMapTy;
341
342   /// MetadataStore - Collection of per-instruction metadata used in this
343   /// context.
344   DenseMap<const Instruction *, MDMapTy> MetadataStore;
345   
346   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
347   /// entry with no "inlined at" element.
348   DenseMap<MDNode*, int> ScopeRecordIdx;
349   
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;
354   
355   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
356   /// scope/inlined-at pair.
357   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
358   
359   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
360   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
361   /// to date.
362   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
363
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;
368
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;
373
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;
378
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
381   /// Use.
382   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
383   PrologueDataMapTy PrologueDataMap;
384
385   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
386   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
387   
388   LLVMContextImpl(LLVMContext &C);
389   ~LLVMContextImpl();
390 };
391
392 }
393
394 #endif