]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.cpp
Use default member initializers for simple types
[libs/gl.git] / source / glsl / optimize.cpp
index afa6394e19018a4766cb3c05252f97d18700bbbc..8c586e7ad886cf433c4c5cadf6315783980804a6 100644 (file)
@@ -1,6 +1,9 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include "optimize.h"
+#include "reflect.h"
 
 using namespace std;
 
@@ -8,10 +11,49 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-InlineableFunctionLocator::InlineableFunctionLocator():
-       current_function(0),
-       return_count(0)
-{ }
+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)
 {
@@ -23,7 +65,9 @@ void InlineableFunctionLocator::visit(FunctionCall &call)
        {
                unsigned &count = refcounts[def];
                ++count;
-               if(count>1 || def==current_function)
+               /* 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);
        }
 
@@ -32,8 +76,11 @@ void InlineableFunctionLocator::visit(FunctionCall &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.parameters.empty())
+       if((count<=1 || func.source==BUILTIN_SOURCE) && !has_out_params)
                inlineable.insert(func.definition);
 
        SetForScope<FunctionDeclaration *> set(current_function, &func);
@@ -62,77 +109,94 @@ void InlineableFunctionLocator::visit(Return &ret)
 }
 
 
-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)
+string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList<Statement>::iterator &ins_pt, FunctionCall &call)
 {
-       target_block = &tgtb;
-       source_func = &src;
-       for(NodeList<Statement>::iterator i=src.body.body.begin(); i!=src.body.body.end(); ++i)
+       source_func = call.declaration->definition;
+
+       /* Populate referenced_names from the target function so we can rename
+       variables from the inlined function that would conflict. */
+       pass = REFERENCED;
+       target_func.visit(*this);
+
+       /* 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;
-               (*i)->visit(*this);
+               s->visit(*this);
                if(!r_inlined_statement)
-                       r_inlined_statement = (*i)->clone();
+                       r_inlined_statement = s->clone();
 
-               SetFlag set_remap(remap_names);
+               SetForScope<Pass> set_pass(pass, RENAME);
                r_inlined_statement->visit(*this);
-               tgtb.body.insert(ins_pt, r_inlined_statement);
+
+               staging_block.body.push_back_nocopy(r_inlined_statement);
        }
 
-       NodeReorderer().apply(stage, target_func, dependencies);
+       /* 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);
 
-       return r_result_name;
-}
+       /* 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);
 
-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;
+       // 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(remap_names)
+       if(pass==RENAME)
        {
-               map<string, VariableDeclaration *>::const_iterator i = variable_map.find(var.name);
-               if(i!=variable_map.end())
+               auto i = staging_block.variables.find(var.name);
+               if(i!=staging_block.variables.end())
                        var.name = i->second->name;
        }
-       else if(var.declaration)
-       {
-               SetFlag set_deps(deps_only);
-               dependencies.insert(var.declaration);
-               var.declaration->visit(*this);
-       }
+       else if(pass==REFERENCED)
+               referenced_names.insert(var.name);
 }
 
 void InlineContentInjector::visit(InterfaceBlockReference &iface)
 {
-       if(!remap_names && iface.declaration)
-       {
-               SetFlag set_deps(deps_only);
-               dependencies.insert(iface.declaration);
-               iface.declaration->visit(*this);
-       }
+       if(pass==REFERENCED)
+               referenced_names.insert(iface.name);
 }
 
 void InlineContentInjector::visit(FunctionCall &call)
 {
-       if(!remap_names && call.declaration)
-               dependencies.insert(call.declaration);
+       if(pass==REFERENCED)
+               referenced_names.insert(call.name);
        TraversingVisitor::visit(call);
 }
 
@@ -140,30 +204,34 @@ void InlineContentInjector::visit(VariableDeclaration &var)
 {
        TraversingVisitor::visit(var);
 
-       if(var.type_declaration)
+       if(pass==RENAME)
        {
-               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);
-               r_inlined_statement = inlined_var;
-
-               variable_map[var.name] = inlined_var.get();
+               /* 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(ret.expression)
+       if(pass==INLINE && ret.expression)
        {
-               r_result_name = create_unused_name("return", true);
+               // 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;
@@ -175,11 +243,6 @@ void InlineContentInjector::visit(Return &ret)
 }
 
 
-FunctionInliner::FunctionInliner():
-       current_function(0),
-       r_any_inlined(false)
-{ }
-
 bool FunctionInliner::apply(Stage &s)
 {
        stage = &s;
@@ -189,7 +252,7 @@ bool FunctionInliner::apply(Stage &s)
        return r_any_inlined;
 }
 
-void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
+void FunctionInliner::visit(RefPtr<Expression> &ptr)
 {
        r_inline_result = 0;
        ptr->visit(*this);
@@ -198,42 +261,27 @@ void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
                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(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(auto i=block.body.begin(); (!r_inlined_here && i!=block.body.end()); ++i)
        {
                insert_point = i;
                (*i)->visit(*this);
        }
 }
 
-void FunctionInliner::visit(UnaryExpression &unary)
-{
-       visit_and_inline(unary.expression);
-       r_inline_result = 0;
-}
-
-void FunctionInliner::visit(BinaryExpression &binary)
-{
-       visit_and_inline(binary.left);
-       visit_and_inline(binary.right);
-       r_inline_result = 0;
-}
-
-void FunctionInliner::visit(MemberAccess &memacc)
-{
-       visit_and_inline(memacc.left);
-       r_inline_result = 0;
-}
-
 void FunctionInliner::visit(FunctionCall &call)
 {
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_inline(*i);
+       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)
@@ -241,11 +289,11 @@ void FunctionInliner::visit(FunctionCall &call)
 
        if(def && inlineable.count(def))
        {
-               string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, *def);
+               string result_name = InlineContentInjector().apply(*stage, *current_function, *current_block, insert_point, call);
 
-               // This will later get removed by UnusedVariableRemover
+               // This will later get removed by UnusedVariableRemover.
                if(result_name.empty())
-                       result_name = "msp_unused_from_inline";
+                       result_name = "_msp_unused_from_inline";
 
                RefPtr<VariableReference> ref = new VariableReference;
                ref->name = result_name;
@@ -254,55 +302,549 @@ void FunctionInliner::visit(FunctionCall &call)
                /* Inlined variables need to be resolved before this function can be
                inlined further. */
                inlineable.erase(current_function);
+               r_inlined_here = true;
        }
-       else
-               r_inline_result = 0;
 }
 
