+void ConstantSpecializer::apply(Stage &stage, const map<string, int> &v)
+{
+ values = &v;
+ stage.content.visit(*this);
+}
+
+void ConstantSpecializer::visit(VariableDeclaration &var)
+{
+ bool specializable = false;
+ if(var.layout)
+ {
+ vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
+ auto i = find_member(qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=qualifiers.end())
+ {
+ specializable = true;
+ qualifiers.erase(i);
+ if(qualifiers.empty())
+ var.layout = 0;
+ }
+ }
+
+ if(specializable)
+ {
+ auto i = values->find(var.name);
+ if(i!=values->end())
+ {
+ RefPtr<Literal> literal = new Literal;
+ if(var.type=="bool")
+ {
+ literal->token = (i->second ? "true" : "false");
+ literal->value = static_cast<bool>(i->second);
+ }
+ else if(var.type=="int")
+ {
+ literal->token = lexical_cast<string>(i->second);
+ literal->value = i->second;
+ }
+ var.init_expression = literal;
+ }
+ }
+}
+
+
+void InlineableFunctionLocator::visit(FunctionCall &call)
+{
+ FunctionDeclaration *def = call.declaration;
+ if(def)
+ def = def->definition;
+
+ if(def)
+ {
+ unsigned &count = refcounts[def];
+ ++count;
+ /* Don't inline functions which are called more than once or are called
+ recursively. */
+ if((count>1 && def->source!=BUILTIN_SOURCE) || def==current_function)
+ inlineable.erase(def);
+ }
+
+ TraversingVisitor::visit(call);
+}
+
+void InlineableFunctionLocator::visit(FunctionDeclaration &func)
+{
+ bool has_out_params = any_of(func.parameters.begin(), func.parameters.end(),
+ [](const RefPtr<VariableDeclaration> &p){ return p->interface=="out"; });
+
+ unsigned &count = refcounts[func.definition];
+ if((count<=1 || func.source==BUILTIN_SOURCE) && !has_out_params)
+ inlineable.insert(func.definition);
+
+ SetForScope<FunctionDeclaration *> set(current_function, &func);
+ return_count = 0;
+ TraversingVisitor::visit(func);
+}
+
+void InlineableFunctionLocator::visit(Conditional &cond)
+{
+ TraversingVisitor::visit(cond);
+ inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Iteration &iter)
+{
+ TraversingVisitor::visit(iter);
+ inlineable.erase(current_function);
+}
+
+void InlineableFunctionLocator::visit(Return &ret)
+{
+ TraversingVisitor::visit(ret);
+ if(return_count)
+ inlineable.erase(current_function);
+ ++return_count;
+}
+
+
+string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList<Statement>::iterator &ins_pt, FunctionCall &call)
+{
+ source_func = call.declaration->definition;
+
+ /* Populate referenced_names from the target function so we can rename
+ variables from the inlined function that would conflict. Only consider
+ names declared in blocks linearly related to the target block. */
+ pass = REFERENCED;
+ tgt_blk.visit(*this);
+ for(const Block *b=&tgt_blk; b; b=b->parent)
+ for(const auto &kvp: b->variables)
+ referenced_names.insert(kvp.first);
+ for(const auto &kvp: stage.interface_blocks)
+ if(kvp.second->name.find(' ')!=string::npos)
+ for(const auto &kvp2: kvp.second->block_declaration->members.variables)
+ referenced_names.insert(kvp2.first);
+
+ /* Inline and rename passes must be interleaved so used variable names are
+ known when inlining the return statement. */
+ pass = INLINE;
+ staging_block.parent = &tgt_blk;
+ staging_block.variables.clear();
+
+ vector<RefPtr<VariableDeclaration> > params;
+ params.reserve(source_func->parameters.size());
+ for(const RefPtr<VariableDeclaration> &p: source_func->parameters)
+ {
+ RefPtr<VariableDeclaration> var = p->clone();
+ var->interface.clear();
+
+ SetForScope<Pass> set_pass(pass, RENAME);
+ var->visit(*this);
+
+ staging_block.body.push_back_nocopy(var);
+ params.push_back(var);
+ }
+
+ for(const RefPtr<Statement> &s: source_func->body.body)
+ {
+ r_inlined_statement = 0;
+ s->visit(*this);
+ if(!r_inlined_statement)
+ r_inlined_statement = s->clone();
+
+ SetForScope<Pass> set_pass(pass, RENAME);
+ r_inlined_statement->visit(*this);
+
+ staging_block.body.push_back_nocopy(r_inlined_statement);
+ }
+
+ /* Now collect names from the staging block. Local variables that would
+ have conflicted with the target function were renamed earlier. */
+ pass = REFERENCED;
+ referenced_names.clear();
+ staging_block.variables.clear();
+ staging_block.visit(*this);
+
+ /* Rename variables in the target function so they don't interfere with
+ global identifiers used by the source function. */
+ pass = RENAME;
+ staging_block.parent = source_func->body.parent;
+ target_func.visit(*this);
+
+ // Put the argument expressions in place after all renaming has been done.
+ for(unsigned i=0; i<source_func->parameters.size(); ++i)
+ params[i]->init_expression = call.arguments[i]->clone();
+
+ tgt_blk.body.splice(ins_pt, staging_block.body);
+
+ NodeReorderer().apply(stage, target_func, DependencyCollector().apply(*source_func));
+
+ return r_result_name;
+}
+
+void InlineContentInjector::visit(VariableReference &var)
+{
+ if(pass==RENAME)
+ {
+ auto i = staging_block.variables.find(var.name);
+ if(i!=staging_block.variables.end())
+ var.name = i->second->name;
+ }
+ else if(pass==REFERENCED)
+ referenced_names.insert(var.name);
+}
+
+void InlineContentInjector::visit(FunctionCall &call)
+{
+ if(pass==REFERENCED)
+ referenced_names.insert(call.name);
+ TraversingVisitor::visit(call);
+}
+
+void InlineContentInjector::visit(VariableDeclaration &var)
+{
+ TraversingVisitor::visit(var);
+
+ if(pass==RENAME)
+ {
+ /* Check against conflicts with the other context as well as variables
+ already renamed here. */
+ bool conflict = (staging_block.variables.count(var.name) || referenced_names.count(var.name));
+ staging_block.variables[var.name] = &var;
+ if(conflict)
+ {
+ string mapped_name = get_unused_variable_name(staging_block, var.name);
+ if(mapped_name!=var.name)
+ {
+ staging_block.variables[mapped_name] = &var;
+ var.name = mapped_name;
+ }
+ }
+ }
+ else if(pass==REFERENCED)
+ referenced_names.insert(var.type);
+}
+
+void InlineContentInjector::visit(Return &ret)
+{
+ TraversingVisitor::visit(ret);
+
+ if(pass==INLINE && ret.expression)
+ {
+ // Create a new variable to hold the return value of the inlined function.
+ r_result_name = get_unused_variable_name(staging_block, "_return");
+ RefPtr<VariableDeclaration> var = new VariableDeclaration;
+ var->source = ret.source;
+ var->line = ret.line;
+ var->type = source_func->return_type;
+ var->name = r_result_name;
+ var->init_expression = ret.expression->clone();
+ r_inlined_statement = var;
+ }
+}
+
+
+bool FunctionInliner::apply(Stage &s)
+{
+ stage = &s;
+ inlineable = InlineableFunctionLocator().apply(s);
+ r_any_inlined = false;
+ s.content.visit(*this);
+ return r_any_inlined;
+}
+
+void FunctionInliner::visit(RefPtr<Expression> &ptr)
+{
+ r_inline_result = 0;
+ ptr->visit(*this);
+ if(r_inline_result)
+ {
+ ptr = r_inline_result;
+ r_any_inlined = true;
+ }
+ r_inline_result = 0;
+}
+
+void FunctionInliner::visit(Block &block)
+{
+ SetForScope<Block *> set_block(current_block, &block);
+ SetForScope<NodeList<Statement>::iterator> save_insert_point(insert_point, block.body.begin());
+ for(auto i=block.body.begin(); (!r_inlined_here && i!=block.body.end()); ++i)
+ {
+ insert_point = i;
+ (*i)->visit(*this);
+ }
+}
+
+void FunctionInliner::visit(FunctionCall &call)
+{
+ for(auto i=call.arguments.begin(); (!r_inlined_here && i!=call.arguments.end()); ++i)
+ visit(*i);
+
+ if(r_inlined_here)
+ return;
+
+ FunctionDeclaration *def = call.declaration;
+ if(def)
+ def = def->definition;
+
+ if(def && inlineable.count(def))
+ {
+ string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, call);
+
+ // This will later get removed by UnusedVariableRemover.
+ if(result_name.empty())
+ result_name = "_msp_unused_from_inline";
+
+ RefPtr<VariableReference> ref = new VariableReference;
+ ref->name = result_name;
+ r_inline_result = ref;
+
+ /* Inlined variables need to be resolved before this function can be
+ inlined further. */
+ inlineable.erase(current_function);
+ r_inlined_here = true;
+ }
+}
+
+void FunctionInliner::visit(FunctionDeclaration &func)
+{
+ SetForScope<FunctionDeclaration *> set_func(current_function, &func);
+ TraversingVisitor::visit(func);
+ r_inlined_here = false;
+}
+
+void FunctionInliner::visit(Iteration &iter)
+{
+ /* Visit the initialization statement before entering the loop body so the
+ inlined statements get inserted outside. */
+ if(iter.init_statement)
+ iter.init_statement->visit(*this);
+
+ SetForScope<Block *> set_block(current_block, &iter.body);
+ /* Skip the condition and loop expression parts because they're not properly
+ inside the body block. Inlining anything into them will require a more
+ comprehensive transformation. */
+ iter.body.visit(*this);
+}
+
+
+bool ExpressionInliner::apply(Stage &s)
+{
+ s.content.visit(*this);
+
+ bool any_inlined = false;
+ for(ExpressionInfo &e: expressions)
+ if(e.expression && (e.trivial || e.uses.size()==1))
+ {
+ for(ExpressionUse &u: e.uses)
+ if(!u.blocked)
+ {
+ *u.reference = e.expression->clone();
+ any_inlined = true;
+ }
+ }
+
+ return any_inlined;
+}
+
+void ExpressionInliner::visit(RefPtr<Expression> &expr)
+{
+ r_ref_info = 0;
+ expr->visit(*this);
+ if(r_ref_info && r_ref_info->expression)
+ {
+ ExpressionUse use;
+ use.reference = &expr;
+ use.ref_scope = current_block;
+ use.blocked = access_write || r_ref_info->blocked;
+
+ if(iteration_body && !r_ref_info->trivial)
+ {
+ /* Block inlining of non-trivial expressions assigned outside an
+ iteration statement. The iteration may run multiple times, which
+ would cause the expression to also be evaluated multiple times. */
+ for(Block *i=iteration_body->parent; (!use.blocked && i); i=i->parent)
+ use.blocked = (i==r_ref_info->assign_scope);
+ }
+
+ /* Block inlining assignments from from inner scopes. The assignment may
+ depend on local variables of that scope or may not always be executed. */
+ for(Block *i=r_ref_info->assign_scope->parent; (!use.blocked && i); i=i->parent)
+ use.blocked = (i==current_block);
+
+ r_ref_info->uses.push_back(use);
+ }
+ r_oper = expr->oper;
+ r_ref_info = 0;
+}
+
+void ExpressionInliner::visit(VariableReference &var)
+{
+ if(var.declaration && access_read)
+ {
+ auto i = assignments.find(var.declaration);
+ if(i!=assignments.end())
+ r_ref_info = i->second;
+ }
+}
+
+void ExpressionInliner::visit(MemberAccess &memacc)
+{
+ visit(memacc.left);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Swizzle &swizzle)
+{
+ visit(swizzle.left);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(UnaryExpression &unary)
+{
+ SetFlag set_write(access_write, (unary.oper->token[1]=='+' || unary.oper->token[1]=='-'));
+ visit(unary.expression);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(BinaryExpression &binary)
+{
+ visit(binary.left);
+ {
+ SetFlag clear_write(access_write, false);
+ visit(binary.right);
+ }
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(Assignment &assign)
+{
+ {
+ SetFlag set_read(access_read, assign.oper->token[0]!='=');
+ SetFlag set_write(access_write);
+ visit(assign.left);
+ }
+ r_oper = 0;
+ r_trivial = true;
+ visit(assign.right);
+
+ auto i = assignments.find(assign.target.declaration);
+ if(i!=assignments.end())
+ {
+ if(iteration_body && i->second && i->second->expression)
+ {
+ /* Block inlining into previous references within the iteration
+ statement. On iterations after the first they would refer to the
+ assignment within the iteration. */
+ for(ExpressionUse &u: i->second->uses)
+ for(Block *k=u.ref_scope; (!u.blocked && k); k=k->parent)
+ u.blocked = (k==iteration_body);
+ }
+
+ for(; (i!=assignments.end() && i->first.declaration==assign.target.declaration); ++i)
+ if(targets_overlap(i->first, assign.target))
+ i->second->blocked = true;
+
+ expressions.emplace_back();
+ ExpressionInfo &info = expressions.back();
+ info.target = assign.target;
+ // Self-referencing assignments can't be inlined without additional work.
+ if(!assign.self_referencing)
+ info.expression = assign.right;
+ info.assign_scope = current_block;
+ info.trivial = r_trivial;
+
+ assignments[assign.target] = &info;
+ }
+
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(TernaryExpression &ternary)
+{
+ visit(ternary.condition);
+ visit(ternary.true_expr);
+ visit(ternary.false_expr);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+ r_trivial = false;
+}
+
+void ExpressionInliner::visit(VariableDeclaration &var)
+{
+ r_oper = 0;
+ r_trivial = true;
+ TraversingVisitor::visit(var);
+
+ bool constant = var.constant;
+ if(constant && var.layout)
+ {
+ constant = !any_of(var.layout->qualifiers.begin(), var.layout->qualifiers.end(),
+ [](const Layout::Qualifier &q){ return q.name=="constant_id"; });
+ }
+
+ /* Only inline global variables if they're constant and have trivial
+ initializers. Non-constant variables could change in ways which are hard to
+ analyze and non-trivial expressions could be expensive to inline. */
+ if((current_block->parent || (constant && r_trivial)) && var.interface.empty())
+ {
+ expressions.emplace_back();
+ ExpressionInfo &info = expressions.back();
+ info.target = &var;
+ /* Assume variables declared in an iteration initialization statement
+ will have their values change throughout the iteration. */
+ if(!iteration_init)
+ info.expression = var.init_expression;
+ info.assign_scope = current_block;
+ info.trivial = r_trivial;
+
+ assignments[&var] = &info;
+ }
+}
+
+void ExpressionInliner::visit(Iteration &iter)
+{
+ SetForScope<Block *> set_block(current_block, &iter.body);
+ if(iter.init_statement)
+ {
+ SetFlag set_init(iteration_init);
+ iter.init_statement->visit(*this);
+ }
+
+ SetForScope<Block *> set_body(iteration_body, &iter.body);
+ if(iter.condition)
+ visit(iter.condition);
+ iter.body.visit(*this);
+ if(iter.loop_expression)
+ visit(iter.loop_expression);
+}
+
+
+bool AggregateDismantler::apply(Stage &stage)
+{
+ stage.content.visit(*this);
+
+ bool any_dismantled = false;
+ for(const auto &kvp: aggregates)
+ {
+ if(kvp.second.referenced || !kvp.second.members_referenced)
+ continue;
+
+ for(const AggregateMember &m: kvp.second.members)
+ {
+ string name;
+ if(m.declaration)
+ name = format("%s_%s", kvp.second.declaration->name, m.declaration->name);
+ else
+ name = format("%s_%d", kvp.second.declaration->name, m.index);
+
+ VariableDeclaration *var = new VariableDeclaration;
+ var->source = kvp.first->source;
+ var->line = kvp.first->line;
+ var->name = get_unused_variable_name(*kvp.second.decl_scope, name);
+ /* XXX This is kind of brittle and depends on the array declaration's
+ textual type not having brackets in it. */
+ var->type = (m.declaration ? m.declaration : kvp.second.declaration)->type;
+ if(m.initializer)
+ var->init_expression = m.initializer->clone();
+
+ kvp.second.decl_scope->body.insert(kvp.second.insert_point, var);
+
+ for(RefPtr<Expression> *r: m.references)
+ {
+ VariableReference *ref = new VariableReference;
+ ref->name = var->name;
+ *r = ref;
+ }
+
+ any_dismantled = true;
+ }
+ }
+
+ return any_dismantled;
+}
+
+void AggregateDismantler::visit(Block &block)
+{
+ SetForScope<Block *> set_block(current_block, &block);
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
+ {
+ insert_point = i;
+ (*i)->visit(*this);
+ }
+}
+
+void AggregateDismantler::visit(RefPtr<Expression> &expr)
+{
+ r_aggregate_ref = 0;
+ expr->visit(*this);
+ if(r_aggregate_ref && r_reference.chain_len==1)
+ {
+ if((r_reference.chain[0]&0x3F)!=0x3F)
+ {
+ r_aggregate_ref->members[r_reference.chain[0]&0x3F].references.push_back(&expr);
+ r_aggregate_ref->members_referenced = true;
+ }
+ else
+ /* If the accessed member is not known, mark the entire aggregate as
+ referenced. */
+ r_aggregate_ref->referenced = true;
+ }
+ r_aggregate_ref = 0;
+}
+
+void AggregateDismantler::visit(VariableReference &var)
+{
+ if(composite_reference)
+ r_reference.declaration = var.declaration;
+ else
+ {
+ /* If an aggregate variable is referenced as a whole, it should not be
+ dismantled. */
+ auto i = aggregates.find(var.declaration);
+ if(i!=aggregates.end())
+ i->second.referenced = true;
+ }
+}
+
+void AggregateDismantler::visit_composite(RefPtr<Expression> &expr)
+{
+ if(!composite_reference)
+ r_reference = Assignment::Target();
+
+ SetFlag set_composite(composite_reference);
+ visit(expr);
+}
+
+void AggregateDismantler::visit(MemberAccess &memacc)
+{
+ visit_composite(memacc.left);
+
+ add_to_chain(r_reference, Assignment::Target::MEMBER, memacc.index);
+
+ if(r_reference.declaration && r_reference.chain_len==1)
+ {
+ auto i = aggregates.find(r_reference.declaration);
+ r_aggregate_ref = (i!=aggregates.end() ? &i->second : 0);
+ }
+ else
+ r_aggregate_ref = 0;
+}
+
+void AggregateDismantler::visit(BinaryExpression &binary)
+{
+ if(binary.oper->token[0]=='[')
+ {
+ visit_composite(binary.left);
+ {
+ SetFlag clear_composite(composite_reference, false);
+ visit(binary.right);
+ }
+
+ unsigned index = 0x3F;
+ if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
+ if(literal_subscript->value.check_type<int>())
+ index = literal_subscript->value.value<int>();
+ add_to_chain(r_reference, Assignment::Target::ARRAY, index);
+
+ if(r_reference.declaration && r_reference.chain_len==1)
+ {
+ auto i = aggregates.find(r_reference.declaration);
+ r_aggregate_ref = (i!=aggregates.end() ? &i->second : 0);
+ }
+ else
+ r_aggregate_ref = 0;
+ }
+ else
+ {
+ SetFlag clear_composite(composite_reference, false);
+ TraversingVisitor::visit(binary);
+ }
+}
+
+void AggregateDismantler::visit(VariableDeclaration &var)
+{
+ TraversingVisitor::visit(var);
+
+ if(var.interface.empty())
+ {
+ if(const StructDeclaration *strct = dynamic_cast<const StructDeclaration *>(var.type_declaration))
+ {
+ const FunctionCall *init_call = dynamic_cast<const FunctionCall *>(var.init_expression.get());
+ if((init_call && init_call->constructor) || !var.init_expression)
+ {
+
+ Aggregate &aggre = aggregates[&var];
+ aggre.declaration = &var;
+ aggre.decl_scope = current_block;
+ aggre.insert_point = insert_point;
+
+ unsigned i = 0;
+ for(const RefPtr<Statement> &s: strct->members.body)
+ {
+ if(const VariableDeclaration *mem_decl = dynamic_cast<const VariableDeclaration *>(s.get()))
+ {
+ AggregateMember member;
+ member.declaration = mem_decl;
+ member.index = i;
+ if(init_call)
+ member.initializer = init_call->arguments[i];
+ aggre.members.push_back(member);
+ }
+ ++i;
+ }
+ }
+ }
+ else if(const Literal *literal_size = dynamic_cast<const Literal *>(var.array_size.get()))
+ {
+ if(literal_size->value.check_type<int>())
+ {
+ Aggregate &aggre = aggregates[&var];
+ aggre.declaration = &var;
+ aggre.decl_scope = current_block;
+ aggre.insert_point = insert_point;
+
+ int size = literal_size->value.value<int>();
+ for(int i=0; i<size; ++i)
+ {
+ AggregateMember member;
+ member.index = i;
+ // Array initializers are not supported yet
+ aggre.members.push_back(member);
+ }
+ }
+ }
+ }
+}