middle-end: Simplify (sign_extend:HI (truncate:QI (ashiftrt:HI X 8)))
authorRoger Sayle <roger@nextmovesoftware.com>
Sun, 16 Aug 2020 18:20:27 +0000 (19:20 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Sun, 16 Aug 2020 18:20:27 +0000 (19:20 +0100)
commitc99116aeeb9644ebddec653ee8b19de4d38b65bd
tree2eba122f1ba3767c2de299eb6f9b79c2aa3f5f77
parent10218124c66445517dca81a79728216064f7f1fe
middle-end: Simplify (sign_extend:HI (truncate:QI (ashiftrt:HI X 8)))

The combination of several my recent nvptx patches has revealed an
interesting RTL optimization opportunity.  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 equivalent zero_extend variant appears to already
be implemented in simplify_unary_operation_1.

These result from RTL expansion generating a reasonable arithmetic right
shift and truncation to char, only to then discover the backend doesn't
support QImode comparisons, so the next optab widens this result/operand
back to HImode.  In this sequence the truncate and sign extend are
redundant as the original arithmetic shift has already set 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 truncatation.

2020-08-16  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
* simplify-rtx.c (simplify_unary_operation_1) [SIGN_EXTEND]:
Simplify (sign_extend:M (truncate:N (lshiftrt:M x C))) to
(ashiftrt:M x C) when the shift sets the high bits appropriately.
gcc/simplify-rtx.c