1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 InlineableFunctionLocator::InlineableFunctionLocator():
16 void InlineableFunctionLocator::visit(FunctionCall &call)
18 FunctionDeclaration *def = call.declaration;
20 def = def->definition;
24 unsigned &count = refcounts[def];
26 /* Don't inline functions which are called more than once or are called
28 if(count>1 || def==current_function)
29 inlineable.erase(def);
32 TraversingVisitor::visit(call);
35 void InlineableFunctionLocator::visit(FunctionDeclaration &func)
37 unsigned &count = refcounts[func.definition];
38 if(count<=1 && func.parameters.empty())
39 inlineable.insert(func.definition);
41 SetForScope<FunctionDeclaration *> set(current_function, &func);
43 TraversingVisitor::visit(func);
46 void InlineableFunctionLocator::visit(Conditional &cond)
48 TraversingVisitor::visit(cond);
49 inlineable.erase(current_function);
52 void InlineableFunctionLocator::visit(Iteration &iter)
54 TraversingVisitor::visit(iter);
55 inlineable.erase(current_function);
58 void InlineableFunctionLocator::visit(Return &ret)
60 TraversingVisitor::visit(ret);
62 inlineable.erase(current_function);
67 InlineContentInjector::InlineContentInjector():
73 const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList<Statement>::iterator &ins_pt, FunctionDeclaration &src)
75 target_block = &tgt_blk;
77 for(NodeList<Statement>::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i)
79 r_inlined_statement = 0;
81 if(!r_inlined_statement)
82 r_inlined_statement = (*i)->clone();
84 SetFlag set_remap(remap_names);
85 r_inlined_statement->visit(*this);
86 tgt_blk.body.insert(ins_pt, r_inlined_statement);
89 NodeReorderer().apply(stage, target_func, dependencies);
94 string InlineContentInjector::create_unused_name(const string &base, bool always_prefix)
97 if(always_prefix || target_block->variables.count(result))
98 result = format("_%s_%s", source_func->name, base);
99 unsigned initial_size = result.size();
100 for(unsigned i=1; target_block->variables.count(result); ++i)
102 result.erase(initial_size);
103 result += format("_%d", i);
108 void InlineContentInjector::visit(VariableReference &var)
112 map<string, VariableDeclaration *>::const_iterator i = variable_map.find(var.name);
113 if(i!=variable_map.end())
114 var.name = i->second->name;
116 else if(var.declaration)
118 SetFlag set_deps(deps_only);
119 dependencies.insert(var.declaration);
120 var.declaration->visit(*this);
124 void InlineContentInjector::visit(InterfaceBlockReference &iface)
126 if(!remap_names && iface.declaration)
128 SetFlag set_deps(deps_only);
129 dependencies.insert(iface.declaration);
130 iface.declaration->visit(*this);
134 void InlineContentInjector::visit(FunctionCall &call)
136 if(!remap_names && call.declaration)
137 dependencies.insert(call.declaration);
138 TraversingVisitor::visit(call);
141 void InlineContentInjector::visit(VariableDeclaration &var)
143 TraversingVisitor::visit(var);
145 if(var.type_declaration)
147 SetFlag set_deps(deps_only);
148 dependencies.insert(var.type_declaration);
149 var.type_declaration->visit(*this);
152 if(!remap_names && !deps_only)
154 RefPtr<VariableDeclaration> inlined_var = var.clone();
155 inlined_var->name = create_unused_name(var.name, false);
156 r_inlined_statement = inlined_var;
158 variable_map[var.name] = inlined_var.get();
162 void InlineContentInjector::visit(Return &ret)
164 TraversingVisitor::visit(ret);
168 /* Create a new variable to hold the return value of the inlined
170 r_result_name = create_unused_name("return", true);
171 RefPtr<VariableDeclaration> var = new VariableDeclaration;
172 var->source = ret.source;
173 var->line = ret.line;
174 var->type = source_func->return_type;
175 var->name = r_result_name;
176 var->init_expression = ret.expression->clone();
177 r_inlined_statement = var;
182 FunctionInliner::FunctionInliner():
187 bool FunctionInliner::apply(Stage &s)
190 inlineable = InlineableFunctionLocator().apply(s);
191 r_any_inlined = false;
192 s.content.visit(*this);
193 return r_any_inlined;
196 void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
202 ptr = r_inline_result;
203 r_any_inlined = true;
208 void FunctionInliner::visit(Block &block)
210 SetForScope<Block *> set_block(current_block, &block);
211 SetForScope<NodeList<Statement>::iterator> save_insert_point(insert_point, block.body.begin());
212 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
219 void FunctionInliner::visit(UnaryExpression &unary)
221 visit_and_inline(unary.expression);
224 void FunctionInliner::visit(BinaryExpression &binary)
226 visit_and_inline(binary.left);
227 visit_and_inline(binary.right);
230 void FunctionInliner::visit(MemberAccess &memacc)
232 visit_and_inline(memacc.left);
235 void FunctionInliner::visit(Swizzle &swizzle)
237 visit_and_inline(swizzle.left);
240 void FunctionInliner::visit(FunctionCall &call)
242 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
243 visit_and_inline(*i);
245 FunctionDeclaration *def = call.declaration;
247 def = def->definition;
249 if(def && inlineable.count(def))
251 string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
253 // This will later get removed by UnusedVariableRemover.
254 if(result_name.empty())
255 result_name = "msp_unused_from_inline";
257 RefPtr<VariableReference> ref = new VariableReference;
258 ref->name = result_name;
259 r_inline_result = ref;
261 /* Inlined variables need to be resolved before this function can be
263 inlineable.erase(current_function);
267 void FunctionInliner::visit(ExpressionStatement &expr)
269 visit_and_inline(expr.expression);
272 void FunctionInliner::visit(VariableDeclaration &var)
274 if(var.init_expression)
275 visit_and_inline(var.init_expression);
278 void FunctionInliner::visit(FunctionDeclaration &func)
280 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
281 TraversingVisitor::visit(func);
284 void FunctionInliner::visit(Conditional &cond)
286 visit_and_inline(cond.condition);
287 cond.body.visit(*this);
290 void FunctionInliner::visit(Iteration &iter)
292 /* Visit the initialization statement before entering the loop body so the
293 inlined statements get inserted outside. */
294 if(iter.init_statement)
295 iter.init_statement->visit(*this);
297 SetForScope<Block *> set_block(current_block, &iter.body);
298 /* Skip the condition and loop expression parts because they're not properly
299 inside the body block. Inlining anything into them will require a more
300 comprehensive transformation. */
301 iter.body.visit(*this);
304 void FunctionInliner::visit(Return &ret)
307 visit_and_inline(ret.expression);
311 ExpressionInliner::ExpressionInfo::ExpressionInfo():
317 inline_on_rhs(false),
323 ExpressionInliner::ExpressionInliner():
325 r_any_inlined(false),
328 iteration_init(false),
333 bool ExpressionInliner::apply(Stage &s)
335 s.content.visit(*this);
336 return r_any_inlined;
339 void ExpressionInliner::visit_and_record(RefPtr<Expression> &ptr, const Operator *outer_oper, bool on_rhs)
343 if(r_ref_info && r_ref_info->expression && r_ref_info->available)
345 if(iteration_body && !r_ref_info->trivial)
347 /* Don't inline non-trivial expressions which were assigned outside
348 an iteration statement. The iteration may run multiple times, which
349 would cause the expression to also be evaluated multiple times. */
350 Block *i = r_ref_info->assign_scope;
351 for(; (i && i!=iteration_body); i=i->parent) ;
356 r_ref_info->outer_oper = outer_oper;
357 if(r_ref_info->trivial)
358 inline_expression(*r_ref_info->expression, ptr, outer_oper, r_ref_info->inner_oper, on_rhs);
361 /* Record the inline point for a non-trivial expression but don't
362 inline it yet. It might turn out it shouldn't be inlined after all. */
363 r_ref_info->inline_point = &ptr;
364 r_ref_info->inline_on_rhs = on_rhs;
370 void ExpressionInliner::inline_expression(Expression &expr, RefPtr<Expression> &ptr, const Operator *outer_oper, const Operator *inner_oper, bool on_rhs)
372 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
373 unsigned inner_precedence = (inner_oper ? inner_oper->precedence : 0);
375 bool needs_parentheses = (inner_precedence>=outer_precedence);
376 if(inner_oper && inner_oper==outer_oper)
377 // Omit parentheses if the operator's natural grouping works out.
378 needs_parentheses = (inner_oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(inner_oper->assoc==Operator::RIGHT_TO_LEFT));
380 if(needs_parentheses)
382 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
383 parexpr->expression = expr.clone();
389 r_any_inlined = true;
392 void ExpressionInliner::visit(Block &block)
394 TraversingVisitor::visit(block);
396 for(map<VariableDeclaration *, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); )
398 map<string, VariableDeclaration *>::iterator j = block.variables.find(i->first->name);
399 if(j!=block.variables.end() && j->second==i->first)
401 if(i->second.expression && i->second.inline_point)
402 inline_expression(*i->second.expression, *i->second.inline_point, i->second.outer_oper, i->second.inner_oper, i->second.inline_on_rhs);
404 expressions.erase(i++);
408 /* The expression was assigned in this block and may depend on local
409 variables of the block. If this is a conditionally executed block,
410 the assignment might not always happen. Mark the expression as not
411 available to any outer blocks. */
412 if(i->second.assign_scope==&block)
413 i->second.available = false;
420 void ExpressionInliner::visit(VariableReference &var)
424 map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(var.declaration);
425 if(i!=expressions.end())
427 /* If a non-trivial expression is referenced multiple times, don't
429 if(i->second.inline_point && !i->second.trivial)
430 i->second.expression = 0;
431 /* Mutating expressions are analogous to self-referencing assignments
432 and prevent inlining. */
434 i->second.expression = 0;
435 r_ref_info = &i->second;
440 void ExpressionInliner::visit(MemberAccess &memacc)
442 visit_and_record(memacc.left, memacc.oper, false);
443 r_oper = memacc.oper;
447 void ExpressionInliner::visit(Swizzle &swizzle)
449 visit_and_record(swizzle.left, swizzle.oper, false);
450 r_oper = swizzle.oper;
454 void ExpressionInliner::visit(UnaryExpression &unary)
456 SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-');
457 visit_and_record(unary.expression, unary.oper, false);
462 void ExpressionInliner::visit(BinaryExpression &binary)
464 visit_and_record(binary.left, binary.oper, false);
466 SetFlag clear_target(mutating, false);
467 visit_and_record(binary.right, binary.oper, true);
469 r_oper = binary.oper;
473 void ExpressionInliner::visit(Assignment &assign)
476 SetFlag set_target(mutating);
477 visit_and_record(assign.left, assign.oper, false);
480 visit_and_record(assign.right, assign.oper, true);
482 if(VariableDeclaration *target_var = dynamic_cast<VariableDeclaration *>(assign.target.declaration))
484 map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(target_var);
485 if(i!=expressions.end())
487 /* Self-referencing assignments can't be inlined without additional
488 work. Just clear any previous expression. */
489 i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
490 i->second.assign_scope = current_block;
491 i->second.inline_point = 0;
492 i->second.inner_oper = r_oper;
493 i->second.available = true;
497 r_oper = assign.oper;
501 void ExpressionInliner::visit(FunctionCall &call)
503 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
504 visit_and_record(*i, 0, false);
509 void ExpressionInliner::visit(VariableDeclaration &var)
513 if(var.init_expression)
514 visit_and_record(var.init_expression, 0, false);
516 bool constant = var.constant;
517 if(constant && var.layout)
519 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
520 constant = (i->name!="constant_id");
523 /* Only inline global variables if they're constant and have trivial
524 initializers. Non-constant variables could change in ways which are hard to
525 analyze and non-trivial expressions could be expensive to inline. */
526 if((current_block->parent || (constant && r_trivial)) && var.interface.empty())
528 ExpressionInfo &info = expressions[&var];
529 /* Assume variables declared in an iteration initialization statement
530 will have their values change throughout the iteration. */
531 info.expression = (iteration_init ? 0 : var.init_expression.get());
532 info.assign_scope = current_block;
533 info.inner_oper = r_oper;
534 info.trivial = r_trivial;
538 void ExpressionInliner::visit(Conditional &cond)
540 visit_and_record(cond.condition, 0, false);
541 cond.body.visit(*this);
544 void ExpressionInliner::visit(Iteration &iter)
546 SetForScope<Block *> set_block(current_block, &iter.body);
547 if(iter.init_statement)
549 SetFlag set_init(iteration_init);
550 iter.init_statement->visit(*this);
553 SetForScope<Block *> set_body(iteration_body, &iter.body);
555 iter.condition->visit(*this);
556 iter.body.visit(*this);
557 if(iter.loop_expression)
558 iter.loop_expression->visit(*this);
561 void ExpressionInliner::visit(Return &ret)
564 visit_and_record(ret.expression, 0, false);
568 void ConstantConditionEliminator::apply(Stage &stage)
570 stage.content.visit(*this);
571 NodeRemover().apply(stage, nodes_to_remove);
574 void ConstantConditionEliminator::visit(Block &block)
576 SetForScope<Block *> set_block(current_block, &block);
577 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
584 void ConstantConditionEliminator::visit(Conditional &cond)
586 ExpressionEvaluator eval;
587 cond.condition->visit(eval);
588 if(eval.is_result_valid())
590 Block &block = (eval.get_result() ? cond.body : cond.else_body);
591 current_block->body.splice(insert_point, block.body);
592 nodes_to_remove.insert(&cond);
596 TraversingVisitor::visit(cond);
599 void ConstantConditionEliminator::visit(Iteration &iter)
603 /* If the loop condition is always false on the first iteration, the
604 entire loop can be removed */
605 ExpressionEvaluator::ValueMap values;
606 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
607 values[var] = var->init_expression.get();
608 ExpressionEvaluator eval(values);
609 iter.condition->visit(eval);
610 if(eval.is_result_valid() && !eval.get_result())
612 nodes_to_remove.insert(&iter);
617 TraversingVisitor::visit(iter);
621 UnusedVariableRemover::VariableInfo::VariableInfo():
624 conditionally_assigned(false),
630 bool UnusedTypeRemover::apply(Stage &stage)
632 stage.content.visit(*this);
633 NodeRemover().apply(stage, unused_nodes);
634 return !unused_nodes.empty();
637 void UnusedTypeRemover::visit(Literal &literal)
639 unused_nodes.erase(literal.type);
642 void UnusedTypeRemover::visit(UnaryExpression &unary)
644 unused_nodes.erase(unary.type);
645 TraversingVisitor::visit(unary);
648 void UnusedTypeRemover::visit(BinaryExpression &binary)
650 unused_nodes.erase(binary.type);
651 TraversingVisitor::visit(binary);
654 void UnusedTypeRemover::visit(FunctionCall &call)
656 unused_nodes.erase(call.type);
657 TraversingVisitor::visit(call);
660 void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
663 unused_nodes.erase(type.base_type);
664 unused_nodes.insert(&type);
667 void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
670 unused_nodes.erase(type.base_type);
671 unused_nodes.insert(&type);
674 void UnusedTypeRemover::visit(StructDeclaration &strct)
676 unused_nodes.insert(&strct);
677 TraversingVisitor::visit(strct);
680 void UnusedTypeRemover::visit(VariableDeclaration &var)
682 unused_nodes.erase(var.type_declaration);
685 void UnusedTypeRemover::visit(InterfaceBlock &iface)
687 unused_nodes.erase(iface.type_declaration);
690 void UnusedTypeRemover::visit(FunctionDeclaration &func)
692 unused_nodes.erase(func.return_type_declaration);
693 TraversingVisitor::visit(func);
697 UnusedVariableRemover::UnusedVariableRemover():
701 assignment_target(false),
702 r_side_effects(false)
705 bool UnusedVariableRemover::apply(Stage &s)
708 variables.push_back(BlockVariableMap());
709 s.content.visit(*this);
711 BlockVariableMap &global_variables = variables.back();
712 set<InterfaceBlock *> used_interface_blocks;
713 Statement *prev_decl = 0;
715 for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
717 if(i->first.declaration!=prev_decl)
719 prev_decl = i->first.declaration;
720 output = i->second.output;
724 if(!i->second.assignments.empty() && i->second.interface_block)
725 used_interface_blocks.insert(i->second.interface_block);
729 // Mark other unreferenced global variables as unused.
730 if(!i->second.referenced)
732 if(!i->second.interface_block && !i->first.chain_len)
733 unused_nodes.insert(i->first.declaration);
734 clear_assignments(i->second, true);
736 else if(i->second.interface_block)
737 used_interface_blocks.insert(i->second.interface_block);
739 variables.pop_back();
741 for(map<string, InterfaceBlock *>::const_iterator i=s.interface_blocks.begin(); i!=s.interface_blocks.end(); ++i)
742 if(i->second->instance_name.empty() && !used_interface_blocks.count(i->second))
743 unused_nodes.insert(i->second);
745 NodeRemover().apply(s, unused_nodes);
747 return !unused_nodes.empty();
750 void UnusedVariableRemover::reference_used(Statement &declaration)
752 BlockVariableMap &block_vars = variables.back();
753 /* Previous assignments of all subfields of this variable are used by
755 for(BlockVariableMap::iterator i=block_vars.lower_bound(&declaration); (i!=block_vars.end() && i->first.declaration==&declaration); ++i)
757 clear_assignments(i->second, false);
758 i->second.referenced = true;
761 // Always record a reference to the primary declaration, even if it didn't exist before
762 block_vars[&declaration].referenced = true;
765 void UnusedVariableRemover::visit(VariableReference &var)
767 if(var.declaration && !assignment_target)
768 reference_used(*var.declaration);
771 void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
773 if(iface.declaration && !assignment_target)
774 reference_used(*iface.declaration);
777 void UnusedVariableRemover::visit(UnaryExpression &unary)
779 TraversingVisitor::visit(unary);
780 if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
781 r_side_effects = true;
784 void UnusedVariableRemover::visit(BinaryExpression &binary)
786 if(binary.oper->token[0]=='[')
788 binary.left->visit(*this);
789 SetFlag set(assignment_target, false);
790 binary.right->visit(*this);
793 TraversingVisitor::visit(binary);
796 void UnusedVariableRemover::visit(Assignment &assign)
799 SetFlag set(assignment_target, !assign.self_referencing);
800 assign.left->visit(*this);
802 assign.right->visit(*this);
803 r_assignment = &assign;
804 r_side_effects = true;
807 void UnusedVariableRemover::visit(FunctionCall &call)
809 TraversingVisitor::visit(call);
810 /* Treat function calls as having side effects so expression statements
811 consisting of nothing but a function call won't be optimized away. */
812 r_side_effects = true;
815 void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node, bool chained)
817 BlockVariableMap &block_vars = variables.back();
818 for(BlockVariableMap::iterator i=block_vars.lower_bound(target); (i!=block_vars.end() && i->first.declaration==target.declaration); ++i)
820 bool subfield = (i->first.chain_len>=target.chain_len);
821 for(unsigned j=0; (subfield && j<target.chain_len); ++j)
822 subfield = (i->first.chain[j]==target.chain[j]);
826 /* An assignment to the target causes any previous unreferenced
827 assignments to the same target or its subfields to be unused. */
829 clear_assignments(i->second, true);
832 VariableInfo &var_info = variables.back()[target];
833 var_info.assignments.push_back(&node);
834 var_info.conditionally_assigned = false;
837 void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused)
841 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
842 unused_nodes.insert(*i);
844 var_info.assignments.clear();
847 void UnusedVariableRemover::visit(ExpressionStatement &expr)
850 r_side_effects = false;
851 TraversingVisitor::visit(expr);
852 if(r_assignment && r_assignment->target.declaration)
853 record_assignment(r_assignment->target, expr, r_assignment->self_referencing);
855 unused_nodes.insert(&expr);
858 void UnusedVariableRemover::visit(VariableDeclaration &var)
860 VariableInfo &var_info = variables.back()[&var];
861 var_info.local = true;
862 var_info.interface_block = interface_block;
864 /* Mark variables as output if they're used by the next stage or the
867 var_info.output = (interface_block->interface=="out" && (interface_block->linked_block || !interface_block->name.compare(0, 3, "gl_")));
869 var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
871 if(var.init_expression)
872 record_assignment(&var, *var.init_expression, false);
873 TraversingVisitor::visit(var);
876 void UnusedVariableRemover::visit(InterfaceBlock &iface)
878 if(iface.instance_name.empty())
880 SetForScope<InterfaceBlock *> set_block(interface_block, &iface);
881 iface.struct_declaration->members.visit(*this);
885 VariableInfo &var_info = variables.back()[&iface];
886 var_info.local = true;
887 var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.name.compare(0, 3, "gl_")));
891 void UnusedVariableRemover::visit(FunctionDeclaration &func)
893 variables.push_back(BlockVariableMap());
895 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
897 func.body.visit(*this);
899 BlockVariableMap &block_variables = variables.back();
901 /* Mark global variables as conditionally assigned so assignments in other
902 functions won't be removed. */
903 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
905 i->second.conditionally_assigned = true;
907 /* Always treat function parameters as referenced. Removing unused
908 parameters is not currently supported. */
909 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
910 block_variables[i->get()].referenced = true;
912 merge_down_variables();
915 void UnusedVariableRemover::merge_down_variables()
917 BlockVariableMap &parent_variables = variables[variables.size()-2];
918 BlockVariableMap &block_variables = variables.back();
919 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
923 if(!i->second.referenced && !i->first.chain_len)
924 unused_nodes.insert(i->first.declaration);
925 /* Any unreferenced assignments when a variable runs out of scope
927 clear_assignments(i->second, true);
931 BlockVariableMap::iterator j = parent_variables.find(i->first);
932 if(j==parent_variables.end())
933 parent_variables.insert(*i);
936 // Merge a non-local variable's state into the parent scope.
937 if(i->second.referenced || !i->second.conditionally_assigned)
938 clear_assignments(j->second, !i->second.referenced);
939 j->second.conditionally_assigned = i->second.conditionally_assigned;
940 j->second.referenced |= i->second.referenced;
941 j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
944 variables.pop_back();
947 void UnusedVariableRemover::visit(Conditional &cond)
949 cond.condition->visit(*this);
950 variables.push_back(BlockVariableMap());
951 cond.body.visit(*this);
953 BlockVariableMap if_variables;
954 swap(variables.back(), if_variables);
955 cond.else_body.visit(*this);
957 // Combine variables from both branches.
958 BlockVariableMap &else_variables = variables.back();
959 for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
961 BlockVariableMap::iterator j = if_variables.find(i->first);
962 if(j!=if_variables.end())
964 // The variable was found in both branches.
965 i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
966 i->second.conditionally_assigned |= j->second.conditionally_assigned;
967 if_variables.erase(j);
970 // Mark variables found in only one branch as conditionally assigned.
971 i->second.conditionally_assigned = true;
974 /* Move variables which were only used in the if block into the combined
976 for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
978 i->second.conditionally_assigned = true;
979 else_variables.insert(*i);
982 merge_down_variables();
985 void UnusedVariableRemover::visit(Iteration &iter)
987 variables.push_back(BlockVariableMap());
988 TraversingVisitor::visit(iter);
989 merge_down_variables();
993 bool UnusedFunctionRemover::apply(Stage &stage)
995 stage.content.visit(*this);
996 NodeRemover().apply(stage, unused_nodes);
997 return !unused_nodes.empty();
1000 void UnusedFunctionRemover::visit(FunctionCall &call)
1002 TraversingVisitor::visit(call);
1004 unused_nodes.erase(call.declaration);
1005 if(call.declaration && call.declaration->definition!=call.declaration)
1006 used_definitions.insert(call.declaration->definition);
1009 void UnusedFunctionRemover::visit(FunctionDeclaration &func)
1011 TraversingVisitor::visit(func);
1013 if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
1014 unused_nodes.insert(&func);