public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
@ 2020-03-29 14:23 petro.karashchenko at gmail dot com
  2020-03-29 14:28 ` [Bug middle-end/94387] " petro.karashchenko at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: petro.karashchenko at gmail dot com @ 2020-03-29 14:23 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
@ 2020-03-29 14:28 ` petro.karashchenko at gmail dot com
  2020-03-29 22:41 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: petro.karashchenko at gmail dot com @ 2020-03-29 14:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Petro Karashchenko <petro.karashchenko at gmail dot com> ---
Also the ambiguity of the issue is that excess read instructions generation
depends on type of the field. Excess reads are not generated when 8 bit types
are accessed and generated when data types greater that 8 bits are accessed.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
  2020-03-29 14:28 ` [Bug middle-end/94387] " petro.karashchenko at gmail dot com
@ 2020-03-29 22:41 ` pinskia at gcc dot gnu.org
  2020-03-29 22:43 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-29 22:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
volatile and packed on strict alignment targets are interesting to say the
least.
packed makes the alignment to 1 too.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
  2020-03-29 14:28 ` [Bug middle-end/94387] " petro.karashchenko at gmail dot com
  2020-03-29 22:41 ` pinskia at gcc dot gnu.org
@ 2020-03-29 22:43 ` pinskia at gcc dot gnu.org
  2020-03-30  5:44 ` petro.karashchenko at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-03-29 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
so I think this is fine as it is doing a read-write cycle as needed for strict
alignment requirements

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
                   ` (2 preceding siblings ...)
  2020-03-29 22:43 ` pinskia at gcc dot gnu.org
@ 2020-03-30  5:44 ` petro.karashchenko at gmail dot com
  2020-03-30  8:17 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: petro.karashchenko at gmail dot com @ 2020-03-30  5:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Petro Karashchenko <petro.karashchenko at gmail dot com> ---
Andrew Pinski could you please share with me requirements needed for strict
alignment?
Actually I do not understand why read-write cycle is needed if no "read" or
"modify" operation is requested (I mean no operations like |=, &=, +=, etc. are
issued), but a "pure" write a constant value is requested. In other words: what
is the reason of reading value that is discarded?

If I remove 'volatile' from a struct typedef I'm getting pretty optimised code
without excessive reads, to it seems to be a 'volatile'+'packed' combo.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
                   ` (3 preceding siblings ...)
  2020-03-30  5:44 ` petro.karashchenko at gmail dot com
@ 2020-03-30  8:17 ` rguenth at gcc dot gnu.org
  2020-03-30  8:44 ` petro.karashchenko at gmail dot com
  2020-04-19 20:16 ` petro.karashchenko at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-03-30  8:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
volatile semantics on misaligned fields and strict-align targets cannot be
honored.  I suggest you add appropriate __attribute__((aligned(..))) if you
know the whole structure is aligned and just want fields to be tightly
packed.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
                   ` (4 preceding siblings ...)
  2020-03-30  8:17 ` rguenth at gcc dot gnu.org
@ 2020-03-30  8:44 ` petro.karashchenko at gmail dot com
  2020-04-19 20:16 ` petro.karashchenko at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: petro.karashchenko at gmail dot com @ 2020-03-30  8:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Petro Karashchenko <petro.karashchenko at gmail dot com> ---
Richard Biener thank you for suggestion, but __attribute__((aligned(..))) is
applied only to the base address of the struct, hence to the first field only,
so if I'm having other fields tightly packed and there are 16, 32 or 64 bit
types I will still get excess read instructions generated. In my case I'm
having uint8_t *p0, uint8_t *p1 as an inputs and can't rely that those pointers
are aligned to 16, 32 or 64, but are byte aligned.

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

* [Bug middle-end/94387] Excess read instructions are generated in case of writing to fields of volatile + packed type (structure)
  2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
                   ` (5 preceding siblings ...)
  2020-03-30  8:44 ` petro.karashchenko at gmail dot com
@ 2020-04-19 20:16 ` petro.karashchenko at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: petro.karashchenko at gmail dot com @ 2020-04-19 20:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Petro Karashchenko <petro.karashchenko at gmail dot com> ---
Is it still 'UNCONFIRMED'? Or it can be moved to 'CONFIRMED' or 'ASSIGNED'
state?

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

end of thread, other threads:[~2020-04-19 20:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-29 14:23 [Bug middle-end/94387] New: Excess read instructions are generated in case of writing to fields of volatile + packed type (structure) petro.karashchenko at gmail dot com
2020-03-29 14:28 ` [Bug middle-end/94387] " petro.karashchenko at gmail dot com
2020-03-29 22:41 ` pinskia at gcc dot gnu.org
2020-03-29 22:43 ` pinskia at gcc dot gnu.org
2020-03-30  5:44 ` petro.karashchenko at gmail dot com
2020-03-30  8:17 ` rguenth at gcc dot gnu.org
2020-03-30  8:44 ` petro.karashchenko at gmail dot com
2020-04-19 20:16 ` petro.karashchenko at gmail dot com

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