From: Mikko Rasa Date: Wed, 7 Dec 2016 00:31:45 +0000 (+0200) Subject: Recognize backward references in GLSL loops X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ced18c244f874638dc17ae486802bb3bb01ecf71;p=libs%2Fgl.git Recognize backward references in GLSL loops A lone assignment at the end of a loop isn't unused if the next iteration uses the value. --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index c7da6c55..4e4b4814 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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::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(); } diff --git a/source/programcompiler.h b/source/programcompiler.h index e43b3a2d..db63eb36 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -262,6 +262,8 @@ private: std::vector assignments; ProgramSyntax::Assignment *assignment; bool assignment_target; + bool iteration; + std::set used_in_iteration; UnusedVariableLocator();