public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109440] New: Missed optimization of vector::at when a function is called inside the loop
@ 2023-04-06 18:15 hiraditya at msn dot com
  2023-04-06 18:36 ` [Bug tree-optimization/109440] " pinskia at gcc dot gnu.org
  2023-04-11 13:15 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: hiraditya at msn dot com @ 2023-04-06 18:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109440
           Summary: Missed optimization of vector::at when a function is
                    called inside the loop
           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: ---

#include<vector>
#include<string>
using namespace std;

bool bar();

using T = int;

T vat(std::vector<T> v) {
    T s;
    for (auto i = 0; i < v.size(); ++i) {
        if (bar())
            s += v.at(i);
    }

    return s;
}


$ gcc -O2 -fexceptions -fno-unroll-loops


.LC0:
        .string "vector::_M_range_check: __n (which is %zu) >= this->size()
(which is %zu)"
vat(std::vector<int, std::allocator<int> >):
        mov     rax, QWORD PTR [rdi]
        cmp     QWORD PTR [rdi+8], rax
        je      .L9
        push    r12
        push    rbp
        mov     rbp, rdi
        push    rbx
        xor     ebx, ebx
        jmp     .L6
.L14:
        mov     rax, QWORD PTR [rbp+8]
        sub     rax, QWORD PTR [rbp+0]
        add     rbx, 1
        sar     rax, 2
        cmp     rbx, rax
        jnb     .L13
.L6:
        call    bar()
        test    al, al
        je      .L14
        mov     rcx, QWORD PTR [rbp+0]
        mov     rdx, QWORD PTR [rbp+8]
        sub     rdx, rcx
        sar     rdx, 2
        mov     rax, rdx
        cmp     rbx, rdx
        jnb     .L15
        add     r12d, DWORD PTR [rcx+rbx*4]
        add     rbx, 1
        cmp     rbx, rax
        jb      .L6
.L13:
        mov     eax, r12d
        pop     rbx
        pop     rbp
        pop     r12
        ret
.L9:
        mov     eax, r12d
        ret
.L15:
        mov     rsi, rbx
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        call    std::__throw_out_of_range_fmt(char const*, ...)

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

* [Bug tree-optimization/109440] Missed optimization of vector::at when a function is called inside the loop
  2023-04-06 18:15 [Bug tree-optimization/109440] New: Missed optimization of vector::at when a function is called inside the loop hiraditya at msn dot com
@ 2023-04-06 18:36 ` pinskia at gcc dot gnu.org
  2023-04-11 13:15 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-06 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |alias

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There are a few things, the most obvious thing is the middle-end thinks the
content of v can change via the call to foo.

If you make a local copy, then there is a missing VRP which can be fixed by
changing the type of i to size_t.

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

* [Bug tree-optimization/109440] Missed optimization of vector::at when a function is called inside the loop
  2023-04-06 18:15 [Bug tree-optimization/109440] New: Missed optimization of vector::at when a function is called inside the loop hiraditya at msn dot com
  2023-04-06 18:36 ` [Bug tree-optimization/109440] " pinskia at gcc dot gnu.org
@ 2023-04-11 13:15 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-11 13:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
There's that other bug which would be basically a duplicate, so I leave this
one tree-optimization, not C++.

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

end of thread, other threads:[~2023-04-11 13:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 18:15 [Bug tree-optimization/109440] New: Missed optimization of vector::at when a function is called inside the loop hiraditya at msn dot com
2023-04-06 18:36 ` [Bug tree-optimization/109440] " pinskia at gcc dot gnu.org
2023-04-11 13:15 ` rguenth 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).