[PATCH] Constant fold/simplify SS_ASHIFT and US_ASHIFT in simplify-rtx.c

Roger Sayle roger@nextmovesoftware.com
Mon Oct 25 07:42:42 GMT 2021


This patch adds compile-time evaluation of signed saturating left shift
(SS_ASHIFT) and unsigned saturating left shift (US_ASHIFT) to simplify-rtx's
simplify_const_binary_operation.  US_ASHIFT saturates to the maximum
unsigned value on overflow (which occurs when the shift is greater than
the leading zero count), while SS_ASHIFT saturates on overflow to the
maximum signed value for positive arguments, and the minimum signed value
for negative arguments (which occurs when the shift count is greater than
the number of leading redundant sign bits, clrsb).  This suggests
some additional simplifications that this patch implements in
simplify_binary_operation_1; us_ashift:HI of 0xffff remains 0xffff
(much like any ashift of 0x0000 remains 0x0000), and ss_ashift:HI of
0x7fff remains 0x7ffff, and of 0x8000 remains 0x8000.

Conveniently the bfin backend provides instructions/built-ins that allow
this functionality to be tested.  The two functions below

short stest_sat_max() { return __builtin_bfin_shl_fr1x16(10000,8); }
short stest_sat_min() { return __builtin_bfin_shl_fr1x16(-10000,8); }

previously on bfin-elf with -O2 generated:

_stest_sat_max:
        nop;
        nop;
        R0 = 10000 (X);
        R0 = R0 << 8 (V,S);
        rts;

_stest_sat_min:
        nop;
        nop;
        R0 = -10000 (X);
        R0 = R0 << 8 (V,S);
        rts;

With this patch, bfin-elf now generates:

_stest_sat_max:
        nop;
        nop;
        nop;
        R0 = 32767 (X);
        rts;

_stest_sat_min:
        nop;
        nop;
        nop;
        R0 = -32768 (X);
        rts;

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and
make -k check with no new failures, and on a cross-compiler to bfin-elf
with no regressions.  Ok for mainline?


2021-10-25  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* simplify-rtx (simplify_binary_operation_1) [SS_ASHIFT]: Simplify
	shifts of the mode's smin_value and smax_value when the bit count
	operand doesn't have side-effects.
	[US_ASHIFT]: Likewise, simplify shifts of the mode's umax_value
	when the bit count operand doesn't have side-effects.
	(simplify_const_binary_operation) [SS_ASHIFT, US_ASHIFT]: Perform
	compile-time evaluation of saturating left shifts with constant
	arguments.

gcc/testsuite/ChangeLog
	* gcc.target/bfin/ssashift-1.c: New test case.


Roger
--

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patchx.txt
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20211025/37c350d3/attachment.txt>


More information about the Gcc-patches mailing list