]> git.tdb.fi Git - libs/gl.git/commitdiff
Recognize backward references in GLSL loops
authorMikko Rasa <tdb@tdb.fi>
Wed, 7 Dec 2016 00:31:45 +0000 (02:31 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 7 Dec 2016 00:31:45 +0000 (02:31 +0200)
A lone assignment at the end of a loop isn't unused if the next iteration
uses the value.

source/programcompiler.cpp
source/programcompiler.h

index c7da6c5535b96bf649c8640522f9cdc07d459f1d..4e4b4814f171b531bfffedea17c9b9d46101fab1 100644 (file)
@@ -1212,7 +1212,8 @@ void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter)
 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
        aggregate(0),
        assignment(0),
-       assignment_target(false)
+       assignment_target(false),
+       iteration(false)
 { }
 
 void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
@@ -1233,6 +1234,9 @@ void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
        if(assignment_target)
                return;
 
+       if(iteration)
+               used_in_iteration.insert(var.declaration);
+
        for(vector<BlockAssignmentMap>::iterator j=assignments.end(); j!=assignments.begin(); )
        {
                --j;
@@ -1277,7 +1281,8 @@ void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
 
 void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref)
 {
-       unused_nodes.insert(&node);
+       if(!used_in_iteration.count(&var))
+               unused_nodes.insert(&node);
        BlockAssignmentMap &block_assignments = assignments.back();
        AssignmentList &var_assignments = block_assignments[&var];
        if(!self_ref)
@@ -1404,8 +1409,13 @@ void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond)
 void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
 {
        assignments.push_back(BlockAssignmentMap());
-       TraversingVisitor::visit(iter);
+       {
+               SetFlag set(iteration);
+               TraversingVisitor::visit(iter);
+       }
        merge_down_assignments();
+       if(!iteration)
+               used_in_iteration.clear();
 }
 
 
index e43b3a2d45c0e1c272976de51abd6c643cff0054..db63eb3645d62d39fbb6a8717b28d9735d9b5234 100644 (file)
@@ -262,6 +262,8 @@ private:
                std::vector<BlockAssignmentMap> assignments;
                ProgramSyntax::Assignment *assignment;
                bool assignment_target;
+               bool iteration;
+               std::set<ProgramSyntax::Node *> used_in_iteration;
 
                UnusedVariableLocator();