This is the mail archive of the gcc-help@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]

Code Branch Optimization - How to specify rarely used path?


Hello,
	I am attempting to write some high performance code using gcc
2.95 and 2.96 for a MIPS CPU for a debug logging facility
which is run-time enabled.

The source code for a generic function f(), which uses the logging
system (as produced by inline code and/or macros) would be something
like:

void f()
{	
	doStuff1
	if (logOn)
	{
		LogPrint(format, args, ...);
	}
	doStuff2
}

The problem is, the compiler will generate code which looks something
like (pseudo machine code used below):
	doStuff1
	test logOn
	branchZero label1
	push format and args
	call Log_Print
	pop format and args
label1:
	doStuff2
	ret

What I would like is a way to tell the compiler that the call to
LogPrint (ie. the then clause of the if) is rarely taken.
As such I would like the compiler/optimizer to generate code for the
function which looks something like:
	doStuff1
	test logOn
	branchNZ label1
label2:
	doStuff2
	ret
label1:
	push format and args
	call LogPrint
	pop format and args
	jmp label2

I realize the code I would like generated is a little more convoluted
than the original.  However for branches rarely taken, the
more convoluted code is actually faster.  Partly because the Instruction
Cache in the CPU will benefit from pre-fetching and L2 cache
line hits in the 2nd example, where in the 1st example L2 and L1 cache
space is wasted with the infrequently used code.

Now the question is, is there a way in source code to inform the
compiler/optimizer than the then clause is rarely taken?

I realize there are tools to analyze the code runtime and feed back into
the compiler for better branch prediction, however is
there a way to provide that kind of information to the
compiler/optimizer when initially writing the code?

I do not usually read this list, so please send replies to
TRimmer@InfiniConSys.com and I will post a summary of the responses.

Thank You,
Todd Rimmer
Staff Software Engineer        InfiniCon Systems
Voice: 610-205-0130x19         Fax: 610-205-0488
TRimmer@InfiniConSys.com       www.InfiniConSys.com

 <<Todd Rimmer.vcf>> 
BEGIN:VCARD
VERSION:2.1
N:Rimmer;Todd
FN:Todd Rimmer
ORG:InfiniCon Systems;Engineering
TITLE:Lead Software Engineer
TEL;WORK;VOICE:610-205-0130x19
TEL;WORK;FAX:610-205-0488
ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;Suite 101=0D=0A700 American Ave;King of Prussia;Pa;19406;USA
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Suite 101=0D=0A700 American Ave=0D=0AKing of Prussia, Pa 19406=0D=0AUSA
ADR;HOME:;;;;;;United States
LABEL;HOME:United States
URL:
URL:http://www.InfiniConSys.com
EMAIL;PREF;INTERNET:TRimmer@InfiniConSys.com
REV:20000823T131043Z
END:VCARD

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