LLVMContextImpl.h revision 0b8c9a80f20772c3793201ab5b251d3520b9cea3
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_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_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/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/Support/ValueHandle.h"
34#include <vector>
35
36namespace llvm {
37
38class ConstantInt;
39class ConstantFP;
40class LLVMContext;
41class Type;
42class Value;
43
44struct DenseMapAPIntKeyInfo {
45  struct KeyTy {
46    APInt val;
47    Type* type;
48    KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
49    bool operator==(const KeyTy& that) const {
50      return type == that.type && this->val == that.val;
51    }
52    bool operator!=(const KeyTy& that) const {
53      return !this->operator==(that);
54    }
55    friend hash_code hash_value(const KeyTy &Key) {
56      return hash_combine(Key.type, Key.val);
57    }
58  };
59  static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
60  static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
61  static unsigned getHashValue(const KeyTy &Key) {
62    return static_cast<unsigned>(hash_value(Key));
63  }
64  static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
65    return LHS == RHS;
66  }
67};
68
69struct DenseMapAPFloatKeyInfo {
70  struct KeyTy {
71    APFloat val;
72    KeyTy(const APFloat& V) : val(V){}
73    bool operator==(const KeyTy& that) const {
74      return this->val.bitwiseIsEqual(that.val);
75    }
76    bool operator!=(const KeyTy& that) const {
77      return !this->operator==(that);
78    }
79    friend hash_code hash_value(const KeyTy &Key) {
80      return hash_combine(Key.val);
81    }
82  };
83  static inline KeyTy getEmptyKey() {
84    return KeyTy(APFloat(APFloat::Bogus,1));
85  }
86  static inline KeyTy getTombstoneKey() {
87    return KeyTy(APFloat(APFloat::Bogus,2));
88  }
89  static unsigned getHashValue(const KeyTy &Key) {
90    return static_cast<unsigned>(hash_value(Key));
91  }
92  static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
93    return LHS == RHS;
94  }
95};
96
97struct AnonStructTypeKeyInfo {
98  struct KeyTy {
99    ArrayRef<Type*> ETypes;
100    bool isPacked;
101    KeyTy(const ArrayRef<Type*>& E, bool P) :
102      ETypes(E), isPacked(P) {}
103    KeyTy(const StructType* ST) :
104      ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
105      isPacked(ST->isPacked()) {}
106    bool operator==(const KeyTy& that) const {
107      if (isPacked != that.isPacked)
108        return false;
109      if (ETypes != that.ETypes)
110        return false;
111      return true;
112    }
113    bool operator!=(const KeyTy& that) const {
114      return !this->operator==(that);
115    }
116  };
117  static inline StructType* getEmptyKey() {
118    return DenseMapInfo<StructType*>::getEmptyKey();
119  }
120  static inline StructType* getTombstoneKey() {
121    return DenseMapInfo<StructType*>::getTombstoneKey();
122  }
123  static unsigned getHashValue(const KeyTy& Key) {
124    return hash_combine(hash_combine_range(Key.ETypes.begin(),
125                                           Key.ETypes.end()),
126                        Key.isPacked);
127  }
128  static unsigned getHashValue(const StructType *ST) {
129    return getHashValue(KeyTy(ST));
130  }
131  static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
132    if (RHS == getEmptyKey() || RHS == getTombstoneKey())
133      return false;
134    return LHS == KeyTy(RHS);
135  }
136  static bool isEqual(const StructType *LHS, const StructType *RHS) {
137    return LHS == RHS;
138  }
139};
140
141struct FunctionTypeKeyInfo {
142  struct KeyTy {
143    const Type *ReturnType;
144    ArrayRef<Type*> Params;
145    bool isVarArg;
146    KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
147      ReturnType(R), Params(P), isVarArg(V) {}
148    KeyTy(const FunctionType* FT) :
149      ReturnType(FT->getReturnType()),
150      Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
151      isVarArg(FT->isVarArg()) {}
152    bool operator==(const KeyTy& that) const {
153      if (ReturnType != that.ReturnType)
154        return false;
155      if (isVarArg != that.isVarArg)
156        return false;
157      if (Params != that.Params)
158        return false;
159      return true;
160    }
161    bool operator!=(const KeyTy& that) const {
162      return !this->operator==(that);
163    }
164  };
165  static inline FunctionType* getEmptyKey() {
166    return DenseMapInfo<FunctionType*>::getEmptyKey();
167  }
168  static inline FunctionType* getTombstoneKey() {
169    return DenseMapInfo<FunctionType*>::getTombstoneKey();
170  }
171  static unsigned getHashValue(const KeyTy& Key) {
172    return hash_combine(Key.ReturnType,
173                        hash_combine_range(Key.Params.begin(),
174                                           Key.Params.end()),
175                        Key.isVarArg);
176  }
177  static unsigned getHashValue(const FunctionType *FT) {
178    return getHashValue(KeyTy(FT));
179  }
180  static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
181    if (RHS == getEmptyKey() || RHS == getTombstoneKey())
182      return false;
183    return LHS == KeyTy(RHS);
184  }
185  static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
186    return LHS == RHS;
187  }
188};
189
190// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
191// shortcut to avoid comparing all operands.
192template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
193  static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
194                     unsigned IDHash, FoldingSetNodeID &TempID) {
195    assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
196    // First, check if the cached hashes match.  If they don't we can skip the
197    // expensive operand walk.
198    if (X.Hash != IDHash)
199      return false;
200
201    // If they match we have to compare the operands.
202    X.Profile(TempID);
203    return TempID == ID;
204  }
205  static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
206    return X.Hash; // Return cached hash.
207  }
208};
209
210/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
211/// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
212class DebugRecVH : public CallbackVH {
213  /// Ctx - This is the LLVM Context being referenced.
214  LLVMContextImpl *Ctx;
215
216  /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
217  /// this reference lives in.  If this is zero, then it represents a
218  /// non-canonical entry that has no DenseMap value.  This can happen due to
219  /// RAUW.
220  int Idx;
221public:
222  DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
223    : CallbackVH(n), Ctx(ctx), Idx(idx) {}
224
225  MDNode *get() const {
226    return cast_or_null<MDNode>(getValPtr());
227  }
228
229  virtual void deleted();
230  virtual void allUsesReplacedWith(Value *VNew);
231};
232
233class LLVMContextImpl {
234public:
235  /// OwnedModules - The set of modules instantiated in this context, and which
236  /// will be automatically deleted if this context is deleted.
237  SmallPtrSet<Module*, 4> OwnedModules;
238
239  LLVMContext::DiagHandlerTy DiagHandler;
240  void *DiagContext;
241
242  typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
243                         DenseMapAPIntKeyInfo> IntMapTy;
244  IntMapTy IntConstants;
245
246  typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
247                         DenseMapAPFloatKeyInfo> FPMapTy;
248  FPMapTy FPConstants;
249
250  FoldingSet<AttributeImpl> AttrsSet;
251  FoldingSet<AttributeSetImpl> AttrsLists;
252
253  StringMap<Value*> MDStringCache;
254
255  FoldingSet<MDNode> MDNodeSet;
256
257  // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
258  // aren't in the MDNodeSet, but they're still shared between objects, so no
259  // one object can destroy them.  This set allows us to at least destroy them
260  // on Context destruction.
261  SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
262
263  DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
264
265  typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
266  ArrayConstantsTy ArrayConstants;
267
268  typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
269  StructConstantsTy StructConstants;
270
271  typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
272  VectorConstantsTy VectorConstants;
273
274  DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
275
276  DenseMap<Type*, UndefValue*> UVConstants;
277
278  StringMap<ConstantDataSequential*> CDSConstants;
279
280
281  DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
282  ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
283    ExprConstants;
284
285  ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
286                    InlineAsm> InlineAsms;
287
288  ConstantInt *TheTrueVal;
289  ConstantInt *TheFalseVal;
290
291  LeakDetectorImpl<Value> LLVMObjects;
292
293  // Basic type instances.
294  Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
295  Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
296  IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
297
298
299  /// TypeAllocator - All dynamically allocated types are allocated from this.
300  /// They live forever until the context is torn down.
301  BumpPtrAllocator TypeAllocator;
302
303  DenseMap<unsigned, IntegerType*> IntegerTypes;
304
305  typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
306  FunctionTypeMap FunctionTypes;
307  typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
308  StructTypeMap AnonStructTypes;
309  StringMap<StructType*> NamedStructTypes;
310  unsigned NamedStructTypesUniqueID;
311
312  DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
313  DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
314  DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
315  DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
316
317
318  /// ValueHandles - This map keeps track of all of the value handles that are
319  /// watching a Value*.  The Value::HasValueHandle bit is used to know
320  // whether or not a value has an entry in this map.
321  typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
322  ValueHandlesTy ValueHandles;
323
324  /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
325  StringMap<unsigned> CustomMDKindNames;
326
327  typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
328  typedef SmallVector<MDPairTy, 2> MDMapTy;
329
330  /// MetadataStore - Collection of per-instruction metadata used in this
331  /// context.
332  DenseMap<const Instruction *, MDMapTy> MetadataStore;
333
334  /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
335  /// entry with no "inlined at" element.
336  DenseMap<MDNode*, int> ScopeRecordIdx;
337
338  /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
339  /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
340  /// the MDNode is RAUW'd.
341  std::vector<DebugRecVH> ScopeRecords;
342
343  /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
344  /// scope/inlined-at pair.
345  DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
346
347  /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
348  /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
349  /// to date.
350  std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
351
352  int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
353  int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
354
355  LLVMContextImpl(LLVMContext &C);
356  ~LLVMContextImpl();
357};
358
359}
360
361#endif
362