blob: b92c5dcaf5457e513b1f3d8bad4ded4b4a4b65b6 [file] [log] [blame]
Chris Lattner169726b2003-09-05 02:21:39 +00001//===-- Type.cpp - Implement the Type class -------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Type class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/DerivedTypes.h"
Chris Lattnerc038a2f2001-09-07 16:56:42 +000015#include "llvm/SymbolTable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000017#include "Support/StringExtras.h"
18#include "Support/STLExtras.h"
Chris Lattner417081c2002-04-07 06:14:56 +000019#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000020
Chris Lattnerc038a2f2001-09-07 16:56:42 +000021// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
22// created and later destroyed, all in an effort to make sure that there is only
Chris Lattner065a6162003-09-10 05:29:43 +000023// a single canonical version of a type.
Chris Lattnerc038a2f2001-09-07 16:56:42 +000024//
25//#define DEBUG_MERGE_TYPES 1
26
27
Chris Lattner00950542001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
29// Type Class Implementation
30//===----------------------------------------------------------------------===//
31
32static unsigned CurUID = 0;
Chris Lattner47697a12003-05-22 21:21:43 +000033static std::vector<const Type *> UIDMappings;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattnerdd4b4212003-09-02 16:35:17 +000035// Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
36// for types as they are needed. Because resolution of types must invalidate
37// all of the abstract type descriptions, we keep them in a seperate map to make
38// this easy.
39static std::map<const Type*, std::string> ConcreteTypeDescriptions;
40static std::map<const Type*, std::string> AbstractTypeDescriptions;
41
Chris Lattner47697a12003-05-22 21:21:43 +000042Type::Type(const std::string &name, PrimitiveID id)
Chris Lattner1c5164e2003-10-02 23:35:57 +000043 : Value(Type::TypeTy, Value::TypeVal), ForwardType(0) {
Chris Lattnerd8d6c762003-09-02 22:50:02 +000044 if (!name.empty())
45 ConcreteTypeDescriptions[this] = name;
Chris Lattner00950542001-06-06 20:29:01 +000046 ID = id;
Chris Lattner87ca5fa2003-09-02 21:41:05 +000047 Abstract = false;
Chris Lattner00950542001-06-06 20:29:01 +000048 UID = CurUID++; // Assign types UID's as they are created
49 UIDMappings.push_back(this);
50}
51
Chris Lattner47697a12003-05-22 21:21:43 +000052void Type::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +000053 assert(ST && "Type::setName - Must provide symbol table argument!");
54
55 if (Name.size()) ST->insert(Name, this);
56}
57
58
Chris Lattner00950542001-06-06 20:29:01 +000059const Type *Type::getUniqueIDType(unsigned UID) {
60 assert(UID < UIDMappings.size() &&
61 "Type::getPrimitiveType: UID out of range!");
62 return UIDMappings[UID];
63}
64
65const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
66 switch (IDNumber) {
67 case VoidTyID : return VoidTy;
68 case BoolTyID : return BoolTy;
69 case UByteTyID : return UByteTy;
70 case SByteTyID : return SByteTy;
71 case UShortTyID: return UShortTy;
72 case ShortTyID : return ShortTy;
73 case UIntTyID : return UIntTy;
74 case IntTyID : return IntTy;
75 case ULongTyID : return ULongTy;
76 case LongTyID : return LongTy;
77 case FloatTyID : return FloatTy;
78 case DoubleTyID: return DoubleTy;
79 case TypeTyID : return TypeTy;
80 case LabelTyID : return LabelTy;
Chris Lattner00950542001-06-06 20:29:01 +000081 default:
82 return 0;
83 }
84}
85
Misha Brukmanf117cc92003-05-20 18:45:36 +000086// isLosslesslyConvertibleTo - Return true if this type can be converted to
Chris Lattner58716b92001-11-26 17:01:47 +000087// 'Ty' without any reinterpretation of bits. For example, uint to int.
88//
Misha Brukmanf117cc92003-05-20 18:45:36 +000089bool Type::isLosslesslyConvertibleTo(const Type *Ty) const {
Chris Lattner58716b92001-11-26 17:01:47 +000090 if (this == Ty) return true;
Chris Lattnerd44023e2002-05-06 16:14:39 +000091 if ((!isPrimitiveType() && !isa<PointerType>(this)) ||
92 (!isa<PointerType>(Ty) && !Ty->isPrimitiveType())) return false;
Chris Lattner58716b92001-11-26 17:01:47 +000093
94 if (getPrimitiveID() == Ty->getPrimitiveID())
95 return true; // Handles identity cast, and cast of differing pointer types
96
97 // Now we know that they are two differing primitive or pointer types
98 switch (getPrimitiveID()) {
99 case Type::UByteTyID: return Ty == Type::SByteTy;
100 case Type::SByteTyID: return Ty == Type::UByteTy;
101 case Type::UShortTyID: return Ty == Type::ShortTy;
102 case Type::ShortTyID: return Ty == Type::UShortTy;
103 case Type::UIntTyID: return Ty == Type::IntTy;
104 case Type::IntTyID: return Ty == Type::UIntTy;
105 case Type::ULongTyID:
106 case Type::LongTyID:
107 case Type::PointerTyID:
Chris Lattnerc72114c2002-04-27 02:26:03 +0000108 return Ty == Type::ULongTy || Ty == Type::LongTy || isa<PointerType>(Ty);
Chris Lattner58716b92001-11-26 17:01:47 +0000109 default:
110 return false; // Other types have no identity values
111 }
112}
113
Misha Brukman6b634522003-10-10 17:54:14 +0000114// getPrimitiveSize - Return the basic size of this type if it is a primitive
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000115// type. These are fixed by LLVM and are not target dependent. This will
Chris Lattnerd44023e2002-05-06 16:14:39 +0000116// return zero if the type does not have a size or is not a primitive type.
117//
118unsigned Type::getPrimitiveSize() const {
119 switch (getPrimitiveID()) {
120#define HANDLE_PRIM_TYPE(TY,SIZE) case TY##TyID: return SIZE;
121#include "llvm/Type.def"
122 default: return 0;
123 }
124}
125
Chris Lattner58716b92001-11-26 17:01:47 +0000126
Chris Lattner1c5164e2003-10-02 23:35:57 +0000127/// getForwardedTypeInternal - This method is used to implement the union-find
128/// algorithm for when a type is being forwarded to another type.
129const Type *Type::getForwardedTypeInternal() const {
130 assert(ForwardType && "This type is not being forwarded to another type!");
131
132 // Check to see if the forwarded type has been forwarded on. If so, collapse
133 // the forwarding links.
134 const Type *RealForwardedType = ForwardType->getForwardedType();
135 if (!RealForwardedType)
136 return ForwardType; // No it's not forwarded again
137
138 // Yes, it is forwarded again. First thing, add the reference to the new
139 // forward type.
140 if (RealForwardedType->isAbstract())
141 cast<DerivedType>(RealForwardedType)->addRef();
142
143 // Now drop the old reference. This could cause ForwardType to get deleted.
144 cast<DerivedType>(ForwardType)->dropRef();
145
146 // Return the updated type.
147 ForwardType = RealForwardedType;
148 return ForwardType;
149}
150
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000151// getTypeDescription - This is a recursive function that walks a type hierarchy
152// calculating the description for a type.
153//
154static std::string getTypeDescription(const Type *Ty,
155 std::vector<const Type *> &TypeStack) {
156 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
157 std::map<const Type*, std::string>::iterator I =
158 AbstractTypeDescriptions.lower_bound(Ty);
159 if (I != AbstractTypeDescriptions.end() && I->first == Ty)
160 return I->second;
161 std::string Desc = "opaque"+utostr(Ty->getUniqueID());
162 AbstractTypeDescriptions.insert(std::make_pair(Ty, Desc));
163 return Desc;
164 }
165
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000166 if (!Ty->isAbstract()) { // Base case for the recursion
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000167 std::map<const Type*, std::string>::iterator I =
168 ConcreteTypeDescriptions.find(Ty);
169 if (I != ConcreteTypeDescriptions.end()) return I->second;
170 }
171
172 // Check to see if the Type is already on the stack...
173 unsigned Slot = 0, CurSize = TypeStack.size();
174 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
175
176 // This is another base case for the recursion. In this case, we know
177 // that we have looped back to a type that we have previously visited.
178 // Generate the appropriate upreference to handle this.
179 //
180 if (Slot < CurSize)
181 return "\\" + utostr(CurSize-Slot); // Here's the upreference
182
183 // Recursive case: derived types...
184 std::string Result;
185 TypeStack.push_back(Ty); // Add us to the stack..
186
187 switch (Ty->getPrimitiveID()) {
188 case Type::FunctionTyID: {
189 const FunctionType *FTy = cast<FunctionType>(Ty);
190 Result = getTypeDescription(FTy->getReturnType(), TypeStack) + " (";
191 for (FunctionType::ParamTypes::const_iterator
192 I = FTy->getParamTypes().begin(),
193 E = FTy->getParamTypes().end(); I != E; ++I) {
194 if (I != FTy->getParamTypes().begin())
195 Result += ", ";
196 Result += getTypeDescription(*I, TypeStack);
197 }
198 if (FTy->isVarArg()) {
199 if (!FTy->getParamTypes().empty()) Result += ", ";
200 Result += "...";
201 }
202 Result += ")";
203 break;
204 }
205 case Type::StructTyID: {
206 const StructType *STy = cast<StructType>(Ty);
207 Result = "{ ";
208 for (StructType::ElementTypes::const_iterator
209 I = STy->getElementTypes().begin(),
210 E = STy->getElementTypes().end(); I != E; ++I) {
211 if (I != STy->getElementTypes().begin())
212 Result += ", ";
213 Result += getTypeDescription(*I, TypeStack);
214 }
215 Result += " }";
216 break;
217 }
218 case Type::PointerTyID: {
219 const PointerType *PTy = cast<PointerType>(Ty);
220 Result = getTypeDescription(PTy->getElementType(), TypeStack) + " *";
221 break;
222 }
223 case Type::ArrayTyID: {
224 const ArrayType *ATy = cast<ArrayType>(Ty);
225 unsigned NumElements = ATy->getNumElements();
226 Result = "[";
227 Result += utostr(NumElements) + " x ";
228 Result += getTypeDescription(ATy->getElementType(), TypeStack) + "]";
229 break;
230 }
231 default:
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000232 Result = "<error>";
Chris Lattnerd8d6c762003-09-02 22:50:02 +0000233 assert(0 && "Unhandled type in getTypeDescription!");
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000234 }
235
236 TypeStack.pop_back(); // Remove self from stack...
237
Chris Lattnera3057e82003-09-04 23:41:03 +0000238 return Result;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000239}
240
241
242
243static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
244 const Type *Ty) {
245 std::map<const Type*, std::string>::iterator I = Map.find(Ty);
246 if (I != Map.end()) return I->second;
247
248 std::vector<const Type *> TypeStack;
Chris Lattnera3057e82003-09-04 23:41:03 +0000249 return Map[Ty] = getTypeDescription(Ty, TypeStack);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000250}
251
252
253const std::string &Type::getDescription() const {
254 if (isAbstract())
255 return getOrCreateDesc(AbstractTypeDescriptions, this);
256 else
257 return getOrCreateDesc(ConcreteTypeDescriptions, this);
258}
259
260
Chris Lattner58716b92001-11-26 17:01:47 +0000261bool StructType::indexValid(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000262 if (!isa<Constant>(V)) return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000263 if (V->getType() != Type::UByteTy) return false;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000264 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000265 return Idx < ETypes.size();
266}
267
268// getTypeAtIndex - Given an index value into the type, return the type of the
269// element. For a structure type, this must be a constant value...
270//
271const Type *StructType::getTypeAtIndex(const Value *V) const {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000272 assert(isa<Constant>(V) && "Structure index must be a constant!!");
Chris Lattner58716b92001-11-26 17:01:47 +0000273 assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000274 unsigned Idx = cast<ConstantUInt>(V)->getValue();
Chris Lattner58716b92001-11-26 17:01:47 +0000275 assert(Idx < ETypes.size() && "Structure index out of range!");
276 assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
277
278 return ETypes[Idx];
279}
280
281
Chris Lattner00950542001-06-06 20:29:01 +0000282//===----------------------------------------------------------------------===//
Misha Brukman6b634522003-10-10 17:54:14 +0000283// Auxiliary classes
Chris Lattner00950542001-06-06 20:29:01 +0000284//===----------------------------------------------------------------------===//
285//
286// These classes are used to implement specialized behavior for each different
287// type.
288//
Chris Lattner0c4e8862002-09-03 01:08:28 +0000289struct SignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000290 SignedIntType(const std::string &Name, PrimitiveID id) : Type(Name, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 // isSigned - Return whether a numeric type is signed.
293 virtual bool isSigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000294
Chris Lattner0c4e8862002-09-03 01:08:28 +0000295 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000296 // virtual function invocation.
297 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000298 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000299};
300
Chris Lattner0c4e8862002-09-03 01:08:28 +0000301struct UnsignedIntType : public Type {
Chris Lattner47697a12003-05-22 21:21:43 +0000302 UnsignedIntType(const std::string &N, PrimitiveID id) : Type(N, id) {}
Chris Lattner00950542001-06-06 20:29:01 +0000303
304 // isUnsigned - Return whether a numeric type is signed.
305 virtual bool isUnsigned() const { return 1; }
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000306
Chris Lattner0c4e8862002-09-03 01:08:28 +0000307 // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single
Chris Lattner1a2cefc2001-07-21 19:15:26 +0000308 // virtual function invocation.
309 //
Chris Lattner0c4e8862002-09-03 01:08:28 +0000310 virtual bool isInteger() const { return 1; }
Chris Lattner00950542001-06-06 20:29:01 +0000311};
312
Chris Lattner950273b2003-05-22 21:31:52 +0000313struct OtherType : public Type {
314 OtherType(const std::string &N, PrimitiveID id) : Type(N, id) {}
315};
316
Chris Lattner00950542001-06-06 20:29:01 +0000317static struct TypeType : public Type {
318 TypeType() : Type("type", TypeTyID) {}
Chris Lattner950273b2003-05-22 21:31:52 +0000319} TheTypeTy; // Implement the type that is global.
Chris Lattner00950542001-06-06 20:29:01 +0000320
321
322//===----------------------------------------------------------------------===//
323// Static 'Type' data
324//===----------------------------------------------------------------------===//
325
Chris Lattner950273b2003-05-22 21:31:52 +0000326static OtherType TheVoidTy ("void" , Type::VoidTyID);
327static OtherType TheBoolTy ("bool" , Type::BoolTyID);
328static SignedIntType TheSByteTy ("sbyte" , Type::SByteTyID);
329static UnsignedIntType TheUByteTy ("ubyte" , Type::UByteTyID);
330static SignedIntType TheShortTy ("short" , Type::ShortTyID);
331static UnsignedIntType TheUShortTy("ushort", Type::UShortTyID);
332static SignedIntType TheIntTy ("int" , Type::IntTyID);
333static UnsignedIntType TheUIntTy ("uint" , Type::UIntTyID);
334static SignedIntType TheLongTy ("long" , Type::LongTyID);
335static UnsignedIntType TheULongTy ("ulong" , Type::ULongTyID);
336static OtherType TheFloatTy ("float" , Type::FloatTyID);
337static OtherType TheDoubleTy("double", Type::DoubleTyID);
338static OtherType TheLabelTy ("label" , Type::LabelTyID);
339
340Type *Type::VoidTy = &TheVoidTy;
341Type *Type::BoolTy = &TheBoolTy;
342Type *Type::SByteTy = &TheSByteTy;
343Type *Type::UByteTy = &TheUByteTy;
344Type *Type::ShortTy = &TheShortTy;
345Type *Type::UShortTy = &TheUShortTy;
346Type *Type::IntTy = &TheIntTy;
347Type *Type::UIntTy = &TheUIntTy;
348Type *Type::LongTy = &TheLongTy;
349Type *Type::ULongTy = &TheULongTy;
350Type *Type::FloatTy = &TheFloatTy;
351Type *Type::DoubleTy = &TheDoubleTy;
352Type *Type::TypeTy = &TheTypeTy;
353Type *Type::LabelTy = &TheLabelTy;
Chris Lattner00950542001-06-06 20:29:01 +0000354
355
356//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000357// Derived Type Constructors
358//===----------------------------------------------------------------------===//
359
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000360FunctionType::FunctionType(const Type *Result,
Chris Lattner47697a12003-05-22 21:21:43 +0000361 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000362 bool IsVarArgs) : DerivedType(FunctionTyID),
Chris Lattner893f0252003-06-18 19:22:36 +0000363 ResultType(PATypeHandle(Result, this)),
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000364 isVarArgs(IsVarArgs) {
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000365 bool isAbstract = Result->isAbstract();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000366 ParamTys.reserve(Params.size());
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000367 for (unsigned i = 0; i < Params.size(); ++i) {
Chris Lattner893f0252003-06-18 19:22:36 +0000368 ParamTys.push_back(PATypeHandle(Params[i], this));
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000369 isAbstract |= Params[i]->isAbstract();
370 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000371
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000372 // Calculate whether or not this type is abstract
373 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000374}
375
Chris Lattner47697a12003-05-22 21:21:43 +0000376StructType::StructType(const std::vector<const Type*> &Types)
Chris Lattner6c42c312001-12-14 16:41:56 +0000377 : CompositeType(StructTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000378 ETypes.reserve(Types.size());
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000379 bool isAbstract = false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000380 for (unsigned i = 0; i < Types.size(); ++i) {
381 assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");
Chris Lattner893f0252003-06-18 19:22:36 +0000382 ETypes.push_back(PATypeHandle(Types[i], this));
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000383 isAbstract |= Types[i]->isAbstract();
Chris Lattner56c5acb2001-10-13 07:01:33 +0000384 }
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000385
386 // Calculate whether or not this type is abstract
387 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000388}
389
Chris Lattner6c42c312001-12-14 16:41:56 +0000390ArrayType::ArrayType(const Type *ElType, unsigned NumEl)
391 : SequentialType(ArrayTyID, ElType) {
392 NumElements = NumEl;
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000393
394 // Calculate whether or not this type is abstract
395 setAbstract(ElType->isAbstract());
Chris Lattner00950542001-06-06 20:29:01 +0000396}
397
Chris Lattner6c42c312001-12-14 16:41:56 +0000398PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000399 // Calculate whether or not this type is abstract
400 setAbstract(E->isAbstract());
Chris Lattner6c42c312001-12-14 16:41:56 +0000401}
402
403OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000404 setAbstract(true);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000405#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000406 std::cerr << "Derived new type: " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000407#endif
408}
409
410
Chris Lattner18250092003-10-13 14:03:36 +0000411// getAlwaysOpaqueTy - This function returns an opaque type. It doesn't matter
412// _which_ opaque type it is, but the opaque type must never get resolved.
413//
414static Type *getAlwaysOpaqueTy() {
415 static Type *AlwaysOpaqueTy = OpaqueType::get();
416 static PATypeHolder Holder(AlwaysOpaqueTy);
417 return AlwaysOpaqueTy;
418}
419
420
421//===----------------------------------------------------------------------===//
422// dropAllTypeUses methods - These methods eliminate any possibly recursive type
423// references from a derived type. The type must remain abstract, so we make
424// sure to use an always opaque type as an argument.
425//
426
427void FunctionType::dropAllTypeUses() {
428 ResultType = getAlwaysOpaqueTy();
429 ParamTys.clear();
430}
431
432void ArrayType::dropAllTypeUses() {
433 ElementType = getAlwaysOpaqueTy();
434}
435
436void StructType::dropAllTypeUses() {
437 ETypes.clear();
438 ETypes.push_back(PATypeHandle(getAlwaysOpaqueTy(), this));
439}
440
441void PointerType::dropAllTypeUses() {
442 ElementType = getAlwaysOpaqueTy();
443}
444
445
446
447
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000448// isTypeAbstract - This is a recursive function that walks a type hierarchy
449// calculating whether or not a type is abstract. Worst case it will have to do
450// a lot of traversing if you have some whacko opaque types, but in most cases,
451// it will do some simple stuff when it hits non-abstract types that aren't
452// recursive.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000453//
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000454bool Type::isTypeAbstract() {
455 if (!isAbstract()) // Base case for the recursion
456 return false; // Primitive = leaf type
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000457
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000458 if (isa<OpaqueType>(this)) // Base case for the recursion
459 return true; // This whole type is abstract!
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000460
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000461 // We have to guard against recursion. To do this, we temporarily mark this
462 // type as concrete, so that if we get back to here recursively we will think
463 // it's not abstract, and thus not scan it again.
464 setAbstract(false);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000465
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000466 // Scan all of the sub-types. If any of them are abstract, than so is this
467 // one!
468 for (Type::subtype_iterator I = subtype_begin(), E = subtype_end();
469 I != E; ++I)
470 if (const_cast<Type*>(*I)->isTypeAbstract()) {
Chris Lattner1bb62632003-09-02 22:15:15 +0000471 setAbstract(true); // Restore the abstract bit.
472 return true; // This type is abstract if subtype is abstract!
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000473 }
474
475 // Restore the abstract bit.
476 setAbstract(true);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000477
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000478 // Nothing looks abstract here...
479 return false;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000480}
481
482
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000483//===----------------------------------------------------------------------===//
484// Type Structural Equality Testing
485//===----------------------------------------------------------------------===//
486
487// TypesEqual - Two types are considered structurally equal if they have the
488// same "shape": Every level and element of the types have identical primitive
489// ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
490// be pointer equals to be equivalent though. This uses an optimistic algorithm
491// that assumes that two graphs are the same until proven otherwise.
492//
493static bool TypesEqual(const Type *Ty, const Type *Ty2,
Chris Lattner47697a12003-05-22 21:21:43 +0000494 std::map<const Type *, const Type *> &EqTypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000495 if (Ty == Ty2) return true;
496 if (Ty->getPrimitiveID() != Ty2->getPrimitiveID()) return false;
497 if (Ty->isPrimitiveType()) return true;
Chris Lattner008f9062001-10-24 05:12:04 +0000498 if (isa<OpaqueType>(Ty))
Misha Brukman6b634522003-10-10 17:54:14 +0000499 return false; // Two unequal opaque types are never equal
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000500
Chris Lattner5af41972003-10-13 14:55:56 +0000501 std::map<const Type*, const Type*>::iterator It = EqTypes.lower_bound(Ty);
502 if (It != EqTypes.end() && It->first == Ty)
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000503 return It->second == Ty2; // Looping back on a type, check for equality
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000504
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000505 // Otherwise, add the mapping to the table to make sure we don't get
506 // recursion on the types...
Chris Lattner5af41972003-10-13 14:55:56 +0000507 EqTypes.insert(It, std::make_pair(Ty, Ty2));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000508
Chris Lattner56c5acb2001-10-13 07:01:33 +0000509 // Two really annoying special cases that breaks an otherwise nice simple
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000510 // algorithm is the fact that arraytypes have sizes that differentiates types,
Chris Lattner169726b2003-09-05 02:21:39 +0000511 // and that function types can be varargs or not. Consider this now.
Chris Lattner5af41972003-10-13 14:55:56 +0000512 //
513 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
514 return TypesEqual(PTy->getElementType(),
515 cast<PointerType>(Ty2)->getElementType(), EqTypes);
516 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
517 const StructType::ElementTypes &STyE = STy->getElementTypes();
518 const StructType::ElementTypes &STyE2 =
519 cast<StructType>(Ty2)->getElementTypes();
520 if (STyE.size() != STyE2.size()) return false;
521 for (unsigned i = 0, e = STyE.size(); i != e; ++i)
522 if (!TypesEqual(STyE[i], STyE2[i], EqTypes))
523 return false;
524 return true;
525 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
526 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
527 return ATy->getNumElements() == ATy2->getNumElements() &&
528 TypesEqual(ATy->getElementType(), ATy2->getElementType(), EqTypes);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000529 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner5af41972003-10-13 14:55:56 +0000530 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
531 if (FTy->isVarArg() != FTy2->isVarArg() ||
532 FTy->getParamTypes().size() != FTy2->getParamTypes().size() ||
533 !TypesEqual(FTy->getReturnType(), FTy2->getReturnType(), EqTypes))
Chris Lattner56c5acb2001-10-13 07:01:33 +0000534 return false;
Chris Lattner5af41972003-10-13 14:55:56 +0000535 const FunctionType::ParamTypes &FTyP = FTy->getParamTypes();
536 const FunctionType::ParamTypes &FTy2P = FTy2->getParamTypes();
537 for (unsigned i = 0, e = FTyP.size(); i != e; ++i)
538 if (!TypesEqual(FTyP[i], FTy2P[i], EqTypes))
539 return false;
540 return true;
541 } else {
542 assert(0 && "Unknown derived type!");
543 return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000544 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000545}
546
547static bool TypesEqual(const Type *Ty, const Type *Ty2) {
Chris Lattner47697a12003-05-22 21:21:43 +0000548 std::map<const Type *, const Type *> EqTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000549 return TypesEqual(Ty, Ty2, EqTypes);
550}
551
552
553
554//===----------------------------------------------------------------------===//
555// Derived Type Factory Functions
556//===----------------------------------------------------------------------===//
557
558// TypeMap - Make sure that only one instance of a particular type may be
559// created on any given run of the compiler... note that this involves updating
560// our map if an abstract type gets refined somehow...
561//
562template<class ValType, class TypeClass>
Chris Lattner7685ac82003-10-03 18:46:24 +0000563class TypeMap {
564 typedef std::map<ValType, TypeClass *> MapTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000565 MapTy Map;
566public:
Chris Lattner169726b2003-09-05 02:21:39 +0000567 typedef typename MapTy::iterator iterator;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000568 ~TypeMap() { print("ON EXIT"); }
569
570 inline TypeClass *get(const ValType &V) {
Chris Lattner169726b2003-09-05 02:21:39 +0000571 iterator I = Map.find(V);
Chris Lattner7685ac82003-10-03 18:46:24 +0000572 return I != Map.end() ? I->second : 0;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000573 }
574
575 inline void add(const ValType &V, TypeClass *T) {
Chris Lattner7685ac82003-10-03 18:46:24 +0000576 Map.insert(std::make_pair(V, T));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000577 print("add");
578 }
579
Chris Lattner169726b2003-09-05 02:21:39 +0000580 iterator getEntryForType(TypeClass *Ty) {
581 iterator I = Map.find(ValType::get(Ty));
582 if (I == Map.end()) print("ERROR!");
583 assert(I != Map.end() && "Didn't find type entry!");
Chris Lattner7685ac82003-10-03 18:46:24 +0000584 assert(I->second == Ty && "Type entry wrong?");
Chris Lattner169726b2003-09-05 02:21:39 +0000585 return I;
586 }
587
Chris Lattner266caa22003-09-05 02:30:47 +0000588
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000589 void finishRefinement(iterator TyIt) {
590 TypeClass *Ty = TyIt->second;
Chris Lattner7685ac82003-10-03 18:46:24 +0000591
592 // The old record is now out-of-date, because one of the children has been
593 // updated. Remove the obsolete entry from the map.
594 Map.erase(TyIt);
595
596 // Now we check to see if there is an existing entry in the table which is
597 // structurally identical to the newly refined type. If so, this type gets
598 // refined to the pre-existing type.
599 //
Chris Lattner169726b2003-09-05 02:21:39 +0000600 for (iterator I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattner7685ac82003-10-03 18:46:24 +0000601 if (TypesEqual(Ty, I->second)) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +0000602 assert(Ty->isAbstract() && "Replacing a non-abstract type?");
Chris Lattner7685ac82003-10-03 18:46:24 +0000603 TypeClass *NewTy = I->second;
604
Chris Lattner8df956c2003-09-05 02:39:52 +0000605 // Refined to a different type altogether?
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000606 Ty->refineAbstractTypeTo(NewTy);
Chris Lattner8df956c2003-09-05 02:39:52 +0000607 return;
Chris Lattner169726b2003-09-05 02:21:39 +0000608 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000609
Chris Lattner7685ac82003-10-03 18:46:24 +0000610 // If there is no existing type of the same structure, we reinsert an
611 // updated record into the map.
612 Map.insert(std::make_pair(ValType::get(Ty), Ty));
613
Chris Lattner8df956c2003-09-05 02:39:52 +0000614 // If the type is currently thought to be abstract, rescan all of our
615 // subtypes to see if the type has just become concrete!
Chris Lattner7685ac82003-10-03 18:46:24 +0000616 if (Ty->isAbstract()) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +0000617 Ty->setAbstract(Ty->isTypeAbstract());
618
Chris Lattner7685ac82003-10-03 18:46:24 +0000619 // If the type just became concrete, notify all users!
620 if (!Ty->isAbstract())
621 Ty->notifyUsesThatTypeBecameConcrete();
622 }
Chris Lattner266caa22003-09-05 02:30:47 +0000623 }
Chris Lattner7685ac82003-10-03 18:46:24 +0000624
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000625 void remove(const ValType &OldVal) {
Chris Lattner169726b2003-09-05 02:21:39 +0000626 iterator I = Map.find(OldVal);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000627 assert(I != Map.end() && "TypeMap::remove, element not found!");
628 Map.erase(I);
629 }
630
Chris Lattner169726b2003-09-05 02:21:39 +0000631 void remove(iterator I) {
632 assert(I != Map.end() && "Cannot remove invalid iterator pointer!");
633 Map.erase(I);
634 }
635
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000636 void print(const char *Arg) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000637#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000638 std::cerr << "TypeMap<>::" << Arg << " table contents:\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000639 unsigned i = 0;
Chris Lattner169726b2003-09-05 02:21:39 +0000640 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
641 I != E; ++I)
Chris Lattner7685ac82003-10-03 18:46:24 +0000642 std::cerr << " " << (++i) << ". " << (void*)I->second << " "
643 << *I->second << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000644#endif
645 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000646
647 void dump() const { print("dump output"); }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000648};
649
650
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000651
652//===----------------------------------------------------------------------===//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000653// Function Type Factory and Value Class...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000654//
655
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000656// FunctionValType - Define a class to hold the key that goes into the TypeMap
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000657//
Chris Lattner7685ac82003-10-03 18:46:24 +0000658class FunctionValType {
659 const Type *RetTy;
660 std::vector<const Type*> ArgTypes;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000661 bool isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000662public:
Chris Lattner47697a12003-05-22 21:21:43 +0000663 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
Chris Lattner7685ac82003-10-03 18:46:24 +0000664 bool IVA) : RetTy(ret), isVarArg(IVA) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000665 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattner7685ac82003-10-03 18:46:24 +0000666 ArgTypes.push_back(args[i]);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000667 }
668
Chris Lattner169726b2003-09-05 02:21:39 +0000669 static FunctionValType get(const FunctionType *FT);
670
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000671 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +0000672 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000673 if (RetTy == OldType) RetTy = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +0000674 for (unsigned i = 0, e = ArgTypes.size(); i != e; ++i)
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000675 if (ArgTypes[i] == OldType) ArgTypes[i] = NewType;
676 }
677
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000678 inline bool operator<(const FunctionValType &MTV) const {
Chris Lattner7685ac82003-10-03 18:46:24 +0000679 if (RetTy < MTV.RetTy) return true;
680 if (RetTy > MTV.RetTy) return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000681
682 if (ArgTypes < MTV.ArgTypes) return true;
Chris Lattner7685ac82003-10-03 18:46:24 +0000683 return ArgTypes == MTV.ArgTypes && isVarArg < MTV.isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000684 }
685};
686
687// Define the actual map itself now...
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000688static TypeMap<FunctionValType, FunctionType> FunctionTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000689
Chris Lattner169726b2003-09-05 02:21:39 +0000690FunctionValType FunctionValType::get(const FunctionType *FT) {
691 // Build up a FunctionValType
692 std::vector<const Type *> ParamTypes;
693 ParamTypes.reserve(FT->getParamTypes().size());
694 for (unsigned i = 0, e = FT->getParamTypes().size(); i != e; ++i)
695 ParamTypes.push_back(FT->getParamType(i));
Chris Lattner7685ac82003-10-03 18:46:24 +0000696 return FunctionValType(FT->getReturnType(), ParamTypes, FT->isVarArg());
Chris Lattner169726b2003-09-05 02:21:39 +0000697}
698
699
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000700// FunctionType::get - The factory function for the FunctionType class...
701FunctionType *FunctionType::get(const Type *ReturnType,
Chris Lattner47697a12003-05-22 21:21:43 +0000702 const std::vector<const Type*> &Params,
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000703 bool isVarArg) {
Chris Lattner7685ac82003-10-03 18:46:24 +0000704 FunctionValType VT(ReturnType, Params, isVarArg);
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000705 FunctionType *MT = FunctionTypes.get(VT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000706 if (MT) return MT;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000707
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000708 FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000709
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000710#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000711 std::cerr << "Derived new type: " << MT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000712#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000713 return MT;
Chris Lattner00950542001-06-06 20:29:01 +0000714}
715
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000716//===----------------------------------------------------------------------===//
717// Array Type Factory...
718//
Chris Lattner7685ac82003-10-03 18:46:24 +0000719class ArrayValType {
720 const Type *ValTy;
Chris Lattner6c42c312001-12-14 16:41:56 +0000721 unsigned Size;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000722public:
Chris Lattner7685ac82003-10-03 18:46:24 +0000723 ArrayValType(const Type *val, int sz) : ValTy(val), Size(sz) {}
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattner7685ac82003-10-03 18:46:24 +0000725 static ArrayValType get(const ArrayType *AT) {
726 return ArrayValType(AT->getElementType(), AT->getNumElements());
727 }
Chris Lattner169726b2003-09-05 02:21:39 +0000728
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000729 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +0000730 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000731 assert(ValTy == OldType);
732 ValTy = NewType;
Chris Lattner00950542001-06-06 20:29:01 +0000733 }
734
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000735 inline bool operator<(const ArrayValType &MTV) const {
736 if (Size < MTV.Size) return true;
Chris Lattner7685ac82003-10-03 18:46:24 +0000737 return Size == MTV.Size && ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000738 }
739};
740
741static TypeMap<ArrayValType, ArrayType> ArrayTypes;
742
Chris Lattner169726b2003-09-05 02:21:39 +0000743
Chris Lattner6c42c312001-12-14 16:41:56 +0000744ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000745 assert(ElementType && "Can't get array of null types!");
746
Chris Lattner7685ac82003-10-03 18:46:24 +0000747 ArrayValType AVT(ElementType, NumElements);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000748 ArrayType *AT = ArrayTypes.get(AVT);
749 if (AT) return AT; // Found a match, return it!
750
Chris Lattner00950542001-06-06 20:29:01 +0000751 // Value not found. Derive a new type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000752 ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements));
Chris Lattner00950542001-06-06 20:29:01 +0000753
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000754#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000755 std::cerr << "Derived new type: " << *AT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000756#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000757 return AT;
Chris Lattner00950542001-06-06 20:29:01 +0000758}
759
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000760//===----------------------------------------------------------------------===//
761// Struct Type Factory...
762//
Chris Lattner00950542001-06-06 20:29:01 +0000763
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000764// StructValType - Define a class to hold the key that goes into the TypeMap
765//
Chris Lattner7685ac82003-10-03 18:46:24 +0000766class StructValType {
767 std::vector<const Type*> ElTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000768public:
Chris Lattner7685ac82003-10-03 18:46:24 +0000769 StructValType(const std::vector<const Type*> &args) : ElTypes(args) {}
Chris Lattner00950542001-06-06 20:29:01 +0000770
Chris Lattner7685ac82003-10-03 18:46:24 +0000771 static StructValType get(const StructType *ST) {
772 std::vector<const Type *> ElTypes;
773 ElTypes.reserve(ST->getElementTypes().size());
774 for (unsigned i = 0, e = ST->getElementTypes().size(); i != e; ++i)
775 ElTypes.push_back(ST->getElementTypes()[i]);
776
777 return StructValType(ElTypes);
778 }
Chris Lattner169726b2003-09-05 02:21:39 +0000779
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000780 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +0000781 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000782 for (unsigned i = 0; i < ElTypes.size(); ++i)
783 if (ElTypes[i] == OldType) ElTypes[i] = NewType;
784 }
785
786 inline bool operator<(const StructValType &STV) const {
787 return ElTypes < STV.ElTypes;
788 }
789};
790
791static TypeMap<StructValType, StructType> StructTypes;
792
Chris Lattner47697a12003-05-22 21:21:43 +0000793StructType *StructType::get(const std::vector<const Type*> &ETypes) {
Chris Lattner7685ac82003-10-03 18:46:24 +0000794 StructValType STV(ETypes);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000795 StructType *ST = StructTypes.get(STV);
796 if (ST) return ST;
797
798 // Value not found. Derive a new type!
799 StructTypes.add(STV, ST = new StructType(ETypes));
800
801#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000802 std::cerr << "Derived new type: " << *ST << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000803#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000804 return ST;
805}
806
Chris Lattner169726b2003-09-05 02:21:39 +0000807
808
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000809//===----------------------------------------------------------------------===//
810// Pointer Type Factory...
811//
812
813// PointerValType - Define a class to hold the key that goes into the TypeMap
814//
Chris Lattner7685ac82003-10-03 18:46:24 +0000815class PointerValType {
816 const Type *ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000817public:
Chris Lattner7685ac82003-10-03 18:46:24 +0000818 PointerValType(const Type *val) : ValTy(val) {}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000819
Chris Lattner7685ac82003-10-03 18:46:24 +0000820 static PointerValType get(const PointerType *PT) {
821 return PointerValType(PT->getElementType());
822 }
Chris Lattner169726b2003-09-05 02:21:39 +0000823
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000824 // Subclass should override this... to update self as usual
Chris Lattner7685ac82003-10-03 18:46:24 +0000825 void doRefinement(const DerivedType *OldType, const Type *NewType) {
Chris Lattner417081c2002-04-07 06:14:56 +0000826 assert(ValTy == OldType);
827 ValTy = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000828 }
829
Chris Lattner7685ac82003-10-03 18:46:24 +0000830 bool operator<(const PointerValType &MTV) const {
831 return ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000832 }
833};
834
835static TypeMap<PointerValType, PointerType> PointerTypes;
836
837PointerType *PointerType::get(const Type *ValueType) {
838 assert(ValueType && "Can't get a pointer to <null> type!");
Chris Lattner7685ac82003-10-03 18:46:24 +0000839 PointerValType PVT(ValueType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000840
841 PointerType *PT = PointerTypes.get(PVT);
842 if (PT) return PT;
843
844 // Value not found. Derive a new type!
845 PointerTypes.add(PVT, PT = new PointerType(ValueType));
846
847#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000848 std::cerr << "Derived new type: " << *PT << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000849#endif
850 return PT;
851}
852
Chris Lattner339ba452002-04-06 00:21:11 +0000853void debug_type_tables() {
854 FunctionTypes.dump();
855 ArrayTypes.dump();
856 StructTypes.dump();
857 PointerTypes.dump();
858}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000859
860
861//===----------------------------------------------------------------------===//
862// Derived Type Refinement Functions
863//===----------------------------------------------------------------------===//
864
865// removeAbstractTypeUser - Notify an abstract type that a user of the class
866// no longer has a handle to the type. This function is called primarily by
867// the PATypeHandle class. When there are no users of the abstract type, it
Misha Brukman6b634522003-10-10 17:54:14 +0000868// is annihilated, because there is no way to get a reference to it ever again.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000869//
870void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const {
871 // Search from back to front because we will notify users from back to
872 // front. Also, it is likely that there will be a stack like behavior to
873 // users that register and unregister users.
874 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000875 unsigned i;
876 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
877 assert(i != 0 && "AbstractTypeUser not in user list!");
878
879 --i; // Convert to be in range 0 <= i < size()
880 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
881
882 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000883
884#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000885 std::cerr << " remAbstractTypeUser[" << (void*)this << ", "
Chris Lattnera5112c72003-09-04 23:46:03 +0000886 << *this << "][" << i << "] User = " << U << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000887#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000888
Chris Lattner1c5164e2003-10-02 23:35:57 +0000889 if (AbstractTypeUsers.empty() && RefCount == 0 && isAbstract()) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000890#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000891 std::cerr << "DELETEing unused abstract type: <" << *this
Chris Lattner47697a12003-05-22 21:21:43 +0000892 << ">[" << (void*)this << "]" << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000893#endif
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000894 delete this; // No users of this abstract type!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000895 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000896}
897
898
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000899// refineAbstractTypeTo - This function is used to when it is discovered that
900// the 'this' abstract type is actually equivalent to the NewType specified.
901// This causes all users of 'this' to switch to reference the more concrete type
902// NewType and for 'this' to be deleted.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000903//
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000904void DerivedType::refineAbstractTypeTo(const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000905 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
906 assert(this != NewType && "Can't refine to myself!");
Chris Lattner7685ac82003-10-03 18:46:24 +0000907 assert(ForwardType == 0 && "This type has already been refined!");
908
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000909 // The descriptions may be out of date. Conservatively clear them all!
910 AbstractTypeDescriptions.clear();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000911
912#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +0000913 std::cerr << "REFINING abstract type [" << (void*)this << " "
Chris Lattnera5112c72003-09-04 23:46:03 +0000914 << *this << "] to [" << (void*)NewType << " "
915 << *NewType << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000916#endif
917
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000918 // Make sure to put the type to be refined to into a holder so that if IT gets
919 // refined, that we will not continue using a dead reference...
920 //
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000921 PATypeHolder NewTy(NewType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000922
Chris Lattner7685ac82003-10-03 18:46:24 +0000923 // Any PATypeHolders referring to this type will now automatically forward to
924 // the type we are resolved to.
Chris Lattner1c5164e2003-10-02 23:35:57 +0000925 ForwardType = NewType;
926 if (NewType->isAbstract())
927 cast<DerivedType>(NewType)->addRef();
928
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000929 // Add a self use of the current type so that we don't delete ourself until
Chris Lattner8ef852f2003-10-03 04:48:21 +0000930 // after the function exits.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000931 //
Chris Lattner8ef852f2003-10-03 04:48:21 +0000932 PATypeHolder CurrentTy(this);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000933
Chris Lattner169726b2003-09-05 02:21:39 +0000934 // To make the situation simpler, we ask the subclass to remove this type from
935 // the type map, and to replace any type uses with uses of non-abstract types.
936 // This dramatically limits the amount of recursive type trouble we can find
937 // ourselves in.
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000938 dropAllTypeUses();
Chris Lattner169726b2003-09-05 02:21:39 +0000939
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000940 // Iterate over all of the uses of this type, invoking callback. Each user
Chris Lattner3f59b7e2002-04-05 19:44:07 +0000941 // should remove itself from our use list automatically. We have to check to
942 // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types
943 // will not cause users to drop off of the use list. If we resolve to ourself
944 // we succeed!
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000945 //
Chris Lattner8ef852f2003-10-03 04:48:21 +0000946 while (!AbstractTypeUsers.empty() && NewTy != this) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000947 AbstractTypeUser *User = AbstractTypeUsers.back();
948
Chris Lattner8ef852f2003-10-03 04:48:21 +0000949 unsigned OldSize = AbstractTypeUsers.size();
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000950#ifdef DEBUG_MERGE_TYPES
Chris Lattner8ef852f2003-10-03 04:48:21 +0000951 std::cerr << " REFINING user " << OldSize-1 << "[" << (void*)User
952 << "] of abstract type [" << (void*)this << " "
953 << *this << "] to [" << (void*)NewTy.get() << " "
954 << *NewTy << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000955#endif
Chris Lattner8ef852f2003-10-03 04:48:21 +0000956 User->refineAbstractType(this, NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000957
Chris Lattner8ef852f2003-10-03 04:48:21 +0000958 assert(AbstractTypeUsers.size() != OldSize &&
959 "AbsTyUser did not remove self from user list!");
Chris Lattner00950542001-06-06 20:29:01 +0000960 }
961
Chris Lattner8ef852f2003-10-03 04:48:21 +0000962 // If we were successful removing all users from the type, 'this' will be
963 // deleted when the last PATypeHolder is destroyed or updated from this type.
964 // This may occur on exit of this function, as the CurrentTy object is
965 // destroyed.
Chris Lattner00950542001-06-06 20:29:01 +0000966}
967
Chris Lattner7685ac82003-10-03 18:46:24 +0000968// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
969// the current type has transitioned from being abstract to being concrete.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000970//
Chris Lattner7685ac82003-10-03 18:46:24 +0000971void DerivedType::notifyUsesThatTypeBecameConcrete() {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000972#ifdef DEBUG_MERGE_TYPES
Chris Lattnera5112c72003-09-04 23:46:03 +0000973 std::cerr << "typeIsREFINED type: " << (void*)this << " " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000974#endif
Chris Lattner417081c2002-04-07 06:14:56 +0000975
Chris Lattner7685ac82003-10-03 18:46:24 +0000976 unsigned OldSize = AbstractTypeUsers.size();
977 while (!AbstractTypeUsers.empty()) {
978 AbstractTypeUser *ATU = AbstractTypeUsers.back();
979 ATU->typeBecameConcrete(this);
Chris Lattner417081c2002-04-07 06:14:56 +0000980
Chris Lattner7685ac82003-10-03 18:46:24 +0000981 assert(AbstractTypeUsers.size() < OldSize-- &&
982 "AbstractTypeUser did not remove itself from the use list!");
Chris Lattner00950542001-06-06 20:29:01 +0000983 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000984}
985
Chris Lattner00950542001-06-06 20:29:01 +0000986
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000987
988
989// refineAbstractType - Called when a contained type is found to be more
990// concrete - this could potentially change us from an abstract type to a
991// concrete type.
992//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000993void FunctionType::refineAbstractType(const DerivedType *OldType,
994 const Type *NewType) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +0000995 assert((isAbstract() || !OldType->isAbstract()) &&
996 "Refining a non-abstract type!");
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 << "["
Chris Lattnera5112c72003-09-04 23:46:03 +0000999 << *OldType << "], " << (void*)NewType << " ["
1000 << *NewType << "])\n";
Chris Lattner00950542001-06-06 20:29:01 +00001001#endif
Chris Lattner169726b2003-09-05 02:21:39 +00001002
1003 // Look up our current type map entry..
Chris Lattner169726b2003-09-05 02:21:39 +00001004 TypeMap<FunctionValType, FunctionType>::iterator TMI =
1005 FunctionTypes.getEntryForType(this);
Chris Lattner169726b2003-09-05 02:21:39 +00001006
Chris Lattner3855f2f2002-04-05 22:25:26 +00001007 // Find the type element we are refining...
Chris Lattner417081c2002-04-07 06:14:56 +00001008 if (ResultType == OldType) {
1009 ResultType.removeUserFromConcrete();
1010 ResultType = NewType;
Chris Lattnere244a252001-11-03 03:27:53 +00001011 }
Chris Lattner417081c2002-04-07 06:14:56 +00001012 for (unsigned i = 0, e = ParamTys.size(); i != e; ++i)
1013 if (ParamTys[i] == OldType) {
1014 ParamTys[i].removeUserFromConcrete();
1015 ParamTys[i] = NewType;
1016 }
Chris Lattnere244a252001-11-03 03:27:53 +00001017
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001018 FunctionTypes.finishRefinement(TMI);
Chris Lattner7685ac82003-10-03 18:46:24 +00001019}
1020
1021void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
1022 refineAbstractType(AbsTy, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001023}
1024
1025
1026// refineAbstractType - Called when a contained type is found to be more
1027// concrete - this could potentially change us from an abstract type to a
1028// concrete type.
1029//
1030void ArrayType::refineAbstractType(const DerivedType *OldType,
1031 const Type *NewType) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +00001032 assert((isAbstract() || !OldType->isAbstract()) &&
1033 "Refining a non-abstract type!");
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001034#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001035 std::cerr << "ArrayTy::refineAbstractTy(" << (void*)OldType << "["
Chris Lattnera5112c72003-09-04 23:46:03 +00001036 << *OldType << "], " << (void*)NewType << " ["
1037 << *NewType << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001038#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001039
Chris Lattner169726b2003-09-05 02:21:39 +00001040 // Look up our current type map entry..
1041 TypeMap<ArrayValType, ArrayType>::iterator TMI =
1042 ArrayTypes.getEntryForType(this);
Chris Lattner169726b2003-09-05 02:21:39 +00001043
Chris Lattner417081c2002-04-07 06:14:56 +00001044 assert(getElementType() == OldType);
1045 ElementType.removeUserFromConcrete();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001046 ElementType = NewType;
Chris Lattner417081c2002-04-07 06:14:56 +00001047
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001048 ArrayTypes.finishRefinement(TMI);
Chris Lattner7685ac82003-10-03 18:46:24 +00001049}
1050
1051void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
1052 refineAbstractType(AbsTy, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001053}
1054
1055
1056// refineAbstractType - Called when a contained type is found to be more
1057// concrete - this could potentially change us from an abstract type to a
1058// concrete type.
1059//
1060void StructType::refineAbstractType(const DerivedType *OldType,
1061 const Type *NewType) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +00001062 assert((isAbstract() || !OldType->isAbstract()) &&
1063 "Refining a non-abstract type!");
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001064#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001065 std::cerr << "StructTy::refineAbstractTy(" << (void*)OldType << "["
Chris Lattnera5112c72003-09-04 23:46:03 +00001066 << *OldType << "], " << (void*)NewType << " ["
1067 << *NewType << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001068#endif
Chris Lattner169726b2003-09-05 02:21:39 +00001069
Chris Lattner169726b2003-09-05 02:21:39 +00001070 // Look up our current type map entry..
1071 TypeMap<StructValType, StructType>::iterator TMI =
1072 StructTypes.getEntryForType(this);
Chris Lattner169726b2003-09-05 02:21:39 +00001073
Chris Lattner6a490ce2003-01-14 19:42:39 +00001074 for (int i = ETypes.size()-1; i >= 0; --i)
Chris Lattner417081c2002-04-07 06:14:56 +00001075 if (ETypes[i] == OldType) {
1076 ETypes[i].removeUserFromConcrete();
Chris Lattnere244a252001-11-03 03:27:53 +00001077
Chris Lattner417081c2002-04-07 06:14:56 +00001078 // Update old type to new type in the array...
1079 ETypes[i] = NewType;
1080 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001081
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001082 StructTypes.finishRefinement(TMI);
Chris Lattner7685ac82003-10-03 18:46:24 +00001083}
1084
1085void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
1086 refineAbstractType(AbsTy, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001087}
1088
1089// refineAbstractType - Called when a contained type is found to be more
1090// concrete - this could potentially change us from an abstract type to a
1091// concrete type.
1092//
1093void PointerType::refineAbstractType(const DerivedType *OldType,
1094 const Type *NewType) {
Chris Lattnerc9b24a32003-09-05 05:10:04 +00001095 assert((isAbstract() || !OldType->isAbstract()) &&
1096 "Refining a non-abstract type!");
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001097#ifdef DEBUG_MERGE_TYPES
Chris Lattner47697a12003-05-22 21:21:43 +00001098 std::cerr << "PointerTy::refineAbstractTy(" << (void*)OldType << "["
Chris Lattnera5112c72003-09-04 23:46:03 +00001099 << *OldType << "], " << (void*)NewType << " ["
1100 << *NewType << "])\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001101#endif
Chris Lattner3b1e3f42001-11-02 07:51:31 +00001102
Chris Lattner169726b2003-09-05 02:21:39 +00001103 // Look up our current type map entry..
1104 TypeMap<PointerValType, PointerType>::iterator TMI =
1105 PointerTypes.getEntryForType(this);
Chris Lattner169726b2003-09-05 02:21:39 +00001106
Chris Lattner417081c2002-04-07 06:14:56 +00001107 assert(ElementType == OldType);
1108 ElementType.removeUserFromConcrete();
Chris Lattner6c42c312001-12-14 16:41:56 +00001109 ElementType = NewType;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001110
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001111 PointerTypes.finishRefinement(TMI);
Chris Lattner7685ac82003-10-03 18:46:24 +00001112}
1113
1114void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
1115 refineAbstractType(AbsTy, AbsTy);
Chris Lattner00950542001-06-06 20:29:01 +00001116}
1117