Skip to content

Failure to recognize that int and int[1] cannot alias #145398

Open
@Kmeakin

Description

@Kmeakin

Thanks to C's strict aliasing rules, s->x and a->xs[0] cannot alias.
Therefore the second load of s->x in src is unnecessary.

https://godbolt.org/z/dcEPvE3ef

typedef struct Singleton {
    int x;
} Singleton;

typedef struct Array {
    int xs[1];
} Array;

int src(Singleton* s, Array* a) {
    s->x = 0;
    a->xs[0] = 1;
    return s->x;
}

int tgt(Singleton* s, Array* a) {
    s->x = 0;
    a->xs[0] = 1;
    return 0;
}
src:
        mov     w8, #1
        str     wzr, [x0]
        str     w8, [x1]
        ldr     w0, [x0]
        ret

tgt:
        mov     w8, #1
        str     wzr, [x0]
        mov     w0, wzr
        str     w8, [x1]
        ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions