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