X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=ae55e5af10f5590f886906355342327a8ddb87be;hp=ea61b671d2e1ea2a68b10c4b4682e8cb6a560260;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=72a02f2f3f1c454aa670b256262d7bc0541222e3 diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index ea61b671..ae55e5af 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -14,11 +14,9 @@ constants. */ class ConstantSpecializer: private TraversingVisitor { private: - const std::map *values; + const std::map *values = 0; public: - ConstantSpecializer(); - void apply(Stage &, const std::map &); private: @@ -33,12 +31,10 @@ class InlineableFunctionLocator: private TraversingVisitor private: std::map refcounts; std::set inlineable; - FunctionDeclaration *current_function; - unsigned return_count; + FunctionDeclaration *current_function = 0; + unsigned return_count = 0; public: - InlineableFunctionLocator(); - std::set apply(Stage &s) { s.content.visit(*this); return inlineable; } private: @@ -62,17 +58,15 @@ private: RENAME }; - FunctionDeclaration *source_func; + FunctionDeclaration *source_func = 0; Block staging_block; - Pass pass; + Pass pass = REFERENCED; RefPtr r_inlined_statement; std::set dependencies; std::set referenced_names; std::string r_result_name; public: - InlineContentInjector(); - std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionCall &); private: @@ -89,17 +83,15 @@ are inlined. */ class FunctionInliner: private TraversingVisitor { private: - Stage *stage; + Stage *stage = 0; std::set inlineable; - FunctionDeclaration *current_function; + FunctionDeclaration *current_function = 0; NodeList::iterator insert_point; RefPtr r_inline_result; - bool r_any_inlined; - bool r_inlined_here; + bool r_any_inlined = false; + bool r_inlined_here = false; public: - FunctionInliner(); - bool apply(Stage &); private: @@ -116,34 +108,36 @@ Variables which are only referenced once are also inlined. */ class ExpressionInliner: private TraversingVisitor { private: - struct ExpressionInfo + struct ExpressionUse { - Expression *expression; - Block *assign_scope; - RefPtr *inline_point; - bool trivial; - bool available; + RefPtr *reference = 0; + Block *ref_scope = 0; + bool blocked = false; + }; - ExpressionInfo(): expression(0), assign_scope(0), inline_point(0), trivial(false), available(true) { } + struct ExpressionInfo + { + Assignment::Target target; + RefPtr expression; + Block *assign_scope = 0; + std::vector uses; + bool trivial = false; }; - std::map expressions; - ExpressionInfo *r_ref_info; - bool r_any_inlined; - bool r_trivial; - bool mutating; - bool iteration_init; - Block *iteration_body; - const Operator *r_oper; + std::list expressions; + std::map assignments; + ExpressionInfo *r_ref_info = 0; + bool r_trivial = false; + bool access_read = true; + bool access_write = false; + bool iteration_init = false; + Block *iteration_body = 0; + const Operator *r_oper = 0; public: - ExpressionInliner(); - 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 &); @@ -162,26 +156,29 @@ evaluating the expression.*/ class ConstantFolder: private TraversingVisitor { private: - VariableDeclaration *iteration_var; + VariableDeclaration *iteration_var = 0; 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; + bool iteration_init = false; + bool r_constant = false; + bool r_literal = false; + bool r_uses_iter_var = false; + bool r_any_folded = false; 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); + 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 &); @@ -199,7 +196,8 @@ private: }; /** Removes conditional statements and loops where the condition can be -determined as constant at compile time. */ +determined as constant at compile time. Also removes such statements where +the body is empty and the condition has no side effects. */ class ConstantConditionEliminator: private TraversingVisitor { private: @@ -213,6 +211,7 @@ private: NodeList::iterator insert_point; std::set nodes_to_remove; RefPtr r_ternary_result; + bool r_external_side_effects = false; public: void apply(Stage &); @@ -222,7 +221,10 @@ private: virtual void visit(Block &); virtual void visit(RefPtr &); + virtual void visit(UnaryExpression &); + virtual void visit(Assignment &); virtual void visit(TernaryExpression &); + virtual void visit(FunctionCall &); virtual void visit(Conditional &); virtual void visit(Iteration &); }; @@ -230,12 +232,10 @@ private: class UnreachableCodeRemover: private TraversingVisitor { private: - bool reachable; + bool reachable = true; std::set unreachable_nodes; public: - UnreachableCodeRemover(); - virtual bool apply(Stage &); private: @@ -273,41 +273,38 @@ class UnusedVariableRemover: private TraversingVisitor private: struct AssignmentInfo { - Node *node; + Node *node = 0; Assignment::Target target; std::vector used_by; - - AssignmentInfo(): node(0) { } + unsigned in_loop = 0; }; struct VariableInfo { - InterfaceBlock *interface_block; + InterfaceBlock *interface_block = 0; std::vector assignments; - bool initialized; - bool output; - bool referenced; - - VariableInfo(): interface_block(0), initialized(false), output(false), referenced(false) { } + bool initialized = false; + bool output = false; + bool referenced = false; }; typedef std::map BlockVariableMap; - Stage *stage; + Stage *stage = 0; BlockVariableMap variables; std::list assignments; - InterfaceBlock *interface_block; - Assignment *r_assignment; - bool assignment_target; - bool r_side_effects; - bool in_struct; - bool composite_reference; + InterfaceBlock *interface_block = 0; + Assignment *r_assignment = 0; + bool assignment_target = false; + bool r_side_effects = false; + bool in_struct = false; + bool composite_reference = false; + unsigned in_loop = 0; + std::vector loop_ext_refs; Assignment::Target r_reference; std::set unused_nodes; public: - UnusedVariableRemover(); - bool apply(Stage &); private: