public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative?
@ 2023-05-24  5:36 lh_mouse at 126 dot com
  2023-05-24  6:02 ` [Bug tree-optimization/109950] " amonakov at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: lh_mouse at 126 dot com @ 2023-05-24  5:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109950
           Summary: can array subscripts be assumed to be non-negative?
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

There is a lot of historical code which has been using `int` for array indexes:

```
extern int data[];
extern int next;

int
test_function(int* outptr)
  {
    *outptr = data[next];
    return next >= 0;
  }
```

In this example, the value of `next` is used as an array index. Despite the
unknown size, elements in an array can't have negative indexes, so maybe here
`next >= 0` can be optimized to a constant?


Specifically about x86_64, there is some more optimization that we can do: The
value of `next` can be loaded into a 32-bit register, zeroing the upper half
implicitly, without using a sign-extension instruction, as in machine code:

```
48 63 05 IMM32   ; movsxd rax, dword ptr [rip + IMM32]
   8B 05 IMM32   ; mov    eax, dword ptr [rip + IMM32]
```

The former contains an operand size override prefix, takes up one more byte,
and costs an extra cycle to decode.

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

* [Bug tree-optimization/109950] can array subscripts be assumed to be non-negative?
  2023-05-24  5:36 [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative? lh_mouse at 126 dot com
@ 2023-05-24  6:02 ` amonakov at gcc dot gnu.org
  2023-05-24  6:15 ` lh_mouse at 126 dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: amonakov at gcc dot gnu.org @ 2023-05-24  6:02 UTC (permalink / raw)
  To: gcc-bugs

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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #1 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
That's the REX prefix, not an operand size override prefix. It doesn't cause a
decoding stall.

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

* [Bug tree-optimization/109950] can array subscripts be assumed to be non-negative?
  2023-05-24  5:36 [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative? lh_mouse at 126 dot com
  2023-05-24  6:02 ` [Bug tree-optimization/109950] " amonakov at gcc dot gnu.org
@ 2023-05-24  6:15 ` lh_mouse at 126 dot com
  2023-05-25  7:38 ` rguenth at gcc dot gnu.org
  2023-05-25  8:00 ` lh_mouse at 126 dot com
  3 siblings, 0 replies; 5+ messages in thread
From: lh_mouse at 126 dot com @ 2023-05-24  6:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from LIU Hao <lh_mouse at 126 dot com> ---
(In reply to Alexander Monakov from comment #1)
> That's the REX prefix, not an operand size override prefix. It doesn't cause
> a decoding stall.

Thanks for pointing this out. Thought it was 66H.

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

* [Bug tree-optimization/109950] can array subscripts be assumed to be non-negative?
  2023-05-24  5:36 [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative? lh_mouse at 126 dot com
  2023-05-24  6:02 ` [Bug tree-optimization/109950] " amonakov at gcc dot gnu.org
  2023-05-24  6:15 ` lh_mouse at 126 dot com
@ 2023-05-25  7:38 ` rguenth at gcc dot gnu.org
  2023-05-25  8:00 ` lh_mouse at 126 dot com
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-25  7:38 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
a negative 'next' is fine, data[next] is just *(data + next) so we cannot elide
the sign-extension either.

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

* [Bug tree-optimization/109950] can array subscripts be assumed to be non-negative?
  2023-05-24  5:36 [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative? lh_mouse at 126 dot com
                   ` (2 preceding siblings ...)
  2023-05-25  7:38 ` rguenth at gcc dot gnu.org
@ 2023-05-25  8:00 ` lh_mouse at 126 dot com
  3 siblings, 0 replies; 5+ messages in thread
From: lh_mouse at 126 dot com @ 2023-05-25  8:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from LIU Hao <lh_mouse at 126 dot com> ---
Given the fact that GCC is already able to warn about out-of-range indexes for
an array, why wouldn't it be possible to infer that `*(data + next)` is always
an element of `data`?

If the result of `data + next` (after array-to-pointer conversion) does not
point to an element of `data` (or the past-the-end position, but that's not the
case, as it's not dereferenceable), then the behavior will be undefined.

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

end of thread, other threads:[~2023-05-25  8:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-24  5:36 [Bug tree-optimization/109950] New: can array subscripts be assumed to be non-negative? lh_mouse at 126 dot com
2023-05-24  6:02 ` [Bug tree-optimization/109950] " amonakov at gcc dot gnu.org
2023-05-24  6:15 ` lh_mouse at 126 dot com
2023-05-25  7:38 ` rguenth at gcc dot gnu.org
2023-05-25  8:00 ` lh_mouse at 126 dot com

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).