public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/114547] New: missed-optimization: use sign flag
@ 2024-04-01 16:24 goon.pri.low at gmail dot com
2024-04-01 16:25 ` [Bug middle-end/114547] " pinskia at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: goon.pri.low at gmail dot com @ 2024-04-01 16:24 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114547
Bug ID: 114547
Summary: missed-optimization: use sign flag
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: goon.pri.low at gmail dot com
Target Milestone: ---
Example functions
int s(int *v, int n) {
*v -= n;
return *v < 0;
}
int ns(int *v, int n) {
*v -= n;
return *v >= 0;
}
Generated assembly
s:
mov eax, DWORD PTR [rdi]
sub eax, esi
mov DWORD PTR [rdi], eax
shr eax, 31
ret
ns:
mov eax, DWORD PTR [rdi]
sub eax, esi
mov DWORD PTR [rdi], eax
not eax
shr eax, 31
ret
Optimal assembly
s:
xor eax, eax
sub DWORD PTR [rdi], esi
sets al
ret
ns:
xor eax, eax
sub DWORD PTR [rdi], esi
setns al
ret
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug middle-end/114547] missed-optimization: use sign flag
2024-04-01 16:24 [Bug middle-end/114547] New: missed-optimization: use sign flag goon.pri.low at gmail dot com
@ 2024-04-01 16:25 ` pinskia at gcc dot gnu.org
2024-04-01 16:32 ` [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster pinskia at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01 16:25 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114547
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |pinskia at gcc dot gnu.org
Severity|normal |enhancement
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster
2024-04-01 16:24 [Bug middle-end/114547] New: missed-optimization: use sign flag goon.pri.low at gmail dot com
2024-04-01 16:25 ` [Bug middle-end/114547] " pinskia at gcc dot gnu.org
@ 2024-04-01 16:32 ` pinskia at gcc dot gnu.org
2024-04-01 16:43 ` goon.pri.low at gmail dot com
2024-04-02 18:38 ` ubizjak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01 16:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114547
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|missed-optimization: use |comparison than less than 0
|sign flag |(or greater or equal to
| |than 0) after a subtraction
| |does not use the flags
| |regster
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am not sure this is always better ...
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster
2024-04-01 16:24 [Bug middle-end/114547] New: missed-optimization: use sign flag goon.pri.low at gmail dot com
2024-04-01 16:25 ` [Bug middle-end/114547] " pinskia at gcc dot gnu.org
2024-04-01 16:32 ` [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster pinskia at gcc dot gnu.org
@ 2024-04-01 16:43 ` goon.pri.low at gmail dot com
2024-04-02 18:38 ` ubizjak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: goon.pri.low at gmail dot com @ 2024-04-01 16:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114547
--- Comment #2 from gooncreeper <goon.pri.low at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> I am not sure this is always better ...
sets and setns are 1 uop, just like any other setcc
why wouldn't it be better?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster
2024-04-01 16:24 [Bug middle-end/114547] New: missed-optimization: use sign flag goon.pri.low at gmail dot com
` (2 preceding siblings ...)
2024-04-01 16:43 ` goon.pri.low at gmail dot com
@ 2024-04-02 18:38 ` ubizjak at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: ubizjak at gmail dot com @ 2024-04-02 18:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114547
Uroš Bizjak <ubizjak at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
CC| |ubizjak at gmail dot com
Last reconfirmed| |2024-04-02
Status|UNCONFIRMED |NEW
--- Comment #3 from Uroš Bizjak <ubizjak at gmail dot com> ---
The similar testcase with comparison to zero:
int z(int *v, int n) {
*v -= n;
return *v == 0;
}
int nz(int *v, int n) {
*v -= n;
return *v != 0;
}
generates expected code:
z:
xorl %eax, %eax
subl %esi, (%rdi)
sete %al
ret
nz:
xorl %eax, %eax
subl %esi, (%rdi)
setne %al
ret
The middle end expands via standard sequence:
(insn 10 9 11 (set (reg:CCZ 17 flags)
(compare:CCZ (reg:SI 83 [ _2 ])
(const_int 0 [0]))) "s.c":3:12 -1
(nil))
(insn 11 10 12 (set (reg:QI 91)
(eq:QI (reg:CCZ 17 flags)
(const_int 0 [0]))) "s.c":3:12 -1
(nil))
(insn 12 11 13 (set (reg:SI 90)
(zero_extend:SI (reg:QI 91))) "s.c":3:12 -1
(nil))
(insn 13 12 14 (set (reg:SI 85 [ <retval> ])
(reg:SI 90)) "s.c":3:12 -1
(nil))
OTOH, the sign compare is expanded via:
(insn 12 9 13 (parallel [
(set (reg:SI 90)
(lshiftrt:SI (reg:SI 83 [ _2 ])
(const_int 31 [0x1f])))
(clobber (reg:CC 17 flags))
]) "pr114547.c":4:12 -1
(nil))
(insn 13 12 14 (set (reg:SI 85 [ <retval> ])
(reg:SI 90)) "pr114547.c":4:12 -1
(nil))
(The above shift also interferes with RMW creation, resulting in unoptimal asm
sequence with two extra moves, and additional NEG insn in "ns" case.)
Middle-end expansion should avoid premature optimization in this case, at least
for targets that can merge sign comparison with the arith instruction.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-04-02 18:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-01 16:24 [Bug middle-end/114547] New: missed-optimization: use sign flag goon.pri.low at gmail dot com
2024-04-01 16:25 ` [Bug middle-end/114547] " pinskia at gcc dot gnu.org
2024-04-01 16:32 ` [Bug middle-end/114547] comparison than less than 0 (or greater or equal to than 0) after a subtraction does not use the flags regster pinskia at gcc dot gnu.org
2024-04-01 16:43 ` goon.pri.low at gmail dot com
2024-04-02 18:38 ` ubizjak at gmail dot com
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).