public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local
@ 2022-12-24 10:53 jhaberman at gmail dot com
  2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jhaberman at gmail dot com @ 2022-12-24 10:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108217
           Summary: bogus -Warray-bounds with pointer to constant local
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jhaberman at gmail dot com
  Target Milestone: ---

Repro:

void ExternFunc1();
void ExternFunc2(const int*);

char mem[32];

static void StaticFunc(const int* i) {
  void* ptr = (void*)0;
  switch (*i) {
    case 0:
      ExternFunc2(i);
      return;
    case 1:
      __builtin_memcpy(mem, &ptr, sizeof(ptr));
      return;
    case 2: {
      __builtin_memcpy(mem, &ptr, 32);
      return;
    }
  }
}

void Bad() {
  const int i = 1;
  ExternFunc1();
  StaticFunc(&i);
}

This reproduces on trunk according to Godbolt: https://godbolt.org/z/vYGo1z6bG

Godbolt also indicates a missed optimization, which is probably related to the
bogus warning.  Clang correctly performs constant propagation of the local `i`,
whereas GCC seems to think that all cases of the switch() are reachable.

It is true that &i escapes, but mutating `i` is UB because it is const, so it
should be legal to perform constant propagation here.

Additionally, even if ExternFunc2() mutated `i`, it would be too late to change
its value in time to affect the switch().

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

* [Bug middle-end/108217] bogus -Warray-bounds with pointer to constant local
  2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
@ 2022-12-24 20:11 ` pinskia at gcc dot gnu.org
  2022-12-24 20:14 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-24 20:11 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|diagnostic                  |missed-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Well with -fno-inline, GCC warns too so ....

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

* [Bug middle-end/108217] bogus -Warray-bounds with pointer to constant local
  2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
  2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
@ 2022-12-24 20:14 ` pinskia at gcc dot gnu.org
  2022-12-24 20:35 ` jhaberman at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-24 20:14 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang does not warn even at all.
That being said there is a missed optimization but that is the same as PR 23384
.
The const part is a misleading you really.

*** This bug has been marked as a duplicate of bug 23384 ***

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

* [Bug middle-end/108217] bogus -Warray-bounds with pointer to constant local
  2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
  2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
  2022-12-24 20:14 ` pinskia at gcc dot gnu.org
@ 2022-12-24 20:35 ` jhaberman at gmail dot com
  2022-12-24 20:42 ` pinskia at gcc dot gnu.org
  2022-12-24 20:46 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jhaberman at gmail dot com @ 2022-12-24 20:35 UTC (permalink / raw)
  To: gcc-bugs

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

Josh Haberman <jhaberman at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|DUPLICATE                   |---

--- Comment #3 from Josh Haberman <jhaberman at gmail dot com> ---
> That being said there is a missed optimization but that is the same as PR 23384 .
> The const part is a misleading you really.

I think there are two issues here.

1. Escape analysis is not flow sensitive.  I agree that aspect of my bug report
is a dup of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=23384, and closing as
a dup is appropriate there.

2. Escape analysis does not take 'const-ness' of the underlying object into
account.

Let me illustrate (2) with an example that isolates that issue (Godbolt:
https://godbolt.org/z/16cv87s9d)

void ExternFunc(const int*);

int Bad() {
  const int i = 0;
  const int* pi = &i;
  ExternFunc(pi);
  return *pi;
}

int Good() {
  const int i = 0;
  ExternFunc(&i);
  return i;
}

These two functions are effectively the same, but in Bad() GCC does not perform
constant propagation across the external function call.  While it's true that
the pointer escapes, the underlying object is const and cannot change, so
constant propagation should work here, as it does in Good().

Currently GCC re-loads `i` from the stack in Bad(), even though we know
statically that the value must be zero.

The same missed optimization is present in Clang:
https://github.com/llvm/llvm-project/issues/59694

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

* [Bug middle-end/108217] bogus -Warray-bounds with pointer to constant local
  2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
                   ` (2 preceding siblings ...)
  2022-12-24 20:35 ` jhaberman at gmail dot com
@ 2022-12-24 20:42 ` pinskia at gcc dot gnu.org
  2022-12-24 20:46 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-24 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The problem with const is GCC does not have an idea of a store once and then
become constant.
an example of that would be:
```
void ExternFunc(const int*);

int f();

int Bad() {
  const int i = f();
  const int* pi = &i;
  ExternFunc(pi);
  return *pi;
}

int Good() {
  const int i = f();
  ExternFunc(&i);
  return i;
}
```
This is similar to LLVM/clang really because this does not come up that much.

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

* [Bug middle-end/108217] bogus -Warray-bounds with pointer to constant local
  2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
                   ` (3 preceding siblings ...)
  2022-12-24 20:42 ` pinskia at gcc dot gnu.org
@ 2022-12-24 20:46 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-24 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Then it is a dup of bug 86318.

*** This bug has been marked as a duplicate of bug 86318 ***

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

end of thread, other threads:[~2022-12-24 20:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-24 10:53 [Bug middle-end/108217] New: bogus -Warray-bounds with pointer to constant local jhaberman at gmail dot com
2022-12-24 20:11 ` [Bug middle-end/108217] " pinskia at gcc dot gnu.org
2022-12-24 20:14 ` pinskia at gcc dot gnu.org
2022-12-24 20:35 ` jhaberman at gmail dot com
2022-12-24 20:42 ` pinskia at gcc dot gnu.org
2022-12-24 20:46 ` 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).