-void FunctionInliner::visit(ExpressionStatement &expr)
+void FunctionInliner::visit(FunctionDeclaration &func)
 {
-       visit_and_inline(expr.expression);
+       SetForScope<FunctionDeclaration *> set_func(current_function, &func);
+       TraversingVisitor::visit(func);
+       r_inlined_here = false;
 }
 
-void FunctionInliner::visit(VariableDeclaration &var)
+void FunctionInliner::visit(Iteration &iter)
 {
-       if(var.init_expression)
-               visit_and_inline(var.init_expression);
-       r_inline_result = 0;
+       /* 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);
 }
 
-void FunctionInliner::visit(FunctionDeclaration &func)
+
+bool ExpressionInliner::apply(Stage &s)
 {
-       SetForScope<FunctionDeclaration *> set_func(current_function, &func);
-       TraversingVisitor::visit(func);
+       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 FunctionInliner::visit(Conditional &cond)
+void ExpressionInliner::visit(RefPtr<Expression> &expr)
 {
-       visit_and_inline(cond.condition);
-       cond.body.visit(*this);
+       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 FunctionInliner::visit(Iteration &iter)
+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);
-       /* Skip the condition and loop expression parts because they're executed on
-       every iteration of the loop */
+       }
+
+       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);
+}
+
+
+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)
+       {
+       case '+': return left+right;
+       case '-': return left-right;
+       case '*': return left*right;
+       case '/': return left/right;
+       default: return T();
+       }
+}
+
+template<typename T>
+T ConstantFolder::evaluate_int_special_op(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>
+void ConstantFolder::convert_to_result(const Variant &value)
+{
+       if(value.check_type<bool>())
+               set_result(static_cast<T>(value.value<bool>()));
+       else if(value.check_type<int>())
+               set_result(static_cast<T>(value.value<int>()));
+       else if(value.check_type<unsigned>())
+               set_result(static_cast<T>(value.value<unsigned>()));
+       else if(value.check_type<float>())
+               set_result(static_cast<T>(value.value<float>()));
+}
+
+void ConstantFolder::set_result(const Variant &value, bool literal)
+{
+       r_constant_value = value;
+       r_constant = true;
+       r_literal = literal;
+}
+
+void ConstantFolder::visit(RefPtr<Expression> &expr)
+{
+       r_constant_value = Variant();
+       r_constant = false;
+       r_literal = false;
+       r_uses_iter_var = false;
+       expr->visit(*this);
+       /* Don't replace literals since they'd only be replaced with an identical
+       literal.  Also skip anything that uses an iteration variable, but pass on
+       the result so the Iteration visiting function can handle it. */
+       if(!r_constant || r_literal || r_uses_iter_var)
+               return;
+
+       RefPtr<Literal> literal = new Literal;
+       if(r_constant_value.check_type<bool>())
+               literal->token = (r_constant_value.value<bool>() ? "true" : "false");
+       else if(r_constant_value.check_type<int>())
+               literal->token = lexical_cast<string>(r_constant_value.value<int>());
+       else if(r_constant_value.check_type<unsigned>())
+               literal->token = lexical_cast<string>(r_constant_value.value<unsigned>())+"u";
+       else if(r_constant_value.check_type<float>())
+       {
+               literal->token = lexical_cast<string>(r_constant_value.value<float>(), Fmt().precision(8));
+               if(literal->token.find('.')==string::npos && literal->token.find('e')==string::npos)
+                       literal->token += ".0";
+       }
+       else
+       {
+               r_constant = false;
+               return;
+       }
+       literal->value = r_constant_value;
+       expr = literal;
+       r_any_folded = true;
+}
+
+void ConstantFolder::visit(Literal &literal)
+{
+       set_result(literal.value, true);
+}
+
+void ConstantFolder::visit(VariableReference &var)
+{
+       /* If an iteration variable is initialized with a constant value, return
+       that value here for the purpose of evaluating the loop condition for the
+       first iteration. */
+       if(var.declaration==iteration_var)
+       {
+               set_result(iter_init_value);
+               r_uses_iter_var = true;
+       }
+}
+
+void ConstantFolder::visit(MemberAccess &memacc)
+{
+       TraversingVisitor::visit(memacc);
+       r_constant = false;
+}
+
+void ConstantFolder::visit(Swizzle &swizzle)
+{
+       TraversingVisitor::visit(swizzle);
+       r_constant = false;
+}
+
+void ConstantFolder::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+       bool can_fold = r_constant;
+       r_constant = false;
+       if(!can_fold)
+               return;
+
+       char oper = unary.oper->token[0];
+       char oper2 = unary.oper->token[1];
+       if(oper=='!')
+       {
+               if(r_constant_value.check_type<bool>())
+                       set_result(!r_constant_value.value<bool>());
+       }
+       else if(oper=='~')
+       {
+               if(r_constant_value.check_type<int>())
+                       set_result(~r_constant_value.value<int>());
+               else if(r_constant_value.check_type<unsigned>())
+                       set_result(~r_constant_value.value<unsigned>());
+       }
+       else if(oper=='-' && !oper2)
+       {
+               if(r_constant_value.check_type<int>())
+                       set_result(-r_constant_value.value<int>());
+               else if(r_constant_value.check_type<unsigned>())
+                       set_result(-r_constant_value.value<unsigned>());
+               else if(r_constant_value.check_type<float>())
+                       set_result(-r_constant_value.value<float>());
+       }
+}
+
+void ConstantFolder::visit(BinaryExpression &binary)
+{
+       visit(binary.left);
+       bool left_constant = r_constant;
+       bool left_iter_var = r_uses_iter_var;
+       Variant left_value = r_constant_value;
+       visit(binary.right);
+       if(left_iter_var)
+               r_uses_iter_var = true;
+
+       bool can_fold = (left_constant && r_constant);
+       r_constant = false;
+       if(!can_fold)
+               return;
+
+       // Currently only expressions with both sides of equal types are handled.
+       if(!left_value.check_same_type(r_constant_value))
+               return;
+
+       char oper = binary.oper->token[0];
+       char oper2 = binary.oper->token[1];
+       if(oper=='&' || oper=='|' || oper=='^')
+       {
+               if(oper2==oper && left_value.check_type<bool>())
+                       set_result(evaluate_logical(oper, left_value.value<bool>(), r_constant_value.value<bool>()));
+               else if(!oper2 && left_value.check_type<int>())
+                       set_result(evaluate_logical(oper, left_value.value<int>(), r_constant_value.value<int>()));
+               else if(!oper2 && left_value.check_type<unsigned>())
+                       set_result(evaluate_logical(oper, left_value.value<unsigned>(), r_constant_value.value<unsigned>()));
+       }
+       else if((oper=='<' || oper=='>') && oper2!=oper)
+       {
+               if(left_value.check_type<int>())
+                       set_result(evaluate_relation(binary.oper->token, left_value.value<int>(), r_constant_value.value<int>()));
+               else if(left_value.check_type<unsigned>())
+                       set_result(evaluate_relation(binary.oper->token, left_value.value<unsigned>(), r_constant_value.value<unsigned>()));
+               else if(left_value.check_type<float>())
+                       set_result(evaluate_relation(binary.oper->token, left_value.value<float>(), r_constant_value.value<float>()));
+       }
+       else if((oper=='=' || oper=='!') && oper2=='=')
+       {
+               if(left_value.check_type<int>())
+                       set_result((left_value.value<int>()==r_constant_value.value<int>()) == (oper=='='));
+               else if(left_value.check_type<unsigned>())
+                       set_result((left_value.value<unsigned>()==r_constant_value.value<unsigned>()) == (oper=='='));
+               else if(left_value.check_type<float>())
+                       set_result((left_value.value<float>()==r_constant_value.value<float>()) == (oper=='='));
+       }
+       else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
+       {
+               if(left_value.check_type<int>())
+                       set_result(evaluate_arithmetic(oper, left_value.value<int>(), r_constant_value.value<int>()));
+               else if(left_value.check_type<unsigned>())
+                       set_result(evaluate_arithmetic(oper, left_value.value<unsigned>(), r_constant_value.value<unsigned>()));
+               else if(left_value.check_type<float>())
+                       set_result(evaluate_arithmetic(oper, left_value.value<float>(), r_constant_value.value<float>()));
+       }
+       else if(oper=='%' || ((oper=='<' || oper=='>') && oper2==oper))
+       {
+               if(left_value.check_type<int>())
+                       set_result(evaluate_int_special_op(oper, left_value.value<int>(), r_constant_value.value<int>()));
+               else if(left_value.check_type<unsigned>())
+                       set_result(evaluate_int_special_op(oper, left_value.value<unsigned>(), r_constant_value.value<unsigned>()));
+       }
+}
+
+void ConstantFolder::visit(Assignment &assign)
+{
+       TraversingVisitor::visit(assign);
+       r_constant = false;
+}
+
+void ConstantFolder::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+       r_constant = false;
 }
 
