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(FunctionCall &call)
237 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
238 visit_and_inline(*i);
240 FunctionDeclaration *def = call.declaration;
242 def = def->definition;
244 if(def && inlineable.count(def))
246 string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
248 // This will later get removed by UnusedVariableRemover.
249 if(result_name.empty())
250 result_name = "msp_unused_from_inline";
252 RefPtr<VariableReference> ref = new VariableReference;
253 ref->name = result_name;
254 r_inline_result = ref;
256 /* Inlined variables need to be resolved before this function can be
258 inlineable.erase(current_function);
262 void FunctionInliner::visit(ExpressionStatement &expr)
264 visit_and_inline(expr.expression);
267 void FunctionInliner::visit(VariableDeclaration &var)
269 if(var.init_expression)
270 visit_and_inline(var.init_expression);
273 void FunctionInliner::visit(FunctionDeclaration &func)
275 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
276 TraversingVisitor::visit(func);
279 void FunctionInliner::visit(Conditional &cond)
281 visit_and_inline(cond.condition);
282 cond.body.visit(*this);
285 void FunctionInliner::visit(Iteration &iter)
287 /* Visit the initialization statement before entering the loop body so the
288 inlined statements get inserted outside. */
289 if(iter.init_statement)
290 iter.init_statement->visit(*this);
292 SetForScope<Block *> set_block(current_block, &iter.body);
293 /* Skip the condition and loop expression parts because they're not properly
294 inside the body block. Inlining anything into them will require a more
295 comprehensive transformation. */
296 iter.body.visit(*this);
299 void FunctionInliner::visit(Return &ret)
302 visit_and_inline(ret.expression);
306 ExpressionInliner::ExpressionInfo::ExpressionInfo():
312 inline_on_rhs(false),
318 ExpressionInliner::ExpressionInliner():
320 r_any_inlined(false),
323 iteration_init(false),
328 bool ExpressionInliner::apply(Stage &s)
330 s.content.visit(*this);
331 return r_any_inlined;
334 void ExpressionInliner::visit_and_record(RefPtr<Expression> &ptr, const Operator *outer_oper, bool on_rhs)
338 if(r_ref_info && r_ref_info->expression && r_ref_info->available)
340 if(iteration_body && !r_ref_info->trivial)
342 /* Don't inline non-trivial expressions which were assigned outside
343 an iteration statement. The iteration may run multiple times, which
344 would cause the expression to also be evaluated multiple times. */
345 Block *i = r_ref_info->assign_scope;
346 for(; (i && i!=iteration_body); i=i->parent) ;
351 r_ref_info->outer_oper = outer_oper;
352 if(r_ref_info->trivial)
353 inline_expression(*r_ref_info->expression, ptr, outer_oper, r_ref_info->inner_oper, on_rhs);
356 /* Record the inline point for a non-trivial expression but don't
357 inline it yet. It might turn out it shouldn't be inlined after all. */
358 r_ref_info->inline_point = &ptr;
359 r_ref_info->inline_on_rhs = on_rhs;
365 void ExpressionInliner::inline_expression(Expression &expr, RefPtr<Expression> &ptr, const Operator *outer_oper, const Operator *inner_oper, bool on_rhs)
367 unsigned outer_precedence = (outer_oper ? outer_oper->precedence : 20);
368 unsigned inner_precedence = (inner_oper ? inner_oper->precedence : 0);
370 bool needs_parentheses = (inner_precedence>=outer_precedence);
371 if(inner_oper && inner_oper==outer_oper)
372 // Omit parentheses if the operator's natural grouping works out.
373 needs_parentheses = (inner_oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(inner_oper->assoc==Operator::RIGHT_TO_LEFT));
375 if(needs_parentheses)
377 RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
378 parexpr->expression = expr.clone();
384 r_any_inlined = true;
387 void ExpressionInliner::visit(Block &block)
389 TraversingVisitor::visit(block);
391 for(map<VariableDeclaration *, ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); )
393 map<string, VariableDeclaration *>::iterator j = block.variables.find(i->first->name);
394 if(j!=block.variables.end() && j->second==i->first)
396 if(i->second.expression && i->second.inline_point)
397 inline_expression(*i->second.expression, *i->second.inline_point, i->second.outer_oper, i->second.inner_oper, i->second.inline_on_rhs);
399 expressions.erase(i++);
403 /* The expression was assigned in this block and may depend on local
404 variables of the block. If this is a conditionally executed block,
405 the assignment might not always happen. Mark the expression as not
406 available to any outer blocks. */
407 if(i->second.assign_scope==&block)
408 i->second.available = false;
415 void ExpressionInliner::visit(VariableReference &var)
419 map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(var.declaration);
420 if(i!=expressions.end())
422 /* If a non-trivial expression is referenced multiple times, don't
424 if(i->second.inline_point && !i->second.trivial)
425 i->second.expression = 0;
426 /* Mutating expressions are analogous to self-referencing assignments
427 and prevent inlining. */
429 i->second.expression = 0;
430 r_ref_info = &i->second;
435 void ExpressionInliner::visit(MemberAccess &memacc)
437 visit_and_record(memacc.left, memacc.oper, false);
438 r_oper = memacc.oper;
442 void ExpressionInliner::visit(UnaryExpression &unary)
444 SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-');
445 visit_and_record(unary.expression, unary.oper, false);
450 void ExpressionInliner::visit(BinaryExpression &binary)
452 visit_and_record(binary.left, binary.oper, false);
454 SetFlag clear_target(mutating, false);
455 visit_and_record(binary.right, binary.oper, true);
457 r_oper = binary.oper;
461 void ExpressionInliner::visit(Assignment &assign)
464 SetFlag set_target(mutating);
465 visit_and_record(assign.left, assign.oper, false);
468 visit_and_record(assign.right, assign.oper, true);
470 if(assign.target_declaration)
472 map<VariableDeclaration *, ExpressionInfo>::iterator i = expressions.find(assign.target_declaration);
473 if(i!=expressions.end())
475 /* Self-referencing assignments can't be inlined without additional
476 work. Just clear any previous expression. */
477 i->second.expression = (assign.self_referencing ? 0 : assign.right.get());
478 i->second.assign_scope = current_block;
479 i->second.inline_point = 0;
480 i->second.inner_oper = r_oper;
481 i->second.available = true;
485 r_oper = assign.oper;
489 void ExpressionInliner::visit(FunctionCall &call)
491 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
492 visit_and_record(*i, 0, false);
497 void ExpressionInliner::visit(VariableDeclaration &var)
501 if(var.init_expression)
502 visit_and_record(var.init_expression, 0, false);
504 bool constant = var.constant;
505 if(constant && var.layout)
507 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
508 constant = (i->name!="constant_id");
511 /* Only inline global variables if they're constant and have trivial
512 initializers. Non-constant variables could change in ways which are hard to
513 analyze and non-trivial expressions could be expensive to inline. */
514 if((current_block->parent || (constant && r_trivial)) && var.interface.empty())
516 ExpressionInfo &info = expressions[&var];
517 /* Assume variables declared in an iteration initialization statement
518 will have their values change throughout the iteration. */
519 info.expression = (iteration_init ? 0 : var.init_expression.get());
520 info.assign_scope = current_block;
521 info.inner_oper = r_oper;
522 info.trivial = r_trivial;
526 void ExpressionInliner::visit(Conditional &cond)
528 visit_and_record(cond.condition, 0, false);
529 cond.body.visit(*this);
532 void ExpressionInliner::visit(Iteration &iter)
534 SetForScope<Block *> set_block(current_block, &iter.body);
535 if(iter.init_statement)
537 SetFlag set_init(iteration_init);
538 iter.init_statement->visit(*this);
541 SetForScope<Block *> set_body(iteration_body, &iter.body);
543 iter.condition->visit(*this);
544 iter.body.visit(*this);
545 if(iter.loop_expression)
546 iter.loop_expression->visit(*this);
549 void ExpressionInliner::visit(Return &ret)
552 visit_and_record(ret.expression, 0, false);
556 void ConstantConditionEliminator::apply(Stage &stage)
558 stage.content.visit(*this);
559 NodeRemover().apply(stage, nodes_to_remove);
562 void ConstantConditionEliminator::visit(Block &block)
564 SetForScope<Block *> set_block(current_block, &block);
565 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
572 void ConstantConditionEliminator::visit(Conditional &cond)
574 ExpressionEvaluator eval;
575 cond.condition->visit(eval);
576 if(eval.is_result_valid())
578 Block &block = (eval.get_result() ? cond.body : cond.else_body);
579 current_block->body.splice(insert_point, block.body);
580 nodes_to_remove.insert(&cond);
584 TraversingVisitor::visit(cond);
587 void ConstantConditionEliminator::visit(Iteration &iter)
591 /* If the loop condition is always false on the first iteration, the
592 entire loop can be removed */
593 ExpressionEvaluator::ValueMap values;
594 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
595 values[var] = var->init_expression.get();
596 ExpressionEvaluator eval(values);
597 iter.condition->visit(eval);
598 if(eval.is_result_valid() && !eval.get_result())
600 nodes_to_remove.insert(&iter);
605 TraversingVisitor::visit(iter);
609 UnusedVariableRemover::VariableInfo::VariableInfo():
611 conditionally_assigned(false),
616 bool UnusedTypeRemover::apply(Stage &stage)
618 stage.content.visit(*this);
619 NodeRemover().apply(stage, unused_nodes);
620 return !unused_nodes.empty();
623 void UnusedTypeRemover::visit(Literal &literal)
625 unused_nodes.erase(literal.type);
628 void UnusedTypeRemover::visit(UnaryExpression &unary)
630 unused_nodes.erase(unary.type);
631 TraversingVisitor::visit(unary);
634 void UnusedTypeRemover::visit(BinaryExpression &binary)
636 unused_nodes.erase(binary.type);
637 TraversingVisitor::visit(binary);
640 void UnusedTypeRemover::visit(FunctionCall &call)
642 unused_nodes.erase(call.type);
643 TraversingVisitor::visit(call);
646 void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
649 unused_nodes.erase(type.base_type);
650 unused_nodes.insert(&type);
653 void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
656 unused_nodes.erase(type.base_type);
657 unused_nodes.insert(&type);
660 void UnusedTypeRemover::visit(StructDeclaration &strct)
662 unused_nodes.insert(&strct);
663 TraversingVisitor::visit(strct);
666 void UnusedTypeRemover::visit(VariableDeclaration &var)
668 unused_nodes.erase(var.type_declaration);
671 void UnusedTypeRemover::visit(FunctionDeclaration &func)
673 unused_nodes.erase(func.return_type_declaration);
674 TraversingVisitor::visit(func);
678 UnusedVariableRemover::UnusedVariableRemover():
681 assignment_target(false),
682 r_assign_to_subfield(false),
683 r_side_effects(false)
686 bool UnusedVariableRemover::apply(Stage &stage)
688 variables.push_back(BlockVariableMap());
689 stage.content.visit(*this);
690 BlockVariableMap &global_variables = variables.back();
691 for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
693 /* Don't remove output variables which are used by the next stage or the
695 if(i->first->interface=="out" && (stage.type==Stage::FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
698 // Mark other unreferenced global variables as unused.
699 if(!i->second.referenced)
701 unused_nodes.insert(i->first);
702 clear_assignments(i->second, true);
705 variables.pop_back();
707 NodeRemover().apply(stage, unused_nodes);
709 return !unused_nodes.empty();
712 void UnusedVariableRemover::visit(VariableReference &var)
714 map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
715 if(i!=aggregates.end())
716 unused_nodes.erase(i->second);
718 if(var.declaration && !assignment_target)
720 VariableInfo &var_info = variables.back()[var.declaration];
721 // Previous assignments are used by this reference.
722 clear_assignments(var_info, false);
723 var_info.referenced = true;
727 void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
729 unused_nodes.erase(iface.declaration);
732 void UnusedVariableRemover::visit(MemberAccess &memacc)
734 if(assignment_target)
735 r_assign_to_subfield = true;
736 TraversingVisitor::visit(memacc);
737 unused_nodes.erase(memacc.declaration);
740 void UnusedVariableRemover::visit(UnaryExpression &unary)
742 TraversingVisitor::visit(unary);
743 if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
744 r_side_effects = true;
747 void UnusedVariableRemover::visit(BinaryExpression &binary)
749 if(binary.oper->token[0]=='[')
751 if(assignment_target)
752 r_assign_to_subfield = true;
753 binary.left->visit(*this);
754 SetFlag set(assignment_target, false);
755 binary.right->visit(*this);
758 TraversingVisitor::visit(binary);
761 void UnusedVariableRemover::visit(Assignment &assign)
764 SetFlag set(assignment_target, !assign.self_referencing);
765 assign.left->visit(*this);
767 assign.right->visit(*this);
768 r_assignment = &assign;
769 r_side_effects = true;
772 void UnusedVariableRemover::visit(FunctionCall &call)
774 TraversingVisitor::visit(call);
775 /* Treat function calls as having side effects so expression statements
776 consisting of nothing but a function call won't be optimized away. */
777 r_side_effects = true;
780 void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained)
782 VariableInfo &var_info = variables.back()[&var];
783 /* An assignment which completely replaces the value of the variable causes
784 any previous unreferenced assignments to be unused. */
786 clear_assignments(var_info, true);
787 var_info.assignments.push_back(&node);
788 var_info.conditionally_assigned = false;
791 void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused)
795 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
796 unused_nodes.insert(*i);
798 var_info.assignments.clear();
801 void UnusedVariableRemover::visit(ExpressionStatement &expr)
804 r_assign_to_subfield = false;
805 r_side_effects = false;
806 TraversingVisitor::visit(expr);
807 if(r_assignment && r_assignment->target_declaration)
808 record_assignment(*r_assignment->target_declaration, expr, (r_assignment->self_referencing || r_assign_to_subfield));
810 unused_nodes.insert(&expr);
813 void UnusedVariableRemover::visit(StructDeclaration &strct)
815 SetForScope<Node *> set(aggregate, &strct);
816 TraversingVisitor::visit(strct);
819 void UnusedVariableRemover::visit(VariableDeclaration &var)
822 aggregates[&var] = aggregate;
825 variables.back()[&var].local = true;
826 if(var.init_expression)
827 record_assignment(var, *var.init_expression, false);
829 TraversingVisitor::visit(var);
832 void UnusedVariableRemover::visit(InterfaceBlock &iface)
834 SetForScope<Node *> set(aggregate, &iface);
835 unused_nodes.insert(&iface);
836 TraversingVisitor::visit(iface);
839 void UnusedVariableRemover::visit(FunctionDeclaration &func)
841 variables.push_back(BlockVariableMap());
843 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
845 func.body.visit(*this);
847 BlockVariableMap &block_variables = variables.back();
849 /* Mark global variables as conditionally assigned so assignments in other
850 functions won't be removed. */
851 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
853 i->second.conditionally_assigned = true;
855 /* Always treat function parameters as referenced. Removing unused
856 parameters is not currently supported. */
857 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
858 block_variables[i->get()].referenced = true;
860 merge_down_variables();
863 void UnusedVariableRemover::merge_down_variables()
865 BlockVariableMap &parent_variables = variables[variables.size()-2];
866 BlockVariableMap &block_variables = variables.back();
867 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
871 if(!i->second.referenced)
872 unused_nodes.insert(i->first);
873 /* Any unreferenced assignments when a variable runs out of scope
875 clear_assignments(i->second, true);
879 BlockVariableMap::iterator j = parent_variables.find(i->first);
880 if(j==parent_variables.end())
881 parent_variables.insert(*i);
884 // Merge a non-local variable's state into the parent scope.
885 if(i->second.referenced || !i->second.conditionally_assigned)
886 clear_assignments(j->second, !i->second.referenced);
887 j->second.conditionally_assigned = i->second.conditionally_assigned;
888 j->second.referenced |= i->second.referenced;
889 j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
892 variables.pop_back();
895 void UnusedVariableRemover::visit(Conditional &cond)
897 cond.condition->visit(*this);
898 variables.push_back(BlockVariableMap());
899 cond.body.visit(*this);
901 BlockVariableMap if_variables;
902 swap(variables.back(), if_variables);
903 cond.else_body.visit(*this);
905 // Combine variables from both branches.
906 BlockVariableMap &else_variables = variables.back();
907 for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
909 BlockVariableMap::iterator j = if_variables.find(i->first);
910 if(j!=if_variables.end())
912 // The variable was found in both branches.
913 i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
914 i->second.conditionally_assigned |= j->second.conditionally_assigned;
915 if_variables.erase(j);
918 // Mark variables found in only one branch as conditionally assigned.
919 i->second.conditionally_assigned = true;
922 /* Move variables which were only used in the if block into the combined
924 for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
926 i->second.conditionally_assigned = true;
927 else_variables.insert(*i);
930 merge_down_variables();
933 void UnusedVariableRemover::visit(Iteration &iter)
935 variables.push_back(BlockVariableMap());
936 TraversingVisitor::visit(iter);
937 merge_down_variables();
941 bool UnusedFunctionRemover::apply(Stage &stage)
943 stage.content.visit(*this);
944 NodeRemover().apply(stage, unused_nodes);
945 return !unused_nodes.empty();
948 void UnusedFunctionRemover::visit(FunctionCall &call)
950 TraversingVisitor::visit(call);
952 unused_nodes.erase(call.declaration);
953 if(call.declaration && call.declaration->definition!=call.declaration)
954 used_definitions.insert(call.declaration->definition);
957 void UnusedFunctionRemover::visit(FunctionDeclaration &func)
959 TraversingVisitor::visit(func);
961 if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
962 unused_nodes.insert(&func);