]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.cpp
Make ConstantConditionEliminator less trigger-happy
[libs/gl.git] / source / glsl / optimize.cpp
index 85f97faecd108452bb15b9f3b5508f7c503e8c92..87a57eb7f82496c797c6afdcf7ddbb257049655c 100644 (file)
@@ -193,7 +193,7 @@ bool FunctionInliner::apply(Stage &s)
        return r_any_inlined;
 }
 
-void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
+void FunctionInliner::visit(RefPtr<Expression> &ptr)
 {
        r_inline_result = 0;
        ptr->visit(*this);
@@ -216,31 +216,10 @@ void FunctionInliner::visit(Block &block)
        }
 }
 
-void FunctionInliner::visit(UnaryExpression &unary)
-{
-       visit_and_inline(unary.expression);
-}
-
-void FunctionInliner::visit(BinaryExpression &binary)
-{
-       visit_and_inline(binary.left);
-       visit_and_inline(binary.right);
-}
-
-void FunctionInliner::visit(MemberAccess &memacc)
-{
-       visit_and_inline(memacc.left);
-}
-
-void FunctionInliner::visit(Swizzle &swizzle)
-{
-       visit_and_inline(swizzle.left);
-}
-
 void FunctionInliner::visit(FunctionCall &call)
 {
        for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_inline(*i);
+               visit(*i);
 
        FunctionDeclaration *def = call.declaration;
        if(def)
@@ -264,29 +243,12 @@ void FunctionInliner::visit(FunctionCall &call)
        }
 }
 
-void FunctionInliner::visit(ExpressionStatement &expr)
-{
-       visit_and_inline(expr.expression);
-}
-
-void FunctionInliner::visit(VariableDeclaration &var)
-{
-       if(var.init_expression)
-               visit_and_inline(var.init_expression);
-}
-
 void FunctionInliner::visit(FunctionDeclaration &func)
 {
        SetForScope<FunctionDeclaration *> set_func(current_function, &func);
        TraversingVisitor::visit(func);
 }
 
-void FunctionInliner::visit(Conditional &cond)
-{
-       visit_and_inline(cond.condition);
-       cond.body.visit(*this);
-}
-
 void FunctionInliner::visit(Iteration &iter)
 {
        /* Visit the initialization statement before entering the loop body so the
@@ -301,12 +263,6 @@ void FunctionInliner::visit(Iteration &iter)
        iter.body.visit(*this);
 }
 
-void FunctionInliner::visit(Return &ret)
-{
-       if(ret.expression)
-               visit_and_inline(ret.expression);
-}
-
 
 ExpressionInliner::ExpressionInfo::ExpressionInfo():
        expression(0),
@@ -393,35 +349,36 @@ void ExpressionInliner::visit(Block &block)
 {
        TraversingVisitor::visit(block);
 
-       for(map<VariableDeclaration *, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); )
+       for(map<string, VariableDeclaration *>::iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
        {
-               map<string, VariableDeclaration *>::iterator j = block.variables.find(i->first->name);
-               if(j!=block.variables.end() && j->second==i->first)
+               map<Assignment::Target, ExpressionInfo>::iterator j = expressions.lower_bound(i->second);
+               for(; (j!=expressions.end() && j->first.declaration==i->second); )
                {
-                       if(i->second.expression && i->second.inline_point)
-                               inline_expression(*i->second.expression, *i->second.inline_point, i->second.outer_oper, i->second.inner_oper, i->second.inline_on_rhs);
+                       if(j->second.expression && j->second.inline_point)
+                               inline_expression(*j->second.expression, *j->second.inline_point, j->second.outer_oper, j->second.inner_oper, j->second.inline_on_rhs);
 
-                       expressions.erase(i++);
-               }
-               else
-               {
-                       /* The expression was assigned in this block and may depend on local
-                       variables of the block.  If this is a conditionally executed block,
-                       the assignment might not always happen.  Mark the expression as not
-                       available to any outer blocks. */
-                       if(i->second.assign_scope==&block)
-                               i->second.available = false;
-
-                       ++i;
+                       expressions.erase(j++);
                }
        }
