+ 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;
+
+ 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, 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);
+ if(i!=assignments.end())
+ {
+ if(iteration_body && 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);
+ }
+
+ expressions.push_back(ExpressionInfo());
+ 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;
+
+ i->second = &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.push_back(ExpressionInfo());
+ 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);
+ }
+ }
+ }
+ }
+}
+
+void AggregateDismantler::visit(FunctionDeclaration &func)
+{
+ func.body.visit(*this);
+}
+
+
+template<typename T>
+T ConstantFolder::evaluate_logical(char oper, T left, T right)
+{
+ switch(oper)
+ {
+ case '&': return left&right;
+ case '|': return left|right;
+ case '^': return left^right;
+ default: return T();
+ }
+}
+
+template<typename T>
+bool ConstantFolder::evaluate_relation(const char *oper, T left, T right)
+{
+ switch(oper[0]|oper[1])
+ {
+ case '<': return left<right;
+ case '<'|'=': return left<=right;
+ case '>': return left>right;
+ case '>'|'=': return left>=right;
+ default: return false;
+ }
+}
+
+template<typename T>
+T ConstantFolder::evaluate_arithmetic(char oper, T left, T right)
+{
+ switch(oper)