public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97225] New: Failure to optimize out conditional addition of zero
@ 2020-09-28 10:22 osandov at osandov dot com
2020-09-28 11:18 ` [Bug tree-optimization/97225] " rguenth at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: osandov at osandov dot com @ 2020-09-28 10:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225
Bug ID: 97225
Summary: Failure to optimize out conditional addition of zero
Product: gcc
Version: 10.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: osandov at osandov dot com
Target Milestone: ---
For the following code:
#include <stddef.h>
struct vector {
int *data;
size_t size;
};
int *vector_end(struct vector *vec)
{
return vec->data + vec->size;
}
GCC 10.2.0 on x86-64 generates the following code (same on -O2, -O3, and -Os):
vector_end:
movq 8(%rdi), %rdx
movq (%rdi), %rax
leaq (%rax,%rdx,4), %rax
ret
However, vector_end() needs to handle empty vectors represented as { NULL, 0 }.
Pointer arithmetic on a null pointer is undefined behavior (even NULL + 0, as
far as I can tell from the C standard), so the correct code is:
int *vector_end(struct vector *vec)
{
if (vec->size == 0)
return vec->data;
return vec->data + vec->size;
}
I'd expect this to generate the same code, but GCC 10.2.0 generates a
conditional move with -O2 and -O3:
vector_end:
movq 8(%rdi), %rdx
movq (%rdi), %rax
testq %rdx, %rdx
leaq (%rax,%rdx,4), %rcx
cmovne %rcx, %rax
ret
And a branch with -Os:
vector_end:
movq 8(%rdi), %rdx
movq (%rdi), %rax
testq %rdx, %rdx
je .L1
leaq (%rax,%rdx,4), %rax
.L1:
ret
Clang 10.0.1, on the other hand, generates the same code with and without the
size check (oddly enough, it also falls back to a conditional move if the size
member is an int or unsigned int instead of size_t/unsigned long):
vector_end: # @vector_end
movq 8(%rdi), %rax
shlq $2, %rax
addq (%rdi), %rax
retq
Can GCC avoid the conditional move/branch here?
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/97225] Failure to optimize out conditional addition of zero
2020-09-28 10:22 [Bug c/97225] New: Failure to optimize out conditional addition of zero osandov at osandov dot com
@ 2020-09-28 11:18 ` rguenth at gcc dot gnu.org
2020-09-30 16:36 ` pinskia at gcc dot gnu.org
2023-08-04 22:28 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-09-28 11:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2020-09-28
Component|c |tree-optimization
Keywords| |missed-optimization
Ever confirmed|0 |1
Status|UNCONFIRMED |NEW
--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think we've seen a duplicate report for this.
Confirmed. late phiopt sees (we need hoisting to get rid of the loads
in the two if arms):
<bb 2> [local count: 1073741824]:
_1 = vec_6(D)->size;
pretmp_9 = vec_6(D)->data;
if (_1 == 0)
goto <bb 4>; [34.00%]
else
goto <bb 3>; [66.00%]
<bb 3> [local count: 708669601]:
_3 = _1 * 4;
_7 = pretmp_9 + _3;
<bb 4> [local count: 1073741824]:
# _4 = PHI <_7(3), pretmp_9(2)>
return _4;
where it misses the value-conversion, possibly either based on lack
of handling the mult+add or because of consideration of making the
not infrequent path more expensive. Later if-converting this anyway
on RTL shows a disconnect in cost then.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/97225] Failure to optimize out conditional addition of zero
2020-09-28 10:22 [Bug c/97225] New: Failure to optimize out conditional addition of zero osandov at osandov dot com
2020-09-28 11:18 ` [Bug tree-optimization/97225] " rguenth at gcc dot gnu.org
@ 2020-09-30 16:36 ` pinskia at gcc dot gnu.org
2023-08-04 22:28 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-09-30 16:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/97225] Failure to optimize out conditional addition of zero
2020-09-28 10:22 [Bug c/97225] New: Failure to optimize out conditional addition of zero osandov at osandov dot com
2020-09-28 11:18 ` [Bug tree-optimization/97225] " rguenth at gcc dot gnu.org
2020-09-30 16:36 ` pinskia at gcc dot gnu.org
@ 2023-08-04 22:28 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-04 22:28 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |pinskia at gcc dot gnu.org
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I noticed this also while creating a testcase for
https://gcc.gnu.org/pipermail/gcc-patches/2023-August/626117.html .
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-08-04 22:28 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-28 10:22 [Bug c/97225] New: Failure to optimize out conditional addition of zero osandov at osandov dot com
2020-09-28 11:18 ` [Bug tree-optimization/97225] " rguenth at gcc dot gnu.org
2020-09-30 16:36 ` pinskia at gcc dot gnu.org
2023-08-04 22:28 ` pinskia 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).