+
+       /* Expressions assigned in this block may depend on local variables of the
+       block.  If this is a conditionally executed block, the assignments might not
+       always happen.  Mark the expressions as not available to any outer blocks. */
+       for(map<Assignment::Target, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); ++i)
+               if(i->second.assign_scope==&block)
+                       i->second.available = false;
+}
+
+void ExpressionInliner::visit(RefPtr<Expression> &expr)
+{
+       visit_and_record(expr, 0, false);
 }
 
 void ExpressionInliner::visit(VariableReference &var)
 {
        if(var.declaration)
        {
-               map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(var.declaration);
+               map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(var.declaration);
                if(i!=expressions.end())
                {
                        /* If a non-trivial expression is referenced multiple times, don't
@@ -479,19 +436,16 @@ void ExpressionInliner::visit(Assignment &assign)
        r_oper = 0;
        visit_and_record(assign.right, assign.oper, true);
 
-       if(VariableDeclaration *target_var = dynamic_cast<VariableDeclaration *>(assign.target.declaration))
+       map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(assign.target);
+       if(i!=expressions.end())
        {
-               map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(target_var);
-               if(i!=expressions.end())
-               {
-                       /* Self-referencing assignments can't be inlined without additional
-                       work.  Just clear any previous expression. */
-                       i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
-                       i->second.assign_scope = current_block;
-                       i->second.inline_point = 0;
-                       i->second.inner_oper = r_oper;
-                       i->second.available = true;
-               }
+               /* Self-referencing assignments can't be inlined without additional
+               work.  Just clear any previous expression. */
+               i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
+               i->second.assign_scope = current_block;
+               i->second.inline_point = 0;
+               i->second.inner_oper = r_oper;
+               i->second.available = true;
        }
 
        r_oper = assign.oper;
@@ -500,8 +454,7 @@ void ExpressionInliner::visit(Assignment &assign)
 
 void ExpressionInliner::visit(FunctionCall &call)
 {
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_record(*i, 0, false);
+       TraversingVisitor::visit(call);
        r_oper = 0;
        r_trivial = false;
 }
@@ -510,8 +463,7 @@ void ExpressionInliner::visit(VariableDeclaration &var)
 {
        r_oper = 0;
        r_trivial = true;
-       if(var.init_expression)
-               visit_and_record(var.init_expression, 0, false);
+       TraversingVisitor::visit(var);
 
        bool constant = var.constant;
        if(constant && var.layout)
@@ -535,12 +487,6 @@ void ExpressionInliner::visit(VariableDeclaration &var)
        }
 }
 
-void ExpressionInliner::visit(Conditional &cond)
-{
-       visit_and_record(cond.condition, 0, false);
-       cond.body.visit(*this);
-}
-
 void ExpressionInliner::visit(Iteration &iter)
 {
        SetForScope<Block *> set_block(current_block, &iter.body);
@@ -552,16 +498,10 @@ void ExpressionInliner::visit(Iteration &iter)
 
        SetForScope<Block *> set_body(iteration_body, &iter.body);
        if(iter.condition)
-               iter.condition->visit(*this);
+               visit(iter.condition);
        iter.body.visit(*this);
        if(iter.loop_expression)
-               iter.loop_expression->visit(*this);
-}
-
-void ExpressionInliner::visit(Return &ret)
-{
-       if(ret.expression)
-               visit_and_record(ret.expression, 0, false);
+               visit(iter.loop_expression);
 }
 
 
@@ -583,15 +523,14 @@ void ConstantConditionEliminator::visit(Block &block)
 
 void ConstantConditionEliminator::visit(Conditional &cond)
 {
-       ExpressionEvaluator eval;
-       cond.condition->visit(eval);
-       if(eval.is_result_valid())
-       {
-               Block &block = (eval.get_result() ? cond.body : cond.else_body);
-               current_block->body.splice(insert_point, block.body);
-               nodes_to_remove.insert(&cond);
-               return;
-       }
+       if(Literal *literal = dynamic_cast<Literal *>(cond.condition.get()))
+               if(literal->value.check_type<bool>())
+               {
+                       Block &block = (literal->value.value<bool>() ? cond.body : cond.else_body);
+                       current_block->body.splice(insert_point, block.body);
+                       nodes_to_remove.insert(&cond);
+                       return;
+               }
 
        TraversingVisitor::visit(cond);
 }
@@ -618,13 +557,6 @@ void ConstantConditionEliminator::visit(Iteration &iter)
 }
 
 
-UnusedVariableRemover::VariableInfo::VariableInfo():
-       local(false),
-       conditionally_assigned(false),
-       referenced(false)
-{ }
-
-
 bool UnusedTypeRemover::apply(Stage &stage)
 {
        stage.content.visit(*this);
@@ -692,83 +624,93 @@ void UnusedTypeRemover::visit(FunctionDeclaration &func)
 }
 
 
+UnusedVariableRemover::VariableInfo::VariableInfo():
+       local(false),
+       output(false),
+       conditionally_assigned(false),
+       referenced(false),
+       interface_block(0)
+{ }
+
+
 UnusedVariableRemover::UnusedVariableRemover():
-       aggregate(0),
+       stage(0),
+       interface_block(0),
        r_assignment(0),
        assignment_target(false),
-       r_assign_to_subfield(false),
        r_side_effects(false)
 { }
 
-bool UnusedVariableRemover::apply(Stage &stage)
+bool UnusedVariableRemover::apply(Stage &s)
 {
+       stage = &s;
        variables.push_back(BlockVariableMap());
-       stage.content.visit(*this);
+       s.content.visit(*this);
+
        BlockVariableMap &global_variables = variables.back();
+       set<InterfaceBlock *> used_interface_blocks;
+       Statement *prev_decl = 0;
+       bool output;
        for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
        {
-               string interface = i->first->interface;
-               bool linked = i->first->linked_declaration;
-               map<VariableDeclaration *, Node *>::iterator j = aggregates.find(i->first);
-               if(j!=aggregates.end())
-                       if(InterfaceBlock *iface = dynamic_cast<InterfaceBlock *>(j->second))
-                       {
-                               interface = iface->interface;
-                               linked = iface->linked_block;
-                       }
-
-               /* Don't remove output variables which are used by the next stage or the
-               graphics API. */
-               if(interface=="out" && (stage.type==Stage::FRAGMENT || linked || !i->first->name.compare(0, 3, "gl_")))
+               if(i->first.declaration!=prev_decl)
+               {
+                       prev_decl = i->first.declaration;
+                       output = i->second.output;
+               }
+               if(output)
+               {
+                       if((i->second.referenced || !i->second.assignments.empty()) && i->second.interface_block)
+                               used_interface_blocks.insert(i->second.interface_block);
                        continue;
+               }
 
                // Mark other unreferenced global variables as unused.
                if(!i->second.referenced)
                {
-                       unused_nodes.insert(i->first);
+                       if(!i->second.interface_block && !i->first.chain_len)
+                               unused_nodes.insert(i->first.declaration);
                        clear_assignments(i->second, true);
                }
+               else if(i->second.interface_block)
+                       used_interface_blocks.insert(i->second.interface_block);
        }
        variables.pop_back();
 
-       NodeRemover().apply(stage, unused_nodes);
+       for(map<string, InterfaceBlock *>::const_iterator i=s.interface_blocks.begin(); i!=s.interface_blocks.end(); ++i)
+               if(i->second->instance_name.empty() && !used_interface_blocks.count(i->second))
+                       unused_nodes.insert(i->second);
+
+       NodeRemover().apply(s, unused_nodes);
 
        return !unused_nodes.empty();
 }
 
-void UnusedVariableRemover::visit(VariableReference &var)
+void UnusedVariableRemover::reference_used(Statement &declaration)
 {
-       map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
-       if(i!=aggregates.end())
-               unused_nodes.erase(i->second);
-
-       if(var.declaration && !assignment_target)
+       BlockVariableMap &block_vars = variables.back();
+       /* Previous assignments of all subfields of this variable are used by
+       this reference. */
+       for(BlockVariableMap::iterator i=block_vars.lower_bound(&declaration); (i!=block_vars.end() && i->first.declaration==&declaration); ++i)
        {
-               VariableInfo &var_info = variables.back()[var.declaration];
-               // Previous assignments are used by this reference.
-               clear_assignments(var_info, false);
-               var_info.referenced = true;
+               clear_assignments(i->second, false);
+               i->second.referenced = true;
        }
-}
 
-void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
-{
-       unused_nodes.erase(iface.declaration);
+       // Always record a reference to the primary declaration, even if it didn't exist before
+       block_vars[&declaration].referenced = true;
 }
 
-void UnusedVariableRemover::visit(MemberAccess &memacc)
+void UnusedVariableRemover::visit(VariableReference &var)
 {
-       if(assignment_target)
-               r_assign_to_subfield = true;
-       TraversingVisitor::visit(memacc);
-       unused_nodes.erase(memacc.declaration);
+       if(var.declaration && !assignment_target)
+               reference_used(*var.declaration);
 }
 
-void UnusedVariableRemover::visit(Swizzle &swizzle)
+void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
 {
-       if(assignment_target)
-               r_assign_to_subfield = true;
-       TraversingVisitor::visit(swizzle);
+       if(iface.declaration && !assignment_target)
+               reference_used(*iface.declaration);
 }
 
 void UnusedVariableRemover::visit(UnaryExpression &unary)
@@ -782,8 +724,6 @@ void UnusedVariableRemover::visit(BinaryExpression &binary)
 {
        if(binary.oper->token[0]=='[')
        {
-               if(assignment_target)
-                       r_assign_to_subfield = true;
                binary.left->visit(*this);
                SetFlag set(assignment_target, false);
                binary.right->visit(*this);
@@ -811,13 +751,24 @@ void UnusedVariableRemover::visit(FunctionCall &call)
        r_side_effects = true;
 }
 
-void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained)
+void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node, bool chained)
 {
-       VariableInfo &var_info = variables.back()[&var];
-       /* An assignment which completely replaces the value of the variable causes
-       any previous unreferenced assignments to be unused. */
-       if(!chained)
-               clear_assignments(var_info, true);
+       BlockVariableMap &block_vars = variables.back();
+       for(BlockVariableMap::iterator i=block_vars.lower_bound(target); (i!=block_vars.end() && i->first.declaration==target.declaration); ++i)
+       {
+               bool subfield = (i->first.chain_len>=target.chain_len);
+               for(unsigned j=0; (subfield && j<target.chain_len); ++j)
+                       subfield = (i->first.chain[j]==target.chain[j]);
+               if(!subfield)
+                       break;
+
+               /* An assignment to the target causes any previous unreferenced
+               assignments to the same target or its subfields to be unused. */
+               if(!chained)
+                       clear_assignments(i->second, true);
+       }
+
+       VariableInfo &var_info = variables.back()[target];
        var_info.assignments.push_back(&node);
        var_info.conditionally_assigned = false;
 }
@@ -835,40 +786,45 @@ void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_
 void UnusedVariableRemover::visit(ExpressionStatement &expr)
 {
        r_assignment = 0;
-       r_assign_to_subfield = false;
        r_side_effects = false;
        TraversingVisitor::visit(expr);
        if(r_assignment && r_assignment->target.declaration)
-               if(VariableDeclaration *target_var = dynamic_cast<VariableDeclaration *>(r_assignment->target.declaration))
-                       record_assignment(*target_var, expr, (r_assignment->self_referencing || r_assign_to_subfield));
+               record_assignment(r_assignment->target, expr, r_assignment->self_referencing);
        if(!r_side_effects)
                unused_nodes.insert(&expr);
 }
 
-void UnusedVariableRemover::visit(StructDeclaration &strct)
-{
-       SetForScope<Node *> set(aggregate, &strct);
-       TraversingVisitor::visit(strct);
-}
-
 void UnusedVariableRemover::visit(VariableDeclaration &var)
 {
-       if(aggregate)
-               aggregates[&var] = aggregate;
+       VariableInfo &var_info = variables.back()[&var];
+       var_info.local = true;
+       var_info.interface_block = interface_block;
+
+       /* Mark variables as output if they're used by the next stage or the
+       graphics API. */
+       if(interface_block)
+               var_info.output = (interface_block->interface=="out" && (interface_block->linked_block || !interface_block->name.compare(0, 3, "gl_")));
        else
-       {
-               variables.back()[&var].local = true;
-               if(var.init_expression)
-                       record_assignment(var, *var.init_expression, false);
-       }
+               var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
+
+       if(var.init_expression)
+               record_assignment(&var, *var.init_expression, false);
        TraversingVisitor::visit(var);
 }
 
 void UnusedVariableRemover::visit(InterfaceBlock &iface)
 {
-       SetForScope<Node *> set(aggregate, &iface);
-       unused_nodes.insert(&iface);
-       iface.struct_declaration->members.visit(*this);
+       if(iface.instance_name.empty())
+       {
+               SetForScope<InterfaceBlock *> set_block(interface_block, &iface);
+               iface.struct_declaration->members.visit(*this);
+       }
+       else
+       {
+               VariableInfo &var_info = variables.back()[&iface];
+               var_info.local = true;
+               var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.name.compare(0, 3, "gl_")));
+       }
 }
 
 void UnusedVariableRemover::visit(FunctionDeclaration &func)
@@ -903,8 +859,8 @@ void UnusedVariableRemover::merge_down_variables()
        {
                if(i->second.local)
                {
-                       if(!i->second.referenced)
-                               unused_nodes.insert(i->first);
+                       if(!i->second.referenced && !i->first.chain_len)
+                               unused_nodes.insert(i->first.declaration);
                        /* Any unreferenced assignments when a variable runs out of scope
                        become unused. */
                        clear_assignments(i->second, true);