-void FunctionInliner::visit(Return &ret)
+void ConstantFolder::visit(FunctionCall &call)
 {
-       if(ret.expression)
-               visit_and_inline(ret.expression);
+       if(call.constructor && call.type && call.arguments.size()==1)
+       {
+               const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(call.type);
+               if(basic)
+               {
+                       visit(call.arguments[0]);
+                       bool can_fold = r_constant;
+                       r_constant = false;
+                       if(!can_fold)
+                               return;
+
+                       if(basic->kind==BasicTypeDeclaration::BOOL)
+                               convert_to_result<bool>(r_constant_value);
+                       else if(basic->kind==BasicTypeDeclaration::INT && basic->size==32 && basic->sign)
+                               convert_to_result<int>(r_constant_value);
+                       else if(basic->kind==BasicTypeDeclaration::INT && basic->size==32 && !basic->sign)
+                               convert_to_result<unsigned>(r_constant_value);
+                       else if(basic->kind==BasicTypeDeclaration::FLOAT && basic->size==32)
+                               convert_to_result<float>(r_constant_value);
+
+                       return;
+               }
+       }
+
+       TraversingVisitor::visit(call);
+       r_constant = false;
 }
 
+void ConstantFolder::visit(VariableDeclaration &var)
+{
+       if(iteration_init && var.init_expression)
+       {
+               visit(var.init_expression);
+               if(r_constant)
+               {
+                       /* Record the value of a constant initialization expression of an
+                       iteration, so it can be used to evaluate the loop condition. */
+                       iteration_var = &var;
+                       iter_init_value = r_constant_value;
+               }
+       }
+       else
+               TraversingVisitor::visit(var);
+}
+
+void ConstantFolder::visit(Iteration &iter)
+{
+       SetForScope<Block *> set_block(current_block, &iter.body);
+
+       /* The iteration variable is not normally inlined into expressions, so we
+       process it specially here.  If the initial value causes the loop condition
+       to evaluate to false, then the expression can be folded. */
+       iteration_var = 0;
+       if(iter.init_statement)
+       {
+               SetFlag set_init(iteration_init);
+               iter.init_statement->visit(*this);
+       }
+
+       if(iter.condition)
+       {
+               visit(iter.condition);
+               if(r_constant && r_constant_value.check_type<bool>() && !r_constant_value.value<bool>())
+               {
+                       RefPtr<Literal> literal = new Literal;
+                       literal->token = "false";
+                       literal->value = r_constant_value;
+                       iter.condition = literal;
+               }
+       }
+       iteration_var = 0;
+
+       iter.body.visit(*this);
+       if(iter.loop_expression)
+               visit(iter.loop_expression);
+}
 
