blob: 29b81ce10f80f5c7301c33cc4eee7aced7962381 [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 Lattnerdd4b4212003-09-02 16:35:17 +000028// Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
29// for types as they are needed. Because resolution of types must invalidate
30// all of the abstract type descriptions, we keep them in a seperate map to make
31// this easy.
32static std::map<const Type*, std::string> ConcreteTypeDescriptions;
33static std::map<const Type*, std::string> AbstractTypeDescriptions;
34
Chris Lattneraa06d2c2002-04-04 19:26:02 +000035void PATypeHolder::dump() const {
Chris Lattner47697a12003-05-22 21:21:43 +000036 std::cerr << "PATypeHolder(" << (void*)this << ")\n";
Chris Lattneraa06d2c2002-04-04 19:26:02 +000037}
38
Chris Lattner3855f2f2002-04-05 22:25:26 +000039
Chris Lattner47697a12003-05-22 21:21:43 +000040Type::Type(const std::string &name, PrimitiveID id)
Chris Lattnerc038a2f2001-09-07 16:56:42 +000041 : Value(Type::TypeTy, Value::TypeVal) {
Chris Lattnerdd4b4212003-09-02 16:35:17 +000042 ConcreteTypeDescriptions[this] = name;
Chris Lattner00950542001-06-06 20:29:01 +000043 ID = id;
Chris Lattner87ca5fa2003-09-02 21:41:05 +000044 Abstract = false;
Chris Lattner00950542001-06-06 20:29:01 +000045 UID = CurUID++; // Assign types UID's as they are created
46 UIDMappings.push_back(this);
47}
48
Chris Lattner47697a12003-05-22 21:21:43 +000049void Type::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +000050 assert(ST && "Type::setName - Must provide symbol table argument!");
51
52 if (Name.size()) ST->insert(Name, this);
53}
54
55
Chris Lattner00950542001-06-06 20:29:01 +000056const Type *Type::getUniqueIDType(unsigned UID) {
57 assert(UID < UIDMappings.size() &&
58 "Type::getPrimitiveType: UID out of range!");
59 return UIDMappings[UID];
60}
61
62const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
63 switch (IDNumber) {
64 case VoidTyID : return VoidTy;
65 case BoolTyID : return BoolTy;
66 case UByteTyID : return UByteTy;
67 case SByteTyID : return SByteTy;
68 case UShortTyID: return UShortTy;
69 case ShortTyID : return ShortTy;
70 case UIntTyID : return UIntTy;
71 case IntTyID : return IntTy;
72 case ULongTyID : return ULongTy;
73 case LongTyID : return LongTy;
74 case FloatTyID : return FloatTy;
75 case DoubleTyID: return DoubleTy;
76 case TypeTyID : return TypeTy;
77 case LabelTyID : return LabelTy;
Chris Lattner00950542001-06-06 20:29:01 +000078 default:
79 return 0;
80 }
81}
82
Misha Brukmanf117cc92003-05-20 18:45:36 +000083// isLosslesslyConvertibleTo - Return true if this type can be converted to
Chris Lattner58716b92001-11-26 17:01:47 +000084// 'Ty' without any reinterpretation of bits. For example, uint to int.
85//
Misha Brukmanf117cc92003-05-20 18:45:36 +000086bool Type::isLosslesslyConvertibleTo(const Type *Ty) const {
Chris Lattner58716b92001-11-26 17:01:47 +000087 if (this == Ty) return true;
Chris Lattnerd44023e2002-05-06 16:14:39 +000088 if ((!isPrimitiveType() && !isa<PointerType>(this)) ||
89 (!isa<PointerType>(Ty) && !Ty->isPrimitiveType())) return false;
Chris Lattner58716b92001-11-26 17:01:47 +000090
91 if (getPrimitiveID() == Ty->getPrimitiveID())
92 return true; // Handles identity cast, and cast of differing pointer types
93
94 // Now we know that they are two differing primitive or pointer types
95 switch (getPrimitiveID()) {
96 case Type::UByteTyID: return Ty == Type::SByteTy;
97 case Type::SByteTyID: return Ty == Type::UByteTy;
98 case Type::UShortTyID: return Ty == Type::ShortTy;
99 case Type::ShortTyID: return Ty == Type::UShortTy;
100 case Type::UIntTyID: return Ty == Type::IntTy;
101 case Type::IntTyID: return Ty == Type::UIntTy;
102 case Type::ULongTyID:
103 case Type::LongTyID:
104 case Type::PointerTyID:
Chris Lattnerc72114c2002-04-27 02:26:03 +0000105 return Ty == Type::ULongTy || Ty == Type::LongTy || isa<PointerType>(Ty);
Chris Lattner58716b92001-11-26 17:01:47 +0000106 default:
107 return false; // Other types have no identity values
108 }
109}
110
Chris Lattnerd44023e2002-05-06 16:14:39 +0000111// getPrimitiveSize - Return the basic size of this type if it is a primative
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000112// type. These are fixed by LLVM and are not target dependent. This will
Chris Lattnerd44023e2002-05-06 16:14:39 +0000113// return zero if the type does not have a size or is not a primitive type.
114//
115unsigned Type::getPrimitiveSize() const {
116 switch (getPrimitiveID()) {
117#define HANDLE_PRIM_TYPE(TY,SIZE) case TY##TyID: return SIZE;
118#include "llvm/Type.def"
119 default: return 0;
120 }
121}
122
Chris Lattner58716b92001-11-26 17:01:47 +0000123
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000124// getTypeDescription - This is a recursive function that walks a type hierarchy
125// calculating the description for a type.
126//
127static std::string getTypeDescription(const Type *Ty,
128 std::vector<const Type *> &TypeStack) {
129 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
130 std::map<const Type*, std::string>::iterator I =
131 AbstractTypeDescriptions.lower_bound(Ty);
132 if (I != AbstractTypeDescriptions.end() && I->first == Ty)
133 return I->second;
134 std::string Desc = "opaque"+utostr(Ty->getUniqueID());
135 AbstractTypeDescriptions.insert(std::make_pair(Ty, Desc));
136 return Desc;
137 }
138
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000139 if (!Ty->isAbstract()) { // Base case for the recursion
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000140 std::map<const Type*, std::string>::iterator I =
141 ConcreteTypeDescriptions.find(Ty);
142 if (I != ConcreteTypeDescriptions.end()) return I->second;
143 }
144
145 // Check to see if the Type is already on the stack...
146 unsigned Slot = 0, CurSize = TypeStack.size();
147 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
148
149 // This is another base case for the recursion. In this case, we know
150 // that we have looped back to a type that we have previously visited.
151 // Generate the appropriate upreference to handle this.
152 //
153 if (Slot < CurSize)
154 return "\\" + utostr(CurSize-Slot); // Here's the upreference
155
156 // Recursive case: derived types...
157 std::string Result;
158 TypeStack.push_back(Ty); // Add us to the stack..
159
160 switch (Ty->getPrimitiveID()) {
161 case Type::FunctionTyID: {
162 const FunctionType *FTy = cast<FunctionType>(Ty);
163 Result = getTypeDescription(FTy->getReturnType(), TypeStack) + " (";
164 for (FunctionType::ParamTypes::const_iterator
165 I = FTy->getParamTypes().begin(),
166 E = FTy->getParamTypes().end(); I != E; ++I) {
167 if (I != FTy->getParamTypes().begin())
168 Result += ", ";
169 Result += getTypeDescription(*I, TypeStack);
170 }
171 if (FTy->isVarArg()) {
172 if (!FTy->getParamTypes().empty()) Result += ", ";
173 Result += "...";
174 }
175 Result += ")";
176 break;
177 }
178 case Type::StructTyID: {
179 const StructType *STy = cast<StructType>(Ty);
180 Result = "{ ";
181 for (StructType::ElementTypes::const_iterator
182 I = STy->getElementTypes().begin(),
183 E = STy->getElementTypes().end(); I != E; ++I) {
184 if (I != STy->getElementTypes().begin())
185 Result += ", ";
186 Result += getTypeDescription(*I, TypeStack);
187 }
188 Result += " }";
189 break;
190 }
191 case Type::PointerTyID: {
192 const PointerType *PTy = cast<PointerType>(Ty);
193 Result = getTypeDescription(PTy->getElementType(), TypeStack) + " *";
194 break;
195 }
196 case Type::ArrayTyID: {
197 const ArrayType *ATy = cast<ArrayType>(Ty);
198 unsigned NumElements = ATy->getNumElements();
199 Result = "[";
200 Result += utostr(NumElements) + " x ";
201 Result += getTypeDescription(ATy->getElementType(), TypeStack) + "]";
202 break;
203 }
204 default:
205 assert(0 && "Unhandled type in getTypeDescription!");
206 Result = "<error>";
207 }
208
209 TypeStack.pop_back(); // Remove self from stack...
210
211 // In order to reduce the amount of repeated computation, we cache the computd
212 // value for later.
213 if (Ty->isAbstract())
214 AbstractTypeDescriptions[Ty] = Result;
215 else
216 ConcreteTypeDescriptions[Ty] = Result;
217
218 return Result;
219}
220
221
222
223static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
224 const Type *Ty) {
225 std::map<const Type*, std::string>::iterator I = Map.find(Ty);
226 if (I != Map.end()) return I->second;
227
228 std::vector<const Type *> TypeStack;
229 getTypeDescription(Ty, TypeStack);
230 assert(Map.count(Ty) && "Type didn't get inserted!!");
231 return Map[Ty];
232}
233
234
235const std::string &Type::getDescription() const {
236 if (isAbstract())
237 return getOrCreateDesc(AbstractTypeDescriptions, this);
238 else
239 return getOrCreateDesc(ConcreteTypeDescriptions, this);
240}
241
242
Chris Lattner58716b92001-11-26 17:01:47 +0000243bool StructType::indexValid(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000244 if (!isa<Constant>(V)) return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000245 if (V->getType() != Type::UByteTy) return false;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000246 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000247 return Idx < ETypes.size();
248}
249
250// getTypeAtIndex - Given an index value into the type, return the type of the
251// element. For a structure type, this must be a constant value...
252//
253const Type *StructType::getTypeAtIndex(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000254 assert(isa<Constant>(V) && "Structure index must be a constant!!");
Chris Lattner58716b92001-11-26 17:01:47 +0000255 assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000256 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000257 assert(Idx < ETypes.size() && "Structure index out of range!");
258 assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
259
260 return ETypes[Idx];
261}
262
263
Chris Lattner00950542001-06-06 20:29:01 +0000264//===----------------------------------------------------------------------===//
265// Auxilliary classes
266//===----------------------------------------------------------------------===//
267//
268// These classes are used to implement specialized behavior for each different
269// type.
270//
Chris Lattner0c4e8862002-09-03 01:08:28 +0000271struct SignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000272 SignedIntType(const std::string &Name, PrimitiveID id) : Type(Name, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000273
274 // isSigned - Return whether a numeric type is signed.
275 virtual bool isSigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000276
Chris Lattner0c4e8862002-09-03 01:08:28 +0000277 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000278 // virtual function invocation.
279 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000280 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000281};
282
Chris Lattner0c4e8862002-09-03 01:08:28 +0000283struct UnsignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000284 UnsignedIntType(const std::string &N, PrimitiveID id) : Type(N, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000285
286 // isUnsigned - Return whether a numeric type is signed.
287 virtual bool isUnsigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000288
Chris Lattner0c4e8862002-09-03 01:08:28 +0000289 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000290 // virtual function invocation.
291 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000292 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000293};
294
Chris Lattner950273b2003-05-22 21:31:52 +0000295struct OtherType : public Type {
296 OtherType(const std::string &N, PrimitiveID id) : Type(N, id) {}
297};
298
Chris Lattner00950542001-06-06 20:29:01 +0000299static struct TypeType : public Type {
300 TypeType() : Type("type", TypeTyID) {}
Chris Lattner950273b2003-05-22 21:31:52 +0000301} TheTypeTy; // Implement the type that is global.
Chris Lattner00950542001-06-06 20:29:01 +0000302
303
304//===----------------------------------------------------------------------===//
305// Static 'Type' data
306//===----------------------------------------------------------------------===//
307
Chris Lattner950273b2003-05-22 21:31:52 +0000308static OtherType TheVoidTy ("void" , Type::VoidTyID);
309static OtherType TheBoolTy ("bool" , Type::BoolTyID);
310static SignedIntType TheSByteTy ("sbyte" , Type::SByteTyID);
311static UnsignedIntType TheUByteTy ("ubyte" , Type::UByteTyID);
312static SignedIntType TheShortTy ("short" , Type::ShortTyID);
313static UnsignedIntType TheUShortTy("ushort", Type::UShortTyID);
314static SignedIntType TheIntTy ("int" , Type::IntTyID);
315static UnsignedIntType TheUIntTy ("uint" , Type::UIntTyID);
316static SignedIntType TheLongTy ("long" , Type::LongTyID);
317static UnsignedIntType TheULongTy ("ulong" , Type::ULongTyID);
318static OtherType TheFloatTy ("float" , Type::FloatTyID);
319static OtherType TheDoubleTy("double", Type::DoubleTyID);
320static OtherType TheLabelTy ("label" , Type::LabelTyID);
321
322Type *Type::VoidTy = &TheVoidTy;
323Type *Type::BoolTy = &TheBoolTy;
324Type *Type::SByteTy = &TheSByteTy;
325Type *Type::UByteTy = &TheUByteTy;
326Type *Type::ShortTy = &TheShortTy;
327Type *Type::UShortTy = &TheUShortTy;
328Type *Type::IntTy = &TheIntTy;
329Type *Type::UIntTy = &TheUIntTy;
330Type *Type::LongTy = &TheLongTy;
331Type *Type::ULongTy = &TheULongTy;
332Type *Type::FloatTy = &TheFloatTy;
333Type *Type::DoubleTy = &TheDoubleTy;
334Type *Type::TypeTy = &TheTypeTy;
335Type *Type::LabelTy = &TheLabelTy;
Chris Lattner00950542001-06-06 20:29:01 +0000336
337
338//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000339// Derived Type Constructors
340//===----------------------------------------------------------------------===//
341
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000342FunctionType::FunctionType(const Type *Result,
Chris Lattner47697a12003-05-22 21:21:43 +0000343 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000344 bool IsVarArgs) : DerivedType(FunctionTyID),
Chris Lattner893f0252003-06-18 19:22:36 +0000345 ResultType(PATypeHandle(Result, this)),
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000346 isVarArgs(IsVarArgs) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000347 ParamTys.reserve(Params.size());
Chris Lattner56c5acb2001-10-13 07:01:33 +0000348 for (unsigned i = 0; i < Params.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000349 ParamTys.push_back(PATypeHandle(Params[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000350
351 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000352}
353
Chris Lattner47697a12003-05-22 21:21:43 +0000354StructType::StructType(const std::vector<const Type*> &Types)
Chris Lattner6c42c312001-12-14 16:41:56 +0000355 : CompositeType(StructTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000356 ETypes.reserve(Types.size());
Chris Lattner56c5acb2001-10-13 07:01:33 +0000357 for (unsigned i = 0; i < Types.size(); ++i) {
358 assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");
Chris Lattner893f0252003-06-18 19:22:36 +0000359 ETypes.push_back(PATypeHandle(Types[i], this));
Chris Lattner56c5acb2001-10-13 07:01:33 +0000360 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000361 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000362}
363
Chris Lattner6c42c312001-12-14 16:41:56 +0000364ArrayType::ArrayType(const Type *ElType, unsigned NumEl)
365 : SequentialType(ArrayTyID, ElType) {
366 NumElements = NumEl;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000367 setDerivedTypeProperties();
Chris Lattner00950542001-06-06 20:29:01 +0000368}
369
Chris Lattner6c42c312001-12-14 16:41:56 +0000370PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
371 setDerivedTypeProperties();
372}
373
374OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000375 setAbstract(true);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000376#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000377 std::cerr << "Derived new type: " << getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000378#endif
379}
380
381
382
383
Chris Lattner00950542001-06-06 20:29:01 +0000384//===----------------------------------------------------------------------===//
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000385// Derived Type setDerivedTypeProperties Function
Chris Lattner00950542001-06-06 20:29:01 +0000386//===----------------------------------------------------------------------===//
387
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000388// getTypeProps - This is a recursive function that walks a type hierarchy
389// calculating the description for a type and whether or not it is abstract or
390// recursive. Worst case it will have to do a lot of traversing if you have
391// some whacko opaque types, but in most cases, it will do some simple stuff
392// when it hits non-abstract types that aren't recursive.
393//
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000394static void getTypeProps(const Type *Ty, std::vector<const Type *> &TypeStack,
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000395 bool &isAbstract) {
396 if (!Ty->isAbstract()) // Base case for the recursion
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000397 return; // Primitive = leaf type
398
399 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
400 isAbstract = true; // This whole type is abstract!
401 return; // Opaque = leaf type
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000402 }
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000403
404 // Check to see if the Type is already on the stack...
Chris Lattner7ba77f22003-09-02 16:46:41 +0000405 for (unsigned Slot = 0; Slot != TypeStack.size(); ++Slot)
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000406 if (TypeStack[Slot] == Ty) // Scan for type
407 return; // is a recursive check.
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000408
409 // Recursive case: derived type...
410 TypeStack.push_back(Ty); // Add us to the stack..
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000411
Chris Lattner7ba77f22003-09-02 16:46:41 +0000412 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattnerde731932003-09-02 20:06:29 +0000413 I != E; ++I) {
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000414 getTypeProps(*I, TypeStack, isAbstract);
415 if (isAbstract) break;
Chris Lattnerde731932003-09-02 20:06:29 +0000416 }
Chris Lattner7ba77f22003-09-02 16:46:41 +0000417
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000418 TypeStack.pop_back(); // Remove self from stack...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000419}
420
421
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000422// setDerivedTypeProperties - This function is used to calculate the isAbstract
423// setting for a type. The getTypeProps function does all the dirty work.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000424//
425void DerivedType::setDerivedTypeProperties() {
Chris Lattner47697a12003-05-22 21:21:43 +0000426 std::vector<const Type *> TypeStack;
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000427 bool isAbstract = false;
Chris Lattner11e40502003-09-02 19:14:12 +0000428
429 setAbstract(true);
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000430 getTypeProps(this, TypeStack, isAbstract);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000431 setAbstract(isAbstract);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000432}
433
434
435//===----------------------------------------------------------------------===//
436// Type Structural Equality Testing
437//===----------------------------------------------------------------------===//
438
439// TypesEqual - Two types are considered structurally equal if they have the
440// same "shape": Every level and element of the types have identical primitive
441// ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
442// be pointer equals to be equivalent though. This uses an optimistic algorithm
443// that assumes that two graphs are the same until proven otherwise.
444//
445static bool TypesEqual(const Type *Ty, const Type *Ty2,
Chris Lattner47697a12003-05-22 21:21:43 +0000446 std::map<const Type *, const Type *> &EqTypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000447 if (Ty == Ty2) return true;
448 if (Ty->getPrimitiveID() != Ty2->getPrimitiveID()) return false;
449 if (Ty->isPrimitiveType()) return true;
Chris Lattner008f9062001-10-24 05:12:04 +0000450 if (isa<OpaqueType>(Ty))
451 return false; // Two nonequal opaque types are never equal
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000452
Chris Lattner47697a12003-05-22 21:21:43 +0000453 std::map<const Type*, const Type*>::iterator It = EqTypes.find(Ty);
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000454 if (It != EqTypes.end())
455 return It->second == Ty2; // Looping back on a type, check for equality
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000456
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000457 // Otherwise, add the mapping to the table to make sure we don't get
458 // recursion on the types...
Chris Lattner47697a12003-05-22 21:21:43 +0000459 EqTypes.insert(std::make_pair(Ty, Ty2));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000460
461 // Iterate over the types and make sure the the contents are equivalent...
Chris Lattner74c2b762001-09-09 22:26:58 +0000462 Type::subtype_iterator I = Ty ->subtype_begin(), IE = Ty ->subtype_end();
463 Type::subtype_iterator I2 = Ty2->subtype_begin(), IE2 = Ty2->subtype_end();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000464 for (; I != IE && I2 != IE2; ++I, ++I2)
465 if (!TypesEqual(*I, *I2, EqTypes)) return false;
466
Chris Lattner56c5acb2001-10-13 07:01:33 +0000467 // Two really annoying special cases that breaks an otherwise nice simple
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000468 // algorithm is the fact that arraytypes have sizes that differentiates types,
Chris Lattner56c5acb2001-10-13 07:01:33 +0000469 // and that method types can be varargs or not. Consider this now.
470 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattner949a3622003-07-23 15:30:06 +0000471 if (ATy->getNumElements() != cast<ArrayType>(Ty2)->getNumElements())
Chris Lattner56c5acb2001-10-13 07:01:33 +0000472 return false;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000473 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
474 if (FTy->isVarArg() != cast<FunctionType>(Ty2)->isVarArg())
Chris Lattner56c5acb2001-10-13 07:01:33 +0000475 return false;
476 }
477
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000478 return I == IE && I2 == IE2; // Types equal if both iterators are done
479}
480
481static bool TypesEqual(const Type *Ty, const Type *Ty2) {
Chris Lattner47697a12003-05-22 21:21:43 +0000482 std::map<const Type *, const Type *> EqTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000483 return TypesEqual(Ty, Ty2, EqTypes);
484}
485
486
487
488//===----------------------------------------------------------------------===//
489// Derived Type Factory Functions
490//===----------------------------------------------------------------------===//
491
492// TypeMap - Make sure that only one instance of a particular type may be
493// created on any given run of the compiler... note that this involves updating
494// our map if an abstract type gets refined somehow...
495//
496template<class ValType, class TypeClass>
497class TypeMap : public AbstractTypeUser {
Chris Lattner893f0252003-06-18 19:22:36 +0000498 typedef std::map<ValType, PATypeHandle> MapTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000499 MapTy Map;
500public:
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000501 ~TypeMap() { print("ON EXIT"); }
502
503 inline TypeClass *get(const ValType &V) {
Chris Lattner893f0252003-06-18 19:22:36 +0000504 typename std::map<ValType, PATypeHandle>::iterator I
Chris Lattner47697a12003-05-22 21:21:43 +0000505 = Map.find(V);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000506 // TODO: FIXME: When Types are not CONST.
507 return (I != Map.end()) ? (TypeClass*)I->second.get() : 0;
508 }
509
510 inline void add(const ValType &V, TypeClass *T) {
Chris Lattner893f0252003-06-18 19:22:36 +0000511 Map.insert(std::make_pair(V, PATypeHandle(T, this)));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000512 print("add");
513 }
514
515 // containsEquivalent - Return true if the typemap contains a type that is
516 // structurally equivalent to the specified type.
517 //
518 inline const TypeClass *containsEquivalent(const TypeClass *Ty) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000519 for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000520 if (I->second.get() != Ty && TypesEqual(Ty, I->second.get()))
521 return (TypeClass*)I->second.get(); // FIXME TODO when types not const
522 return 0;
523 }
524
525 // refineAbstractType - This is called when one of the contained abstract
526 // types gets refined... this simply removes the abstract type from our table.
527 // We expect that whoever refined the type will add it back to the table,
528 // corrected.
529 //
530 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000531#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000532 std::cerr << "Removing Old type from Tab: " << (void*)OldTy << ", "
533 << OldTy->getDescription() << " replacement == " << (void*)NewTy
534 << ", " << NewTy->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000535#endif
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000536 for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000537 if (I->second == OldTy) {
Chris Lattner417081c2002-04-07 06:14:56 +0000538 // Check to see if the type just became concrete. If so, remove self
539 // from user list.
540 I->second.removeUserFromConcrete();
541 I->second = cast<TypeClass>(NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000542 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000543 }
544
545 void remove(const ValType &OldVal) {
Chris Lattnerfe8041a2002-07-24 22:08:53 +0000546 typename MapTy::iterator I = Map.find(OldVal);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000547 assert(I != Map.end() && "TypeMap::remove, element not found!");
548 Map.erase(I);
549 }
550
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000551 void print(const char *Arg) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000552#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000553 std::cerr << "TypeMap<>::" << Arg << " table contents:\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000554 unsigned i = 0;
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000555 for (MapTy::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattner47697a12003-05-22 21:21:43 +0000556 std::cerr << " " << (++i) << ". " << I->second << " "
557 << I->second->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000558#endif
559 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000560
561 void dump() const { print("dump output"); }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000562};
563
564
565// ValTypeBase - This is the base class that is used by the various
566// instantiations of TypeMap. This class is an AbstractType user that notifies
567// the underlying TypeMap when it gets modified.
568//
569template<class ValType, class TypeClass>
570class ValTypeBase : public AbstractTypeUser {
571 TypeMap<ValType, TypeClass> &MyTable;
572protected:
573 inline ValTypeBase(TypeMap<ValType, TypeClass> &tab) : MyTable(tab) {}
574
575 // Subclass should override this... to update self as usual
576 virtual void doRefinement(const DerivedType *OldTy, const Type *NewTy) = 0;
Chris Lattnere244a252001-11-03 03:27:53 +0000577
578 // typeBecameConcrete - This callback occurs when a contained type refines
579 // to itself, but becomes concrete in the process. Our subclass should remove
580 // itself from the ATU list of the specified type.
581 //
582 virtual void typeBecameConcrete(const DerivedType *Ty) = 0;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000583
584 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Chris Lattner3855f2f2002-04-05 22:25:26 +0000585 assert(OldTy == NewTy || OldTy->isAbstract());
Chris Lattner417081c2002-04-07 06:14:56 +0000586
587 if (!OldTy->isAbstract())
588 typeBecameConcrete(OldTy);
589
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000590 TypeMap<ValType, TypeClass> &Table = MyTable; // Copy MyTable reference
591 ValType Tmp(*(ValType*)this); // Copy this.
Chris Lattner893f0252003-06-18 19:22:36 +0000592 PATypeHandle OldType(Table.get(*(ValType*)this), this);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000593 Table.remove(*(ValType*)this); // Destroy's this!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000594
Chris Lattner417081c2002-04-07 06:14:56 +0000595 // Refine temporary to new state...
596 if (OldTy != NewTy)
597 Tmp.doRefinement(OldTy, NewTy);
598
599 // FIXME: when types are not const!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000600 Table.add((ValType&)Tmp, (TypeClass*)OldType.get());
601 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000602
603 void dump() const {
Chris Lattner47697a12003-05-22 21:21:43 +0000604 std::cerr << "ValTypeBase instance!\n";
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000605 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000606};
607
608
609
610//===----------------------------------------------------------------------===//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000611// Function Type Factory and Value Class...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000612//
613
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000614// FunctionValType - Define a class to hold the key that goes into the TypeMap
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000615//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000616class FunctionValType : public ValTypeBase<FunctionValType, FunctionType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000617 PATypeHandle RetTy;
618 std::vector<PATypeHandle> ArgTypes;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000619 bool isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000620public:
Chris Lattner47697a12003-05-22 21:21:43 +0000621 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000622 bool IVA, TypeMap<FunctionValType, FunctionType> &Tab)
623 : ValTypeBase<FunctionValType, FunctionType>(Tab), RetTy(ret, this),
Chris Lattner56c5acb2001-10-13 07:01:33 +0000624 isVarArg(IVA) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000625 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000626 ArgTypes.push_back(PATypeHandle(args[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000627 }
628
629 // We *MUST* have an explicit copy ctor so that the TypeHandles think that
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000630 // this FunctionValType owns them, not the old one!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000631 //
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000632 FunctionValType(const FunctionValType &MVT)
633 : ValTypeBase<FunctionValType, FunctionType>(MVT), RetTy(MVT.RetTy, this),
Chris Lattner56c5acb2001-10-13 07:01:33 +0000634 isVarArg(MVT.isVarArg) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000635 ArgTypes.reserve(MVT.ArgTypes.size());
636 for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000637 ArgTypes.push_back(PATypeHandle(MVT.ArgTypes[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000638 }
639
640 // Subclass should override this... to update self as usual
641 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
642 if (RetTy == OldType) RetTy = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +0000643 for (unsigned i = 0, e = ArgTypes.size(); i != e; ++i)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000644 if (ArgTypes[i] == OldType) ArgTypes[i] = NewType;
645 }
646
Chris Lattnere244a252001-11-03 03:27:53 +0000647 virtual void typeBecameConcrete(const DerivedType *Ty) {
648 if (RetTy == Ty) RetTy.removeUserFromConcrete();
649
650 for (unsigned i = 0; i < ArgTypes.size(); ++i)
651 if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete();
652 }
653
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000654 inline bool operator<(const FunctionValType &MTV) const {
Chris Lattner56c5acb2001-10-13 07:01:33 +0000655 if (RetTy.get() < MTV.RetTy.get()) return true;
656 if (RetTy.get() > MTV.RetTy.get()) return false;
657
658 if (ArgTypes < MTV.ArgTypes) return true;
659 return (ArgTypes == MTV.ArgTypes) && isVarArg < MTV.isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000660 }
661};
662
663// Define the actual map itself now...
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000664static TypeMap<FunctionValType, FunctionType> FunctionTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000665
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000666// FunctionType::get - The factory function for the FunctionType class...
667FunctionType *FunctionType::get(const Type *ReturnType,
Chris Lattner47697a12003-05-22 21:21:43 +0000668 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000669 bool isVarArg) {
670 FunctionValType VT(ReturnType, Params, isVarArg, FunctionTypes);
671 FunctionType *MT = FunctionTypes.get(VT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000672 if (MT) return MT;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000673
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000674 FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000675
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000676#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000677 std::cerr << "Derived new type: " << MT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000678#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000679 return MT;
Chris Lattner00950542001-06-06 20:29:01 +0000680}
681
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000682//===----------------------------------------------------------------------===//
683// Array Type Factory...
684//
685class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000686 PATypeHandle ValTy;
Chris Lattner6c42c312001-12-14 16:41:56 +0000687 unsigned Size;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000688public:
689 ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab)
690 : ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {}
Chris Lattner00950542001-06-06 20:29:01 +0000691
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000692 // We *MUST* have an explicit copy ctor so that the ValTy thinks that this
693 // ArrayValType owns it, not the old one!
694 //
695 ArrayValType(const ArrayValType &AVT)
696 : ValTypeBase<ArrayValType, ArrayType>(AVT), ValTy(AVT.ValTy, this),
697 Size(AVT.Size) {}
Chris Lattner00950542001-06-06 20:29:01 +0000698
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000699 // Subclass should override this... to update self as usual
700 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000701 assert(ValTy == OldType);
702 ValTy = NewType;
Chris Lattner00950542001-06-06 20:29:01 +0000703 }
704
Chris Lattnere244a252001-11-03 03:27:53 +0000705 virtual void typeBecameConcrete(const DerivedType *Ty) {
706 assert(ValTy == Ty &&
707 "Contained type became concrete but we're not using it!");
708 ValTy.removeUserFromConcrete();
709 }
710
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000711 inline bool operator<(const ArrayValType &MTV) const {
712 if (Size < MTV.Size) return true;
713 return Size == MTV.Size && ValTy.get() < MTV.ValTy.get();
714 }
715};
716
717static TypeMap<ArrayValType, ArrayType> ArrayTypes;
718
Chris Lattner6c42c312001-12-14 16:41:56 +0000719ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000720 assert(ElementType && "Can't get array of null types!");
721
722 ArrayValType AVT(ElementType, NumElements, ArrayTypes);
723 ArrayType *AT = ArrayTypes.get(AVT);
724 if (AT) return AT; // Found a match, return it!
725
Chris Lattner00950542001-06-06 20:29:01 +0000726 // Value not found. Derive a new type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000727 ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements));
Chris Lattner00950542001-06-06 20:29:01 +0000728
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000729#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000730 std::cerr << "Derived new type: " << AT->getDescription() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000731#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000732 return AT;
Chris Lattner00950542001-06-06 20:29:01 +0000733}
734
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000735//===----------------------------------------------------------------------===//
736// Struct Type Factory...
737//
Chris Lattner00950542001-06-06 20:29:01 +0000738
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000739// StructValType - Define a class to hold the key that goes into the TypeMap
740//
741class StructValType : public ValTypeBase<StructValType, StructType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000742 std::vector<PATypeHandle> ElTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000743public:
Chris Lattner47697a12003-05-22 21:21:43 +0000744 StructValType(const std::vector<const Type*> &args,
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000745 TypeMap<StructValType, StructType> &Tab)
746 : ValTypeBase<StructValType, StructType>(Tab) {
Chris Lattner3855f2f2002-04-05 22:25:26 +0000747 ElTypes.reserve(args.size());
748 for (unsigned i = 0, e = args.size(); i != e; ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000749 ElTypes.push_back(PATypeHandle(args[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000750 }
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000752 // We *MUST* have an explicit copy ctor so that the TypeHandles think that
753 // this StructValType owns them, not the old one!
754 //
755 StructValType(const StructValType &SVT)
756 : ValTypeBase<StructValType, StructType>(SVT){
757 ElTypes.reserve(SVT.ElTypes.size());
Chris Lattner3855f2f2002-04-05 22:25:26 +0000758 for (unsigned i = 0, e = SVT.ElTypes.size(); i != e; ++i)
Chris Lattner893f0252003-06-18 19:22:36 +0000759 ElTypes.push_back(PATypeHandle(SVT.ElTypes[i], this));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000760 }
761
762 // Subclass should override this... to update self as usual
763 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
764 for (unsigned i = 0; i < ElTypes.size(); ++i)
765 if (ElTypes[i] == OldType) ElTypes[i] = NewType;
766 }
767
Chris Lattnere244a252001-11-03 03:27:53 +0000768 virtual void typeBecameConcrete(const DerivedType *Ty) {
Chris Lattner417081c2002-04-07 06:14:56 +0000769 for (unsigned i = 0, e = ElTypes.size(); i != e; ++i)
770 if (ElTypes[i] == Ty)
771 ElTypes[i].removeUserFromConcrete();
Chris Lattnere244a252001-11-03 03:27:53 +0000772 }
773
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000774 inline bool operator<(const StructValType &STV) const {
775 return ElTypes < STV.ElTypes;
776 }
777};
778
779static TypeMap<StructValType, StructType> StructTypes;
780
Chris Lattner47697a12003-05-22 21:21:43 +0000781StructType *StructType::get(const std::vector<const Type*> &ETypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000782 StructValType STV(ETypes, StructTypes);
783 StructType *ST = StructTypes.get(STV);
784 if (ST) return ST;
785
786 // Value not found. Derive a new type!
787 StructTypes.add(STV, ST = new StructType(ETypes));
788
789#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000790 std::cerr << "Derived new type: " << ST->getDescription() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000791#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000792 return ST;
793}
794
795//===----------------------------------------------------------------------===//
796// Pointer Type Factory...
797//
798
799// PointerValType - Define a class to hold the key that goes into the TypeMap
800//
801class PointerValType : public ValTypeBase<PointerValType, PointerType> {
Chris Lattner893f0252003-06-18 19:22:36 +0000802 PATypeHandle ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000803public:
804 PointerValType(const Type *val, TypeMap<PointerValType, PointerType> &Tab)
805 : ValTypeBase<PointerValType, PointerType>(Tab), ValTy(val, this) {}
806
807 // We *MUST* have an explicit copy ctor so that the ValTy thinks that this
808 // PointerValType owns it, not the old one!
809 //
810 PointerValType(const PointerValType &PVT)
811 : ValTypeBase<PointerValType, PointerType>(PVT), ValTy(PVT.ValTy, this) {}
812
813 // Subclass should override this... to update self as usual
814 virtual void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000815 assert(ValTy == OldType);
816 ValTy = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000817 }
818
Chris Lattnere244a252001-11-03 03:27:53 +0000819 virtual void typeBecameConcrete(const DerivedType *Ty) {
820 assert(ValTy == Ty &&
821 "Contained type became concrete but we're not using it!");
822 ValTy.removeUserFromConcrete();
823 }
824
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000825 inline bool operator<(const PointerValType &MTV) const {
826 return ValTy.get() < MTV.ValTy.get();
827 }
828};
829
830static TypeMap<PointerValType, PointerType> PointerTypes;
831
832PointerType *PointerType::get(const Type *ValueType) {
833 assert(ValueType && "Can't get a pointer to <null> type!");
834 PointerValType PVT(ValueType, PointerTypes);
835
836 PointerType *PT = PointerTypes.get(PVT);
837 if (PT) return PT;
838
839 // Value not found. Derive a new type!
840 PointerTypes.add(PVT, PT = new PointerType(ValueType));
841
842#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000843 std::cerr << "Derived new type: " << PT->getDescription() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000844#endif
845 return PT;
846}
847
Chris Lattner339ba452002-04-06 00:21:11 +0000848void debug_type_tables() {
849 FunctionTypes.dump();
850 ArrayTypes.dump();
851 StructTypes.dump();
852 PointerTypes.dump();
853}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000854
855
856//===----------------------------------------------------------------------===//
857// Derived Type Refinement Functions
858//===----------------------------------------------------------------------===//
859
Chris Lattner3855f2f2002-04-05 22:25:26 +0000860// addAbstractTypeUser - Notify an abstract type that there is a new user of
861// it. This function is called primarily by the PATypeHandle class.
862//
863void DerivedType::addAbstractTypeUser(AbstractTypeUser *U) const {
864 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
Chris Lattner3855f2f2002-04-05 22:25:26 +0000865
866#if DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000867 std::cerr << " addAbstractTypeUser[" << (void*)this << ", "
868 << getDescription() << "][" << AbstractTypeUsers.size()
869 << "] User = " << U << "\n";
Chris Lattner3855f2f2002-04-05 22:25:26 +0000870#endif
871 AbstractTypeUsers.push_back(U);
872}
873
874
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000875// removeAbstractTypeUser - Notify an abstract type that a user of the class
876// no longer has a handle to the type. This function is called primarily by
877// the PATypeHandle class. When there are no users of the abstract type, it
878// is anihilated, because there is no way to get a reference to it ever again.
879//
880void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const {
881 // Search from back to front because we will notify users from back to
882 // front. Also, it is likely that there will be a stack like behavior to
883 // users that register and unregister users.
884 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000885 unsigned i;
886 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
887 assert(i != 0 && "AbstractTypeUser not in user list!");
888
889 --i; // Convert to be in range 0 <= i < size()
890 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
891
892 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000893
894#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000895 std::cerr << " remAbstractTypeUser[" << (void*)this << ", "
896 << getDescription() << "][" << i << "] User = " << U << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000897#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000898
899 if (AbstractTypeUsers.empty() && isAbstract()) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000900#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000901 std::cerr << "DELETEing unused abstract type: <" << getDescription()
902 << ">[" << (void*)this << "]" << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000903#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000904 delete this; // No users of this abstract type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000905 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000906}
907
908
909// refineAbstractTypeTo - This function is used to when it is discovered that
910// the 'this' abstract type is actually equivalent to the NewType specified.
911// This causes all users of 'this' to switch to reference the more concrete
912// type NewType and for 'this' to be deleted.
913//
914void DerivedType::refineAbstractTypeTo(const Type *NewType) {
915 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
916 assert(this != NewType && "Can't refine to myself!");
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000917
918 // The descriptions may be out of date. Conservatively clear them all!
919 AbstractTypeDescriptions.clear();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000920
921#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000922 std::cerr << "REFINING abstract type [" << (void*)this << " "
923 << getDescription() << "] to [" << (void*)NewType << " "
924 << NewType->getDescription() << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000925#endif
926
927
928 // Make sure to put the type to be refined to into a holder so that if IT gets
929 // refined, that we will not continue using a dead reference...
930 //
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000931 PATypeHolder NewTy(NewType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000932
933 // Add a self use of the current type so that we don't delete ourself until
934 // after this while loop. We are careful to never invoke refine on ourself,
935 // so this extra reference shouldn't be a problem. Note that we must only
936 // remove a single reference at the end, but we must tolerate multiple self
937 // references because we could be refineAbstractTypeTo'ing recursively on the
938 // same type.
939 //
940 addAbstractTypeUser(this);
941
942 // Count the number of self uses. Stop looping when sizeof(list) == NSU.
943 unsigned NumSelfUses = 0;
944
945 // Iterate over all of the uses of this type, invoking callback. Each user
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000946 // should remove itself from our use list automatically. We have to check to
947 // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types
948 // will not cause users to drop off of the use list. If we resolve to ourself
949 // we succeed!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000950 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000951 while (AbstractTypeUsers.size() > NumSelfUses && NewTy != this) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000952 AbstractTypeUser *User = AbstractTypeUsers.back();
953
954 if (User == this) {
955 // Move self use to the start of the list. Increment NSU.
Chris Lattner47697a12003-05-22 21:21:43 +0000956 std::swap(AbstractTypeUsers.back(), AbstractTypeUsers[NumSelfUses++]);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000957 } else {
958 unsigned OldSize = AbstractTypeUsers.size();
959#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000960 std::cerr << " REFINING user " << OldSize-1 << "[" << (void*)User
961 << "] of abstract type [" << (void*)this << " "
962 << getDescription() << "] to [" << (void*)NewTy.get() << " "
963 << NewTy->getDescription() << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000964#endif
Chris Lattner008f9062001-10-24 05:12:04 +0000965 User->refineAbstractType(this, NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000966
Chris Lattnercecb5202002-04-05 19:53:06 +0000967#ifdef DEBUG_MERGE_TYPES
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000968 if (AbstractTypeUsers.size() == OldSize) {
Chris Lattnercecb5202002-04-05 19:53:06 +0000969 User->refineAbstractType(this, NewTy);
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000970 if (AbstractTypeUsers.back() != User)
Chris Lattner47697a12003-05-22 21:21:43 +0000971 std::cerr << "User changed!\n";
972 std::cerr << "Top of user list is:\n";
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000973 AbstractTypeUsers.back()->dump();
974
Chris Lattner47697a12003-05-22 21:21:43 +0000975 std::cerr <<"\nOld User=\n";
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000976 User->dump();
977 }
Chris Lattnercecb5202002-04-05 19:53:06 +0000978#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000979 assert(AbstractTypeUsers.size() != OldSize &&
980 "AbsTyUser did not remove self from user list!");
Chris Lattner00950542001-06-06 20:29:01 +0000981 }
982 }
983
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000984 // Remove a single self use, even though there may be several here. This will
985 // probably 'delete this', so no instance variables may be used after this
986 // occurs...
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000987 //
988 assert((NewTy == this || AbstractTypeUsers.back() == this) &&
989 "Only self uses should be left!");
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000990 removeAbstractTypeUser(this);
Chris Lattner00950542001-06-06 20:29:01 +0000991}
992
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000993// typeIsRefined - Notify AbstractTypeUsers of this type that the current type
994// has been refined a bit. The pointer is still valid and still should be
995// used, but the subtypes have changed.
996//
997void DerivedType::typeIsRefined() {
998 assert(isRefining >= 0 && isRefining <= 2 && "isRefining out of bounds!");
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000999 if (isRefining == 1) return; // Kill recursion here...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001000 ++isRefining;
Chris Lattner00950542001-06-06 20:29:01 +00001001
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001002#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001003 std::cerr << "typeIsREFINED type: " << (void*)this <<" "<<getDescription()
1004 << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001005#endif
Chris Lattner417081c2002-04-07 06:14:56 +00001006
1007 // In this loop we have to be very careful not to get into infinite loops and
1008 // other problem cases. Specifically, we loop through all of the abstract
1009 // type users in the user list, notifying them that the type has been refined.
1010 // At their choice, they may or may not choose to remove themselves from the
1011 // list of users. Regardless of whether they do or not, we have to be sure
1012 // that we only notify each user exactly once. Because the refineAbstractType
1013 // method can cause an arbitrary permutation to the user list, we cannot loop
1014 // through it in any particular order and be guaranteed that we will be
1015 // successful at this aim. Because of this, we keep track of all the users we
1016 // have visited and only visit users we have not seen. Because this user list
1017 // should be small, we use a vector instead of a full featured set to keep
1018 // track of what users we have notified so far.
1019 //
Chris Lattner47697a12003-05-22 21:21:43 +00001020 std::vector<AbstractTypeUser*> Refined;
Chris Lattner417081c2002-04-07 06:14:56 +00001021 while (1) {
1022 unsigned i;
1023 for (i = AbstractTypeUsers.size(); i != 0; --i)
1024 if (find(Refined.begin(), Refined.end(), AbstractTypeUsers[i-1]) ==
1025 Refined.end())
1026 break; // Found an unrefined user?
1027
1028 if (i == 0) break; // Noone to refine left, break out of here!
1029
1030 AbstractTypeUser *ATU = AbstractTypeUsers[--i];
1031 Refined.push_back(ATU); // Keep track of which users we have refined!
1032
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001033#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001034 std::cerr << " typeIsREFINED user " << i << "[" << ATU
1035 << "] of abstract type [" << (void*)this << " "
1036 << getDescription() << "]\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001037#endif
1038 ATU->refineAbstractType(this, this);
Chris Lattner00950542001-06-06 20:29:01 +00001039 }
1040
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001041 --isRefining;
Chris Lattnere244a252001-11-03 03:27:53 +00001042
1043#ifndef _NDEBUG
1044 if (!(isAbstract() || AbstractTypeUsers.empty()))
1045 for (unsigned i = 0; i < AbstractTypeUsers.size(); ++i) {
1046 if (AbstractTypeUsers[i] != this) {
1047 // Debugging hook
Chris Lattner47697a12003-05-22 21:21:43 +00001048 std::cerr << "FOUND FAILURE\nUser: ";
Chris Lattnercecb5202002-04-05 19:53:06 +00001049 AbstractTypeUsers[i]->dump();
Chris Lattner47697a12003-05-22 21:21:43 +00001050 std::cerr << "\nCatch:\n";
Chris Lattnere244a252001-11-03 03:27:53 +00001051 AbstractTypeUsers[i]->refineAbstractType(this, this);
1052 assert(0 && "Type became concrete,"
1053 " but it still has abstract type users hanging around!");
1054 }
1055 }
1056#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001057}
1058
Chris Lattner00950542001-06-06 20:29:01 +00001059
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001060
1061
1062// refineAbstractType - Called when a contained type is found to be more
1063// concrete - this could potentially change us from an abstract type to a
1064// concrete type.
1065//
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001066void FunctionType::refineAbstractType(const DerivedType *OldType,
1067 const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001068#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001069 std::cerr << "FunctionTy::refineAbstractTy(" << (void*)OldType << "["
1070 << OldType->getDescription() << "], " << (void*)NewType << " ["
1071 << NewType->getDescription() << "])\n";
Chris Lattner00950542001-06-06 20:29:01 +00001072#endif
Chris Lattner3855f2f2002-04-05 22:25:26 +00001073 // Find the type element we are refining...
Chris Lattner417081c2002-04-07 06:14:56 +00001074 if (ResultType == OldType) {
1075 ResultType.removeUserFromConcrete();
1076 ResultType = NewType;
Chris Lattnere244a252001-11-03 03:27:53 +00001077 }
Chris Lattner417081c2002-04-07 06:14:56 +00001078 for (unsigned i = 0, e = ParamTys.size(); i != e; ++i)
1079 if (ParamTys[i] == OldType) {
1080 ParamTys[i].removeUserFromConcrete();
1081 ParamTys[i] = NewType;
1082 }
Chris Lattnere244a252001-11-03 03:27:53 +00001083
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001084 const FunctionType *MT = FunctionTypes.containsEquivalent(this);
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001085 if (MT && MT != this) {
Chris Lattnere244a252001-11-03 03:27:53 +00001086 refineAbstractTypeTo(MT); // Different type altogether...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001087 } else {
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001088 setDerivedTypeProperties(); // Update the name and isAbstract
Chris Lattnere244a252001-11-03 03:27:53 +00001089 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001090 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001091}
1092
1093
1094// refineAbstractType - Called when a contained type is found to be more
1095// concrete - this could potentially change us from an abstract type to a
1096// concrete type.
1097//
1098void ArrayType::refineAbstractType(const DerivedType *OldType,
1099 const Type *NewType) {
1100#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001101 std::cerr << "ArrayTy::refineAbstractTy(" << (void*)OldType << "["
1102 << OldType->getDescription() << "], " << (void*)NewType << " ["
1103 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001104#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001105
Chris Lattner417081c2002-04-07 06:14:56 +00001106 assert(getElementType() == OldType);
1107 ElementType.removeUserFromConcrete();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001108 ElementType = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +00001109
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001110 const ArrayType *AT = ArrayTypes.containsEquivalent(this);
1111 if (AT && AT != this) {
1112 refineAbstractTypeTo(AT); // Different type altogether...
1113 } else {
1114 setDerivedTypeProperties(); // Update the name and isAbstract
1115 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001116 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001117}
1118
1119
1120// refineAbstractType - Called when a contained type is found to be more
1121// concrete - this could potentially change us from an abstract type to a
1122// concrete type.
1123//
1124void StructType::refineAbstractType(const DerivedType *OldType,
1125 const Type *NewType) {
1126#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001127 std::cerr << "StructTy::refineAbstractTy(" << (void*)OldType << "["
1128 << OldType->getDescription() << "], " << (void*)NewType << " ["
1129 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001130#endif
Chris Lattner6a490ce2003-01-14 19:42:39 +00001131 for (int i = ETypes.size()-1; i >= 0; --i)
Chris Lattner417081c2002-04-07 06:14:56 +00001132 if (ETypes[i] == OldType) {
1133 ETypes[i].removeUserFromConcrete();
Chris Lattnere244a252001-11-03 03:27:53 +00001134
Chris Lattner417081c2002-04-07 06:14:56 +00001135 // Update old type to new type in the array...
1136 ETypes[i] = NewType;
1137 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001138
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001139 const StructType *ST = StructTypes.containsEquivalent(this);
1140 if (ST && ST != this) {
1141 refineAbstractTypeTo(ST); // Different type altogether...
1142 } else {
1143 setDerivedTypeProperties(); // Update the name and isAbstract
1144 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001145 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001146}
1147
1148// refineAbstractType - Called when a contained type is found to be more
1149// concrete - this could potentially change us from an abstract type to a
1150// concrete type.
1151//
1152void PointerType::refineAbstractType(const DerivedType *OldType,
1153 const Type *NewType) {
1154#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001155 std::cerr << "PointerTy::refineAbstractTy(" << (void*)OldType << "["
1156 << OldType->getDescription() << "], " << (void*)NewType << " ["
1157 << NewType->getDescription() << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001158#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001159
Chris Lattner417081c2002-04-07 06:14:56 +00001160 assert(ElementType == OldType);
1161 ElementType.removeUserFromConcrete();
Chris Lattner6c42c312001-12-14 16:41:56 +00001162 ElementType = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001163
Chris Lattner417081c2002-04-07 06:14:56 +00001164 const PointerType *PT = PointerTypes.containsEquivalent(this);
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001165 if (PT && PT != this) {
1166 refineAbstractTypeTo(PT); // Different type altogether...
1167 } else {
1168 setDerivedTypeProperties(); // Update the name and isAbstract
1169 typeIsRefined(); // Same type, different contents...
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001170 }
Chris Lattner00950542001-06-06 20:29:01 +00001171}
1172