+ // Don't remove variables from inside interface blocks.
+ if(!kvp.second.interface_block)
+ unused_nodes.insert(kvp.first);
+ }
+ else if(kvp.second.interface_block)
+ // Interface blocks are kept if even one member is used.
+ unused_nodes.erase(kvp.second.interface_block);
+ }
+
+ NodeRemover().apply(s, unused_nodes);
+
+ return !unused_nodes.empty();
+}
+
+void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &node)
+{
+ VariableInfo &var_info = variables[target.declaration];
+ var_info.referenced = true;
+ if(!assignment_target)
+ {
+ bool loop_external = false;
+ for(AssignmentInfo *a: var_info.assignments)
+ {
+ bool covered = true;
+ for(unsigned j=0; (covered && j<a->target.chain_len && j<target.chain_len); ++j)
+ {
+ Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>(a->target.chain[j]&0xC0);
+ Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target.chain[j]&0xC0);
+ unsigned index1 = a->target.chain[j]&0x3F;
+ unsigned index2 = target.chain[j]&0x3F;
+ if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
+ {
+ if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
+ covered = index1&index2;
+ else if(type1==Assignment::Target::ARRAY && index1<4)
+ covered = index2&(1<<index1);
+ else if(type2==Assignment::Target::ARRAY && index2<4)
+ covered = index1&(1<<index2);
+ /* If it's some other combination (shouldn't happen), leave
+ covered as true */
+ }
+ else
+ covered = (type1==type2 && (index1==index2 || index1==0x3F || index2==0x3F));
+ }
+
+ if(covered)
+ {
+ a->used_by.push_back(&node);
+ if(a->in_loop<in_loop)
+ loop_external = true;
+ }
+ }
+
+ if(loop_external)
+ loop_ext_refs.push_back(&node);
+ }
+}
+
+void UnusedVariableRemover::visit(VariableReference &var)
+{
+ if(composite_reference)
+ r_reference.declaration = var.declaration;
+ else
+ referenced(var.declaration, var);
+}
+
+void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
+{
+ if(composite_reference)
+ r_reference.declaration = iface.declaration;
+ else
+ referenced(iface.declaration, iface);
+}
+
+void UnusedVariableRemover::visit_composite(Expression &expr)
+{
+ if(!composite_reference)
+ r_reference = Assignment::Target();
+
+ SetFlag set_composite(composite_reference);
+ expr.visit(*this);
+}
+
+void UnusedVariableRemover::visit(MemberAccess &memacc)
+{
+ visit_composite(*memacc.left);
+
+ add_to_chain(r_reference, Assignment::Target::MEMBER, memacc.index);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, memacc);
+}
+
+void UnusedVariableRemover::visit(Swizzle &swizzle)
+{
+ visit_composite(*swizzle.left);
+
+ unsigned mask = 0;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ mask |= 1<<swizzle.components[i];
+ add_to_chain(r_reference, Assignment::Target::SWIZZLE, mask);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, swizzle);
+}
+
+void UnusedVariableRemover::visit(UnaryExpression &unary)
+{
+ TraversingVisitor::visit(unary);
+ if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
+ r_side_effects = true;
+}
+
+void UnusedVariableRemover::visit(BinaryExpression &binary)
+{
+ if(binary.oper->token[0]=='[')
+ {
+ visit_composite(*binary.left);
+
+ {
+ SetFlag clear_assignment(assignment_target, false);
+ SetFlag clear_composite(composite_reference, false);
+ binary.right->visit(*this);
+ }
+
+ add_to_chain(r_reference, Assignment::Target::ARRAY, 0x3F);
+
+ if(!composite_reference && r_reference.declaration)
+ referenced(r_reference, binary);
+ }
+ else
+ {
+ SetFlag clear_composite(composite_reference, false);
+ TraversingVisitor::visit(binary);
+ }
+}
+
+void UnusedVariableRemover::visit(TernaryExpression &ternary)
+{
+ SetFlag clear_composite(composite_reference, false);
+ TraversingVisitor::visit(ternary);
+}
+
+void UnusedVariableRemover::visit(Assignment &assign)
+{
+ {
+ SetFlag set(assignment_target, (assign.oper->token[0]=='='));
+ assign.left->visit(*this);
+ }
+ assign.right->visit(*this);
+ r_assignment = &assign;
+ r_side_effects = true;
+}
+
+void UnusedVariableRemover::visit(FunctionCall &call)
+{
+ SetFlag clear_composite(composite_reference, false);
+ TraversingVisitor::visit(call);
+ /* Treat function calls as having side effects so expression statements
+ consisting of nothing but a function call won't be optimized away. */
+ r_side_effects = true;
+
+ if(stage->type==Stage::GEOMETRY && call.name=="EmitVertex")
+ {
+ for(const auto &kvp: variables)
+ if(kvp.second.output)
+ referenced(kvp.first, call);
+ }
+}
+
+void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node)
+{
+ assignments.push_back(AssignmentInfo());
+ AssignmentInfo &assign_info = assignments.back();
+ assign_info.node = &node;
+ assign_info.target = target;
+ assign_info.in_loop = in_loop;
+
+ /* An assignment to the target hides any assignments to the same target or
+ its subfields. */
+ VariableInfo &var_info = variables[target.declaration];
+ for(unsigned i=0; i<var_info.assignments.size(); )
+ {
+ const Assignment::Target &t = var_info.assignments[i]->target;
+
+ bool subfield = (t.chain_len>=target.chain_len);
+ for(unsigned j=0; (subfield && j<target.chain_len); ++j)
+ subfield = (t.chain[j]==target.chain[j]);
+
+ if(subfield)
+ var_info.assignments.erase(var_info.assignments.begin()+i);
+ else
+ ++i;
+ }
+
+ var_info.assignments.push_back(&assign_info);
+}
+
+void UnusedVariableRemover::visit(ExpressionStatement &expr)
+{
+ r_assignment = 0;
+ r_side_effects = false;
+ TraversingVisitor::visit(expr);
+ if(r_assignment && r_assignment->target.declaration)
+ record_assignment(r_assignment->target, expr);
+ if(!r_side_effects)
+ unused_nodes.insert(&expr);
+}
+
+void UnusedVariableRemover::visit(StructDeclaration &strct)
+{
+ SetFlag set_struct(in_struct);
+ TraversingVisitor::visit(strct);
+}
+
+void UnusedVariableRemover::visit(VariableDeclaration &var)
+{
+ TraversingVisitor::visit(var);
+
+ if(in_struct)
+ return;
+
+ VariableInfo &var_info = variables[&var];
+ 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->block_name.compare(0, 3, "gl_")));
+ else
+ var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
+
+ if(var.init_expression)
+ {
+ var_info.initialized = true;
+ record_assignment(&var, *var.init_expression);
+ }
+}
+
+void UnusedVariableRemover::visit(InterfaceBlock &iface)
+{
+ VariableInfo &var_info = variables[&iface];
+ var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.block_name.compare(0, 3, "gl_")));
+}
+
+void UnusedVariableRemover::merge_variables(const BlockVariableMap &other_vars)
+{
+ for(const auto &kvp: other_vars)
+ {
+ auto j = variables.find(kvp.first);
+ if(j!=variables.end())
+ {
+ /* The merged blocks started as copies of each other so any common
+ assignments must be in the beginning. */
+ unsigned k = 0;
+ for(; (k<kvp.second.assignments.size() && k<j->second.assignments.size()); ++k)
+ if(kvp.second.assignments[k]!=j->second.assignments[k])
+ break;
+
+ // Remaining assignments are unique to each block; merge them.
+ j->second.assignments.insert(j->second.assignments.end(), kvp.second.assignments.begin()+k, kvp.second.assignments.end());
+ j->second.referenced |= kvp.second.referenced;