namespace SL {
InlineableFunctionLocator::InlineableFunctionLocator():
- in_function(0)
+ current_function(0),
+ return_count(0)
{ }
void InlineableFunctionLocator::visit(FunctionCall &call)
{
unsigned &count = refcounts[def];
++count;
- if(count>1 || def==in_function)
+ if(count>1 || def==current_function)
inlineable.erase(def);
}
void InlineableFunctionLocator::visit(FunctionDeclaration &func)
{
unsigned &count = refcounts[func.definition];
- if(!count && func.parameters.empty())
+ if(count<=1 && func.parameters.empty())
inlineable.insert(func.definition);
- SetForScope<FunctionDeclaration *> set(in_function, &func);
+ SetForScope<FunctionDeclaration *> set(current_function, &func);
+ return_count = 0;
TraversingVisitor::visit(func);
}
+void InlineableFunctionLocator::visit(Conditional &)
+{
+ inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Iteration &)
+{
+ inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Return &)
+{
+ if(return_count)
+ inlineable.erase(current_function);
+ ++return_count;
+}
+
+
+InlineContentInjector::InlineContentInjector():
+ source_func(0),
+ remap_names(false),
+ deps_only(false)
+{ }
+
+const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgtb, const NodeList<Statement>::iterator &ins_pt, FunctionDeclaration &src)
+{
+ target_block = &tgtb;
+ source_func = &src;
+ for(NodeList<Statement>::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i)
+ {
+ inlined_statement = 0;
+ (*i)->visit(*this);
+ if(!inlined_statement)
+ inlined_statement = (*i)->clone();
+
+ SetFlag set_remap(remap_names);
+ inlined_statement->visit(*this);
+ tgtb.body.insert(ins_pt, inlined_statement);
+ }
+
+ NodeReorderer().apply(stage, target_func, dependencies);
+
+ return result_name;
+}
+
+string InlineContentInjector::create_unused_name(const string &base, bool always_prefix)
+{
+ string result = base;
+ if(always_prefix || target_block->variables.count(result))
+ result = format("_%s_%s", source_func->name, base);
+ unsigned initial_size = result.size();
+ for(unsigned i=1; target_block->variables.count(result); ++i)
+ {
+ result.erase(initial_size);
+ result += format("_%d", i);
+ }
+ return result;
+}
-void InlineDependencyCollector::visit(VariableReference &var)
+void InlineContentInjector::visit(VariableReference &var)
{
- if(var.declaration)
+ if(remap_names)
+ {
+ map<string, VariableDeclaration *>::const_iterator i = variable_map.find(var.name);
+ if(i!=variable_map.end())
+ var.name = i->second->name;
+ }
+ else if(var.declaration)
{
+ SetFlag set_deps(deps_only);
dependencies.insert(var.declaration);
var.declaration->visit(*this);
}
}
-void InlineDependencyCollector::visit(InterfaceBlockReference &iface)
+void InlineContentInjector::visit(InterfaceBlockReference &iface)
{
- if(iface.declaration)
+ if(!remap_names && iface.declaration)
{
+ SetFlag set_deps(deps_only);
dependencies.insert(iface.declaration);
iface.declaration->visit(*this);
}
}
-void InlineDependencyCollector::visit(FunctionCall &call)
+void InlineContentInjector::visit(FunctionCall &call)
{
- if(call.declaration)
+ if(!remap_names && call.declaration)
dependencies.insert(call.declaration);
TraversingVisitor::visit(call);
}
-void InlineDependencyCollector::visit(VariableDeclaration &var)
+void InlineContentInjector::visit(VariableDeclaration &var)
{
+ TraversingVisitor::visit(var);
+
if(var.type_declaration)
{
+ SetFlag set_deps(deps_only);
dependencies.insert(var.type_declaration);
var.type_declaration->visit(*this);
}
+
+ if(!remap_names && !deps_only)
+ {
+ RefPtr<VariableDeclaration> inlined_var = var.clone();
+ inlined_var->name = create_unused_name(var.name, false);
+ inlined_statement = inlined_var;
+
+ variable_map[var.name] = inlined_var.get();
+ }
+}
+
+void InlineContentInjector::visit(Return &ret)
+{
+ TraversingVisitor::visit(ret);
+
+ if(ret.expression)
+ {
+ result_name = create_unused_name("return", true);
+ RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ var->source = ret.source;
+ var->line = ret.line;
+ var->type = source_func->return_type;
+ var->name = result_name;
+ var->init_expression = ret.expression->clone();
+ inlined_statement = var;
+ }
}
FunctionInliner::FunctionInliner():
current_function(0),
- extract_result(0),
any_inlined(false)
{ }
void FunctionInliner::visit(Block &block)
{
- if(extract_result)
- --extract_result;
-
+ SetForScope<NodeList<Statement>::iterator> save_insert_point(insert_point, block.body.begin());
for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
+ insert_point = i;
(*i)->visit(*this);
- if(extract_result)
- --extract_result;
}
}
if(def && inlineable.count(def))
{
- extract_result = 2;
- def->visit(*this);
+ string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
+
+ // This will later get removed by UnusedVariableRemover
+ if(result_name.empty())
+ result_name = "msp_unused_from_inline";
- if(inline_result)
- NodeReorderer().apply(*stage, *current_function, InlineDependencyCollector().apply(*def));
+ RefPtr<VariableReference> ref = new VariableReference;
+ ref->name = result_name;
+ inline_result = ref;
+
+ /* Inlined variables need to be resolved before this function can be
+ inlined further. */
+ inlineable.erase(current_function);
}
else
inline_result = 0;
}
+void FunctionInliner::visit(ExpressionStatement &expr)
+{
+ visit_and_inline(expr.expression);
+}
+
void FunctionInliner::visit(VariableDeclaration &var)
{
if(var.init_expression)
void FunctionInliner::visit(Return &ret)
{
- TraversingVisitor::visit(ret);
-
- if(extract_result)
- inline_result = ret.expression->clone();
+ if(ret.expression)
+ visit_and_inline(ret.expression);
}
namespace SL {
/** Finds functions which are candidates for inlining. Currently this means
-functions which have no parameters and are only called once. */
+functions which have no parameters, contain no more than one return statement,
+and are only called once. */
class InlineableFunctionLocator: private TraversingVisitor
{
private:
std::map<FunctionDeclaration *, unsigned> refcounts;
std::set<FunctionDeclaration *> inlineable;
- FunctionDeclaration *in_function;
+ FunctionDeclaration *current_function;
+ unsigned return_count;
public:
InlineableFunctionLocator();
private:
virtual void visit(FunctionCall &);
virtual void visit(FunctionDeclaration &);
+ virtual void visit(Conditional &);
+ virtual void visit(Iteration &);
+ virtual void visit(Return &);
};
-/** Collects declarations referenced by a function. */
-class InlineDependencyCollector: private TraversingVisitor
+/** Injects statements from one function into another. Local variables are
+renamed to avoid conflicts. After inlining, uses NodeReorderer to cause
+dependencies of the inlined statements to appear before the target function. */
+class InlineContentInjector: private TraversingVisitor
{
private:
+ FunctionDeclaration *source_func;
+ Block *target_block;
+ std::map<std::string, VariableDeclaration *> variable_map;
+ bool remap_names;
+ bool deps_only;
+ RefPtr<Statement> inlined_statement;
std::set<Node *> dependencies;
+ std::string result_name;
public:
- const std::set<Node *> &apply(FunctionDeclaration &f) { f.visit(*this); return dependencies; }
+ InlineContentInjector();
+
+ const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
private:
+ std::string create_unused_name(const std::string &, bool);
+
virtual void visit(VariableReference &);
virtual void visit(InterfaceBlockReference &);
virtual void visit(FunctionCall &);
virtual void visit(VariableDeclaration &);
+ virtual void visit(Return &);
};
/** Inlines functions. Internally uses InlineableFunctionLocator to find
Stage *stage;
std::set<FunctionDeclaration *> inlineable;
FunctionDeclaration *current_function;
- unsigned extract_result;
+ NodeList<Statement>::iterator insert_point;
RefPtr<Expression> inline_result;
bool any_inlined;
virtual void visit(BinaryExpression &);
virtual void visit(MemberAccess &);
virtual void visit(FunctionCall &);
+ virtual void visit(ExpressionStatement &);
virtual void visit(VariableDeclaration &);
virtual void visit(FunctionDeclaration &);
virtual void visit(Return &);