]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.h
Remove conditional and iteration statements with no effect from GLSL
[libs/gl.git] / source / glsl / optimize.h
index 6d8a6194e7e9f5d2076fa81938a75aff421e39c2..92353a77ee1748c651d8d6f35ab266d6a429593c 100644 (file)
@@ -116,22 +116,32 @@ Variables which are only referenced once are also inlined. */
 class ExpressionInliner: private TraversingVisitor
 {
 private:
+       struct ExpressionUse
+       {
+               RefPtr<Expression> *reference;
+               Block *ref_scope;
+               bool blocked;
+
+               ExpressionUse(): reference(0), ref_scope(0), blocked(false) { }
+       };
+
        struct ExpressionInfo
        {
-               Expression *expression;
+               Assignment::Target target;
+               RefPtr<Expression> expression;
                Block *assign_scope;
-               RefPtr<Expression> *inline_point;
+               std::vector<ExpressionUse> uses;
                bool trivial;
-               bool available;
 
-               ExpressionInfo(): expression(0), assign_scope(0), inline_point(0), trivial(false), available(true) { }
+               ExpressionInfo(): expression(0), assign_scope(0), trivial(false) { }
        };
 
-       std::map<Assignment::Target, ExpressionInfo> expressions;
+       std::list<ExpressionInfo> expressions;
+       std::map<Assignment::Target, ExpressionInfo *> 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;
@@ -142,8 +152,6 @@ public:
        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 &);
@@ -202,7 +210,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:
@@ -216,6 +225,7 @@ private:
        NodeList<Statement>::iterator insert_point;
        std::set<Node *> nodes_to_remove;
        RefPtr<Expression> r_ternary_result;
+       bool r_external_side_effects;
 
 public:
        void apply(Stage &);
@@ -225,7 +235,10 @@ 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 &);
 };