[Mesa-dev] [PATCH 1/3] glsl: Propagate negates through multiplication chains.

Ian Romanick idr at freedesktop.org
Wed Feb 11 15:51:20 PST 2015


On 02/11/2015 02:54 PM, Matt Turner wrote:
> We propagate negations to the right-most leaves of the multiplication
> expression trees:
> 
>  - mul(neg(x), neg(y)) -> mul(x, y)
>  - mul(neg(x), y) -> neg(mul(x, y))
>  - mul(x, neg(y)) -> neg(mul(x, y))
> 
> total instructions in shared programs: 5943123 -> 5937229 (-0.10%)
> instructions in affected programs:     868221 -> 862327 (-0.68%)
> helped:                                4518
> HURT:                                  356
> GAINED:                                1

This seems very counter intuitive.  Why does this help?  It seems like
the negate modifier on the multiplication sources should be free... how
do we get cheaper than free? :)

> ---
>  src/glsl/opt_algebraic.cpp | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
> index 7bc65da..aecd6e1 100644
> --- a/src/glsl/opt_algebraic.cpp
> +++ b/src/glsl/opt_algebraic.cpp
> @@ -514,6 +514,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
>        if (is_vec_negative_one(op_const[1]))
>           return neg(ir->operands[0]);
>  
> +      if (op_expr[0] && op_expr[0]->operation == ir_unop_neg) {
> +         if (op_expr[1] && op_expr[1]->operation == ir_unop_neg) {
> +            /* mul(neg(x), neg(y)) -> mul(x, y) */
> +            return mul(op_expr[0]->operands[0], op_expr[1]->operands[0]);
> +         }
> +
> +         /* mul(neg(x), y) -> neg(mul(x, y)) */
> +         return neg(mul(op_expr[0]->operands[0], ir->operands[1]));
> +      }
> +
> +      /* mul(x, neg(y)) -> neg(mul(x, y)) */
> +      if (op_expr[1] && op_expr[1]->operation == ir_unop_neg) {
> +         return neg(mul(ir->operands[0], op_expr[1]->operands[0]));
> +      }
>  
>        /* Reassociate multiplication of constants so that we can do
>         * constant folding.
> 



More information about the mesa-dev mailing list