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() { return APInt(nullptr, 0); }
50   static inline APInt getTombstoneKey() {
51     return APInt(reinterpret_cast<uint64_t *>(sizeof(uint64_t)), 0);
52   }
53   static unsigned getHashValue(const APInt &Key) {
54     return static_cast<unsigned>(hash_value(Key));
55   }
56   static bool isEqual(const APInt &LHS, const APInt &RHS) {
57     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
58   }
59 };
60
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));
66   }
67   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
68     return LHS.bitwiseIsEqual(RHS);
69   }
70 };
71
72 struct AnonStructTypeKeyInfo {
73   struct KeyTy {
74     ArrayRef<Type*> ETypes;
75     bool isPacked;
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)
82         return false;
83       if (ETypes != that.ETypes)
84         return false;
85       return true;
86     }
87     bool operator!=(const KeyTy& that) const {
88       return !this->operator==(that);
89     }
90   };
91   static inline StructType* getEmptyKey() {
92     return DenseMapInfo<StructType*>::getEmptyKey();
93   }
94   static inline StructType* getTombstoneKey() {
95     return DenseMapInfo<StructType*>::getTombstoneKey();
96   }
97   static unsigned getHashValue(const KeyTy& Key) {
98     return hash_combine(hash_combine_range(Key.ETypes.begin(),
99                                            Key.ETypes.end()),
100                         Key.isPacked);
101   }
102   static unsigned getHashValue(const StructType *ST) {
103     return getHashValue(KeyTy(ST));
104   }
105   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
106     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
107       return false;
108     return LHS == KeyTy(RHS);
109   }
110   static bool isEqual(const StructType *LHS, const StructType *RHS) {
111     return LHS == RHS;
112   }
113 };
114
115 struct FunctionTypeKeyInfo {
116   struct KeyTy {
117     const Type *ReturnType;
118     ArrayRef<Type*> Params;
119     bool isVarArg;
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)
127         return false;
128       if (isVarArg != that.isVarArg)
129         return false;
130       if (Params != that.Params)
131         return false;
132       return true;
133     }
134     bool operator!=(const KeyTy& that) const {
135       return !this->operator==(that);
136     }
137   };
138   static inline FunctionType* getEmptyKey() {
139     return DenseMapInfo<FunctionType*>::getEmptyKey();
140   }
141   static inline FunctionType* getTombstoneKey() {
142     return DenseMapInfo<FunctionType*>::getTombstoneKey();
143   }
144   static unsigned getHashValue(const KeyTy& Key) {
145     return hash_combine(Key.ReturnType,
146                         hash_combine_range(Key.Params.begin(),
147                                            Key.Params.end()),
148                         Key.isVarArg);
149   }
150   static unsigned getHashValue(const FunctionType *FT) {
151     return getHashValue(KeyTy(FT));
152   }
153   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
154     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
155       return false;
156     return LHS == KeyTy(RHS);
157   }
158   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
159     return LHS == RHS;
160   }
161 };
162
163 /// \brief DenseMapInfo for GenericMDNode.
164 ///
165 /// Note that we don't need the is-function-local bit, since that's implicit in
166 /// the operands.
167 struct GenericMDNodeInfo {
168   struct KeyTy {
169     ArrayRef<Value *> Ops;
170     unsigned Hash;
171
172     KeyTy(ArrayRef<Value *> Ops)
173         : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
174
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);
179       Ops = Storage;
180       Hash = hash_combine_range(Ops.begin(), Ops.end());
181     }
182
183     bool operator==(const GenericMDNode *RHS) const {
184       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
185         return false;
186       if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
187         return false;
188       for (unsigned I = 0, E = Ops.size(); I != E; ++I)
189         if (Ops[I] != RHS->getOperand(I))
190           return false;
191       return true;
192     }
193   };
194   static inline GenericMDNode *getEmptyKey() {
195     return DenseMapInfo<GenericMDNode *>::getEmptyKey();
196   }
197   static inline GenericMDNode *getTombstoneKey() {
198     return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
199   }
200   static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
201   static unsigned getHashValue(const GenericMDNode *U) {
202     return U->getHash();
203   }
204   static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
205     return LHS == RHS;
206   }
207   static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
208     return LHS == RHS;
209   }
210 };
211
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;
217   
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
221   /// RAUW.
222   int Idx;
223 public:
224   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
225     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
226   
227   MDNode *get() const {
228     return cast_or_null<MDNode>(getValPtr());
229   }
230
231   void deleted() override;
232   void allUsesReplacedWith(Value *VNew) override;
233 };
234   
235 class LLVMContextImpl {
236 public:
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;
240   
241   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
242   void *InlineAsmDiagContext;
243
244   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
245   void *DiagnosticContext;
246   bool RespectDiagnosticFilters;
247
248   LLVMContext::YieldCallbackTy YieldCallback;
249   void *YieldOpaqueHandle;
250
251   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
252   IntMapTy IntConstants;
253
254   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
255   FPMapTy FPConstants;
256
257   FoldingSet<AttributeImpl> AttrsSet;
258   FoldingSet<AttributeSetImpl> AttrsLists;
259   FoldingSet<AttributeSetNode> AttrsSetNodes;
260
261   StringMap<MDString> MDStringCache;
262
263   DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
264
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;
270
271   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
272
273   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
274   ArrayConstantsTy ArrayConstants;
275   
276   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
277   StructConstantsTy StructConstants;
278   
279   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
280   VectorConstantsTy VectorConstants;
281   
282   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
283
284   DenseMap<Type*, UndefValue*> UVConstants;
285   
286   StringMap<ConstantDataSequential*> CDSConstants;
287
288   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
289     BlockAddresses;
290   ConstantUniqueMap<ConstantExpr> ExprConstants;
291
292   ConstantUniqueMap<InlineAsm> InlineAsms;
293
294   ConstantInt *TheTrueVal;
295   ConstantInt *TheFalseVal;
296   
297   LeakDetectorImpl<Value> LLVMObjects;
298   
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;
303
304   
305   /// TypeAllocator - All dynamically allocated types are allocated from this.
306   /// They live forever until the context is torn down.
307   BumpPtrAllocator TypeAllocator;
308   
309   DenseMap<unsigned, IntegerType*> IntegerTypes;
310   
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;
317     
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;
322
323
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;
329   
330   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
331   StringMap<unsigned> CustomMDKindNames;
332   
333   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
334   typedef SmallVector<MDPairTy, 2> MDMapTy;
335
336   /// MetadataStore - Collection of per-instruction metadata used in this
337   /// context.
338   DenseMap<const Instruction *, MDMapTy> MetadataStore;
339   
340   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
341   /// entry with no "inlined at" element.
342   DenseMap<MDNode*, int> ScopeRecordIdx;
343   
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;
348   
349   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
350   /// scope/inlined-at pair.
351   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
352   
353   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
354   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
355   /// to date.
356   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
357
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;
362
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;
367
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;
372
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
375   /// Use.
376   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
377   PrologueDataMapTy PrologueDataMap;
378
379   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
380   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
381   
382   LLVMContextImpl(LLVMContext &C);
383   ~LLVMContextImpl();
384 };
385
386 }
387
388 #endif