blob: ca37ffa9faf7814591fd172910bade0d8be48249 [file] [log] [blame]
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001//===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file declares LLVMContextImpl, the opaque implementation
11// of LLVMContext.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#ifndef LLVM_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_H
17
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000018#include "llvm/LLVMContext.h"
Owen Andersonafd0c4c2009-08-04 22:41:48 +000019#include "ConstantsContext.h"
Owen Anderson6d549d62009-08-19 17:07:46 +000020#include "LeaksContext.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000021#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000022#include "llvm/DerivedTypes.h"
Jeffrey Yasskincd3706c2010-03-21 22:08:41 +000023#include "llvm/Metadata.h"
Chris Lattnera0566972009-12-29 09:01:33 +000024#include "llvm/Support/ValueHandle.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000025#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000026#include "llvm/ADT/APInt.h"
Jay Foadc365eea2011-06-22 08:50:06 +000027#include "llvm/ADT/ArrayRef.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000028#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000029#include "llvm/ADT/FoldingSet.h"
Jeffrey Yasskin28f24482009-12-17 19:55:06 +000030#include "llvm/ADT/SmallPtrSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000031#include "llvm/ADT/StringMap.h"
Owen Anderson909f6002009-07-23 23:25:33 +000032#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000033
Owen Anderson20b34ac2009-07-16 18:04:31 +000034namespace llvm {
Owen Andersonedb4a702009-07-24 23:12:02 +000035
Owen Anderson20b34ac2009-07-16 18:04:31 +000036class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000037class ConstantFP;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000038class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000039class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000040class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000041
42struct DenseMapAPIntKeyInfo {
43 struct KeyTy {
44 APInt val;
Chris Lattner229907c2011-07-18 04:54:35 +000045 Type* type;
46 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
Owen Anderson20b34ac2009-07-16 18:04:31 +000047 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
48 bool operator==(const KeyTy& that) const {
49 return type == that.type && this->val == that.val;
50 }
51 bool operator!=(const KeyTy& that) const {
52 return !this->operator==(that);
53 }
54 };
55 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
56 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
57 static unsigned getHashValue(const KeyTy &Key) {
58 return DenseMapInfo<void*>::getHashValue(Key.type) ^
59 Key.val.getHashValue();
60 }
61 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
62 return LHS == RHS;
63 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000064};
65
Owen Andersonc277dc42009-07-16 19:05:41 +000066struct DenseMapAPFloatKeyInfo {
67 struct KeyTy {
68 APFloat val;
69 KeyTy(const APFloat& V) : val(V){}
70 KeyTy(const KeyTy& that) : val(that.val) {}
71 bool operator==(const KeyTy& that) const {
72 return this->val.bitwiseIsEqual(that.val);
73 }
74 bool operator!=(const KeyTy& that) const {
75 return !this->operator==(that);
76 }
77 };
78 static inline KeyTy getEmptyKey() {
79 return KeyTy(APFloat(APFloat::Bogus,1));
80 }
81 static inline KeyTy getTombstoneKey() {
82 return KeyTy(APFloat(APFloat::Bogus,2));
83 }
84 static unsigned getHashValue(const KeyTy &Key) {
85 return Key.val.getHashValue();
86 }
87 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
88 return LHS == RHS;
89 }
Owen Andersonc277dc42009-07-16 19:05:41 +000090};
91
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000092/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
93/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
94class DebugRecVH : public CallbackVH {
95 /// Ctx - This is the LLVM Context being referenced.
96 LLVMContextImpl *Ctx;
97
98 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
99 /// this reference lives in. If this is zero, then it represents a
100 /// non-canonical entry that has no DenseMap value. This can happen due to
101 /// RAUW.
102 int Idx;
103public:
104 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
105 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
106
107 MDNode *get() const {
108 return cast_or_null<MDNode>(getValPtr());
109 }
110
111 virtual void deleted();
112 virtual void allUsesReplacedWith(Value *VNew);
113};
114
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +0000115class LLVMContextImpl {
116public:
Owen Anderson8e89e412010-09-08 18:03:32 +0000117 /// OwnedModules - The set of modules instantiated in this context, and which
118 /// will be automatically deleted if this context is deleted.
119 SmallPtrSet<Module*, 4> OwnedModules;
120
Chris Lattnerb0e36082010-11-17 08:13:01 +0000121 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
122 void *InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000123
Owen Anderson20b34ac2009-07-16 18:04:31 +0000124 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000125 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000126 IntMapTy IntConstants;
127
Owen Andersonc277dc42009-07-16 19:05:41 +0000128 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000129 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000130 FPMapTy FPConstants;
131
Owen Anderson69ab4162009-07-16 22:11:26 +0000132 StringMap<MDString*> MDStringCache;
133
Devang Patelf7188322009-09-03 01:39:20 +0000134 FoldingSet<MDNode> MDNodeSet;
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000135 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
136 // aren't in the MDNodeSet, but they're still shared between objects, so no
137 // one object can destroy them. This set allows us to at least destroy them
138 // on Context destruction.
139 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000140
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000141 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000142
Jay Foadc365eea2011-06-22 08:50:06 +0000143 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
144 ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000145 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000146
Jay Foadc365eea2011-06-22 08:50:06 +0000147 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
148 StructType, ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000149 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000150
Jay Foadc365eea2011-06-22 08:50:06 +0000151 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
152 VectorType, ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000153 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000154
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000155 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
156
157 DenseMap<Type*, UndefValue*> UVConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000158
Chris Lattner3756b912012-01-23 22:57:10 +0000159 StringMap<ConstantDataSequential*> CDSConstants;
160
161
Chris Lattner31b132c2009-10-28 00:01:44 +0000162 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jay Foadc365eea2011-06-22 08:50:06 +0000163 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
164 ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000165
Jay Foadc365eea2011-06-22 08:50:06 +0000166 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
167 InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000168
Owen Anderson2ad52172009-07-21 02:47:59 +0000169 ConstantInt *TheTrueVal;
170 ConstantInt *TheFalseVal;
171
Owen Anderson6d549d62009-08-19 17:07:46 +0000172 LeakDetectorImpl<Value> LLVMObjects;
173
Dan Gohman97d2cb82009-08-25 16:00:35 +0000174 // Basic type instances.
Dan Gohman518cda42011-12-17 00:04:22 +0000175 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000176 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
177 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000178
Chris Lattner07bd69c2011-07-15 05:49:15 +0000179
180 /// TypeAllocator - All dynamically allocated types are allocated from this.
181 /// They live forever until the context is torn down.
182 BumpPtrAllocator TypeAllocator;
183
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000184 DenseMap<unsigned, IntegerType*> IntegerTypes;
185
186 // TODO: Optimize FunctionTypes/AnonStructTypes!
187 std::map<std::vector<Type*>, FunctionType*> FunctionTypes;
188 std::map<std::vector<Type*>, StructType*> AnonStructTypes;
189 StringMap<StructType*> NamedStructTypes;
190 unsigned NamedStructTypesUniqueID;
191
192 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
193 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
194 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
195 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000196
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000197
Owen Andersone8f21852009-08-18 18:28:58 +0000198 /// ValueHandles - This map keeps track of all of the value handles that are
199 /// watching a Value*. The Value::HasValueHandle bit is used to know
200 // whether or not a value has an entry in this map.
201 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
202 ValueHandlesTy ValueHandles;
203
Chris Lattnera0566972009-12-29 09:01:33 +0000204 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
205 StringMap<unsigned> CustomMDKindNames;
206
207 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
208 typedef SmallVector<MDPairTy, 2> MDMapTy;
209
210 /// MetadataStore - Collection of per-instruction metadata used in this
211 /// context.
212 DenseMap<const Instruction *, MDMapTy> MetadataStore;
213
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000214 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
215 /// entry with no "inlined at" element.
216 DenseMap<MDNode*, int> ScopeRecordIdx;
217
218 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
219 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
220 /// the MDNode is RAUW'd.
221 std::vector<DebugRecVH> ScopeRecords;
222
223 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
224 /// scope/inlined-at pair.
225 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
226
227 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
228 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
229 /// to date.
230 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
231
232 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
233 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
234
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000235 LLVMContextImpl(LLVMContext &C);
236 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000237};
238
239}
240
Owen Anderson36f62e52009-06-30 17:06:46 +0000241#endif