#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Debug.h"
#include <algorithm>
+#include <cstdarg>
using namespace llvm;
// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
// Structure indexes require 32-bit integer constants.
if (V->getType() == Type::Int32Ty)
if (const ConstantInt *CU = dyn_cast<ConstantInt>(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<ConstantInt>(V)->getZExtValue();
+ return getTypeAtIndex(Idx);
+}
+
+const Type *StructType::getTypeAtIndex(unsigned Idx) const {
+ assert(indexValid(Idx) && "Invalid structure index!");
return ContainedTys[Idx];
}
// Derived Type Constructors
//===----------------------------------------------------------------------===//
-FunctionType::FunctionType(const Type *Result,
- const std::vector<const Type*> &Params,
- bool IsVarArgs)
- : DerivedType(FunctionTyID), isVarArgs(IsVarArgs) {
- ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
- NumContainedTys = Params.size() + 1; // + 1 for result type
- assert((Result->isFirstClassType() || Result == Type::VoidTy ||
- Result->getTypeID() == Type::StructTyID ||
- isa<OpaqueType>(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<OpaqueType>(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<OpaqueType>(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<StructType>(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<const Type *> &Params,
+ const std::vector<const Type*> &Params,
bool IsVarArgs)
: DerivedType(FunctionTyID), isVarArgs(IsVarArgs) {
ContainedTys = reinterpret_cast<PATypeHandle*>(this+1);
NumContainedTys = Params.size() + 1; // + 1 for result type
- assert((Result->isFirstClassType() || Result == Type::VoidTy ||
- Result->getTypeID() == Type::StructTyID ||
- isa<OpaqueType>(Result)) &&
- "LLVM functions cannot return aggregates");
+ assert(isValidReturnType(Result) && "invalid return type for function");
+
+
bool isAbstract = Result->isAbstract();
new (&ContainedTys[0]) PATypeHandle(Result, this);
}
+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
TypePromotionGraph(Type *T) : Ty(T) {}
};
+}
+
namespace llvm {
template <> struct GraphTraits<TypePromotionGraph> {
typedef Type NodeType;
// 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<const Type*> &VisitedTypes) {
+ SmallPtrSet<const Type*, 128> &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(),
}
static bool ConcreteTypeHasCycleThrough(const Type *TargetTy, const Type *CurTy,
- std::set<const Type*> &VisitedTypes) {
+ SmallPtrSet<const Type*, 128> &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(),
/// TypeHasCycleThroughItself - Return true if the specified type has a cycle
/// back to itself.
static bool TypeHasCycleThroughItself(const Type *Ty) {
- std::set<const Type*> VisitedTypes;
+ SmallPtrSet<const Type*, 128> VisitedTypes;
if (Ty->isAbstract()) { // Optimized case for abstract types.
for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
// 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.
bool isVarArg;
public:
FunctionValType(const Type *ret, const std::vector<const Type*> &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<const Type*> &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);
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<const Type*> &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)];
return ST;
}
+StructType *StructType::get(const Type *type, ...) {
+ va_list ap;
+ std::vector<const llvm::Type*> StructFields;
+ va_start(ap, type);
+ while (type) {
+ StructFields.push_back(type);
+ type = va_arg(ap, llvm::Type*);
+ }
+ return llvm::StructType::get(StructFields);
+}
+
//===----------------------------------------------------------------------===//
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 << " "
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);