gcc version 4.0.0 20050203 (experimental) The following code snippet shows how branch prediction isnt used even when specifying builtin_expect. flags used: -O2 -mcpu=power4 #if 1 #define likely(x) __builtin_expect(!!(x), 1) #else #define likely(x) x #endif int i; void foo(void) { while (likely(i--)) { bar(); } } ... b .L2 .p2align 4,,15 .L3: bl bar nop .L2: lwz 9,0(31) addi 9,9,-1 extsw 9,9 cmpwi 7,9,-1 stw 9,0(31) bne 7,.L3
builtin_expect uses PROB_VERY_LIKELY. From predict.c #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 10 - 1) #define PROB_EVEN (REG_BR_PROB_BASE / 2) #define PROB_VERY_LIKELY (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY) #define PROB_ALWAYS (REG_BR_PROB_BASE) In other words, builtin_expect is using 10% / 90% But powerpc64 only emits branch prediction hints when at 2% / 98%. See rs6000.c:output_cbranch
(In reply to comment #1) > builtin_expect uses PROB_VERY_LIKELY. > > But powerpc64 only emits branch prediction hints when at 2% / 98%. See > rs6000.c:output_cbranch This is a target problem as mentioned above. Hmm, I thought there was a patch floating around to fix this but I could be wrong. Also note this changed back in 2002 for power4: <http://gcc.gnu.org/ml/gcc-patches/2002-08/msg00149.html> so in a way it could be considered a regression. It also effects power5 also (and ppc970).
patch http://gcc.gnu.org/ml/gcc-patches/2005-02/msg00225.html
Subject: Bug 19803 CVSROOT: /cvs/gcc Module name: gcc Changes by: amodra@gcc.gnu.org 2005-02-07 21:11:45 Modified files: gcc : ChangeLog predict.c Log message: PR target/19803 * predict.c (PROB_VERY_UNLIKELY): Use 1% instead of 10%. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.7414&r2=2.7415 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/predict.c.diff?cvsroot=gcc&r1=1.137&r2=1.138
Subject: Bug 19803 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_4-branch Changes by: amodra@gcc.gnu.org 2005-02-07 21:18:33 Modified files: gcc : ChangeLog predict.c Log message: PR target/19803 * predict.c (PROB_VERY_UNLIKELY): Use 1% instead of 10%. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=2.2326.2.794&r2=2.2326.2.795 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/predict.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.98&r2=1.98.2.1
Fixed.