<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/59908>59908</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
bpf target, preserve_access_index gives useless bitshifts
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
socketpair
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/ProhnxfTv
`-target bpf -mcpu=v2 -O3`
```C
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=v2 -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;
}
```
```asm
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
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVt-PozYQ_mucl1FWZkw24YGHzeWiPlRqH07qIzJgwFeDkW3S3f71lQ1h84Pd6-21OmSxLJ7xzHzfx2S4tbLuhEjJZk82hxUfXKNNanXxp3A9l2aV6_IlbZzrLWFPBI8Ej7Uuc63cgzY1wePfBI-_G910z9WXE6EHQp-m-yNdO25q4SDvK1i3RT8QdjghrH9j5JHe2I7r0_i_e-lFKSoYupBfCbJzkGUDQ8L21yZKd_V4uzV-jGfj6Y6sN7xuORSKdzVw54zMByegH2xDcJdl86ssI7gjuOuNsMKcRMaLQlibya4UzwSTsD4B2UwZA-979ZI5DYQdwIhCm9LbhE3rzFA4kH1TGiDbKSkAGGuC3minC63mfKf3JS9L81rE9vD6_HYpur-s2WNhdSuyaugI7grdWQeX-RB8AtkTTK4TGw0DilBal8k-FCb7NWGfz3kBjIqATjsxGZ-4kTxX4jIJf6CsgOBu9J_rxQSyjNt2QhsJInidhQUEsRWtNi9hJ7lh8xxbd-oFZOUdprMgkNY5T9C17IB3JSxTOp5phBtMd1GxLzqKEBmL40GpKzLegDnDnwD0hzM_62k6_j10LqvBf6fjeesdKd8DyBYBxB9C8GcqFeAj9Jx74mKj5LadSDl_3CENBiSm86s5esgAAywEnwjuPCXhKSG4MxEQ3EM896vZJfqGC71zkdXk5R0p1Npp-HW_p5mPvl9P12CFEtZ66krppO64gq9D23vgXcMdwa0F3mnXCAPS2kEQdpxjjed56K9Ce5bX33XBH788fQmxXCMthBjRAmqfwjoAw4XNz2Hdb3ok8JbpC0Dii2Liu2LEs3QT5UG87EfXlVgCerdyyW6r-xb7F4K5ov2u2CjDRXp8v7GNrJyFRhgxAxItsPs_A8KWAGEf_4L-u08Hz7xMzzMwUwizoLu3qcAs_i4q8H1tLvaoi_uqTFmZsISvRBo9bhmj290mXjWp4EkltrSgKCJKyyKP84pXVREJUcVxXK5kihQZjWiCLI6j-IFu4jzBPC8TxCraJCSmouVSPSh1av0wugqNIt0kCd2tFM-FsmGuRezEX1MXQfRjrkm9zzofaktiqqR19vUUJ50SqR9Zx-nVTxGLP4pQy5OwcO5luXQjgKvBqNuRWbpmyB8K3RI8-kjTn3Vv9FdROILHkJ8leAz5_xMAAP__tpYeRw">