public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned
@ 2012-07-12 10:12 don.delfin at gmail dot com
  2012-07-12 10:50 ` [Bug c/53937] " don.delfin at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 10:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

             Bug #: 53937
           Summary: Pack'ing struct causes gcc to not recognize that an
                    field's address is aligned
    Classification: Unclassified
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: don.delfin@gmail.com


Created attachment 27779
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27779
Example C code

Hello,

When I pack my structure (use #pragma pack(1)), gcc compiler seems to not
recognize that an field in that structure is aligned.
This causes reading of aligned 32-bit fields, as 4 separate bytes. Apart from
this is very not optimal code, this resets my PowerPC. This is because 32-bit
registers can be accessed only as 32-bit words.

Example code:

================ test.c ================
#pragma pack(1)
typedef struct BasicStructPacked
{
    int field;
}BasicStructPacked;
#pragma pack()

typedef struct BasicStruct
{
    int field;
}BasicStruct;

#define ADDRESS    (0x1000)

#define STRUCT_REGULAR (*((BasicStruct*)(ADDRESS)))
#define STRUCT_PACKED  (*((BasicStructPacked*)(ADDRESS)))

void test_1()
{
    STRUCT_PACKED.field = 0;
}

void test_2()
{
    STRUCT_REGULAR.field = 0;
}
=======================================

This code generates the following assembler:

================ test.s ================
(...)
test_1:
    li 9,4096
    li 0,0
    stb 0,0(9)
    stb 0,1(9)
    stb 0,2(9)
    stb 0,3(9)
    blr
(...)
test_2:
    li 0,0
    li 9,4096
    stw 0,0(9)
    blr
(...)
=======================================

As you can see access to the aligned 32-bit field is done as 4 reads of one
byte (function test_1) instead of reading of one 32-bit word as it is done in
function test_2.


Used compilation command:

...GCC/powerpc-eabispe/4_6_0/bin/powerpc-eabispe-gcc-4.6.0.exe -mstrict-align
-Wall -Wextra -Os -c -S test.c

Regards, Krzysiek


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
@ 2012-07-12 10:50 ` don.delfin at gmail dot com
  2012-07-12 11:21 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 10:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

--- Comment #1 from Krzysztof Gongolewski <don.delfin at gmail dot com> 2012-07-12 10:49:57 UTC ---
Created attachment 27780
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27780
Compiled example code

Assembler


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
  2012-07-12 10:50 ` [Bug c/53937] " don.delfin at gmail dot com
@ 2012-07-12 11:21 ` rguenth at gcc dot gnu.org
  2012-07-12 12:24 ` don.delfin at gmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-07-12 11:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #2 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-07-12 11:21:23 UTC ---
Works as designed.  The packed attribute tells GCC that the start of the
struct and all members might not be naturally aligned.


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
  2012-07-12 10:50 ` [Bug c/53937] " don.delfin at gmail dot com
  2012-07-12 11:21 ` rguenth at gcc dot gnu.org
@ 2012-07-12 12:24 ` don.delfin at gmail dot com
  2012-07-12 12:30 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 12:24 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

--- Comment #3 from Krzysztof Gongolewski <don.delfin at gmail dot com> 2012-07-12 12:24:42 UTC ---
But the compiler knows address of the struct at compilation time, it's
hard-coded. So gcc knows that both struct and member are perfectly aligned.

If you access only the struct, You can see that compiler uses knowledge that he
knows the exact address, and fact that the structure is aligned and reads
32-bits in one instruction.

To prove it, I compiled two additional functions:

extern BasicStructPacked aa;
void test_3()
{
    *((int*)&STRUCT_REGULAR) = 0;
}

void test_4()
{
    *((int*)&aa) = 0;
}

And compiler optimized the first function (test_3) as he knew the address and
knew it is aligned. The second function (test_4) was not optimized as compiler
didn't know address at compilation time.

Output of compiler:
(...)
test_3:
    li 0,0
    li 9,4096
    stw 0,0(9)
    blr
(...)
test_4:
    lis 11,aa@ha
    li 0,0
    la 9,aa@l(11)
    stb 0,aa@l(11)
    stb 0,1(9)
    stb 0,2(9)
    stb 0,3(9)
    blr
(...)


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (2 preceding siblings ...)
  2012-07-12 12:24 ` don.delfin at gmail dot com
@ 2012-07-12 12:30 ` rguenth at gcc dot gnu.org
  2012-07-12 12:46 ` don.delfin at gmail dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-07-12 12:30 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|RESOLVED                    |ASSIGNED
   Last reconfirmed|                            |2012-07-12
         Resolution|INVALID                     |
         AssignedTo|unassigned at gcc dot       |rguenth at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #4 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-07-12 12:30:17 UTC ---
Ok, let me investigate on current trunk.  It might be that seeing 'packed'
makes us needlessly conservative here.


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (3 preceding siblings ...)
  2012-07-12 12:30 ` rguenth at gcc dot gnu.org
