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