X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=e74e6e0e812b3a3cdaf2f1b5286960fdc87487b2;hb=f76e62a74917dbd850912587a5e79240dec9681f;hp=698aa0b2617efd3ef1f6a98348aa754b19f04862;hpb=696a97bd7411d69953c1a9e4b5f3dfb4c1d848f1;p=libs%2Fgl.git diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 698aa0b2..e74e6e0e 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -10,60 +10,110 @@ namespace Msp { namespace GL { namespace SL { -class InlineableFunctionLocator: public StageVisitor +/** Finds functions which are candidates for inlining. Currently this means +functions which have no parameters, contain no more than one return statement, +and are only called once. */ +class InlineableFunctionLocator: private TraversingVisitor { -public: - typedef std::set ResultType; - private: std::map refcounts; std::set inlineable; - FunctionDeclaration *in_function; + FunctionDeclaration *current_function; + unsigned return_count; public: InlineableFunctionLocator(); - const ResultType &get_result() const { return inlineable; } - using StageVisitor::visit; + const std::set &apply(Stage &s) { s.content.visit(*this); return inlineable; } + +private: virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Return &); }; -class FunctionInliner: public StageVisitor +/** Injects statements from one function into another. Local variables are +renamed to avoid conflicts. After inlining, uses NodeReorderer to cause +dependencies of the inlined statements to appear before the target function. */ +class InlineContentInjector: private TraversingVisitor { private: + FunctionDeclaration *source_func; + Block *target_block; + std::map variable_map; + bool remap_names; + bool deps_only; + RefPtr inlined_statement; + std::set dependencies; + std::string result_name; + +public: + InlineContentInjector(); + + const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionDeclaration &); + +private: + std::string create_unused_name(const std::string &, bool); + + virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); + virtual void visit(FunctionCall &); + virtual void visit(VariableDeclaration &); + virtual void visit(Return &); +}; + +/** 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: + Stage *stage; std::set inlineable; - unsigned extract_result; + FunctionDeclaration *current_function; + NodeList::iterator insert_point; RefPtr inline_result; + bool any_inlined; public: FunctionInliner(); - FunctionInliner(const std::set &); + + bool apply(Stage &); private: void visit_and_inline(RefPtr &); -public: - using StageVisitor::visit; + virtual void visit(Block &); virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); virtual void visit(MemberAccess &); virtual void visit(FunctionCall &); + virtual void visit(ExpressionStatement &); virtual void visit(VariableDeclaration &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); virtual void visit(Return &); }; -class ConstantConditionEliminator: public 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(); - using StageVisitor::visit; + void apply(Stage &); + +private: virtual void visit(Block &); virtual void visit(UnaryExpression &); virtual void visit(Assignment &); @@ -72,11 +122,10 @@ public: virtual void visit(Iteration &); }; -class UnusedVariableLocator: public StageVisitor +/** Removes variable declarations with no references to them. Assignment +statements where the result is not used are also removed. */ +class UnusedVariableRemover: private TraversingVisitor { -public: - typedef std::set ResultType; - private: struct VariableInfo { @@ -97,21 +146,23 @@ private: Assignment *assignment; bool assignment_target; bool assign_to_subscript; - bool global_scope; + bool side_effects; public: - UnusedVariableLocator(); + UnusedVariableRemover(); + + bool apply(Stage &); - virtual void apply(Stage &); - const ResultType &get_result() const { return unused_nodes; } private: - using StageVisitor::visit; virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); virtual void visit(Assignment &); void record_assignment(VariableDeclaration &, Node &, bool); void clear_assignments(VariableInfo &, bool); + virtual void visit(FunctionCall &); virtual void visit(ExpressionStatement &); virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); @@ -122,18 +173,17 @@ private: virtual void visit(Iteration &); }; -class UnusedFunctionLocator: public StageVisitor +/** Removes function declarations with no references to them. */ +class UnusedFunctionRemover: private TraversingVisitor { -public: - typedef std::set ResultType; - private: std::set unused_nodes; std::set used_definitions; public: - const ResultType &get_result() const { return unused_nodes; } - using StageVisitor::visit; + bool apply(Stage &s); + +private: virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); };