gnu/gcc/4157a5aa7e7667e7873638ee85535aa3d2ea02b9 bpf: TARGET_RTX_COSTS: treat 32-bit CONST_INT as cheap/free
This is to prefer constant return to be setup from imm constant vs.
using a prior reg known to have the same value (by control flow analysis).
While there's nothing wrong with existing approach, it can sometimes leads
to additional sign-extensions and other corner cases which trip up the
bpf kernel verifier bounds checking for return reg.The verifier improvements
are being worked on but lets adjust the cost model anyways so that constants
are favored. There's no additional overhead from new codegen anyways.
For accompanying test const-cost-model.c, at -O2
Before | After
-------------------------------+------------------------------
r1 = 4 | r1 = 4
call bpf_copy_from_user_str | call bpf_copy_from_user_str
r1 = (s32) r0 | ...
... | r1 = 4 <---
call bpf_copy_from_user_str | call bpf_copy_from_user_str
This causes 4 additioanl bpf kernel selftests to pass (as of v7.1)
| -Summary: 596/5387 PASSED, 123 SKIPPED, 114 FAILED
| +Summary: 600/5422 PASSED, 123 SKIPPED, 110 FAILED
| #77 cgroup_xattr:OK
| #112 exe_ctx:OK
| #220 mem_rdonly_untrusted:OK
| #574 verifier_global_ptr_args:OK
gcc/ChangeLog:
* config/bpf/bpf.cc (bpf_rtx_costs): set CONST_INT 0 insns.
* config/bpf/bpf.h (BPF_IMM32_P): New helper macro.
* config/bpf/predicates.md (imm32_operand): Use new macro.
gcc/testsuite/ChangeLog:
* gcc.target/bpf/const-cost-model.c: New test prefers const.
* gcc.target/bpf/const-cost-model-2.c: New test prefers const.
Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
(cherry picked from commit e65d38a0dc843d36dd5605c6805645923b44c6a7)
5 files changed