prune #includes.
[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
836 // FunctionType::get - The factory function for the FunctionType class...
837 FunctionType *FunctionType::get(const Type *ReturnType,
838                                 ArrayRef<const Type*> Params,
839                                 bool isVarArg) {
840   FunctionValType VT(ReturnType, Params, isVarArg);
841   FunctionType *FT = 0;
842   
843   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
844   
845   FT = pImpl->FunctionTypes.get(VT);
846   
847   if (!FT) {
848     FT = (FunctionType*) operator new(sizeof(FunctionType) +
849                                     sizeof(PATypeHandle)*(Params.size()+1));
850     new (FT) FunctionType(ReturnType, Params, isVarArg);
851     pImpl->FunctionTypes.add(VT, FT);
852   }
853
854 #ifdef DEBUG_MERGE_TYPES
855   DEBUG(dbgs() << "Derived new type: " << FT << "\n");
856 #endif
857   return FT;
858 }
859
860 ArrayType *ArrayType::get(const Type *ElementType, uint64_t NumElements) {
861   assert(ElementType && "Can't get array of <null> types!");
862   assert(isValidElementType(ElementType) && "Invalid type for array element!");
863
864   ArrayValType AVT(ElementType, NumElements);
865   ArrayType *AT = 0;
866
867   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
868   
869   AT = pImpl->ArrayTypes.get(AVT);
870       
871   if (!AT) {
872     // Value not found.  Derive a new type!
873     pImpl->ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements));
874   }
875 #ifdef DEBUG_MERGE_TYPES
876   DEBUG(dbgs() << "Derived new type: " << *AT << "\n");
877 #endif
878   return AT;
879 }
880
881 bool ArrayType::isValidElementType(const Type *ElemTy) {
882   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
883          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
884 }
885
886 VectorType *VectorType::get(const Type *ElementType, unsigned NumElements) {
887   assert(ElementType && "Can't get vector of <null> types!");
888
889   VectorValType PVT(ElementType, NumElements);
890   VectorType *PT = 0;
891   
892   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
893   
894   PT = pImpl->VectorTypes.get(PVT);
895     
896   if (!PT) {
897     pImpl->VectorTypes.add(PVT, PT = new VectorType(ElementType, NumElements));
898   }
899 #ifdef DEBUG_MERGE_TYPES
900   DEBUG(dbgs() << "Derived new type: " << *PT << "\n");
901 #endif
902   return PT;
903 }
904
905 bool VectorType::isValidElementType(const Type *ElemTy) {
906   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
907          ElemTy->isOpaqueTy();
908 }
909
910 //===----------------------------------------------------------------------===//
911 // Struct Type Factory...
912 //
913
914 StructType *StructType::get(LLVMContext &Context,
915                             ArrayRef<const Type*> ETypes, 
916                             bool isPacked) {
917   StructValType STV(ETypes, isPacked);
918   StructType *ST = 0;
919   
920   LLVMContextImpl *pImpl = Context.pImpl;
921   
922   ST = pImpl->StructTypes.get(STV);
923     
924   if (!ST) {
925     // Value not found.  Derive a new type!
926     ST = (StructType*) operator new(sizeof(StructType) +
927                                     sizeof(PATypeHandle) * ETypes.size());
928     new (ST) StructType(Context, ETypes, isPacked);
929     pImpl->StructTypes.add(STV, ST);
930   }
931 #ifdef DEBUG_MERGE_TYPES
932   DEBUG(dbgs() << "Derived new type: " << *ST << "\n");
933 #endif
934   return ST;
935 }
936
937 StructType *StructType::get(LLVMContext &Context, const Type *type, ...) {
938   va_list ap;
939   std::vector<const llvm::Type*> StructFields;
940   va_start(ap, type);
941   while (type) {
942     StructFields.push_back(type);
943     type = va_arg(ap, llvm::Type*);
944   }
945   return llvm::StructType::get(Context, StructFields);
946 }
947
948 bool StructType::isValidElementType(const Type *ElemTy) {
949   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
950          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
951 }
952
953
954 //===----------------------------------------------------------------------===//
955 // Pointer Type Factory...
956 //
957
958 PointerType *PointerType::get(const Type *ValueType, unsigned AddressSpace) {
959   assert(ValueType && "Can't get a pointer to <null> type!");
960   assert(ValueType->getTypeID() != VoidTyID &&
961          "Pointer to void is not valid, use i8* instead!");
962   assert(isValidElementType(ValueType) && "Invalid type for pointer element!");
963   PointerValType PVT(ValueType, AddressSpace);
964
965   PointerType *PT = 0;
966   
967   LLVMContextImpl *pImpl = ValueType->getContext().pImpl;
968   
969   PT = pImpl->PointerTypes.get(PVT);
970   
971   if (!PT) {
972     // Value not found.  Derive a new type!
973     pImpl->PointerTypes.add(PVT, PT = new PointerType(ValueType, AddressSpace));
974   }
975 #ifdef DEBUG_MERGE_TYPES
976   DEBUG(dbgs() << "Derived new type: " << *PT << "\n");
977 #endif
978   return PT;
979 }
980
981 const PointerType *Type::getPointerTo(unsigned addrs) const {
982   return PointerType::get(this, addrs);
983 }
984
985 bool PointerType::isValidElementType(const Type *ElemTy) {
986   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
987          !ElemTy->isMetadataTy();
988 }
989
990
991 //===----------------------------------------------------------------------===//
992 // Opaque Type Factory...
993 //
994
995 OpaqueType *OpaqueType::get(LLVMContext &C) {
996   OpaqueType *OT = new OpaqueType(C);       // All opaque types are distinct.
997   LLVMContextImpl *pImpl = C.pImpl;
998   pImpl->OpaqueTypes.insert(OT);
999   return OT;
1000 }
1001
1002
1003
1004 //===----------------------------------------------------------------------===//
1005 //                     Derived Type Refinement Functions
1006 //===----------------------------------------------------------------------===//
1007
1008 // addAbstractTypeUser - Notify an abstract type that there is a new user of
1009 // it.  This function is called primarily by the PATypeHandle class.
1010 void Type::addAbstractTypeUser(AbstractTypeUser *U) const {
1011   assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
1012   AbstractTypeUsers.push_back(U);
1013 }
1014
1015
1016 // removeAbstractTypeUser - Notify an abstract type that a user of the class
1017 // no longer has a handle to the type.  This function is called primarily by
1018 // the PATypeHandle class.  When there are no users of the abstract type, it
1019 // is annihilated, because there is no way to get a reference to it ever again.
1020 //
1021 void Type::removeAbstractTypeUser(AbstractTypeUser *U) const {
1022   
1023   // Search from back to front because we will notify users from back to
1024   // front.  Also, it is likely that there will be a stack like behavior to
1025   // users that register and unregister users.
1026   //
1027   unsigned i;
1028   for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i)
1029     assert(i != 0 && "AbstractTypeUser not in user list!");
1030
1031   --i;  // Convert to be in range 0 <= i < size()
1032   assert(i < AbstractTypeUsers.size() && "Index out of range!");  // Wraparound?
1033
1034   AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i);
1035
1036 #ifdef DEBUG_MERGE_TYPES
1037   DEBUG(dbgs() << "  remAbstractTypeUser[" << (void*)this << ", "
1038                << *this << "][" << i << "] User = " << U << "\n");
1039 #endif
1040
1041   if (AbstractTypeUsers.empty() && getRefCount() == 0 && isAbstract()) {
1042 #ifdef DEBUG_MERGE_TYPES
1043     DEBUG(dbgs() << "DELETEing unused abstract type: <" << *this
1044                  << ">[" << (void*)this << "]" << "\n");
1045 #endif
1046   
1047     this->destroy();
1048   }
1049 }
1050
1051 // refineAbstractTypeTo - This function is used when it is discovered
1052 // that the 'this' abstract type is actually equivalent to the NewType
1053 // specified. This causes all users of 'this' to switch to reference the more 
1054 // concrete type NewType and for 'this' to be deleted.  Only used for internal
1055 // callers.
1056 //
1057 void DerivedType::refineAbstractTypeTo(const Type *NewType) {
1058   assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!");
1059   assert(this != NewType && "Can't refine to myself!");
1060   assert(ForwardType == 0 && "This type has already been refined!");
1061
1062   LLVMContextImpl *pImpl = getContext().pImpl;
1063
1064   // The descriptions may be out of date.  Conservatively clear them all!
1065   pImpl->AbstractTypeDescriptions.clear();
1066
1067 #ifdef DEBUG_MERGE_TYPES
1068   DEBUG(dbgs() << "REFINING abstract type [" << (void*)this << " "
1069                << *this << "] to [" << (void*)NewType << " "
1070                << *NewType << "]!\n");
1071 #endif
1072
1073   // Make sure to put the type to be refined to into a holder so that if IT gets
1074   // refined, that we will not continue using a dead reference...
1075   //
1076   PATypeHolder NewTy(NewType);
1077   // Any PATypeHolders referring to this type will now automatically forward to
1078   // the type we are resolved to.
1079   ForwardType = NewType;
1080   if (ForwardType->isAbstract())
1081     ForwardType->addRef();
1082
1083   // Add a self use of the current type so that we don't delete ourself until
1084   // after the function exits.
1085   //
1086   PATypeHolder CurrentTy(this);
1087
1088   // To make the situation simpler, we ask the subclass to remove this type from
1089   // the type map, and to replace any type uses with uses of non-abstract types.
1090   // This dramatically limits the amount of recursive type trouble we can find
1091   // ourselves in.
1092   dropAllTypeUses();
1093
1094   // Iterate over all of the uses of this type, invoking callback.  Each user
1095   // should remove itself from our use list automatically.  We have to check to
1096   // make sure that NewTy doesn't _become_ 'this'.  If it does, resolving types
1097   // will not cause users to drop off of the use list.  If we resolve to ourself
1098   // we succeed!
1099   //
1100   while (!AbstractTypeUsers.empty() && NewTy != this) {
1101     AbstractTypeUser *User = AbstractTypeUsers.back();
1102
1103     unsigned OldSize = AbstractTypeUsers.size(); (void)OldSize;
1104 #ifdef DEBUG_MERGE_TYPES
1105     DEBUG(dbgs() << " REFINING user " << OldSize-1 << "[" << (void*)User
1106                  << "] of abstract type [" << (void*)this << " "
1107                  << *this << "] to [" << (void*)NewTy.get() << " "
1108                  << *NewTy << "]!\n");
1109 #endif
1110     User->refineAbstractType(this, NewTy);
1111
1112     assert(AbstractTypeUsers.size() != OldSize &&
1113            "AbsTyUser did not remove self from user list!");
1114   }
1115
1116   // If we were successful removing all users from the type, 'this' will be
1117   // deleted when the last PATypeHolder is destroyed or updated from this type.
1118   // This may occur on exit of this function, as the CurrentTy object is
1119   // destroyed.
1120 }
1121
1122 // notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type that
1123 // the current type has transitioned from being abstract to being concrete.
1124 //
1125 void DerivedType::notifyUsesThatTypeBecameConcrete() {
1126 #ifdef DEBUG_MERGE_TYPES
1127   DEBUG(dbgs() << "typeIsREFINED type: " << (void*)this << " " << *this <<"\n");
1128 #endif
1129
1130   unsigned OldSize = AbstractTypeUsers.size(); (void)OldSize;
1131   while (!AbstractTypeUsers.empty()) {
1132     AbstractTypeUser *ATU = AbstractTypeUsers.back();
1133     ATU->typeBecameConcrete(this);
1134
1135     assert(AbstractTypeUsers.size() < OldSize-- &&
1136            "AbstractTypeUser did not remove itself from the use list!");
1137   }
1138 }
1139
1140 // refineAbstractType - Called when a contained type is found to be more
1141 // concrete - this could potentially change us from an abstract type to a
1142 // concrete type.
1143 //
1144 void FunctionType::refineAbstractType(const DerivedType *OldType,
1145                                       const Type *NewType) {
1146   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1147   pImpl->FunctionTypes.RefineAbstractType(this, OldType, NewType);
1148 }
1149
1150 void FunctionType::typeBecameConcrete(const DerivedType *AbsTy) {
1151   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1152   pImpl->FunctionTypes.TypeBecameConcrete(this, AbsTy);
1153 }
1154
1155
1156 // refineAbstractType - Called when a contained type is found to be more
1157 // concrete - this could potentially change us from an abstract type to a
1158 // concrete type.
1159 //
1160 void ArrayType::refineAbstractType(const DerivedType *OldType,
1161                                    const Type *NewType) {
1162   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1163   pImpl->ArrayTypes.RefineAbstractType(this, OldType, NewType);
1164 }
1165
1166 void ArrayType::typeBecameConcrete(const DerivedType *AbsTy) {
1167   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1168   pImpl->ArrayTypes.TypeBecameConcrete(this, AbsTy);
1169 }
1170
1171 // refineAbstractType - Called when a contained type is found to be more
1172 // concrete - this could potentially change us from an abstract type to a
1173 // concrete type.
1174 //
1175 void VectorType::refineAbstractType(const DerivedType *OldType,
1176                                    const Type *NewType) {
1177   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1178   pImpl->VectorTypes.RefineAbstractType(this, OldType, NewType);
1179 }
1180
1181 void VectorType::typeBecameConcrete(const DerivedType *AbsTy) {
1182   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1183   pImpl->VectorTypes.TypeBecameConcrete(this, AbsTy);
1184 }
1185
1186 // refineAbstractType - Called when a contained type is found to be more
1187 // concrete - this could potentially change us from an abstract type to a
1188 // concrete type.
1189 //
1190 void StructType::refineAbstractType(const DerivedType *OldType,
1191                                     const Type *NewType) {
1192   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1193   pImpl->StructTypes.RefineAbstractType(this, OldType, NewType);
1194 }
1195
1196 void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
1197   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1198   pImpl->StructTypes.TypeBecameConcrete(this, AbsTy);
1199 }
1200
1201 // refineAbstractType - Called when a contained type is found to be more
1202 // concrete - this could potentially change us from an abstract type to a
1203 // concrete type.
1204 //
1205 void PointerType::refineAbstractType(const DerivedType *OldType,
1206                                      const Type *NewType) {
1207   LLVMContextImpl *pImpl = OldType->getContext().pImpl;
1208   pImpl->PointerTypes.RefineAbstractType(this, OldType, NewType);
1209 }
1210
1211 void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
1212   LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
1213   pImpl->PointerTypes.TypeBecameConcrete(this, AbsTy);
1214 }
1215
1216 bool SequentialType::indexValid(const Value *V) const {
1217   if (V->getType()->isIntegerTy()) 
1218     return true;
1219   return false;
1220 }
1221
1222 namespace llvm {
1223 raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
1224   T.print(OS);
1225   return OS;
1226 }
1227 }