@ 2012-07-12 12:46 ` don.delfin at gmail dot com
  2012-07-12 12:50 ` don.delfin at gmail dot com
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 12:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Krzysztof Gongolewski <don.delfin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|missed-optimization         |
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |INVALID

--- Comment #5 from Krzysztof Gongolewski <don.delfin at gmail dot com> 2012-07-12 12:46:31 UTC ---
I added __attribute__((aligned(4))) to my packed structure, and this convinced
gcc compiler that my struct and also the member IS aligned. All mentioned
functions was optimized.

For me it is enough workaround, but I think it is bug in gcc, because
hard-coded address convince compiler that the struct is aligned but doesn't
convince that the member is aligned. And using align attribute convince
compiler that both the structure and the member are aligned.

In my opinion both options (hard-coding address and adding align attribute)
should convince that both struct and member are aligned and result in the same
optimized outputs.


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (4 preceding siblings ...)
  2012-07-12 12:46 ` don.delfin at gmail dot com
@ 2012-07-12 12:50 ` don.delfin at gmail dot com
  2012-07-12 12:53 ` don.delfin at gmail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 12:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Krzysztof Gongolewski <don.delfin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |ASSIGNED
         Resolution|INVALID                     |

--- Comment #6 from Krzysztof Gongolewski <don.delfin at gmail dot com> 2012-07-12 12:50:01 UTC ---
Restore of status which I destroyed my last comment.


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (5 preceding siblings ...)
  2012-07-12 12:50 ` don.delfin at gmail dot com
@ 2012-07-12 12:53 ` don.delfin at gmail dot com
  2012-07-13  9:45 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: don.delfin at gmail dot com @ 2012-07-12 12:53 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Krzysztof Gongolewski <don.delfin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #7 from Krzysztof Gongolewski <don.delfin at gmail dot com> 2012-07-12 12:53:33 UTC ---
Restore of destroyed status.
Sorry, It is my first submition :)


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (6 preceding siblings ...)
  2012-07-12 12:53 ` don.delfin at gmail dot com
@ 2012-07-13  9:45 ` rguenth at gcc dot gnu.org
  2012-07-13  9:46 ` rguenth at gcc dot gnu.org
  2012-07-13 10:09 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-07-13  9:45 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

--- Comment #8 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-07-13 09:45:06 UTC ---
Author: rguenth
Date: Fri Jul 13 09:45:00 2012
New Revision: 189458

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189458
Log:
2012-07-13  Richard Guenther  <rguenther@suse.de>

    PR middle-end/53937
    * builtins.c (get_pointer_alignment_1): Handle constant
    pointers.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/builtins.c


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (7 preceding siblings ...)
  2012-07-13  9:45 ` rguenth at gcc dot gnu.org
@ 2012-07-13  9:46 ` rguenth at gcc dot gnu.org
  2012-07-13 10:09 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-07-13  9:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.8.0

--- Comment #9 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-07-13 09:46:22 UTC ---
Fixed for 4.8.


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

* [Bug c/53937] Pack'ing struct causes gcc to not recognize that an field's address is aligned
  2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
                   ` (8 preceding siblings ...)
  2012-07-13  9:46 ` rguenth at gcc dot gnu.org
@ 2012-07-13 10:09 ` rguenth at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-07-13 10:09 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53937

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #10 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-07-13 10:09:12 UTC ---
Fixed I said.


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

end of thread, other threads:[~2012-07-13 10:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-12 10:12 [Bug c/53937] New: Pack'ing struct causes gcc to not recognize that an field's address is aligned don.delfin at gmail dot com
2012-07-12 10:50 ` [Bug c/53937] " don.delfin at gmail dot com
2012-07-12 11:21 ` rguenth at gcc dot gnu.org
2012-07-12 12:24 ` don.delfin at gmail dot com
2012-07-12 12:30 ` rguenth at gcc dot gnu.org
2012-07-12 12:46 ` don.delfin at gmail dot com
2012-07-12 12:50 ` don.delfin at gmail dot com
2012-07-12 12:53 ` don.delfin at gmail dot com
2012-07-13  9:45 ` rguenth at gcc dot gnu.org
2012-07-13  9:46 ` rguenth at gcc dot gnu.org
2012-07-13 10:09 ` rguenth at gcc dot gnu.org

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