public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115041] New: Missed optimization opportunity in std::find of std::vector elements
@ 2024-05-10 22:34 hiraditya at msn dot com
  2024-05-10 22:42 ` [Bug tree-optimization/115041] " sjames at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: hiraditya at msn dot com @ 2024-05-10 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115041
           Summary: Missed optimization opportunity in std::find of
                    std::vector elements
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hiraditya at msn dot com
  Target Milestone: ---

https://gcc.godbolt.org/z/s3hv15935


```
#include <algorithm>
#include <vector>
#include <cstdint>

bool find_epi8(const std::vector<int8_t>& v) {
    return std::find(v.begin(), v.end(), 42) != v.end();
}

bool find_epi32(const std::vector<int32_t>& v) {
    return std::find(v.begin(), v.end(), 42) != v.end();
}
```

$ gcc -O3 -ftree-vectorize -march=pantherlake

```
find_epi8(std::vector<signed char, std::allocator<signed char> > const&):
        mov     rcx, QWORD PTR [rdi+8]
        mov     rdx, QWORD PTR [rdi]
        mov     rsi, rcx
        sub     rsi, rdx
        mov     rax, rsi
        sar     rax, 2
        test    rax, rax
        jle     .L2
        lea     rax, [rdx+rax*4]
        jmp     .L8
.L3:
        cmp     BYTE PTR [rdx+1], 42
        je      .L23
        cmp     BYTE PTR [rdx+2], 42
        je      .L24
        cmp     BYTE PTR [rdx+3], 42
        je      .L25
        add     rdx, 4
        cmp     rdx, rax
        je      .L26
.L8:
        cmp     BYTE PTR [rdx], 42
        jne     .L3
.L21:
        cmp     rcx, rdx
        setne   al
        ret
.L26:
        mov     rsi, rcx
        sub     rsi, rdx
.L2:
        cmp     rsi, 2
        je      .L9
        cmp     rsi, 3
        je      .L10
        cmp     rsi, 1
        je      .L11
        xor     eax, eax
        ret
.L10:
        cmp     BYTE PTR [rdx], 42
        je      .L21
        add     rdx, 1
.L9:
        cmp     BYTE PTR [rdx], 42
        je      .L21
        add     rdx, 1
.L11:
        cmp     BYTE PTR [rdx], 42
        sete    al
        cmp     rcx, rdx
        setne   dl
        and     eax, edx
        ret
.L23:
        add     rdx, 1
        cmp     rcx, rdx
        setne   al
        ret
.L24:
        add     rdx, 2
        cmp     rcx, rdx
        setne   al
        ret
.L25:
        add     rdx, 3
        cmp     rcx, rdx
        setne   al
        ret
find_epi32(std::vector<int, std::allocator<int> > const&):
        mov     rcx, QWORD PTR [rdi+8]
        mov     rdx, QWORD PTR [rdi]
        mov     rax, rcx
        sub     rax, rdx
        mov     rsi, rax
        sar     rax, 4
        sar     rsi, 2
        test    rax, rax
        jle     .L28
        sal     rax, 4
        add     rax, rdx
        jmp     .L34
.L29:
        cmp     DWORD PTR [rdx+4], 42
        je      .L48
        cmp     DWORD PTR [rdx+8], 42
        je      .L49
        cmp     DWORD PTR [rdx+12], 42
        je      .L50
        add     rdx, 16
        cmp     rdx, rax
        je      .L51
.L34:
        cmp     DWORD PTR [rdx], 42
        jne     .L29
.L47:
        cmp     rcx, rdx
        setne   al
        ret
.L51:
        mov     rsi, rcx
        sub     rsi, rdx
        sar     rsi, 2
.L28:
        cmp     rsi, 2
        je      .L35
        cmp     rsi, 3
        je      .L36
        cmp     rsi, 1
        je      .L37
        xor     eax, eax
        ret
.L36:
        cmp     DWORD PTR [rdx], 42
        je      .L47
        add     rdx, 4
.L35:
        cmp     DWORD PTR [rdx], 42
        je      .L47
        add     rdx, 4
.L37:
        cmp     DWORD PTR [rdx], 42
        sete    al
        cmp     rcx, rdx
        setne   dl
        and     eax, edx
        ret
.L48:
        add     rdx, 4
        cmp     rcx, rdx
        setne   al
        ret
.L49:
        add     rdx, 8
        cmp     rcx, rdx
        setne   al
        ret
.L50:
        add     rdx, 12
        cmp     rcx, rdx
        setne   al
        ret
```


clang lowers both the calls to (w)memchr

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

* [Bug tree-optimization/115041] Missed optimization opportunity in std::find of std::vector elements
  2024-05-10 22:34 [Bug tree-optimization/115041] New: Missed optimization opportunity in std::find of std::vector elements hiraditya at msn dot com
@ 2024-05-10 22:42 ` sjames at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: sjames at gcc dot gnu.org @ 2024-05-10 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
                 CC|                            |sjames at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Sam James <sjames at gcc dot gnu.org> ---
Same code in 115040?

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

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

end of thread, other threads:[~2024-05-10 22:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-10 22:34 [Bug tree-optimization/115041] New: Missed optimization opportunity in std::find of std::vector elements hiraditya at msn dot com
2024-05-10 22:42 ` [Bug tree-optimization/115041] " sjames 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).