GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
bpf Performance Win

bpf: Reduce cost of multiply/divide to avoid synthesis

Lowers the cost of BPF multiply and divide operations to match native ISA, preventing unnecessary synthesis.

This commit reduces the estimated cost of multiplication, division, and modulo operations in GCC’s BPF backend. The default costs for these operations were high, leading the compiler to synthesize complex sequences instead of using native BPF instructions. By setting the cost for MPY, DIV, and MOD to 1, GCC now reliably generates direct BPF hardware instructions for these operations. A test case was also adjusted to ensure the signed argument handling is correct for the new cost model.

In Details

The bpf_rtx_costs function is responsible for assigning costs to various RTX primitives for the BPF target. The default costs for MULT, DIV, and MOD in GCC's generic rtx_cost are high (5-7 instructions). For BPF, which has native instructions for these operations, these high default costs cause unnecessary libcall synthesis or complex instruction expansion. This patch assigns a cost of 1 to these operations, ensuring that the compiler prefers the direct BPF ISA instructions. A test case divmod-libcall-2.c was modified to handle signed arguments correctly, as the previous version…

For Context
BPF
Berkeley Packet Filter, a technology allowing sandboxed programs to make safe network packet filtering and handling.
RTX
Representation of intermediate code in GCC, used by the backend to describe operations and data. RTX stands for 'RePresentation of code EXpression'.
libcall
A call to a function within a library, often used by the compiler when a direct instruction cannot fulfill an operation.
synthesis
The process by which a compiler generates code for an operation that is not directly supported by a single machine instruction, by combining multiple instructions or calling library functions.
Filed Under: bpfoptimizationcost modelisa