X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FType.cpp;h=3a9ea582f112efb088d357bd55f48f55f133986e;hb=ebcba612b537f45a033ccd9a60bee0c45e2e2ded;hp=4bddeca73fb7794d78b02d37759b864494506dc2;hpb=52f8ed8368890467ca807605a17afae81dc152fb;p=oota-llvm.git diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp index 4bddeca73fb..3a9ea582f11 100644 --- a/lib/VMCore/Type.cpp +++ b/lib/VMCore/Type.cpp @@ -22,6 +22,7 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Debug.h" #include +#include using namespace llvm; // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are @@ -395,16 +396,24 @@ bool StructType::indexValid(const Value *V) const { // Structure indexes require 32-bit integer constants. if (V->getType() == Type::Int32Ty) if (const ConstantInt *CU = dyn_cast(V)) - return CU->getZExtValue() < NumContainedTys; + return indexValid(CU->getZExtValue()); return false; } +bool StructType::indexValid(unsigned V) const { + return V < NumContainedTys; +} + // getTypeAtIndex - Given an index value into the type, return the type of the // element. For a structure type, this must be a constant value... // const Type *StructType::getTypeAtIndex(const Value *V) const { - assert(indexValid(V) && "Invalid structure index!"); unsigned Idx = (unsigned)cast(V)->getZExtValue(); + return getTypeAtIndex(Idx); +} + +const Type *StructType::getTypeAtIndex(unsigned Idx) const { + assert(indexValid(Idx) && "Invalid structure index!"); return ContainedTys[Idx]; } @@ -436,40 +445,35 @@ const IntegerType *Type::Int64Ty = new BuiltinIntegerType(64); // Derived Type Constructors //===----------------------------------------------------------------------===// -FunctionType::FunctionType(const Type *Result, - const std::vector &Params, - bool IsVarArgs) - : DerivedType(FunctionTyID), isVarArgs(IsVarArgs) { - ContainedTys = reinterpret_cast(this+1); - NumContainedTys = Params.size() + 1; // + 1 for result type - assert((Result->isFirstClassType() || Result == Type::VoidTy || - Result->getTypeID() == Type::StructTyID || - isa(Result)) && - "LLVM functions cannot return aggregates"); - bool isAbstract = Result->isAbstract(); - new (&ContainedTys[0]) PATypeHandle(Result, this); - - for (unsigned i = 0; i != Params.size(); ++i) { - assert((Params[i]->isFirstClassType() || isa(Params[i])) && - "Function arguments must be value types!"); - new (&ContainedTys[i+1]) PATypeHandle(Params[i],this); - isAbstract |= Params[i]->isAbstract(); - } - - // Calculate whether or not this type is abstract - setAbstract(isAbstract); +/// isValidReturnType - Return true if the specified type is valid as a return +/// type. +bool FunctionType::isValidReturnType(const Type *RetTy) { + if (RetTy->isFirstClassType()) + return true; + if (RetTy == Type::VoidTy || isa(RetTy)) + return true; + + // If this is a multiple return case, verify that each return is a first class + // value and that there is at least one value. + const StructType *SRetTy = dyn_cast(RetTy); + if (SRetTy == 0 || SRetTy->getNumElements() == 0) + return false; + + for (unsigned i = 0, e = SRetTy->getNumElements(); i != e; ++i) + if (!SRetTy->getElementType(i)->isFirstClassType()) + return false; + return true; } FunctionType::FunctionType(const Type *Result, - const SmallVectorImpl &Params, + const std::vector &Params, bool IsVarArgs) : DerivedType(FunctionTyID), isVarArgs(IsVarArgs) { ContainedTys = reinterpret_cast(this+1); NumContainedTys = Params.size() + 1; // + 1 for result type - assert((Result->isFirstClassType() || Result == Type::VoidTy || - Result->getTypeID() == Type::StructTyID || - isa(Result)) && - "LLVM functions cannot return aggregates"); + assert(isValidReturnType(Result) && "invalid return type for function"); + + bool isAbstract = Result->isAbstract(); new (&ContainedTys[0]) PATypeHandle(Result, this); @@ -554,6 +558,7 @@ void DerivedType::dropAllTypeUses() { } +namespace { /// TypePromotionGraph and graph traits - this is designed to allow us to do /// efficient SCC processing of type graphs. This is the exact same as @@ -564,6 +569,8 @@ struct TypePromotionGraph { TypePromotionGraph(Type *T) : Ty(T) {} }; +} + namespace llvm { template <> struct GraphTraits { typedef Type NodeType; @@ -710,11 +717,11 @@ static bool TypesEqual(const Type *Ty, const Type *Ty2) { // ever reach a non-abstract type, we know that we don't need to search the // subgraph. static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy, - std::set &VisitedTypes) { + SmallPtrSet &VisitedTypes) { if (TargetTy == CurTy) return true; if (!CurTy->isAbstract()) return false; - if (!VisitedTypes.insert(CurTy).second) + if (!VisitedTypes.insert(CurTy)) return false; // Already been here. for (Type::subtype_iterator I = CurTy->subtype_begin(), @@ -725,10 +732,10 @@ static bool AbstractTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy, } static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy, - std::set &VisitedTypes) { + SmallPtrSet &VisitedTypes) { if (TargetTy == CurTy) return true; - if (!VisitedTypes.insert(CurTy).second) + if (!VisitedTypes.insert(CurTy)) return false; // Already been here. for (Type::subtype_iterator I = CurTy->subtype_begin(), @@ -741,7 +748,7 @@ static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy, /// TypeHasCycleThroughItself - Return true if the specified type has a cycle /// back to itself. static bool TypeHasCycleThroughItself(const Type *Ty) { - std::set VisitedTypes; + SmallPtrSet VisitedTypes; if (Ty->isAbstract()) { // Optimized case for abstract types. for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); @@ -894,7 +901,7 @@ public: // The old record is now out-of-date, because one of the children has been // updated. Remove the obsolete entry from the map. unsigned NumErased = Map.erase(ValType::get(Ty)); - assert(NumErased && "Element not found!"); + assert(NumErased && "Element not found!"); NumErased = NumErased; // Remember the structural hash for the type before we start hacking on it, // in case we need it later. @@ -1074,16 +1081,7 @@ class FunctionValType { bool isVarArg; public: FunctionValType(const Type *ret, const std::vector &args, - bool isVA) : RetTy(ret), isVarArg(isVA) { - for (unsigned i = 0; i < args.size(); ++i) - ArgTypes.push_back(args[i]); - } - - FunctionValType(const Type *ret, const SmallVectorImpl &args, - bool isVA) : RetTy(ret), isVarArg(isVA) { - for (unsigned i = 0; i < args.size(); ++i) - ArgTypes.push_back(args[i]); - } + bool isVA) : RetTy(ret), ArgTypes(args), isVarArg(isVA) {} static FunctionValType get(const FunctionType *FT); @@ -1123,30 +1121,8 @@ FunctionType *FunctionType::get(const Type *ReturnType, bool isVarArg) { FunctionValType VT(ReturnType, Params, isVarArg); FunctionType *FT = FunctionTypes->get(VT); - if (FT) { - return FT; - } - - FT = (FunctionType*) new char[sizeof(FunctionType) + - sizeof(PATypeHandle)*(Params.size()+1)]; - new (FT) FunctionType(ReturnType, Params, isVarArg); - FunctionTypes->add(VT, FT); - -#ifdef DEBUG_MERGE_TYPES - DOUT << "Derived new type: " << FT << "\n"; -#endif - return FT; -} - -// FunctionType::get - The factory function for the FunctionType class... -FunctionType *FunctionType::get(const Type *ReturnType, - const SmallVectorImpl &Params, - bool isVarArg) { - FunctionValType VT(ReturnType, Params, isVarArg); - FunctionType *FT = FunctionTypes->get(VT); - if (FT) { + if (FT) return FT; - } FT = (FunctionType*) new char[sizeof(FunctionType) + sizeof(PATypeHandle)*(Params.size()+1)]; @@ -1301,6 +1277,17 @@ StructType *StructType::get(const std::vector &ETypes, return ST; } +StructType *StructType::get(const Type *type, ...) { + va_list ap; + std::vector StructFields; + va_start(ap, type); + while (type) { + StructFields.push_back(type); + type = va_arg(ap, llvm::Type*); + } + return llvm::StructType::get(StructFields); +} + //===----------------------------------------------------------------------===// @@ -1439,7 +1426,7 @@ void DerivedType::refineAbstractTypeTo(const Type *NewType) { while (!AbstractTypeUsers.empty() && NewTy != this) { AbstractTypeUser *User = AbstractTypeUsers.back(); - unsigned OldSize = AbstractTypeUsers.size(); + unsigned OldSize = AbstractTypeUsers.size(); OldSize=OldSize; #ifdef DEBUG_MERGE_TYPES DOUT << " REFINING user " << OldSize-1 << "[" << (void*)User << "] of abstract type [" << (void*)this << " " @@ -1466,7 +1453,7 @@ void DerivedType::notifyUsesThatTypeBecameConcrete() { DOUT << "typeIsREFINED type: " << (void*)this << " " << *this << "\n"; #endif - unsigned OldSize = AbstractTypeUsers.size(); + unsigned OldSize = AbstractTypeUsers.size(); OldSize=OldSize; while (!AbstractTypeUsers.empty()) { AbstractTypeUser *ATU = AbstractTypeUsers.back(); ATU->typeBecameConcrete(this);