]> git.tdb.fi Git - libs/gl.git/commitdiff
Move unused aggregate detection to UnusedVariableDetector
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 16:40:29 +0000 (18:40 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Nov 2016 16:40:29 +0000 (18:40 +0200)
It isn't really NodeRemover's job.

source/programcompiler.cpp
source/programcompiler.h

index 55f62ae93e16d954b846fa13262f9108220c2dda..f7d231615618178cecbcaf68ecf49caa2ae5835b 100644 (file)
@@ -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<VariableDeclaration *, Node *>::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<Node *> 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<Node *> 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<NodePtr<Node> >::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
index f1ea165296b49f5fc122cbc344a6b37693dd182f..829f75358cdd08695d16730184f109cc03e5c0d6 100644 (file)
@@ -98,6 +98,8 @@ private:
        struct UnusedVariableLocator: Visitor
        {
                std::set<ProgramSyntax::Node *> unused_nodes;
+               std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> aggregates;
+               ProgramSyntax::Node *aggregate;
                std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> 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<ProgramSyntax::Node *> 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;