X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Foptimize.cpp;h=1e974c9f01f8eee6d3eea5faabc00b20759ffd37;hp=9388c04129cfc1b03020ef0f00da650bf2b46f1a;hb=209fc09bcf2947ca60ddf15b8903d0c3194c7d5d;hpb=e9a898f315b5d1396f196d785913a283c30940f2 diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 9388c041..1e974c9f 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -11,10 +11,6 @@ namespace Msp { namespace GL { namespace SL { -ConstantSpecializer::ConstantSpecializer(): - values(0) -{ } - void ConstantSpecializer::apply(Stage &stage, const map &v) { values = &v; @@ -59,11 +55,6 @@ void ConstantSpecializer::visit(VariableDeclaration &var) } -InlineableFunctionLocator::InlineableFunctionLocator(): - current_function(0), - return_count(0) -{ } - void InlineableFunctionLocator::visit(FunctionCall &call) { FunctionDeclaration *def = call.declaration; @@ -118,11 +109,6 @@ void InlineableFunctionLocator::visit(Return &ret) } -InlineContentInjector::InlineContentInjector(): - source_func(0), - pass(REFERENCED) -{ } - string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList::iterator &ins_pt, FunctionCall &call) { source_func = call.declaration->definition; @@ -257,12 +243,6 @@ void InlineContentInjector::visit(Return &ret) } -FunctionInliner::FunctionInliner(): - current_function(0), - r_any_inlined(false), - r_inlined_here(false) -{ } - bool FunctionInliner::apply(Stage &s) { stage = &s; @@ -348,16 +328,6 @@ void FunctionInliner::visit(Iteration &iter) } -ExpressionInliner::ExpressionInliner(): - r_ref_info(0), - r_trivial(false), - access_read(true), - access_write(false), - iteration_init(false), - iteration_body(0), - r_oper(0) -{ } - bool ExpressionInliner::apply(Stage &s) { s.content.visit(*this); @@ -550,6 +520,209 @@ void ExpressionInliner::visit(Iteration &iter) } +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 *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 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 &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 &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(binary.right.get())) + if(literal_subscript->value.check_type()) + index = literal_subscript->value.value(); + 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(var.type_declaration)) + { + const FunctionCall *init_call = dynamic_cast(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 &s: strct->members.body) + { + if(const VariableDeclaration *mem_decl = dynamic_cast(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(var.array_size.get())) + { + if(literal_size->value.check_type()) + { + Aggregate &aggre = aggregates[&var]; + aggre.declaration = &var; + aggre.decl_scope = current_block; + aggre.insert_point = insert_point; + + int size = literal_size->value.value(); + for(int i=0; i T ConstantFolder::evaluate_logical(char oper, T left, T right) { @@ -909,6 +1082,27 @@ void ConstantConditionEliminator::visit(RefPtr &expr) r_ternary_result = 0; } +void ConstantConditionEliminator::visit(UnaryExpression &unary) +{ + if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-') + if(const VariableReference *var = dynamic_cast(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) +{ + auto i = find_if(current_block->variables, [&assign](const pair &kvp){ return kvp.second==assign.target.declaration; }); + if(i==current_block->variables.end()) + r_external_side_effects = true; + TraversingVisitor::visit(assign); +} + void ConstantConditionEliminator::visit(TernaryExpression &ternary) { ConstantStatus result = check_constant_condition(*ternary.condition); @@ -918,6 +1112,12 @@ void ConstantConditionEliminator::visit(TernaryExpression &ternary) r_ternary_result = 0; } +void ConstantConditionEliminator::visit(FunctionCall &call) +{ + r_external_side_effects = true; + TraversingVisitor::visit(call); +} + void ConstantConditionEliminator::visit(Conditional &cond) { ConstantStatus result = check_constant_condition(*cond.condition); @@ -930,7 +1130,11 @@ void ConstantConditionEliminator::visit(Conditional &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) @@ -945,14 +1149,13 @@ void ConstantConditionEliminator::visit(Iteration &iter) } } + r_external_side_effects = false; TraversingVisitor::visit(iter); + if(iter.body.body.empty() && !r_external_side_effects) + nodes_to_remove.insert(&iter); } -UnreachableCodeRemover::UnreachableCodeRemover(): - reachable(true) -{ } - bool UnreachableCodeRemover::apply(Stage &stage) { stage.content.visit(*this); @@ -1046,17 +1249,6 @@ void UnusedTypeRemover::visit(FunctionDeclaration &func) } -UnusedVariableRemover::UnusedVariableRemover(): - stage(0), - interface_block(0), - r_assignment(0), - assignment_target(false), - r_side_effects(false), - in_struct(false), - composite_reference(false), - in_loop(0) -{ } - bool UnusedVariableRemover::apply(Stage &s) { stage = &s; @@ -1068,23 +1260,15 @@ bool UnusedVariableRemover::apply(Stage &s) for(const auto &kvp: variables) { - if(kvp.second.output) + if(!kvp.second.referenced) + unused_nodes.insert(kvp.first); + else 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) - { - // 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); } NodeRemover().apply(s, unused_nodes); @@ -1106,10 +1290,10 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n { Assignment::Target::ChainType type1 = static_cast(a->target.chain[j]&0xC0); Assignment::Target::ChainType type2 = static_cast(target.chain[j]&0xC0); + unsigned index1 = a->target.chain[j]&0x3F; + unsigned index2 = target.chain[j]&0x3F; 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) @@ -1120,7 +1304,7 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n covered as true */ } else - covered = (a->target.chain[j]==target.chain[j]); + covered = (type1==type2 && (index1==index2 || index1==0x3F || index2==0x3F)); } if(covered) @@ -1301,14 +1485,14 @@ void UnusedVariableRemover::visit(VariableDeclaration &var) 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_"))); + var_info.output = (var.interface=="out" && (stage->type==Stage::FRAGMENT || var.linked_declaration || !var.name.compare(0, 3, "gl_"))); + + // Linked outputs are automatically referenced. + if(var_info.output && var.linked_declaration) + var_info.referenced = true; if(var.init_expression) {