-ConstantConditionEliminator::ConstantConditionEliminator():
-       record_only(false)
-{ }
 
 void ConstantConditionEliminator::apply(Stage &stage)
 {
@@ -310,154 +852,325 @@ void ConstantConditionEliminator::apply(Stage &stage)
        NodeRemover().apply(stage, nodes_to_remove);
 }
 
+ConstantConditionEliminator::ConstantStatus ConstantConditionEliminator::check_constant_condition(const Expression &expr)
+{
+       if(const Literal *literal = dynamic_cast<const Literal *>(&expr))
+               if(literal->value.check_type<bool>())
+                       return (literal->value.value<bool>() ? CONSTANT_TRUE : CONSTANT_FALSE);
+       return NOT_CONSTANT;
+}
+
 void ConstantConditionEliminator::visit(Block &block)
 {
        SetForScope<Block *> set_block(current_block, &block);
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(auto i=block.body.begin(); i!=block.body.end(); ++i)
        {
                insert_point = i;
                (*i)->visit(*this);
        }
+}
 
-       for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
-               variable_values.erase(i->second);
+void ConstantConditionEliminator::visit(RefPtr<Expression> &expr)
+{
+       r_ternary_result = 0;
+       expr->visit(*this);
+       if(r_ternary_result)
+               expr = r_ternary_result;
+       r_ternary_result = 0;
 }
 
 void ConstantConditionEliminator::visit(UnaryExpression &unary)
 {
-       if(VariableReference *var = dynamic_cast<VariableReference *>(unary.expression.get()))
-               if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
-                       variable_values.erase(var->declaration);
+       if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
+               if(const VariableReference *var = dynamic_cast<const VariableReference *>(unary.expression.get()))
+               {
+                       auto i = current_block->variables.find(var->name);
+                       r_external_side_effects = (i==current_block->variables.end() || i->second!=var->declaration);
+                       return;
+               }
+
+       TraversingVisitor::visit(unary);
 }
 
 void ConstantConditionEliminator::visit(Assignment &assign)
 {
-       variable_values.erase(assign.target_declaration);
+       auto i = find_if(current_block->variables, [&assign](const pair<string, VariableDeclaration *> &kvp){ return kvp.second==assign.target.declaration; });
+       if(i==current_block->variables.end())
+               r_external_side_effects = true;
+       TraversingVisitor::visit(assign);
 }
 
