LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LLVMContextImpl.h
Go to the documentation of this file.
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"
34 #include <vector>
35 
36 namespace llvm {
37 
38 class ConstantInt;
39 class ConstantFP;
40 class LLVMContext;
41 class Type;
42 class Value;
43 
45  struct KeyTy {
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 
70  struct KeyTy {
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 
98  struct KeyTy {
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() {
119  }
120  static inline 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 
142  struct KeyTy {
143  const Type *ReturnType;
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() {
167  }
168  static inline 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.
192 template<> 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.
212 class 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;
221 public:
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 
234 public:
235  /// OwnedModules - The set of modules instantiated in this context, and which
236  /// will be automatically deleted if this context is deleted.
238 
241 
245 
249 
253 
255 
257 
258  // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
259  // aren't in the MDNodeSet, but they're still shared between objects, so no
260  // one object can destroy them. This set allows us to at least destroy them
261  // on Context destruction.
263 
265 
268 
271 
274 
276 
278 
280 
281 
285 
288 
291 
293 
294  // Basic type instances.
298 
299 
300  /// TypeAllocator - All dynamically allocated types are allocated from this.
301  /// They live forever until the context is torn down.
303 
305 
312 
315  DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
317 
318 
319  /// ValueHandles - This map keeps track of all of the value handles that are
320  /// watching a Value*. The Value::HasValueHandle bit is used to know
321  /// whether or not a value has an entry in this map.
324 
325  /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
327 
328  typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
330 
331  /// MetadataStore - Collection of per-instruction metadata used in this
332  /// context.
334 
335  /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
336  /// entry with no "inlined at" element.
338 
339  /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
340  /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
341  /// the MDNode is RAUW'd.
342  std::vector<DebugRecVH> ScopeRecords;
343 
344  /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
345  /// scope/inlined-at pair.
347 
348  /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
349  /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
350  /// to date.
351  std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
352 
353  /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
354  /// requested in this context
357 
358  /// \brief Mapping from a function to its prefix data, which is stored as the
359  /// operand of an unparented ReturnInst so that the prefix data has a Use.
362 
363  int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
364  int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
365 
368 };
369 
370 }
371 
372 #endif
friend hash_code hash_value(const KeyTy &Key)
bool operator!=(const KeyTy &that) const
static bool isEqual(const KeyTy &LHS, const FunctionType *RHS)
KeyTy(const APInt &V, Type *Ty)
DenseMap< unsigned, IntegerType * > IntegerTypes
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
FoldingSet< AttributeImpl > AttrsSet
KeyTy(const Type *R, const ArrayRef< Type * > &P, bool V)
DenseMap< DenseMapAPIntKeyInfo::KeyTy, ConstantInt *, DenseMapAPIntKeyInfo > IntMapTy
static unsigned getHashValue(const KeyTy &Key)
std::vector< std::pair< DebugRecVH, DebugRecVH > > ScopeInlinedAtRecords
KeyTy(const ArrayRef< Type * > &E, bool P)
PrefixDataMapTy PrefixDataMap
static StructType * getTombstoneKey()
bool operator==(const KeyTy &that) const
static bool isEqual(const KeyTy &LHS, const StructType *RHS)
static unsigned getHashValue(const StructType *ST)
ConstantInt * TheFalseVal
SmallVector< MDPairTy, 2 > MDMapTy
friend hash_code hash_value(const KeyTy &Key)
DenseMap< MDNode *, int > ScopeRecordIdx
MDNode - a tuple of other values.
Definition: Metadata.h:69
static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &)
FunctionTypeMap FunctionTypes
DenseMap< std::pair< Type *, unsigned >, PointerType * > ASPointerTypes
LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler
static const fltSemantics Bogus
Definition: APFloat.h:140
std::vector< DebugRecVH > ScopeRecords
DenseMap< std::pair< MDNode *, MDNode * >, int > ScopeInlinedAtIdx
bool bitwiseIsEqual(const APFloat &) const
Bitwise comparison for equality (QNaNs compare equal, 0!=-0).
Definition: APFloat.cpp:755
ConstantInt * TheTrueVal
static bool isEqual(const KeyTy &LHS, const KeyTy &RHS)
DenseMap< FunctionType *, bool, FunctionTypeKeyInfo > FunctionTypeMap
KeyTy(const FunctionType *FT)
FoldingSet< AttributeSetImpl > AttrsLists
DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
DenseMap< std::pair< Type *, uint64_t >, ArrayType * > ArrayTypes
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
BumpPtrAllocator TypeAllocator
This file implements a class to represent arbitrary precision integral constant values and operations...
static unsigned getHashValue(const FunctionType *FT)
static StructType * getEmptyKey()
ConstantAggrUniqueMap< VectorType, ConstantVector > VectorConstantsTy
DenseMap< StructType *, bool, AnonStructTypeKeyInfo > StructTypeMap
LLVMContextImpl(LLVMContext &C)
DenseMap< const Function *, unsigned > IntrinsicIDCacheTy
hash_code hash_value(const APFloat &Arg)
Definition: APFloat.cpp:2814
ArrayConstantsTy ArrayConstants
DenseMap< PointerType *, ConstantPointerNull * > CPNConstants
ConstantAggrUniqueMap< StructType, ConstantStruct > StructConstantsTy
bool operator!=(const KeyTy &that) const
DenseMap< std::pair< Type *, unsigned >, VectorType * > VectorTypes
DenseMap< Type *, ConstantAggregateZero * > CAZConstants
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
std::pair< unsigned, TrackingVH< MDNode > > MDPairTy
#define P(N)
StringMap< unsigned > CustomMDKindNames
CustomMDKindNames - Map to hold the metadata string to ID mapping.
static bool Equals(const MDNode &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
StructTypeMap AnonStructTypes
Value * getValPtr() const
Definition: ValueHandle.h:103
DenseMap< DenseMapAPFloatKeyInfo::KeyTy, ConstantFP *, DenseMapAPFloatKeyInfo > FPMapTy
void Profile(FoldingSetNodeID &ID) const
Definition: Metadata.cpp:312
int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx)
Definition: DebugLoc.cpp:161
hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6)
Definition: Hashing.h:674
FoldingSet< MDNode > MDNodeSet
static bool isEqual(const FunctionType *LHS, const FunctionType *RHS)
DenseMap< const Instruction *, MDMapTy > MetadataStore
LeakDetectorImpl< Value > LLVMObjects
This file declares a class to represent arbitrary precision floating point values and provide a varie...
bool operator!=(const KeyTy &that) const
This file defines various helper methods and classes used by LLVMContextImpl for creating and managin...
ValueHandlesTy ValueHandles
SmallPtrSet< MDNode *, 1 > NonUniquedMDNodes
Integer representation type.
Definition: DerivedTypes.h:37
static FunctionType * getEmptyKey()
static bool isEqual(const KeyTy &LHS, const KeyTy &RHS)
IntrinsicIDCacheTy IntrinsicIDCache
DenseMap< const Function *, ReturnInst * > PrefixDataMapTy
Mapping from a function to its prefix data, which is stored as the operand of an unparented ReturnIns...
ConstantUniqueMap< ExprMapKeyType, const ExprMapKeyType &, Type, ConstantExpr > ExprConstants
VectorConstantsTy VectorConstants
Class for constant integers.
Definition: Constants.h:51
bool operator==(const KeyTy &that) const
static unsigned getHashValue(const KeyTy &Key)
SmallPtrSet< Module *, 4 > OwnedModules
void(* InlineAsmDiagHandlerTy)(const SMDiagnostic &, void *Context, unsigned LocCookie)
Definition: LLVMContext.h:64
static bool isEqual(const StructType *LHS, const StructType *RHS)
Class for arbitrary precision integers.
Definition: APInt.h:75
bool operator!=(const KeyTy &that) const
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:487
An opaque object representing a hash code.
Definition: Hashing.h:79
DenseMap< Type *, UndefValue * > UVConstants
virtual void allUsesReplacedWith(Value *VNew)
Definition: DebugLoc.cpp:256
static FunctionType * getTombstoneKey()
DenseMap< Value *, ValueHandleBase * > ValueHandlesTy
StringMap< Value * > MDStringCache
StringMap< StructType * > NamedStructTypes
static KeyTy getTombstoneKey()
#define N
ConstantUniqueMap< InlineAsmKeyType, const InlineAsmKeyType &, PointerType, InlineAsm > InlineAsms
bool operator==(const KeyTy &that) const
DenseMap< std::pair< Function *, BasicBlock * >, BlockAddress * > BlockAddresses
StructConstantsTy StructConstants
LLVM Value Representation.
Definition: Value.h:66
ConstantAggrUniqueMap< ArrayType, ConstantArray > ArrayConstantsTy
FoldingSet< AttributeSetNode > AttrsSetNodes
static unsigned getHashValue(const KeyTy &Key)
DenseMap< Type *, PointerType * > PointerTypes
virtual void deleted()
Definition: DebugLoc.cpp:213
StringMap< ConstantDataSequential * > CDSConstants
bool operator==(const KeyTy &that) const
int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA, int ExistingIdx)
Definition: DebugLoc.cpp:184
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
static unsigned getHashValue(const KeyTy &Key)