blob: 28ed4f366e547b45badd25953e578b3ca115a380 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=//
2//
3// This file implements the Type class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/DerivedTypes.h"
Chris Lattnerc038a2f2001-09-07 16:56:42 +00008#include "llvm/SymbolTable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000010#include "Support/StringExtras.h"
11#include "Support/STLExtras.h"
Chris Lattner417081c2002-04-07 06:14:56 +000012#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000013
Chris Lattnerc038a2f2001-09-07 16:56:42 +000014// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
15// created and later destroyed, all in an effort to make sure that there is only
16// a single cannonical version of a type.
17//
18//#define DEBUG_MERGE_TYPES 1
19
20
Chris Lattner00950542001-06-06 20:29:01 +000021//===----------------------------------------------------------------------===//
22// Type Class Implementation
23//===----------------------------------------------------------------------===//
24
25static unsigned CurUID = 0;
Chris Lattner47697a12003-05-22 21:21:43 +000026static std::vector<const Type *> UIDMappings;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattneraa06d2c2002-04-04 19:26:02 +000028void PATypeHolder::dump() const {
Chris Lattner47697a12003-05-22 21:21:43 +000029 std::cerr << "PATypeHolder(" << (void*)this << ")\n";
Chris Lattneraa06d2c2002-04-04 19:26:02 +000030}
31
Chris Lattner3855f2f2002-04-05 22:25:26 +000032
Chris Lattner47697a12003-05-22 21:21:43 +000033Type::Type(const std::string &name, PrimitiveID id)
Chris Lattnerc038a2f2001-09-07 16:56:42 +000034 : Value(Type::TypeTy, Value::TypeVal) {
35 setDescription(name);
Chris Lattner00950542001-06-06 20:29:01 +000036 ID = id;
Chris Lattner55fd9982001-10-30 16:39:16 +000037 Abstract = Recursive = false;
Chris Lattner00950542001-06-06 20:29:01 +000038 UID = CurUID++; // Assign types UID's as they are created
39 UIDMappings.push_back(this);
40}
41
Chris Lattner47697a12003-05-22 21:21:43 +000042void Type::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +000043 assert(ST && "Type::setName - Must provide symbol table argument!");
44
45 if (Name.size()) ST->insert(Name, this);
46}
47
48
Chris Lattner00950542001-06-06 20:29:01 +000049const Type *Type::getUniqueIDType(unsigned UID) {
50 assert(UID < UIDMappings.size() &&
51 "Type::getPrimitiveType: UID out of range!");
52 return UIDMappings[UID];
53}
54
55const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
56 switch (IDNumber) {
57 case VoidTyID : return VoidTy;
58 case BoolTyID : return BoolTy;
59 case UByteTyID : return UByteTy;
60 case SByteTyID : return SByteTy;
61 case UShortTyID: return UShortTy;
62 case ShortTyID : return ShortTy;
63 case UIntTyID : return UIntTy;
64 case IntTyID : return IntTy;
65 case ULongTyID : return ULongTy;
66 case LongTyID : return LongTy;
67 case FloatTyID : return FloatTy;
68 case DoubleTyID: return DoubleTy;
69 case TypeTyID : return TypeTy;
70 case LabelTyID : return LabelTy;
Chris Lattner00950542001-06-06 20:29:01 +000071 default:
72 return 0;
73 }
74}
75
Misha Brukmanf117cc92003-05-20 18:45:36 +000076// isLosslesslyConvertibleTo - Return true if this type can be converted to
Chris Lattner58716b92001-11-26 17:01:47 +000077// 'Ty' without any reinterpretation of bits. For example, uint to int.
78//
Misha Brukmanf117cc92003-05-20 18:45:36 +000079bool Type::isLosslesslyConvertibleTo(const Type *Ty) const {
Chris Lattner58716b92001-11-26 17:01:47 +000080 if (this == Ty) return true;
Chris Lattnerd44023e2002-05-06 16:14:39 +000081 if ((!isPrimitiveType() && !isa<PointerType>(this)) ||
82 (!isa<PointerType>(Ty) && !Ty->isPrimitiveType())) return false;
Chris Lattner58716b92001-11-26 17:01:47 +000083
84 if (getPrimitiveID() == Ty->getPrimitiveID())
85 return true; // Handles identity cast, and cast of differing pointer types
86
87 // Now we know that they are two differing primitive or pointer types
88 switch (getPrimitiveID()) {
89 case Type::UByteTyID: return Ty == Type::SByteTy;
90 case Type::SByteTyID: return Ty == Type::UByteTy;
91 case Type::UShortTyID: return Ty == Type::ShortTy;
92 case Type::ShortTyID: return Ty == Type::UShortTy;
93 case Type::UIntTyID: return Ty == Type::IntTy;
94 case Type::IntTyID: return Ty == Type::UIntTy;
95 case Type::ULongTyID:
96 case Type::LongTyID:
97 case Type::PointerTyID:
Chris Lattnerc72114c2002-04-27 02:26:03 +000098 return Ty == Type::ULongTy || Ty == Type::LongTy || isa<PointerType>(Ty);
Chris Lattner58716b92001-11-26 17:01:47 +000099 default:
100 return false; // Other types have no identity values
101 }
102}
103
Chris Lattnerd44023e2002-05-06 16:14:39 +0000104// getPrimitiveSize - Return the basic size of this type if it is a primative
105// type. These are fixed by LLVM and are not target dependant. This will
106// return zero if the type does not have a size or is not a primitive type.
107//
108unsigned Type::getPrimitiveSize() const {
109 switch (getPrimitiveID()) {
110#define HANDLE_PRIM_TYPE(TY,SIZE) case TY##TyID: return SIZE;
111#include "llvm/Type.def"
112 default: return 0;
113 }
114}
115
Chris Lattner58716b92001-11-26 17:01:47 +0000116
117bool StructType::indexValid(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 if (!isa<Constant>(V)) return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000119 if (V->getType() != Type::UByteTy) return false;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000120 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000121 return Idx < ETypes.size();
122}
123
124// getTypeAtIndex - Given an index value into the type, return the type of the
125// element. For a structure type, this must be a constant value...
126//
127const Type *StructType::getTypeAtIndex(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000128 assert(isa<Constant>(V) && "Structure index must be a constant!!");
Chris Lattner58716b92001-11-26 17:01:47 +0000129 assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000130 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000131 assert(Idx < ETypes.size() && "Structure index out of range!");
132 assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
133
134 return ETypes[Idx];
135}
136
137
Chris Lattner00950542001-06-06 20:29:01 +0000138//===----------------------------------------------------------------------===//
139// Auxilliary classes
140//===----------------------------------------------------------------------===//
141//
142// These classes are used to implement specialized behavior for each different
143// type.
144//
Chris Lattner0c4e8862002-09-03 01:08:28 +0000145struct SignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000146 SignedIntType(const std::string &Name, PrimitiveID id) : Type(Name, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000147
148 // isSigned - Return whether a numeric type is signed.
149 virtual bool isSigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000150
Chris Lattner0c4e8862002-09-03 01:08:28 +0000151 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000152 // virtual function invocation.
153 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000154 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000155};
156
Chris Lattner0c4e8862002-09-03 01:08:28 +0000157struct UnsignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000158 UnsignedIntType(const std::string &N, PrimitiveID id) : Type(N, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000159
160 // isUnsigned - Return whether a numeric type is signed.
161 virtual bool isUnsigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000162
Chris Lattner0c4e8862002-09-03 01:08:28 +0000163 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000164 // virtual function invocation.
165 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000166 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000167};
168
Chris Lattner950273b2003-05-22 21:31:52 +0000169struct OtherType : public Type {
170 OtherType(const std::string &N, PrimitiveID id) : Type(N, id) {}
171};
172
Chris Lattner00950542001-06-06 20:29:01 +0000173static struct TypeType : public Type {
174 TypeType() : Type("type", TypeTyID) {}
Chris Lattner950273b2003-05-22 21:31:52 +0000175} TheTypeTy; // Implement the type that is global.
Chris Lattner00950542001-06-06 20:29:01 +0000176
177
178//===----------------------------------------------------------------------===//
179// Static 'Type' data
180//===----------------------------------------------------------------------===//
181
Chris Lattner950273b2003-05-22 21:31:52 +0000182static OtherType TheVoidTy ("void" , Type::VoidTyID);
183static OtherType TheBoolTy ("bool" , Type::BoolTyID);
184static SignedIntType TheSByteTy ("sbyte" , Type::SByteTyID);
185static UnsignedIntType TheUByteTy ("ubyte" , Type::UByteTyID);
186static SignedIntType TheShortTy ("short" , Type::ShortTyID);
187static UnsignedIntType TheUShortTy("ushort", Type::UShortTyID);
188static SignedIntType TheIntTy ("int" , Type::IntTyID);
189static UnsignedIntType TheUIntTy ("uint" , Type::UIntTyID);
190static SignedIntType TheLongTy ("long" , Type::LongTyID);
191static UnsignedIntType TheULongTy ("ulong" , Type::ULongTyID);
192static OtherType TheFloatTy ("float" , Type::FloatTyID);
193static OtherType TheDoubleTy("double", Type::DoubleTyID);
194static OtherType TheLabelTy ("label" , Type::LabelTyID);
195
196Type *Type::VoidTy = &TheVoidTy;
197Type *Type::BoolTy = &TheBoolTy;
198Type *Type::SByteTy = &TheSByteTy;
199Type *Type::UByteTy = &TheUByteTy;
200Type *Type::ShortTy = &TheShortTy;
201Type *Type::UShortTy = &TheUShortTy;
202Type *Type::IntTy = &TheIntTy;
203Type *Type::UIntTy = &TheUIntTy;
204Type *Type::LongTy = &TheLongTy;
205Type *Type::ULongTy = &TheULongTy;
206Type *Type::FloatTy = &TheFloatTy;
207Type *Type::DoubleTy = &TheDoubleTy;
208Type *Type::TypeTy = &TheTypeTy;
209Type *Type::LabelTy = &TheLabelTy;
Chris Lattner00950542001-06-06 20:29:01 +0000210
211
212//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000213// Derived Type Constructors
214//===----------------------------------------------------------------------===//
215
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000216FunctionType::FunctionType(const Type *Result,
Chris Lattner47697a12003-05-22 21:21:43 +0000217 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000218 bool IsVarArgs) : DerivedType(FunctionTyID),
Chris Lattner893f0252003-06-18 19:22:36 +0000219 ResultType(PATypeHandle(Result, this)),
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000220 isVarArgs(IsVarArgs) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000221 ParamTys.reserve(Params.size());
Chris Lattner56c5acb2001-10-13 07:01:33 +0000222 for (unsigned i = 0; i < Params.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000223 ParamTys.push_back(PATypeHandle(Params[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000224
225 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000226}
227
Chris Lattner47697a12003-05-22 21:21:43 +0000228StructType::StructType(const std::vector<const Type*> &Types)
Chris Lattner6c42c312001-12-14 16:41:56 +0000229 : CompositeType(StructTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000230 ETypes.reserve(Types.size());
Chris Lattner56c5acb2001-10-13 07:01:33 +0000231 for (unsigned i = 0; i < Types.size(); ++i) {
232 assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");
Chris Lattner893f0252003-06-18 19:22:36 +0000233 ETypes.push_back(PATypeHandle(Types[i], this));
Chris Lattner56c5acb2001-10-13 07:01:33 +0000234 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000235 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000236}
237
Chris Lattner6c42c312001-12-14 16:41:56 +0000238ArrayType::ArrayType(const Type *ElType, unsigned NumEl)
239 : SequentialType(ArrayTyID, ElType) {
240 NumElements = NumEl;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000241 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000242}
243
Chris Lattner6c42c312001-12-14 16:41:56 +0000244PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
245 setDerivedTypeProperties();
246}
247
248OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000249 setAbstract(true);
250 setDescription("opaque"+utostr(getUniqueID()));
251#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000252 std::cerr << "Derived new type: " << getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000253#endif
254}
255
256
257
258
Chris Lattner00950542001-06-06 20:29:01 +0000259//===----------------------------------------------------------------------===//
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000260// Derived Type setDerivedTypeProperties Function
Chris Lattner00950542001-06-06 20:29:01 +0000261//===----------------------------------------------------------------------===//
262
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000263// getTypeProps - This is a recursive function that walks a type hierarchy
264// calculating the description for a type and whether or not it is abstract or
265// recursive. Worst case it will have to do a lot of traversing if you have
266// some whacko opaque types, but in most cases, it will do some simple stuff
267// when it hits non-abstract types that aren't recursive.
268//
Chris Lattner47697a12003-05-22 21:21:43 +0000269static std::string getTypeProps(const Type *Ty,
270 std::vector<const Type *> &TypeStack,
271 bool &isAbstract, bool &isRecursive) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000272 if (!Ty->isAbstract() && !Ty->isRecursive() && // Base case for the recursion
273 Ty->getDescription().size()) {
Chris Lattner6a490ce2003-01-14 19:42:39 +0000274 return Ty->getDescription(); // Primitive = leaf type
Chris Lattnerb00c5822001-10-02 03:41:24 +0000275 } else if (isa<OpaqueType>(Ty)) { // Base case for the recursion
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000276 isAbstract = true; // This whole type is abstract!
Chris Lattner6a490ce2003-01-14 19:42:39 +0000277 return Ty->getDescription(); // Opaque = leaf type
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000278 } else {
279 // Check to see if the Type is already on the stack...
280 unsigned Slot = 0, CurSize = TypeStack.size();
281 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
282
283 // This is another base case for the recursion. In this case, we know
284 // that we have looped back to a type that we have previously visited.
285 // Generate the appropriate upreference to handle this.
286 //
287 if (Slot < CurSize) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000288 isRecursive = true; // We know we are recursive
Chris Lattner6a490ce2003-01-14 19:42:39 +0000289 return "\\" + utostr(CurSize-Slot); // Here's the upreference
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000290 } else { // Recursive case: abstract derived type...
Chris Lattner47697a12003-05-22 21:21:43 +0000291 std::string Result;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000292 TypeStack.push_back(Ty); // Add us to the stack..
293
294 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000295 case Type::FunctionTyID: {
296 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000297 Result = getTypeProps(MTy->getReturnType(), TypeStack,
298 isAbstract, isRecursive)+" (";
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000299 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000300 I = MTy->getParamTypes().begin(),
301 E = MTy->getParamTypes().end(); I != E; ++I) {
302 if (I != MTy->getParamTypes().begin())
303 Result += ", ";
304 Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive);
305 }
306 if (MTy->isVarArg()) {
307 if (!MTy->getParamTypes().empty()) Result += ", ";
308 Result += "...";
309 }
310 Result += ")";
311 break;
312 }
313 case Type::StructTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000314 const StructType *STy = cast<const StructType>(Ty);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000315 Result = "{ ";
316 for (StructType::ElementTypes::const_iterator
317 I = STy->getElementTypes().begin(),
318 E = STy->getElementTypes().end(); I != E; ++I) {
319 if (I != STy->getElementTypes().begin())
320 Result += ", ";
321 Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive);
322 }
323 Result += " }";
324 break;
325 }
326 case Type::PointerTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000327 const PointerType *PTy = cast<const PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +0000328 Result = getTypeProps(PTy->getElementType(), TypeStack,
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000329 isAbstract, isRecursive) + " *";
330 break;
331 }
332 case Type::ArrayTyID: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000333 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner6c42c312001-12-14 16:41:56 +0000334 unsigned NumElements = ATy->getNumElements();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000335 Result = "[";
Chris Lattner6c42c312001-12-14 16:41:56 +0000336 Result += utostr(NumElements) + " x ";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000337 Result += getTypeProps(ATy->getElementType(), TypeStack,
338 isAbstract, isRecursive) + "]";
339 break;
340 }
341 default:
342 assert(0 && "Unhandled case in getTypeProps!");
343 Result = "<error>";
344 }
345
346 TypeStack.pop_back(); // Remove self from stack...
Chris Lattner6a490ce2003-01-14 19:42:39 +0000347 return Result;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000348 }
349 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000350}
351
352
353// setDerivedTypeProperties - This function is used to calculate the
354// isAbstract, isRecursive, and the Description settings for a type. The
355// getTypeProps function does all the dirty work.
356//
357void DerivedType::setDerivedTypeProperties() {
Chris Lattner47697a12003-05-22 21:21:43 +0000358 std::vector<const Type *> TypeStack;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000359 bool isAbstract = false, isRecursive = false;
360
361 setDescription(getTypeProps(this, TypeStack, isAbstract, isRecursive));
362 setAbstract(isAbstract);
363 setRecursive(isRecursive);
364}
365
366
367//===----------------------------------------------------------------------===//
368// Type Structural Equality Testing
369//===----------------------------------------------------------------------===//
370
371// TypesEqual - Two types are considered structurally equal if they have the
372// same "shape": Every level and element of the types have identical primitive
373// ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
374// be pointer equals to be equivalent though. This uses an optimistic algorithm
375// that assumes that two graphs are the same until proven otherwise.
376//
377static bool TypesEqual(const Type *Ty, const Type *Ty2,
Chris Lattner47697a12003-05-22 21:21:43 +0000378 std::map<const Type *, const Type *> &EqTypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000379 if (Ty == Ty2) return true;
380 if (Ty->getPrimitiveID() != Ty2->getPrimitiveID()) return false;
381 if (Ty->isPrimitiveType()) return true;
Chris Lattner008f9062001-10-24 05:12:04 +0000382 if (isa<OpaqueType>(Ty))
383 return false; // Two nonequal opaque types are never equal
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000384
Chris Lattner47697a12003-05-22 21:21:43 +0000385 std::map<const Type*, const Type*>::iterator It = EqTypes.find(Ty);
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000386 if (It != EqTypes.end())
387 return It->second == Ty2; // Looping back on a type, check for equality
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000388
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000389 // Otherwise, add the mapping to the table to make sure we don't get
390 // recursion on the types...
Chris Lattner47697a12003-05-22 21:21:43 +0000391 EqTypes.insert(std::make_pair(Ty, Ty2));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000392
393 // Iterate over the types and make sure the the contents are equivalent...
Chris Lattner74c2b762001-09-09 22:26:58 +0000394 Type::subtype_iterator I = Ty ->subtype_begin(), IE = Ty ->subtype_end();
395 Type::subtype_iterator I2 = Ty2->subtype_begin(), IE2 = Ty2->subtype_end();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000396 for (; I != IE && I2 != IE2; ++I, ++I2)
397 if (!TypesEqual(*I, *I2, EqTypes)) return false;
398
Chris Lattner56c5acb2001-10-13 07:01:33 +0000399 // Two really annoying special cases that breaks an otherwise nice simple
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000400 // algorithm is the fact that arraytypes have sizes that differentiates types,
Chris Lattner56c5acb2001-10-13 07:01:33 +0000401 // and that method types can be varargs or not. Consider this now.
402 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
403 if (ATy->getNumElements() != cast<const ArrayType>(Ty2)->getNumElements())
404 return false;
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000405 } else if (const FunctionType *MTy = dyn_cast<FunctionType>(Ty)) {
406 if (MTy->isVarArg() != cast<const FunctionType>(Ty2)->isVarArg())
Chris Lattner56c5acb2001-10-13 07:01:33 +0000407 return false;
408 }
409
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000410 return I == IE && I2 == IE2; // Types equal if both iterators are done
411}
412
413static bool TypesEqual(const Type *Ty, const Type *Ty2) {
Chris Lattner47697a12003-05-22 21:21:43 +0000414 std::map<const Type *, const Type *> EqTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000415 return TypesEqual(Ty, Ty2, EqTypes);
416}
417
418
419
420//===----------------------------------------------------------------------===//
421// Derived Type Factory Functions
422//===----------------------------------------------------------------------===//
423
424// TypeMap - Make sure that only one instance of a particular type may be
425// created on any given run of the compiler... note that this involves updating
426// our map if an abstract type gets refined somehow...
427//
428template<class ValType, class TypeClass>
429class TypeMap : public AbstractTypeUser {
Chris Lattner893f0252003-06-18 19:22:36 +0000430 typedef std::map<ValType, PATypeHandle> MapTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000431 MapTy Map;
432public:
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000433 ~TypeMap() { print("ON EXIT"); }
434
435 inline TypeClass *get(const ValType &V) {
Chris Lattner893f0252003-06-18 19:22:36 +0000436 typename std::map<ValType, PATypeHandle>::iterator I
Chris Lattner47697a12003-05-22 21:21:43 +0000437 = Map.find(V);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000438 // TODO: FIXME: When Types are not CONST.
439 return (I != Map.end()) ? (TypeClass*)I->second.get() : 0;
440 }
441
442 inline void add(const ValType &V, TypeClass *T) {
Chris Lattner893f0252003-06-18 19:22:36 +0000443 Map.insert(std::make_pair(V, PATypeHandle(T, this)));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000444 print("add");
445 }
446
447 // containsEquivalent - Return true if the typemap contains a type that is
448 // structurally equivalent to the specified type.
449 //
450 inline const TypeClass *containsEquivalent(const TypeClass *Ty) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000451 for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000452 if (I->second.get() != Ty && TypesEqual(Ty, I->second.get()))
453 return (TypeClass*)I->second.get(); // FIXME TODO when types not const
454 return 0;
455 }
456
457 // refineAbstractType - This is called when one of the contained abstract
458 // types gets refined... this simply removes the abstract type from our table.
459 // We expect that whoever refined the type will add it back to the table,
460 // corrected.
461 //
462 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000463#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000464 std::cerr << "Removing Old type from Tab: " << (void*)OldTy << ", "
465 << OldTy->getDescription() << " replacement == " << (void*)NewTy
466 << ", " << NewTy->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000467#endif
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000468 for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000469 if (I->second == OldTy) {
Chris Lattner417081c2002-04-07 06:14:56 +0000470 // Check to see if the type just became concrete. If so, remove self
471 // from user list.
472 I->second.removeUserFromConcrete();
473 I->second = cast<TypeClass>(NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000474 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000475 }
476
477 void remove(const ValType &OldVal) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000478 typename MapTy::iterator I = Map.find(OldVal);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000479 assert(I != Map.end() && "TypeMap::remove, element not found!");
480 Map.erase(I);
481 }
482
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000483 void print(const char *Arg) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000484#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000485 std::cerr << "TypeMap<>::" << Arg << " table contents:\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000486 unsigned i = 0;
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000487 for (MapTy::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattner47697a12003-05-22 21:21:43 +0000488 std::cerr << " " << (++i) << ". " << I->second << " "
489 << I->second->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000490#endif
491 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000492
493 void dump() const { print("dump output"); }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000494};
495
496
497// ValTypeBase - This is the base class that is used by the various
498// instantiations of TypeMap. This class is an AbstractType user that notifies
499// the underlying TypeMap when it gets modified.
500//
501template<class ValType, class TypeClass>
502class ValTypeBase : public AbstractTypeUser {
503 TypeMap<ValType, TypeClass> &MyTable;
504protected:
505 inline ValTypeBase(TypeMap<ValType, TypeClass> &tab) : MyTable(tab) {}
506
507 // Subclass should override this... to update self as usual
508 virtual void doRefinement(const DerivedType *OldTy, const Type *NewTy) = 0;
Chris Lattnere244a252001-11-03 03:27:53 +0000509
510 // typeBecameConcrete - This callback occurs when a contained type refines
511 // to itself, but becomes concrete in the process. Our subclass should remove
512 // itself from the ATU list of the specified type.
513 //
514 virtual void typeBecameConcrete(const DerivedType *Ty) = 0;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000515
516 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner3855f2f2002-04-05 22:25:26 +0000517 assert(OldTy == NewTy || OldTy->isAbstract());
Chris Lattner417081c2002-04-07 06:14:56 +0000518
519 if (!OldTy->isAbstract())
520 typeBecameConcrete(OldTy);
521
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000522 TypeMap<ValType, TypeClass> &Table = MyTable; // Copy MyTable reference
523 ValType Tmp(*(ValType*)this); // Copy this.
Chris Lattner893f0252003-06-18 19:22:36 +0000524 PATypeHandle OldType(Table.get(*(ValType*)this), this);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000525 Table.remove(*(ValType*)this); // Destroy's this!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000526
Chris Lattner417081c2002-04-07 06:14:56 +0000527 // Refine temporary to new state...
528 if (OldTy != NewTy)
529 Tmp.doRefinement(OldTy, NewTy);
530
531 // FIXME: when types are not const!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000532 Table.add((ValType&)Tmp, (TypeClass*)OldType.get());
533 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000534
535 void dump() const {
Chris Lattner47697a12003-05-22 21:21:43 +0000536 std::cerr << "ValTypeBase instance!\n";
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000537 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000538};
539
540
541
542//===----------------------------------------------------------------------===//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000543// Function Type Factory and Value Class...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000544//
545
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000546// FunctionValType - Define a class to hold the key that goes into the TypeMap
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000547//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000548class FunctionValType : public ValTypeBase<FunctionValType, FunctionType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000549 PATypeHandle RetTy;
550 std::vector<PATypeHandle> ArgTypes;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000551 bool isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000552public:
Chris Lattner47697a12003-05-22 21:21:43 +0000553 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000554 bool IVA, TypeMap<FunctionValType, FunctionType> &Tab)
555 : ValTypeBase<FunctionValType, FunctionType>(Tab), RetTy(ret, this),
Chris Lattner56c5acb2001-10-13 07:01:33 +0000556 isVarArg(IVA) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000557 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000558 ArgTypes.push_back(PATypeHandle(args[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000559 }
560
561 // We *MUST* have an explicit copy ctor so that the TypeHandles think that
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000562 // this FunctionValType owns them, not the old one!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000563 //
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000564 FunctionValType(const FunctionValType &MVT)
565 : ValTypeBase<FunctionValType, FunctionType>(MVT), RetTy(MVT.RetTy, this),
Chris Lattner56c5acb2001-10-13 07:01:33 +0000566 isVarArg(MVT.isVarArg) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000567 ArgTypes.reserve(MVT.ArgTypes.size());
568 for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000569 ArgTypes.push_back(PATypeHandle(MVT.ArgTypes[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000570 }
571
572 // Subclass should override this... to update self as usual
573 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
574 if (RetTy == OldType) RetTy = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +0000575 for (unsigned i = 0, e = ArgTypes.size(); i != e; ++i)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000576 if (ArgTypes[i] == OldType) ArgTypes[i] = NewType;
577 }
578
Chris Lattnere244a252001-11-03 03:27:53 +0000579 virtual void typeBecameConcrete(const DerivedType *Ty) {
580 if (RetTy == Ty) RetTy.removeUserFromConcrete();
581
582 for (unsigned i = 0; i < ArgTypes.size(); ++i)
583 if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete();
584 }
585
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000586 inline bool operator<(const FunctionValType &MTV) const {
Chris Lattner56c5acb2001-10-13 07:01:33 +0000587 if (RetTy.get() < MTV.RetTy.get()) return true;
588 if (RetTy.get() > MTV.RetTy.get()) return false;
589
590 if (ArgTypes < MTV.ArgTypes) return true;
591 return (ArgTypes == MTV.ArgTypes) && isVarArg < MTV.isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000592 }
593};
594
595// Define the actual map itself now...
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000596static TypeMap<FunctionValType, FunctionType> FunctionTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000597
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000598// FunctionType::get - The factory function for the FunctionType class...
599FunctionType *FunctionType::get(const Type *ReturnType,
Chris Lattner47697a12003-05-22 21:21:43 +0000600 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000601 bool isVarArg) {
602 FunctionValType VT(ReturnType, Params, isVarArg, FunctionTypes);
603 FunctionType *MT = FunctionTypes.get(VT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000604 if (MT) return MT;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000605
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000606 FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000607
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000608#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000609 std::cerr << "Derived new type: " << MT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000610#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000611 return MT;
Chris Lattner00950542001-06-06 20:29:01 +0000612}
613
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000614//===----------------------------------------------------------------------===//
615// Array Type Factory...
616//
617class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000618 PATypeHandle ValTy;
Chris Lattner6c42c312001-12-14 16:41:56 +0000619 unsigned Size;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000620public:
621 ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab)
622 : ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {}
Chris Lattner00950542001-06-06 20:29:01 +0000623
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000624 // We *MUST* have an explicit copy ctor so that the ValTy thinks that this
625 // ArrayValType owns it, not the old one!
626 //
627 ArrayValType(const ArrayValType &AVT)
628 : ValTypeBase<ArrayValType, ArrayType>(AVT), ValTy(AVT.ValTy, this),
629 Size(AVT.Size) {}
Chris Lattner00950542001-06-06 20:29:01 +0000630
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000631 // Subclass should override this... to update self as usual
632 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000633 assert(ValTy == OldType);
634 ValTy = NewType;
Chris Lattner00950542001-06-06 20:29:01 +0000635 }
636
Chris Lattnere244a252001-11-03 03:27:53 +0000637 virtual void typeBecameConcrete(const DerivedType *Ty) {
638 assert(ValTy == Ty &&
639 "Contained type became concrete but we're not using it!");
640 ValTy.removeUserFromConcrete();
641 }
642
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000643 inline bool operator<(const ArrayValType &MTV) const {
644 if (Size < MTV.Size) return true;
645 return Size == MTV.Size && ValTy.get() < MTV.ValTy.get();
646 }
647};
648
649static TypeMap<ArrayValType, ArrayType> ArrayTypes;
650
Chris Lattner6c42c312001-12-14 16:41:56 +0000651ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000652 assert(ElementType && "Can't get array of null types!");
653
654 ArrayValType AVT(ElementType, NumElements, ArrayTypes);
655 ArrayType *AT = ArrayTypes.get(AVT);
656 if (AT) return AT; // Found a match, return it!
657
Chris Lattner00950542001-06-06 20:29:01 +0000658 // Value not found. Derive a new type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000659 ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements));
Chris Lattner00950542001-06-06 20:29:01 +0000660
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000661#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000662 std::cerr << "Derived new type: " << AT->getDescription() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000663#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000664 return AT;
Chris Lattner00950542001-06-06 20:29:01 +0000665}
666
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000667//===----------------------------------------------------------------------===//
668// Struct Type Factory...
669//
Chris Lattner00950542001-06-06 20:29:01 +0000670
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000671// StructValType - Define a class to hold the key that goes into the TypeMap
672//
673class StructValType : public ValTypeBase<StructValType, StructType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000674 std::vector<PATypeHandle> ElTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000675public:
Chris Lattner47697a12003-05-22 21:21:43 +0000676 StructValType(const std::vector<const Type*> &args,
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000677 TypeMap<StructValType, StructType> &Tab)
678 : ValTypeBase<StructValType, StructType>(Tab) {
Chris Lattner3855f2f2002-04-05 22:25:26 +0000679 ElTypes.reserve(args.size());
680 for (unsigned i = 0, e = args.size(); i != e; ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000681 ElTypes.push_back(PATypeHandle(args[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000682 }
Chris Lattner00950542001-06-06 20:29:01 +0000683
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000684 // We *MUST* have an explicit copy ctor so that the TypeHandles think that
685 // this StructValType owns them, not the old one!
686 //
687 StructValType(const StructValType &SVT)
688 : ValTypeBase<StructValType, StructType>(SVT){
689 ElTypes.reserve(SVT.ElTypes.size());
Chris Lattner3855f2f2002-04-05 22:25:26 +0000690 for (unsigned i = 0, e = SVT.ElTypes.size(); i != e; ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000691 ElTypes.push_back(PATypeHandle(SVT.ElTypes[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000692 }
693
694 // Subclass should override this... to update self as usual
695 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
696 for (unsigned i = 0; i < ElTypes.size(); ++i)
697 if (ElTypes[i] == OldType) ElTypes[i] = NewType;
698 }
699
Chris Lattnere244a252001-11-03 03:27:53 +0000700 virtual void typeBecameConcrete(const DerivedType *Ty) {
Chris Lattner417081c2002-04-07 06:14:56 +0000701 for (unsigned i = 0, e = ElTypes.size(); i != e; ++i)
702 if (ElTypes[i] == Ty)
703 ElTypes[i].removeUserFromConcrete();
Chris Lattnere244a252001-11-03 03:27:53 +0000704 }
705
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000706 inline bool operator<(const StructValType &STV) const {
707 return ElTypes < STV.ElTypes;
708 }
709};
710
711static TypeMap<StructValType, StructType> StructTypes;
712
Chris Lattner47697a12003-05-22 21:21:43 +0000713StructType *StructType::get(const std::vector<const Type*> &ETypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000714 StructValType STV(ETypes, StructTypes);
715 StructType *ST = StructTypes.get(STV);
716 if (ST) return ST;
717
718 // Value not found. Derive a new type!
719 StructTypes.add(STV, ST = new StructType(ETypes));
720
721#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000722 std::cerr << "Derived new type: " << ST->getDescription() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000723#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000724 return ST;
725}
726
727//===----------------------------------------------------------------------===//
728// Pointer Type Factory...
729//
730
731// PointerValType - Define a class to hold the key that goes into the TypeMap
732//
733class PointerValType : public ValTypeBase<PointerValType, PointerType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000734 PATypeHandle ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000735public:
736 PointerValType(const Type *val, TypeMap<PointerValType, PointerType> &Tab)
737 : ValTypeBase<PointerValType, PointerType>(Tab), ValTy(val, this) {}
738
739 // We *MUST* have an explicit copy ctor so that the ValTy thinks that this
740 // PointerValType owns it, not the old one!
741 //
742 PointerValType(const PointerValType &PVT)
743 : ValTypeBase<PointerValType, PointerType>(PVT), ValTy(PVT.ValTy, this) {}
744
745 // Subclass should override this... to update self as usual
746 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000747 assert(ValTy == OldType);
748 ValTy = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000749 }
750
Chris Lattnere244a252001-11-03 03:27:53 +0000751 virtual void typeBecameConcrete(const DerivedType *Ty) {
752 assert(ValTy == Ty &&
753 "Contained type became concrete but we're not using it!");
754 ValTy.removeUserFromConcrete();
755 }
756
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000757 inline bool operator<(const PointerValType &MTV) const {
758 return ValTy.get() < MTV.ValTy.get();
759 }
760};
761
762static TypeMap<PointerValType, PointerType> PointerTypes;
763
764PointerType *PointerType::get(const Type *ValueType) {
765 assert(ValueType && "Can't get a pointer to <null> type!");
766 PointerValType PVT(ValueType, PointerTypes);
767
768 PointerType *PT = PointerTypes.get(PVT);
769 if (PT) return PT;
770
771 // Value not found. Derive a new type!
772 PointerTypes.add(PVT, PT = new PointerType(ValueType));
773
774#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000775 std::cerr << "Derived new type: " << PT->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000776#endif
777 return PT;
778}
779
Chris Lattner339ba452002-04-06 00:21:11 +0000780void debug_type_tables() {
781 FunctionTypes.dump();
782 ArrayTypes.dump();
783 StructTypes.dump();
784 PointerTypes.dump();
785}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000786
787
788//===----------------------------------------------------------------------===//
789// Derived Type Refinement Functions
790//===----------------------------------------------------------------------===//
791
Chris Lattner3855f2f2002-04-05 22:25:26 +0000792// addAbstractTypeUser - Notify an abstract type that there is a new user of
793// it. This function is called primarily by the PATypeHandle class.
794//
795void DerivedType::addAbstractTypeUser(AbstractTypeUser *U) const {
796 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
Chris Lattner3855f2f2002-04-05 22:25:26 +0000797
798#if DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000799 std::cerr << " addAbstractTypeUser[" << (void*)this << ", "
800 << getDescription() << "][" << AbstractTypeUsers.size()
801 << "] User = " << U << "\n";
Chris Lattner3855f2f2002-04-05 22:25:26 +0000802#endif
803 AbstractTypeUsers.push_back(U);
804}
805
806
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000807// removeAbstractTypeUser - Notify an abstract type that a user of the class
808// no longer has a handle to the type. This function is called primarily by
809// the PATypeHandle class. When there are no users of the abstract type, it
810// is anihilated, because there is no way to get a reference to it ever again.
811//
812void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const {
813 // Search from back to front because we will notify users from back to
814 // front. Also, it is likely that there will be a stack like behavior to
815 // users that register and unregister users.
816 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000817 unsigned i;
818 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
819 assert(i != 0 && "AbstractTypeUser not in user list!");
820
821 --i; // Convert to be in range 0 <= i < size()
822 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
823
824 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000825
826#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000827 std::cerr << " remAbstractTypeUser[" << (void*)this << ", "
828 << getDescription() << "][" << i << "] User = " << U << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000829#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000830
831 if (AbstractTypeUsers.empty() && isAbstract()) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000832#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000833 std::cerr << "DELETEing unused abstract type: <" << getDescription()
834 << ">[" << (void*)this << "]" << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000835#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000836 delete this; // No users of this abstract type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000837 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000838}
839
840
841// refineAbstractTypeTo - This function is used to when it is discovered that
842// the 'this' abstract type is actually equivalent to the NewType specified.
843// This causes all users of 'this' to switch to reference the more concrete
844// type NewType and for 'this' to be deleted.
845//
846void DerivedType::refineAbstractTypeTo(const Type *NewType) {
847 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
848 assert(this != NewType && "Can't refine to myself!");
849
850#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000851 std::cerr << "REFINING abstract type [" << (void*)this << " "
852 << getDescription() << "] to [" << (void*)NewType << " "
853 << NewType->getDescription() << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000854#endif
855
856
857 // Make sure to put the type to be refined to into a holder so that if IT gets
858 // refined, that we will not continue using a dead reference...
859 //
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000860 PATypeHolder NewTy(NewType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000861
862 // Add a self use of the current type so that we don't delete ourself until
863 // after this while loop. We are careful to never invoke refine on ourself,
864 // so this extra reference shouldn't be a problem. Note that we must only
865 // remove a single reference at the end, but we must tolerate multiple self
866 // references because we could be refineAbstractTypeTo'ing recursively on the
867 // same type.
868 //
869 addAbstractTypeUser(this);
870
871 // Count the number of self uses. Stop looping when sizeof(list) == NSU.
872 unsigned NumSelfUses = 0;
873
874 // Iterate over all of the uses of this type, invoking callback. Each user
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000875 // should remove itself from our use list automatically. We have to check to
876 // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types
877 // will not cause users to drop off of the use list. If we resolve to ourself
878 // we succeed!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000879 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000880 while (AbstractTypeUsers.size() > NumSelfUses && NewTy != this) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000881 AbstractTypeUser *User = AbstractTypeUsers.back();
882
883 if (User == this) {
884 // Move self use to the start of the list. Increment NSU.
Chris Lattner47697a12003-05-22 21:21:43 +0000885 std::swap(AbstractTypeUsers.back(), AbstractTypeUsers[NumSelfUses++]);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000886 } else {
887 unsigned OldSize = AbstractTypeUsers.size();
888#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000889 std::cerr << " REFINING user " << OldSize-1 << "[" << (void*)User
890 << "] of abstract type [" << (void*)this << " "
891 << getDescription() << "] to [" << (void*)NewTy.get() << " "
892 << NewTy->getDescription() << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000893#endif
Chris Lattner008f9062001-10-24 05:12:04 +0000894 User->refineAbstractType(this, NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000895
Chris Lattnercecb5202002-04-05 19:53:06 +0000896#ifdef DEBUG_MERGE_TYPES
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000897 if (AbstractTypeUsers.size() == OldSize) {
Chris Lattnercecb5202002-04-05 19:53:06 +0000898 User->refineAbstractType(this, NewTy);
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000899 if (AbstractTypeUsers.back() != User)
Chris Lattner47697a12003-05-22 21:21:43 +0000900 std::cerr << "User changed!\n";
901 std::cerr << "Top of user list is:\n";
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000902 AbstractTypeUsers.back()->dump();
903
Chris Lattner47697a12003-05-22 21:21:43 +0000904 std::cerr <<"\nOld User=\n";
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000905 User->dump();
906 }
Chris Lattnercecb5202002-04-05 19:53:06 +0000907#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000908 assert(AbstractTypeUsers.size() != OldSize &&
909 "AbsTyUser did not remove self from user list!");
Chris Lattner00950542001-06-06 20:29:01 +0000910 }
911 }
912
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000913 // Remove a single self use, even though there may be several here. This will
914 // probably 'delete this', so no instance variables may be used after this
915 // occurs...
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000916 //
917 assert((NewTy == this || AbstractTypeUsers.back() == this) &&
918 "Only self uses should be left!");
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000919 removeAbstractTypeUser(this);
Chris Lattner00950542001-06-06 20:29:01 +0000920}
921
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000922// typeIsRefined - Notify AbstractTypeUsers of this type that the current type
923// has been refined a bit. The pointer is still valid and still should be
924// used, but the subtypes have changed.
925//
926void DerivedType::typeIsRefined() {
927 assert(isRefining >= 0 && isRefining <= 2 && "isRefining out of bounds!");
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000928 if (isRefining == 1) return; // Kill recursion here...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000929 ++isRefining;
Chris Lattner00950542001-06-06 20:29:01 +0000930
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000931#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000932 std::cerr << "typeIsREFINED type: " << (void*)this <<" "<<getDescription()
933 << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000934#endif
Chris Lattner417081c2002-04-07 06:14:56 +0000935
936 // In this loop we have to be very careful not to get into infinite loops and
937 // other problem cases. Specifically, we loop through all of the abstract
938 // type users in the user list, notifying them that the type has been refined.
939 // At their choice, they may or may not choose to remove themselves from the
940 // list of users. Regardless of whether they do or not, we have to be sure
941 // that we only notify each user exactly once. Because the refineAbstractType
942 // method can cause an arbitrary permutation to the user list, we cannot loop
943 // through it in any particular order and be guaranteed that we will be
944 // successful at this aim. Because of this, we keep track of all the users we
945 // have visited and only visit users we have not seen. Because this user list
946 // should be small, we use a vector instead of a full featured set to keep
947 // track of what users we have notified so far.
948 //
Chris Lattner47697a12003-05-22 21:21:43 +0000949 std::vector<AbstractTypeUser*> Refined;
Chris Lattner417081c2002-04-07 06:14:56 +0000950 while (1) {
951 unsigned i;
952 for (i = AbstractTypeUsers.size(); i != 0; --i)
953 if (find(Refined.begin(), Refined.end(), AbstractTypeUsers[i-1]) ==
954 Refined.end())
955 break; // Found an unrefined user?
956
957 if (i == 0) break; // Noone to refine left, break out of here!
958
959 AbstractTypeUser *ATU = AbstractTypeUsers[--i];
960 Refined.push_back(ATU); // Keep track of which users we have refined!
961
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000962#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000963 std::cerr << " typeIsREFINED user " << i << "[" << ATU
964 << "] of abstract type [" << (void*)this << " "
965 << getDescription() << "]\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000966#endif
967 ATU->refineAbstractType(this, this);
Chris Lattner00950542001-06-06 20:29:01 +0000968 }
969
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000970 --isRefining;
Chris Lattnere244a252001-11-03 03:27:53 +0000971
972#ifndef _NDEBUG
973 if (!(isAbstract() || AbstractTypeUsers.empty()))
974 for (unsigned i = 0; i < AbstractTypeUsers.size(); ++i) {
975 if (AbstractTypeUsers[i] != this) {
976 // Debugging hook
Chris Lattner47697a12003-05-22 21:21:43 +0000977 std::cerr << "FOUND FAILURE\nUser: ";
Chris Lattnercecb5202002-04-05 19:53:06 +0000978 AbstractTypeUsers[i]->dump();
Chris Lattner47697a12003-05-22 21:21:43 +0000979 std::cerr << "\nCatch:\n";
Chris Lattnere244a252001-11-03 03:27:53 +0000980 AbstractTypeUsers[i]->refineAbstractType(this, this);
981 assert(0 && "Type became concrete,"
982 " but it still has abstract type users hanging around!");
983 }
984 }
985#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000986}
987
Chris Lattner00950542001-06-06 20:29:01 +0000988
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000989
990
991// refineAbstractType - Called when a contained type is found to be more
992// concrete - this could potentially change us from an abstract type to a
993// concrete type.
994//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000995void FunctionType::refineAbstractType(const DerivedType *OldType,
996 const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000997#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000998 std::cerr << "FunctionTy::refineAbstractTy(" << (void*)OldType << "["
999 << OldType->getDescription() << "], " << (void*)NewType << " ["
1000 << NewType->getDescription() << "])\n";
Chris Lattner00950542001-06-06 20:29:01 +00001001#endif
Chris Lattner3855f2f2002-04-05 22:25:26 +00001002 // Find the type element we are refining...
Chris Lattner417081c2002-04-07 06:14:56 +00001003 if (ResultType == OldType) {
1004 ResultType.removeUserFromConcrete();
1005 ResultType = NewType;
Chris Lattnere244a252001-11-03 03:27:53 +00001006 }
Chris Lattner417081c2002-04-07 06:14:56 +00001007 for (unsigned i = 0, e = ParamTys.size(); i != e; ++i)
1008 if (ParamTys[i] == OldType) {
1009 ParamTys[i].removeUserFromConcrete();
1010 ParamTys[i] = NewType;
1011 }
Chris Lattnere244a252001-11-03 03:27:53 +00001012
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001013 const FunctionType *MT = FunctionTypes.containsEquivalent(this);
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001014 if (MT && MT != this) {
Chris Lattnere244a252001-11-03 03:27:53 +00001015 refineAbstractTypeTo(MT); // Different type altogether...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001016 } else {
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001017 setDerivedTypeProperties(); // Update the name and isAbstract
Chris Lattnere244a252001-11-03 03:27:53 +00001018 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001019 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001020}
1021
1022
1023// refineAbstractType - Called when a contained type is found to be more
1024// concrete - this could potentially change us from an abstract type to a
1025// concrete type.
1026//
1027void ArrayType::refineAbstractType(const DerivedType *OldType,
1028 const Type *NewType) {
1029#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001030 std::cerr << "ArrayTy::refineAbstractTy(" << (void*)OldType << "["
1031 << OldType->getDescription() << "], " << (void*)NewType << " ["
1032 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001033#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001034
Chris Lattner417081c2002-04-07 06:14:56 +00001035 assert(getElementType() == OldType);
1036 ElementType.removeUserFromConcrete();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001037 ElementType = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +00001038
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001039 const ArrayType *AT = ArrayTypes.containsEquivalent(this);
1040 if (AT && AT != this) {
1041 refineAbstractTypeTo(AT); // Different type altogether...
1042 } else {
1043 setDerivedTypeProperties(); // Update the name and isAbstract
1044 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001045 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001046}
1047
1048
1049// refineAbstractType - Called when a contained type is found to be more
1050// concrete - this could potentially change us from an abstract type to a
1051// concrete type.
1052//
1053void StructType::refineAbstractType(const DerivedType *OldType,
1054 const Type *NewType) {
1055#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001056 std::cerr << "StructTy::refineAbstractTy(" << (void*)OldType << "["
1057 << OldType->getDescription() << "], " << (void*)NewType << " ["
1058 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001059#endif
Chris Lattner6a490ce2003-01-14 19:42:39 +00001060 for (int i = ETypes.size()-1; i >= 0; --i)
Chris Lattner417081c2002-04-07 06:14:56 +00001061 if (ETypes[i] == OldType) {
1062 ETypes[i].removeUserFromConcrete();
Chris Lattnere244a252001-11-03 03:27:53 +00001063
Chris Lattner417081c2002-04-07 06:14:56 +00001064 // Update old type to new type in the array...
1065 ETypes[i] = NewType;
1066 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001067
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001068 const StructType *ST = StructTypes.containsEquivalent(this);
1069 if (ST && ST != this) {
1070 refineAbstractTypeTo(ST); // Different type altogether...
1071 } else {
1072 setDerivedTypeProperties(); // Update the name and isAbstract
1073 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001074 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001075}
1076
1077// refineAbstractType - Called when a contained type is found to be more
1078// concrete - this could potentially change us from an abstract type to a
1079// concrete type.
1080//
1081void PointerType::refineAbstractType(const DerivedType *OldType,
1082 const Type *NewType) {
1083#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001084 std::cerr << "PointerTy::refineAbstractTy(" << (void*)OldType << "["
1085 << OldType->getDescription() << "], " << (void*)NewType << " ["
1086 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001087#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001088
Chris Lattner417081c2002-04-07 06:14:56 +00001089 assert(ElementType == OldType);
1090 ElementType.removeUserFromConcrete();
Chris Lattner6c42c312001-12-14 16:41:56 +00001091 ElementType = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001092
Chris Lattner417081c2002-04-07 06:14:56 +00001093 const PointerType *PT = PointerTypes.containsEquivalent(this);
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001094 if (PT && PT != this) {
1095 refineAbstractTypeTo(PT); // Different type altogether...
1096 } else {
1097 setDerivedTypeProperties(); // Update the name and isAbstract
1098 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001099 }
Chris Lattner00950542001-06-06 20:29:01 +00001100}
1101