1 //===-- Type.cpp - Implement the Type class -------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Type class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/ParameterAttributes.h"
16 #include "llvm/Constants.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/SCCIterator.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Debug.h"
28 // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
29 // created and later destroyed, all in an effort to make sure that there is only
30 // a single canonical version of a type.
32 // #define DEBUG_MERGE_TYPES 1
34 AbstractTypeUser::~AbstractTypeUser() {}
37 //===----------------------------------------------------------------------===//
38 // Type PATypeHolder Implementation
39 //===----------------------------------------------------------------------===//
41 /// get - This implements the forwarding part of the union-find algorithm for
42 /// abstract types. Before every access to the Type*, we check to see if the
43 /// type we are pointing to is forwarding to a new type. If so, we drop our
44 /// reference to the type.
46 Type* PATypeHolder::get() const {
47 const Type *NewTy = Ty->getForwardedType();
48 if (!NewTy) return const_cast<Type*>(Ty);
49 return *const_cast<PATypeHolder*>(this) = NewTy;
52 //===----------------------------------------------------------------------===//
53 // Type Class Implementation
54 //===----------------------------------------------------------------------===//
56 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
57 // for types as they are needed. Because resolution of types must invalidate
58 // all of the abstract type descriptions, we keep them in a seperate map to make
60 static ManagedStatic<std::map<const Type*,
61 std::string> > ConcreteTypeDescriptions;
62 static ManagedStatic<std::map<const Type*,
63 std::string> > AbstractTypeDescriptions;
65 /// Because of the way Type subclasses are allocated, this function is necessary
66 /// to use the correct kind of "delete" operator to deallocate the Type object.
67 /// Some type objects (FunctionTy, StructTy) allocate additional space after
68 /// the space for their derived type to hold the contained types array of
69 /// PATypeHandles. Using this allocation scheme means all the PATypeHandles are
70 /// allocated with the type object, decreasing allocations and eliminating the
71 /// need for a std::vector to be used in the Type class itself.
72 /// @brief Type destruction function
73 void Type::destroy() const {
75 // Structures and Functions allocate their contained types past the end of
76 // the type object itself. These need to be destroyed differently than the
78 if (isa<FunctionType>(this) || isa<StructType>(this)) {
79 // First, make sure we destruct any PATypeHandles allocated by these
80 // subclasses. They must be manually destructed.
81 for (unsigned i = 0; i < NumContainedTys; ++i)
82 ContainedTys[i].PATypeHandle::~PATypeHandle();
84 // Now call the destructor for the subclass directly because we're going
85 // to delete this as an array of char.
86 if (isa<FunctionType>(this))
87 ((FunctionType*)this)->FunctionType::~FunctionType();
89 ((StructType*)this)->StructType::~StructType();
91 // Finally, remove the memory as an array deallocation of the chars it was
93 delete [] reinterpret_cast<const char*>(this);
98 // For all the other type subclasses, there is either no contained types or
99 // just one (all Sequentials). For Sequentials, the PATypeHandle is not
100 // allocated past the type object, its included directly in the SequentialType
101 // class. This means we can safely just do "normal" delete of this object and
102 // all the destructors that need to run will be run.
106 const Type *Type::getPrimitiveType(TypeID IDNumber) {
108 case VoidTyID : return VoidTy;
109 case FloatTyID : return FloatTy;
110 case DoubleTyID: return DoubleTy;
111 case LabelTyID : return LabelTy;
117 const Type *Type::getVAArgsPromotedType() const {
118 if (ID == IntegerTyID && getSubclassData() < 32)
119 return Type::Int32Ty;
120 else if (ID == FloatTyID)
121 return Type::DoubleTy;
126 /// isFPOrFPVector - Return true if this is a FP type or a vector of FP types.
128 bool Type::isFPOrFPVector() const {
129 if (ID == Type::FloatTyID || ID == Type::DoubleTyID) return true;
130 if (ID != Type::VectorTyID) return false;
132 return cast<VectorType>(this)->getElementType()->isFloatingPoint();
135 // canLosslesllyBitCastTo - Return true if this type can be converted to
136 // 'Ty' without any reinterpretation of bits. For example, uint to int.
138 bool Type::canLosslesslyBitCastTo(const Type *Ty) const {
139 // Identity cast means no change so return true
143 // They are not convertible unless they are at least first class types
144 if (!this->isFirstClassType() || !Ty->isFirstClassType())
147 // Vector -> Vector conversions are always lossless if the two vector types
148 // have the same size, otherwise not.
149 if (const VectorType *thisPTy = dyn_cast<VectorType>(this))
150 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
151 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
153 // At this point we have only various mismatches of the first class types
154 // remaining and ptr->ptr. Just select the lossless conversions. Everything
155 // else is not lossless.
156 if (isa<PointerType>(this))
157 return isa<PointerType>(Ty);
158 return false; // Other types have no identity values
161 unsigned Type::getPrimitiveSizeInBits() const {
162 switch (getTypeID()) {
163 case Type::FloatTyID: return 32;
164 case Type::DoubleTyID: return 64;
165 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
166 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
171 /// isSizedDerivedType - Derived types like structures and arrays are sized
172 /// iff all of the members of the type are sized as well. Since asking for
173 /// their size is relatively uncommon, move this operation out of line.
174 bool Type::isSizedDerivedType() const {
175 if (isa<IntegerType>(this))
178 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
179 return ATy->getElementType()->isSized();
181 if (const VectorType *PTy = dyn_cast<VectorType>(this))
182 return PTy->getElementType()->isSized();
184 if (!isa<StructType>(this))
187 // Okay, our struct is sized if all of the elements are...
188 for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
189 if (!(*I)->isSized())
195 /// getForwardedTypeInternal - This method is used to implement the union-find
196 /// algorithm for when a type is being forwarded to another type.
197 const Type *Type::getForwardedTypeInternal() const {
198 assert(ForwardType && "This type is not being forwarded to another type!");
200 // Check to see if the forwarded type has been forwarded on. If so, collapse
201 // the forwarding links.
202 const Type *RealForwardedType = ForwardType->getForwardedType();
203 if (!RealForwardedType)
204 return ForwardType; // No it's not forwarded again
206 // Yes, it is forwarded again. First thing, add the reference to the new
208 if (RealForwardedType->isAbstract())
209 cast<DerivedType>(RealForwardedType)->addRef();
211 // Now drop the old reference. This could cause ForwardType to get deleted.
212 cast<DerivedType>(ForwardType)->dropRef();
214 // Return the updated type.
215 ForwardType = RealForwardedType;
219 void Type::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
222 void Type::typeBecameConcrete(const DerivedType *AbsTy) {
227 // getTypeDescription - This is a recursive function that walks a type hierarchy
228 // calculating the description for a type.
230 static std::string getTypeDescription(const Type *Ty,
231 std::vector<const Type *> &TypeStack) {
232 if (isa<OpaqueType>(Ty)) { // Base case for the recursion
233 std::map<const Type*, std::string>::iterator I =
234 AbstractTypeDescriptions->lower_bound(Ty);
235 if (I != AbstractTypeDescriptions->end() && I->first == Ty)
237 std::string Desc = "opaque";
238 AbstractTypeDescriptions->insert(std::make_pair(Ty, Desc));
242 if (!Ty->isAbstract()) { // Base case for the recursion
243 std::map<const Type*, std::string>::iterator I =
244 ConcreteTypeDescriptions->find(Ty);
245 if (I != ConcreteTypeDescriptions->end())
248 if (Ty->isPrimitiveType()) {
249 switch (Ty->getTypeID()) {
250 default: assert(0 && "Unknown prim type!");
251 case Type::VoidTyID: return (*ConcreteTypeDescriptions)[Ty] = "void";
252 case Type::FloatTyID: return (*ConcreteTypeDescriptions)[Ty] = "float";
253 case Type::DoubleTyID: return (*ConcreteTypeDescriptions)[Ty] = "double";
254 case Type::LabelTyID: return (*ConcreteTypeDescriptions)[Ty] = "label";
259 // Check to see if the Type is already on the stack...
260 unsigned Slot = 0, CurSize = TypeStack.size();
261 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
263 // This is another base case for the recursion. In this case, we know
264 // that we have looped back to a type that we have previously visited.
265 // Generate the appropriate upreference to handle this.
268 return "\\" + utostr(CurSize-Slot); // Here's the upreference
270 // Recursive case: derived types...
272 TypeStack.push_back(Ty); // Add us to the stack..
274 switch (Ty->getTypeID()) {
275 case Type::IntegerTyID: {
276 const IntegerType *ITy = cast<IntegerType>(Ty);
277 Result = "i" + utostr(ITy->getBitWidth());
280 case Type::FunctionTyID: {
281 const FunctionType *FTy = cast<FunctionType>(Ty);
284 Result += getTypeDescription(FTy->getReturnType(), TypeStack) + " (";
286 const ParamAttrsList *Attrs = FTy->getParamAttrs();
287 for (FunctionType::param_iterator I = FTy->param_begin(),
288 E = FTy->param_end(); I != E; ++I) {
289 if (I != FTy->param_begin())
291 if (Attrs && Attrs->getParamAttrs(Idx) != ParamAttr::None)
292 Result += Attrs->getParamAttrsTextByIndex(Idx);
294 Result += getTypeDescription(*I, TypeStack);
296 if (FTy->isVarArg()) {
297 if (FTy->getNumParams()) Result += ", ";
301 if (Attrs && Attrs->getParamAttrs(0) != ParamAttr::None) {
302 Result += " " + Attrs->getParamAttrsTextByIndex(0);
306 case Type::StructTyID: {
307 const StructType *STy = cast<StructType>(Ty);
312 for (StructType::element_iterator I = STy->element_begin(),
313 E = STy->element_end(); I != E; ++I) {
314 if (I != STy->element_begin())
316 Result += getTypeDescription(*I, TypeStack);
323 case Type::PointerTyID: {
324 const PointerType *PTy = cast<PointerType>(Ty);
325 Result = getTypeDescription(PTy->getElementType(), TypeStack) + " *";
328 case Type::ArrayTyID: {
329 const ArrayType *ATy = cast<ArrayType>(Ty);
330 unsigned NumElements = ATy->getNumElements();
332 Result += utostr(NumElements) + " x ";
333 Result += getTypeDescription(ATy->getElementType(), TypeStack) + "]";
336 case Type::VectorTyID: {
337 const VectorType *PTy = cast<VectorType>(Ty);
338 unsigned NumElements = PTy->getNumElements();
340 Result += utostr(NumElements) + " x ";
341 Result += getTypeDescription(PTy->getElementType(), TypeStack) + ">";
346 assert(0 && "Unhandled type in getTypeDescription!");
349 TypeStack.pop_back(); // Remove self from stack...
356 static const std::string &getOrCreateDesc(std::map<const Type*,std::string>&Map,
358 std::map<const Type*, std::string>::iterator I = Map.find(Ty);
359 if (I != Map.end()) return I->second;
361 std::vector<const Type *> TypeStack;
362 std::string Result = getTypeDescription(Ty, TypeStack);
363 return Map[Ty] = Result;
367 const std::string &Type::getDescription() const {
369 return getOrCreateDesc(*AbstractTypeDescriptions, this);
371 return getOrCreateDesc(*ConcreteTypeDescriptions, this);
375 bool StructType::indexValid(const Value *V) const {
376 // Structure indexes require 32-bit integer constants.
377 if (V->getType() == Type::Int32Ty)
378 if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
379 return CU->getZExtValue() < NumContainedTys;
383 // getTypeAtIndex - Given an index value into the type, return the type of the
384 // element. For a structure type, this must be a constant value...
386 const Type *StructType::getTypeAtIndex(const Value *V) const {
387 assert(indexValid(V) && "Invalid structure index!");
388 unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
389 return ContainedTys[Idx];
392 //===----------------------------------------------------------------------===//
393 // Primitive 'Type' data
394 //===----------------------------------------------------------------------===//
396 const Type *Type::VoidTy = new Type(Type::VoidTyID);
397 const Type *Type::FloatTy = new Type(Type::FloatTyID);
398 const Type *Type::DoubleTy = new Type(Type::DoubleTyID);
399 const Type *Type::LabelTy = new Type(Type::LabelTyID);
402 struct BuiltinIntegerType : public IntegerType {
403 BuiltinIntegerType(unsigned W) : IntegerType(W) {}
406 const IntegerType *Type::Int1Ty = new BuiltinIntegerType(1);
407 const IntegerType *Type::Int8Ty = new BuiltinIntegerType(8);
408 const IntegerType *Type::Int16Ty = new BuiltinIntegerType(16);
409 const IntegerType *Type::Int32Ty = new BuiltinIntegerType(32);
410 const IntegerType *Type::Int64Ty = new BuiltinIntegerType(64);
413 //===----------------------------------------------------------------------===//
414 // Derived Type Constructors
415 //===----------------------------------------------------------------------===//
417 FunctionType::FunctionType(const Type *Result,
418 const std::vector<const Type*> &Params,
419 bool IsVarArgs, const ParamAttrsList *Attrs)
420 : DerivedType(FunctionTyID), isVarArgs(IsVarArgs), ParamAttrs(Attrs) {
421 ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
422 NumContainedTys = Params.size() + 1; // + 1 for result type
423 assert((Result->isFirstClassType() || Result == Type::VoidTy ||
424 isa<OpaqueType>(Result)) &&
425 "LLVM functions cannot return aggregates");
426 bool isAbstract = Result->isAbstract();
427 new (&ContainedTys[0]) PATypeHandle(Result, this);
429 for (unsigned i = 0; i != Params.size(); ++i) {
430 assert((Params[i]->isFirstClassType() || isa<OpaqueType>(Params[i])) &&
431 "Function arguments must be value types!");
432 new (&ContainedTys[i+1]) PATypeHandle(Params[i],this);
433 isAbstract |= Params[i]->isAbstract();
436 // Calculate whether or not this type is abstract
437 setAbstract(isAbstract);
440 StructType::StructType(const std::vector<const Type*> &Types, bool isPacked)
441 : CompositeType(StructTyID) {
442 ContainedTys = reinterpret_cast<PATypeHandle*>(this + 1);
443 NumContainedTys = Types.size();
444 setSubclassData(isPacked);
445 bool isAbstract = false;
446 for (unsigned i = 0; i < Types.size(); ++i) {
447 assert(Types[i] != Type::VoidTy && "Void type for structure field!!");
448 new (&ContainedTys[i]) PATypeHandle(Types[i], this);
449 isAbstract |= Types[i]->isAbstract();
452 // Calculate whether or not this type is abstract
453 setAbstract(isAbstract);
456 ArrayType::ArrayType(const Type *ElType, uint64_t NumEl)
457 : SequentialType(ArrayTyID, ElType) {
460 // Calculate whether or not this type is abstract
461 setAbstract(ElType->isAbstract());
464 VectorType::VectorType(const Type *ElType, unsigned NumEl)
465 : SequentialType(VectorTyID, ElType) {
467 setAbstract(ElType->isAbstract());
468 assert(NumEl > 0 && "NumEl of a VectorType must be greater than 0");
469 assert((ElType->isInteger() || ElType->isFloatingPoint() ||
470 isa<OpaqueType>(ElType)) &&
471 "Elements of a VectorType must be a primitive type");
476 PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
477 // Calculate whether or not this type is abstract
478 setAbstract(E->isAbstract());
481 OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
483 #ifdef DEBUG_MERGE_TYPES
484 DOUT << "Derived new type: " << *this << "\n";
488 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
489 // another (more concrete) type, we must eliminate all references to other
490 // types, to avoid some circular reference problems.
491 void DerivedType::dropAllTypeUses() {
492 if (NumContainedTys != 0) {
493 // The type must stay abstract. To do this, we insert a pointer to a type
494 // that will never get resolved, thus will always be abstract.
495 static Type *AlwaysOpaqueTy = OpaqueType::get();
496 static PATypeHolder Holder(AlwaysOpaqueTy);
497 ContainedTys[0] = AlwaysOpaqueTy;
499 // Change the rest of the types to be Int32Ty's. It doesn't matter what we
500 // pick so long as it doesn't point back to this type. We choose something
501 // concrete to avoid overhead for adding to AbstracTypeUser lists and stuff.
502 for (unsigned i = 1, e = NumContainedTys; i != e; ++i)
503 ContainedTys[i] = Type::Int32Ty;
509 /// TypePromotionGraph and graph traits - this is designed to allow us to do
510 /// efficient SCC processing of type graphs. This is the exact same as
511 /// GraphTraits<Type*>, except that we pretend that concrete types have no
512 /// children to avoid processing them.
513 struct TypePromotionGraph {
515 TypePromotionGraph(Type *T) : Ty(T) {}
519 template <> struct GraphTraits<TypePromotionGraph> {
520 typedef Type NodeType;
521 typedef Type::subtype_iterator ChildIteratorType;
523 static inline NodeType *getEntryNode(TypePromotionGraph G) { return G.Ty; }
524 static inline ChildIteratorType child_begin(NodeType *N) {
526 return N->subtype_begin();
527 else // No need to process children of concrete types.
528 return N->subtype_end();
530 static inline ChildIteratorType child_end(NodeType *N) {
531 return N->subtype_end();
537 // PromoteAbstractToConcrete - This is a recursive function that walks a type
538 // graph calculating whether or not a type is abstract.
540 void Type::PromoteAbstractToConcrete() {
541 if (!isAbstract()) return;
543 scc_iterator<TypePromotionGraph> SI = scc_begin(TypePromotionGraph(this));
544 scc_iterator<TypePromotionGraph> SE = scc_end (TypePromotionGraph(this));
546 for (; SI != SE; ++SI) {
547 std::vector<Type*> &SCC = *SI;
549 // Concrete types are leaves in the tree. Since an SCC will either be all
550 // abstract or all concrete, we only need to check one type.
551 if (SCC[0]->isAbstract()) {
552 if (isa<OpaqueType>(SCC[0]))
553 return; // Not going to be concrete, sorry.
555 // If all of the children of all of the types in this SCC are concrete,
556 // then this SCC is now concrete as well. If not, neither this SCC, nor
557 // any parent SCCs will be concrete, so we might as well just exit.
558 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
559 for (Type::subtype_iterator CI = SCC[i]->subtype_begin(),
560 E = SCC[i]->subtype_end(); CI != E; ++CI)
561 if ((*CI)->isAbstract())
562 // If the child type is in our SCC, it doesn't make the entire SCC
563 // abstract unless there is a non-SCC abstract type.
564 if (std::find(SCC.begin(), SCC.end(), *CI) == SCC.end())
565 return; // Not going to be concrete, sorry.
567 // Okay, we just discovered this whole SCC is now concrete, mark it as
569 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
570 assert(SCC[i]->isAbstract() && "Why are we processing concrete types?");
572 SCC[i]->setAbstract(false);
575 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
576 assert(!SCC[i]->isAbstract() && "Concrete type became abstract?");
577 // The type just became concrete, notify all users!
578 cast<DerivedType>(SCC[i])->notifyUsesThatTypeBecameConcrete();
585 //===----------------------------------------------------------------------===//
586 // Type Structural Equality Testing
587 //===----------------------------------------------------------------------===//
589 // TypesEqual - Two types are considered structurally equal if they have the
590 // same "shape": Every level and element of the types have identical primitive
591 // ID's, and the graphs have the same edges/nodes in them. Nodes do not have to
592 // be pointer equals to be equivalent though. This uses an optimistic algorithm
593 // that assumes that two graphs are the same until proven otherwise.
595 static bool TypesEqual(const Type *Ty, const Type *Ty2,
596 std::map<const Type *, const Type *> &EqTypes) {
597 if (Ty == Ty2) return true;
598 if (Ty->getTypeID() != Ty2->getTypeID()) return false;
599 if (isa<OpaqueType>(Ty))
600 return false; // Two unequal opaque types are never equal
602 std::map<const Type*, const Type*>::iterator It = EqTypes.lower_bound(Ty);
603 if (It != EqTypes.end() && It->first == Ty)
604 return It->second == Ty2; // Looping back on a type, check for equality
606 // Otherwise, add the mapping to the table to make sure we don't get
607 // recursion on the types...
608 EqTypes.insert(It, std::make_pair(Ty, Ty2));
610 // Two really annoying special cases that breaks an otherwise nice simple
611 // algorithm is the fact that arraytypes have sizes that differentiates types,
612 // and that function types can be varargs or not. Consider this now.
614 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
615 const IntegerType *ITy2 = cast<IntegerType>(Ty2);
616 return ITy->getBitWidth() == ITy2->getBitWidth();
617 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
618 return TypesEqual(PTy->getElementType(),
619 cast<PointerType>(Ty2)->getElementType(), EqTypes);
620 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
621 const StructType *STy2 = cast<StructType>(Ty2);
622 if (STy->getNumElements() != STy2->getNumElements()) return false;
623 if (STy->isPacked() != STy2->isPacked()) return false;
624 for (unsigned i = 0, e = STy2->getNumElements(); i != e; ++i)
625 if (!TypesEqual(STy->getElementType(i), STy2->getElementType(i), EqTypes))
628 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
629 const ArrayType *ATy2 = cast<ArrayType>(Ty2);
630 return ATy->getNumElements() == ATy2->getNumElements() &&
631 TypesEqual(ATy->getElementType(), ATy2->getElementType(), EqTypes);
632 } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
633 const VectorType *PTy2 = cast<VectorType>(Ty2);
634 return PTy->getNumElements() == PTy2->getNumElements() &&
635 TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
636 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
637 const FunctionType *FTy2 = cast<FunctionType>(Ty2);
638 if (FTy->isVarArg() != FTy2->isVarArg() ||
639 FTy->getNumParams() != FTy2->getNumParams() ||
640 !TypesEqual(FTy->getReturnType(), FTy2->getReturnType(), EqTypes))
642 const ParamAttrsList *Attrs1 = FTy->getParamAttrs();
643 const ParamAttrsList *Attrs2 = FTy2->getParamAttrs();
644 if ((!Attrs1 && Attrs2) || (!Attrs2 && Attrs1) ||
645 (Attrs1 && Attrs2 && (Attrs1->size() != Attrs2->size() ||
646 (Attrs1->getParamAttrs(0) != Attrs2->getParamAttrs(0)))))
649 for (unsigned i = 0, e = FTy2->getNumParams(); i != e; ++i) {
650 if (Attrs1 && Attrs1->getParamAttrs(i+1) != Attrs2->getParamAttrs(i+1))
652 if (!TypesEqual(FTy->getParamType(i), FTy2->getParamType(i), EqTypes))
657 assert(0 && "Unknown derived type!");
662 static bool TypesEqual(const Type *Ty, const Type *Ty2) {
663 std::map<const Type *, const Type *> EqTypes;
664 return TypesEqual(Ty, Ty2, EqTypes);
667 // AbstractTypeHasCycleThrough - Return true there is a path from CurTy to
668 // TargetTy in the type graph. We know that Ty is an abstract type, so if we
669 // ever reach a non-abstract type, we know that we don't need to search the
671 static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
672 std::set<const Type*> &VisitedTypes) {
673 if (TargetTy == CurTy) return true;
674 if (!CurTy->isAbstract()) return false;
676 if (!VisitedTypes.insert(CurTy).second)
677 return false; // Already been here.
679 for (Type::subtype_iterator I = CurTy->subtype_begin(),
680 E = CurTy->subtype_end(); I != E; ++I)
681 if (AbstractTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
686 static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
687 std::set<const Type*> &VisitedTypes) {
688 if (TargetTy == CurTy) return true;
690 if (!VisitedTypes.insert(CurTy).second)
691 return false; // Already been here.
693 for (Type::subtype_iterator I = CurTy->subtype_begin(),
694 E = CurTy->subtype_end(); I != E; ++I)
695 if (ConcreteTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
700 /// TypeHasCycleThroughItself - Return true if the specified type has a cycle
702 static bool TypeHasCycleThroughItself(const Type *Ty) {
703 std::set<const Type*> VisitedTypes;
705 if (Ty->isAbstract()) { // Optimized case for abstract types.
706 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
708 if (AbstractTypeHasCycleThrough(Ty, *I, VisitedTypes))
711 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
713 if (ConcreteTypeHasCycleThrough(Ty, *I, VisitedTypes))
719 /// getSubElementHash - Generate a hash value for all of the SubType's of this
720 /// type. The hash value is guaranteed to be zero if any of the subtypes are
721 /// an opaque type. Otherwise we try to mix them in as well as possible, but do
722 /// not look at the subtype's subtype's.
723 static unsigned getSubElementHash(const Type *Ty) {
724 unsigned HashVal = 0;
725 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
728 const Type *SubTy = I->get();
729 HashVal += SubTy->getTypeID();
730 switch (SubTy->getTypeID()) {
732 case Type::OpaqueTyID: return 0; // Opaque -> hash = 0 no matter what.
733 case Type::IntegerTyID:
734 HashVal ^= (cast<IntegerType>(SubTy)->getBitWidth() << 3);
736 case Type::FunctionTyID:
737 HashVal ^= cast<FunctionType>(SubTy)->getNumParams()*2 +
738 cast<FunctionType>(SubTy)->isVarArg();
740 case Type::ArrayTyID:
741 HashVal ^= cast<ArrayType>(SubTy)->getNumElements();
743 case Type::VectorTyID:
744 HashVal ^= cast<VectorType>(SubTy)->getNumElements();
746 case Type::StructTyID:
747 HashVal ^= cast<StructType>(SubTy)->getNumElements();
751 return HashVal ? HashVal : 1; // Do not return zero unless opaque subty.
754 //===----------------------------------------------------------------------===//
755 // Derived Type Factory Functions
756 //===----------------------------------------------------------------------===//
761 /// TypesByHash - Keep track of types by their structure hash value. Note
762 /// that we only keep track of types that have cycles through themselves in
765 std::multimap<unsigned, PATypeHolder> TypesByHash;
768 void RemoveFromTypesByHash(unsigned Hash, const Type *Ty) {
769 std::multimap<unsigned, PATypeHolder>::iterator I =
770 TypesByHash.lower_bound(Hash);
771 for (; I != TypesByHash.end() && I->first == Hash; ++I) {
772 if (I->second == Ty) {
773 TypesByHash.erase(I);
778 // This must be do to an opaque type that was resolved. Switch down to hash
780 assert(Hash && "Didn't find type entry!");
781 RemoveFromTypesByHash(0, Ty);
784 /// TypeBecameConcrete - When Ty gets a notification that TheType just became
785 /// concrete, drop uses and make Ty non-abstract if we should.
786 void TypeBecameConcrete(DerivedType *Ty, const DerivedType *TheType) {
787 // If the element just became concrete, remove 'ty' from the abstract
788 // type user list for the type. Do this for as many times as Ty uses
790 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
792 if (I->get() == TheType)
793 TheType->removeAbstractTypeUser(Ty);
795 // If the type is currently thought to be abstract, rescan all of our
796 // subtypes to see if the type has just become concrete! Note that this
797 // may send out notifications to AbstractTypeUsers that types become
799 if (Ty->isAbstract())
800 Ty->PromoteAbstractToConcrete();
806 // TypeMap - Make sure that only one instance of a particular type may be
807 // created on any given run of the compiler... note that this involves updating
808 // our map if an abstract type gets refined somehow.
811 template<class ValType, class TypeClass>
812 class TypeMap : public TypeMapBase {
813 std::map<ValType, PATypeHolder> Map;
815 typedef typename std::map<ValType, PATypeHolder>::iterator iterator;
816 ~TypeMap() { print("ON EXIT"); }
818 inline TypeClass *get(const ValType &V) {
819 iterator I = Map.find(V);
820 return I != Map.end() ? cast<TypeClass>((Type*)I->second.get()) : 0;
823 inline void add(const ValType &V, TypeClass *Ty) {
824 Map.insert(std::make_pair(V, Ty));
826 // If this type has a cycle, remember it.
827 TypesByHash.insert(std::make_pair(ValType::hashTypeStructure(Ty), Ty));
831 /// RefineAbstractType - This method is called after we have merged a type
832 /// with another one. We must now either merge the type away with
833 /// some other type or reinstall it in the map with it's new configuration.
834 void RefineAbstractType(TypeClass *Ty, const DerivedType *OldType,
835 const Type *NewType) {
836 #ifdef DEBUG_MERGE_TYPES
837 DOUT << "RefineAbstractType(" << (void*)OldType << "[" << *OldType
838 << "], " << (void*)NewType << " [" << *NewType << "])\n";
841 // Otherwise, we are changing one subelement type into another. Clearly the
842 // OldType must have been abstract, making us abstract.
843 assert(Ty->isAbstract() && "Refining a non-abstract type!");
844 assert(OldType != NewType);
846 // Make a temporary type holder for the type so that it doesn't disappear on
847 // us when we erase the entry from the map.
848 PATypeHolder TyHolder = Ty;
850 // The old record is now out-of-date, because one of the children has been
851 // updated. Remove the obsolete entry from the map.
852 unsigned NumErased = Map.erase(ValType::get(Ty));
853 assert(NumErased && "Element not found!");
855 // Remember the structural hash for the type before we start hacking on it,
856 // in case we need it later.
857 unsigned OldTypeHash = ValType::hashTypeStructure(Ty);
859 // Find the type element we are refining... and change it now!
860 for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i)
861 if (Ty->ContainedTys[i] == OldType)
862 Ty->ContainedTys[i] = NewType;
863 unsigned NewTypeHash = ValType::hashTypeStructure(Ty);
865 // If there are no cycles going through this node, we can do a simple,
866 // efficient lookup in the map, instead of an inefficient nasty linear
868 if (!TypeHasCycleThroughItself(Ty)) {
869 typename std::map<ValType, PATypeHolder>::iterator I;
872 tie(I, Inserted) = Map.insert(std::make_pair(ValType::get(Ty), Ty));
874 // Refined to a different type altogether?
875 RemoveFromTypesByHash(OldTypeHash, Ty);
877 // We already have this type in the table. Get rid of the newly refined
879 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
880 Ty->refineAbstractTypeTo(NewTy);
884 // Now we check to see if there is an existing entry in the table which is
885 // structurally identical to the newly refined type. If so, this type
886 // gets refined to the pre-existing type.
888 std::multimap<unsigned, PATypeHolder>::iterator I, E, Entry;
889 tie(I, E) = TypesByHash.equal_range(NewTypeHash);
891 for (; I != E; ++I) {
892 if (I->second == Ty) {
893 // Remember the position of the old type if we see it in our scan.
896 if (TypesEqual(Ty, I->second)) {
897 TypeClass *NewTy = cast<TypeClass>((Type*)I->second.get());
899 // Remove the old entry form TypesByHash. If the hash values differ
900 // now, remove it from the old place. Otherwise, continue scanning
901 // withing this hashcode to reduce work.
902 if (NewTypeHash != OldTypeHash) {
903 RemoveFromTypesByHash(OldTypeHash, Ty);
906 // Find the location of Ty in the TypesByHash structure if we
907 // haven't seen it already.
908 while (I->second != Ty) {
910 assert(I != E && "Structure doesn't contain type??");
914 TypesByHash.erase(Entry);
916 Ty->refineAbstractTypeTo(NewTy);
922 // If there is no existing type of the same structure, we reinsert an
923 // updated record into the map.
924 Map.insert(std::make_pair(ValType::get(Ty), Ty));
927 // If the hash codes differ, update TypesByHash
928 if (NewTypeHash != OldTypeHash) {
929 RemoveFromTypesByHash(OldTypeHash, Ty);
930 TypesByHash.insert(std::make_pair(NewTypeHash, Ty));
933 // If the type is currently thought to be abstract, rescan all of our
934 // subtypes to see if the type has just become concrete! Note that this
935 // may send out notifications to AbstractTypeUsers that types become
937 if (Ty->isAbstract())
938 Ty->PromoteAbstractToConcrete();
941 void print(const char *Arg) const {
942 #ifdef DEBUG_MERGE_TYPES
943 DOUT << "TypeMap<>::" << Arg << " table contents:\n";
945 for (typename std::map<ValType, PATypeHolder>::const_iterator I
946 = Map.begin(), E = Map.end(); I != E; ++I)
947 DOUT << " " << (++i) << ". " << (void*)I->second.get() << " "
948 << *I->second.get() << "\n";
952 void dump() const { print("dump output"); }
957 //===----------------------------------------------------------------------===//
958 // Function Type Factory and Value Class...
961 //===----------------------------------------------------------------------===//
962 // Integer Type Factory...
965 class IntegerValType {
968 IntegerValType(uint16_t numbits) : bits(numbits) {}
970 static IntegerValType get(const IntegerType *Ty) {
971 return IntegerValType(Ty->getBitWidth());
974 static unsigned hashTypeStructure(const IntegerType *Ty) {
975 return (unsigned)Ty->getBitWidth();
978 inline bool operator<(const IntegerValType &IVT) const {
979 return bits < IVT.bits;
984 static ManagedStatic<TypeMap<IntegerValType, IntegerType> > IntegerTypes;
986 const IntegerType *IntegerType::get(unsigned NumBits) {
987 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
988 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
990 // Check for the built-in integer types
992 case 1: return cast<IntegerType>(Type::Int1Ty);
993 case 8: return cast<IntegerType>(Type::Int8Ty);
994 case 16: return cast<IntegerType>(Type::Int16Ty);
995 case 32: return cast<IntegerType>(Type::Int32Ty);
996 case 64: return cast<IntegerType>(Type::Int64Ty);
1001 IntegerValType IVT(NumBits);
1002 IntegerType *ITy = IntegerTypes->get(IVT);
1003 if (ITy) return ITy; // Found a match, return it!
1005 // Value not found. Derive a new type!
1006 ITy = new IntegerType(NumBits);
1007 IntegerTypes->add(IVT, ITy);
1009 #ifdef DEBUG_MERGE_TYPES
1010 DOUT << "Derived new type: " << *ITy << "\n";
1015 bool IntegerType::isPowerOf2ByteWidth() const {
1016 unsigned BitWidth = getBitWidth();
1017 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
1020 APInt IntegerType::getMask() const {
1021 return APInt::getAllOnesValue(getBitWidth());
1024 // FunctionValType - Define a class to hold the key that goes into the TypeMap
1027 class FunctionValType {
1029 std::vector<const Type*> ArgTypes;
1030 const ParamAttrsList *ParamAttrs;
1033 FunctionValType(const Type *ret, const std::vector<const Type*> &args,
1034 bool IVA, const ParamAttrsList *attrs)
1035 : RetTy(ret), ParamAttrs(attrs), isVarArg(IVA) {
1036 for (unsigned i = 0; i < args.size(); ++i)
1037 ArgTypes.push_back(args[i]);
1040 static FunctionValType get(const FunctionType *FT);
1042 static unsigned hashTypeStructure(const FunctionType *FT) {
1043 unsigned Result = FT->getNumParams()*64 + FT->isVarArg();
1044 if (FT->getParamAttrs())
1045 Result += FT->getParamAttrs()->size()*2;
1049 inline bool operator<(const FunctionValType &MTV) const {
1050 if (RetTy < MTV.RetTy) return true;
1051 if (RetTy > MTV.RetTy) return false;
1052 if (isVarArg < MTV.isVarArg) return true;
1053 if (isVarArg > MTV.isVarArg) return false;
1054 if (ArgTypes < MTV.ArgTypes) return true;
1055 if (ArgTypes > MTV.ArgTypes) return false;
1058 return *ParamAttrs < *MTV.ParamAttrs;
1061 else if (MTV.ParamAttrs)
1068 // Define the actual map itself now...
1069 static ManagedStatic<TypeMap<FunctionValType, FunctionType> > FunctionTypes;
1071 FunctionValType FunctionValType::get(const FunctionType *FT) {
1072 // Build up a FunctionValType
1073 std::vector<const Type *> ParamTypes;
1074 ParamTypes.reserve(FT->getNumParams());
1075 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1076 ParamTypes.push_back(FT->getParamType(i));
1077 return FunctionValType(FT->getReturnType(), ParamTypes, FT->isVarArg(),
1078 FT->getParamAttrs());
1082 // FunctionType::get - The factory function for the FunctionType class...
1083 FunctionType *FunctionType::get(const Type *ReturnType,
1084 const std::vector<const Type*> &Params,
1086 const ParamAttrsList *Attrs) {
1088 FunctionValType VT(ReturnType, Params, isVarArg, Attrs);
1089 FunctionType *FT = FunctionTypes->get(VT);
1094 FT = (FunctionType*) new char[sizeof(FunctionType) +
1095 sizeof(PATypeHandle)*(Params.size()+1)];
1096 new (FT) FunctionType(ReturnType, Params, isVarArg, Attrs);
1097 FunctionTypes->add(VT, FT);
1099 #ifdef DEBUG_MERGE_TYPES
1100 DOUT << "Derived new type: " << FT << "\n";
1105 bool FunctionType::isStructReturn() const {
1107 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
1111 //===----------------------------------------------------------------------===//
1112 // Array Type Factory...
1115 class ArrayValType {
1119 ArrayValType(const Type *val, uint64_t sz) : ValTy(val), Size(sz) {}
1121 static ArrayValType get(const ArrayType *AT) {
1122 return ArrayValType(AT->getElementType(), AT->getNumElements());
1125 static unsigned hashTypeStructure(const ArrayType *AT) {
1126 return (unsigned)AT->getNumElements();
1129 inline bool operator<(const ArrayValType &MTV) const {
1130 if (Size < MTV.Size) return true;
1131 return Size == MTV.Size && ValTy < MTV.ValTy;
1135 static ManagedStatic<TypeMap<ArrayValType, ArrayType> > ArrayTypes;
1138 ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
1139 assert(ElementType && "Can't get array of null types!");
1141 ArrayValType AVT(ElementType, NumElements);
1142 ArrayType *AT = ArrayTypes->get(AVT);
1143 if (AT) return AT; // Found a match, return it!
1145 // Value not found. Derive a new type!
1146 ArrayTypes->add(AVT, AT = new ArrayType(ElementType, NumElements));
1148 #ifdef DEBUG_MERGE_TYPES
1149 DOUT << "Derived new type: " << *AT << "\n";
1155 //===----------------------------------------------------------------------===//
1156 // Vector Type Factory...
1159 class VectorValType {
1163 VectorValType(const Type *val, int sz) : ValTy(val), Size(sz) {}
1165 static VectorValType get(const VectorType *PT) {
1166 return VectorValType(PT->getElementType(), PT->getNumElements());
1169 static unsigned hashTypeStructure(const VectorType *PT) {
1170 return PT->getNumElements();
1173 inline bool operator<(const VectorValType &MTV) const {
1174 if (Size < MTV.Size) return true;
1175 return Size == MTV.Size && ValTy < MTV.ValTy;
1179 static ManagedStatic<TypeMap<VectorValType, VectorType> > VectorTypes;
1182 VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) {
1183 assert(ElementType && "Can't get vector of null types!");
1184 assert(isPowerOf2_32(NumElements) && "Vector length should be a power of 2!");
1186 VectorValType PVT(ElementType, NumElements);
1187 VectorType *PT = VectorTypes->get(PVT);
1188 if (PT) return PT; // Found a match, return it!
1190 // Value not found. Derive a new type!
1191 VectorTypes->add(PVT, PT = new VectorType(ElementType, NumElements));
1193 #ifdef DEBUG_MERGE_TYPES
1194 DOUT << "Derived new type: " << *PT << "\n";
1199 //===----------------------------------------------------------------------===//
1200 // Struct Type Factory...
1204 // StructValType - Define a class to hold the key that goes into the TypeMap
1206 class StructValType {
1207 std::vector<const Type*> ElTypes;
1210 StructValType(const std::vector<const Type*> &args, bool isPacked)
1211 : ElTypes(args), packed(isPacked) {}
1213 static StructValType get(const StructType *ST) {
1214 std::vector<const Type *> ElTypes;
1215 ElTypes.reserve(ST->getNumElements());
1216 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i)
1217 ElTypes.push_back(ST->getElementType(i));
1219 return StructValType(ElTypes, ST->isPacked());
1222 static unsigned hashTypeStructure(const StructType *ST) {
1223 return ST->getNumElements();
1226 inline bool operator<(const StructValType &STV) const {
1227 if (ElTypes < STV.ElTypes) return true;
1228 else if (ElTypes > STV.ElTypes) return false;
1229 else return (int)packed < (int)STV.packed;
1234 static ManagedStatic<TypeMap<StructValType, StructType> > StructTypes;
1236 StructType *StructType::get(const std::vector<const Type*> &ETypes,
1238 StructValType STV(ETypes, isPacked);
1239 StructType *ST = StructTypes->get(STV);
1242 // Value not found. Derive a new type!
1243 ST = (StructType*) new char[sizeof(StructType) +
1244 sizeof(PATypeHandle) * ETypes.size()];
1245 new (ST) StructType(ETypes, isPacked);
1246 StructTypes->add(STV, ST);
1248 #ifdef DEBUG_MERGE_TYPES
1249 DOUT << "Derived new type: " << *ST << "\n";
1256 //===----------------------------------------------------------------------===//
1257 // Pointer Type Factory...
1260 // PointerValType - Define a class to hold the key that goes into the TypeMap
1263 class PointerValType {
1266 PointerValType(const Type *val) : ValTy(val) {}
1268 static PointerValType get(const PointerType *PT) {
1269 return PointerValType(PT->getElementType());
1272 static unsigned hashTypeStructure(const PointerType *PT) {
1273 return getSubElementHash(PT);
1276 bool operator<(const PointerValType &MTV) const {
1277 return ValTy < MTV.ValTy;
1282 static ManagedStatic<TypeMap<PointerValType, PointerType> > PointerTypes;
1284 PointerType *PointerType::get(const Type *ValueType) {
1285 assert(ValueType && "Can't get a pointer to <null> type!");
1286 assert(ValueType != Type::VoidTy &&
1287 "Pointer to void is not valid, use sbyte* instead!");
1288 assert(ValueType != Type::LabelTy && "Pointer to label is not valid!");
1289 PointerValType PVT(ValueType);
1291 PointerType *PT = PointerTypes->get(PVT);
1294 // Value not found. Derive a new type!
1295 PointerTypes->add(PVT, PT = new PointerType(ValueType));
1297 #ifdef DEBUG_MERGE_TYPES
1298 DOUT << "Derived new type: " << *PT << "\n";
1303 //===----------------------------------------------------------------------===//
1304 // Derived Type Refinement Functions
1305 //===----------------------------------------------------------------------===//
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
1310 // is annihilated, because there is no way to get a reference to it ever again.
1312 void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
1313 // 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.
1318 for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
1319 assert(i != 0 && "AbstractTypeUser not in user list!");
1321 --i; // Convert to be in range 0 <= i < size()
1322 assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound?
1324 AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
1326 #ifdef DEBUG_MERGE_TYPES
1327 DOUT << " remAbstractTypeUser[" << (void*)this << ", "
1328 << *this << "][" << i << "] User = " << U << "\n";
1331 if (AbstractTypeUsers.empty() && getRefCount() == 0 && isAbstract()) {
1332 #ifdef DEBUG_MERGE_TYPES
1333 DOUT << "DELETEing unused abstract type: <" << *this
1334 << ">[" << (void*)this << "]" << "\n";
1340 // refineAbstractTypeTo - This function is used when it is discovered that
1341 // 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.
1345 void DerivedType::refineAbstractTypeTo(const Type *NewType) {
1346 assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
1347 assert(this != NewType && "Can't refine to myself!");
1348 assert(ForwardType == 0 && "This type has already been refined!");
1350 // The descriptions may be out of date. Conservatively clear them all!
1351 AbstractTypeDescriptions->clear();
1353 #ifdef DEBUG_MERGE_TYPES
1354 DOUT << "REFINING abstract type [" << (void*)this << " "
1355 << *this << "] to [" << (void*)NewType << " "
1356 << *NewType << "]!\n";
1359 // 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...
1362 PATypeHolder NewTy(NewType);
1364 // Any PATypeHolders referring to this type will now automatically forward to
1365 // the type we are resolved to.
1366 ForwardType = NewType;
1367 if (NewType->isAbstract())
1368 cast<DerivedType>(NewType)->addRef();
1370 // Add a self use of the current type so that we don't delete ourself until
1371 // after the function exits.
1373 PATypeHolder CurrentTy(this);
1375 // 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
1381 // Iterate over all of the uses of this type, invoking callback. Each user
1382 // 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
1387 while (!AbstractTypeUsers.empty() && NewTy != this) {
1388 AbstractTypeUser *User = AbstractTypeUsers.back();
1390 unsigned OldSize = AbstractTypeUsers.size();
1391 #ifdef DEBUG_MERGE_TYPES
1392 DOUT << " REFINING user " << OldSize-1 << "[" << (void*)User
1393 << "] of abstract type [" << (void*)this << " "
1394 << *this << "] to [" << (void*)NewTy.get() << " "
1395 << *NewTy << "]!\n";
1397 User->refineAbstractType(this, NewTy);
1399 assert(AbstractTypeUsers.size() != OldSize &&
1400 "AbsTyUser did not remove self from user list!");
1403 // 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
1409 // notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
1410 // the current type has transitioned from being abstract to being concrete.
1412 void DerivedType::notifyUsesThatTypeBecameConcrete() {
1413 #ifdef DEBUG_MERGE_TYPES
1414 DOUT << "typeIsREFINED type: " << (void*)this << " " << *this << "\n";
1417 unsigned OldSize = AbstractTypeUsers.size();
1418 while (!AbstractTypeUsers.empty()) {
1419 AbstractTypeUser *ATU = AbstractTypeUsers.back();
1420 ATU->typeBecameConcrete(this);
1422 assert(AbstractTypeUsers.size() < OldSize-- &&
1423 "AbstractTypeUser did not remove itself from the use list!");
1427 // refineAbstractType - Called when a contained type is found to be more
1428 // concrete - this could potentially change us from an abstract type to a
1431 void FunctionType::refineAbstractType(const DerivedType *OldType,
1432 const Type *NewType) {
1433 FunctionTypes->RefineAbstractType(this, OldType, NewType);
1436 void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
1437 FunctionTypes->TypeBecameConcrete(this, AbsTy);
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
1445 void ArrayType::refineAbstractType(const DerivedType *OldType,
1446 const Type *NewType) {
1447 ArrayTypes->RefineAbstractType(this, OldType, NewType);
1450 void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
1451 ArrayTypes->TypeBecameConcrete(this, AbsTy);
1454 // refineAbstractType - Called when a contained type is found to be more
1455 // concrete - this could potentially change us from an abstract type to a
1458 void VectorType::refineAbstractType(const DerivedType *OldType,
1459 const Type *NewType) {
1460 VectorTypes->RefineAbstractType(this, OldType, NewType);
1463 void VectorType::typeBecameConcrete(const DerivedType *AbsTy) {
1464 VectorTypes->TypeBecameConcrete(this, AbsTy);
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
1471 void StructType::refineAbstractType(const DerivedType *OldType,
1472 const Type *NewType) {
1473 StructTypes->RefineAbstractType(this, OldType, NewType);
1476 void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
1477 StructTypes->TypeBecameConcrete(this, AbsTy);
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
1484 void PointerType::refineAbstractType(const DerivedType *OldType,
1485 const Type *NewType) {
1486 PointerTypes->RefineAbstractType(this, OldType, NewType);
1489 void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
1490 PointerTypes->TypeBecameConcrete(this, AbsTy);
1493 bool SequentialType::indexValid(const Value *V) const {
1494 if (const IntegerType *IT = dyn_cast<IntegerType>(V->getType()))
1495 return IT->getBitWidth() == 32 || IT->getBitWidth() == 64;
1500 std::ostream &operator<<(std::ostream &OS, const Type *T) {
1502 OS << "<null> value!\n";
1508 std::ostream &operator<<(std::ostream &OS, const Type &T) {