https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87664
Bug ID: 87664 Summary: invariant in loop after optimization Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: drepper.fsp+rhbz at gmail dot com Target Milestone: --- Compile the following code with the current trunk or gcc 8.2.1: #include <array> #include <numeric> int rs(int s) { std::array<int,100> ar; std::iota(ar.begin(), ar.end(), s); return std::accumulate(ar.begin(), ar.end(), 0); } With -O2 this leads on x86-64 to the following code: 0000000000000000 <rs(int)>: 0: 48 81 ec 20 01 00 00 sub $0x120,%rsp 7: 48 8d 44 24 88 lea -0x78(%rsp),%rax c: 0f 1f 40 00 nopl 0x0(%rax) 10: 89 38 mov %edi,(%rax) 12: 48 8d 8c 24 18 01 00 lea 0x118(%rsp),%rcx 19: 00 1a: 48 83 c0 04 add $0x4,%rax 1e: ff c7 inc %edi 20: 48 39 c8 cmp %rcx,%rax 23: 75 eb jne 10 <rs(int)+0x10> 25: 48 8d 54 24 88 lea -0x78(%rsp),%rdx 2a: 31 c0 xor %eax,%eax 2c: 0f 1f 40 00 nopl 0x0(%rax) 30: 03 02 add (%rdx),%eax 32: 48 8d b4 24 18 01 00 lea 0x118(%rsp),%rsi 39: 00 3a: 48 83 c2 04 add $0x4,%rdx 3e: 48 39 f2 cmp %rsi,%rdx 41: 75 ed jne 30 <rs(int)+0x30> 43: 48 81 c4 20 01 00 00 add $0x120,%rsp 4a: c3 retq The relevant parts are the loop starting at offsets 10 and 30. The respective lea instructions to compute the end address of the loop at offset 12 and 32 are invariant and should be hoisted out of the loops.