Skip to content

Commit

Permalink
TypeFinder: prefer iterative algorithm to keep stack usage low.
Browse files Browse the repository at this point in the history
Introduce subtype_reverse_iterator to maintain
the numbering assigned during the recursive type walk.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@192770 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dtzWill committed Oct 16, 2013
1 parent 86df596 commit 1e68100
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
8 changes: 8 additions & 0 deletions include/llvm/IR/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ class Type {
subtype_iterator subtype_begin() const { return ContainedTys; }
subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}

typedef std::reverse_iterator<subtype_iterator> subtype_reverse_iterator;
subtype_reverse_iterator subtype_rbegin() const {
return subtype_reverse_iterator(subtype_end());
}
subtype_reverse_iterator subtype_rend() const {
return subtype_reverse_iterator(subtype_begin());
}

/// getContainedType - This method is used to implement the type iterator
/// (defined a the end of the file). For derived types, this returns the
/// types 'contained' in the derived type.
Expand Down
28 changes: 18 additions & 10 deletions lib/IR/TypeFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,27 @@ void TypeFinder::clear() {
/// incorporateType - This method adds the type to the list of used structures
/// if it's not in there already.
void TypeFinder::incorporateType(Type *Ty) {
// Check to see if we're already visited this type.
// Check to see if we've already visited this type.
if (!VisitedTypes.insert(Ty).second)
return;

// If this is a structure or opaque type, add a name for the type.
if (StructType *STy = dyn_cast<StructType>(Ty))
if (!OnlyNamed || STy->hasName())
StructTypes.push_back(STy);

// Recursively walk all contained types.
for (Type::subtype_iterator I = Ty->subtype_begin(),
E = Ty->subtype_end(); I != E; ++I)
incorporateType(*I);
SmallVector<Type *, 4> TypeWorklist;
TypeWorklist.push_back(Ty);
do {
Ty = TypeWorklist.pop_back_val();

// If this is a structure or opaque type, add a name for the type.
if (StructType *STy = dyn_cast<StructType>(Ty))
if (!OnlyNamed || STy->hasName())
StructTypes.push_back(STy);

// Add all unvisited subtypes to worklist for processing
for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
E = Ty->subtype_rend();
I != E; ++I)
if (VisitedTypes.insert(*I).second)
TypeWorklist.push_back(*I);
} while (!TypeWorklist.empty());
}

/// incorporateValue - This method is used to walk operand lists finding types
Expand Down

0 comments on commit 1e68100

Please sign in to comment.