X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=6250130fb30a39bfd9b18c82cdc9acf654c347ad;hb=c4aeeced7b397d46772577775bd3a0d6c4706cba;hp=fa75e6d7e867cdbb9f18f4a8bd0896e89f33f663;hpb=3a675b53b811f50ab965405fbbf91282cab7f3cd;p=libs%2Fgl.git diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index fa75e6d7..6250130f 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -3,16 +3,31 @@ #include #include -#include "evaluate.h" #include "visitor.h" namespace Msp { namespace GL { namespace SL { +/** Assigns values to specialization constants, turning them into normal +constants. */ +class ConstantSpecializer: private TraversingVisitor +{ +private: + const std::map *values; + +public: + ConstantSpecializer(); + + void apply(Stage &, const std::map &); + +private: + virtual void visit(VariableDeclaration &); +}; + /** 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 either builtins or only called once. */ class InlineableFunctionLocator: private TraversingVisitor { private: @@ -24,7 +39,7 @@ private: public: InlineableFunctionLocator(); - const std::set &apply(Stage &s) { s.content.visit(*this); return inlineable; } + std::set apply(Stage &s) { s.content.visit(*this); return inlineable; } private: virtual void visit(FunctionCall &); @@ -40,23 +55,27 @@ dependencies of the inlined statements to appear before the target function. */ class InlineContentInjector: private TraversingVisitor { private: + enum Pass + { + REFERENCED, + INLINE, + RENAME + }; + FunctionDeclaration *source_func; - Block *target_block; - std::map variable_map; - bool remap_names; - bool deps_only; + Block staging_block; + Pass pass; RefPtr r_inlined_statement; std::set dependencies; + std::set referenced_names; std::string r_result_name; public: InlineContentInjector(); - const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionDeclaration &); + std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionCall &); private: - std::string create_unused_name(const std::string &, bool); - virtual void visit(VariableReference &); virtual void visit(InterfaceBlockReference &); virtual void visit(FunctionCall &); @@ -76,6 +95,7 @@ private: NodeList::iterator insert_point; RefPtr r_inline_result; bool r_any_inlined; + bool r_inlined_here; public: FunctionInliner(); @@ -96,22 +116,32 @@ Variables which are only referenced once are also inlined. */ class ExpressionInliner: private TraversingVisitor { private: + struct ExpressionUse + { + RefPtr *reference; + Block *ref_scope; + bool blocked; + + ExpressionUse(): reference(0), ref_scope(0), blocked(false) { } + }; + struct ExpressionInfo { - Expression *expression; + Assignment::Target target; + RefPtr expression; Block *assign_scope; - RefPtr *inline_point; + std::vector uses; bool trivial; - bool available; - ExpressionInfo(); + ExpressionInfo(): expression(0), assign_scope(0), trivial(false) { } }; - std::map expressions; + std::list expressions; + std::map assignments; ExpressionInfo *r_ref_info; - bool r_any_inlined; bool r_trivial; - bool mutating; + bool access_read; + bool access_write; bool iteration_init; Block *iteration_body; const Operator *r_oper; @@ -122,8 +152,6 @@ public: bool apply(Stage &); private: - void inline_expression(Expression &, RefPtr &); - virtual void visit(Block &); virtual void visit(RefPtr &); virtual void visit(VariableReference &); virtual void visit(MemberAccess &); @@ -137,21 +165,97 @@ 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: + 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); + template + static T evaluate_int_special_op(char, T, T); + template + void convert_to_result(const Variant &); + 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 &); +}; + +class UnreachableCodeRemover: private TraversingVisitor +{ +private: + bool reachable; + std::set unreachable_nodes; + +public: + UnreachableCodeRemover(); + + virtual bool apply(Stage &); + private: virtual void visit(Block &); + virtual void visit(FunctionDeclaration &); virtual void visit(Conditional &); virtual void visit(Iteration &); + virtual void visit(Return &) { reachable = false; } + virtual void visit(Jump &) { reachable = false; } }; /** Removes types which are not used anywhere. */ @@ -164,11 +268,7 @@ public: bool apply(Stage &); private: - virtual void visit(Literal &); - virtual void visit(UnaryExpression &); - virtual void visit(BinaryExpression &); - virtual void visit(TernaryExpression &); - virtual void visit(FunctionCall &); + virtual void visit(RefPtr &); virtual void visit(BasicTypeDeclaration &); virtual void visit(ImageTypeDeclaration &); virtual void visit(StructDeclaration &); @@ -187,6 +287,7 @@ private: Node *node; Assignment::Target target; std::vector used_by; + unsigned in_loop; AssignmentInfo(): node(0) { } }; @@ -211,6 +312,11 @@ private: Assignment *r_assignment; bool assignment_target; bool r_side_effects; + bool in_struct; + bool composite_reference; + unsigned in_loop; + std::vector loop_ext_refs; + Assignment::Target r_reference; std::set unused_nodes; public: @@ -222,14 +328,17 @@ private: void referenced(const Assignment::Target &, Node &); virtual void visit(VariableReference &); virtual void visit(InterfaceBlockReference &); + void visit_composite(Expression &); + 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 &); void record_assignment(const Assignment::Target &, Node &); virtual void visit(ExpressionStatement &); - // Ignore structs because their members can't be accessed directly. - virtual void visit(StructDeclaration &) { } + virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); virtual void visit(InterfaceBlock &); void merge_variables(const BlockVariableMap &);