[Mesa-dev] [PATCH] glsl: Optimize mul(a, -1) into neg(a).

Kenneth Graunke kenneth at whitecape.org
Wed Oct 16 09:30:31 CEST 2013


On 10/15/2013 11:55 PM, Matt Turner wrote:
> Two extra instructions in some heroesofnewerth shaders, but a win for
> everything else.
> 
> total instructions in shared programs: 1531352 -> 1530815 (-0.04%)
> instructions in affected programs:     121898 -> 121361 (-0.44%)
> ---
>  src/glsl/opt_algebraic.cpp | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
> index d706a6a..3e5802e 100644
> --- a/src/glsl/opt_algebraic.cpp
> +++ b/src/glsl/opt_algebraic.cpp
> @@ -85,6 +85,12 @@ is_vec_one(ir_constant *ir)
>  }
>  
>  static inline bool
> +is_vec_negative_one(ir_constant *ir)
> +{
> +   return (ir == NULL) ? false : ir->is_negative_one();
> +}
> +
> +static inline bool
>  is_vec_basis(ir_constant *ir)
>  {
>     return (ir == NULL) ? false : ir->is_basis();
> @@ -287,6 +293,23 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
>  	 this->progress = true;
>  	 return ir_constant::zero(ir, ir->type);
>        }
> +      if (is_vec_negative_one(op_const[0])) {
> +         this->progress = true;
> +         temp = new(mem_ctx) ir_expression(ir_unop_neg,
> +                                           ir->operands[1]->type,
> +                                           ir->operands[1],
> +                                           NULL);
> +         return swizzle_if_required(ir, temp);
> +      }
> +      if (is_vec_negative_one(op_const[1])) {
> +         this->progress = true;
> +         temp = new(mem_ctx) ir_expression(ir_unop_neg,
> +                                           ir->operands[0]->type,
> +                                           ir->operands[0],
> +                                           NULL);
> +         return swizzle_if_required(ir, temp);
> +      }
> +
>  
>        /* Reassociate multiplication of constants so that we can do
>         * constant folding.
> 

Yeah, seems like an obvious idea.  I suspect the tiny regressions in HoN
are due to optimization not coping with source modifiers, and then a
different pass optimizes it a little differently, and it diverges from
there...

Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>


More information about the mesa-dev mailing list