Simplify (truncate:QI (subreg:SI (reg:QI x))) to (reg:QI x)
authorRoger Sayle <roger@nextmovesoftware.com>
Mon, 23 Aug 2021 11:40:10 +0000 (12:40 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Mon, 23 Aug 2021 11:40:10 +0000 (12:40 +0100)
commit7e5f9ead16d7514b3baa0254084de94f0bfcd216
tree86de623cb72f1e98762052e77958157c565583bd
parent1d244020246cb155e4de62ca3b302b920a1f513f
Simplify (truncate:QI (subreg:SI (reg:QI x))) to (reg:QI x)

Whilst working on a backend patch, I noticed that the middle-end's
RTL optimizers weren't simplifying a truncation of a paradoxical
subreg extension, though it does transform closely related (more
complex) expressions.  The main (first) part of this patch
implements this simplification, reusing much of the logic already
in place.

I briefly considered suggesting that it's difficult to provide a new
testcase for this change, but then realized the reviewer's response
would be that this type of transformation should be self-tested
in simplify-rtx, so this patch adds a bunch of tests that integer
extensions and truncations are simplified as expected.  No good
deed goes unpunished and I was equally surprised to see that we
don't currently simplify/check/defend (zero_extend:SI (reg:SI)),
i.e. useless no-op extensions to the same mode.  So I've added
some logic to simplify (or more accurately prevent us generating
dubious RTL for) those.

2021-08-23  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
* simplify-rtx.c (simplify_truncation): Generalize simplification
of (truncate:A (subreg:B X)).
(simplify_unary_operation_1) [FLOAT_TRUNCATE, FLOAT_EXTEND,
SIGN_EXTEND, ZERO_EXTEND]: Handle cases where the operand
already has the desired machine mode.
(test_scalar_int_ops): Add tests that useless extensions and
truncations are optimized away.
(test_scalar_int_ext_ops): New self-test function to confirm
that truncations of extensions are correctly simplified.
(test_scalar_int_ext_ops2): New self-test function to check
truncations of truncations, extensions of extensions, and
truncations of extensions.
(test_scalar_ops): Call the above two functions with a
representative sampling of integer machine modes.
gcc/simplify-rtx.c