blob: 034f3b99979d7624e9900e4cac35087b5a075da6 [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
Owen Andersonafd0c4c2009-08-04 22:41:48 +000018#include "ConstantsContext.h"
Owen Anderson6d549d62009-08-19 17:07:46 +000019#include "LeaksContext.h"
Owen Anderson542cffc2009-08-04 23:33:01 +000020#include "TypesContext.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000021#include "llvm/LLVMContext.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000022#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000023#include "llvm/DerivedTypes.h"
Owen Anderson98015602009-08-17 17:59:35 +000024#include "llvm/Assembly/Writer.h"
Chris Lattnera0566972009-12-29 09:01:33 +000025#include "llvm/Support/ValueHandle.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000026#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000027#include "llvm/ADT/APInt.h"
28#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;
Owen Anderson69ab4162009-07-16 22:11:26 +000038class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000039class MDNode;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000040class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000041class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000042class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000043
44struct DenseMapAPIntKeyInfo {
45 struct KeyTy {
46 APInt val;
47 const Type* type;
48 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
49 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
50 bool operator==(const KeyTy& that) const {
51 return type == that.type && this->val == that.val;
52 }
53 bool operator!=(const KeyTy& that) const {
54 return !this->operator==(that);
55 }
56 };
57 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
58 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
59 static unsigned getHashValue(const KeyTy &Key) {
60 return DenseMapInfo<void*>::getHashValue(Key.type) ^
61 Key.val.getHashValue();
62 }
63 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
64 return LHS == RHS;
65 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000066};
67
Owen Andersonc277dc42009-07-16 19:05:41 +000068struct DenseMapAPFloatKeyInfo {
69 struct KeyTy {
70 APFloat val;
71 KeyTy(const APFloat& V) : val(V){}
72 KeyTy(const KeyTy& that) : val(that.val) {}
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 };
80 static inline KeyTy getEmptyKey() {
81 return KeyTy(APFloat(APFloat::Bogus,1));
82 }
83 static inline KeyTy getTombstoneKey() {
84 return KeyTy(APFloat(APFloat::Bogus,2));
85 }
86 static unsigned getHashValue(const KeyTy &Key) {
87 return Key.val.getHashValue();
88 }
89 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
90 return LHS == RHS;
91 }
Owen Andersonc277dc42009-07-16 19:05:41 +000092};
93
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000094class LLVMContextImpl {
95public:
Owen Anderson20b34ac2009-07-16 18:04:31 +000096 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +000097 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +000098 IntMapTy IntConstants;
99
Owen Andersonc277dc42009-07-16 19:05:41 +0000100 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000101 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000102 FPMapTy FPConstants;
103
Owen Anderson69ab4162009-07-16 22:11:26 +0000104 StringMap<MDString*> MDStringCache;
105
Devang Patelf7188322009-09-03 01:39:20 +0000106 FoldingSet<MDNode> MDNodeSet;
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000107 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
108 // aren't in the MDNodeSet, but they're still shared between objects, so no
109 // one object can destroy them. This set allows us to at least destroy them
110 // on Context destruction.
111 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000112
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000113 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000114
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000115 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
Owen Anderson3d344922009-07-21 20:55:28 +0000116 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000117 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000118
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000119 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
120 ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000121 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000122
Chris Lattner392be582010-02-12 20:49:41 +0000123 typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
124 UnionConstantsTy;
125 UnionConstantsTy UnionConstants;
126
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000127 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
128 ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000129 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000130
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000131 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000132
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000133 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000134
Chris Lattner31b132c2009-10-28 00:01:44 +0000135 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000136 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000137
138 ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000139
Owen Anderson2ad52172009-07-21 02:47:59 +0000140 ConstantInt *TheTrueVal;
141 ConstantInt *TheFalseVal;
142
Owen Anderson6d549d62009-08-19 17:07:46 +0000143 LeakDetectorImpl<Value> LLVMObjects;
144
Dan Gohman97d2cb82009-08-25 16:00:35 +0000145 // Basic type instances.
146 const Type VoidTy;
147 const Type LabelTy;
148 const Type FloatTy;
149 const Type DoubleTy;
150 const Type MetadataTy;
151 const Type X86_FP80Ty;
152 const Type FP128Ty;
153 const Type PPC_FP128Ty;
154 const IntegerType Int1Ty;
155 const IntegerType Int8Ty;
156 const IntegerType Int16Ty;
157 const IntegerType Int32Ty;
158 const IntegerType Int64Ty;
159
Owen Anderson98015602009-08-17 17:59:35 +0000160 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
161 // for types as they are needed. Because resolution of types must invalidate
162 // all of the abstract type descriptions, we keep them in a seperate map to
163 // make this easy.
164 TypePrinting ConcreteTypeDescriptions;
165 TypePrinting AbstractTypeDescriptions;
166
Owen Anderson542cffc2009-08-04 23:33:01 +0000167 TypeMap<ArrayValType, ArrayType> ArrayTypes;
Owen Andersond9186492009-08-04 23:47:44 +0000168 TypeMap<VectorValType, VectorType> VectorTypes;
Owen Andersone5659952009-08-05 00:15:12 +0000169 TypeMap<PointerValType, PointerType> PointerTypes;
Owen Anderson4b5c7612009-08-05 18:13:27 +0000170 TypeMap<FunctionValType, FunctionType> FunctionTypes;
Owen Anderson03cb69f2009-08-05 23:16:16 +0000171 TypeMap<StructValType, StructType> StructTypes;
Chris Lattner392be582010-02-12 20:49:41 +0000172 TypeMap<UnionValType, UnionType> UnionTypes;
Owen Andersona42ac692009-08-13 23:27:32 +0000173 TypeMap<IntegerValType, IntegerType> IntegerTypes;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000174
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000175 // Opaque types are not structurally uniqued, so don't use TypeMap.
176 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
177 OpaqueTypesTy OpaqueTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000178
179 /// Used as an abstract type that will never be resolved.
180 OpaqueType *const AlwaysOpaqueTy;
181
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000182
Owen Andersone8f21852009-08-18 18:28:58 +0000183 /// ValueHandles - This map keeps track of all of the value handles that are
184 /// watching a Value*. The Value::HasValueHandle bit is used to know
185 // whether or not a value has an entry in this map.
186 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
187 ValueHandlesTy ValueHandles;
188
Chris Lattnera0566972009-12-29 09:01:33 +0000189 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
190 StringMap<unsigned> CustomMDKindNames;
191
192 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
193 typedef SmallVector<MDPairTy, 2> MDMapTy;
194
195 /// MetadataStore - Collection of per-instruction metadata used in this
196 /// context.
197 DenseMap<const Instruction *, MDMapTy> MetadataStore;
198
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000199 LLVMContextImpl(LLVMContext &C);
200 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000201};
202
203}
204
Owen Anderson36f62e52009-06-30 17:06:46 +0000205#endif