blob: 9c48fbe73019f8499c3c5904a44c5176aeb069e0 [file] [log] [blame]
Owen Anderson20b34ac2009-07-16 18:04:31 +00001//===----------------- LLVMContextImpl.h - Implementation ------*- 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 Anderson542cffc2009-08-04 23:33:01 +000019#include "TypesContext.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000020#include "llvm/LLVMContext.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"
Owen Anderson20b34ac2009-07-16 18:04:31 +000023#include "llvm/System/RWMutex.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000024#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000025#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000027#include "llvm/ADT/FoldingSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000028#include "llvm/ADT/StringMap.h"
Owen Anderson909f6002009-07-23 23:25:33 +000029#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000030
Owen Anderson20b34ac2009-07-16 18:04:31 +000031namespace llvm {
Owen Andersonedb4a702009-07-24 23:12:02 +000032
Owen Anderson20b34ac2009-07-16 18:04:31 +000033class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000034class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +000035class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000036class MDNode;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000037class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000038class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000039class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000040
41struct DenseMapAPIntKeyInfo {
42 struct KeyTy {
43 APInt val;
44 const Type* type;
45 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
46 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
47 bool operator==(const KeyTy& that) const {
48 return type == that.type && this->val == that.val;
49 }
50 bool operator!=(const KeyTy& that) const {
51 return !this->operator==(that);
52 }
53 };
54 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
55 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
56 static unsigned getHashValue(const KeyTy &Key) {
57 return DenseMapInfo<void*>::getHashValue(Key.type) ^
58 Key.val.getHashValue();
59 }
60 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
61 return LHS == RHS;
62 }
63 static bool isPod() { return false; }
64};
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 }
90 static bool isPod() { return false; }
91};
92
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000093class LLVMContextImpl {
94public:
Owen Anderson20b34ac2009-07-16 18:04:31 +000095 sys::SmartRWMutex<true> ConstantsLock;
96
97 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +000098 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +000099 IntMapTy IntConstants;
100
Owen Andersonc277dc42009-07-16 19:05:41 +0000101 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000102 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000103 FPMapTy FPConstants;
104
Owen Anderson69ab4162009-07-16 22:11:26 +0000105 StringMap<MDString*> MDStringCache;
106
Owen Andersonedb4a702009-07-24 23:12:02 +0000107 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000108
Devang Patelc5aa8c62009-08-11 06:31:57 +0000109 typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/>
110 MDNodeMapTy;
111
112 MDNodeMapTy MDNodes;
Owen Anderson3d344922009-07-21 20:55:28 +0000113
114 typedef ValueMap<std::vector<Constant*>, ArrayType,
115 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000116 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000117
Owen Anderson909f6002009-07-23 23:25:33 +0000118 typedef ValueMap<std::vector<Constant*>, StructType,
119 ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000120 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000121
Owen Anderson0348a132009-07-24 00:36:24 +0000122 typedef ValueMap<std::vector<Constant*>, VectorType,
123 ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000124 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000125
Owen Andersonc8c30262009-07-31 22:45:43 +0000126 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
127
128 ValueMap<char, Type, UndefValue> UndefValueConstants;
129
Owen Anderson1584a292009-08-04 20:25:11 +0000130 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
131
Owen Anderson2ad52172009-07-21 02:47:59 +0000132 ConstantInt *TheTrueVal;
133 ConstantInt *TheFalseVal;
134
Owen Anderson542cffc2009-08-04 23:33:01 +0000135 TypeMap<ArrayValType, ArrayType> ArrayTypes;
Owen Andersond9186492009-08-04 23:47:44 +0000136 TypeMap<VectorValType, VectorType> VectorTypes;
Owen Andersone5659952009-08-05 00:15:12 +0000137 TypeMap<PointerValType, PointerType> PointerTypes;
Owen Anderson4b5c7612009-08-05 18:13:27 +0000138 TypeMap<FunctionValType, FunctionType> FunctionTypes;
Owen Anderson03cb69f2009-08-05 23:16:16 +0000139 TypeMap<StructValType, StructType> StructTypes;
Owen Andersona42ac692009-08-13 23:27:32 +0000140 TypeMap<IntegerValType, IntegerType> IntegerTypes;
Owen Anderson542cffc2009-08-04 23:33:01 +0000141
Owen Andersona42ac692009-08-13 23:27:32 +0000142 const Type *VoidTy;
143 const Type *LabelTy;
144 const Type *FloatTy;
145 const Type *DoubleTy;
146 const Type *MetadataTy;
147 const Type *X86_FP80Ty;
148 const Type *FP128Ty;
149 const Type *PPC_FP128Ty;
150
151 const IntegerType *Int1Ty;
152 const IntegerType *Int8Ty;
153 const IntegerType *Int16Ty;
154 const IntegerType *Int32Ty;
155 const IntegerType *Int64Ty;
156
157 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
158 VoidTy(new Type(C, Type::VoidTyID)),
159 LabelTy(new Type(C, Type::LabelTyID)),
160 FloatTy(new Type(C, Type::FloatTyID)),
161 DoubleTy(new Type(C, Type::DoubleTyID)),
162 MetadataTy(new Type(C, Type::MetadataTyID)),
163 X86_FP80Ty(new Type(C, Type::X86_FP80TyID)),
164 FP128Ty(new Type(C, Type::FP128TyID)),
165 PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)),
166 Int1Ty(new IntegerType(C, 1)),
167 Int8Ty(new IntegerType(C, 8)),
168 Int16Ty(new IntegerType(C, 16)),
169 Int32Ty(new IntegerType(C, 32)),
170 Int64Ty(new IntegerType(C, 64)) { }
171
172 ~LLVMContextImpl() {
173 // In principle, we should delete the member types here. However,
174 // this causes destruction order issues with the types in the TypeMaps.
175 // For now, just leak this, which is at least not a regression from the
176 // previous behavior, though still undesirable.
177#if 0
178 delete VoidTy;
179 delete LabelTy;
180 delete FloatTy;
181 delete DoubleTy;
182 delete MetadataTy;
183 delete X86_FP80Ty;
184 delete FP128Ty;
185 delete PPC_FP128Ty;
186
187 delete Int1Ty;
188 delete Int8Ty;
189 delete Int16Ty;
190 delete Int32Ty;
191 delete Int64Ty;
192#endif
193 }
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000194};
195
196}
197
Owen Anderson36f62e52009-06-30 17:06:46 +0000198#endif