X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=e7592c00a29956e7ad4ab09384cf9798b3d1aacf;hb=22d5405729048ee2677a1e45e309e6328de64a26;hp=8a888e15bec7c11e019b172570e758a06f46963b;hpb=03880839dcd2c067061ac5491083159a9fd06611;p=libs%2Fgl.git diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 8a888e15..e7592c00 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -3,7 +3,6 @@ #include #include -#include "evaluate.h" #include "visitor.h" namespace Msp { @@ -11,8 +10,8 @@ namespace GL { namespace SL { /** 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. */ +functions which have no flow control statements, no more than one return +statement, and are only called once. */ class InlineableFunctionLocator: private TraversingVisitor { private: @@ -40,12 +39,17 @@ dependencies of the inlined statements to appear before the target function. */ class InlineContentInjector: private TraversingVisitor { private: + enum Pass + { + DEPENDS, + REFERENCED, + INLINE, + RENAME + }; + FunctionDeclaration *source_func; - Block *target_block; - std::map variable_map; - std::string remap_prefix; - unsigned remap_names; - bool deps_only; + Block staging_block; + Pass pass; RefPtr r_inlined_statement; std::set dependencies; std::set referenced_names; @@ -54,7 +58,7 @@ private: public: InlineContentInjector(); - const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionDeclaration &); + const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionCall &); private: virtual void visit(VariableReference &); @@ -76,6 +80,7 @@ private: NodeList::iterator insert_point; RefPtr r_inline_result; bool r_any_inlined; + bool r_inlined_here; public: FunctionInliner(); @@ -137,19 +142,72 @@ private: virtual void visit(Iteration &); }; +/** Replaces expressions consisting entirely of literals with the results of +evaluating the expression.*/ +class ConstantFolder: private TraversingVisitor +{ +private: + VariableDeclaration *iteration_var; + Variant iter_init_value; + Variant r_constant_value; + bool iteration_init; + bool r_constant; + bool r_literal; + bool r_uses_iter_var; + bool r_any_folded; + +public: + bool apply(Stage &s) { s.content.visit(*this); return r_any_folded; } + +private: + static BasicTypeDeclaration::Kind get_value_kind(const Variant &); + template + static T evaluate_logical(char, T, T); + template + static bool evaluate_relation(const char *, T, T); + template + static T evaluate_arithmetic(char, T, T); + void set_result(const Variant &, bool = false); + + virtual void visit(RefPtr &); + virtual void visit(Literal &); + virtual void visit(VariableReference &); + virtual void visit(MemberAccess &); + virtual void visit(Swizzle &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(Assignment &); + virtual void visit(TernaryExpression &); + virtual void visit(FunctionCall &); + virtual void visit(VariableDeclaration &); + virtual void visit(Iteration &); +}; + /** Removes conditional statements and loops where the condition can be determined as constant at compile time. */ class ConstantConditionEliminator: private TraversingVisitor { private: + enum ConstantStatus + { + CONSTANT_FALSE, + CONSTANT_TRUE, + NOT_CONSTANT + }; + NodeList::iterator insert_point; std::set nodes_to_remove; + RefPtr r_ternary_result; public: void apply(Stage &); private: + ConstantStatus check_constant_condition(const Expression &); + virtual void visit(Block &); + virtual void visit(RefPtr &); + virtual void visit(TernaryExpression &); virtual void visit(Conditional &); virtual void visit(Iteration &); };