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

Roger Sayle roger@nextmovesoftware.com
Sun Jul 19 09:42:16 GMT 2020


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.

During the compilation of one of the tests in the test suite, we
manage the generate the redundant sequence of instructions:

(insn 17 16 18 3 (set (reg:HI 35)
        (ashiftrt:HI (reg:HI 34 [ arg ])
            (const_int 8 [0x8]))) "v2si-cvt.c":14:8 94 {ashrhi3}
     (expr_list:REG_DEAD (reg:HI 34 [ arg ])
        (nil)))
(insn 18 17 19 3 (set (reg:QI 36)
        (truncate:QI (reg:HI 35))) "v2si-cvt.c":14:8 28 {trunchiqi2}
     (expr_list:REG_DEAD (reg:HI 35)
        (nil)))
(insn 19 18 20 3 (set (reg:HI 37)
        (sign_extend:HI (reg:QI 36))) "v2si-cvt.c":14:6 22 {extendqihi2}
     (expr_list:REG_DEAD (reg:QI 36)
        (nil)))

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 truncation.

With this patch, the code generated by the nvptx backends goes from:
        shr.s16 %r35, %r34, 8;
        cvt.u32.u16     %r36, %r35;
        cvt.s16.s8      %r37, %r36;
to
        shr.s16 %r37, %r34, 8;

This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap"
and "make -k check" (just to be safe), and nvptx-none (both with and
without my other patches), all with no new regressions.
Ok for mainline?


2020-07-19  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.

Thanks in advance,
Roger
--
Roger Sayle
NextMove Software
Cambridge, UK

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patchs.txt
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20200719/2ec5357f/attachment-0001.txt>


More information about the Gcc-patches mailing list