Skip to content

bpf target, preserve_access_index gives useless bitshifts #59908

Open
@socketpair

Description

@socketpair

https://godbolt.org/z/ProhnxfTv

-target bpf -mcpu=v2 -O3

typedef unsigned int __u32;
typedef long long unsigned int __u64;

#pragma clang attribute push(__attribute__((preserve_access_index)), \
                                 apply_to = record)
struct iphdr {
    __u32 protocol;
    __u32 daddr;
};
#pragma clang attribute pop

int some_fun(const struct iphdr* ip) {
    const __u64 dst_ip = ip->daddr;  // note __u64 variable

    if (ip->protocol) __asm__("" ::: "memory");

    // only if: __asm__ present, -mcpu=v{1,2} -O3 and preserve_access_index
    return dst_ip == 11223344ull;
}

int some_fun_2(const struct iphdr* ip) {
    const __u64 dst_ip = ip->daddr;  // note __u64 variable
    return dst_ip == 11223344ull;
}


// no preserve_access_index
struct iphdr2 {
    __u32 protocol;
    __u32 daddr;
};

int some_fun_3(const struct iphdr2* ip) {
    const __u64 dst_ip = ip->daddr; // note __u64 variable

    if (ip->protocol) __asm__("" ::: "memory");

    return dst_ip == 11223344ull;
}
some_fun:                               # @some_fun
        r2 = *(u32 *)(r1 + 4)
        r1 = *(u32 *)(r1 + 0)
        if r1 == 0 goto LBB0_2 ;--------useless conditional jump (that's another issue?)
LBB0_2:
        ; ---------------------------------------------- WHAT's this ?!
        r2 <<= 32
        r2 >>= 32
        if r2 == 11223344 goto LBB0_4
LBB0_4:
        exit
; ######################
some_fun_2:                             # @some_fun_2
        r1 = *(u32 *)(r1 + 4)
        if r1 == 11223344 goto LBB1_2  ; --------------- no shifts here
LBB1_2:
        exit
; ######################
some_fun_3:                             # @some_fun_3
        r2 = *(u32 *)(r1 + 4)
        r1 = *(u32 *)(r1 + 0)
        if r1 == 0 goto LBB2_2
LBB2_2:
        r1 = r2
        if r1 == 11223344 goto LBB2_4  ; --------------- no shifts here
LBB2_4:
        exit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions