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