public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1
@ 2020-09-03 20:44 gabravier at gmail dot com
2020-09-04 12:17 ` [Bug tree-optimization/96929] " jakub at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: gabravier at gmail dot com @ 2020-09-03 20:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96929
Bug ID: 96929
Summary: Failure to optimize right shift of -1 to -1
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: gabravier at gmail dot com
Target Milestone: ---
int f(int b)
{
return -1 >> b;
}
This can be optimized to `return -1;`. This transformation is done by LLVM, but
not by GCC.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/96929] Failure to optimize right shift of -1 to -1
2020-09-03 20:44 [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1 gabravier at gmail dot com
@ 2020-09-04 12:17 ` jakub at gcc dot gnu.org
2020-11-20 9:48 ` jakub at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-04 12:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96929
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Status|UNCONFIRMED |NEW
Last reconfirmed| |2020-09-04
CC| |jakub at gcc dot gnu.org
--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
We already have a rule for this:
/* Optimize -1 >> x for arithmetic right shifts. */
(simplify
(rshift integer_all_onesp@0 @1)
(if (!TYPE_UNSIGNED (type)
&& tree_expr_nonnegative_p (@1))
@0))
but the rule requires that the shift count is non-negative.
That was added in PR38359 fix.
Even current wide_int_binop has:
case RSHIFT_EXPR:
case LSHIFT_EXPR:
if (wi::neg_p (arg2))
{
tmp = -arg2;
if (code == RSHIFT_EXPR)
code = LSHIFT_EXPR;
else
code = RSHIFT_EXPR;
}
else
tmp = arg2;
and so it treats rshift shifts by negative values as left shifts.
So, if we wanted to fix this PR, we'd need to remove the
tree_expr_nonnegative_p and change wide_int_binop to perhaps best punt on
negative arg2 instead of trying to handle it. Wonder what it will break.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/96929] Failure to optimize right shift of -1 to -1
2020-09-03 20:44 [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1 gabravier at gmail dot com
2020-09-04 12:17 ` [Bug tree-optimization/96929] " jakub at gcc dot gnu.org
@ 2020-11-20 9:48 ` jakub at gcc dot gnu.org
2020-11-24 8:03 ` cvs-commit at gcc dot gnu.org
2020-11-24 8:21 ` jakub at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-20 9:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96929
--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 49603
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49603&action=edit
gcc11-pr96929.patch
Untested fix.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/96929] Failure to optimize right shift of -1 to -1
2020-09-03 20:44 [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1 gabravier at gmail dot com
2020-09-04 12:17 ` [Bug tree-optimization/96929] " jakub at gcc dot gnu.org
2020-11-20 9:48 ` jakub at gcc dot gnu.org
@ 2020-11-24 8:03 ` cvs-commit at gcc dot gnu.org
2020-11-24 8:21 ` jakub at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-24 8:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96929
--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:4866b2f5db117f9e89f82c44ffed57178c09cc49
commit r11-5271-g4866b2f5db117f9e89f82c44ffed57178c09cc49
Author: Jakub Jelinek <jakub@redhat.com>
Date: Tue Nov 24 09:03:17 2020 +0100
middle-end, c++: Treat shifts by negative as undefined [PR96929]
The PR38359 change made the -1 >> x to -1 optimization less useful by
requiring that the x must be non-negative.
Shifts by negative amount are UB, but we for historic reasons had in some
(but not all) places some hack to treat shifts by negative value as the
other direction shifts by the negated amount.
The following patch just removes that special handling, instead we punt on
optimizing those (and ideally path isolation should catch that up and turn
those into __builtin_unreachable, perhaps with __builtin_warning next to
it). Folding the shifts in some places as if they were rotates and in
other
as if they were saturating just leads to inconsistencies.
For C++ constexpr diagnostics and -fpermissive, I've added code to pretend
fold-const.c has not changed, without -fpermissive it will be an error
anyway and I think it is better not to change all the diagnostics.
During x86_64-linux and i686-linux bootstrap/regtest, my statistics
gathering patch noted 185 unique -m32/-m64 x TU x function_name x
shift_kind
x fold-const/tree-ssa-ccp cases. I have investigated the
64 ../../gcc/config/i386/i386.c x86_output_aligned_bss LSHIFT_EXPR
wide_int_bitop
64 ../../gcc/config/i386/i386-expand.c emit_memmov LSHIFT_EXPR
wide_int_bitop
64 ../../gcc/config/i386/i386-expand.c ix86_expand_carry_flag_compare
LSHIFT_EXPR wide_int_bitop
64 ../../gcc/expmed.c expand_divmod LSHIFT_EXPR wide_int_bitop
64 ../../gcc/lra-lives.c process_bb_lives LSHIFT_EXPR wide_int_bitop
64 ../../gcc/rtlanal.c nonzero_bits1 LSHIFT_EXPR wide_int_bitop
64 ../../gcc/varasm.c optimize_constant_pool.isra LSHIFT_EXPR
wide_int_bitop
cases and all of them are either during jump threading (dom) or during PRE.
For jump threading, the most common case is 1 << floor_log2 (whatever)
where
floor_log2 is return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
and clz_hwi is if (x == 0) return HOST_BITS_PER_WIDE_INT; return
__builtin_clz* (x);
and so has range [-1, 63] and a comparison against == 0 which makes the
threader think it might be nice to jump thread the case leading to 1 << -1.
I think it is better to keep the 1 << -1 s in the IL for this and let path
isolation turn that into __builtin_unreachable () if the user wishes so.
2020-11-24 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/96929
* fold-const.c (wide_int_binop) <case LSHIFT_EXPR, case
RSHIFT_EXPR>:
Return false on negative second argument rather than trying to
handle
it as shift in the other direction.
* tree-ssa-ccp.c (bit_value_binop) <case LSHIFT_EXPR,
case RSHIFT_EXPR>: Punt on negative shift count rather than trying
to handle it as shift in the other direction.
* match.pd (-1 >> x to -1): Remove tree_expr_nonnegative_p check.
* constexpr.c (cxx_eval_binary_expression): For shifts by constant
with MSB set, emulate older wide_int_binop behavior to preserve
diagnostics and -fpermissive behavior.
* gcc.dg/tree-ssa/pr96929.c: New test.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/96929] Failure to optimize right shift of -1 to -1
2020-09-03 20:44 [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1 gabravier at gmail dot com
` (2 preceding siblings ...)
2020-11-24 8:03 ` cvs-commit at gcc dot gnu.org
@ 2020-11-24 8:21 ` jakub at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-11-24 8:21 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96929
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |FIXED
--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-11-24 8:21 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-03 20:44 [Bug tree-optimization/96929] New: Failure to optimize right shift of -1 to -1 gabravier at gmail dot com
2020-09-04 12:17 ` [Bug tree-optimization/96929] " jakub at gcc dot gnu.org
2020-11-20 9:48 ` jakub at gcc dot gnu.org
2020-11-24 8:03 ` cvs-commit at gcc dot gnu.org
2020-11-24 8:21 ` jakub at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).