https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110010
Bug ID: 110010
Summary: (a>>N) != (b>>N) is not optimized to (a^b) >= (1<<N)
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Take:
```
#define N 5
unsigned f(unsigned a, unsigned b)
{
return (a>>N) != (b>>N);
}
```
This could be optimized down to just `return (a^b) <= (1<<N)`.
This shows up in the testsuite as a pr94718-5.c failure when you add the
following 2 match patterns:
(simplify
(negate (convert (lt @0 integer_zerop)))
(if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
(with { tree type_unsigned = unsigned_type_for (TREE_TYPE (@0)); }
(if (types_match (type, TREE_TYPE (@0)) || types_match (type_unsigned,
type))
(convert (rshift @0
{ build_int_cst (integer_type_node,
element_precision (type) - 1); }))))))
(simplify
(convert (lt @0 integer_zerop))
(if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
(with { tree type_unsigned = unsigned_type_for (TREE_TYPE (@0)); }
(if (types_match (type, TREE_TYPE (@0)) || types_match (type_unsigned,
type))
(convert (rshift (convert:type_unsigned @0)
{ build_int_cst (integer_type_node,
element_precision (type) - 1); }))))))