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