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