blob: 3000730be23ffc6dae37c8fbfbe9ed17002ec8d4 [file] [log] [blame]
Chris Lattner169726b2003-09-05 02:21:39 +00001//===-- Type.cpp - Implement the Type class -------------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Type class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/ADT/DepthFirstIterator.h"
17#include "llvm/ADT/StringExtras.h"
Chris Lattnera6b10b62004-10-07 19:20:48 +000018#include "llvm/ADT/SCCIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
Chris Lattnerd115ef82005-11-10 01:40:59 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerde65fb32006-09-28 23:38:07 +000022#include "llvm/Support/ManagedStatic.h"
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000023#include "llvm/Support/Debug.h"
Chris Lattner417081c2002-04-07 06:14:56 +000024#include <algorithm>
Chris Lattnerf2586d12003-11-19 06:14:38 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnerc038a2f2001-09-07 16:56:42 +000027// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
28// created and later destroyed, all in an effort to make sure that there is only
Chris Lattner065a6162003-09-10 05:29:43 +000029// a single canonical version of a type.
Chris Lattnerc038a2f2001-09-07 16:56:42 +000030//
Andrew Lenharth38ecbf12006-12-08 18:06:16 +000031// #define DEBUG_MERGE_TYPES 1
Chris Lattnerc038a2f2001-09-07 16:56:42 +000032
Chris Lattner1cd4c722004-02-26 07:24:18 +000033AbstractTypeUser::~AbstractTypeUser() {}
Chris Lattnerc038a2f2001-09-07 16:56:42 +000034
Jim Laskeya25dfd22006-07-25 23:22:00 +000035
36//===----------------------------------------------------------------------===//
37// Type PATypeHolder Implementation
38//===----------------------------------------------------------------------===//
39
Jim Laskeya25dfd22006-07-25 23:22:00 +000040/// get - This implements the forwarding part of the union-find algorithm for
41/// abstract types. Before every access to the Type*, we check to see if the
42/// type we are pointing to is forwarding to a new type. If so, we drop our
43/// reference to the type.
44///
45Type* PATypeHolder::get() const {
46 const Type *NewTy = Ty->getForwardedType();
47 if (!NewTy) return const_cast<Type*>(Ty);
48 return *const_cast<PATypeHolder*>(this) = NewTy;
49}
50
Chris Lattner00950542001-06-06 20:29:01 +000051//===----------------------------------------------------------------------===//
52// Type Class Implementation
53//===----------------------------------------------------------------------===//
54
Chris Lattnerdd4b4212003-09-02 16:35:17 +000055// Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
56// for types as they are needed. Because resolution of types must invalidate
57// all of the abstract type descriptions, we keep them in a seperate map to make
58// this easy.
Chris Lattnerde65fb32006-09-28 23:38:07 +000059static ManagedStatic<std::map<const Type*,
60 std::string> > ConcreteTypeDescriptions;
61static ManagedStatic<std::map<const Type*,
62 std::string> > AbstractTypeDescriptions;
Chris Lattnerdd4b4212003-09-02 16:35:17 +000063
Reid Spencer5a1ebb32007-04-06 02:02:20 +000064/// Because of the way Type subclasses are allocated, this function is necessary
65/// to use the correct kind of "delete" operator to deallocate the Type object.
66/// Some type objects (FunctionTy, StructTy) allocate additional space after
67/// the space for their derived type to hold the contained types array of
68/// PATypeHandles. Using this allocation scheme means all the PATypeHandles are
69/// allocated with the type object, decreasing allocations and eliminating the
70/// need for a std::vector to be used in the Type class itself.
71/// @brief Type destruction function
72void Type::destroy() const {
73
74 // Structures and Functions allocate their contained types past the end of
75 // the type object itself. These need to be destroyed differently than the
76 // other types.
77 if (isa<FunctionType>(this) || isa<StructType>(this)) {
78 // First, make sure we destruct any PATypeHandles allocated by these
79 // subclasses. They must be manually destructed.
80 for (unsigned i = 0; i < NumContainedTys; ++i)
81 ContainedTys[i].PATypeHandle::~PATypeHandle();
82
83 // Now call the destructor for the subclass directly because we're going
84 // to delete this as an array of char.
85 if (isa<FunctionType>(this))
86 ((FunctionType*)this)->FunctionType::~FunctionType();
87 else
88 ((StructType*)this)->StructType::~StructType();
89
90 // Finally, remove the memory as an array deallocation of the chars it was
91 // constructed from.
92 delete [] reinterpret_cast<const char*>(this);
93
94 return;
95 }
96
97 // For all the other type subclasses, there is either no contained types or
98 // just one (all Sequentials). For Sequentials, the PATypeHandle is not
99 // allocated past the type object, its included directly in the SequentialType
100 // class. This means we can safely just do "normal" delete of this object and
101 // all the destructors that need to run will be run.
102 delete this;
103}
Chris Lattnercfe82272005-11-13 03:14:09 +0000104
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000105const Type *Type::getPrimitiveType(TypeID IDNumber) {
Chris Lattner00950542001-06-06 20:29:01 +0000106 switch (IDNumber) {
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000107 case VoidTyID : return VoidTy;
108 case FloatTyID : return FloatTy;
109 case DoubleTyID : return DoubleTy;
110 case X86_FP80TyID : return X86_FP80Ty;
111 case FP128TyID : return FP128Ty;
112 case PPC_FP128TyID : return PPC_FP128Ty;
113 case LabelTyID : return LabelTy;
Chris Lattner00950542001-06-06 20:29:01 +0000114 default:
115 return 0;
116 }
117}
118
Reid Spencerc1030572007-01-19 21:13:56 +0000119const Type *Type::getVAArgsPromotedType() const {
120 if (ID == IntegerTyID && getSubclassData() < 32)
121 return Type::Int32Ty;
122 else if (ID == FloatTyID)
123 return Type::DoubleTy;
124 else
125 return this;
126}
127
Dan Gohmanef1af7d2007-08-20 19:25:59 +0000128/// isIntOrIntVector - Return true if this is an integer type or a vector of
129/// integer types.
130///
131bool Type::isIntOrIntVector() const {
132 if (isInteger())
133 return true;
134 if (ID != Type::VectorTyID) return false;
135
136 return cast<VectorType>(this)->getElementType()->isInteger();
137}
138
Chris Lattner2e1c1962006-10-26 18:22:45 +0000139/// isFPOrFPVector - Return true if this is a FP type or a vector of FP types.
140///
141bool Type::isFPOrFPVector() const {
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000142 if (ID == Type::FloatTyID || ID == Type::DoubleTyID ||
143 ID == Type::FP128TyID || ID == Type::X86_FP80TyID ||
144 ID == Type::PPC_FP128TyID)
145 return true;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000146 if (ID != Type::VectorTyID) return false;
Chris Lattner2e1c1962006-10-26 18:22:45 +0000147
Reid Spencer9d6565a2007-02-15 02:26:10 +0000148 return cast<VectorType>(this)->getElementType()->isFloatingPoint();
Chris Lattner2e1c1962006-10-26 18:22:45 +0000149}
150
Reid Spencer3da59db2006-11-27 01:05:10 +0000151// canLosslesllyBitCastTo - Return true if this type can be converted to
Chris Lattner58716b92001-11-26 17:01:47 +0000152// 'Ty' without any reinterpretation of bits. For example, uint to int.
153//
Reid Spencer3da59db2006-11-27 01:05:10 +0000154bool Type::canLosslesslyBitCastTo(const Type *Ty) const {
155 // Identity cast means no change so return true
156 if (this == Ty)
Chris Lattnera3124a32006-04-02 05:40:28 +0000157 return true;
158
Reid Spencer3da59db2006-11-27 01:05:10 +0000159 // They are not convertible unless they are at least first class types
160 if (!this->isFirstClassType() || !Ty->isFirstClassType())
161 return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000162
Reid Spencerac9dcb92007-02-15 03:39:18 +0000163 // Vector -> Vector conversions are always lossless if the two vector types
Reid Spencer3da59db2006-11-27 01:05:10 +0000164 // have the same size, otherwise not.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000165 if (const VectorType *thisPTy = dyn_cast<VectorType>(this))
166 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +0000167 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
Chris Lattner58716b92001-11-26 17:01:47 +0000168
Reid Spencer3da59db2006-11-27 01:05:10 +0000169 // At this point we have only various mismatches of the first class types
170 // remaining and ptr->ptr. Just select the lossless conversions. Everything
171 // else is not lossless.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000172 if (isa<PointerType>(this))
Reid Spencer9c2e86a2006-12-31 05:25:34 +0000173 return isa<PointerType>(Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +0000174 return false; // Other types have no identity values
Chris Lattner58716b92001-11-26 17:01:47 +0000175}
176
Chris Lattner4f0247c2005-04-23 22:00:09 +0000177unsigned Type::getPrimitiveSizeInBits() const {
178 switch (getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000179 case Type::FloatTyID: return 32;
Chris Lattnerea104912005-04-23 22:01:39 +0000180 case Type::DoubleTyID: return 64;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000181 case Type::X86_FP80TyID: return 80;
182 case Type::FP128TyID: return 128;
183 case Type::PPC_FP128TyID: return 128;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000184 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
Reid Spencer9d6565a2007-02-15 02:26:10 +0000185 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
Chris Lattnerd44023e2002-05-06 16:14:39 +0000186 default: return 0;
187 }
188}
189
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000190/// isSizedDerivedType - Derived types like structures and arrays are sized
191/// iff all of the members of the type are sized as well. Since asking for
192/// their size is relatively uncommon, move this operation out of line.
193bool Type::isSizedDerivedType() const {
Reid Spencer43276ee2007-01-26 07:51:36 +0000194 if (isa<IntegerType>(this))
195 return true;
196
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000197 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
198 return ATy->getElementType()->isSized();
199
Reid Spencer9d6565a2007-02-15 02:26:10 +0000200 if (const VectorType *PTy = dyn_cast<VectorType>(this))
Chris Lattnerf0228052004-12-01 17:12:16 +0000201 return PTy->getElementType()->isSized();
202
Reid Spencera54b7cb2007-01-12 07:05:14 +0000203 if (!isa<StructType>(this))
204 return false;
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000205
206 // Okay, our struct is sized if all of the elements are...
207 for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000208 if (!(*I)->isSized())
209 return false;
Chris Lattnerc5f143b2004-07-02 23:20:17 +0000210
211 return true;
212}
Chris Lattner58716b92001-11-26 17:01:47 +0000213
Chris Lattner1c5164e2003-10-02 23:35:57 +0000214/// getForwardedTypeInternal - This method is used to implement the union-find
215/// algorithm for when a type is being forwarded to another type.
216const Type *Type::getForwardedTypeInternal() const {
217 assert(ForwardType && "This type is not being forwarded to another type!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000218
Chris Lattner1c5164e2003-10-02 23:35:57 +0000219 // Check to see if the forwarded type has been forwarded on. If so, collapse
220 // the forwarding links.
221 const Type *RealForwardedType = ForwardType->getForwardedType();
222 if (!RealForwardedType)
223 return ForwardType; // No it's not forwarded again
224
225 // Yes, it is forwarded again. First thing, add the reference to the new
226 // forward type.
227 if (RealForwardedType->isAbstract())
228 cast<DerivedType>(RealForwardedType)->addRef();
229
230 // Now drop the old reference. This could cause ForwardType to get deleted.
231 cast<DerivedType>(ForwardType)->dropRef();
Misha Brukmanfd939082005-04-21 23:48:37 +0000232
Chris Lattner1c5164e2003-10-02 23:35:57 +0000233 // Return the updated type.
234 ForwardType = RealForwardedType;
235 return ForwardType;
236}
237
Chris Lattnerc35abc22005-11-13 03:26:33 +0000238void Type::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
239 abort();
240}
241void Type::typeBecameConcrete(const DerivedType *AbsTy) {
242 abort();
243}
244
245
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000246// getTypeDescription - This is a recursive function that walks a type hierarchy
247// calculating the description for a type.
248//
249static std::string getTypeDescription(const Type *Ty,
250 std::vector<const Type *> &TypeStack) {
251 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
252 std::map<const Type*, std::string>::iterator I =
Chris Lattnerde65fb32006-09-28 23:38:07 +0000253 AbstractTypeDescriptions->lower_bound(Ty);
254 if (I != AbstractTypeDescriptions->end() && I->first == Ty)
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000255 return I->second;
Chris Lattner51662c72004-07-08 22:31:09 +0000256 std::string Desc = "opaque";
Chris Lattnerde65fb32006-09-28 23:38:07 +0000257 AbstractTypeDescriptions->insert(std::make_pair(Ty, Desc));
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000258 return Desc;
259 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000260
Chris Lattner87ca5fa2003-09-02 21:41:05 +0000261 if (!Ty->isAbstract()) { // Base case for the recursion
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000262 std::map<const Type*, std::string>::iterator I =
Chris Lattnerde65fb32006-09-28 23:38:07 +0000263 ConcreteTypeDescriptions->find(Ty);
Chris Lattnera5f5a9c2007-04-20 22:33:47 +0000264 if (I != ConcreteTypeDescriptions->end())
265 return I->second;
266
267 if (Ty->isPrimitiveType()) {
268 switch (Ty->getTypeID()) {
269 default: assert(0 && "Unknown prim type!");
270 case Type::VoidTyID: return (*ConcreteTypeDescriptions)[Ty] = "void";
271 case Type::FloatTyID: return (*ConcreteTypeDescriptions)[Ty] = "float";
272 case Type::DoubleTyID: return (*ConcreteTypeDescriptions)[Ty] = "double";
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000273 case Type::X86_FP80TyID:
274 return (*ConcreteTypeDescriptions)[Ty] = "x86_fp80";
275 case Type::FP128TyID: return (*ConcreteTypeDescriptions)[Ty] = "fp128";
276 case Type::PPC_FP128TyID:
277 return (*ConcreteTypeDescriptions)[Ty] = "ppc_fp128";
Chris Lattnera5f5a9c2007-04-20 22:33:47 +0000278 case Type::LabelTyID: return (*ConcreteTypeDescriptions)[Ty] = "label";
279 }
280 }
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000281 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000282
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000283 // Check to see if the Type is already on the stack...
284 unsigned Slot = 0, CurSize = TypeStack.size();
285 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Misha Brukmanfd939082005-04-21 23:48:37 +0000286
287 // This is another base case for the recursion. In this case, we know
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000288 // that we have looped back to a type that we have previously visited.
289 // Generate the appropriate upreference to handle this.
Misha Brukmanfd939082005-04-21 23:48:37 +0000290 //
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000291 if (Slot < CurSize)
292 return "\\" + utostr(CurSize-Slot); // Here's the upreference
293
294 // Recursive case: derived types...
295 std::string Result;
296 TypeStack.push_back(Ty); // Add us to the stack..
Misha Brukmanfd939082005-04-21 23:48:37 +0000297
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000298 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000299 case Type::IntegerTyID: {
300 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencera1fed2d2007-01-13 01:09:33 +0000301 Result = "i" + utostr(ITy->getBitWidth());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000302 break;
303 }
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000304 case Type::FunctionTyID: {
305 const FunctionType *FTy = cast<FunctionType>(Ty);
Reid Spencer9c2e86a2006-12-31 05:25:34 +0000306 if (!Result.empty())
307 Result += " ";
308 Result += getTypeDescription(FTy->getReturnType(), TypeStack) + " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000309 for (FunctionType::param_iterator I = FTy->param_begin(),
Duncan Sandsdc024672007-11-27 13:23:08 +0000310 E = FTy->param_end(); I != E; ++I) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000311 if (I != FTy->param_begin())
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000312 Result += ", ";
313 Result += getTypeDescription(*I, TypeStack);
314 }
315 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000316 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000317 Result += "...";
318 }
319 Result += ")";
320 break;
321 }
322 case Type::StructTyID: {
323 const StructType *STy = cast<StructType>(Ty);
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000324 if (STy->isPacked())
325 Result = "<{ ";
326 else
327 Result = "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000328 for (StructType::element_iterator I = STy->element_begin(),
329 E = STy->element_end(); I != E; ++I) {
330 if (I != STy->element_begin())
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000331 Result += ", ";
332 Result += getTypeDescription(*I, TypeStack);
333 }
334 Result += " }";
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000335 if (STy->isPacked())
336 Result += ">";
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000337 break;
338 }
339 case Type::PointerTyID: {
340 const PointerType *PTy = cast<PointerType>(Ty);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000341 Result = getTypeDescription(PTy->getElementType(), TypeStack);
342 if (unsigned AddressSpace = PTy->getAddressSpace())
343 Result += " addrspace(" + utostr(AddressSpace) + ")";
344 Result += " *";
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000345 break;
346 }
347 case Type::ArrayTyID: {
348 const ArrayType *ATy = cast<ArrayType>(Ty);
349 unsigned NumElements = ATy->getNumElements();
350 Result = "[";
351 Result += utostr(NumElements) + " x ";
352 Result += getTypeDescription(ATy->getElementType(), TypeStack) + "]";
353 break;
354 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000355 case Type::VectorTyID: {
356 const VectorType *PTy = cast<VectorType>(Ty);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000357 unsigned NumElements = PTy->getNumElements();
358 Result = "<";
359 Result += utostr(NumElements) + " x ";
360 Result += getTypeDescription(PTy->getElementType(), TypeStack) + ">";
361 break;
362 }
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000363 default:
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000364 Result = "<error>";
Chris Lattnerd8d6c762003-09-02 22:50:02 +0000365 assert(0 && "Unhandled type in getTypeDescription!");
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000366 }
367
368 TypeStack.pop_back(); // Remove self from stack...
369
Chris Lattnera3057e82003-09-04 23:41:03 +0000370 return Result;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000371}
372
373
374
375static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
376 const Type *Ty) {
377 std::map<const Type*, std::string>::iterator I = Map.find(Ty);
378 if (I != Map.end()) return I->second;
Misha Brukmanfd939082005-04-21 23:48:37 +0000379
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000380 std::vector<const Type *> TypeStack;
Chris Lattnerded36132005-03-02 03:54:43 +0000381 std::string Result = getTypeDescription(Ty, TypeStack);
382 return Map[Ty] = Result;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000383}
384
385
386const std::string &Type::getDescription() const {
387 if (isAbstract())
Chris Lattnerde65fb32006-09-28 23:38:07 +0000388 return getOrCreateDesc(*AbstractTypeDescriptions, this);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000389 else
Chris Lattnerde65fb32006-09-28 23:38:07 +0000390 return getOrCreateDesc(*ConcreteTypeDescriptions, this);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000391}
392
393
Chris Lattner58716b92001-11-26 17:01:47 +0000394bool StructType::indexValid(const Value *V) const {
Reid Spencer9c2e86a2006-12-31 05:25:34 +0000395 // Structure indexes require 32-bit integer constants.
396 if (V->getType() == Type::Int32Ty)
Reid Spencerb83eb642006-10-20 07:07:24 +0000397 if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000398 return CU->getZExtValue() < NumContainedTys;
Chris Lattnerb9da9c12003-11-25 21:21:46 +0000399 return false;
Chris Lattner58716b92001-11-26 17:01:47 +0000400}
401
402// getTypeAtIndex - Given an index value into the type, return the type of the
403// element. For a structure type, this must be a constant value...
404//
405const Type *StructType::getTypeAtIndex(const Value *V) const {
Chris Lattner28977af2004-04-05 01:30:19 +0000406 assert(indexValid(V) && "Invalid structure index!");
Reid Spencerb83eb642006-10-20 07:07:24 +0000407 unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
Chris Lattnerf32f5682004-02-09 05:40:24 +0000408 return ContainedTys[Idx];
Chris Lattner58716b92001-11-26 17:01:47 +0000409}
410
Chris Lattner00950542001-06-06 20:29:01 +0000411//===----------------------------------------------------------------------===//
Chris Lattnerab4fa4f2006-09-28 23:45:00 +0000412// Primitive 'Type' data
Chris Lattner00950542001-06-06 20:29:01 +0000413//===----------------------------------------------------------------------===//
414
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000415const Type *Type::VoidTy = new Type(Type::VoidTyID);
416const Type *Type::FloatTy = new Type(Type::FloatTyID);
417const Type *Type::DoubleTy = new Type(Type::DoubleTyID);
418const Type *Type::X86_FP80Ty = new Type(Type::X86_FP80TyID);
419const Type *Type::FP128Ty = new Type(Type::FP128TyID);
420const Type *Type::PPC_FP128Ty = new Type(Type::PPC_FP128TyID);
421const Type *Type::LabelTy = new Type(Type::LabelTyID);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000422
Chris Lattnerd1f711f2007-02-09 22:24:04 +0000423namespace {
424 struct BuiltinIntegerType : public IntegerType {
425 BuiltinIntegerType(unsigned W) : IntegerType(W) {}
426 };
427}
428const IntegerType *Type::Int1Ty = new BuiltinIntegerType(1);
429const IntegerType *Type::Int8Ty = new BuiltinIntegerType(8);
430const IntegerType *Type::Int16Ty = new BuiltinIntegerType(16);
431const IntegerType *Type::Int32Ty = new BuiltinIntegerType(32);
432const IntegerType *Type::Int64Ty = new BuiltinIntegerType(64);
Chris Lattner00950542001-06-06 20:29:01 +0000433
434
435//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000436// Derived Type Constructors
437//===----------------------------------------------------------------------===//
438
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000439FunctionType::FunctionType(const Type *Result,
Misha Brukmanfd939082005-04-21 23:48:37 +0000440 const std::vector<const Type*> &Params,
Duncan Sandsdc024672007-11-27 13:23:08 +0000441 bool IsVarArgs)
442 : DerivedType(FunctionTyID), isVarArgs(IsVarArgs) {
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000443 ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
444 NumContainedTys = Params.size() + 1; // + 1 for result type
Chris Lattnerc9647152004-07-07 06:48:27 +0000445 assert((Result->isFirstClassType() || Result == Type::VoidTy ||
Misha Brukmanfd939082005-04-21 23:48:37 +0000446 isa<OpaqueType>(Result)) &&
Chris Lattneredfc49d2004-07-06 23:25:19 +0000447 "LLVM functions cannot return aggregates");
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000448 bool isAbstract = Result->isAbstract();
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000449 new (&ContainedTys[0]) PATypeHandle(Result, this);
Chris Lattnerf32f5682004-02-09 05:40:24 +0000450
451 for (unsigned i = 0; i != Params.size(); ++i) {
Chris Lattnerb4091e52004-07-13 20:09:51 +0000452 assert((Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])) &&
453 "Function arguments must be value types!");
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000454 new (&ContainedTys[i+1]) PATypeHandle(Params[i],this);
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000455 isAbstract |= Params[i]->isAbstract();
456 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000457
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000458 // Calculate whether or not this type is abstract
459 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000460}
461
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000462StructType::StructType(const std::vector<const Type*> &Types, bool isPacked)
Chris Lattner6c42c312001-12-14 16:41:56 +0000463 : CompositeType(StructTyID) {
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000464 ContainedTys = reinterpret_cast<PATypeHandle*>(this + 1);
465 NumContainedTys = Types.size();
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000466 setSubclassData(isPacked);
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000467 bool isAbstract = false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000468 for (unsigned i = 0; i < Types.size(); ++i) {
Chris Lattnere83593b2004-03-29 00:17:20 +0000469 assert(Types[i] != Type::VoidTy && "Void type for structure field!!");
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000470 new (&ContainedTys[i]) PATypeHandle(Types[i], this);
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000471 isAbstract |= Types[i]->isAbstract();
Chris Lattner56c5acb2001-10-13 07:01:33 +0000472 }
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000473
474 // Calculate whether or not this type is abstract
475 setAbstract(isAbstract);
Chris Lattner00950542001-06-06 20:29:01 +0000476}
477
Chris Lattner7d7a0ed2005-01-08 20:19:51 +0000478ArrayType::ArrayType(const Type *ElType, uint64_t NumEl)
Chris Lattner6c42c312001-12-14 16:41:56 +0000479 : SequentialType(ArrayTyID, ElType) {
480 NumElements = NumEl;
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000481
482 // Calculate whether or not this type is abstract
483 setAbstract(ElType->isAbstract());
Chris Lattner00950542001-06-06 20:29:01 +0000484}
485
Reid Spencer9d6565a2007-02-15 02:26:10 +0000486VectorType::VectorType(const Type *ElType, unsigned NumEl)
487 : SequentialType(VectorTyID, ElType) {
Brian Gaeke715c90b2004-08-20 06:00:58 +0000488 NumElements = NumEl;
Reid Spencere463fc82007-02-10 19:03:01 +0000489 setAbstract(ElType->isAbstract());
Reid Spencer9d6565a2007-02-15 02:26:10 +0000490 assert(NumEl > 0 && "NumEl of a VectorType must be greater than 0");
Reid Spencercc5dc2e2007-02-10 22:02:45 +0000491 assert((ElType->isInteger() || ElType->isFloatingPoint() ||
492 isa<OpaqueType>(ElType)) &&
Reid Spencer9d6565a2007-02-15 02:26:10 +0000493 "Elements of a VectorType must be a primitive type");
Reid Spencercc5dc2e2007-02-10 22:02:45 +0000494
Brian Gaeke715c90b2004-08-20 06:00:58 +0000495}
496
497
Christopher Lambfe63fb92007-12-11 08:59:05 +0000498PointerType::PointerType(const Type *E, unsigned AddrSpace)
499 : SequentialType(PointerTyID, E) {
500 AddressSpace = AddrSpace;
Chris Lattnera3ad5b22003-09-03 14:44:53 +0000501 // Calculate whether or not this type is abstract
502 setAbstract(E->isAbstract());
Chris Lattner6c42c312001-12-14 16:41:56 +0000503}
504
505OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000506 setAbstract(true);
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000507#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000508 DOUT << "Derived new type: " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000509#endif
510}
511
Chris Lattnerf32f5682004-02-09 05:40:24 +0000512// dropAllTypeUses - When this (abstract) type is resolved to be equal to
513// another (more concrete) type, we must eliminate all references to other
514// types, to avoid some circular reference problems.
515void DerivedType::dropAllTypeUses() {
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000516 if (NumContainedTys != 0) {
Chris Lattnerf32f5682004-02-09 05:40:24 +0000517 // The type must stay abstract. To do this, we insert a pointer to a type
518 // that will never get resolved, thus will always be abstract.
519 static Type *AlwaysOpaqueTy = OpaqueType::get();
520 static PATypeHolder Holder(AlwaysOpaqueTy);
521 ContainedTys[0] = AlwaysOpaqueTy;
Chris Lattner3787c952005-11-16 06:09:47 +0000522
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000523 // Change the rest of the types to be Int32Ty's. It doesn't matter what we
Chris Lattner3787c952005-11-16 06:09:47 +0000524 // pick so long as it doesn't point back to this type. We choose something
525 // concrete to avoid overhead for adding to AbstracTypeUser lists and stuff.
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000526 for (unsigned i = 1, e = NumContainedTys; i != e; ++i)
Reid Spencer9c2e86a2006-12-31 05:25:34 +0000527 ContainedTys[i] = Type::Int32Ty;
Chris Lattnerf32f5682004-02-09 05:40:24 +0000528 }
Chris Lattner18250092003-10-13 14:03:36 +0000529}
530
Chris Lattnera6b10b62004-10-07 19:20:48 +0000531
532
533/// TypePromotionGraph and graph traits - this is designed to allow us to do
534/// efficient SCC processing of type graphs. This is the exact same as
535/// GraphTraits<Type*>, except that we pretend that concrete types have no
536/// children to avoid processing them.
537struct TypePromotionGraph {
538 Type *Ty;
539 TypePromotionGraph(Type *T) : Ty(T) {}
540};
541
542namespace llvm {
543 template <> struct GraphTraits<TypePromotionGraph> {
544 typedef Type NodeType;
545 typedef Type::subtype_iterator ChildIteratorType;
Misha Brukmanfd939082005-04-21 23:48:37 +0000546
Chris Lattnera6b10b62004-10-07 19:20:48 +0000547 static inline NodeType *getEntryNode(TypePromotionGraph G) { return G.Ty; }
Misha Brukmanfd939082005-04-21 23:48:37 +0000548 static inline ChildIteratorType child_begin(NodeType *N) {
Chris Lattnera6b10b62004-10-07 19:20:48 +0000549 if (N->isAbstract())
Misha Brukmanfd939082005-04-21 23:48:37 +0000550 return N->subtype_begin();
Chris Lattnera6b10b62004-10-07 19:20:48 +0000551 else // No need to process children of concrete types.
Misha Brukmanfd939082005-04-21 23:48:37 +0000552 return N->subtype_end();
Chris Lattnera6b10b62004-10-07 19:20:48 +0000553 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000554 static inline ChildIteratorType child_end(NodeType *N) {
Chris Lattnera6b10b62004-10-07 19:20:48 +0000555 return N->subtype_end();
556 }
557 };
558}
559
560
Chris Lattnerb5c16702004-10-06 16:36:46 +0000561// PromoteAbstractToConcrete - This is a recursive function that walks a type
562// graph calculating whether or not a type is abstract.
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000563//
Chris Lattnera6b10b62004-10-07 19:20:48 +0000564void Type::PromoteAbstractToConcrete() {
565 if (!isAbstract()) return;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000566
Chris Lattnera6b10b62004-10-07 19:20:48 +0000567 scc_iterator<TypePromotionGraph> SI = scc_begin(TypePromotionGraph(this));
568 scc_iterator<TypePromotionGraph> SE = scc_end (TypePromotionGraph(this));
Chris Lattnerb5c16702004-10-06 16:36:46 +0000569
Chris Lattnera6b10b62004-10-07 19:20:48 +0000570 for (; SI != SE; ++SI) {
571 std::vector<Type*> &SCC = *SI;
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000572
Chris Lattnera6b10b62004-10-07 19:20:48 +0000573 // Concrete types are leaves in the tree. Since an SCC will either be all
574 // abstract or all concrete, we only need to check one type.
575 if (SCC[0]->isAbstract()) {
576 if (isa<OpaqueType>(SCC[0]))
577 return; // Not going to be concrete, sorry.
578
579 // If all of the children of all of the types in this SCC are concrete,
580 // then this SCC is now concrete as well. If not, neither this SCC, nor
581 // any parent SCCs will be concrete, so we might as well just exit.
582 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
583 for (Type::subtype_iterator CI = SCC[i]->subtype_begin(),
584 E = SCC[i]->subtype_end(); CI != E; ++CI)
585 if ((*CI)->isAbstract())
Chris Lattner76da6162005-03-09 17:34:27 +0000586 // If the child type is in our SCC, it doesn't make the entire SCC
587 // abstract unless there is a non-SCC abstract type.
588 if (std::find(SCC.begin(), SCC.end(), *CI) == SCC.end())
589 return; // Not going to be concrete, sorry.
Misha Brukmanfd939082005-04-21 23:48:37 +0000590
Chris Lattnera6b10b62004-10-07 19:20:48 +0000591 // Okay, we just discovered this whole SCC is now concrete, mark it as
592 // such!
593 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
594 assert(SCC[i]->isAbstract() && "Why are we processing concrete types?");
Misha Brukmanfd939082005-04-21 23:48:37 +0000595
Chris Lattnera6b10b62004-10-07 19:20:48 +0000596 SCC[i]->setAbstract(false);
Chris Lattner76da6162005-03-09 17:34:27 +0000597 }
598
599 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
600 assert(!SCC[i]->isAbstract() && "Concrete type became abstract?");
Chris Lattnera6b10b62004-10-07 19:20:48 +0000601 // The type just became concrete, notify all users!
602 cast<DerivedType>(SCC[i])->notifyUsesThatTypeBecameConcrete();
603 }
Chris Lattnerbc4846d2003-09-02 21:56:34 +0000604 }
Chris Lattnera6b10b62004-10-07 19:20:48 +0000605 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000606}
607
608
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000609//===----------------------------------------------------------------------===//
610// Type Structural Equality Testing
611//===----------------------------------------------------------------------===//
612
613// TypesEqual - Two types are considered structurally equal if they have the
614// same "shape": Every level and element of the types have identical primitive
615// ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
616// be pointer equals to be equivalent though. This uses an optimistic algorithm
617// that assumes that two graphs are the same until proven otherwise.
618//
619static bool TypesEqual(const Type *Ty, const Type *Ty2,
Reid Spencer6e885d02004-07-04 12:14:17 +0000620 std::map<const Type *, const Type *> &EqTypes) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000621 if (Ty == Ty2) return true;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000622 if (Ty->getTypeID() != Ty2->getTypeID()) return false;
Chris Lattner008f9062001-10-24 05:12:04 +0000623 if (isa<OpaqueType>(Ty))
Misha Brukman6b634522003-10-10 17:54:14 +0000624 return false; // Two unequal opaque types are never equal
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000625
Chris Lattner5af41972003-10-13 14:55:56 +0000626 std::map<const Type*, const Type*>::iterator It = EqTypes.lower_bound(Ty);
627 if (It != EqTypes.end() && It->first == Ty)
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000628 return It->second == Ty2; // Looping back on a type, check for equality
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000629
Chris Lattner3b1e3f42001-11-02 07:51:31 +0000630 // Otherwise, add the mapping to the table to make sure we don't get
631 // recursion on the types...
Chris Lattner5af41972003-10-13 14:55:56 +0000632 EqTypes.insert(It, std::make_pair(Ty, Ty2));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000633
Chris Lattner56c5acb2001-10-13 07:01:33 +0000634 // Two really annoying special cases that breaks an otherwise nice simple
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000635 // algorithm is the fact that arraytypes have sizes that differentiates types,
Chris Lattner169726b2003-09-05 02:21:39 +0000636 // and that function types can be varargs or not. Consider this now.
Chris Lattner5af41972003-10-13 14:55:56 +0000637 //
Reid Spencera54b7cb2007-01-12 07:05:14 +0000638 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
639 const IntegerType *ITy2 = cast<IntegerType>(Ty2);
640 return ITy->getBitWidth() == ITy2->getBitWidth();
641 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Christopher Lambfe63fb92007-12-11 08:59:05 +0000642 const PointerType *PTy2 = cast<PointerType>(Ty2);
643 return PTy->getAddressSpace() == PTy2->getAddressSpace() &&
644 TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
Chris Lattner5af41972003-10-13 14:55:56 +0000645 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000646 const StructType *STy2 = cast<StructType>(Ty2);
647 if (STy->getNumElements() != STy2->getNumElements()) return false;
Andrew Lenharth38ecbf12006-12-08 18:06:16 +0000648 if (STy->isPacked() != STy2->isPacked()) return false;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000649 for (unsigned i = 0, e = STy2->getNumElements(); i != e; ++i)
650 if (!TypesEqual(STy->getElementType(i), STy2->getElementType(i), EqTypes))
Chris Lattner5af41972003-10-13 14:55:56 +0000651 return false;
652 return true;
653 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
654 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
655 return ATy->getNumElements() == ATy2->getNumElements() &&
656 TypesEqual(ATy->getElementType(), ATy2->getElementType(), EqTypes);
Reid Spencer9d6565a2007-02-15 02:26:10 +0000657 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
658 const VectorType *PTy2 = cast<VectorType>(Ty2);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000659 return PTy->getNumElements() == PTy2->getNumElements() &&
660 TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
Chris Lattnerdd4b4212003-09-02 16:35:17 +0000661 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner5af41972003-10-13 14:55:56 +0000662 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
663 if (FTy->isVarArg() != FTy2->isVarArg() ||
Chris Lattnerd5d89962004-02-09 04:14:01 +0000664 FTy->getNumParams() != FTy2->getNumParams() ||
Chris Lattner5af41972003-10-13 14:55:56 +0000665 !TypesEqual(FTy->getReturnType(), FTy2->getReturnType(), EqTypes))
Chris Lattner56c5acb2001-10-13 07:01:33 +0000666 return false;
Reid Spencer877e7ce2007-01-08 19:41:01 +0000667 for (unsigned i = 0, e = FTy2->getNumParams(); i != e; ++i) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000668 if (!TypesEqual(FTy->getParamType(i), FTy2->getParamType(i), EqTypes))
Chris Lattner5af41972003-10-13 14:55:56 +0000669 return false;
Reid Spencer877e7ce2007-01-08 19:41:01 +0000670 }
Chris Lattner5af41972003-10-13 14:55:56 +0000671 return true;
672 } else {
673 assert(0 && "Unknown derived type!");
674 return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +0000675 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000676}
677
678static bool TypesEqual(const Type *Ty, const Type *Ty2) {
Chris Lattner47697a12003-05-22 21:21:43 +0000679 std::map<const Type *, const Type *> EqTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000680 return TypesEqual(Ty, Ty2, EqTypes);
681}
682
Chris Lattner8511c352004-11-16 20:30:53 +0000683// AbstractTypeHasCycleThrough - Return true there is a path from CurTy to
684// TargetTy in the type graph. We know that Ty is an abstract type, so if we
685// ever reach a non-abstract type, we know that we don't need to search the
686// subgraph.
687static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
Chris Lattnerc3b58492004-02-09 20:23:44 +0000688 std::set<const Type*> &VisitedTypes) {
689 if (TargetTy == CurTy) return true;
690 if (!CurTy->isAbstract()) return false;
691
Chris Lattner8511c352004-11-16 20:30:53 +0000692 if (!VisitedTypes.insert(CurTy).second)
693 return false; // Already been here.
Chris Lattnerc3b58492004-02-09 20:23:44 +0000694
Misha Brukmanfd939082005-04-21 23:48:37 +0000695 for (Type::subtype_iterator I = CurTy->subtype_begin(),
Reid Spencer6e885d02004-07-04 12:14:17 +0000696 E = CurTy->subtype_end(); I != E; ++I)
Chris Lattner8511c352004-11-16 20:30:53 +0000697 if (AbstractTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
Chris Lattnerc3b58492004-02-09 20:23:44 +0000698 return true;
699 return false;
700}
701
Chris Lattner8511c352004-11-16 20:30:53 +0000702static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
703 std::set<const Type*> &VisitedTypes) {
704 if (TargetTy == CurTy) return true;
705
706 if (!VisitedTypes.insert(CurTy).second)
707 return false; // Already been here.
708
Misha Brukmanfd939082005-04-21 23:48:37 +0000709 for (Type::subtype_iterator I = CurTy->subtype_begin(),
Chris Lattner8511c352004-11-16 20:30:53 +0000710 E = CurTy->subtype_end(); I != E; ++I)
711 if (ConcreteTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
712 return true;
713 return false;
714}
Chris Lattnerc3b58492004-02-09 20:23:44 +0000715
Chris Lattner27295402004-02-09 16:35:14 +0000716/// TypeHasCycleThroughItself - Return true if the specified type has a cycle
717/// back to itself.
718static bool TypeHasCycleThroughItself(const Type *Ty) {
719 std::set<const Type*> VisitedTypes;
Chris Lattner8511c352004-11-16 20:30:53 +0000720
721 if (Ty->isAbstract()) { // Optimized case for abstract types.
Misha Brukmanfd939082005-04-21 23:48:37 +0000722 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
Chris Lattner8511c352004-11-16 20:30:53 +0000723 I != E; ++I)
724 if (AbstractTypeHasCycleThrough(Ty, *I, VisitedTypes))
725 return true;
726 } else {
727 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
728 I != E; ++I)
729 if (ConcreteTypeHasCycleThrough(Ty, *I, VisitedTypes))
730 return true;
731 }
Chris Lattner27295402004-02-09 16:35:14 +0000732 return false;
733}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000734
Chris Lattner3787c952005-11-16 06:09:47 +0000735/// getSubElementHash - Generate a hash value for all of the SubType's of this
736/// type. The hash value is guaranteed to be zero if any of the subtypes are
737/// an opaque type. Otherwise we try to mix them in as well as possible, but do
738/// not look at the subtype's subtype's.
739static unsigned getSubElementHash(const Type *Ty) {
740 unsigned HashVal = 0;
741 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
742 I != E; ++I) {
743 HashVal *= 32;
744 const Type *SubTy = I->get();
745 HashVal += SubTy->getTypeID();
746 switch (SubTy->getTypeID()) {
747 default: break;
748 case Type::OpaqueTyID: return 0; // Opaque -> hash = 0 no matter what.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000749 case Type::IntegerTyID:
750 HashVal ^= (cast<IntegerType>(SubTy)->getBitWidth() << 3);
751 break;
Chris Lattner3787c952005-11-16 06:09:47 +0000752 case Type::FunctionTyID:
753 HashVal ^= cast<FunctionType>(SubTy)->getNumParams()*2 +
754 cast<FunctionType>(SubTy)->isVarArg();
755 break;
756 case Type::ArrayTyID:
757 HashVal ^= cast<ArrayType>(SubTy)->getNumElements();
758 break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000759 case Type::VectorTyID:
760 HashVal ^= cast<VectorType>(SubTy)->getNumElements();
Chris Lattner3787c952005-11-16 06:09:47 +0000761 break;
762 case Type::StructTyID:
763 HashVal ^= cast<StructType>(SubTy)->getNumElements();
764 break;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000765 case Type::PointerTyID:
766 HashVal ^= cast<PointerType>(SubTy)->getAddressSpace();
767 break;
Chris Lattner3787c952005-11-16 06:09:47 +0000768 }
769 }
770 return HashVal ? HashVal : 1; // Do not return zero unless opaque subty.
771}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000772
773//===----------------------------------------------------------------------===//
774// Derived Type Factory Functions
775//===----------------------------------------------------------------------===//
776
Chris Lattnerf2586d12003-11-19 06:14:38 +0000777namespace llvm {
Chris Lattnercfe82272005-11-13 03:14:09 +0000778class TypeMapBase {
779protected:
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000780 /// TypesByHash - Keep track of types by their structure hash value. Note
781 /// that we only keep track of types that have cycles through themselves in
782 /// this map.
Chris Lattner0cdaf942004-02-09 18:32:40 +0000783 ///
784 std::multimap<unsigned, PATypeHolder> TypesByHash;
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000785
Chris Lattnercfe82272005-11-13 03:14:09 +0000786public:
787 void RemoveFromTypesByHash(unsigned Hash, const Type *Ty) {
788 std::multimap<unsigned, PATypeHolder>::iterator I =
Chris Lattner3787c952005-11-16 06:09:47 +0000789 TypesByHash.lower_bound(Hash);
790 for (; I != TypesByHash.end() && I->first == Hash; ++I) {
791 if (I->second == Ty) {
792 TypesByHash.erase(I);
793 return;
794 }
Chris Lattnercfe82272005-11-13 03:14:09 +0000795 }
Chris Lattner3787c952005-11-16 06:09:47 +0000796
797 // This must be do to an opaque type that was resolved. Switch down to hash
798 // code of zero.
799 assert(Hash && "Didn't find type entry!");
800 RemoveFromTypesByHash(0, Ty);
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000801 }
Chris Lattnercfe82272005-11-13 03:14:09 +0000802
803 /// TypeBecameConcrete - When Ty gets a notification that TheType just became
804 /// concrete, drop uses and make Ty non-abstract if we should.
805 void TypeBecameConcrete(DerivedType *Ty, const DerivedType *TheType) {
806 // If the element just became concrete, remove 'ty' from the abstract
807 // type user list for the type. Do this for as many times as Ty uses
808 // OldType.
809 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
810 I != E; ++I)
811 if (I->get() == TheType)
812 TheType->removeAbstractTypeUser(Ty);
813
814 // If the type is currently thought to be abstract, rescan all of our
815 // subtypes to see if the type has just become concrete! Note that this
816 // may send out notifications to AbstractTypeUsers that types become
817 // concrete.
818 if (Ty->isAbstract())
819 Ty->PromoteAbstractToConcrete();
820 }
821};
822}
823
824
825// TypeMap - Make sure that only one instance of a particular type may be
826// created on any given run of the compiler... note that this involves updating
827// our map if an abstract type gets refined somehow.
828//
829namespace llvm {
830template<class ValType, class TypeClass>
831class TypeMap : public TypeMapBase {
832 std::map<ValType, PATypeHolder> Map;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000833public:
Chris Lattner27295402004-02-09 16:35:14 +0000834 typedef typename std::map<ValType, PATypeHolder>::iterator iterator;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000835 ~TypeMap() { print("ON EXIT"); }
836
837 inline TypeClass *get(const ValType &V) {
Chris Lattner169726b2003-09-05 02:21:39 +0000838 iterator I = Map.find(V);
Chris Lattner3b41e0e2003-12-31 03:19:37 +0000839 return I != Map.end() ? cast<TypeClass>((Type*)I->second.get()) : 0;
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000840 }
841
Chris Lattner0cdaf942004-02-09 18:32:40 +0000842 inline void add(const ValType &V, TypeClass *Ty) {
843 Map.insert(std::make_pair(V, Ty));
844
845 // If this type has a cycle, remember it.
846 TypesByHash.insert(std::make_pair(ValType::hashTypeStructure(Ty), Ty));
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000847 print("add");
848 }
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000849
Chris Lattnerf00c6ad2007-02-20 05:29:47 +0000850 /// RefineAbstractType - This method is called after we have merged a type
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000851 /// with another one. We must now either merge the type away with
Chris Lattner4b1be102003-12-31 02:50:02 +0000852 /// some other type or reinstall it in the map with it's new configuration.
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000853 void RefineAbstractType(TypeClass *Ty, const DerivedType *OldType,
Chris Lattner27295402004-02-09 16:35:14 +0000854 const Type *NewType) {
Chris Lattner27295402004-02-09 16:35:14 +0000855#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000856 DOUT << "RefineAbstractType(" << (void*)OldType << "[" << *OldType
857 << "], " << (void*)NewType << " [" << *NewType << "])\n";
Chris Lattner27295402004-02-09 16:35:14 +0000858#endif
Chris Lattner66cafb32005-11-13 01:27:50 +0000859
860 // Otherwise, we are changing one subelement type into another. Clearly the
861 // OldType must have been abstract, making us abstract.
862 assert(Ty->isAbstract() && "Refining a non-abstract type!");
Chris Lattnerbcf6bc22005-11-13 01:58:06 +0000863 assert(OldType != NewType);
Chris Lattnercfe82272005-11-13 03:14:09 +0000864
Chris Lattner3b41e0e2003-12-31 03:19:37 +0000865 // Make a temporary type holder for the type so that it doesn't disappear on
866 // us when we erase the entry from the map.
Chris Lattner27295402004-02-09 16:35:14 +0000867 PATypeHolder TyHolder = Ty;
868
Chris Lattner7685ac82003-10-03 18:46:24 +0000869 // The old record is now out-of-date, because one of the children has been
870 // updated. Remove the obsolete entry from the map.
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000871 unsigned NumErased = Map.erase(ValType::get(Ty));
872 assert(NumErased && "Element not found!");
Chris Lattner7685ac82003-10-03 18:46:24 +0000873
Chris Lattner0cdaf942004-02-09 18:32:40 +0000874 // Remember the structural hash for the type before we start hacking on it,
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000875 // in case we need it later.
Chris Lattner0cdaf942004-02-09 18:32:40 +0000876 unsigned OldTypeHash = ValType::hashTypeStructure(Ty);
877
878 // Find the type element we are refining... and change it now!
Reid Spencer5a1ebb32007-04-06 02:02:20 +0000879 for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i)
Chris Lattner66cafb32005-11-13 01:27:50 +0000880 if (Ty->ContainedTys[i] == OldType)
881 Ty->ContainedTys[i] = NewType;
Chris Lattner75485902005-11-12 08:39:48 +0000882 unsigned NewTypeHash = ValType::hashTypeStructure(Ty);
883
Chris Lattner542d9912003-11-19 19:10:23 +0000884 // If there are no cycles going through this node, we can do a simple,
885 // efficient lookup in the map, instead of an inefficient nasty linear
886 // lookup.
Chris Lattner66cafb32005-11-13 01:27:50 +0000887 if (!TypeHasCycleThroughItself(Ty)) {
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000888 typename std::map<ValType, PATypeHolder>::iterator I;
889 bool Inserted;
890
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000891 tie(I, Inserted) = Map.insert(std::make_pair(ValType::get(Ty), Ty));
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000892 if (!Inserted) {
Chris Lattner8df956c2003-09-05 02:39:52 +0000893 // Refined to a different type altogether?
Chris Lattner66cafb32005-11-13 01:27:50 +0000894 RemoveFromTypesByHash(OldTypeHash, Ty);
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000895
896 // We already have this type in the table. Get rid of the newly refined
897 // type.
898 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
Chris Lattneraf6f93c2003-10-03 18:57:54 +0000899 Ty->refineAbstractTypeTo(NewTy);
Chris Lattner8df956c2003-09-05 02:39:52 +0000900 return;
Chris Lattner169726b2003-09-05 02:21:39 +0000901 }
Chris Lattner542d9912003-11-19 19:10:23 +0000902 } else {
903 // Now we check to see if there is an existing entry in the table which is
904 // structurally identical to the newly refined type. If so, this type
905 // gets refined to the pre-existing type.
906 //
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000907 std::multimap<unsigned, PATypeHolder>::iterator I, E, Entry;
Chris Lattner3787c952005-11-16 06:09:47 +0000908 tie(I, E) = TypesByHash.equal_range(NewTypeHash);
Chris Lattner0cdaf942004-02-09 18:32:40 +0000909 Entry = E;
910 for (; I != E; ++I) {
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000911 if (I->second == Ty) {
912 // Remember the position of the old type if we see it in our scan.
913 Entry = I;
914 } else {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000915 if (TypesEqual(Ty, I->second)) {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000916 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
Misha Brukmanfd939082005-04-21 23:48:37 +0000917
Chris Lattner3787c952005-11-16 06:09:47 +0000918 // Remove the old entry form TypesByHash. If the hash values differ
919 // now, remove it from the old place. Otherwise, continue scanning
920 // withing this hashcode to reduce work.
921 if (NewTypeHash != OldTypeHash) {
922 RemoveFromTypesByHash(OldTypeHash, Ty);
923 } else {
924 if (Entry == E) {
925 // Find the location of Ty in the TypesByHash structure if we
926 // haven't seen it already.
927 while (I->second != Ty) {
928 ++I;
929 assert(I != E && "Structure doesn't contain type??");
930 }
931 Entry = I;
Chris Lattner0cdaf942004-02-09 18:32:40 +0000932 }
Chris Lattner3787c952005-11-16 06:09:47 +0000933 TypesByHash.erase(Entry);
Chris Lattner0cdaf942004-02-09 18:32:40 +0000934 }
Chris Lattner0cdaf942004-02-09 18:32:40 +0000935 Ty->refineAbstractTypeTo(NewTy);
936 return;
937 }
Chris Lattner542d9912003-11-19 19:10:23 +0000938 }
Chris Lattner27295402004-02-09 16:35:14 +0000939 }
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000940
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000941 // If there is no existing type of the same structure, we reinsert an
942 // updated record into the map.
943 Map.insert(std::make_pair(ValType::get(Ty), Ty));
Chris Lattner542d9912003-11-19 19:10:23 +0000944 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000945
Chris Lattnerbcb31d62004-11-16 20:39:04 +0000946 // If the hash codes differ, update TypesByHash
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000947 if (NewTypeHash != OldTypeHash) {
Chris Lattner0cdaf942004-02-09 18:32:40 +0000948 RemoveFromTypesByHash(OldTypeHash, Ty);
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000949 TypesByHash.insert(std::make_pair(NewTypeHash, Ty));
Chris Lattner0cdaf942004-02-09 18:32:40 +0000950 }
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000951
Chris Lattner8df956c2003-09-05 02:39:52 +0000952 // If the type is currently thought to be abstract, rescan all of our
Chris Lattnerd4f328e2005-11-12 08:22:41 +0000953 // subtypes to see if the type has just become concrete! Note that this
954 // may send out notifications to AbstractTypeUsers that types become
955 // concrete.
Chris Lattnera6b10b62004-10-07 19:20:48 +0000956 if (Ty->isAbstract())
957 Ty->PromoteAbstractToConcrete();
Chris Lattner266caa22003-09-05 02:30:47 +0000958 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000959
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000960 void print(const char *Arg) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000961#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +0000962 DOUT << "TypeMap<>::" << Arg << " table contents:\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000963 unsigned i = 0;
Chris Lattner27295402004-02-09 16:35:14 +0000964 for (typename std::map<ValType, PATypeHolder>::const_iterator I
965 = Map.begin(), E = Map.end(); I != E; ++I)
Bill Wendling2e3def12006-11-17 08:03:48 +0000966 DOUT << " " << (++i) << ". " << (void*)I->second.get() << " "
967 << *I->second.get() << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000968#endif
969 }
Chris Lattneraa06d2c2002-04-04 19:26:02 +0000970
971 void dump() const { print("dump output"); }
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000972};
Chris Lattnerf2586d12003-11-19 06:14:38 +0000973}
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000974
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000975
976//===----------------------------------------------------------------------===//
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000977// Function Type Factory and Value Class...
Chris Lattnerc038a2f2001-09-07 16:56:42 +0000978//
979
Reid Spencera54b7cb2007-01-12 07:05:14 +0000980//===----------------------------------------------------------------------===//
981// Integer Type Factory...
982//
983namespace llvm {
984class IntegerValType {
Reid Spencer062f0362007-01-13 00:12:29 +0000985 uint32_t bits;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000986public:
987 IntegerValType(uint16_t numbits) : bits(numbits) {}
988
989 static IntegerValType get(const IntegerType *Ty) {
990 return IntegerValType(Ty->getBitWidth());
991 }
992
993 static unsigned hashTypeStructure(const IntegerType *Ty) {
994 return (unsigned)Ty->getBitWidth();
995 }
996
997 inline bool operator<(const IntegerValType &IVT) const {
998 return bits < IVT.bits;
999 }
1000};
1001}
1002
1003static ManagedStatic<TypeMap<IntegerValType, IntegerType> > IntegerTypes;
1004
1005const IntegerType *IntegerType::get(unsigned NumBits) {
1006 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
1007 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
1008
1009 // Check for the built-in integer types
1010 switch (NumBits) {
1011 case 1: return cast<IntegerType>(Type::Int1Ty);
1012 case 8: return cast<IntegerType>(Type::Int8Ty);
1013 case 16: return cast<IntegerType>(Type::Int16Ty);
1014 case 32: return cast<IntegerType>(Type::Int32Ty);
1015 case 64: return cast<IntegerType>(Type::Int64Ty);
1016 default:
1017 break;
1018 }
1019
1020 IntegerValType IVT(NumBits);
1021 IntegerType *ITy = IntegerTypes->get(IVT);
1022 if (ITy) return ITy; // Found a match, return it!
1023
1024 // Value not found. Derive a new type!
1025 ITy = new IntegerType(NumBits);
1026 IntegerTypes->add(IVT, ITy);
1027
1028#ifdef DEBUG_MERGE_TYPES
1029 DOUT << "Derived new type: " << *ITy << "\n";
1030#endif
1031 return ITy;
1032}
1033
Reid Spencer7808dcb2007-01-18 02:59:54 +00001034bool IntegerType::isPowerOf2ByteWidth() const {
1035 unsigned BitWidth = getBitWidth();
Reid Spencerb5f378e2007-01-18 18:14:49 +00001036 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
Reid Spencer7808dcb2007-01-18 02:59:54 +00001037}
1038
Reid Spencer9e574142007-03-01 04:02:06 +00001039APInt IntegerType::getMask() const {
1040 return APInt::getAllOnesValue(getBitWidth());
1041}
1042
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001043// FunctionValType - Define a class to hold the key that goes into the TypeMap
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001044//
Chris Lattnerf2586d12003-11-19 06:14:38 +00001045namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +00001046class FunctionValType {
1047 const Type *RetTy;
1048 std::vector<const Type*> ArgTypes;
Chris Lattner56c5acb2001-10-13 07:01:33 +00001049 bool isVarArg;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001050public:
Chris Lattner47697a12003-05-22 21:21:43 +00001051 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
Duncan Sandsdc024672007-11-27 13:23:08 +00001052 bool isVA) : RetTy(ret), isVarArg(isVA) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001053 for (unsigned i = 0; i < args.size(); ++i)
Chris Lattner7685ac82003-10-03 18:46:24 +00001054 ArgTypes.push_back(args[i]);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001055 }
1056
Chris Lattner169726b2003-09-05 02:21:39 +00001057 static FunctionValType get(const FunctionType *FT);
1058
Chris Lattner27295402004-02-09 16:35:14 +00001059 static unsigned hashTypeStructure(const FunctionType *FT) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001060 unsigned Result = FT->getNumParams()*2 + FT->isVarArg();
Reid Spencer89b1f462007-04-09 06:07:52 +00001061 return Result;
Chris Lattner27295402004-02-09 16:35:14 +00001062 }
1063
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001064 inline bool operator<(const FunctionValType &MTV) const {
Chris Lattner7685ac82003-10-03 18:46:24 +00001065 if (RetTy < MTV.RetTy) return true;
1066 if (RetTy > MTV.RetTy) return false;
Reid Spencer9c2e86a2006-12-31 05:25:34 +00001067 if (isVarArg < MTV.isVarArg) return true;
1068 if (isVarArg > MTV.isVarArg) return false;
Chris Lattner56c5acb2001-10-13 07:01:33 +00001069 if (ArgTypes < MTV.ArgTypes) return true;
Reid Spencer89b1f462007-04-09 06:07:52 +00001070 if (ArgTypes > MTV.ArgTypes) return false;
Reid Spencer89b1f462007-04-09 06:07:52 +00001071 return false;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001072 }
1073};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001074}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001075
1076// Define the actual map itself now...
Chris Lattnerde65fb32006-09-28 23:38:07 +00001077static ManagedStatic<TypeMap<FunctionValType, FunctionType> > FunctionTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001078
Chris Lattner169726b2003-09-05 02:21:39 +00001079FunctionValType FunctionValType::get(const FunctionType *FT) {
1080 // Build up a FunctionValType
1081 std::vector<const Type *> ParamTypes;
Chris Lattnerd5d89962004-02-09 04:14:01 +00001082 ParamTypes.reserve(FT->getNumParams());
1083 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
Chris Lattner169726b2003-09-05 02:21:39 +00001084 ParamTypes.push_back(FT->getParamType(i));
Duncan Sandsdc024672007-11-27 13:23:08 +00001085 return FunctionValType(FT->getReturnType(), ParamTypes, FT->isVarArg());
Chris Lattner169726b2003-09-05 02:21:39 +00001086}
1087
1088
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001089// FunctionType::get - The factory function for the FunctionType class...
Misha Brukmanfd939082005-04-21 23:48:37 +00001090FunctionType *FunctionType::get(const Type *ReturnType,
Chris Lattner47697a12003-05-22 21:21:43 +00001091 const std::vector<const Type*> &Params,
Duncan Sandsdc024672007-11-27 13:23:08 +00001092 bool isVarArg) {
1093 FunctionValType VT(ReturnType, Params, isVarArg);
Reid Spencer4f859aa2007-04-22 05:46:44 +00001094 FunctionType *FT = FunctionTypes->get(VT);
1095 if (FT) {
1096 return FT;
Reid Spencer3aad26e2007-04-09 17:20:18 +00001097 }
1098
Reid Spencer4f859aa2007-04-22 05:46:44 +00001099 FT = (FunctionType*) new char[sizeof(FunctionType) +
Reid Spencer5a1ebb32007-04-06 02:02:20 +00001100 sizeof(PATypeHandle)*(Params.size()+1)];
Duncan Sandsdc024672007-11-27 13:23:08 +00001101 new (FT) FunctionType(ReturnType, Params, isVarArg);
Reid Spencer4f859aa2007-04-22 05:46:44 +00001102 FunctionTypes->add(VT, FT);
Chris Lattnere5a57ee2001-07-25 22:47:55 +00001103
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001104#ifdef DEBUG_MERGE_TYPES
Reid Spencer4f859aa2007-04-22 05:46:44 +00001105 DOUT << "Derived new type: " << FT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001106#endif
Reid Spencer4f859aa2007-04-22 05:46:44 +00001107 return FT;
Reid Spencer9c2e86a2006-12-31 05:25:34 +00001108}
1109
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001110//===----------------------------------------------------------------------===//
1111// Array Type Factory...
1112//
Chris Lattnerf2586d12003-11-19 06:14:38 +00001113namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +00001114class ArrayValType {
1115 const Type *ValTy;
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001116 uint64_t Size;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001117public:
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001118 ArrayValType(const Type *val, uint64_t sz) : ValTy(val), Size(sz) {}
Chris Lattner00950542001-06-06 20:29:01 +00001119
Chris Lattner7685ac82003-10-03 18:46:24 +00001120 static ArrayValType get(const ArrayType *AT) {
1121 return ArrayValType(AT->getElementType(), AT->getNumElements());
1122 }
Chris Lattner169726b2003-09-05 02:21:39 +00001123
Chris Lattner27295402004-02-09 16:35:14 +00001124 static unsigned hashTypeStructure(const ArrayType *AT) {
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001125 return (unsigned)AT->getNumElements();
Chris Lattner27295402004-02-09 16:35:14 +00001126 }
1127
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001128 inline bool operator<(const ArrayValType &MTV) const {
1129 if (Size < MTV.Size) return true;
Chris Lattner7685ac82003-10-03 18:46:24 +00001130 return Size == MTV.Size && ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001131 }
1132};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001133}
Chris Lattnerde65fb32006-09-28 23:38:07 +00001134static ManagedStatic<TypeMap<ArrayValType, ArrayType> > ArrayTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001135
Chris Lattner169726b2003-09-05 02:21:39 +00001136
Chris Lattner7d7a0ed2005-01-08 20:19:51 +00001137ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001138 assert(ElementType && "Can't get array of null types!");
1139
Chris Lattner7685ac82003-10-03 18:46:24 +00001140 ArrayValType AVT(ElementType, NumElements);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001141 ArrayType *AT = ArrayTypes->get(AVT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001142 if (AT) return AT; // Found a match, return it!
1143
Chris Lattner00950542001-06-06 20:29:01 +00001144 // Value not found. Derive a new type!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001145 ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
Chris Lattner00950542001-06-06 20:29:01 +00001146
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001147#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001148 DOUT << "Derived new type: " << *AT << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001149#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001150 return AT;
Chris Lattner00950542001-06-06 20:29:01 +00001151}
1152
Brian Gaeke715c90b2004-08-20 06:00:58 +00001153
1154//===----------------------------------------------------------------------===//
Reid Spencera85210a2007-02-15 03:11:50 +00001155// Vector Type Factory...
Brian Gaeke715c90b2004-08-20 06:00:58 +00001156//
1157namespace llvm {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001158class VectorValType {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001159 const Type *ValTy;
1160 unsigned Size;
1161public:
Reid Spencer9d6565a2007-02-15 02:26:10 +00001162 VectorValType(const Type *val, int sz) : ValTy(val), Size(sz) {}
Brian Gaeke715c90b2004-08-20 06:00:58 +00001163
Reid Spencer9d6565a2007-02-15 02:26:10 +00001164 static VectorValType get(const VectorType *PT) {
1165 return VectorValType(PT->getElementType(), PT->getNumElements());
Brian Gaeke715c90b2004-08-20 06:00:58 +00001166 }
1167
Reid Spencer9d6565a2007-02-15 02:26:10 +00001168 static unsigned hashTypeStructure(const VectorType *PT) {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001169 return PT->getNumElements();
1170 }
1171
Reid Spencer9d6565a2007-02-15 02:26:10 +00001172 inline bool operator<(const VectorValType &MTV) const {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001173 if (Size < MTV.Size) return true;
1174 return Size == MTV.Size && ValTy < MTV.ValTy;
1175 }
1176};
1177}
Reid Spencer9d6565a2007-02-15 02:26:10 +00001178static ManagedStatic<TypeMap<VectorValType, VectorType> > VectorTypes;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001179
1180
Reid Spencer9d6565a2007-02-15 02:26:10 +00001181VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) {
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001182 assert(ElementType && "Can't get vector of null types!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001183
Reid Spencer9d6565a2007-02-15 02:26:10 +00001184 VectorValType PVT(ElementType, NumElements);
1185 VectorType *PT = VectorTypes->get(PVT);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001186 if (PT) return PT; // Found a match, return it!
1187
1188 // Value not found. Derive a new type!
Reid Spencer9d6565a2007-02-15 02:26:10 +00001189 VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements));
Brian Gaeke715c90b2004-08-20 06:00:58 +00001190
1191#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001192 DOUT << "Derived new type: " << *PT << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +00001193#endif
1194 return PT;
1195}
1196
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001197//===----------------------------------------------------------------------===//
1198// Struct Type Factory...
1199//
Chris Lattner00950542001-06-06 20:29:01 +00001200
Chris Lattnerf2586d12003-11-19 06:14:38 +00001201namespace llvm {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001202// StructValType - Define a class to hold the key that goes into the TypeMap
1203//
Chris Lattner7685ac82003-10-03 18:46:24 +00001204class StructValType {
1205 std::vector<const Type*> ElTypes;
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001206 bool packed;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001207public:
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001208 StructValType(const std::vector<const Type*> &args, bool isPacked)
1209 : ElTypes(args), packed(isPacked) {}
Chris Lattner00950542001-06-06 20:29:01 +00001210
Chris Lattner7685ac82003-10-03 18:46:24 +00001211 static StructValType get(const StructType *ST) {
1212 std::vector<const Type *> ElTypes;
Chris Lattnerd21cd802004-02-09 04:37:31 +00001213 ElTypes.reserve(ST->getNumElements());
1214 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i)
1215 ElTypes.push_back(ST->getElementType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +00001216
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001217 return StructValType(ElTypes, ST->isPacked());
Chris Lattner7685ac82003-10-03 18:46:24 +00001218 }
Chris Lattner169726b2003-09-05 02:21:39 +00001219
Chris Lattner27295402004-02-09 16:35:14 +00001220 static unsigned hashTypeStructure(const StructType *ST) {
Chris Lattner0cdaf942004-02-09 18:32:40 +00001221 return ST->getNumElements();
Chris Lattner27295402004-02-09 16:35:14 +00001222 }
1223
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001224 inline bool operator<(const StructValType &STV) const {
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001225 if (ElTypes < STV.ElTypes) return true;
1226 else if (ElTypes > STV.ElTypes) return false;
1227 else return (int)packed < (int)STV.packed;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001228 }
1229};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001230}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001231
Chris Lattnerde65fb32006-09-28 23:38:07 +00001232static ManagedStatic<TypeMap<StructValType, StructType> > StructTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001233
Andrew Lenharth38ecbf12006-12-08 18:06:16 +00001234StructType *StructType::get(const std::vector<const Type*> &ETypes,
1235 bool isPacked) {
1236 StructValType STV(ETypes, isPacked);
Chris Lattnerde65fb32006-09-28 23:38:07 +00001237 StructType *ST = StructTypes->get(STV);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001238 if (ST) return ST;
1239
1240 // Value not found. Derive a new type!
Reid Spencer5a1ebb32007-04-06 02:02:20 +00001241 ST = (StructType*) new char[sizeof(StructType) +
1242 sizeof(PATypeHandle) * ETypes.size()];
1243 new (ST) StructType(ETypes, isPacked);
1244 StructTypes->add(STV, ST);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001245
1246#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001247 DOUT << "Derived new type: " << *ST << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001248#endif
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001249 return ST;
1250}
1251
Chris Lattner169726b2003-09-05 02:21:39 +00001252
1253
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001254//===----------------------------------------------------------------------===//
1255// Pointer Type Factory...
1256//
1257
1258// PointerValType - Define a class to hold the key that goes into the TypeMap
1259//
Chris Lattnerf2586d12003-11-19 06:14:38 +00001260namespace llvm {
Chris Lattner7685ac82003-10-03 18:46:24 +00001261class PointerValType {
1262 const Type *ValTy;
Christopher Lambfe63fb92007-12-11 08:59:05 +00001263 unsigned AddressSpace;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001264public:
Christopher Lambfe63fb92007-12-11 08:59:05 +00001265 PointerValType(const Type *val, unsigned as) : ValTy(val), AddressSpace(as) {}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001266
Chris Lattner7685ac82003-10-03 18:46:24 +00001267 static PointerValType get(const PointerType *PT) {
Christopher Lambfe63fb92007-12-11 08:59:05 +00001268 return PointerValType(PT->getElementType(), PT->getAddressSpace());
Chris Lattner7685ac82003-10-03 18:46:24 +00001269 }
Chris Lattner169726b2003-09-05 02:21:39 +00001270
Chris Lattner27295402004-02-09 16:35:14 +00001271 static unsigned hashTypeStructure(const PointerType *PT) {
Chris Lattner3787c952005-11-16 06:09:47 +00001272 return getSubElementHash(PT);
Chris Lattner27295402004-02-09 16:35:14 +00001273 }
1274
Chris Lattner7685ac82003-10-03 18:46:24 +00001275 bool operator<(const PointerValType &MTV) const {
Christopher Lambfe63fb92007-12-11 08:59:05 +00001276 if (AddressSpace < MTV.AddressSpace) return true;
1277 return AddressSpace == MTV.AddressSpace && ValTy < MTV.ValTy;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001278 }
1279};
Chris Lattnerf2586d12003-11-19 06:14:38 +00001280}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001281
Chris Lattnerde65fb32006-09-28 23:38:07 +00001282static ManagedStatic<TypeMap<PointerValType, PointerType> > PointerTypes;
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001283
Christopher Lambfe63fb92007-12-11 08:59:05 +00001284PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001285 assert(ValueType && "Can't get a pointer to <null> type!");
Chris Lattnera5d824e2006-04-21 15:33:35 +00001286 assert(ValueType != Type::VoidTy &&
1287 "Pointer to void is not valid, use sbyte* instead!");
Chris Lattnerf5310402006-10-15 23:21:12 +00001288 assert(ValueType != Type::LabelTy && "Pointer to label is not valid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001289 PointerValType PVT(ValueType, AddressSpace);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001290
Chris Lattnerde65fb32006-09-28 23:38:07 +00001291 PointerType *PT = PointerTypes->get(PVT);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001292 if (PT) return PT;
1293
1294 // Value not found. Derive a new type!
Christopher Lambfe63fb92007-12-11 08:59:05 +00001295 PointerTypes->add(PVT, PT = new PointerType(ValueType, AddressSpace));
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001296
1297#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001298 DOUT << "Derived new type: " << *PT << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001299#endif
1300 return PT;
1301}
1302
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001303//===----------------------------------------------------------------------===//
1304// Derived Type Refinement Functions
1305//===----------------------------------------------------------------------===//
1306
1307// removeAbstractTypeUser - Notify an abstract type that a user of the class
1308// no longer has a handle to the type. This function is called primarily by
1309// the PATypeHandle class. When there are no users of the abstract type, it
Misha Brukman6b634522003-10-10 17:54:14 +00001310// is annihilated, because there is no way to get a reference to it ever again.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001311//
Chris Lattnercfe82272005-11-13 03:14:09 +00001312void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001313 // Search from back to front because we will notify users from back to
1314 // front. Also, it is likely that there will be a stack like behavior to
1315 // users that register and unregister users.
1316 //
Chris Lattner3f59b7e2002-04-05 19:44:07 +00001317 unsigned i;
1318 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
1319 assert(i != 0 && "AbstractTypeUser not in user list!");
1320
1321 --i; // Convert to be in range 0 <= i < size()
1322 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
1323
1324 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
Misha Brukmanfd939082005-04-21 23:48:37 +00001325
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001326#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001327 DOUT << " remAbstractTypeUser[" << (void*)this << ", "
1328 << *this << "][" << i << "] User = " << U << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001329#endif
Misha Brukmanfd939082005-04-21 23:48:37 +00001330
Chris Lattnerac891642004-02-17 03:03:47 +00001331 if (AbstractTypeUsers.empty() && getRefCount() == 0 && isAbstract()) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001332#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001333 DOUT << "DELETEing unused abstract type: <" << *this
1334 << ">[" << (void*)this << "]" << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001335#endif
Reid Spencer5a1ebb32007-04-06 02:02:20 +00001336 this->destroy();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001337 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001338}
1339
Reid Spencerfd20c0a2006-05-29 02:34:34 +00001340// refineAbstractTypeTo - This function is used when it is discovered that
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001341// the 'this' abstract type is actually equivalent to the NewType specified.
1342// This causes all users of 'this' to switch to reference the more concrete type
1343// NewType and for 'this' to be deleted.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001344//
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001345void DerivedType::refineAbstractTypeTo(const Type *NewType) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001346 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
1347 assert(this != NewType && "Can't refine to myself!");
Chris Lattner7685ac82003-10-03 18:46:24 +00001348 assert(ForwardType == 0 && "This type has already been refined!");
1349
Chris Lattnerdd4b4212003-09-02 16:35:17 +00001350 // The descriptions may be out of date. Conservatively clear them all!
Chris Lattnerde65fb32006-09-28 23:38:07 +00001351 AbstractTypeDescriptions->clear();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001352
1353#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001354 DOUT << "REFINING abstract type [" << (void*)this << " "
1355 << *this << "] to [" << (void*)NewType << " "
1356 << *NewType << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001357#endif
1358
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001359 // Make sure to put the type to be refined to into a holder so that if IT gets
1360 // refined, that we will not continue using a dead reference...
1361 //
Chris Lattneraa06d2c2002-04-04 19:26:02 +00001362 PATypeHolder NewTy(NewType);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001363
Chris Lattner7685ac82003-10-03 18:46:24 +00001364 // Any PATypeHolders referring to this type will now automatically forward to
1365 // the type we are resolved to.
Chris Lattner1c5164e2003-10-02 23:35:57 +00001366 ForwardType = NewType;
1367 if (NewType->isAbstract())
1368 cast<DerivedType>(NewType)->addRef();
1369
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001370 // Add a self use of the current type so that we don't delete ourself until
Chris Lattner8ef852f2003-10-03 04:48:21 +00001371 // after the function exits.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001372 //
Chris Lattner8ef852f2003-10-03 04:48:21 +00001373 PATypeHolder CurrentTy(this);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001374
Chris Lattner169726b2003-09-05 02:21:39 +00001375 // To make the situation simpler, we ask the subclass to remove this type from
1376 // the type map, and to replace any type uses with uses of non-abstract types.
1377 // This dramatically limits the amount of recursive type trouble we can find
1378 // ourselves in.
Chris Lattneraf6f93c2003-10-03 18:57:54 +00001379 dropAllTypeUses();
Chris Lattner169726b2003-09-05 02:21:39 +00001380
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001381 // Iterate over all of the uses of this type, invoking callback. Each user
Chris Lattner3f59b7e2002-04-05 19:44:07 +00001382 // should remove itself from our use list automatically. We have to check to
1383 // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types
1384 // will not cause users to drop off of the use list. If we resolve to ourself
1385 // we succeed!
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001386 //
Chris Lattner8ef852f2003-10-03 04:48:21 +00001387 while (!AbstractTypeUsers.empty() && NewTy != this) {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001388 AbstractTypeUser *User = AbstractTypeUsers.back();
1389
Chris Lattner8ef852f2003-10-03 04:48:21 +00001390 unsigned OldSize = AbstractTypeUsers.size();
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001391#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001392 DOUT << " REFINING user " << OldSize-1 << "[" << (void*)User
1393 << "] of abstract type [" << (void*)this << " "
1394 << *this << "] to [" << (void*)NewTy.get() << " "
1395 << *NewTy << "]!\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001396#endif
Chris Lattner8ef852f2003-10-03 04:48:21 +00001397 User->refineAbstractType(this, NewTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001398
Chris Lattner8ef852f2003-10-03 04:48:21 +00001399 assert(AbstractTypeUsers.size() != OldSize &&
1400 "AbsTyUser did not remove self from user list!");
Chris Lattner00950542001-06-06 20:29:01 +00001401 }
1402
Chris Lattner8ef852f2003-10-03 04:48:21 +00001403 // If we were successful removing all users from the type, 'this' will be
1404 // deleted when the last PATypeHolder is destroyed or updated from this type.
1405 // This may occur on exit of this function, as the CurrentTy object is
1406 // destroyed.
Chris Lattner00950542001-06-06 20:29:01 +00001407}
1408
Chris Lattner7685ac82003-10-03 18:46:24 +00001409// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
1410// the current type has transitioned from being abstract to being concrete.
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001411//
Chris Lattner7685ac82003-10-03 18:46:24 +00001412void DerivedType::notifyUsesThatTypeBecameConcrete() {
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001413#ifdef DEBUG_MERGE_TYPES
Bill Wendling2e3def12006-11-17 08:03:48 +00001414 DOUT << "typeIsREFINED type: " << (void*)this << " " << *this << "\n";
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001415#endif
Chris Lattner417081c2002-04-07 06:14:56 +00001416
Chris Lattner7685ac82003-10-03 18:46:24 +00001417 unsigned OldSize = AbstractTypeUsers.size();
1418 while (!AbstractTypeUsers.empty()) {
1419 AbstractTypeUser *ATU = AbstractTypeUsers.back();
1420 ATU->typeBecameConcrete(this);
Chris Lattner417081c2002-04-07 06:14:56 +00001421
Chris Lattner7685ac82003-10-03 18:46:24 +00001422 assert(AbstractTypeUsers.size() < OldSize-- &&
1423 "AbstractTypeUser did not remove itself from the use list!");
Chris Lattner00950542001-06-06 20:29:01 +00001424 }
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001425}
Misha Brukmanfd939082005-04-21 23:48:37 +00001426
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001427// refineAbstractType - Called when a contained type is found to be more
1428// concrete - this could potentially change us from an abstract type to a
1429// concrete type.
1430//
Chris Lattner6bfd6a52002-03-29 03:44:36 +00001431void FunctionType::refineAbstractType(const DerivedType *OldType,
1432 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001433 FunctionTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001434}
1435
1436void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001437 FunctionTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001438}
1439
1440
1441// refineAbstractType - Called when a contained type is found to be more
1442// concrete - this could potentially change us from an abstract type to a
1443// concrete type.
1444//
1445void ArrayType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001446 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001447 ArrayTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001448}
1449
1450void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001451 ArrayTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001452}
1453
Brian Gaeke715c90b2004-08-20 06:00:58 +00001454// refineAbstractType - Called when a contained type is found to be more
1455// concrete - this could potentially change us from an abstract type to a
1456// concrete type.
1457//
Reid Spencer9d6565a2007-02-15 02:26:10 +00001458void VectorType::refineAbstractType(const DerivedType *OldType,
Brian Gaeke715c90b2004-08-20 06:00:58 +00001459 const Type *NewType) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001460 VectorTypes->RefineAbstractType(this, OldType, NewType);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001461}
1462
Reid Spencer9d6565a2007-02-15 02:26:10 +00001463void VectorType::typeBecameConcrete(const DerivedType *AbsTy) {
1464 VectorTypes->TypeBecameConcrete(this, AbsTy);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001465}
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001466
1467// refineAbstractType - Called when a contained type is found to be more
1468// concrete - this could potentially change us from an abstract type to a
1469// concrete type.
1470//
1471void StructType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001472 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001473 StructTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001474}
1475
1476void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001477 StructTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattnerc038a2f2001-09-07 16:56:42 +00001478}
1479
1480// refineAbstractType - Called when a contained type is found to be more
1481// concrete - this could potentially change us from an abstract type to a
1482// concrete type.
1483//
1484void PointerType::refineAbstractType(const DerivedType *OldType,
Reid Spencer6e885d02004-07-04 12:14:17 +00001485 const Type *NewType) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001486 PointerTypes->RefineAbstractType(this, OldType, NewType);
Chris Lattner7685ac82003-10-03 18:46:24 +00001487}
1488
1489void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
Chris Lattnerde65fb32006-09-28 23:38:07 +00001490 PointerTypes->TypeBecameConcrete(this, AbsTy);
Chris Lattner00950542001-06-06 20:29:01 +00001491}
Reid Spencer6e885d02004-07-04 12:14:17 +00001492
1493bool SequentialType::indexValid(const Value *V) const {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001494 if (const IntegerType *IT = dyn_cast<IntegerType>(V->getType()))
1495 return IT->getBitWidth() == 32 || IT->getBitWidth() == 64;
1496 return false;
Reid Spencer6e885d02004-07-04 12:14:17 +00001497}
1498
1499namespace llvm {
1500std::ostream &operator<<(std::ostream &OS, const Type *T) {
1501 if (T == 0)
1502 OS << "<null> value!\n";
1503 else
1504 T->print(OS);
1505 return OS;
1506}
1507
1508std::ostream &operator<<(std::ostream &OS, const Type &T) {
1509 T.print(OS);
1510 return OS;
1511}
1512}