-void ConstantConditionEliminator::visit(VariableDeclaration &var)
+void ConstantConditionEliminator::visit(TernaryExpression &ternary)
 {
-       bool constant = var.constant;
-       if(constant && var.layout)
-       {
-               for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
-                       constant = (i->name!="constant_id");
-       }
-       if((constant || current_block->parent) && var.init_expression)
-               variable_values[&var] = var.init_expression.get();
+       ConstantStatus result = check_constant_condition(*ternary.condition);
+       if(result!=NOT_CONSTANT)
+               r_ternary_result = (result==CONSTANT_TRUE ? ternary.true_expr : ternary.false_expr);
+       else
+               r_ternary_result = 0;
+}
+
+void ConstantConditionEliminator::visit(FunctionCall &call)
+{
+       r_external_side_effects = true;
+       TraversingVisitor::visit(call);
 }
 
 void ConstantConditionEliminator::visit(Conditional &cond)
 {
-       if(!record_only)
+       ConstantStatus result = check_constant_condition(*cond.condition);
+       if(result!=NOT_CONSTANT)
        {
-               ExpressionEvaluator eval(variable_values);
-               cond.condition->visit(eval);
-               if(eval.is_result_valid())
-               {
-                       Block &block = (eval.get_result() ? cond.body : cond.else_body);
-                       current_block->body.splice(insert_point, block.body);
-                       nodes_to_remove.insert(&cond);
-                       return;
-               }
+               Block &block = (result==CONSTANT_TRUE ? cond.body : cond.else_body);
+               // TODO should check variable names for conflicts.  Potentially reuse InlineContentInjector?
+               current_block->body.splice(insert_point, block.body);
+               nodes_to_remove.insert(&cond);
+               return;
        }
 
+       r_external_side_effects = false;
        TraversingVisitor::visit(cond);
+
+       if(cond.body.body.empty() && cond.else_body.body.empty() && !r_external_side_effects)
+               nodes_to_remove.insert(&cond);
 }
 
 void ConstantConditionEliminator::visit(Iteration &iter)
 {
-       if(!record_only)
+       if(iter.condition)
        {
-               if(iter.condition)
+               ConstantStatus result = check_constant_condition(*iter.condition);
+               if(result==CONSTANT_FALSE)
                {
-                       /* If the loop condition is always false on the first iteration, the
-                       entire loop can be removed */
-                       if(iter.init_statement)
-                               iter.init_statement->visit(*this);
-                       ExpressionEvaluator eval(variable_values);
-                       iter.condition->visit(eval);
-                       if(eval.is_result_valid() && !eval.get_result())
-                       {
-                               nodes_to_remove.insert(&iter);
-                               return;
-                       }
+                       nodes_to_remove.insert(&iter);
+                       return;
                }
-
-               /* Record all assignments that occur inside the loop body so those
-               variables won't be considered as constant */
-               SetFlag set_record(record_only);
-               TraversingVisitor::visit(iter);
        }
 
+       r_external_side_effects = false;
        TraversingVisitor::visit(iter);
+       if(iter.body.body.empty() && !r_external_side_effects)
+               nodes_to_remove.insert(&iter);
+}
 
-       if(VariableDeclaration *init_decl = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
-               variable_values.erase(init_decl);
+
+bool UnreachableCodeRemover::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       NodeRemover().apply(stage, unreachable_nodes);
+       return !unreachable_nodes.empty();
 }
 
+void UnreachableCodeRemover::visit(Block &block)
+{
+       auto i = block.body.begin();
+       for(; (reachable && i!=block.body.end()); ++i)
+               (*i)->visit(*this);
+       for(; i!=block.body.end(); ++i)
+               unreachable_nodes.insert(i->get());
+}
 
-UnusedVariableRemover::VariableInfo::VariableInfo():
-       local(false),
-       conditionally_assigned(false),
-       referenced(false)
-}
+void UnreachableCodeRemover::visit(FunctionDeclaration &func)
+{
+       TraversingVisitor::visit(func);
+       reachable = true;
+}
 
+void UnreachableCodeRemover::visit(Conditional &cond)
+{
+       cond.body.visit(*this);
+       bool reachable_if_true = reachable;
+       reachable = true;
+       cond.else_body.visit(*this);
+
+       reachable |= reachable_if_true;
+}
+
+void UnreachableCodeRemover::visit(Iteration &iter)
+{
+       TraversingVisitor::visit(iter);
+
+       /* Always consider code after a loop reachable, since there's no checking
+       for whether the loop executes. */
+       reachable = true;
+}
 
-UnusedVariableRemover::UnusedVariableRemover():
-       aggregate(0),
-       r_assignment(0),
-       assignment_target(false),
-       r_assign_to_subfield(false),
-       r_side_effects(false)
-{ }
 
