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