From: Mikko Rasa Date: Mon, 1 Mar 2021 23:13:23 +0000 (+0200) Subject: Remove expression statements without side effects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b9a001eeac28d75e73ef643500724585e03d6714;p=libs%2Fgl.git Remove expression statements without side effects --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 40d1c26f..d6e5fa55 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -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) diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index eae90399..1c044fb4 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -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 &);