Constant fold SS_NEG and SS_ABS in simplify-rtx.c
This simple patch performs compile-time constant folding of
signed saturating negation and signed saturating absolute value
in the RTL optimizers. Normally in two's complement arithmetic
the lowest representable signed value overflows on negation,
With these saturating operators they "saturate" to the maximum
representable signed value, so SS_NEG:QI -128 is 127, and
SS_ABS:HI -32768 is 32767.
On bfin-elf, the following two short functions:
short foo()
{
short t = -32768;
short r = __builtin_bfin_negate_fr1x16(t);
return r;
}
int bar()
{
int t = -
2147483648;
int r = __builtin_bfin_abs_fr1x32(t);
return r;
}
currently compile to:
_foo: nop;
nop;
R0 = -32768 (X);
R0 = -R0 (V);
rts;
_bar: nop;
R0 = -1 (X);
R0 <<= 31;
R0 = abs R0;
rts;
but with this middle-end patch now compile to:
_foo: nop;
nop;
nop;
R0 = 32767 (X);
rts;
_bar: nop;
nop;
R0 = -1 (X);
R0.H = 32767;
rts;
2021-10-18 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
* simplify-rtx.c (simplify_const_unary_operation) [SS_NEG, SS_ABS]:
Evalute SS_NEG and SS_ABS of a constant argument.
gcc/testsuite/ChangeLog
* gcc.target/bfin/ssabs.c: New test case.
* gcc.target/bfin/ssneg.c: New test case.