This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: branch predictions


Jamie Lokier <jamie.lokier@cern.ch> writes:

> On this theme of rearranging basic blocks, I have a couple of feature wishes.
> 
> 1. Annotations.  E.g., an attribute on a label to say "unlikely" or
>    "likely".  So you could write:
> 
>       if (unlikely_condition()) {
>         __attribute__ ((unlikely)):
>           /* ... */
>       }
> 
>    And the basic block rearranger would move the unlikely code after the
>    end of the fast path.  The if statement would have a branch
>    (predicted not taken) to the unlikely code, and the unlikely code
>    would end with a branch back to the next part of the fast path.
> 
>    I assume this whole thread is about that sort of BB rearrangement --
>    but based on guesswork and not programmer-specified annotations.
> 
>    I'm thinking things like fast paths in network code would benefit
>    from such annotations.  Not the generated code so much as the
>    source!

Already implemented: 

f(int i)
{ 
        int c,n;
        if (__builtin_expect(i == 0, 0)) { 
                printf("slow path\n"); 
                return 1;
        }
        printf("fast path\n"); 
        return 0;
}


compiled with gcc-current -O2 -freorder-blocks yields:

	.file	"t2.c"
	.version	"01.01"
gcc2_compiled.:
		.section	.rodata
.LC0:
	.string	"slow path\n"
.LC1:
	.string	"fast path\n"
.text
	.align 16
.globl f
	.type	 f,@function
f:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%eax
	pushl	%eax
	movl	8(%ebp), %eax
	testl	%eax, %eax
	je	.L4
	subl	$12, %esp
	pushl	$.LC1
	call	printf
	xorl	%eax, %eax
.L2:
	movl	%ebp, %esp
	popl	%ebp
	ret
	.p2align 4,,7
.L4:
	subl	$12, %esp
	pushl	$.LC0
	call	printf
	movl	$1, %eax
	jmp	.L2
.Lfe1:
	.size	 f,.Lfe1-f
	.ident	"GCC: (GNU) 2.96 20000501 (experimental)"


-Andi

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]