X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=inline;f=source%2Fglsl%2Foptimize.h;h=ed016ca0b9ddd26dd5e8e871b25a0f15484e2881;hb=3f899b1fc2e04f4fe74c99ad3e8ebb900c257214;hp=01508f450257ac4bee4b00dffd4e062449f3e8a5;hpb=48700161395ece1c8132ee687bf94cd62c6ec2df;p=libs%2Fgl.git diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 01508f45..ed016ca0 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -10,6 +10,9 @@ namespace Msp { namespace GL { namespace SL { +/** Finds functions which are candidates for inlining. Currently this means +functions which have no parameters, are only called once, and that call occurs +after the definition of the function. */ class InlineableFunctionLocator: private TraversingVisitor { private: @@ -20,14 +23,16 @@ private: public: InlineableFunctionLocator(); - const std::set &apply(Stage &s) { visit(s.content); return inlineable; } + const std::set &apply(Stage &s) { s.content.visit(*this); return inlineable; } private: virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); - using TraversingVisitor::visit; }; +/** Inlines functions. Internally uses InlineableFunctionLocator to find +candidate functions. Only functions which consist of a single return statement +are inlined. */ class FunctionInliner: private TraversingVisitor { private: @@ -36,9 +41,9 @@ private: RefPtr inline_result; public: - FunctionInliner(const std::set &); + FunctionInliner(); - void apply(Stage &s) { visit(s.content); } + void apply(Stage &); private: void visit_and_inline(RefPtr &); @@ -50,20 +55,22 @@ private: virtual void visit(FunctionCall &); virtual void visit(VariableDeclaration &); virtual void visit(Return &); - using TraversingVisitor::visit; }; -class ConstantConditionEliminator: private BlockModifier +/** Removes conditional statements and loops where the condition can be +determined as constant at compile time. */ +class ConstantConditionEliminator: private TraversingVisitor { private: - unsigned scope_level; bool record_only; ExpressionEvaluator::ValueMap variable_values; + NodeList::iterator insert_point; + std::set nodes_to_remove; public: ConstantConditionEliminator(); - void apply(Stage &s) { visit(s.content); } + void apply(Stage &); private: virtual void visit(Block &); @@ -72,10 +79,11 @@ private: virtual void visit(VariableDeclaration &); virtual void visit(Conditional &); virtual void visit(Iteration &); - using BlockModifier::visit; }; -class UnusedVariableLocator: private TraversingVisitor +/** Removes variable declarations with no references to them. Assignment +statements where the result is not used are also removed. */ +class UnusedVariableRemover: private TraversingVisitor { private: struct VariableInfo @@ -97,15 +105,15 @@ private: Assignment *assignment; bool assignment_target; bool assign_to_subscript; - bool global_scope; public: - UnusedVariableLocator(); + UnusedVariableRemover(); - const std::set &apply(Stage &); + bool apply(Stage &); private: virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); virtual void visit(MemberAccess &); virtual void visit(BinaryExpression &); virtual void visit(Assignment &); @@ -119,22 +127,21 @@ private: void merge_down_variables(); virtual void visit(Conditional &); virtual void visit(Iteration &); - using TraversingVisitor::visit; }; -class UnusedFunctionLocator: private TraversingVisitor +/** Removes function declarations with no references to them. */ +class UnusedFunctionRemover: private TraversingVisitor { private: std::set unused_nodes; std::set used_definitions; public: - const std::set &apply(Stage &s) { visit(s.content); return unused_nodes; } + bool apply(Stage &s); private: virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); - using TraversingVisitor::visit; }; } // namespace SL