[PATCH] middle-end: Simplify (sign_extend:HI (truncate:QI (ashiftrt:HI X 8)))

Segher Boessenkool segher@kernel.crashing.org
Sun Aug 16 01:23:34 GMT 2020


Hi!

On Sun, Jul 19, 2020 at 10:42:16AM +0100, Roger Sayle wrote:
> This patch to simplify-rtx.c
> simplifies (sign_extend:HI (truncate:QI (?shiftrt:HI x 8))) to just
> (ashiftrt:HI x 8), as the inner shift already sets the high bits
> appropriately.

> The one oddity of the patch is that it tests for
> LSHIFTRT as inner shift, as simplify/combine has already canonicalized
> this to a logical shift, assuming that the distinction is unimportant
> following the truncation.

If simplify-rtx does that, that is a bug.  But combine will do this, I
think that is what you are seeing?  Can you verify it already works if
the ASHIFTRT is not changed to an LSHIFTRT?

> +	  if (is_a <scalar_int_mode> (mode, &m_mode)
> +	      && is_a <scalar_int_mode> (GET_MODE (op), &n_mode)
> +	      && is_a <scalar_int_mode> (GET_MODE (old_shift), &o_mode)
> +	      && GET_MODE_PRECISION (o_mode) - GET_MODE_PRECISION (n_mode)
> +		 == INTVAL (XEXP (old_shift, 1)))
> +	    {
> +	      rtx new_shift = simplify_gen_binary (ASHIFTRT,
> +						   GET_MODE (old_shift),
> +						   XEXP (old_shift, 0),
> +						   XEXP (old_shift, 1));
> +	      if (GET_MODE_PRECISION (m_mode) > GET_MODE_PRECISION (o_mode))
> +		return simplify_gen_unary (SIGN_EXTEND, mode, new_shift,
> +					   GET_MODE (new_shift));
> +	      if (mode != GET_MODE (new_shift))
> +		return simplify_gen_unary (TRUNCATE, mode, new_shift,
> +					   GET_MODE (new_shift));
> +	      return new_shift;
> +	    }

Yeah looks like it :-)

You could say combine should be smarter about this, but this is a valid
simplification in itself.  So, okay for trunk.  Thank you!


Segher


More information about the Gcc-patches mailing list