]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.h
Use default member initializers for simple types
[libs/gl.git] / source / glsl / optimize.h
index 08ee082b99cd9e070dd5003de3cdc936ef21d396..ae55e5af10f5590f886906355342327a8ddb87be 100644 (file)
@@ -9,21 +9,33 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+/** Assigns values to specialization constants, turning them into normal
+constants. */
+class ConstantSpecializer: private TraversingVisitor
+{
+private:
+       const std::map<std::string, int> *values = 0;
+
+public:
+       void apply(Stage &, const std::map<std::string, int> &);
+
+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:
        std::map<FunctionDeclaration *, unsigned> refcounts;
        std::set<FunctionDeclaration *> inlineable;
-       FunctionDeclaration *current_function;
-       unsigned return_count;
+       FunctionDeclaration *current_function = 0;
+       unsigned return_count = 0;
 
 public:
-       InlineableFunctionLocator();
-
-       const std::set<FunctionDeclaration *> &apply(Stage &s) { s.content.visit(*this); return inlineable; }
+       std::set<FunctionDeclaration *> apply(Stage &s) { s.content.visit(*this); return inlineable; }
 
 private:
        virtual void visit(FunctionCall &);
@@ -39,19 +51,23 @@ dependencies of the inlined statements to appear before the target function. */
 class InlineContentInjector: private TraversingVisitor
 {
 private:
-       FunctionDeclaration *source_func;
+       enum Pass
+       {
+               REFERENCED,
+               INLINE,
+               RENAME
+       };
+
+       FunctionDeclaration *source_func = 0;
        Block staging_block;
-       std::string remap_prefix;
-       unsigned remap_names;
+       Pass pass = REFERENCED;
        RefPtr<Statement> r_inlined_statement;
        std::set<Node *> dependencies;
        std::set<std::string> referenced_names;
        std::string r_result_name;
 
 public:
-       InlineContentInjector();
-
-       const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
+       std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionCall &);
 
 private:
        virtual void visit(VariableReference &);
@@ -67,17 +83,15 @@ are inlined. */
 class FunctionInliner: private TraversingVisitor
 {
 private:
-       Stage *stage;
+       Stage *stage = 0;
        std::set<FunctionDeclaration *> inlineable;
-       FunctionDeclaration *current_function;
+       FunctionDeclaration *current_function = 0;
        NodeList<Statement>::iterator insert_point;
        RefPtr<Expression> 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:
@@ -94,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<Expression> *inline_point;
-               bool trivial;
-               bool available;
+               RefPtr<Expression> *reference = 0;
+               Block *ref_scope = 0;
+               bool blocked = false;
+       };
 
-               ExpressionInfo();
+       struct ExpressionInfo
+       {
+               Assignment::Target target;
+               RefPtr<Expression> expression;
+               Block *assign_scope = 0;
+               std::vector<ExpressionUse> uses;
+               bool trivial = false;
        };
 
-       std::map<Assignment::Target, ExpressionInfo> 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<ExpressionInfo> expressions;
+       std::map<Assignment::Target, ExpressionInfo *> 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<Expression> &);
-       virtual void visit(Block &);
        virtual void visit(RefPtr<Expression> &);
        virtual void visit(VariableReference &);
        virtual void visit(MemberAccess &);
@@ -140,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<typename T>
        static T evaluate_logical(char, T, T);
        template<typename T>
        static bool evaluate_relation(const char *, T, T);
        template<typename T>
        static T evaluate_arithmetic(char, T, T);
+       template<typename T>
+       static T evaluate_int_special_op(char, T, T);
+       template<typename T>
+       void convert_to_result(const Variant &);
        void set_result(const Variant &, bool = false);
 
        virtual void visit(RefPtr<Expression> &);
@@ -177,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:
@@ -191,6 +211,7 @@ private:
        NodeList<Statement>::iterator insert_point;
        std::set<Node *> nodes_to_remove;
        RefPtr<Expression> r_ternary_result;
+       bool r_external_side_effects = false;
 
 public:
        void apply(Stage &);
@@ -200,9 +221,30 @@ private:
 
        virtual void visit(Block &);
        virtual void visit(RefPtr<Expression> &);
+       virtual void visit(UnaryExpression &);
+       virtual void visit(Assignment &);
        virtual void visit(TernaryExpression &);
+       virtual void visit(FunctionCall &);
+       virtual void visit(Conditional &);
+       virtual void visit(Iteration &);
+};
+
+class UnreachableCodeRemover: private TraversingVisitor
+{
+private:
+       bool reachable = true;
+       std::set<Node *> unreachable_nodes;
+
+public:
+       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. */
@@ -215,11 +257,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<Expression> &);
        virtual void visit(BasicTypeDeclaration &);
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
@@ -235,52 +273,55 @@ class UnusedVariableRemover: private TraversingVisitor
 private:
        struct AssignmentInfo
        {
-               Node *node;
+               Node *node = 0;
                Assignment::Target target;
                std::vector<Node *> used_by;
-
-               AssignmentInfo(): node(0) { }
+               unsigned in_loop = 0;
        };
 
        struct VariableInfo
        {
-               InterfaceBlock *interface_block;
+               InterfaceBlock *interface_block = 0;
                std::vector<AssignmentInfo *> 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<Statement *, VariableInfo> BlockVariableMap;
 
-       Stage *stage;
+       Stage *stage = 0;
        BlockVariableMap variables;
        std::list<AssignmentInfo> assignments;
-       InterfaceBlock *interface_block;
-       Assignment *r_assignment;
-       bool assignment_target;
-       bool r_side_effects;
+       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<Node *> loop_ext_refs;
+       Assignment::Target r_reference;
        std::set<Node *> unused_nodes;
 
 public:
-       UnusedVariableRemover();
-
        bool apply(Stage &);
 
 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 &);