]> git.tdb.fi Git - libs/gl.git/commitdiff
Remove expression statements without side effects
authorMikko Rasa <tdb@tdb.fi>
Mon, 1 Mar 2021 23:13:23 +0000 (01:13 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 1 Mar 2021 23:29:35 +0000 (01:29 +0200)
source/glsl/optimize.cpp
source/glsl/optimize.h

index 40d1c26f97f5f4886c2c3db4d28c9627aa499eb6..d6e5fa5592576ce3080bd46afb756d8e7dbb5c1e 100644 (file)
@@ -283,7 +283,8 @@ UnusedVariableRemover::UnusedVariableRemover():
        aggregate(0),
        assignment(0),
        assignment_target(false),
-       assign_to_subscript(false)
+       assign_to_subscript(false),
+       side_effects(false)
 { }
 
 bool UnusedVariableRemover::apply(Stage &stage)
@@ -333,6 +334,13 @@ void UnusedVariableRemover::visit(MemberAccess &memacc)
        unused_nodes.erase(memacc.declaration);
 }
 
+void UnusedVariableRemover::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+       if(unary.oper=="++" || unary.oper=="--")
+               side_effects = true;
+}
+
 void UnusedVariableRemover::visit(BinaryExpression &binary)
 {
        if(binary.oper=="[")
@@ -356,6 +364,13 @@ void UnusedVariableRemover::visit(Assignment &assign)
        }
        assign.right->visit(*this);
        assignment = &assign;
+       side_effects = true;
+}
+
+void UnusedVariableRemover::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+       side_effects = true;
 }
 
 void UnusedVariableRemover::record_assignment(VariableDeclaration &var, Node &node, bool chained)
@@ -380,9 +395,12 @@ void UnusedVariableRemover::clear_assignments(VariableInfo &var_info, bool mark_
 void UnusedVariableRemover::visit(ExpressionStatement &expr)
 {
        assignment = 0;
+       side_effects = false;
        TraversingVisitor::visit(expr);
        if(assignment && assignment->target_declaration)
                record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript));
+       if(!side_effects)
+               unused_nodes.insert(&expr);
 }
 
 void UnusedVariableRemover::visit(StructDeclaration &strct)
index eae90399542d3130ba38e08c24d87f6f13a7ff50..1c044fb47f0ea5e9da9efdda39a75f5fbccfee58 100644 (file)
@@ -124,6 +124,7 @@ private:
        Assignment *assignment;
        bool assignment_target;
        bool assign_to_subscript;
+       bool side_effects;
 
 public:
        UnusedVariableRemover();
@@ -134,10 +135,12 @@ private:
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(MemberAccess &);
+       virtual void visit(UnaryExpression &);
        virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
        void record_assignment(VariableDeclaration &, Node &, bool);
        void clear_assignments(VariableInfo &, bool);
+       virtual void visit(FunctionCall &);
        virtual void visit(ExpressionStatement &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);