blob: ca8149e402234ed87a38bd4f6bc3270eca64cd44 [file] [log] [blame]
Chris Lattner169726b2003-09-05 02:21:39 +00001//===-- Type.cpp - Implement the Type class -------------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Type class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer6e885d02004-07-04 12:14:17 +000014#include "llvm/AbstractTypeUser.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/DerivedTypes.h"
Chris Lattnerc038a2f2001-09-07 16:56:42 +000016#include "llvm/SymbolTable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/DepthFirstIterator.h"
19#include "llvm/ADT/StringExtras.h"
Chris Lattnera6b10b62004-10-07 19:20:48 +000020#include "llvm/ADT/SCCIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattnerd115ef82005-11-10 01:40:59 +000022#include "llvm/Support/MathExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattnerde65fb32006-09-28 23:38:07 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner417081c2002-04-07 06:14:56 +000025#include <algorithm>
Chris Lattnerf2586d12003-11-19 06:14:38 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerc038a2f2001-09-07 16:56:42 +000028// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
29// created and later destroyed, all in an effort to make sure that there is only
Chris Lattner065a6162003-09-10 05:29:43 +000030// a single canonical version of a type.
Chris Lattnerc038a2f2001-09-07 16:56:42 +000031//
32//#define DEBUG_MERGE_TYPES 1
33
Chris Lattner1cd4c722004-02-26 07:24:18 +000034AbstractTypeUser::~AbstractTypeUser() {}
Chris Lattnerc038a2f2001-09-07 16:56:42 +000035
Jim Laskeya25dfd22006-07-25 23:22:00 +000036
37//===----------------------------------------------------------------------===//
38// Type PATypeHolder Implementation
39//===----------------------------------------------------------------------===//
40
Jim Laskeya25dfd22006-07-25 23:22:00 +000041/// get - This implements the forwarding part of the union-find algorithm for
42/// abstract types. Before every access to the Type*, we check to see if the
43/// type we are pointing to is forwarding to a new type. If so, we drop our
44/// reference to the type.
45///
46Type* PATypeHolder::get() const {
47 const Type *NewTy = Ty->getForwardedType();
48 if (!NewTy) return const_cast<Type*>(Ty);
49 return *const_cast<PATypeHolder*>(this) = NewTy;
50}
51
Chris Lattner00950542001-06-06 20:29:01 +000052//===----------------------------------------------------------------------===//
53// Type Class Implementation
54//===----------------------------------------------------------------------===//
55
Chris Lattnerdd4b4212003-09-02 16:35:17 +000056// Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
57// for types as they are needed. Because resolution of types must invalidate
58// all of the abstract type descriptions, we keep them in a seperate map to make
59// this easy.
Chris Lattnerde65fb32006-09-28 23:38:07 +000060static ManagedStatic<std::map<const Type*,
61 std::string> > ConcreteTypeDescriptions;
62static ManagedStatic<std::map<const Type*,
63 std::string> > AbstractTypeDescriptions;
Chris Lattnerdd4b4212003-09-02 16:35:17 +000064
Chris Lattnercfe82272005-11-13 03:14:09 +000065Type::Type(const char *Name, TypeID id)
66 : ID(id), Abstract(false), RefCount(0), ForwardType(0) {
67 assert(Name && Name[0] && "Should use other ctor if no name!");
Chris Lattnerde65fb32006-09-28 23:38:07 +000068 (*ConcreteTypeDescriptions)[this] = Name;
Chris Lattner00950542001-06-06 20:29:01 +000069}
70
Chris Lattnercfe82272005-11-13 03:14:09 +000071
Chris Lattnerf70c22b2004-06-17 18:19:28 +000072const Type *Type::getPrimitiveType(TypeID IDNumber) {
Chris Lattner00950542001-06-06 20:29:01 +000073 switch (IDNumber) {
74 case VoidTyID : return VoidTy;
75 case BoolTyID : return BoolTy;
76 case UByteTyID : return UByteTy;
77 case SByteTyID : return SByteTy;
78 case UShortTyID: return UShortTy;
79 case ShortTyID : return ShortTy;
80 case UIntTyID : return UIntTy;
81 case IntTyID : return IntTy;
82 case ULongTyID : return ULongTy;
83 case LongTyID : return LongTy;
84 case FloatTyID : return FloatTy;
85 case DoubleTyID: return DoubleTy;
Chris Lattner00950542001-06-06 20:29:01 +000086 case LabelTyID : return LabelTy;
Chris Lattner00950542001-06-06 20:29:01 +000087 default:
88 return 0;
89 }
90}
91
Chris Lattner2e1c1962006-10-26 18:22:45 +000092/// isFPOrFPVector - Return true if this is a FP type or a vector of FP types.
93///
94bool Type::isFPOrFPVector() const {
95 if (ID == Type::FloatTyID || ID == Type::DoubleTyID) return true;
96 if (ID != Type::PackedTyID) return false;
97
98 return cast<PackedType>(this)->getElementType()->isFloatingPoint();
99}
100
Reid Spencer3da59db2006-11-27 01:05:10 +0000101// canLosslesllyBitCastTo - Return true if this type can be converted to
Chris Lattner58716b92001-11-26 17:01:47 +0000102// 'Ty' without any reinterpretation of bits. For example, uint to int.
103//
Reid Spencer3da59db2006-11-27 01:05:10 +0000104bool Type::canLosslesslyBitCastTo(const Type *Ty) const {
105 // Identity cast means no change so return true
106 if (this == Ty)
Chris Lattnera3124a32006-04-02 05:40:28 +0000107 return true;
108
Reid Spencer3da59db2006-11-27 01:05:10 +0000109 // They are not convertible unless they are at least first class types
110 if (!this->isFirstClassType() || !Ty->isFirstClassType())
111 return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000112
Reid Spencer3da59db2006-11-27 01:05:10 +0000113 // Packed -> Packed conversions are always lossless if the two packed types
114 // have the same size, otherwise not.
115 if (const PackedType *thisPTy = dyn_cast<PackedType>(this))
116 if (const PackedType *thatPTy = dyn_cast<PackedType>(Ty))
117 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
Chris Lattner58716b92001-11-26 17:01:47 +0000118
Reid Spencer3da59db2006-11-27 01:05:10 +0000119 // At this point we have only various mismatches of the first class types
120 // remaining and ptr->ptr. Just select the lossless conversions. Everything
121 // else is not lossless.
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000122 switch (getTypeID()) {
Chris Lattner58716b92001-11-26 17:01:47 +0000123 case Type::UByteTyID: return Ty == Type::SByteTy;
124 case Type::SByteTyID: return Ty == Type::UByteTy;
125 case Type::UShortTyID: return Ty == Type::ShortTy;
126 case Type::ShortTyID: return Ty == Type::UShortTy;
127 case Type::UIntTyID: return Ty == Type::IntTy;
128 case Type::IntTyID: return Ty == Type::UIntTy;
Chris Lattner5bc3e312003-11-03 18:44:58 +0000129 case Type::ULongTyID: return Ty == Type::LongTy;
130 case Type::LongTyID: return Ty == Type::ULongTy;
131 case Type::PointerTyID: return isa<PointerType>(Ty);
Chris Lattner58716b92001-11-26 17:01:47 +0000132 default:
Reid Spencer3da59db2006-11-27 01:05:10 +0000133 break;
Chris Lattner58716b92001-11-26 17:01:47 +0000134 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000135 return false; // Other types have no identity values
Chris Lattner58716b92001-11-26 17:01:47 +0000136}
137
Chris Lattnere3651f02004-03-26 21:43:22 +0000138/// getUnsignedVersion - If this is an integer type, return the unsigned
139/// variant of this type. For example int -> uint.
140const Type *Type::getUnsignedVersion() const {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000141 switch (getTypeID()) {
Chris Lattnere3651f02004-03-26 21:43:22 +0000142 default:
143 assert(isInteger()&&"Type::getUnsignedVersion is only valid for integers!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000144 case Type::UByteTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000145 case Type::SByteTyID: return Type::UByteTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000146 case Type::UShortTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000147 case Type::ShortTyID: return Type::UShortTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000148 case Type::UIntTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000149 case Type::IntTyID: return Type::UIntTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000150 case Type::ULongTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000151 case Type::LongTyID: return Type::ULongTy;
152 }
153}
154
155/// getSignedVersion - If this is an integer type, return the signed variant
156/// of this type. For example uint -> int.
157const Type *Type::getSignedVersion() const {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000158 switch (getTypeID()) {
Chris Lattnere3651f02004-03-26 21:43:22 +0000159 default:
160 assert(isInteger() && "Type::getSignedVersion is only valid for integers!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000161 case Type::UByteTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000162 case Type::SByteTyID: return Type::SByteTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000163 case Type::UShortTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000164 case Type::ShortTyID: return Type::ShortTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000165 case Type::UIntTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000166 case Type::IntTyID: return Type::IntTy;
Misha Brukmanfd939082005-04-21 23:48:37 +0000167 case Type::ULongTyID:
Chris Lattnere3651f02004-03-26 21:43:22 +0000168 case Type::LongTyID: return Type::LongTy;
169 }
170}
171
172
Misha Brukman6b634522003-10-10 17:54:14 +0000173// getPrimitiveSize - Return the basic size of this type if it is a primitive
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000174// type. These are fixed by LLVM and are not target dependent. This will
Chris Lattnerd44023e2002-05-06 16:14:39 +0000175// return zero if the type does not have a size or is not a primitive type.
176//
177unsigned Type::getPrimitiveSize() const {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000178 switch (getTypeID()) {
Chris Lattnerea104912005-04-23 22:01:39 +0000179 case Type::BoolTyID:
180 case Type::SByteTyID:
181 case Type::UByteTyID: return 1;
182 case Type::UShortTyID:
183 case Type::ShortTyID: return 2;
Jeff Cohen00b168892005-07-27 06:12:32 +0000184 case Type::FloatTyID:
185 case Type::IntTyID:
Chris Lattnerea104912005-04-23 22:01:39 +0000186 case Type::UIntTyID: return 4;
187 case Type::LongTyID:
188 case Type::ULongTyID:
189 case Type::DoubleTyID: return 8;
Chris Lattner4f0247c2005-04-23 22:00:09 +0000190 default: return 0;
191 }
192}
193
194unsigned Type::getPrimitiveSizeInBits() const {
195 switch (getTypeID()) {
Chris Lattnerea104912005-04-23 22:01:39 +0000196 case Type::BoolTyID: return 1;
197 case Type::SByteTyID:
198 case Type::UByteTyID: return 8;
199 case Type::UShortTyID:
200 case Type::ShortTyID: return 16;
Jeff Cohen00b168892005-07-27 06:12:32 +0000201 case Type::FloatTyID:
Chris Lattnerea104912005-04-23 22:01:39 +0000202 case Type::IntTyID:
203 case Type::UIntTyID: return 32;
204 case Type::LongTyID:
205 case Type::ULongTyID:
206 case Type::DoubleTyID: return 64;
Reid Spencer3da59db2006-11-27 01:05:10 +0000207 case Type::PackedTyID: {
208 const PackedType *PTy = cast<PackedType>(this);
209 return PTy->getBitWidth();
210 }
Chris Lattnerd44023e2002-05-06 16:14:39 +0000211 default: return 0;
212 }
213}
214
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000215/// isSizedDerivedType - Derived types like structures and arrays are sized
216/// iff all of the members of the type are sized as well. Since asking for
217/// their size is relatively uncommon, move this operation out of line.
218bool Type::isSizedDerivedType() const {
219 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
220 return ATy->getElementType()->isSized();
221
Chris Lattnerf0228052004-12-01 17:12:16 +0000222 if (const PackedType *PTy = dyn_cast<PackedType>(this))
223 return PTy->getElementType()->isSized();
224
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000225 if (!isa<StructType>(this)) return false;
226
227 // Okay, our struct is sized if all of the elements are...
228 for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
229 if (!(*I)->isSized()) return false;
230
231 return true;
232}
Chris Lattner58716b92001-11-26 17:01:47 +0000233
Chris Lattner1c5164e2003-10-02 23:35:57 +0000234/// getForwardedTypeInternal - This method is used to implement the union-find
235/// algorithm for when a type is being forwarded to another type.
236const Type *Type::getForwardedTypeInternal() const {
237 assert(ForwardType && "This type is not being forwarded to another type!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000238
Chris Lattner1c5164e2003-10-02 23:35:57 +0000239 // Check to see if the forwarded type has been forwarded on. If so, collapse
240 // the forwarding links.
241 const Type *RealForwardedType = ForwardType->getForwardedType();
242 if (!RealForwardedType)
243 return ForwardType; // No it's not forwarded again
244
245 // Yes, it is forwarded again. First thing, add the reference to the new
246 // forward type.
247 if (RealForwardedType->isAbstract())
248 cast<DerivedType>(RealForwardedType)->addRef();
249
250 // Now drop the old reference. This could cause ForwardType to get deleted.
251 cast<DerivedType>(ForwardType)->dropRef();
Misha Brukmanfd939082005-04-21 23:48:37 +0000252
Chris Lattner1c5164e2003-10-02 23:35:57 +0000253 // Return the updated type.
254 ForwardType = RealForwardedType;
255 return ForwardType;
256}
257
Chris Lattnerc35abc22005-11-13 03:26:33 +0000258void Type::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
259 abort();
260}
261void Type::typeBecameConcrete(const DerivedType *AbsTy) {
262 abort();
263}
264
265
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000266// getTypeDescription - This is a recursive function that walks a type hierarchy
267// calculating the description for a type.
268//
269static std::string getTypeDescription(const Type *Ty,
270 std::vector<const Type *> &TypeStack) {
271 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
272 std::map<const Type*, std::string>::iterator I =
Chris Lattnerde65fb32006-09-28 23:38:07 +0000273 AbstractTypeDescriptions->lower_bound(Ty);
274 if (I != AbstractTypeDescriptions->end() && I->first == Ty)
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000275 return I->second;
Chris Lattner51662c72004-07-08 22:31:09 +0000276 std::string Desc = "opaque";
Chris Lattnerde65fb32006-09-28 23:38:07 +0000277 AbstractTypeDescriptions->insert(std::make_pair(Ty, Desc));
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000278 return Desc;
279 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000280
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000281 if (!Ty->isAbstract()) { // Base case for the recursion
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000282 std::map<const Type*, std::string>::iterator I =
Chris Lattnerde65fb32006-09-28 23:38:07 +0000283 ConcreteTypeDescriptions->find(Ty);
284 if (I != ConcreteTypeDescriptions->end()) return I->second;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000285 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000286
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000287 // Check to see if the Type is already on the stack...
288 unsigned Slot = 0, CurSize = TypeStack.size();
289 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Misha Brukmanfd939082005-04-21 23:48:37 +0000290
291 // This is another base case for the recursion. In this case, we know
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000292 // that we have looped back to a type that we have previously visited.
293 // Generate the appropriate upreference to handle this.
Misha Brukmanfd939082005-04-21 23:48:37 +0000294 //
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000295 if (Slot < CurSize)
296 return "\\" + utostr(CurSize-Slot); // Here's the upreference
297
298 // Recursive case: derived types...
299 std::string Result;
300 TypeStack.push_back(Ty); // Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000301
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000302 switch (Ty->getTypeID()) {
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000303 case Type::FunctionTyID: {
304 const FunctionType *FTy = cast<FunctionType>(Ty);
305 Result = getTypeDescription(FTy->getReturnType(), TypeStack) + " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000306 for (FunctionType::param_iterator I = FTy->param_begin(),
307 E = FTy->param_end(); I != E; ++I) {
308 if (I != FTy->param_begin())
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000309 Result += ", ";
310 Result += getTypeDescription(*I, TypeStack);
311 }
312 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000313 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000314 Result += "...";
315 }
316 Result += ")";
317 break;
318 }
319 case Type::StructTyID: {
320 const StructType *STy = cast<StructType>(Ty);
321 Result = "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000322 for (StructType::element_iterator I = STy->element_begin(),
323 E = STy->element_end(); I != E; ++I) {
324 if (I != STy->element_begin())
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000325 Result += ", ";
326 Result += getTypeDescription(*I, TypeStack);
327 }
328 Result += " }";
329 break;
330 }
331 case Type::PointerTyID: {
332 const PointerType *PTy = cast<PointerType>(Ty);
333 Result = getTypeDescription(PTy->getElementType(), TypeStack) + " *";
334 break;
335 }
336 case Type::ArrayTyID: {
337 const ArrayType *ATy = cast<ArrayType>(Ty);
338 unsigned NumElements = ATy->getNumElements();
339 Result = "[";
340 Result += utostr(NumElements) + " x ";
341 Result += getTypeDescription(ATy->getElementType(), TypeStack) + "]";
342 break;
343 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000344 case Type::PackedTyID: {
345 const PackedType *PTy = cast<PackedType>(Ty);
346 unsigned NumElements = PTy->getNumElements();
347 Result = "<";
348 Result += utostr(NumElements) + " x ";
349 Result += getTypeDescription(PTy->getElementType(), TypeStack) + ">";
350 break;
351 }
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000352 default:
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000353 Result = "<error>";
Chris Lattnerd8d6c762003-09-02 22:50:02 +0000354 assert(0 && "Unhandled type in getTypeDescription!");
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000355 }
356
357 TypeStack.pop_back(); // Remove self from stack...
358
Chris Lattnera3057e82003-09-04 23:41:03 +0000359 return Result;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000360}
361
362
363
364static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
365 const Type *Ty) {
366 std::map<const Type*, std::string>::iterator I = Map.find(Ty);
367 if (I != Map.end()) return I->second;
Misha Brukmanfd939082005-04-21 23:48:37 +0000368
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000369 std::vector<const Type *> TypeStack;
Chris Lattnerded36132005-03-02 03:54:43 +0000370 std::string Result = getTypeDescription(Ty, TypeStack);
371 return Map[Ty] = Result;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000372}
373
374
375const std::string &Type::getDescription() const {
376 if (isAbstract())
Chris Lattnerde65fb32006-09-28 23:38:07 +0000377 return getOrCreateDesc(*AbstractTypeDescriptions, this);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000378 else
Chris Lattnerde65fb32006-09-28 23:38:07 +0000379 return getOrCreateDesc(*ConcreteTypeDescriptions, this);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000380}
381
382
Chris Lattner58716b92001-11-26 17:01:47 +0000383bool StructType::indexValid(const Value *V) const {
Chris Lattnerb9da9c12003-11-25 21:21:46 +0000384 // Structure indexes require unsigned integer constants.
Chris Lattner28977af2004-04-05 01:30:19 +0000385 if (V->getType() == Type::UIntTy)
Reid Spencerb83eb642006-10-20 07:07:24 +0000386 if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
387 return CU->getZExtValue() < ContainedTys.size();
Chris Lattnerb9da9c12003-11-25 21:21:46 +0000388 return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000389}
390
391// getTypeAtIndex - Given an index value into the type, return the type of the
392// element. For a structure type, this must be a constant value...
393//
394const Type *StructType::getTypeAtIndex(const Value *V) const {
Chris Lattner28977af2004-04-05 01:30:19 +0000395 assert(indexValid(V) && "Invalid structure index!");
Reid Spencerb83eb642006-10-20 07:07:24 +0000396 unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
Chris Lattnerf32f5682004-02-09 05:40:24 +0000397 return ContainedTys[Idx];
Chris Lattner58716b92001-11-26 17:01:47 +0000398}
399
400
Chris Lattner00950542001-06-06 20:29:01 +0000401//===----------------------------------------------------------------------===//
Chris Lattnerab4fa4f2006-09-28 23:45:00 +0000402// Primitive 'Type' data
Chris Lattner00950542001-06-06 20:29:01 +0000403//===----------------------------------------------------------------------===//
404
Chris Lattnerab4fa4f2006-09-28 23:45:00 +0000405#define DeclarePrimType(TY, Str) \
406 namespace { \
407 struct VISIBILITY_HIDDEN TY##Type : public Type { \
408 TY##Type() : Type(Str, Type::TY##TyID) {} \
409 }; \
410 } \
411 static ManagedStatic<TY##Type> The##TY##Ty; \
412 Type *Type::TY##Ty = &*The##TY##Ty
Chris Lattnerde65fb32006-09-28 23:38:07 +0000413
Chris Lattnerab4fa4f2006-09-28 23:45:00 +0000414DeclarePrimType(Void, "void");
415DeclarePrimType(Bool, "bool");
416DeclarePrimType(SByte, "sbyte");
417DeclarePrimType(UByte, "ubyte");
418DeclarePrimType(Short, "short");
419DeclarePrimType(UShort, "ushort");
420DeclarePrimType(Int, "int");
421DeclarePrimType(UInt, "uint");
422DeclarePrimType(Long, "long");
423DeclarePrimType(ULong, "ulong");
424DeclarePrimType(Float, "float");
425DeclarePrimType(Double, "double");
426DeclarePrimType(Label, "label");
427#undef DeclarePrimType
Chris Lattner00950542001-06-06 20:29:01 +0000428
429
430//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000431// Derived Type Constructors
432//===----------------------------------------------------------------------===//
433
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000434FunctionType::FunctionType(const Type *Result,
Misha Brukmanfd939082005-04-21 23:48:37 +0000435 const std::vector<const Type*> &Params,
436 bool IsVarArgs) : DerivedType(FunctionTyID),
Chris Lattnerf32f5682004-02-09 05:40:24 +0000437 isVarArgs(IsVarArgs) {
Chris Lattnerc9647152004-07-07 06:48:27 +0000438 assert((Result->isFirstClassType() || Result == Type::VoidTy ||
Misha Brukmanfd939082005-04-21 23:48:37 +0000439 isa<OpaqueType>(Result)) &&
Chris Lattneredfc49d2004-07-06 23:25:19 +0000440 "LLVM functions cannot return aggregates");
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000441 bool isAbstract = Result->isAbstract();
Chris Lattnerf32f5682004-02-09 05:40:24 +0000442 ContainedTys.reserve(Params.size()+1);
443 ContainedTys.push_back(PATypeHandle(Result, this));
444
445 for (unsigned i = 0; i != Params.size(); ++i) {
Chris Lattnerb4091e52004-07-13 20:09:51 +0000446 assert((Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])) &&
447 "Function arguments must be value types!");
448
Chris Lattnerf32f5682004-02-09 05:40:24 +0000449 ContainedTys.push_back(PATypeHandle(Params[i], this));
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000450 isAbstract |= Params[i]->isAbstract();
451 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000452
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000453 // Calculate whether or not this type is abstract
454 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000455}
456
Chris Lattner47697a12003-05-22 21:21:43 +0000457StructType::StructType(const std::vector<const Type*> &Types)
Chris Lattner6c42c312001-12-14 16:41:56 +0000458 : CompositeType(StructTyID) {
Chris Lattnerf32f5682004-02-09 05:40:24 +0000459 ContainedTys.reserve(Types.size());
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000460 bool isAbstract = false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000461 for (unsigned i = 0; i < Types.size(); ++i) {
Chris Lattnere83593b2004-03-29 00:17:20 +0000462 assert(Types[i] != Type::VoidTy && "Void type for structure field!!");
Chris Lattnerf32f5682004-02-09 05:40:24 +0000463 ContainedTys.push_back(PATypeHandle(Types[i], this));
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000464 isAbstract |= Types[i]->isAbstract();
Chris Lattner56c5acb2001-10-13 07:01:33 +0000465 }
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000466
467 // Calculate whether or not this type is abstract
468 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000469}
470
Chris Lattner7d7a0ed2005-01-08 20:19:51 +0000471ArrayType::ArrayType(const Type *ElType, uint64_t NumEl)
Chris Lattner6c42c312001-12-14 16:41:56 +0000472 : SequentialType(ArrayTyID, ElType) {
473 NumElements = NumEl;
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000474
475 // Calculate whether or not this type is abstract
476 setAbstract(ElType->isAbstract());
Chris Lattner00950542001-06-06 20:29:01 +0000477}
478
Brian Gaeke715c90b2004-08-20 06:00:58 +0000479PackedType::PackedType(const Type *ElType, unsigned NumEl)
480 : SequentialType(PackedTyID, ElType) {
481 NumElements = NumEl;
482
483 assert(NumEl > 0 && "NumEl of a PackedType must be greater than 0");
Misha Brukmanfd939082005-04-21 23:48:37 +0000484 assert((ElType->isIntegral() || ElType->isFloatingPoint()) &&
Brian Gaeke715c90b2004-08-20 06:00:58 +0000485 "Elements of a PackedType must be a primitive type");
486}
487
488
Chris Lattner6c42c312001-12-14 16:41:56 +0000489PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000490 // Calculate whether or not this type is abstract
491 setAbstract(E->isAbstract());
Chris Lattner6c42c312001-12-14 16:41:56 +0000492}
493
494OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000495 setAbstract(true);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000496#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000497 DOUT << "Derived new type: " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000498#endif
499}
500
Chris Lattnerf32f5682004-02-09 05:40:24 +0000501// dropAllTypeUses - When this (abstract) type is resolved to be equal to
502// another (more concrete) type, we must eliminate all references to other
503// types, to avoid some circular reference problems.
504void DerivedType::dropAllTypeUses() {
505 if (!ContainedTys.empty()) {
Chris Lattnerf32f5682004-02-09 05:40:24 +0000506 // The type must stay abstract. To do this, we insert a pointer to a type
507 // that will never get resolved, thus will always be abstract.
508 static Type *AlwaysOpaqueTy = OpaqueType::get();
509 static PATypeHolder Holder(AlwaysOpaqueTy);
510 ContainedTys[0] = AlwaysOpaqueTy;
Chris Lattner3787c952005-11-16 06:09:47 +0000511
512 // Change the rest of the types to be intty's. It doesn't matter what we
513 // pick so long as it doesn't point back to this type. We choose something
514 // concrete to avoid overhead for adding to AbstracTypeUser lists and stuff.
515 for (unsigned i = 1, e = ContainedTys.size(); i != e; ++i)
516 ContainedTys[i] = Type::IntTy;
Chris Lattnerf32f5682004-02-09 05:40:24 +0000517 }
Chris Lattner18250092003-10-13 14:03:36 +0000518}
519
Chris Lattnera6b10b62004-10-07 19:20:48 +0000520
521
522/// TypePromotionGraph and graph traits - this is designed to allow us to do
523/// efficient SCC processing of type graphs. This is the exact same as
524/// GraphTraits<Type*>, except that we pretend that concrete types have no
525/// children to avoid processing them.
526struct TypePromotionGraph {
527 Type *Ty;
528 TypePromotionGraph(Type *T) : Ty(T) {}
529};
530
531namespace llvm {
532 template <> struct GraphTraits<TypePromotionGraph> {
533 typedef Type NodeType;
534 typedef Type::subtype_iterator ChildIteratorType;
Misha Brukmanfd939082005-04-21 23:48:37 +0000535
Chris Lattnera6b10b62004-10-07 19:20:48 +0000536 static inline NodeType *getEntryNode(TypePromotionGraph G) { return G.Ty; }
Misha Brukmanfd939082005-04-21 23:48:37 +0000537 static inline ChildIteratorType child_begin(NodeType *N) {
Chris Lattnera6b10b62004-10-07 19:20:48 +0000538 if (N->isAbstract())
Misha Brukmanfd939082005-04-21 23:48:37 +0000539 return N->subtype_begin();
Chris Lattnera6b10b62004-10-07 19:20:48 +0000540 else // No need to process children of concrete types.
Misha Brukmanfd939082005-04-21 23:48:37 +0000541 return N->subtype_end();
Chris Lattnera6b10b62004-10-07 19:20:48 +0000542 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000543 static inline ChildIteratorType child_end(NodeType *N) {
Chris Lattnera6b10b62004-10-07 19:20:48 +0000544 return N->subtype_end();
545 }
546 };
547}
548
549
Chris Lattnerb5c16702004-10-06 16:36:46 +0000550// PromoteAbstractToConcrete - This is a recursive function that walks a type
551// graph calculating whether or not a type is abstract.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000552//
Chris Lattnera6b10b62004-10-07 19:20:48 +0000553void Type::PromoteAbstractToConcrete() {
554 if (!isAbstract()) return;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000555
Chris Lattnera6b10b62004-10-07 19:20:48 +0000556 scc_iterator<TypePromotionGraph> SI = scc_begin(TypePromotionGraph(this));
557 scc_iterator<TypePromotionGraph> SE = scc_end (TypePromotionGraph(this));
Chris Lattnerb5c16702004-10-06 16:36:46 +0000558
Chris Lattnera6b10b62004-10-07 19:20:48 +0000559 for (; SI != SE; ++SI) {
560 std::vector<Type*> &SCC = *SI;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000561
Chris Lattnera6b10b62004-10-07 19:20:48 +0000562 // Concrete types are leaves in the tree. Since an SCC will either be all
563 // abstract or all concrete, we only need to check one type.
564 if (SCC[0]->isAbstract()) {
565 if (isa<OpaqueType>(SCC[0]))
566 return; // Not going to be concrete, sorry.
567
568 // If all of the children of all of the types in this SCC are concrete,
569 // then this SCC is now concrete as well. If not, neither this SCC, nor
570 // any parent SCCs will be concrete, so we might as well just exit.
571 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
572 for (Type::subtype_iterator CI = SCC[i]->subtype_begin(),
573 E = SCC[i]->subtype_end(); CI != E; ++CI)
574 if ((*CI)->isAbstract())
Chris Lattner76da6162005-03-09 17:34:27 +0000575 // If the child type is in our SCC, it doesn't make the entire SCC
576 // abstract unless there is a non-SCC abstract type.
577 if (std::find(SCC.begin(), SCC.end(), *CI) == SCC.end())
578 return; // Not going to be concrete, sorry.
Misha Brukmanfd939082005-04-21 23:48:37 +0000579
Chris Lattnera6b10b62004-10-07 19:20:48 +0000580 // Okay, we just discovered this whole SCC is now concrete, mark it as
581 // such!
582 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
583 assert(SCC[i]->isAbstract() && "Why are we processing concrete types?");
Misha Brukmanfd939082005-04-21 23:48:37 +0000584
Chris Lattnera6b10b62004-10-07 19:20:48 +0000585 SCC[i]->setAbstract(false);
Chris Lattner76da6162005-03-09 17:34:27 +0000586 }
587
588 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
589 assert(!SCC[i]->isAbstract() && "Concrete type became abstract?");
Chris Lattnera6b10b62004-10-07 19:20:48 +0000590 // The type just became concrete, notify all users!
591 cast<DerivedType>(SCC[i])->notifyUsesThatTypeBecameConcrete();
592 }
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000593 }
Chris Lattnera6b10b62004-10-07 19:20:48 +0000594 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000595}
596
597
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000598//===----------------------------------------------------------------------===//
599// Type Structural Equality Testing
600//===----------------------------------------------------------------------===//
601
602// TypesEqual - Two types are considered structurally equal if they have the
603// same "shape": Every level and element of the types have identical primitive
604// ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
605// be pointer equals to be equivalent though. This uses an optimistic algorithm
606// that assumes that two graphs are the same until proven otherwise.
607//
608static bool TypesEqual(const Type *Ty, const Type *Ty2,
Reid Spencer6e885d02004-07-04 12:14:17 +0000609 std::map<const Type *, const Type *> &EqTypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000610 if (Ty == Ty2) return true;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000611 if (Ty->getTypeID() != Ty2->getTypeID()) return false;
Chris Lattner008f9062001-10-24 05:12:04 +0000612 if (isa<OpaqueType>(Ty))
Misha Brukman6b634522003-10-10 17:54:14 +0000613 return false; // Two unequal opaque types are never equal
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000614
Chris Lattner5af41972003-10-13 14:55:56 +0000615 std::map<const Type*, const Type*>::iterator It = EqTypes.lower_bound(Ty);
616 if (It != EqTypes.end() && It->first == Ty)
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000617 return It->second == Ty2; // Looping back on a type, check for equality
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000618
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000619 // Otherwise, add the mapping to the table to make sure we don't get
620 // recursion on the types...
Chris Lattner5af41972003-10-13 14:55:56 +0000621 EqTypes.insert(It, std::make_pair(Ty, Ty2));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000622
Chris Lattner56c5acb2001-10-13 07:01:33 +0000623 // Two really annoying special cases that breaks an otherwise nice simple
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000624 // algorithm is the fact that arraytypes have sizes that differentiates types,
Chris Lattner169726b2003-09-05 02:21:39 +0000625 // and that function types can be varargs or not. Consider this now.
Chris Lattner5af41972003-10-13 14:55:56 +0000626 //
627 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
628 return TypesEqual(PTy->getElementType(),
629 cast<PointerType>(Ty2)->getElementType(), EqTypes);
630 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000631 const StructType *STy2 = cast<StructType>(Ty2);
632 if (STy->getNumElements() != STy2->getNumElements()) return false;
633 for (unsigned i = 0, e = STy2->getNumElements(); i != e; ++i)
634 if (!TypesEqual(STy->getElementType(i), STy2->getElementType(i), EqTypes))
Chris Lattner5af41972003-10-13 14:55:56 +0000635 return false;
636 return true;
637 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
638 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
639 return ATy->getNumElements() == ATy2->getNumElements() &&
640 TypesEqual(ATy->getElementType(), ATy2->getElementType(), EqTypes);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000641 } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
642 const PackedType *PTy2 = cast<PackedType>(Ty2);
643 return PTy->getNumElements() == PTy2->getNumElements() &&
644 TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000645 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner5af41972003-10-13 14:55:56 +0000646 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
647 if (FTy->isVarArg() != FTy2->isVarArg() ||
Chris Lattnerd5d89962004-02-09 04:14:01 +0000648 FTy->getNumParams() != FTy2->getNumParams() ||
Chris Lattner5af41972003-10-13 14:55:56 +0000649 !TypesEqual(FTy->getReturnType(), FTy2->getReturnType(), EqTypes))
Chris Lattner56c5acb2001-10-13 07:01:33 +0000650 return false;
Chris Lattnerd5d89962004-02-09 04:14:01 +0000651 for (unsigned i = 0, e = FTy2->getNumParams(); i != e; ++i)
652 if (!TypesEqual(FTy->getParamType(i), FTy2->getParamType(i), EqTypes))
Chris Lattner5af41972003-10-13 14:55:56 +0000653 return false;
654 return true;
655 } else {
656 assert(0 && "Unknown derived type!");
657 return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000658 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000659}
660
661static bool TypesEqual(const Type *Ty, const Type *Ty2) {
Chris Lattner47697a12003-05-22 21:21:43 +0000662 std::map<const Type *, const Type *> EqTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000663 return TypesEqual(Ty, Ty2, EqTypes);
664}
665
Chris Lattner8511c352004-11-16 20:30:53 +0000666// AbstractTypeHasCycleThrough - Return true there is a path from CurTy to
667// TargetTy in the type graph. We know that Ty is an abstract type, so if we
668// ever reach a non-abstract type, we know that we don't need to search the
669// subgraph.
670static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
Chris Lattnerc3b58492004-02-09 20:23:44 +0000671 std::set<const Type*> &VisitedTypes) {
672 if (TargetTy == CurTy) return true;
673 if (!CurTy->isAbstract()) return false;
674
Chris Lattner8511c352004-11-16 20:30:53 +0000675 if (!VisitedTypes.insert(CurTy).second)
676 return false; // Already been here.
Chris Lattnerc3b58492004-02-09 20:23:44 +0000677
Misha Brukmanfd939082005-04-21 23:48:37 +0000678 for (Type::subtype_iterator I = CurTy->subtype_begin(),
Reid Spencer6e885d02004-07-04 12:14:17 +0000679 E = CurTy->subtype_end(); I != E; ++I)
Chris Lattner8511c352004-11-16 20:30:53 +0000680 if (AbstractTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
Chris Lattnerc3b58492004-02-09 20:23:44 +0000681 return true;
682 return false;
683}
684
Chris Lattner8511c352004-11-16 20:30:53 +0000685static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
686 std::set<const Type*> &VisitedTypes) {
687 if (TargetTy == CurTy) return true;
688
689 if (!VisitedTypes.insert(CurTy).second)
690 return false; // Already been here.
691
Misha Brukmanfd939082005-04-21 23:48:37 +0000692 for (Type::subtype_iterator I = CurTy->subtype_begin(),
Chris Lattner8511c352004-11-16 20:30:53 +0000693 E = CurTy->subtype_end(); I != E; ++I)
694 if (ConcreteTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
695 return true;
696 return false;
697}
Chris Lattnerc3b58492004-02-09 20:23:44 +0000698
Chris Lattner27295402004-02-09 16:35:14 +0000699/// TypeHasCycleThroughItself - Return true if the specified type has a cycle
700/// back to itself.
701static bool TypeHasCycleThroughItself(const Type *Ty) {
702 std::set<const Type*> VisitedTypes;
Chris Lattner8511c352004-11-16 20:30:53 +0000703
704 if (Ty->isAbstract()) { // Optimized case for abstract types.
Misha Brukmanfd939082005-04-21 23:48:37 +0000705 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattner8511c352004-11-16 20:30:53 +0000706 I != E; ++I)
707 if (AbstractTypeHasCycleThrough(Ty, *I, VisitedTypes))
708 return true;
709 } else {
710 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
711 I != E; ++I)
712 if (ConcreteTypeHasCycleThrough(Ty, *I, VisitedTypes))
713 return true;
714 }
Chris Lattner27295402004-02-09 16:35:14 +0000715 return false;
716}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000717
Chris Lattner3787c952005-11-16 06:09:47 +0000718/// getSubElementHash - Generate a hash value for all of the SubType's of this
719/// type. The hash value is guaranteed to be zero if any of the subtypes are
720/// an opaque type. Otherwise we try to mix them in as well as possible, but do
721/// not look at the subtype's subtype's.
722static unsigned getSubElementHash(const Type *Ty) {
723 unsigned HashVal = 0;
724 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
725 I != E; ++I) {
726 HashVal *= 32;
727 const Type *SubTy = I->get();
728 HashVal += SubTy->getTypeID();
729 switch (SubTy->getTypeID()) {
730 default: break;
731 case Type::OpaqueTyID: return 0; // Opaque -> hash = 0 no matter what.
732 case Type::FunctionTyID:
733 HashVal ^= cast<FunctionType>(SubTy)->getNumParams()*2 +
734 cast<FunctionType>(SubTy)->isVarArg();
735 break;
736 case Type::ArrayTyID:
737 HashVal ^= cast<ArrayType>(SubTy)->getNumElements();
738 break;
739 case Type::PackedTyID:
740 HashVal ^= cast<PackedType>(SubTy)->getNumElements();
741 break;
742 case Type::StructTyID:
743 HashVal ^= cast<StructType>(SubTy)->getNumElements();
744 break;
745 }
746 }
747 return HashVal ? HashVal : 1; // Do not return zero unless opaque subty.
748}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000749
750//===----------------------------------------------------------------------===//
751// Derived Type Factory Functions
752//===----------------------------------------------------------------------===//
753
Chris Lattnerf2586d12003-11-19 06:14:38 +0000754namespace llvm {
Chris Lattnercfe82272005-11-13 03:14:09 +0000755class TypeMapBase {
756protected:
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000757 /// TypesByHash - Keep track of types by their structure hash value. Note
758 /// that we only keep track of types that have cycles through themselves in
759 /// this map.
Chris Lattner0cdaf942004-02-09 18:32:40 +0000760 ///
761 std::multimap<unsigned, PATypeHolder> TypesByHash;
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000762
Chris Lattnercfe82272005-11-13 03:14:09 +0000763public:
764 void RemoveFromTypesByHash(unsigned Hash, const Type *Ty) {
765 std::multimap<unsigned, PATypeHolder>::iterator I =
Chris Lattner3787c952005-11-16 06:09:47 +0000766 TypesByHash.lower_bound(Hash);
767 for (; I != TypesByHash.end() && I->first == Hash; ++I) {
768 if (I->second == Ty) {
769 TypesByHash.erase(I);
770 return;
771 }
Chris Lattnercfe82272005-11-13 03:14:09 +0000772 }
Chris Lattner3787c952005-11-16 06:09:47 +0000773
774 // This must be do to an opaque type that was resolved. Switch down to hash
775 // code of zero.
776 assert(Hash && "Didn't find type entry!");
777 RemoveFromTypesByHash(0, Ty);
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000778 }
Chris Lattnercfe82272005-11-13 03:14:09 +0000779
780 /// TypeBecameConcrete - When Ty gets a notification that TheType just became
781 /// concrete, drop uses and make Ty non-abstract if we should.
782 void TypeBecameConcrete(DerivedType *Ty, const DerivedType *TheType) {
783 // If the element just became concrete, remove 'ty' from the abstract
784 // type user list for the type. Do this for as many times as Ty uses
785 // OldType.
786 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
787 I != E; ++I)
788 if (I->get() == TheType)
789 TheType->removeAbstractTypeUser(Ty);
790
791 // If the type is currently thought to be abstract, rescan all of our
792 // subtypes to see if the type has just become concrete! Note that this
793 // may send out notifications to AbstractTypeUsers that types become
794 // concrete.
795 if (Ty->isAbstract())
796 Ty->PromoteAbstractToConcrete();
797 }
798};
799}
800
801
802// TypeMap - Make sure that only one instance of a particular type may be
803// created on any given run of the compiler... note that this involves updating
804// our map if an abstract type gets refined somehow.
805//
806namespace llvm {
807template<class ValType, class TypeClass>
808class TypeMap : public TypeMapBase {
809 std::map<ValType, PATypeHolder> Map;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000810public:
Chris Lattner27295402004-02-09 16:35:14 +0000811 typedef typename std::map<ValType, PATypeHolder>::iterator iterator;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000812 ~TypeMap() { print("ON EXIT"); }
813
814 inline TypeClass *get(const ValType &V) {
Chris Lattner169726b2003-09-05 02:21:39 +0000815 iterator I = Map.find(V);
Chris Lattner3b41e0e2003-12-31 03:19:37 +0000816 return I != Map.end() ? cast<TypeClass>((Type*)I->second.get()) : 0;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000817 }
818
Chris Lattner0cdaf942004-02-09 18:32:40 +0000819 inline void add(const ValType &V, TypeClass *Ty) {
820 Map.insert(std::make_pair(V, Ty));
821
822 // If this type has a cycle, remember it.
823 TypesByHash.insert(std::make_pair(ValType::hashTypeStructure(Ty), Ty));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000824 print("add");
825 }
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000826
Chris Lattnercfe82272005-11-13 03:14:09 +0000827 void clear(std::vector<Type *> &DerivedTypes) {
828 for (typename std::map<ValType, PATypeHolder>::iterator I = Map.begin(),
829 E = Map.end(); I != E; ++I)
830 DerivedTypes.push_back(I->second.get());
831 TypesByHash.clear();
832 Map.clear();
833 }
834
835 /// RefineAbstractType - This method is called after we have merged a type
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000836 /// with another one. We must now either merge the type away with
Chris Lattner4b1be102003-12-31 02:50:02 +0000837 /// some other type or reinstall it in the map with it's new configuration.
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000838 void RefineAbstractType(TypeClass *Ty, const DerivedType *OldType,
Chris Lattner27295402004-02-09 16:35:14 +0000839 const Type *NewType) {
Chris Lattner27295402004-02-09 16:35:14 +0000840#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000841 DOUT << "RefineAbstractType(" << (void*)OldType << "[" << *OldType
842 << "], " << (void*)NewType << " [" << *NewType << "])\n";
Chris Lattner27295402004-02-09 16:35:14 +0000843#endif
Chris Lattner66cafb32005-11-13 01:27:50 +0000844
845 // Otherwise, we are changing one subelement type into another. Clearly the
846 // OldType must have been abstract, making us abstract.
847 assert(Ty->isAbstract() && "Refining a non-abstract type!");
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000848 assert(OldType != NewType);
Chris Lattnercfe82272005-11-13 03:14:09 +0000849
Chris Lattner3b41e0e2003-12-31 03:19:37 +0000850 // Make a temporary type holder for the type so that it doesn't disappear on
851 // us when we erase the entry from the map.
Chris Lattner27295402004-02-09 16:35:14 +0000852 PATypeHolder TyHolder = Ty;
853
Chris Lattner7685ac82003-10-03 18:46:24 +0000854 // The old record is now out-of-date, because one of the children has been
855 // updated. Remove the obsolete entry from the map.
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000856 unsigned NumErased = Map.erase(ValType::get(Ty));
857 assert(NumErased && "Element not found!");
Chris Lattner7685ac82003-10-03 18:46:24 +0000858
Chris Lattner0cdaf942004-02-09 18:32:40 +0000859 // Remember the structural hash for the type before we start hacking on it,
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000860 // in case we need it later.
Chris Lattner0cdaf942004-02-09 18:32:40 +0000861 unsigned OldTypeHash = ValType::hashTypeStructure(Ty);
862
863 // Find the type element we are refining... and change it now!
Chris Lattner66cafb32005-11-13 01:27:50 +0000864 for (unsigned i = 0, e = Ty->ContainedTys.size(); i != e; ++i)
865 if (Ty->ContainedTys[i] == OldType)
866 Ty->ContainedTys[i] = NewType;
Chris Lattner75485902005-11-12 08:39:48 +0000867 unsigned NewTypeHash = ValType::hashTypeStructure(Ty);
868
Chris Lattner542d9912003-11-19 19:10:23 +0000869 // If there are no cycles going through this node, we can do a simple,
870 // efficient lookup in the map, instead of an inefficient nasty linear
871 // lookup.
Chris Lattner66cafb32005-11-13 01:27:50 +0000872 if (!TypeHasCycleThroughItself(Ty)) {
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000873 typename std::map<ValType, PATypeHolder>::iterator I;
874 bool Inserted;
875
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000876 tie(I, Inserted) = Map.insert(std::make_pair(ValType::get(Ty), Ty));
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000877 if (!Inserted) {
Chris Lattner8df956c2003-09-05 02:39:52 +0000878 // Refined to a different type altogether?
Chris Lattner66cafb32005-11-13 01:27:50 +0000879 RemoveFromTypesByHash(OldTypeHash, Ty);
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000880
881 // We already have this type in the table. Get rid of the newly refined
882 // type.
883 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000884 Ty->refineAbstractTypeTo(NewTy);
Chris Lattner8df956c2003-09-05 02:39:52 +0000885 return;
Chris Lattner169726b2003-09-05 02:21:39 +0000886 }
Chris Lattner542d9912003-11-19 19:10:23 +0000887 } else {
888 // Now we check to see if there is an existing entry in the table which is
889 // structurally identical to the newly refined type. If so, this type
890 // gets refined to the pre-existing type.
891 //
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000892 std::multimap<unsigned, PATypeHolder>::iterator I, E, Entry;
Chris Lattner3787c952005-11-16 06:09:47 +0000893 tie(I, E) = TypesByHash.equal_range(NewTypeHash);
Chris Lattner0cdaf942004-02-09 18:32:40 +0000894 Entry = E;
895 for (; I != E; ++I) {
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000896 if (I->second == Ty) {
897 // Remember the position of the old type if we see it in our scan.
898 Entry = I;
899 } else {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000900 if (TypesEqual(Ty, I->second)) {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000901 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
Misha Brukmanfd939082005-04-21 23:48:37 +0000902
Chris Lattner3787c952005-11-16 06:09:47 +0000903 // Remove the old entry form TypesByHash. If the hash values differ
904 // now, remove it from the old place. Otherwise, continue scanning
905 // withing this hashcode to reduce work.
906 if (NewTypeHash != OldTypeHash) {
907 RemoveFromTypesByHash(OldTypeHash, Ty);
908 } else {
909 if (Entry == E) {
910 // Find the location of Ty in the TypesByHash structure if we
911 // haven't seen it already.
912 while (I->second != Ty) {
913 ++I;
914 assert(I != E && "Structure doesn't contain type??");
915 }
916 Entry = I;
Chris Lattner0cdaf942004-02-09 18:32:40 +0000917 }
Chris Lattner3787c952005-11-16 06:09:47 +0000918 TypesByHash.erase(Entry);
Chris Lattner0cdaf942004-02-09 18:32:40 +0000919 }
Chris Lattner0cdaf942004-02-09 18:32:40 +0000920 Ty->refineAbstractTypeTo(NewTy);
921 return;
922 }
Chris Lattner542d9912003-11-19 19:10:23 +0000923 }
Chris Lattner27295402004-02-09 16:35:14 +0000924 }
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000925
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000926 // If there is no existing type of the same structure, we reinsert an
927 // updated record into the map.
928 Map.insert(std::make_pair(ValType::get(Ty), Ty));
Chris Lattner542d9912003-11-19 19:10:23 +0000929 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000930
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000931 // If the hash codes differ, update TypesByHash
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000932 if (NewTypeHash != OldTypeHash) {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000933 RemoveFromTypesByHash(OldTypeHash, Ty);
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000934 TypesByHash.insert(std::make_pair(NewTypeHash, Ty));
Chris Lattner0cdaf942004-02-09 18:32:40 +0000935 }
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000936
Chris Lattner8df956c2003-09-05 02:39:52 +0000937 // If the type is currently thought to be abstract, rescan all of our
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000938 // subtypes to see if the type has just become concrete! Note that this
939 // may send out notifications to AbstractTypeUsers that types become
940 // concrete.
Chris Lattnera6b10b62004-10-07 19:20:48 +0000941 if (Ty->isAbstract())
942 Ty->PromoteAbstractToConcrete();
Chris Lattner266caa22003-09-05 02:30:47 +0000943 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000944
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000945 void print(const char *Arg) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000946#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000947 DOUT << "TypeMap<>::" << Arg << " table contents:\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000948 unsigned i = 0;
Chris Lattner27295402004-02-09 16:35:14 +0000949 for (typename std::map<ValType, PATypeHolder>::const_iterator I
950 = Map.begin(), E = Map.end(); I != E; ++I)
Bill Wendling2e3def12006-11-17 08:03:48 +0000951 DOUT << " " << (++i) << ". " << (void*)I->second.get() << " "
952 << *I->second.get() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000953#endif
954 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000955
956 void dump() const { print("dump output"); }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000957};
Chris Lattnerf2586d12003-11-19 06:14:38 +0000958}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000959
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000960
961//===----------------------------------------------------------------------===//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000962// Function Type Factory and Value Class...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000963//
964
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000965// FunctionValType - Define a class to hold the key that goes into the TypeMap
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000966//
Chris Lattnerf2586d12003-11-19 06:14:38 +0000967namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +0000968class FunctionValType {
969 const Type *RetTy;
970 std::vector<const Type*> ArgTypes;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000971 bool isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000972public:
Chris Lattner47697a12003-05-22 21:21:43 +0000973 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
Chris Lattner7685ac82003-10-03 18:46:24 +0000974 bool IVA) : RetTy(ret), isVarArg(IVA) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000975 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattner7685ac82003-10-03 18:46:24 +0000976 ArgTypes.push_back(args[i]);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000977 }
978
Chris Lattner169726b2003-09-05 02:21:39 +0000979 static FunctionValType get(const FunctionType *FT);
980
Chris Lattner27295402004-02-09 16:35:14 +0000981 static unsigned hashTypeStructure(const FunctionType *FT) {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000982 return FT->getNumParams()*2+FT->isVarArg();
Chris Lattner27295402004-02-09 16:35:14 +0000983 }
984
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000985 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +0000986 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000987 if (RetTy == OldType) RetTy = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +0000988 for (unsigned i = 0, e = ArgTypes.size(); i != e; ++i)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000989 if (ArgTypes[i] == OldType) ArgTypes[i] = NewType;
990 }
991
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000992 inline bool operator<(const FunctionValType &MTV) const {
Chris Lattner7685ac82003-10-03 18:46:24 +0000993 if (RetTy < MTV.RetTy) return true;
994 if (RetTy > MTV.RetTy) return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000995
996 if (ArgTypes < MTV.ArgTypes) return true;
Chris Lattner7685ac82003-10-03 18:46:24 +0000997 return ArgTypes == MTV.ArgTypes && isVarArg < MTV.isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000998 }
999};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001000}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001001
1002// Define the actual map itself now...
Chris Lattnerde65fb32006-09-28 23:38:07 +00001003static ManagedStatic<TypeMap<FunctionValType, FunctionType> > FunctionTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001004
Chris Lattner169726b2003-09-05 02:21:39 +00001005FunctionValType FunctionValType::get(const FunctionType *FT) {
1006 // Build up a FunctionValType
1007 std::vector<const Type *> ParamTypes;
Chris Lattnerd5d89962004-02-09 04:14:01 +00001008 ParamTypes.reserve(FT->getNumParams());
1009 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
Chris Lattner169726b2003-09-05 02:21:39 +00001010 ParamTypes.push_back(FT->getParamType(i));
Chris Lattner7685ac82003-10-03 18:46:24 +00001011 return FunctionValType(FT->getReturnType(), ParamTypes, FT->isVarArg());
Chris Lattner169726b2003-09-05 02:21:39 +00001012}
1013
1014
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001015// FunctionType::get - The factory function for the FunctionType class...
Misha Brukmanfd939082005-04-21 23:48:37 +00001016FunctionType *FunctionType::get(const Type *ReturnType,
Chris Lattner47697a12003-05-22 21:21:43 +00001017 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001018 bool isVarArg) {
Chris Lattner7685ac82003-10-03 18:46:24 +00001019 FunctionValType VT(ReturnType, Params, isVarArg);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001020 FunctionType *MT = FunctionTypes->get(VT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001021 if (MT) return MT;
Chris Lattnere5a57ee2001-07-25 22:47:55 +00001022
Chris Lattnerde65fb32006-09-28 23:38:07 +00001023 FunctionTypes->add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
Chris Lattnere5a57ee2001-07-25 22:47:55 +00001024
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001025#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001026 DOUT << "Derived new type: " << MT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001027#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001028 return MT;
Chris Lattner00950542001-06-06 20:29:01 +00001029}
1030
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001031//===----------------------------------------------------------------------===//
1032// Array Type Factory...
1033//
Chris Lattnerf2586d12003-11-19 06:14:38 +00001034namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +00001035class ArrayValType {
1036 const Type *ValTy;
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001037 uint64_t Size;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001038public:
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001039 ArrayValType(const Type *val, uint64_t sz) : ValTy(val), Size(sz) {}
Chris Lattner00950542001-06-06 20:29:01 +00001040
Chris Lattner7685ac82003-10-03 18:46:24 +00001041 static ArrayValType get(const ArrayType *AT) {
1042 return ArrayValType(AT->getElementType(), AT->getNumElements());
1043 }
Chris Lattner169726b2003-09-05 02:21:39 +00001044
Chris Lattner27295402004-02-09 16:35:14 +00001045 static unsigned hashTypeStructure(const ArrayType *AT) {
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001046 return (unsigned)AT->getNumElements();
Chris Lattner27295402004-02-09 16:35:14 +00001047 }
1048
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001049 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +00001050 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +00001051 assert(ValTy == OldType);
1052 ValTy = NewType;
Chris Lattner00950542001-06-06 20:29:01 +00001053 }
1054
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001055 inline bool operator<(const ArrayValType &MTV) const {
1056 if (Size < MTV.Size) return true;
Chris Lattner7685ac82003-10-03 18:46:24 +00001057 return Size == MTV.Size && ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001058 }
1059};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001060}
Chris Lattnerde65fb32006-09-28 23:38:07 +00001061static ManagedStatic<TypeMap<ArrayValType, ArrayType> > ArrayTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001062
Chris Lattner169726b2003-09-05 02:21:39 +00001063
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001064ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001065 assert(ElementType && "Can't get array of null types!");
1066
Chris Lattner7685ac82003-10-03 18:46:24 +00001067 ArrayValType AVT(ElementType, NumElements);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001068 ArrayType *AT = ArrayTypes->get(AVT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001069 if (AT) return AT; // Found a match, return it!
1070
Chris Lattner00950542001-06-06 20:29:01 +00001071 // Value not found. Derive a new type!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001072 ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
Chris Lattner00950542001-06-06 20:29:01 +00001073
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001074#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001075 DOUT << "Derived new type: " << *AT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001076#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001077 return AT;
Chris Lattner00950542001-06-06 20:29:01 +00001078}
1079
Brian Gaeke715c90b2004-08-20 06:00:58 +00001080
1081//===----------------------------------------------------------------------===//
1082// Packed Type Factory...
1083//
1084namespace llvm {
1085class PackedValType {
1086 const Type *ValTy;
1087 unsigned Size;
1088public:
1089 PackedValType(const Type *val, int sz) : ValTy(val), Size(sz) {}
1090
1091 static PackedValType get(const PackedType *PT) {
1092 return PackedValType(PT->getElementType(), PT->getNumElements());
1093 }
1094
1095 static unsigned hashTypeStructure(const PackedType *PT) {
1096 return PT->getNumElements();
1097 }
1098
1099 // Subclass should override this... to update self as usual
1100 void doRefinement(const DerivedType *OldType, const Type *NewType) {
1101 assert(ValTy == OldType);
1102 ValTy = NewType;
1103 }
1104
1105 inline bool operator<(const PackedValType &MTV) const {
1106 if (Size < MTV.Size) return true;
1107 return Size == MTV.Size && ValTy < MTV.ValTy;
1108 }
1109};
1110}
Chris Lattnerde65fb32006-09-28 23:38:07 +00001111static ManagedStatic<TypeMap<PackedValType, PackedType> > PackedTypes;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001112
1113
1114PackedType *PackedType::get(const Type *ElementType, unsigned NumElements) {
1115 assert(ElementType && "Can't get packed of null types!");
Chris Lattnerd115ef82005-11-10 01:40:59 +00001116 assert(isPowerOf2_32(NumElements) && "Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001117
1118 PackedValType PVT(ElementType, NumElements);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001119 PackedType *PT = PackedTypes->get(PVT);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001120 if (PT) return PT; // Found a match, return it!
1121
1122 // Value not found. Derive a new type!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001123 PackedTypes->add(PVT, PT = new PackedType(ElementType, NumElements));
Brian Gaeke715c90b2004-08-20 06:00:58 +00001124
1125#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001126 DOUT << "Derived new type: " << *PT << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +00001127#endif
1128 return PT;
1129}
1130
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001131//===----------------------------------------------------------------------===//
1132// Struct Type Factory...
1133//
Chris Lattner00950542001-06-06 20:29:01 +00001134
Chris Lattnerf2586d12003-11-19 06:14:38 +00001135namespace llvm {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001136// StructValType - Define a class to hold the key that goes into the TypeMap
1137//
Chris Lattner7685ac82003-10-03 18:46:24 +00001138class StructValType {
1139 std::vector<const Type*> ElTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001140public:
Chris Lattner7685ac82003-10-03 18:46:24 +00001141 StructValType(const std::vector<const Type*> &args) : ElTypes(args) {}
Chris Lattner00950542001-06-06 20:29:01 +00001142
Chris Lattner7685ac82003-10-03 18:46:24 +00001143 static StructValType get(const StructType *ST) {
1144 std::vector<const Type *> ElTypes;
Chris Lattnerd21cd802004-02-09 04:37:31 +00001145 ElTypes.reserve(ST->getNumElements());
1146 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i)
1147 ElTypes.push_back(ST->getElementType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +00001148
Chris Lattner7685ac82003-10-03 18:46:24 +00001149 return StructValType(ElTypes);
1150 }
Chris Lattner169726b2003-09-05 02:21:39 +00001151
Chris Lattner27295402004-02-09 16:35:14 +00001152 static unsigned hashTypeStructure(const StructType *ST) {
Chris Lattner0cdaf942004-02-09 18:32:40 +00001153 return ST->getNumElements();
Chris Lattner27295402004-02-09 16:35:14 +00001154 }
1155
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001156 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +00001157 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001158 for (unsigned i = 0; i < ElTypes.size(); ++i)
1159 if (ElTypes[i] == OldType) ElTypes[i] = NewType;
1160 }
1161
1162 inline bool operator<(const StructValType &STV) const {
1163 return ElTypes < STV.ElTypes;
1164 }
1165};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001166}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001167
Chris Lattnerde65fb32006-09-28 23:38:07 +00001168static ManagedStatic<TypeMap<StructValType, StructType> > StructTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001169
Chris Lattner47697a12003-05-22 21:21:43 +00001170StructType *StructType::get(const std::vector<const Type*> &ETypes) {
Chris Lattner7685ac82003-10-03 18:46:24 +00001171 StructValType STV(ETypes);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001172 StructType *ST = StructTypes->get(STV);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001173 if (ST) return ST;
1174
1175 // Value not found. Derive a new type!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001176 StructTypes->add(STV, ST = new StructType(ETypes));
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001177
1178#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001179 DOUT << "Derived new type: " << *ST << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001180#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001181 return ST;
1182}
1183
Chris Lattner169726b2003-09-05 02:21:39 +00001184
1185
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001186//===----------------------------------------------------------------------===//
1187// Pointer Type Factory...
1188//
1189
1190// PointerValType - Define a class to hold the key that goes into the TypeMap
1191//
Chris Lattnerf2586d12003-11-19 06:14:38 +00001192namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +00001193class PointerValType {
1194 const Type *ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001195public:
Chris Lattner7685ac82003-10-03 18:46:24 +00001196 PointerValType(const Type *val) : ValTy(val) {}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001197
Chris Lattner7685ac82003-10-03 18:46:24 +00001198 static PointerValType get(const PointerType *PT) {
1199 return PointerValType(PT->getElementType());
1200 }
Chris Lattner169726b2003-09-05 02:21:39 +00001201
Chris Lattner27295402004-02-09 16:35:14 +00001202 static unsigned hashTypeStructure(const PointerType *PT) {
Chris Lattner3787c952005-11-16 06:09:47 +00001203 return getSubElementHash(PT);
Chris Lattner27295402004-02-09 16:35:14 +00001204 }
1205
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001206 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +00001207 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +00001208 assert(ValTy == OldType);
1209 ValTy = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001210 }
1211
Chris Lattner7685ac82003-10-03 18:46:24 +00001212 bool operator<(const PointerValType &MTV) const {
1213 return ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001214 }
1215};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001216}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001217
Chris Lattnerde65fb32006-09-28 23:38:07 +00001218static ManagedStatic<TypeMap<PointerValType, PointerType> > PointerTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001219
1220PointerType *PointerType::get(const Type *ValueType) {
1221 assert(ValueType && "Can't get a pointer to <null> type!");
Chris Lattnera5d824e2006-04-21 15:33:35 +00001222 assert(ValueType != Type::VoidTy &&
1223 "Pointer to void is not valid, use sbyte* instead!");
Chris Lattnerf5310402006-10-15 23:21:12 +00001224 assert(ValueType != Type::LabelTy && "Pointer to label is not valid!");
Chris Lattner7685ac82003-10-03 18:46:24 +00001225 PointerValType PVT(ValueType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001226
Chris Lattnerde65fb32006-09-28 23:38:07 +00001227 PointerType *PT = PointerTypes->get(PVT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001228 if (PT) return PT;
1229
1230 // Value not found. Derive a new type!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001231 PointerTypes->add(PVT, PT = new PointerType(ValueType));
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001232
1233#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001234 DOUT << "Derived new type: " << *PT << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001235#endif
1236 return PT;
1237}
1238
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001239//===----------------------------------------------------------------------===//
1240// Derived Type Refinement Functions
1241//===----------------------------------------------------------------------===//
1242
1243// removeAbstractTypeUser - Notify an abstract type that a user of the class
1244// no longer has a handle to the type. This function is called primarily by
1245// the PATypeHandle class. When there are no users of the abstract type, it
Misha Brukman6b634522003-10-10 17:54:14 +00001246// is annihilated, because there is no way to get a reference to it ever again.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001247//
Chris Lattnercfe82272005-11-13 03:14:09 +00001248void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001249 // Search from back to front because we will notify users from back to
1250 // front. Also, it is likely that there will be a stack like behavior to
1251 // users that register and unregister users.
1252 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +00001253 unsigned i;
1254 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
1255 assert(i != 0 && "AbstractTypeUser not in user list!");
1256
1257 --i; // Convert to be in range 0 <= i < size()
1258 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
1259
1260 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
Misha Brukmanfd939082005-04-21 23:48:37 +00001261
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001262#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001263 DOUT << " remAbstractTypeUser[" << (void*)this << ", "
1264 << *this << "][" << i << "] User = " << U << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001265#endif
Misha Brukmanfd939082005-04-21 23:48:37 +00001266
Chris Lattnerac891642004-02-17 03:03:47 +00001267 if (AbstractTypeUsers.empty() && getRefCount() == 0 && isAbstract()) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001268#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001269 DOUT << "DELETEing unused abstract type: <" << *this
1270 << ">[" << (void*)this << "]" << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001271#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +00001272 delete this; // No users of this abstract type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001273 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001274}
1275
1276
Reid Spencerfd20c0a2006-05-29 02:34:34 +00001277// refineAbstractTypeTo - This function is used when it is discovered that
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001278// the 'this' abstract type is actually equivalent to the NewType specified.
1279// This causes all users of 'this' to switch to reference the more concrete type
1280// NewType and for 'this' to be deleted.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001281//
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001282void DerivedType::refineAbstractTypeTo(const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001283 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
1284 assert(this != NewType && "Can't refine to myself!");
Chris Lattner7685ac82003-10-03 18:46:24 +00001285 assert(ForwardType == 0 && "This type has already been refined!");
1286
Chris Lattnerdd4b4212003-09-02 16:35:17 +00001287 // The descriptions may be out of date. Conservatively clear them all!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001288 AbstractTypeDescriptions->clear();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001289
1290#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001291 DOUT << "REFINING abstract type [" << (void*)this << " "
1292 << *this << "] to [" << (void*)NewType << " "
1293 << *NewType << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001294#endif
1295
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001296 // Make sure to put the type to be refined to into a holder so that if IT gets
1297 // refined, that we will not continue using a dead reference...
1298 //
Chris Lattneraa06d2c2002-04-04 19:26:02 +00001299 PATypeHolder NewTy(NewType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001300
Chris Lattner7685ac82003-10-03 18:46:24 +00001301 // Any PATypeHolders referring to this type will now automatically forward to
1302 // the type we are resolved to.
Chris Lattner1c5164e2003-10-02 23:35:57 +00001303 ForwardType = NewType;
1304 if (NewType->isAbstract())
1305 cast<DerivedType>(NewType)->addRef();
1306
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001307 // Add a self use of the current type so that we don't delete ourself until
Chris Lattner8ef852f2003-10-03 04:48:21 +00001308 // after the function exits.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001309 //
Chris Lattner8ef852f2003-10-03 04:48:21 +00001310 PATypeHolder CurrentTy(this);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001311
Chris Lattner169726b2003-09-05 02:21:39 +00001312 // To make the situation simpler, we ask the subclass to remove this type from
1313 // the type map, and to replace any type uses with uses of non-abstract types.
1314 // This dramatically limits the amount of recursive type trouble we can find
1315 // ourselves in.
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001316 dropAllTypeUses();
Chris Lattner169726b2003-09-05 02:21:39 +00001317
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001318 // Iterate over all of the uses of this type, invoking callback. Each user
Chris Lattner3f59b7e2002-04-05 19:44:07 +00001319 // should remove itself from our use list automatically. We have to check to
1320 // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types
1321 // will not cause users to drop off of the use list. If we resolve to ourself
1322 // we succeed!
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001323 //
Chris Lattner8ef852f2003-10-03 04:48:21 +00001324 while (!AbstractTypeUsers.empty() && NewTy != this) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001325 AbstractTypeUser *User = AbstractTypeUsers.back();
1326
Chris Lattner8ef852f2003-10-03 04:48:21 +00001327 unsigned OldSize = AbstractTypeUsers.size();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001328#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001329 DOUT << " REFINING user " << OldSize-1 << "[" << (void*)User
1330 << "] of abstract type [" << (void*)this << " "
1331 << *this << "] to [" << (void*)NewTy.get() << " "
1332 << *NewTy << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001333#endif
Chris Lattner8ef852f2003-10-03 04:48:21 +00001334 User->refineAbstractType(this, NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001335
Chris Lattner8ef852f2003-10-03 04:48:21 +00001336 assert(AbstractTypeUsers.size() != OldSize &&
1337 "AbsTyUser did not remove self from user list!");
Chris Lattner00950542001-06-06 20:29:01 +00001338 }
1339
Chris Lattner8ef852f2003-10-03 04:48:21 +00001340 // If we were successful removing all users from the type, 'this' will be
1341 // deleted when the last PATypeHolder is destroyed or updated from this type.
1342 // This may occur on exit of this function, as the CurrentTy object is
1343 // destroyed.
Chris Lattner00950542001-06-06 20:29:01 +00001344}
1345
Chris Lattner7685ac82003-10-03 18:46:24 +00001346// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
1347// the current type has transitioned from being abstract to being concrete.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001348//
Chris Lattner7685ac82003-10-03 18:46:24 +00001349void DerivedType::notifyUsesThatTypeBecameConcrete() {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001350#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001351 DOUT << "typeIsREFINED type: " << (void*)this << " " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001352#endif
Chris Lattner417081c2002-04-07 06:14:56 +00001353
Chris Lattner7685ac82003-10-03 18:46:24 +00001354 unsigned OldSize = AbstractTypeUsers.size();
1355 while (!AbstractTypeUsers.empty()) {
1356 AbstractTypeUser *ATU = AbstractTypeUsers.back();
1357 ATU->typeBecameConcrete(this);
Chris Lattner417081c2002-04-07 06:14:56 +00001358
Chris Lattner7685ac82003-10-03 18:46:24 +00001359 assert(AbstractTypeUsers.size() < OldSize-- &&
1360 "AbstractTypeUser did not remove itself from the use list!");
Chris Lattner00950542001-06-06 20:29:01 +00001361 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001362}
Misha Brukmanfd939082005-04-21 23:48:37 +00001363
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001364// refineAbstractType - Called when a contained type is found to be more
1365// concrete - this could potentially change us from an abstract type to a
1366// concrete type.
1367//
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001368void FunctionType::refineAbstractType(const DerivedType *OldType,
1369 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001370 FunctionTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001371}
1372
1373void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001374 FunctionTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001375}
1376
1377
1378// refineAbstractType - Called when a contained type is found to be more
1379// concrete - this could potentially change us from an abstract type to a
1380// concrete type.
1381//
1382void ArrayType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001383 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001384 ArrayTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001385}
1386
1387void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001388 ArrayTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001389}
1390
Brian Gaeke715c90b2004-08-20 06:00:58 +00001391// refineAbstractType - Called when a contained type is found to be more
1392// concrete - this could potentially change us from an abstract type to a
1393// concrete type.
1394//
1395void PackedType::refineAbstractType(const DerivedType *OldType,
1396 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001397 PackedTypes->RefineAbstractType(this, OldType, NewType);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001398}
1399
1400void PackedType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001401 PackedTypes->TypeBecameConcrete(this, AbsTy);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001402}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001403
1404// refineAbstractType - Called when a contained type is found to be more
1405// concrete - this could potentially change us from an abstract type to a
1406// concrete type.
1407//
1408void StructType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001409 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001410 StructTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001411}
1412
1413void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001414 StructTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001415}
1416
1417// refineAbstractType - Called when a contained type is found to be more
1418// concrete - this could potentially change us from an abstract type to a
1419// concrete type.
1420//
1421void PointerType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001422 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001423 PointerTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001424}
1425
1426void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001427 PointerTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattner00950542001-06-06 20:29:01 +00001428}
Reid Spencer6e885d02004-07-04 12:14:17 +00001429
1430bool SequentialType::indexValid(const Value *V) const {
1431 const Type *Ty = V->getType();
1432 switch (Ty->getTypeID()) {
1433 case Type::IntTyID:
1434 case Type::UIntTyID:
1435 case Type::LongTyID:
1436 case Type::ULongTyID:
1437 return true;
1438 default:
1439 return false;
1440 }
1441}
1442
1443namespace llvm {
1444std::ostream &operator<<(std::ostream &OS, const Type *T) {
1445 if (T == 0)
1446 OS << "<null> value!\n";
1447 else
1448 T->print(OS);
1449 return OS;
1450}
1451
1452std::ostream &operator<<(std::ostream &OS, const Type &T) {
1453 T.print(OS);
1454 return OS;
1455}
1456}