-bool UnusedVariableRemover::apply(Stage &stage)
+bool UnusedTypeRemover::apply(Stage &stage)
 {
-       variables.push_back(BlockVariableMap());
        stage.content.visit(*this);
-       BlockVariableMap &global_variables = variables.back();
-       for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
+       NodeRemover().apply(stage, unused_nodes);
+       return !unused_nodes.empty();
+}
+
+void UnusedTypeRemover::visit(RefPtr<Expression> &expr)
+{
+       unused_nodes.erase(expr->type);
+       TraversingVisitor::visit(expr);
+}
+
+void UnusedTypeRemover::visit(BasicTypeDeclaration &type)
+{
+       if(type.base_type)
+               unused_nodes.erase(type.base_type);
+       unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(ImageTypeDeclaration &type)
+{
+       if(type.base_type)
+               unused_nodes.erase(type.base_type);
+       unused_nodes.insert(&type);
+}
+
+void UnusedTypeRemover::visit(StructDeclaration &strct)
+{
+       unused_nodes.insert(&strct);
+       TraversingVisitor::visit(strct);
+}
+
+void UnusedTypeRemover::visit(VariableDeclaration &var)
+{
+       unused_nodes.erase(var.type_declaration);
+       TraversingVisitor::visit(var);
+}
+
+void UnusedTypeRemover::visit(InterfaceBlock &iface)
+{
+       unused_nodes.erase(iface.type_declaration);
+}
+
+void UnusedTypeRemover::visit(FunctionDeclaration &func)
+{
+       unused_nodes.erase(func.return_type_declaration);
+       TraversingVisitor::visit(func);
+}
+
+
+bool UnusedVariableRemover::apply(Stage &s)
+{
+       stage = &s;
+       s.content.visit(*this);
+
+       for(const AssignmentInfo &a: assignments)
+               if(a.used_by.empty())
+                       unused_nodes.insert(a.node);
+
+       for(const auto &kvp: variables)
        {
-               if(i->first->interface=="out" && (stage.type==Stage::FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
-                       continue;
-               if(!i->second.referenced)
+               if(kvp.second.output)
+               {
+                       /* The last visible assignments of output variables are used by the
+                       next stage or the API. */
+                       for(AssignmentInfo *a: kvp.second.assignments)
+                               unused_nodes.erase(a->node);
+               }
+
+               if(!kvp.second.output && !kvp.second.referenced)
                {
-                       unused_nodes.insert(i->first);
-                       clear_assignments(i->second, true);
+                       // Don't remove variables from inside interface blocks.
+                       if(!kvp.second.interface_block)
+                               unused_nodes.insert(kvp.first);
                }
+               else if(kvp.second.interface_block)
+                       // Interface blocks are kept if even one member is used.
+                       unused_nodes.erase(kvp.second.interface_block);
        }
-       variables.pop_back();
 
-       NodeRemover().apply(stage, unused_nodes);
+       NodeRemover().apply(s, unused_nodes);
 
        return !unused_nodes.empty();
 }
 
-void UnusedVariableRemover::visit(VariableReference &var)
+void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &node)
 {
-       map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
-       if(i!=aggregates.end())
-               unused_nodes.erase(i->second);
-
-       if(var.declaration && !assignment_target)
+       VariableInfo &var_info = variables[target.declaration];
+       var_info.referenced = true;
+       if(!assignment_target)
        {
-               VariableInfo &var_info = variables.back()[var.declaration];
-               clear_assignments(var_info, false);
-               var_info.referenced = true;
+               bool loop_external = false;
+               for(AssignmentInfo *a: var_info.assignments)
+               {
+                       bool covered = true;
+                       for(unsigned j=0; (covered && j<a->target.chain_len && j<target.chain_len); ++j)
+                       {
+                               Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>(a->target.chain[j]&0xC0);
+                               Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target.chain[j]&0xC0);
+                               if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
+                               {
+                                       unsigned index1 = a->target.chain[j]&0x3F;
+                                       unsigned index2 = target.chain[j]&0x3F;
+                                       if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
+                                               covered = index1&index2;
+                                       else if(type1==Assignment::Target::ARRAY && index1<4)
+                                               covered = index2&(1<<index1);
+                                       else if(type2==Assignment::Target::ARRAY && index2<4)
+                                               covered = index1&(1<<index2);
+                                       /* If it's some other combination (shouldn't happen), leave
+                                       covered as true */
+                               }
+                               else
+                                       covered = (a->target.chain[j]==target.chain[j]);
+                       }
+
+                       if(covered)
+                       {
+                               a->used_by.push_back(&node);
+                               if(a->in_loop<in_loop)
+                                       loop_external = true;
+                       }
+               }
+
+               if(loop_external)
+                       loop_ext_refs.push_back(&node);
        }
 }
 
+void UnusedVariableRemover::visit(VariableReference &var)
+{
+       if(composite_reference)
+               r_reference.declaration = var.declaration;
+       else
+               referenced(var.declaration, var);
+}
+
 void UnusedVariableRemover::visit(InterfaceBlockReference &iface)
 {
-       unused_nodes.erase(iface.declaration);
+       if(composite_reference)
+               r_reference.declaration = iface.declaration;
+       else
+               referenced(iface.declaration, iface);
+}
+
+void UnusedVariableRemover::visit_composite(Expression &expr)
+{
+       if(!composite_reference)
+               r_reference = Assignment::Target();
+
+       SetFlag set_composite(composite_reference);
+       expr.visit(*this);
 }
 
 void UnusedVariableRemover::visit(MemberAccess &memacc)
 {
-       r_assign_to_subfield = true;
-       TraversingVisitor::visit(memacc);
-       unused_nodes.erase(memacc.declaration);
+       visit_composite(*memacc.left);
+
+       add_to_chain(r_reference, Assignment::Target::MEMBER, memacc.index);
+
+       if(!composite_reference && r_reference.declaration)
+               referenced(r_reference, memacc);
+}
+
+void UnusedVariableRemover::visit(Swizzle &swizzle)
+{
+       visit_composite(*swizzle.left);
+
+       unsigned mask = 0;
+       for(unsigned i=0; i<swizzle.count; ++i)
+               mask |= 1<<swizzle.components[i];
+       add_to_chain(r_reference, Assignment::Target::SWIZZLE, mask);
+
+       if(!composite_reference && r_reference.declaration)
+               referenced(r_reference, swizzle);
 }
 
 void UnusedVariableRemover::visit(UnaryExpression &unary)
@@ -471,20 +1184,36 @@ void UnusedVariableRemover::visit(BinaryExpression &binary)
 {
        if(binary.oper->token[0]=='[')
        {
-               if(assignment_target)
-                       r_assign_to_subfield = true;
-               binary.left->visit(*this);
-               SetForScope<bool> set(assignment_target, false);
-               binary.right->visit(*this);
+               visit_composite(*binary.left);
+
+               {
+                       SetFlag clear_assignment(assignment_target, false);
+                       SetFlag clear_composite(composite_reference, false);
+                       binary.right->visit(*this);
+               }
+
+               add_to_chain(r_reference, Assignment::Target::ARRAY, 0x3F);
+
+               if(!composite_reference && r_reference.declaration)
+                       referenced(r_reference, binary);
        }
        else
+       {
+               SetFlag clear_composite(composite_reference, false);
                TraversingVisitor::visit(binary);
+       }
+}
+
+void UnusedVariableRemover::visit(TernaryExpression &ternary)
+{
+       SetFlag clear_composite(composite_reference, false);
+       TraversingVisitor::visit(ternary);
 }
 
 void UnusedVariableRemover::visit(Assignment &assign)
 {
        {
-               SetForScope<bool> set(assignment_target, !assign.self_referencing);
+               SetFlag set(assignment_target, (assign.oper->token[0]=='='));
                assign.left->visit(*this);
        }
        assign.right->visit(*this);
@@ -494,158 +1223,175 @@ void UnusedVariableRemover::visit(Assignment &assign)
 
 void UnusedVariableRemover::visit(FunctionCall &call)
 {
+       SetFlag clear_composite(composite_reference, false);
        TraversingVisitor::visit(call);
+       /* Treat function calls as having side effects so expression statements
+       consisting of nothing but a function call won't be optimized away. */
        r_side_effects = true;
-}
 
-void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained)
-{
-       VariableInfo &var_info = variables.back()[&var];
-       if(!chained)
-               clear_assignments(var_info, true);
-       var_info.assignments.push_back(&node);
-       var_info.conditionally_assigned = false;
+       if(stage->type==Stage::GEOMETRY && call.name=="EmitVertex")
+       {
+               for(const auto &kvp: variables)
+                       if(kvp.second.output)
+                               referenced(kvp.first, call);
+       }
 }
 
-void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_unused)
+void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node)
 {
-       if(mark_unused)
+       assignments.push_back(AssignmentInfo());
+       AssignmentInfo &assign_info = assignments.back();
+       assign_info.node = &node;
+       assign_info.target = target;
+       assign_info.in_loop = in_loop;
+
+       /* An assignment to the target hides any assignments to the same target or
+       its subfields. */
+       VariableInfo &var_info = variables[target.declaration];
+       for(unsigned i=0; i<var_info.assignments.size(); )
        {
-               for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
-                       unused_nodes.insert(*i);
+               const Assignment::Target &t = var_info.assignments[i]->target;
+
+               bool subfield = (t.chain_len>=target.chain_len);
+               for(unsigned j=0; (subfield && j<target.chain_len); ++j)
+                       subfield = (t.chain[j]==target.chain[j]);
+
+               if(subfield)
+                       var_info.assignments.erase(var_info.assignments.begin()+i);
+               else
+                       ++i;
        }
-       var_info.assignments.clear();
+
+       var_info.assignments.push_back(&assign_info);
 }
 
 void UnusedVariableRemover::visit(ExpressionStatement &expr)
 {
        r_assignment = 0;
-       r_assign_to_subfield = false;
        r_side_effects = false;
        TraversingVisitor::visit(expr);
-       if(r_assignment && r_assignment->target_declaration)
-               record_assignment(*r_assignment->target_declaration, expr, (r_assignment->self_referencing || r_assign_to_subfield));
+       if(r_assignment && r_assignment->target.declaration)
+               record_assignment(r_assignment->target, expr);
        if(!r_side_effects)
                unused_nodes.insert(&expr);
 }
 
 void UnusedVariableRemover::visit(StructDeclaration &strct)
 {
-       SetForScope<Node *> set(aggregate, &strct);
-       unused_nodes.insert(&strct);
+       SetFlag set_struct(in_struct);
        TraversingVisitor::visit(strct);
 }
 
 void UnusedVariableRemover::visit(VariableDeclaration &var)
 {
-       if(aggregate)
-               aggregates[&var] = aggregate;
+       TraversingVisitor::visit(var);
+
+       if(in_struct)
+               return;
+
+       VariableInfo &var_info = variables[&var];
+       var_info.interface_block = interface_block;
+
+       /* Mark variables as output if they're used by the next stage or the
+       graphics API. */
+       if(interface_block)
+               var_info.output = (interface_block->interface=="out" && (interface_block->linked_block || !interface_block->block_name.compare(0, 3, "gl_")));
        else
+               var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_")));
+
+       if(var.init_expression)
        {
-               variables.back()[&var].local = true;
-               if(var.init_expression)
-                       record_assignment(var, *var.init_expression, false);
+               var_info.initialized = true;
+               record_assignment(&var, *var.init_expression);
        }
-       unused_nodes.erase(var.type_declaration);
-       TraversingVisitor::visit(var);
 }
 
 void UnusedVariableRemover::visit(InterfaceBlock &iface)
 {
-       SetForScope<Node *> set(aggregate, &iface);
-       unused_nodes.insert(&iface);
-       TraversingVisitor::visit(iface);
-}
-
-void UnusedVariableRemover::visit(FunctionDeclaration &func)
-{
-       variables.push_back(BlockVariableMap());
-
-       for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               (*i)->visit(*this);
-       func.body.visit(*this);
-
-       BlockVariableMap &block_variables = variables.back();
-       for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
-               i->second.conditionally_assigned = true;
-       for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               block_variables[i->get()].referenced = true;
-       merge_down_variables();
+       VariableInfo &var_info = variables[&iface];
+       var_info.output = (iface.interface=="out" && (iface.linked_block || !iface.block_name.compare(0, 3, "gl_")));
 }
 
