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

            Bug ID: 94662
           Summary: __attribute__ aligned is ignored
           Product: gcc
           Version: 9.2.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: ---

__attribute__ 'aligned' is ignored.

Test case 1:
----------
int __attribute__((aligned(1))) var;

int foo(void)
{
  return var;
}
----------
arm-none-eabi-gcc -save-temps -Wall -Wextra -c -mcpu=arm7tdmi -mthumb test.c
-O0
----------
Generated assembly:
        .cpu arm7tdmi
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 6
        .eabi_attribute 34, 0
        .eabi_attribute 18, 4
        .file   "test.c"
        .text
        .comm   var,4,1
        .align  1
        .global foo
        .arch armv4t
        .syntax unified
        .code   16
        .thumb_func
        .fpu softvfp
        .type   foo, %function
foo:
        @ Function supports interworking.
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        add     r7, sp, #0
        ldr     r3, .L3
        ldr     r3, [r3]
        movs    r0, r3
        mov     sp, r7
        @ sp needed
        pop     {r7}
        pop     {r1}
        bx      r1
.L4:
        .align  2
.L3:
        .word   var
        .size   foo, .-foo
        .ident  "GCC: (GNU Tools for Arm Embedded Processors 9-2019-q4-major)
9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]"
----------

Test case 2:
----------
int __attribute__((aligned(1))) * var = (int *)0x03;

int foo(void)
{
  return *var;
}
----------
arm-none-eabi-gcc -save-temps -Wall -Wextra -c -mcpu=arm7tdmi -mthumb test.c
-O0
----------
Generated assembly:
        .cpu arm7tdmi
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 6
        .eabi_attribute 34, 0
        .eabi_attribute 18, 4
        .file   "test.c"
        .text
        .global var
        .data
        .type   var, %object
        .size   var, 4
var:
        .4byte  3
        .text
        .align  1
        .global foo
        .arch armv4t
        .syntax unified
        .code   16
        .thumb_func
        .fpu softvfp
        .type   foo, %function
foo:
        @ Function supports interworking.
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        add     r7, sp, #0
        ldr     r3, .L3
        ldr     r3, [r3]
        ldr     r3, [r3]
        movs    r0, r3
        mov     sp, r7
        @ sp needed
        pop     {r7}
        pop     {r1}
        bx      r1
.L4:
        .align  2
.L3:
        .word   var
        .size   foo, .-foo
        .ident  "GCC: (GNU Tools for Arm Embedded Processors 9-2019-q4-major)
9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]"
----------

Test case 3:
----------
int foo(void)
{
  return *(int __attribute__((aligned(1))) *) 0x03;
}
----------
arm-none-eabi-gcc -save-temps -Wall -Wextra -c -mcpu=arm7tdmi -mthumb test.c
-O0
----------
Generated assembly:
        .cpu arm7tdmi
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 6
        .eabi_attribute 34, 0
        .eabi_attribute 18, 4
        .file   "test.c"
        .text
        .align  1
        .global foo
        .arch armv4t
        .syntax unified
        .code   16
        .thumb_func
        .fpu softvfp
        .type   foo, %function
foo:
        @ Function supports interworking.
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        add     r7, sp, #0
        movs    r3, #3
        ldr     r3, [r3]
        movs    r0, r3
        mov     sp, r7
        @ sp needed
        pop     {r7}
        pop     {r1}
        bx      r1
        .size   foo, .-foo
        .ident  "GCC: (GNU Tools for Arm Embedded Processors 9-2019-q4-major)
9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]"
----------

In all 3 test cases I'm expecting that unaligned access code should be
generated, but in all 3 test cases generated access is aligned.

However in next test case the align attribute actually takes effect:
----------
int foo(void)
{
  return **(int * __attribute__((aligned(1))) *) 0x03;
}

int foo1(void)
{
  return ***(int * __attribute__((aligned(1))) * __attribute__((aligned(1))) *)
0x03;
}
----------
arm-none-eabi-gcc -save-temps -Wall -Wextra -c -mcpu=arm7tdmi -mthumb test.c
-O0
----------
Generated assembly:
        .cpu arm7tdmi
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 1
        .eabi_attribute 30, 6
        .eabi_attribute 34, 0
        .eabi_attribute 18, 4
        .file   "test.c"
        .text
        .align  1
        .global foo
        .arch armv4t
        .syntax unified
        .code   16
        .thumb_func
        .fpu softvfp
        .type   foo, %function
foo:
        @ Function supports interworking.
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        add     r7, sp, #0
        movs    r3, #3
        ldrb    r2, [r3]
        ldrb    r1, [r3, #1]
        lsls    r1, r1, #8
        orrs    r2, r1
        ldrb    r1, [r3, #2]
        lsls    r1, r1, #16
        orrs    r2, r1
        ldrb    r3, [r3, #3]
        lsls    r3, r3, #24
        orrs    r3, r2
        ldr     r3, [r3]
        movs    r0, r3
        mov     sp, r7
        @ sp needed
        pop     {r7}
        pop     {r1}
        bx      r1
        .size   foo, .-foo
        .align  1
        .global foo1
        .syntax unified
        .code   16
        .thumb_func
        .fpu softvfp
        .type   foo1, %function
foo1:
        @ Function supports interworking.
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        add     r7, sp, #0
        movs    r3, #3
        ldrb    r2, [r3]
        ldrb    r1, [r3, #1]
        lsls    r1, r1, #8
        orrs    r2, r1
        ldrb    r1, [r3, #2]
        lsls    r1, r1, #16
        orrs    r2, r1
        ldrb    r3, [r3, #3]
        lsls    r3, r3, #24
        orrs    r3, r2
        ldrb    r2, [r3]
        ldrb    r1, [r3, #1]
        lsls    r1, r1, #8
        orrs    r2, r1
        ldrb    r1, [r3, #2]
        lsls    r1, r1, #16
        orrs    r2, r1
        ldrb    r3, [r3, #3]
        lsls    r3, r3, #24
        orrs    r3, r2
        ldr     r3, [r3]
        movs    r0, r3
        mov     sp, r7
        @ sp needed
        pop     {r7}
        pop     {r1}
        bx      r1
        .size   foo1, .-foo1
        .ident  "GCC: (GNU Tools for Arm Embedded Processors 9-2019-q4-major)
9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]"
----------

My expectation is next:
1. int __attribute__((aligned(1))) * var; -- aligned pointer to unaligned 'int'
value, hence the '*var' (memory addressed by pointer access) code should
generate a set of 'ldrb'->`lsls`->'orrs', but 'var' (pointer value access) code
should generate 'ldr'.
2. int * __attribute__((aligned(1))) var; -- unaligned pointer to aligned 'int'
value, hence the 'var' (pointer value access) code should generate a set of
'ldrb'->`lsls`->'orrs', but '*var' (memory addressed by pointer access) code
should generate 'ldr'.
3. int __attribute__((aligned(1))) * __attribute__((aligned(1))) var; --
unaligned pointer to unaligned 'int' value, hence both 'var' (pointer value
access) and '*var' (memory addressed by pointer access) code should generate  a
set of 'ldrb'->`lsls`->'orrs'.

Reply via email to