Use and misuse of __builtin_expect

Denis Efremov efremov@linux.com
Mon Aug 26 12:59:00 GMT 2019


Hi,

Is it correct to use __builtin_expect these 2 ways?

#define likely(x)	__builtin_expect(!!(x), 1)
#define unlikely(x)	__builtin_expect(!!(x), 0)

1) Negation before likely/unlikely.
   if (!unlikely(cond)) { ... }
   if (!likely(cond)) { ... }

I've replaced all !unlikely to likely and vice versa in the Linux
and obtained different binary results:

./scripts/bloat-o-meter ./vmlinux-old ./vmlinux-patched
add/remove: 0/0 grow/shrink: 3/4 up/down: 41/-35 (6)
Function                                     old     new   delta
dpaa2_io_service_rearm                       357     382     +25
intel_pmu_hw_config                         1277    1285      +8
get_sigframe.isra.constprop                 1657    1665      +8
csum_partial_copy_from_user                  605     603      -2
wait_consider_task                          3807    3797     -10
__acct_update_integrals                      384     373     -11
pipe_to_sendpage                             459     447     -12
Total: Before=312759461, After=312759467, chg +0.00%

2) As a part of the condition.
   if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) { ... }
   if (unlikely(info->thread_notes == 0) || unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) { ... }
   if (*s && unlikely(!d_can_lookup(root)) { ... }

Thanks,
Denis



More information about the Gcc-help mailing list