change Type.h to forward declare ArrayRef instead of #including it.
[oota-llvm.git] / lib / VMCore / Type.cpp
1 //===-- Type.cpp - Implement the Type class -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Type class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/SCCIterator.h"
16 #include <algorithm>
17 #include <cstdarg>
18 using namespace llvm;
19
20 // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
21 // created and later destroyed, all in an effort to make sure that there is only
22 // a single canonical version of a type.
23 //
24 // #define DEBUG_MERGE_TYPES 1
25
26 AbstractTypeUser::~AbstractTypeUser() {}
27
28 void AbstractTypeUser::setType(Value *V, const Type *NewTy) {
29   V->VTy = NewTy;
30 }
31
32 //===----------------------------------------------------------------------===//
33 //                         Type Class Implementation
34 //===----------------------------------------------------------------------===//
35
36 /// Because of the way Type subclasses are allocated, this function is necessary
37 /// to use the correct kind of "delete" operator to deallocate the Type object.
38 /// Some type objects (FunctionTy, StructTy) allocate additional space
39 /// after the space for their derived type to hold the contained types array of
40 /// PATypeHandles. Using this allocation scheme means all the PATypeHandles are
41 /// allocated with the type object, decreasing allocations and eliminating the
42 /// need for a std::vector to be used in the Type class itself. 
43 /// @brief Type destruction function
44 void Type::destroy() const {
45   // Nothing calls getForwardedType from here on.
46   if (ForwardType && ForwardType->isAbstract()) {
47     ForwardType->dropRef();
48     ForwardType = NULL;
49   }
50
51   // Structures and Functions allocate their contained types past the end of
52   // the type object itself. These need to be destroyed differently than the
53   // other types.
54   if (this->isFunctionTy() || this->isStructTy()) {
55     // First, make sure we destruct any PATypeHandles allocated by these
56     // subclasses.  They must be manually destructed. 
57     for (unsigned i = 0; i < NumContainedTys; ++i)
58       ContainedTys[i].PATypeHandle::~PATypeHandle();
59
60     // Now call the destructor for the subclass directly because we're going
61     // to delete this as an array of char.
62     if (this->isFunctionTy())
63       static_cast<const FunctionType*>(this)->FunctionType::~FunctionType();
64     else {
65       assert(isStructTy());
66       static_cast<const StructType*>(this)->StructType::~StructType();
67     }
68
69     // Finally, remove the memory as an array deallocation of the chars it was
70     // constructed from.
71     operator delete(const_cast<Type *>(this));
72
73     return;
74   }
75   
76   if (const OpaqueType *opaque_this = dyn_cast<OpaqueType>(this)) {
77     LLVMContextImpl *pImpl = this->getContext().pImpl;
78     pImpl->OpaqueTypes.erase(opaque_this);
79   }
80
81   // For all the other type subclasses, there is either no contained types or 
82   // just one (all Sequentials). For Sequentials, the PATypeHandle is not
83   // allocated past the type object, its included directly in the SequentialType
84   // class. This means we can safely just do "normal" delete of this object and
85   // all the destructors that need to run will be run.
86   delete this; 
87 }
88
89 const Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
90   switch (IDNumber) {
91   case VoidTyID      : return getVoidTy(C);
92   case FloatTyID     : return getFloatTy(C);
93   case DoubleTyID    : return getDoubleTy(C);
94   case X86_FP80TyID  : return getX86_FP80Ty(C);
95   case FP128TyID     : return getFP128Ty(C);
96   case PPC_FP128TyID : return getPPC_FP128Ty(C);
97   case LabelTyID     : return getLabelTy(C);
98   case MetadataTyID  : return getMetadataTy(C);
99   case X86_MMXTyID   : return getX86_MMXTy(C);
100   default:
101     return 0;
102   }
103 }
104
105 /// getScalarType - If this is a vector type, return the element type,
106 /// otherwise return this.
107 const Type *Type::getScalarType() const {
108   if (const VectorType *VTy = dyn_cast<VectorType>(this))
109     return VTy->getElementType();
110   return this;
111 }
112
113 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
114 bool Type::isIntegerTy(unsigned Bitwidth) const {
115   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
116 }
117
118 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
119 /// integer types.
120 ///
121 bool Type::isIntOrIntVectorTy() const {
122   if (isIntegerTy())
123     return true;
124   if (ID != Type::VectorTyID) return false;
125   
126   return cast<VectorType>(this)->getElementType()->isIntegerTy();
127 }
128
129 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP types.
130 ///
131 bool Type::isFPOrFPVectorTy() const {
132   if (ID == Type::FloatTyID || ID == Type::DoubleTyID || 
133       ID == Type::FP128TyID || ID == Type::X86_FP80TyID || 
134       ID == Type::PPC_FP128TyID)
135     return true;
136   if (ID != Type::VectorTyID) return false;
137   
138   return cast<VectorType>(this)->getElementType()->isFloatingPointTy();
139 }
140
141 // canLosslesslyBitCastTo - Return true if this type can be converted to
142 // 'Ty' without any reinterpretation of bits.  For example, i8* to i32*.
143 //
144 bool Type::canLosslesslyBitCastTo(const Type *Ty) const {
145   // Identity cast means no change so return true
146   if (this == Ty) 
147     return true;
148   
149   // They are not convertible unless they are at least first class types
150   if (!this->isFirstClassType() || !Ty->isFirstClassType())
151     return false;
152
153   // Vector -> Vector conversions are always lossless if the two vector types
154   // have the same size, otherwise not.  Also, 64-bit vector types can be
155   // converted to x86mmx.
156   if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
157     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
158       return thisPTy->getBitWidth() == thatPTy->getBitWidth();
159     if (Ty->getTypeID() == Type::X86_MMXTyID &&
160         thisPTy->getBitWidth() == 64)
161       return true;
162   }
163
164   if (this->getTypeID() == Type::X86_MMXTyID)
165     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
166       if (thatPTy->getBitWidth() == 64)
167         return true;
168
169   // 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.
172   if (this->isPointerTy())
173     return Ty->isPointerTy();
174   return false;  // Other types have no identity values
175 }
176
177 bool Type::isEmptyTy() const {
178   const ArrayType *ATy = dyn_cast<ArrayType>(this);
179   if (ATy) {
180     unsigned NumElements = ATy->getNumElements();
181     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
182   }
183
184   const StructType *STy = dyn_cast<StructType>(this);
185   if (STy) {
186     unsigned NumElements = STy->getNumElements();
187     for (unsigned i = 0; i < NumElements; ++i)
188       if (!STy->getElementType(i)->isEmptyTy())
189         return false;
190     return true;
191   }
192
193   return false;
194 }
195
196 unsigned Type::getPrimitiveSizeInBits() const {
197   switch (getTypeID()) {
198   case Type::FloatTyID: return 32;
199   case Type::DoubleTyID: return 64;
200   case Type::X86_FP80TyID: return 80;
201   case Type::FP128TyID: return 128;
202   case Type::PPC_FP128TyID: return 128;
203   case Type::X86_MMXTyID: return 64;
204   case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
205   case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
206   default: return 0;
207   }
208 }
209
210 /// getScalarSizeInBits - If this is a vector type, return the
211 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
212 /// getPrimitiveSizeInBits value for this type.
213 unsigned Type::getScalarSizeInBits() const {
214   return getScalarType()->getPrimitiveSizeInBits();
215 }
216
217 /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
218 /// is only valid on floating point types.  If the FP type does not
219 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
220 int Type::getFPMantissaWidth() const {
221   if (const VectorType *VTy = dyn_cast<VectorType>(this))
222     return VTy->getElementType()->getFPMantissaWidth();
223   assert(isFloatingPointTy() && "Not a floating point type!");
224   if (ID == FloatTyID) return 24;
225   if (ID == DoubleTyID) return 53;
226   if (ID == X86_FP80TyID) return 64;
227   if (ID == FP128TyID) return 113;
228   assert(ID == PPC_FP128TyID && "unknown fp type");
229   return -1;
230 }
231
232 /// isSizedDerivedType - Derived types like structures and arrays are sized
233 /// iff all of the members of the type are sized as well.  Since asking for
234 /// their size is relatively uncommon, move this operation out of line.
235 bool Type::isSizedDerivedType() const {
236   if (this->isIntegerTy())
237     return true;
238
239   if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
240     return ATy->getElementType()->isSized();
241
242   if (const VectorType *VTy = dyn_cast<VectorType>(this))
243     return VTy->getElementType()->isSized();
244
245   if (!this->isStructTy()) 
246     return false;
247
248   // Okay, our struct is sized if all of the elements are...
249   for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
250     if (!(*I)->isSized()) 
251       return false;
252
253   return true;
254 }
255
256 /// getForwardedTypeInternal - This method is used to implement the union-find
257 /// algorithm for when a type is being forwarded to another type.
258 const Type *Type::getForwardedTypeInternal() const {
259   assert(ForwardType && "This type is not being forwarded to another type!");
260
261   // Check to see if the forwarded type has been forwarded on.  If so, collapse
262   // the forwarding links.
263   const Type *RealForwardedType = ForwardType->getForwardedType();
264   if (!RealForwardedType)
265     return ForwardType;  // No it's not forwarded again
266
267   // Yes, it is forwarded again.  First thing, add the reference to the new
268   // forward type.
269   if (RealForwardedType->isAbstract())
270     RealForwardedType->addRef();
271
272   // Now drop the old reference.  This could cause ForwardType to get deleted.
273   // ForwardType must be abstract because only abstract types can have their own
274   // ForwardTypes.
275   ForwardType->dropRef();
276
277   // Return the updated type.
278   ForwardType = RealForwardedType;
279   return ForwardType;
280 }
281
282 void Type::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
283   llvm_unreachable("Attempting to refine a derived type!");
284 }
285 void Type::typeBecameConcrete(const DerivedType *AbsTy) {
286   llvm_unreachable("DerivedType is already a concrete type!");
287 }
288
289
290 std::string Type::getDescription() const {
291   LLVMContextImpl *pImpl = getContext().pImpl;
292   TypePrinting &Map =
293     isAbstract() ?
294       pImpl->AbstractTypeDescriptions :
295       pImpl->ConcreteTypeDescriptions;
296   
297   std::string DescStr;
298   raw_string_ostream DescOS(DescStr);
299   Map.print(this, DescOS);
300   return DescOS.str();
301 }
302
303
304 bool StructType::indexValid(const Value *V) const {
305   // Structure indexes require 32-bit integer constants.
306   if (V->getType()->isIntegerTy(32))
307     if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
308       return indexValid(CU->getZExtValue());
309   return false;
310 }
311
312 bool StructType::indexValid(unsigned V) const {
313   return V < NumContainedTys;
314 }
315
316 // getTypeAtIndex - Given an index value into the type, return the type of the
317 // element.  For a structure type, this must be a constant value...
318 //
319 const Type *StructType::getTypeAtIndex(const Value *V) const {
320   unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
321   return getTypeAtIndex(Idx);
322 }
323
324 const Type *StructType::getTypeAtIndex(unsigned Idx) const {
325   assert(indexValid(Idx) && "Invalid structure index!");
326   return ContainedTys[Idx];
327 }
328
329
330 //===----------------------------------------------------------------------===//
331 //                          Primitive 'Type' data
332 //===----------------------------------------------------------------------===//
333
334 const Type *Type::getVoidTy(LLVMContext &C) {
335   return &C.pImpl->VoidTy;
336 }
337
338 const Type *Type::getLabelTy(LLVMContext &C) {
339   return &C.pImpl->LabelTy;
340 }
341
342 const Type *Type::getFloatTy(LLVMContext &C) {
343   return &C.pImpl->FloatTy;
344 }
345
346 const Type *Type::getDoubleTy(LLVMContext &C) {
347   return &C.pImpl->DoubleTy;
348 }
349
350 const Type *Type::getMetadataTy(LLVMContext &C) {
351   return &C.pImpl->MetadataTy;
352 }
353
354 const Type *Type::getX86_FP80Ty(LLVMContext &C) {
355   return &C.pImpl->X86_FP80Ty;
356 }
357
358 const Type *Type::getFP128Ty(LLVMContext &C) {
359   return &C.pImpl->FP128Ty;
360 }
361
362 const Type *Type::getPPC_FP128Ty(LLVMContext &C) {
363   return &C.pImpl->PPC_FP128Ty;
364 }
365
366 const Type *Type::getX86_MMXTy(LLVMContext &C) {
367   return &C.pImpl->X86_MMXTy;
368 }
369
370 const IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
371   return IntegerType::get(C, N);
372 }
373
374 const IntegerType *Type::getInt1Ty(LLVMContext &C) {
375   return &C.pImpl->Int1Ty;
376 }
377
378 const IntegerType *Type::getInt8Ty(LLVMContext &C) {
379   return &C.pImpl->Int8Ty;
380 }
381
382 const IntegerType *Type::getInt16Ty(LLVMContext &C) {
383   return &C.pImpl->Int16Ty;
384 }
385
386 const IntegerType *Type::getInt32Ty(LLVMContext &C) {
387   return &C.pImpl->Int32Ty;
388 }
389
390 const IntegerType *Type::getInt64Ty(LLVMContext &C) {
391   return &C.pImpl->Int64Ty;
392 }
393
394 const PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
395   return getFloatTy(C)->getPointerTo(AS);
396 }
397
398 const PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
399   return getDoubleTy(C)->getPointerTo(AS);
400 }
401
402 const PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
403   return getX86_FP80Ty(C)->getPointerTo(AS);
404 }
405
406 const PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
407   return getFP128Ty(C)->getPointerTo(AS);
408 }
409
410 const PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
411   return getPPC_FP128Ty(C)->getPointerTo(AS);
412 }
413
414 const PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
415   return getX86_MMXTy(C)->getPointerTo(AS);
416 }
417
418 const PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
419   return getIntNTy(C, N)->getPointerTo(AS);
420 }
421
422 const PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
423   return getInt1Ty(C)->getPointerTo(AS);
424 }
425
426 const PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
427   return getInt8Ty(C)->getPointerTo(AS);
428 }
429
430 const PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
431   return getInt16Ty(C)->getPointerTo(AS);
432 }
433
434 const PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
435   return getInt32Ty(C)->getPointerTo(AS);
436 }
437
438 const PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
439   return getInt64Ty(C)->getPointerTo(AS);
440 }
441
442 //===----------------------------------------------------------------------===//
443 //                          Derived Type Constructors
444 //===----------------------------------------------------------------------===//
445
446 /// isValidReturnType - Return true if the specified type is valid as a return
447 /// type.
448 bool FunctionType::isValidReturnType(const Type *RetTy) {
449   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
450          !RetTy->isMetadataTy();
451 }
452
453 /// isValidArgumentType - Return true if the specified type is valid as an
454 /// argument type.
455 bool FunctionType::isValidArgumentType(const Type *ArgTy) {
456   return ArgTy->isFirstClassType() || ArgTy->isOpaqueTy();
457 }
458
459 FunctionType::FunctionType(const Type *Result,
460                            ArrayRef<const Type*> Params,
461                            bool IsVarArgs)
462   : DerivedType(Result->getContext(), FunctionTyID) {
463   ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
464   NumContainedTys = Params.size() + 1; // + 1 for result type
465   assert(isValidReturnType(Result) && "invalid return type for function");
466   setSubclassData(IsVarArgs);
467
468   bool isAbstract = Result->isAbstract();
469   new (&ContainedTys[0]) PATypeHandle(Result, this);
470
471   for (unsigned i = 0; i != Params.size(); ++i) {
472     assert(isValidArgumentType(Params[i]) &&
473            "Not a valid type for function argument!");
474     new (&ContainedTys[i+1]) PATypeHandle(Params[i], this);
475     isAbstract |= Params[i]->isAbstract();
476   }
477
478   // Calculate whether or not this type is abstract
479   setAbstract(isAbstract);
480 }
481
482 StructType::StructType(LLVMContext &C, 
483                        ArrayRef<const Type*> Types, bool isPacked)
484   : CompositeType(C, StructTyID) {
485   ContainedTys = reinterpret_cast<PATypeHandle*>(this + 1);
486   NumContainedTys = Types.size();
487   setSubclassData(isPacked);
488   bool isAbstract = false;
489   for (unsigned i = 0; i < Types.size(); ++i) {
490     assert(Types[i] && "<null> type for structure field!");
491     assert(isValidElementType(Types[i]) &&
492            "Invalid type for structure element!");
493     new (&ContainedTys[i]) PATypeHandle(Types[i], this);
494     isAbstract |= Types[i]->isAbstract();
495   }
496
497   // Calculate whether or not this type is abstract
498   setAbstract(isAbstract);
499 }
500
501 ArrayType::ArrayType(const Type *ElType, uint64_t NumEl)
502   : SequentialType(ArrayTyID, ElType) {
503   NumElements = NumEl;
504
505   // Calculate whether or not this type is abstract
506   setAbstract(ElType->isAbstract());
507 }
508
509 VectorType::VectorType(const Type *ElType, unsigned NumEl)
510   : SequentialType(VectorTyID, ElType) {
511   NumElements = NumEl;
512   setAbstract(ElType->isAbstract());
513   assert(NumEl > 0 && "NumEl of a VectorType must be greater than 0");
514   assert(isValidElementType(ElType) &&
515          "Elements of a VectorType must be a primitive type");
516
517 }
518
519
520 PointerType::PointerType(const Type *E, unsigned AddrSpace)
521   : SequentialType(PointerTyID, E) {
522   setSubclassData(AddrSpace);
523   // Calculate whether or not this type is abstract
524   setAbstract(E->isAbstract());
525 }
526
527 OpaqueType::OpaqueType(LLVMContext &C) : DerivedType(C, OpaqueTyID) {
528   setAbstract(true);
529 #ifdef DEBUG_MERGE_TYPES
530   DEBUG(dbgs() << "Derived new type: " << *this << "\n");
531 #endif
532 }
533
534 void PATypeHolder::destroy() {
535   Ty = 0;
536 }
537
538 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
539 // another (more concrete) type, we must eliminate all references to other
540 // types, to avoid some circular reference problems.
541 void DerivedType::dropAllTypeUses() {
542   if (NumContainedTys != 0) {
543     // The type must stay abstract.  To do this, we insert a pointer to a type
544     // that will never get resolved, thus will always be abstract.
545     ContainedTys[0] = getContext().pImpl->AlwaysOpaqueTy;
546
547     // Change the rest of the types to be Int32Ty's.  It doesn't matter what we
548     // pick so long as it doesn't point back to this type.  We choose something
549     // concrete to avoid overhead for adding to AbstractTypeUser lists and
550     // stuff.
551     const Type *ConcreteTy = Type::getInt32Ty(getContext());
552     for (unsigned i = 1, e = NumContainedTys; i != e; ++i)
553       ContainedTys[i] = ConcreteTy;
554   }
555 }
556
557
558 namespace {
559
560 /// TypePromotionGraph and graph traits - this is designed to allow us to do
561 /// efficient SCC processing of type graphs.  This is the exact same as
562 /// GraphTraits<Type*>, except that we pretend that concrete types have no
563 /// children to avoid processing them.
564 struct TypePromotionGraph {
565   Type *Ty;
566   TypePromotionGraph(Type *T) : Ty(T) {}
567 };
568
569 }
570
571 namespace llvm {
572   template <> struct GraphTraits<TypePromotionGraph> {
573     typedef Type NodeType;
574     typedef Type::subtype_iterator ChildIteratorType;
575
576     static inline NodeType *getEntryNode(TypePromotionGraph G) { return G.Ty; }
577     static inline ChildIteratorType child_begin(NodeType *N) {
578       if (N->isAbstract())
579         return N->subtype_begin();
580       // No need to process children of concrete types.
581       return N->subtype_end();
582     }
583     static inline ChildIteratorType child_end(NodeType *N) {
584       return N->subtype_end();
585     }
586   };
587 }
588
589
590 // PromoteAbstractToConcrete - This is a recursive function that walks a type
591 // graph calculating whether or not a type is abstract.
592 //
593 void Type::PromoteAbstractToConcrete() {
594   if (!isAbstract()) return;
595
596   scc_iterator<TypePromotionGraph> SI = scc_begin(TypePromotionGraph(this));
597   scc_iterator<TypePromotionGraph> SE = scc_end  (TypePromotionGraph(this));
598
599   for (; SI != SE; ++SI) {
600     std::vector<Type*> &SCC = *SI;
601
602     // Concrete types are leaves in the tree.  Since an SCC will either be all
603     // abstract or all concrete, we only need to check one type.
604     if (!SCC[0]->isAbstract()) continue;
605     
606     if (SCC[0]->isOpaqueTy())
607       return;     // Not going to be concrete, sorry.
608
609     // If all of the children of all of the types in this SCC are concrete,
610     // then this SCC is now concrete as well.  If not, neither this SCC, nor
611     // any parent SCCs will be concrete, so we might as well just exit.
612     for (unsigned i = 0, e = SCC.size(); i != e; ++i)
613       for (Type::subtype_iterator CI = SCC[i]->subtype_begin(),
614              E = SCC[i]->subtype_end(); CI != E; ++CI)
615         if ((*CI)->isAbstract())
616           // If the child type is in our SCC, it doesn't make the entire SCC
617           // abstract unless there is a non-SCC abstract type.
618           if (std::find(SCC.begin(), SCC.end(), *CI) == SCC.end())
619             return;               // Not going to be concrete, sorry.
620
621     // Okay, we just discovered this whole SCC is now concrete, mark it as
622     // such!
623     for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
624       assert(SCC[i]->isAbstract() && "Why are we processing concrete types?");
625
626       SCC[i]->setAbstract(false);
627     }
628
629     for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
630       assert(!SCC[i]->isAbstract() && "Concrete type became abstract?");
631       // The type just became concrete, notify all users!
632       cast<DerivedType>(SCC[i])->notifyUsesThatTypeBecameConcrete();
633     }
634   }
635 }
636
637
638 //===----------------------------------------------------------------------===//
639 //                      Type Structural Equality Testing
640 //===----------------------------------------------------------------------===//
641
642 // TypesEqual - Two types are considered structurally equal if they have the
643 // same "shape": Every level and element of the types have identical primitive
644 // ID's, and the graphs have the same edges/nodes in them.  Nodes do not have to
645 // be pointer equals to be equivalent though.  This uses an optimistic algorithm
646 // that assumes that two graphs are the same until proven otherwise.
647 //
648 static bool TypesEqual(const Type *Ty, const Type *Ty2,
649                        std::map<const Type *, const Type *> &EqTypes) {
650   if (Ty == Ty2) return true;
651   if (Ty->getTypeID() != Ty2->getTypeID()) return false;
652   if (Ty->isOpaqueTy())
653     return false;  // Two unequal opaque types are never equal
654
655   std::map<const Type*, const Type*>::iterator It = EqTypes.find(Ty);
656   if (It != EqTypes.end())
657     return It->second == Ty2;    // Looping back on a type, check for equality
658
659   // Otherwise, add the mapping to the table to make sure we don't get
660   // recursion on the types...
661   EqTypes.insert(It, std::make_pair(Ty, Ty2));
662
663   // Two really annoying special cases that breaks an otherwise nice simple
664   // algorithm is the fact that arraytypes have sizes that differentiates types,
665   // and that function types can be varargs or not.  Consider this now.
666   //
667   if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
668     const IntegerType *ITy2 = cast<IntegerType>(Ty2);
669     return ITy->getBitWidth() == ITy2->getBitWidth();
670   }
671   
672   if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
673     const PointerType *PTy2 = cast<PointerType>(Ty2);
674     return PTy->getAddressSpace() == PTy2->getAddressSpace() &&
675            TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
676   }
677   
678   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
679     const StructType *STy2 = cast<StructType>(Ty2);
680     if (STy->getNumElements() != STy2->getNumElements()) return false;
681     if (STy->isPacked() != STy2->isPacked()) return false;
682     for (unsigned i = 0, e = STy2->getNumElements(); i != e; ++i)
683       if (!TypesEqual(STy->getElementType(i), STy2->getElementType(i), EqTypes))
684         return false;
685     return true;
686   }
687   
688   if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
689     const ArrayType *ATy2 = cast<ArrayType>(Ty2);
690     return ATy->getNumElements() == ATy2->getNumElements() &&
691            TypesEqual(ATy->getElementType(), ATy2->getElementType(), EqTypes);
692   }
693   
694   if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
695     const VectorType *PTy2 = cast<VectorType>(Ty2);
696     return PTy->getNumElements() == PTy2->getNumElements() &&
697            TypesEqual(PTy->getElementType(), PTy2->getElementType(), EqTypes);
698   }
699   
700   if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
701     const FunctionType *FTy2 = cast<FunctionType>(Ty2);
702     if (FTy->isVarArg() != FTy2->isVarArg() ||
703         FTy->getNumParams() != FTy2->getNumParams() ||
704         !TypesEqual(FTy->getReturnType(), FTy2->getReturnType(), EqTypes))
705       return false;
706     for (unsigned i = 0, e = FTy2->getNumParams(); i != e; ++i) {
707       if (!TypesEqual(FTy->getParamType(i), FTy2->getParamType(i), EqTypes))
708         return false;
709     }
710     return true;
711   }
712   
713   llvm_unreachable("Unknown derived type!");
714   return false;
715 }
716
717 namespace llvm { // in namespace llvm so findable by ADL
718 static bool TypesEqual(const Type *Ty, const Type *Ty2) {
719   std::map<const Type *, const Type *> EqTypes;
720   return ::TypesEqual(Ty, Ty2, EqTypes);
721 }
722 }
723
724 // AbstractTypeHasCycleThrough - Return true there is a path from CurTy to
725 // TargetTy in the type graph.  We know that Ty is an abstract type, so if we
726 // ever reach a non-abstract type, we know that we don't need to search the
727 // subgraph.
728 static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
729                                 SmallPtrSet<const Type*, 128> &VisitedTypes) {
730   if (TargetTy == CurTy) return true;
731   if (!CurTy->isAbstract()) return false;
732
733   if (!VisitedTypes.insert(CurTy))
734     return false;  // Already been here.
735
736   for (Type::subtype_iterator I = CurTy->subtype_begin(),
737        E = CurTy->subtype_end(); I != E; ++I)
738     if (AbstractTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
739       return true;
740   return false;
741 }
742
743 static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
744                                 SmallPtrSet<const Type*, 128> &VisitedTypes) {
745   if (TargetTy == CurTy) return true;
746
747   if (!VisitedTypes.insert(CurTy))
748     return false;  // Already been here.
749
750   for (Type::subtype_iterator I = CurTy->subtype_begin(),
751        E = CurTy->subtype_end(); I != E; ++I)
752     if (ConcreteTypeHasCycleThrough(TargetTy, *I, VisitedTypes))
753       return true;
754   return false;
755 }
756
757 /// TypeHasCycleThroughItself - Return true if the specified type has
758 /// a cycle back to itself.
759
760 namespace llvm { // in namespace llvm so it's findable by ADL
761 static bool TypeHasCycleThroughItself(const Type *Ty) {
762   SmallPtrSet<const Type*, 128> VisitedTypes;
763
764   if (Ty->isAbstract()) {  // Optimized case for abstract types.
765     for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
766          I != E; ++I)
767       if (AbstractTypeHasCycleThrough(Ty, *I, VisitedTypes))
768         return true;
769   } else {
770     for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
771          I != E; ++I)
772       if (ConcreteTypeHasCycleThrough(Ty, *I, VisitedTypes))
773         return true;
774   }
775   return false;
776 }
777 }
778
779 //===----------------------------------------------------------------------===//
780 // Function Type Factory and Value Class...
781 //
782 const IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
783   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
784   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
785
786   // Check for the built-in integer types
787   switch (NumBits) {
788   case  1: return cast<IntegerType>(Type::getInt1Ty(C));
789   case  8: return cast<IntegerType>(Type::getInt8Ty(C));
790   case 16: return cast<IntegerType>(Type::getInt16Ty(C));
791   case 32: return cast<IntegerType>(Type::getInt32Ty(C));
792   case 64: return cast<IntegerType>(Type::getInt64Ty(C));
793   default: 
794     break;
795   }
796
797   LLVMContextImpl *pImpl = C.pImpl;
798   
799   IntegerValType IVT(NumBits);
800   IntegerType *ITy = 0;
801   
802   // First, see if the type is already in the table, for which
803   // a reader lock suffices.
804   ITy = pImpl->IntegerTypes.get(IVT);
805     
806   if (!ITy) {
807     // Value not found.  Derive a new type!
808     ITy = new IntegerType(C, NumBits);
809     pImpl->IntegerTypes.add(IVT, ITy);
810   }
811 #ifdef DEBUG_MERGE_TYPES
812   DEBUG(dbgs() << "Derived new type: " << *ITy << "\n");
813 #endif
814   return ITy;
815 }
816
817 bool IntegerType::isPowerOf2ByteWidth() const {
818   unsigned BitWidth = getBitWidth();
819   return (BitWidth > 7) && isPowerOf2_32(BitWidth);
820 }
821
822 APInt IntegerType::getMask() const {
823   return APInt::getAllOnesValue(getBitWidth());
824 }
825
826 FunctionValType FunctionValType::get(const FunctionType *FT) {
827   // Build up a FunctionValType
828   std::vector<const Type *> ParamTypes;
829   ParamTypes.reserve(FT->getNumParams());
830   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
831     ParamTypes.push_back(FT->getParamType(i));
832   return FunctionValType(FT->getReturnType(), ParamTypes, FT->isVarArg());
833 }
834
835 FunctionType *FunctionType::get(const Type *Result, bool isVarArg) {
836   return get(Result, ArrayRef<const Type *>(), isVarArg);
837 }
838
839 // FunctionType::get - The factory function for the FunctionType class...
840 FunctionType *FunctionType::get(const Type *ReturnType,
841                                 ArrayRef<const Type*> Params,
842                                 bool isVarArg) {
843   FunctionValType VT(ReturnType, Params, isVarArg);
844   FunctionType *FT = 0;
845   
846   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
847   
848   FT = pImpl->FunctionTypes.get(VT);
849   
850   if (!FT) {
851     FT = (FunctionType*) operator new(sizeof(FunctionType) +
852                                     sizeof(PATypeHandle)*(Params.size()+1));
853     new (FT) FunctionType(ReturnType, Params, isVarArg);
854     pImpl->FunctionTypes.add(VT, FT);
855   }
856
857 #ifdef DEBUG_MERGE_TYPES
858   DEBUG(dbgs() << "Derived new type: " << FT << "\n");
859 #endif
860   return FT;
861 }
862
863 ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
864   assert(ElementType && "Can't get array of <null> types!");
865   assert(isValidElementType(ElementType) && "Invalid type for array element!");
866
867   ArrayValType AVT(ElementType, NumElements);
868   ArrayType *AT = 0;
869
870   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
871   
872   AT = pImpl->ArrayTypes.get(AVT);
873       
874   if (!AT) {
875     // Value not found.  Derive a new type!
876     pImpl->ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements));
877   }
878 #ifdef DEBUG_MERGE_TYPES
879   DEBUG(dbgs() << "Derived new type: " << *AT << "\n");
880 #endif
881   return AT;
882 }
883
884 bool ArrayType::isValidElementType(const Type *ElemTy) {
885   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
886          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
887 }
888
889 VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) {
890   assert(ElementType && "Can't get vector of <null> types!");
891
892   VectorValType PVT(ElementType, NumElements);
893   VectorType *PT = 0;
894   
895   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
896   
897   PT = pImpl->VectorTypes.get(PVT);
898     
899   if (!PT) {
900     pImpl->VectorTypes.add(PVT, PT = new VectorType(ElementType, NumElements));
901   }
902 #ifdef DEBUG_MERGE_TYPES
903   DEBUG(dbgs() << "Derived new type: " << *PT << "\n");
904 #endif
905   return PT;
906 }
907
908 bool VectorType::isValidElementType(const Type *ElemTy) {
909   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
910          ElemTy->isOpaqueTy();
911 }
912
913 //===----------------------------------------------------------------------===//
914 // Struct Type Factory.
915 //
916
917 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
918   return get(Context, llvm::ArrayRef<const Type*>(), isPacked);
919 }
920
921
922 StructType *StructType::get(LLVMContext &Context,
923                             ArrayRef<const Type*> ETypes, 
924                             bool isPacked) {
925   StructValType STV(ETypes, isPacked);
926   StructType *ST = 0;
927   
928   LLVMContextImpl *pImpl = Context.pImpl;
929   
930   ST = pImpl->StructTypes.get(STV);
931     
932   if (!ST) {
933     // Value not found.  Derive a new type!
934     ST = (StructType*) operator new(sizeof(StructType) +
935                                     sizeof(PATypeHandle) * ETypes.size());
936     new (ST) StructType(Context, ETypes, isPacked);
937     pImpl->StructTypes.add(STV, ST);
938   }
939 #ifdef DEBUG_MERGE_TYPES
940   DEBUG(dbgs() << "Derived new type: " << *ST << "\n");
941 #endif
942   return ST;
943 }
944
945 StructType *StructType::get(LLVMContext &Context, const Type *type, ...) {
946   va_list ap;
947   std::vector<const llvm::Type*> StructFields;
948   va_start(ap, type);
949   while (type) {
950     StructFields.push_back(type);
951     type = va_arg(ap, llvm::Type*);
952   }
953   return llvm::StructType::get(Context, StructFields);
954 }
955
956 bool StructType::isValidElementType(const Type *ElemTy) {
957   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
958          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
959 }
960
961
962 //===----------------------------------------------------------------------===//
963 // Pointer Type Factory...
964 //
965
966 PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) {
967   assert(ValueType && "Can't get a pointer to <null> type!");
968   assert(ValueType->getTypeID() != VoidTyID &&
969          "Pointer to void is not valid, use i8* instead!");
970   assert(isValidElementType(ValueType) && "Invalid type for pointer element!");
971   PointerValType PVT(ValueType, AddressSpace);
972
973   PointerType *PT = 0;
974   
975   LLVMContextImpl *pImpl = ValueType->getContext().pImpl;
976   
977   PT = pImpl->PointerTypes.get(PVT);
978   
979   if (!PT) {
980     // Value not found.  Derive a new type!
981     pImpl->PointerTypes.add(PVT, PT = new PointerType(ValueType, AddressSpace));
982   }
983 #ifdef DEBUG_MERGE_TYPES
984   DEBUG(dbgs() << "Derived new type: " << *PT << "\n");
985 #endif
986   return PT;
987 }
988
989 const PointerType *Type::getPointerTo(unsigned addrs) const {
990   return PointerType::get(this, addrs);
991 }
992
993 bool PointerType::isValidElementType(const Type *ElemTy) {
994   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
995          !ElemTy->isMetadataTy();
996 }
997
998
999 //===----------------------------------------------------------------------===//
1000 // Opaque Type Factory...
1001 //
1002
1003 OpaqueType *OpaqueType::get(LLVMContext &C) {
1004   OpaqueType *OT = new OpaqueType(C);       // All opaque types are distinct.
1005   LLVMContextImpl *pImpl = C.pImpl;
1006   pImpl->OpaqueTypes.insert(OT);
1007   return OT;
1008 }
1009
1010
1011
1012 //===----------------------------------------------------------------------===//
1013 //                     Derived Type Refinement Functions
1014 //===----------------------------------------------------------------------===//
1015
1016 // addAbstractTypeUser - Notify an abstract type that there is a new user of
1017 // it.  This function is called primarily by the PATypeHandle class.
1018 void Type::addAbstractTypeUser(AbstractTypeUser *U) const {
1019   assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
1020   AbstractTypeUsers.push_back(U);
1021 }
1022
1023
1024 // removeAbstractTypeUser - Notify an abstract type that a user of the class
1025 // no longer has a handle to the type.  This function is called primarily by
1026 // the PATypeHandle class.  When there are no users of the abstract type, it
1027 // is annihilated, because there is no way to get a reference to it ever again.
1028 //
1029 void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
1030   
1031   // Search from back to front because we will notify users from back to
1032   // front.  Also, it is likely that there will be a stack like behavior to
1033   // users that register and unregister users.
1034   //
1035   unsigned i;
1036   for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
1037     assert(i != 0 && "AbstractTypeUser not in user list!");
1038
1039   --i;  // Convert to be in range 0 <= i < size()
1040   assert(i < AbstractTypeUsers.size() && "Index out of range!");  // Wraparound?
1041
1042   AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
1043
1044 #ifdef DEBUG_MERGE_TYPES
1045   DEBUG(dbgs() << "  remAbstractTypeUser[" << (void*)this << ", "
1046                << *this << "][" << i << "] User = " << U << "\n");
1047 #endif
1048
1049   if (AbstractTypeUsers.empty() && getRefCount() == 0 && isAbstract()) {
1050 #ifdef DEBUG_MERGE_TYPES
1051     DEBUG(dbgs() << "DELETEing unused abstract type: <" << *this
1052                  << ">[" << (void*)this << "]" << "\n");
1053 #endif
1054   
1055     this->destroy();
1056   }
1057 }
1058
1059 // refineAbstractTypeTo - This function is used when it is discovered
1060 // that the 'this' abstract type is actually equivalent to the NewType
1061 // specified. This causes all users of 'this' to switch to reference the more 
1062 // concrete type NewType and for 'this' to be deleted.  Only used for internal
1063 // callers.
1064 //
1065 void DerivedType::refineAbstractTypeTo(const Type *NewType) {
1066   assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
1067   assert(this != NewType && "Can't refine to myself!");
1068   assert(ForwardType == 0 && "This type has already been refined!");
1069
1070   LLVMContextImpl *pImpl = getContext().pImpl;
1071
1072   // The descriptions may be out of date.  Conservatively clear them all!
1073   pImpl->AbstractTypeDescriptions.clear();
1074
1075 #ifdef DEBUG_MERGE_TYPES
1076   DEBUG(dbgs() << "REFINING abstract type [" << (void*)this << " "
1077                << *this << "] to [" << (void*)NewType << " "
1078                << *NewType << "]!\n");
1079 #endif
1080
1081   // Make sure to put the type to be refined to into a holder so that if IT gets
1082   // refined, that we will not continue using a dead reference...
1083   //
1084   PATypeHolder NewTy(NewType);
1085   // Any PATypeHolders referring to this type will now automatically forward to
1086   // the type we are resolved to.
1087   ForwardType = NewType;
1088   if (ForwardType->isAbstract())
1089     ForwardType->addRef();
1090
1091   // Add a self use of the current type so that we don't delete ourself until
1092   // after the function exits.
1093   //
1094   PATypeHolder CurrentTy(this);
1095
1096   // To make the situation simpler, we ask the subclass to remove this type from
1097   // the type map, and to replace any type uses with uses of non-abstract types.
1098   // This dramatically limits the amount of recursive type trouble we can find
1099   // ourselves in.
1100   dropAllTypeUses();
1101
1102   // Iterate over all of the uses of this type, invoking callback.  Each user
1103   // should remove itself from our use list automatically.  We have to check to
1104   // make sure that NewTy doesn't _become_ 'this'.  If it does, resolving types
1105   // will not cause users to drop off of the use list.  If we resolve to ourself
1106   // we succeed!
1107   //
1108   while (!AbstractTypeUsers.empty() && NewTy != this) {
1109     AbstractTypeUser *User = AbstractTypeUsers.back();
1110
1111     unsigned OldSize = AbstractTypeUsers.size(); (void)OldSize;
1112 #ifdef DEBUG_MERGE_TYPES
1113     DEBUG(dbgs() << " REFINING user " << OldSize-1 << "[" << (void*)User
1114                  << "] of abstract type [" << (void*)this << " "
1115                  << *this << "] to [" << (void*)NewTy.get() << " "
1116                  << *NewTy << "]!\n");
1117 #endif
1118     User->refineAbstractType(this, NewTy);
1119
1120     assert(AbstractTypeUsers.size() != OldSize &&
1121            "AbsTyUser did not remove self from user list!");
1122   }
1123
1124   // If we were successful removing all users from the type, 'this' will be
1125   // deleted when the last PATypeHolder is destroyed or updated from this type.
1126   // This may occur on exit of this function, as the CurrentTy object is
1127   // destroyed.
1128 }
1129
1130 // notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
1131 // the current type has transitioned from being abstract to being concrete.
1132 //
1133 void DerivedType::notifyUsesThatTypeBecameConcrete() {
1134 #ifdef DEBUG_MERGE_TYPES
1135   DEBUG(dbgs() << "typeIsREFINED type: " << (void*)this << " " << *this <<"\n");
1136 #endif
1137
1138   unsigned OldSize = AbstractTypeUsers.size(); (void)OldSize;
1139   while (!AbstractTypeUsers.empty()) {
1140     AbstractTypeUser *ATU = AbstractTypeUsers.back();
1141     ATU->typeBecameConcrete(this);
1142
1143     assert(AbstractTypeUsers.size() < OldSize-- &&
1144            "AbstractTypeUser did not remove itself from the use list!");
1145   }
1146 }
1147
1148 // refineAbstractType - Called when a contained type is found to be more
1149 // concrete - this could potentially change us from an abstract type to a
1150 // concrete type.
1151 //
1152 void FunctionType::refineAbstractType(const DerivedType *OldType,
1153                                       const Type *NewType) {
1154   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1155   pImpl->FunctionTypes.RefineAbstractType(this, OldType, NewType);
1156 }
1157
1158 void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
1159   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1160   pImpl->FunctionTypes.TypeBecameConcrete(this, AbsTy);
1161 }
1162
1163
1164 // refineAbstractType - Called when a contained type is found to be more
1165 // concrete - this could potentially change us from an abstract type to a
1166 // concrete type.
1167 //
1168 void ArrayType::refineAbstractType(const DerivedType *OldType,
1169                                    const Type *NewType) {
1170   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1171   pImpl->ArrayTypes.RefineAbstractType(this, OldType, NewType);
1172 }
1173
1174 void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
1175   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1176   pImpl->ArrayTypes.TypeBecameConcrete(this, AbsTy);
1177 }
1178
1179 // refineAbstractType - Called when a contained type is found to be more
1180 // concrete - this could potentially change us from an abstract type to a
1181 // concrete type.
1182 //
1183 void VectorType::refineAbstractType(const DerivedType *OldType,
1184                                    const Type *NewType) {
1185   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1186   pImpl->VectorTypes.RefineAbstractType(this, OldType, NewType);
1187 }
1188
1189 void VectorType::typeBecameConcrete(const DerivedType *AbsTy) {
1190   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1191   pImpl->VectorTypes.TypeBecameConcrete(this, AbsTy);
1192 }
1193
1194 // refineAbstractType - Called when a contained type is found to be more
1195 // concrete - this could potentially change us from an abstract type to a
1196 // concrete type.
1197 //
1198 void StructType::refineAbstractType(const DerivedType *OldType,
1199                                     const Type *NewType) {
1200   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1201   pImpl->StructTypes.RefineAbstractType(this, OldType, NewType);
1202 }
1203
1204 void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
1205   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1206   pImpl->StructTypes.TypeBecameConcrete(this, AbsTy);
1207 }
1208
1209 // refineAbstractType - Called when a contained type is found to be more
1210 // concrete - this could potentially change us from an abstract type to a
1211 // concrete type.
1212 //
1213 void PointerType::refineAbstractType(const DerivedType *OldType,
1214                                      const Type *NewType) {
1215   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1216   pImpl->PointerTypes.RefineAbstractType(this, OldType, NewType);
1217 }
1218
1219 void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
1220   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1221   pImpl->PointerTypes.TypeBecameConcrete(this, AbsTy);
1222 }
1223
1224 bool SequentialType::indexValid(const Value *V) const {
1225   if (V->getType()->isIntegerTy()) 
1226     return true;
1227   return false;
1228 }
1229
1230 namespace llvm {
1231 raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
1232   T.print(OS);
1233   return OS;
1234 }
1235 }