From: Mikko Rasa Date: Sat, 13 Mar 2021 19:29:40 +0000 (+0200) Subject: Cleanup ExpressionInliner X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3a675b53b811f50ab965405fbbf91282cab7f3cd;p=libs%2Fgl.git Cleanup ExpressionInliner --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 0d18b76c..22001797 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -289,33 +289,6 @@ bool ExpressionInliner::apply(Stage &s) return r_any_inlined; } -void ExpressionInliner::visit_and_record(RefPtr &ptr) -{ - r_ref_info = 0; - ptr->visit(*this); - if(r_ref_info && r_ref_info->expression && r_ref_info->available) - { - if(iteration_body && !r_ref_info->trivial) - { - /* Don't inline non-trivial expressions which were assigned outside - an iteration statement. The iteration may run multiple times, which - would cause the expression to also be evaluated multiple times. */ - Block *i = r_ref_info->assign_scope; - for(; (i && i!=iteration_body); i=i->parent) ; - if(!i) - return; - } - - if(r_ref_info->trivial) - inline_expression(*r_ref_info->expression, ptr); - else - /* Record the inline point for a non-trivial expression but don't - inline it yet. It might turn out it shouldn't be inlined after all. */ - r_ref_info->inline_point = &ptr; - } - r_ref_info = 0; -} - void ExpressionInliner::inline_expression(Expression &expr, RefPtr &ptr) { ptr = expr.clone(); @@ -348,7 +321,30 @@ void ExpressionInliner::visit(Block &block) void ExpressionInliner::visit(RefPtr &expr) { - visit_and_record(expr); + r_ref_info = 0; + expr->visit(*this); + if(r_ref_info && r_ref_info->expression && r_ref_info->available) + { + if(iteration_body && !r_ref_info->trivial) + { + /* Don't inline non-trivial expressions which were assigned outside + an iteration statement. The iteration may run multiple times, which + would cause the expression to also be evaluated multiple times. */ + Block *i = r_ref_info->assign_scope; + for(; (i && i!=iteration_body); i=i->parent) ; + if(!i) + return; + } + + if(r_ref_info->trivial) + inline_expression(*r_ref_info->expression, expr); + else + /* Record the inline point for a non-trivial expression but don't + inline it yet. It might turn out it shouldn't be inlined after all. */ + r_ref_info->inline_point = &expr; + } + r_oper = expr->oper; + r_ref_info = 0; } void ExpressionInliner::visit(VariableReference &var) @@ -373,34 +369,30 @@ void ExpressionInliner::visit(VariableReference &var) void ExpressionInliner::visit(MemberAccess &memacc) { - visit_and_record(memacc.left); - r_oper = memacc.oper; + visit(memacc.left); r_trivial = false; } void ExpressionInliner::visit(Swizzle &swizzle) { - visit_and_record(swizzle.left); - r_oper = swizzle.oper; + visit(swizzle.left); r_trivial = false; } void ExpressionInliner::visit(UnaryExpression &unary) { SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-'); - visit_and_record(unary.expression); - r_oper = unary.oper; + visit(unary.expression); r_trivial = false; } void ExpressionInliner::visit(BinaryExpression &binary) { - visit_and_record(binary.left); + visit(binary.left); { SetFlag clear_target(mutating, false); - visit_and_record(binary.right); + visit(binary.right); } - r_oper = binary.oper; r_trivial = false; } @@ -408,10 +400,10 @@ void ExpressionInliner::visit(Assignment &assign) { { SetFlag set_target(mutating); - visit_and_record(assign.left); + visit(assign.left); } r_oper = 0; - visit_and_record(assign.right); + visit(assign.right); map::iterator i = expressions.find(assign.target); if(i!=expressions.end()) @@ -424,23 +416,20 @@ void ExpressionInliner::visit(Assignment &assign) i->second.available = true; } - r_oper = assign.oper; r_trivial = false; } void ExpressionInliner::visit(TernaryExpression &ternary) { - visit_and_record(ternary.condition); - visit_and_record(ternary.true_expr); - visit_and_record(ternary.false_expr); - r_oper = ternary.oper; + visit(ternary.condition); + visit(ternary.true_expr); + visit(ternary.false_expr); r_trivial = false; } void ExpressionInliner::visit(FunctionCall &call) { TraversingVisitor::visit(call); - r_oper = 0; r_trivial = false; } diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index ca22c9d8..fa75e6d7 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -122,7 +122,6 @@ public: bool apply(Stage &); private: - void visit_and_record(RefPtr &); void inline_expression(Expression &, RefPtr &); virtual void visit(Block &); virtual void visit(RefPtr &);