-void UnusedVariableRemover::merge_down_variables()
+void UnusedVariableRemover::merge_variables(const BlockVariableMap &other_vars)
 {
-       BlockVariableMap &parent_variables = variables[variables.size()-2];
-       BlockVariableMap &block_variables = variables.back();
-       for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
+       for(const auto &kvp: other_vars)
        {
-               if(i->second.local)
+               auto j = variables.find(kvp.first);
+               if(j!=variables.end())
                {
-                       if(!i->second.referenced)
-                               unused_nodes.insert(i->first);
-                       clear_assignments(i->second, i->first->interface!="out");
-                       continue;
+                       /* The merged blocks started as copies of each other so any common
+                       assignments must be in the beginning. */
+                       unsigned k = 0;
+                       for(; (k<kvp.second.assignments.size() && k<j->second.assignments.size()); ++k)
+                               if(kvp.second.assignments[k]!=j->second.assignments[k])
+                                       break;
+
+                       // Remaining assignments are unique to each block; merge them.
+                       j->second.assignments.insert(j->second.assignments.end(), kvp.second.assignments.begin()+k, kvp.second.assignments.end());
+                       j->second.referenced |= kvp.second.referenced;
                }
-
-               BlockVariableMap::iterator j = parent_variables.find(i->first);
-               if(j==parent_variables.end())
-                       parent_variables.insert(*i);
                else
-               {
-                       if(i->second.referenced || !i->second.conditionally_assigned)
-                               clear_assignments(j->second, !i->second.referenced);
-                       j->second.conditionally_assigned = i->second.conditionally_assigned;
-                       j->second.referenced |= i->second.referenced;
-                       j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
-               }
+                       variables.insert(kvp);
        }
-       variables.pop_back();
 }
 
