Bug 19803 - __builtin_expect does not modify branch prediction for power4/5
Summary: __builtin_expect does not modify branch prediction for power4/5
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2005-02-07 03:24 UTC by Anton Blanchard
Modified: 2005-02-07 21:23 UTC (History)
2 users (show)

See Also:
Host:
Target: powerpc*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-02-07 03:28:18


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anton Blanchard 2005-02-07 03:24:08 UTC
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
Comment 1 Alan Modra 2005-02-07 03:28:17 UTC
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
Comment 2 Andrew Pinski 2005-02-07 03:39:45 UTC
(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).
Comment 3 Alan Modra 2005-02-07 04:47:25 UTC
patch http://gcc.gnu.org/ml/gcc-patches/2005-02/msg00225.html
Comment 4 GCC Commits 2005-02-07 21:12:21 UTC
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

Comment 5 GCC Commits 2005-02-07 21:18:50 UTC
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

Comment 6 Alan Modra 2005-02-07 21:23:53 UTC
Fixed.