https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90962
Bug ID: 90962 Summary: Array bound over optimization Product: gcc Version: 7.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: patrick.pelissier at gmail dot com Target Milestone: --- For the following program, GCC optimizes away the loop that affects the array: #include <stdlib.h> #include <stdint.h> struct node_s { unsigned int size; struct m_s *tab[1]; }; typedef struct m_s { unsigned int type; union { struct node_s n; } val[1]; } *m_t; extern m_t node_c(uint8_t, unsigned int_t); #define SET_AT(_x,_n,_y) ((_x)->val[0].n.tab[(_n)] = (_y)) m_t add_vc (unsigned int size, const m_t *tab) { if (size == 0) return NULL; m_t y = node_c (2, size); for (unsigned int i = 0; i < size; i++) /* here */ SET_AT (y, i, tab[i]); return y; } The following asm is generated for x86-64 when building it with gcc -O2 -S t.c: add_vc: .LFB10: .cfi_startproc testl %edi, %edi je .L3 pushq %rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 movq %rsi, %rbx movl %edi, %esi movl $2, %edi call node_c movq (%rbx), %rdx movq %rdx, 16(%rax) // Only tab[0] is set popq %rbx .cfi_def_cfa_offset 8 ret .p2align 4,,10 .p2align 3 .L3: .cfi_restore 3 xorl %eax, %eax ret The loop is removed with GCC 7.3, GCC 8.2 and GCC 9.1: only tab[0] is set. The loop is not removed and the program behaves as expected with GCC 4.9, GCC 6.3