-void UnusedVariableRemover::visit(Conditional &cond)
+void UnusedVariableRemover::visit(FunctionDeclaration &func)
 {
-       cond.condition->visit(*this);
-       variables.push_back(BlockVariableMap());
-       cond.body.visit(*this);
+       if(func.body.body.empty())
+               return;
 
-       BlockVariableMap if_variables;
-       swap(variables.back(), if_variables);
-       cond.else_body.visit(*this);
+       BlockVariableMap saved_vars = variables;
+       // Assignments from other functions should not be visible.
+       for(auto &kvp: variables)
+               kvp.second.assignments.resize(kvp.second.initialized);
+       TraversingVisitor::visit(func);
+       swap(variables, saved_vars);
+       merge_variables(saved_vars);
 
-       BlockVariableMap &else_variables = variables.back();
-       for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
+       /* Always treat function parameters as referenced.  Removing unused
+       parameters is not currently supported. */
+       for(const RefPtr<VariableDeclaration> &p: func.parameters)
        {
-               BlockVariableMap::iterator j = if_variables.find(i->first);
-               if(j!=if_variables.end())
-               {
-                       i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
-                       i->second.conditionally_assigned |= j->second.conditionally_assigned;
-                       if_variables.erase(j);
-               }
-               else
-                       i->second.conditionally_assigned = true;
+               auto j = variables.find(p.get());
+               if(j!=variables.end())
+                       j->second.referenced = true;
        }
+}
 
-       for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
-       {
-               i->second.conditionally_assigned = true;
-               else_variables.insert(*i);
-       }
+void UnusedVariableRemover::visit(Conditional &cond)
+{
+       cond.condition->visit(*this);
+       BlockVariableMap saved_vars = variables;
+       cond.body.visit(*this);
+       swap(saved_vars, variables);
+       cond.else_body.visit(*this);
 
-       merge_down_variables();
+       /* Visible assignments after the conditional is the union of those visible
+       at the end of the if and else blocks.  If there was no else block, then it's
+       the union of the if block and the state before it. */
+       merge_variables(saved_vars);
 }
 
 void UnusedVariableRemover::visit(Iteration &iter)
 {
-       variables.push_back(BlockVariableMap());
-       TraversingVisitor::visit(iter);
+       BlockVariableMap saved_vars = variables;
+       vector<Node *> saved_refs;
+       swap(loop_ext_refs, saved_refs);
+       {
+               SetForScope<unsigned> set_loop(in_loop, in_loop+1);
+               TraversingVisitor::visit(iter);
+       }
+       swap(loop_ext_refs, saved_refs);
 
-       BlockVariableMap &block_variables = variables.back();
-       for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
-               if(!i->second.local && i->second.referenced)
-                       clear_assignments(i->second, false);
+       /* Visit the external references of the loop again to record assignments
+       done in the loop as used. */
+       for(Node *n: saved_refs)
+               n->visit(*this);
 
-       merge_down_variables();
+       /* Merge assignments from the iteration, without clearing previous state.
+       Further analysis is needed to determine which parts of the iteration body
+       are always executed, if any. */
+       merge_variables(saved_vars);
 }