https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126711
Bug ID: 126711
Summary: Missed elimination of sign-extend
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: ktkachov at gcc dot gnu.org
Target Milestone: ---
THis may be a dup, but this example appears in the hot parts of Cpython:
int f (int i)
{
long k = i;
if (k < 0) return -1;
if (!k) return 2;
return 3;
}
on aarch64 it generates at -O2:
f:
sxtw x0, w0
cmp x0, 0
tbnz x0, #63, .L3
cset w0, ne
add w0, w0, 2
ret
.L3:
mov w0, -1
ret
Clange generates:
f:
mov w8, #2
cmp w0, #0
cinc w8, w8, ne
cmn w0, #1
csinv w0, w8, wzr, gt
ret
if-conversion/phiopt aside, GCC has a redundant sign-extend sxtw that Clang
manages to avoid.
I think in match.pd:
/* From fold_sign_changed_comparison and fold_widened_comparison.
FIXME: the lack of symmetry is disturbing. */
(for cmp (simple_comparison)
(simplify
(cmp (convert@0 @00) (convert?@1 @10))
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
/* Disable this optimization if we're casting a function pointer
type on targets that require function pointer canonicalization. */
&& !(targetm.have_canonicalize_funcptr_for_compare ()
&& ((POINTER_TYPE_P (TREE_TYPE (@00))
&& FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
|| (POINTER_TYPE_P (TREE_TYPE (@10))
&& FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
&& single_use (@0))
The single_use is blocking the required folding?