From 02c1541c20adf36ea09825dc7bc745f8487c4bc8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 12 Nov 2016 18:40:29 +0200 Subject: [PATCH] Move unused aggregate detection to UnusedVariableDetector It isn't really NodeRemover's job. --- source/programcompiler.cpp | 61 +++++++++++++++++++------------------- source/programcompiler.h | 11 +++---- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 55f62ae9..f7d23161 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -83,7 +83,7 @@ bool ProgramCompiler::optimize(Stage &stage) remover.to_remove = unused_locator.unused_nodes; remover.apply(stage); - return remover.n_removed; + return !unused_locator.unused_nodes.empty(); } void ProgramCompiler::inject_block(Block &target, const Block &source) @@ -626,6 +626,7 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var) ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator(): + aggregate(0), assignment(false), record_target(false), assignment_target(0), @@ -656,6 +657,10 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) if(assignment && var.declaration==assignment_target) self_referencing = true; } + + map::iterator i = aggregates.find(var.declaration); + if(i!=aggregates.end()) + unused_nodes.erase(i->second); } void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc) @@ -710,50 +715,50 @@ void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr) assignment = false; } +void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct) +{ + SetForScope set(aggregate, &strct); + unused_nodes.insert(&strct); + TraversingVisitor::visit(strct); +} + void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) { - unused_nodes.insert(&var); - if(var.init_expression) + if(aggregate) + aggregates[&var] = aggregate; + else { - unused_nodes.insert(&*var.init_expression); - assignments[&var] = &*var.init_expression; + unused_nodes.insert(&var); + if(var.init_expression) + { + unused_nodes.insert(&*var.init_expression); + assignments[&var] = &*var.init_expression; + } } + unused_nodes.erase(var.type_declaration); TraversingVisitor::visit(var); } +void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface) +{ + SetForScope set(aggregate, &iface); + unused_nodes.insert(&iface); + TraversingVisitor::visit(iface); +} -ProgramCompiler::NodeRemover::NodeRemover(): - n_removed(0), - immutable_block(false), - remove_block(false) -{ } void ProgramCompiler::NodeRemover::visit(Block &block) { - remove_block = immutable_block; for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) { - bool remove = to_remove.count(&**i); - if(!remove) - remove_block = false; (*i)->visit(*this); - - if(remove ? !immutable_block : remove_block) - { + if(to_remove.count(&**i)) block.body.erase(i++); - ++n_removed; - } else ++i; } } -void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct) -{ - SetFlag set(immutable_block); - TraversingVisitor::visit(strct); -} - void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var) { if(to_remove.count(&var)) @@ -767,11 +772,5 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var) var.init_expression = 0; } -void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface) -{ - SetFlag set(immutable_block); - TraversingVisitor::visit(iface); -} - } // namespace GL } // namespace Msp diff --git a/source/programcompiler.h b/source/programcompiler.h index f1ea1652..829f7535 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -98,6 +98,8 @@ private: struct UnusedVariableLocator: Visitor { std::set unused_nodes; + std::map aggregates; + ProgramSyntax::Node *aggregate; std::map assignments; bool assignment; bool record_target; @@ -111,22 +113,17 @@ private: virtual void visit(ProgramSyntax::MemberAccess &); virtual void visit(ProgramSyntax::BinaryExpression &); virtual void visit(ProgramSyntax::ExpressionStatement &); + virtual void visit(ProgramSyntax::StructDeclaration &); virtual void visit(ProgramSyntax::VariableDeclaration &); + virtual void visit(ProgramSyntax::InterfaceBlock &); }; struct NodeRemover: Visitor { std::set to_remove; - unsigned n_removed; - bool immutable_block; - bool remove_block; - - NodeRemover(); virtual void visit(ProgramSyntax::Block &); - virtual void visit(ProgramSyntax::StructDeclaration &); virtual void visit(ProgramSyntax::VariableDeclaration &); - virtual void visit(ProgramSyntax::InterfaceBlock &); }; ProgramParser parser; -- 2.45.2