bpf: Favor 32-bit constants over registers for return values
Adjusts BPF cost model to prefer immediate 32-bit constants for return values, improving verifier compatibility.
GCC’s BPF backend now treats 32-bit immediate constants as cheap or free when estimating instruction costs. This change encourages the compiler to use immediate constants for return values rather than relying on previously computed register values. The goal is to avoid potential issues with the BPF kernel verifier, such as extra sign-extensions, that can arise when constants are introduced through register initialization. This adjustment has already led to more BPF selftests passing.
In Details
This commit tunes the TARGET_RTX_COSTS for the BPF backend, specifically assigning a cost of 0 to CONST_INT when it represents a 32-bit value (using the new BPF_IMM32_P predicate). Previously, using a register known to hold a value might have been favored over a constant, leading to complex initialization sequences. By making immediate constants cheaper, the compiler prefers generating code like mov r1, <immediate> over loading a value into a register and then using that register. This simplification helps avoid corner cases that can trip up the BPF verifier, particularly concerning r…
- BPF
- Berkeley Packet Filter, a technology allowing sandboxed programs to make safe network packet filtering and handling.
- TARGET_RTX_COSTS
- GCC target hook that defines the cost of various Representation of code EXpression (RTX) primitives, influencing optimization decisions.
- CONST_INT
- An RTX primitive representing an integer constant value.
- BPF verifier
- A component of the Linux kernel that statically analyzes BPF programs before execution to ensure they are safe and do not crash the system.