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(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(FunctionCall &call)
221 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
224 FunctionDeclaration *def = call.declaration;
226 def = def->definition;
228 if(def && inlineable.count(def))
230 string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
232 // This will later get removed by UnusedVariableRemover.
233 if(result_name.empty())
234 result_name = "msp_unused_from_inline";
236 RefPtr<VariableReference> ref = new VariableReference;
237 ref->name = result_name;
238 r_inline_result = ref;
240 /* Inlined variables need to be resolved before this function can be
242 inlineable.erase(current_function);
246 void FunctionInliner::visit(FunctionDeclaration &func)
248 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
249 TraversingVisitor::visit(func);
252 void FunctionInliner::visit(Iteration &iter)
254 /* Visit the initialization statement before entering the loop body so the
255 inlined statements get inserted outside. */
256 if(iter.init_statement)
257 iter.init_statement->visit(*this);
259 SetForScope<Block *> set_block(current_block, &iter.body);
260 /* Skip the condition and loop expression parts because they're not properly
261 inside the body block. Inlining anything into them will require a more
262 comprehensive transformation. */
263 iter.body.visit(*this);
267 ExpressionInliner::ExpressionInfo::ExpressionInfo():
273 inline_on_rhs(false),
279 ExpressionInliner::ExpressionInliner():
281 r_any_inlined(false),
284 iteration_init(false),
289 bool ExpressionInliner::apply(Stage &s)
291 s.content.visit(*this);
292 return r_any_inlined;
295 void ExpressionInliner::visit_and_record(RefPtr<Expression> &ptr, const Operator *outer_oper, bool on_rhs)
299 if(r_ref_info && r_ref_info->expression && r_ref_info->available)
301 if(iteration_body && !r_ref_info->trivial)
303 /* Don't inline non-trivial expressions which were assigned outside
304 an iteration statement. The iteration may run multiple times, which
305 would cause the expression to also be evaluated multiple times. */
306 Block *i = r_ref_info->assign_scope;
307 for(; (i && i!=iteration_body); i=i->parent) ;
312 r_ref_info->outer_oper = outer_oper;
313 if(r_ref_info->trivial)
314 inline_expression(*r_ref_info->expression, ptr, outer_oper, r_ref_info->inner_oper, on_rhs);
317 /* Record the inline point for a non-trivial expression but don't
318 inline it yet. It might turn out it shouldn't be inlined after all. */
319 r_ref_info->inline_point = &ptr;
320 r_ref_info->inline_on_rhs = on_rhs;
326 void ExpressionInliner::inline_expression(Expression &expr, RefPtr<Expression> &ptr, const Operator *outer_oper, const Operator *inner_oper, bool on_rhs)
328 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
329 unsigned inner_precedence = (inner_oper ? inner_oper->precedence : 0);
331 bool needs_parentheses = (inner_precedence>=outer_precedence);
332 if(inner_oper && inner_oper==outer_oper)
333 // Omit parentheses if the operator's natural grouping works out.
334 needs_parentheses = (inner_oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(inner_oper->assoc==Operator::RIGHT_TO_LEFT));
336 if(needs_parentheses)
338 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
339 parexpr->expression = expr.clone();
345 r_any_inlined = true;
348 void ExpressionInliner::visit(Block &block)
350 TraversingVisitor::visit(block);
352 for(map<string, VariableDeclaration *>::iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
354 map<Assignment::Target, ExpressionInfo>::iterator j = expressions.lower_bound(i->second);
355 for(; (j!=expressions.end() && j->first.declaration==i->second); )
357 if(j->second.expression && j->second.inline_point)
358 inline_expression(*j->second.expression, *j->second.inline_point, j->second.outer_oper, j->second.inner_oper, j->second.inline_on_rhs);
360 expressions.erase(j++);
364 /* Expressions assigned in this block may depend on local variables of the
365 block. If this is a conditionally executed block, the assignments might not
366 always happen. Mark the expressions as not available to any outer blocks. */
367 for(map<Assignment::Target, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); ++i)
368 if(i->second.assign_scope==&block)
369 i->second.available = false;
372 void ExpressionInliner::visit(RefPtr<Expression> &expr)
374 visit_and_record(expr, 0, false);
377 void ExpressionInliner::visit(VariableReference &var)
381 map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(var.declaration);
382 if(i!=expressions.end())
384 /* If a non-trivial expression is referenced multiple times, don't
386 if(i->second.inline_point && !i->second.trivial)
387 i->second.expression = 0;
388 /* Mutating expressions are analogous to self-referencing assignments
389 and prevent inlining. */
391 i->second.expression = 0;
392 r_ref_info = &i->second;
397 void ExpressionInliner::visit(MemberAccess &memacc)
399 visit_and_record(memacc.left, memacc.oper, false);
400 r_oper = memacc.oper;
404 void ExpressionInliner::visit(Swizzle &swizzle)
406 visit_and_record(swizzle.left, swizzle.oper, false);
407 r_oper = swizzle.oper;
411 void ExpressionInliner::visit(UnaryExpression &unary)
413 SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-');
414 visit_and_record(unary.expression, unary.oper, false);
419 void ExpressionInliner::visit(BinaryExpression &binary)
421 visit_and_record(binary.left, binary.oper, false);
423 SetFlag clear_target(mutating, false);
424 visit_and_record(binary.right, binary.oper, true);
426 r_oper = binary.oper;
430 void ExpressionInliner::visit(Assignment &assign)
433 SetFlag set_target(mutating);
434 visit_and_record(assign.left, assign.oper, false);
437 visit_and_record(assign.right, assign.oper, true);
439 map<Assignment::Target, ExpressionInfo>::iterator i = expressions.find(assign.target);
440 if(i!=expressions.end())
442 /* Self-referencing assignments can't be inlined without additional
443 work. Just clear any previous expression. */
444 i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
445 i->second.assign_scope = current_block;
446 i->second.inline_point = 0;
447 i->second.inner_oper = r_oper;
448 i->second.available = true;
451 r_oper = assign.oper;
455 void ExpressionInliner::visit(FunctionCall &call)
457 TraversingVisitor::visit(call);
462 void ExpressionInliner::visit(VariableDeclaration &var)
466 TraversingVisitor::visit(var);
468 bool constant = var.constant;
469 if(constant && var.layout)
471 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
472 constant = (i->name!="constant_id");
475 /* Only inline global variables if they're constant and have trivial
476 initializers. Non-constant variables could change in ways which are hard to
477 analyze and non-trivial expressions could be expensive to inline. */
478 if((current_block->parent || (constant && r_trivial)) && var.interface.empty())
480 ExpressionInfo &info = expressions[&var];
481 /* Assume variables declared in an iteration initialization statement
482 will have their values change throughout the iteration. */
483 info.expression = (iteration_init ? 0 : var.init_expression.get());
484 info.assign_scope = current_block;
485 info.inner_oper = r_oper;
486 info.trivial = r_trivial;
490 void ExpressionInliner::visit(Iteration &iter)
492 SetForScope<Block *> set_block(current_block, &iter.body);
493 if(iter.init_statement)
495 SetFlag set_init(iteration_init);
496 iter.init_statement->visit(*this);
499 SetForScope<Block *> set_body(iteration_body, &iter.body);
501 visit(iter.condition);
502 iter.body.visit(*this);
503 if(iter.loop_expression)
504 visit(iter.loop_expression);
508 void ConstantConditionEliminator::apply(Stage &stage)
510 stage.content.visit(*this);
511 NodeRemover().apply(stage, nodes_to_remove);
514 void ConstantConditionEliminator::visit(Block &block)
516 SetForScope<Block *> set_block(current_block, &block);
517 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
524 void ConstantConditionEliminator::visit(Conditional &cond)
526 ExpressionEvaluator eval;
527 cond.condition->visit(eval);
528 if(eval.is_result_valid())
530 Block &block = (eval.get_result() ? cond.body : cond.else_body);
531 current_block->body.splice(insert_point, block.body);
532 nodes_to_remove.insert(&cond);
536 TraversingVisitor::visit(cond);
539 void ConstantConditionEliminator::visit(Iteration &iter)
543 /* If the loop condition is always false on the first iteration, the
544 entire loop can be removed */
545 ExpressionEvaluator::ValueMap values;
546 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
547 values[var] = var->init_expression.get();
548 ExpressionEvaluator eval(values);
549 iter.condition->visit(eval);
550 if(eval.is_result_valid() && !eval.get_result())
552 nodes_to_remove.insert(&iter);
557 TraversingVisitor::visit(iter);
561 bool UnusedTypeRemover::apply(Stage &stage)
563 stage.content.visit(*this);
564 NodeRemover().apply(stage, unused_nodes);
565 return !unused_nodes.empty();
568 void UnusedTypeRemover::visit(Literal &literal)
570 unused_nodes.erase(literal.type);
573 void UnusedTypeRemover::visit(UnaryExpression &unary)
575 unused_nodes.erase(unary.type);
576 TraversingVisitor::visit(unary);
579 void UnusedTypeRemover::visit(BinaryExpression &binary)
581 unused_nodes.erase(binary.type);
582 TraversingVisitor::visit(binary);
585 void UnusedTypeRemover::visit(FunctionCall &call)
587 unused_nodes.erase(call.type);
588 TraversingVisitor::visit(call);
591 void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
594 unused_nodes.erase(type.base_type);
595 unused_nodes.insert(&type);
598 void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
601 unused_nodes.erase(type.base_type);
602 unused_nodes.insert(&type);
605 void UnusedTypeRemover::visit(StructDeclaration &strct)
607 unused_nodes.insert(&strct);
608 TraversingVisitor::visit(strct);
611 void UnusedTypeRemover::visit(VariableDeclaration &var)
613 unused_nodes.erase(var.type_declaration);
616 void UnusedTypeRemover::visit(InterfaceBlock &iface)
618 unused_nodes.erase(iface.type_declaration);
621 void UnusedTypeRemover::visit(FunctionDeclaration &func)
623 unused_nodes.erase(func.return_type_declaration);
624 TraversingVisitor::visit(func);
628 UnusedVariableRemover::VariableInfo::VariableInfo():
631 conditionally_assigned(false),
637 UnusedVariableRemover::UnusedVariableRemover():
641 assignment_target(false),
642 r_side_effects(false)
645 bool UnusedVariableRemover::apply(Stage &s)
648 variables.push_back(BlockVariableMap());
649 s.content.visit(*this);
651 BlockVariableMap &global_variables = variables.back();
652 set<InterfaceBlock *> used_interface_blocks;
653 Statement *prev_decl = 0;
655 for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
657 if(i->first.declaration!=prev_decl)
659 prev_decl = i->first.declaration;
660 output = i->second.output;
664 if(!i->second.assignments.empty() && i->second.interface_block)
665 used_interface_blocks.insert(i->second.interface_block);
669 // Mark other unreferenced global variables as unused.
670 if(!i->second.referenced)
672 if(!i->second.interface_block && !i->first.chain_len)
673 unused_nodes.insert(i->first.declaration);
674 clear_assignments(i->second, true);
676 else if(i->second.interface_block)
677 used_interface_blocks.insert(i->second.interface_block);
679 variables.pop_back();
681 for(map<string, InterfaceBlock *>::const_iterator i=s.interface_blocks.begin(); i!=s.interface_blocks.end(); ++i)
682 if(i->second->instance_name.empty() && !used_interface_blocks.count(i->second))
683 unused_nodes.insert(i->second);
685 NodeRemover().apply(s, unused_nodes);
687 return !unused_nodes.empty();
690 void UnusedVariableRemover::reference_used(Statement &declaration)
692 BlockVariableMap &block_vars = variables.back();
693 /* Previous assignments of all subfields of this variable are used by
695 for(BlockVariableMap::iterator i=block_vars.lower_bound(&declaration); (i!=block_vars.end() && i->first.declaration==&declaration); ++i)
697 clear_assignments(i->second, false);
698 i->second.referenced = true;
701 // Always record a reference to the primary declaration, even if it didn't exist before
702 block_vars[&declaration].referenced = true;
705 void UnusedVariableRemover::visit(VariableReference &var)
707 if(var.declaration && !assignment_target)
708 reference_used(*var.declaration);
711 void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
713 if(iface.declaration && !assignment_target)
714 reference_used(*iface.declaration);
717 void UnusedVariableRemover::visit(UnaryExpression &unary)
719 TraversingVisitor::visit(unary);
720 if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
721 r_side_effects = true;
724 void UnusedVariableRemover::visit(BinaryExpression &binary)
726 if(binary.oper->token[0]=='[')
728 binary.left->visit(*this);
729 SetFlag set(assignment_target, false);
730 binary.right->visit(*this);
733 TraversingVisitor::visit(binary);
736 void UnusedVariableRemover::visit(Assignment &assign)
739 SetFlag set(assignment_target, !assign.self_referencing);
740 assign.left->visit(*this);
742 assign.right->visit(*this);
743 r_assignment = &assign;
744 r_side_effects = true;
747 void UnusedVariableRemover::visit(FunctionCall &call)
749 TraversingVisitor::visit(call);
750 /* Treat function calls as having side effects so expression statements
751 consisting of nothing but a function call won't be optimized away. */
752 r_side_effects = true;
755 void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node, bool chained)
757 BlockVariableMap &block_vars = variables.back();
758 for(BlockVariableMap::iterator i=block_vars.lower_bound(target); (i!=block_vars.end() && i->first.declaration==target.declaration); ++i)
760 bool subfield = (i->first.chain_len>=target.chain_len);
761 for(unsigned j=0; (subfield && j<target.chain_len); ++j)
762 subfield = (i->first.chain[j]==target.chain[j]);
766 /* An assignment to the target causes any previous unreferenced
767 assignments to the same target or its subfields to be unused. */
769 clear_assignments(i->second, true);
772 VariableInfo &var_info = variables.back()[target];
773 var_info.assignments.push_back(&node);
774 var_info.conditionally_assigned = false;
777 void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused)
781 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
782 unused_nodes.insert(*i);
784 var_info.assignments.clear();
787 void UnusedVariableRemover::visit(ExpressionStatement &expr)
790 r_side_effects = false;
791 TraversingVisitor::visit(expr);
792 if(r_assignment && r_assignment->target.declaration)
793 record_assignment(r_assignment->target, expr, r_assignment->self_referencing);
795 unused_nodes.insert(&expr);
798 void UnusedVariableRemover::visit(VariableDeclaration &var)
800 VariableInfo &var_info = variables.back()[&var];
801 var_info.local = true;
802 var_info.interface_block = interface_block;
804 /* Mark variables as output if they're used by the next stage or the
807 var_info.output = (interface_block->interface=="out" && (interface_block->linked_block || !interface_block->name.compare(0, 3, "gl_")));
809 var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
811 if(var.init_expression)
812 record_assignment(&var, *var.init_expression, false);
813 TraversingVisitor::visit(var);
816 void UnusedVariableRemover::visit(InterfaceBlock &iface)
818 if(iface.instance_name.empty())
820 SetForScope<InterfaceBlock *> set_block(interface_block, &iface);
821 iface.struct_declaration->members.visit(*this);
825 VariableInfo &var_info = variables.back()[&iface];
826 var_info.local = true;
827 var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.name.compare(0, 3, "gl_")));
831 void UnusedVariableRemover::visit(FunctionDeclaration &func)
833 variables.push_back(BlockVariableMap());
835 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
837 func.body.visit(*this);
839 BlockVariableMap &block_variables = variables.back();
841 /* Mark global variables as conditionally assigned so assignments in other
842 functions won't be removed. */
843 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
845 i->second.conditionally_assigned = true;
847 /* Always treat function parameters as referenced. Removing unused
848 parameters is not currently supported. */
849 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
850 block_variables[i->get()].referenced = true;
852 merge_down_variables();
855 void UnusedVariableRemover::merge_down_variables()
857 BlockVariableMap &parent_variables = variables[variables.size()-2];
858 BlockVariableMap &block_variables = variables.back();
859 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
863 if(!i->second.referenced && !i->first.chain_len)
864 unused_nodes.insert(i->first.declaration);
865 /* Any unreferenced assignments when a variable runs out of scope
867 clear_assignments(i->second, true);
871 BlockVariableMap::iterator j = parent_variables.find(i->first);
872 if(j==parent_variables.end())
873 parent_variables.insert(*i);
876 // Merge a non-local variable's state into the parent scope.
877 if(i->second.referenced || !i->second.conditionally_assigned)
878 clear_assignments(j->second, !i->second.referenced);
879 j->second.conditionally_assigned = i->second.conditionally_assigned;
880 j->second.referenced |= i->second.referenced;
881 j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
884 variables.pop_back();
887 void UnusedVariableRemover::visit(Conditional &cond)
889 cond.condition->visit(*this);
890 variables.push_back(BlockVariableMap());
891 cond.body.visit(*this);
893 BlockVariableMap if_variables;
894 swap(variables.back(), if_variables);
895 cond.else_body.visit(*this);
897 // Combine variables from both branches.
898 BlockVariableMap &else_variables = variables.back();
899 for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
901 BlockVariableMap::iterator j = if_variables.find(i->first);
902 if(j!=if_variables.end())
904 // The variable was found in both branches.
905 i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
906 i->second.conditionally_assigned |= j->second.conditionally_assigned;
907 if_variables.erase(j);
910 // Mark variables found in only one branch as conditionally assigned.
911 i->second.conditionally_assigned = true;
914 /* Move variables which were only used in the if block into the combined
916 for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
918 i->second.conditionally_assigned = true;
919 else_variables.insert(*i);
922 merge_down_variables();
925 void UnusedVariableRemover::visit(Iteration &iter)
927 variables.push_back(BlockVariableMap());
928 TraversingVisitor::visit(iter);
929 merge_down_variables();
933 bool UnusedFunctionRemover::apply(Stage &stage)
935 stage.content.visit(*this);
936 NodeRemover().apply(stage, unused_nodes);
937 return !unused_nodes.empty();
940 void UnusedFunctionRemover::visit(FunctionCall &call)
942 TraversingVisitor::visit(call);
944 unused_nodes.erase(call.declaration);
945 if(call.declaration && call.declaration->definition!=call.declaration)
946 used_definitions.insert(call.declaration->definition);
949 void UnusedFunctionRemover::visit(FunctionDeclaration &func)
951 TraversingVisitor::visit(func);
953 if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
954 unused_nodes.insert(&func);