public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/113130] New: `abs(a) == b` could be expanded as `(a == b || a == -b)`
@ 2023-12-24 22:38 pinskia at gcc dot gnu.org
  2023-12-29  1:06 ` [Bug middle-end/113130] " jsm28 at gcc dot gnu.org
  2023-12-29  1:16 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-24 22:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113130

            Bug ID: 113130
           Summary: `abs(a) == b` could be expanded as `(a == b || a ==
                    -b)`
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int f(int a, int c)
{
 // c = 1;
  int b = a > 0 ? a : -a;
  return b == c;
}

int f1(int a, int c)
{
 // c = 1;
  return a == -c || a == c;
}
```

These 2 functions should produce the same assembly code.
This is especially true for when c is a 1 or 2.

Note for gimple, it might make sense to `ABS_EXPR <a> == c` as the conconal
form as it is only 2 gimple statements rather than 3.

Note `<`, `>`, `>=`, `<=`, and `!=` should be handled too.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug middle-end/113130] `abs(a) == b` could be expanded as `(a == b || a == -b)`
  2023-12-24 22:38 [Bug middle-end/113130] New: `abs(a) == b` could be expanded as `(a == b || a == -b)` pinskia at gcc dot gnu.org
@ 2023-12-29  1:06 ` jsm28 at gcc dot gnu.org
  2023-12-29  1:16 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2023-12-29  1:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113130

--- Comment #1 from Joseph S. Myers <jsm28 at gcc dot gnu.org> ---
Note that the first example (and the suggested one with ABS_EXPR) has undefined
behavior for a == INT_MIN, while the second has undefined behavior for c ==
INT_MIN (and is also not equivalent to the first for negative c).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug middle-end/113130] `abs(a) == b` could be expanded as `(a == b || a == -b)`
  2023-12-24 22:38 [Bug middle-end/113130] New: `abs(a) == b` could be expanded as `(a == b || a == -b)` pinskia at gcc dot gnu.org
  2023-12-29  1:06 ` [Bug middle-end/113130] " jsm28 at gcc dot gnu.org
@ 2023-12-29  1:16 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-29  1:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113130

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Oh right but the following testcase it should be valid to do and not worry
about INT_MIN either:
```
int f(int a, unsigned short c)
{
 // c = 1;
  int b = a > 0 ? a : -a;
  return b == c;
}

int f1(int a, unsigned short c)
{
 // c = 1;
  return a == -c || a == c;
}
```

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-29  1:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-24 22:38 [Bug middle-end/113130] New: `abs(a) == b` could be expanded as `(a == b || a == -b)` pinskia at gcc dot gnu.org
2023-12-29  1:06 ` [Bug middle-end/113130] " jsm28 at gcc dot gnu.org
2023-12-29  1:16 ` 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).