https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94387
Bug ID: 94387 Summary: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) Product: gcc Version: 9.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: petro.karashchenko at gmail dot com Target Milestone: --- Created attachment 48140 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48140&action=edit preprocessed file Excess read instructions are generated when access members of volatile + packed types (structures): test.c: ---------- #include <stdint.h> typedef volatile struct type1_s { uint32_t a1; uint8_t a2; uint8_t a3; uint8_t a4; uint8_t a5; } __attribute__((packed)) type1_t; typedef volatile struct { uint32_t b1; uint32_t b2; } __attribute__((packed)) type2_t; typedef volatile struct type3_s { type1_t h1; volatile union { uint8_t b[24]; type2_t c1; } __attribute__((packed)) h2; } __attribute__((packed)) type3_t; typedef volatile struct type4_s { uint32_t x1; uint8_t x2; uint16_t x3; uint8_t x4; uint8_t x5; uint8_t x6; } __attribute__((packed)) type4_t; static void my_func2(type3_t *p0, type4_t *p1) ; int my_func1(uint8_t *p0, uint8_t *p1) { type3_t *i = (type3_t *)p0; type4_t *o = (type4_t *)p1; my_func2(i, o); return 0; } static void my_func2(type3_t *p0, type4_t *p1) { p1->x1 = 0xFFFFFF01; p1->x6 = 1; p1->x2 = 2; p1->x4 = p0->h1.a3; p1->x5 = p0->h1.a4; p1->x3 = 0; } ---------- arceb-elf32-gcc -save-temps -Wall -Wextra -c -mcpu=arc600 -mtune=arc600 -mbig-endian -mmul64 test.c -Os ---------- Disassembly: .global my_func1 .type my_func1, @function my_func1: ldb_s r2,[r1] mov r2,-1 ;6 stb_s r2,[r1] ldb_s r3,[r1,1] stb_s r2,[r1,1] ldb_s r3,[r1,2] stb_s r2,[r1,2] ldb_s r2,[r1,3] mov_s r3,1 ;0 stb_s r3,[r1,3] stb_s r3,[r1,9] mov_s r3,2 stb_s r3,[r1,4] ldb_s r3,[r0,5] mov_s r2,0 ;0 stb_s r3,[r1,7] ldb_s r0,[r0,6] stb_s r0,[r1,8] ldb_s r0,[r1,5] stb_s r2,[r1,5] ldb_s r0,[r1,6] stb_s r2,[r1,6] mov_s r0,0 ;0 j_s [blink] .size my_func1, .-my_func1 ---------- Expected disassembly: .global my_func1 .type my_func1, @function my_func1: mov r2,-1 ;6 stb_s r2,[r1] stb_s r2,[r1,1] stb_s r2,[r1,2] mov_s r3,1 ;0 stb_s r3,[r1,3] stb_s r3,[r1,9] mov_s r3,2 stb_s r3,[r1,4] ldb_s r3,[r0,5] mov_s r2,0 ;0 stb_s r3,[r1,7] ldb_s r0,[r0,6] stb_s r0,[r1,8] stb_s r2,[r1,5] stb_s r2,[r1,6] mov_s r0,0 ;0 j_s [blink] .size my_func1, .-my_func1 ---------- I have checked same code compilation with: arm-none-eabi-gcc -save-temps -Wall -Wextra -c -mcpu=arm7tdmi -mthumb test.c -Os The result is pretty much the same, so it is not architecture dependent bug.