public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105751] New: std::array comparision does not inline memcmp
@ 2022-05-27 15:45 gcc at maxbachmann dot de
  2022-05-30 11:29 ` [Bug c++/105751] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: gcc at maxbachmann dot de @ 2022-05-27 15:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105751
           Summary: std::array comparision does not inline memcmp
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at maxbachmann dot de
  Target Milestone: ---

On trunk and gcc 12.1, with -O2/-O3 the following array comparison is only
optimized to a memcmp, but not to a direct comparision:

```
bool test(std::array<int8_t, 8>& a, std::array<int8_t, 8>& b)
{
    return a == b;
}
```
which is optimized to:
```
test(std::array<signed char, 8ul>&, std::array<signed char, 8ul>&):
        sub     rsp, 8
        mov     edx, 8
        call    memcmp
        test    eax, eax
        sete    al
        add     rsp, 8
        ret
```

However for a direct memcmp gcc is able to optimize the memcmp to a single 64
bit comparision:
```
bool test(int8_t* a, int8_t* b)
{
    return memcmp(a, b, sizeof(int8_t) * 8) == 0;
}
```
which is optimized to
```
test(signed char*, signed char*):
        mov     rax, QWORD PTR [rsi]
        cmp     QWORD PTR [rdi], rax
        sete    al
        ret
```

This is optimized to a single comparision both in clang and msvc:
https://godbolt.org/z/99reqf3Yz

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

* [Bug c++/105751] std::array comparision does not inline memcmp
  2022-05-27 15:45 [Bug c++/105751] New: std::array comparision does not inline memcmp gcc at maxbachmann dot de
@ 2022-05-30 11:29 ` rguenth at gcc dot gnu.org
  2022-06-22 21:58 ` [Bug tree-optimization/105751] " pinskia at gcc dot gnu.org
  2022-12-12 17:14 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-30 11:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-05-30
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  We end up expanding

  <bb 2> [local count: 1073741824]:
  _5 = a_2(D) + 8;
  _6 = &MEM[(const struct array *)a_2(D)]._M_elems;
  _8 = _5 - _6;
  __len_9 = (const size_t) _8;
  if (__len_9 != 0)
    goto <bb 3>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 3> [local count: 536870913]:
  _4 = &MEM[(const struct array *)b_3(D)]._M_elems;
  _10 = __builtin_memcmp_eq (_6, _4, __len_9);
  _11 = _10 == 0;

  <bb 4> [local count: 1073741824]:
  # _12 = PHI <1(2), _11(3)>
  return _12;

So somehow the size is not known to be 8 and thus the proposed optimized code
would compare uninitialized memory?

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

* [Bug tree-optimization/105751] std::array comparision does not inline memcmp
  2022-05-27 15:45 [Bug c++/105751] New: std::array comparision does not inline memcmp gcc at maxbachmann dot de
  2022-05-30 11:29 ` [Bug c++/105751] " rguenth at gcc dot gnu.org
@ 2022-06-22 21:58 ` pinskia at gcc dot gnu.org
  2022-12-12 17:14 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-22 21:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm:

  _5 = a_2(D) + 8;
  _6 = &MEM[(const struct array *)a_2(D)]._M_elems;
  _8 = _5 - _6;

I think there is another bug for this similar thing.

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

* [Bug tree-optimization/105751] std::array comparision does not inline memcmp
  2022-05-27 15:45 [Bug c++/105751] New: std::array comparision does not inline memcmp gcc at maxbachmann dot de
  2022-05-30 11:29 ` [Bug c++/105751] " rguenth at gcc dot gnu.org
  2022-06-22 21:58 ` [Bug tree-optimization/105751] " pinskia at gcc dot gnu.org
@ 2022-12-12 17:14 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-12 17:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |13.0
             Status|NEW                         |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed for GCC 13 by r13-4600-gd13b86f932ff7b .

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

end of thread, other threads:[~2022-12-12 17:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-27 15:45 [Bug c++/105751] New: std::array comparision does not inline memcmp gcc at maxbachmann dot de
2022-05-30 11:29 ` [Bug c++/105751] " rguenth at gcc dot gnu.org
2022-06-22 21:58 ` [Bug tree-optimization/105751] " pinskia at gcc dot gnu.org
2022-12-12 17:14 ` 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).