Subject: Re: [PATCH] tg3 : avoid an expensive divide

From: Andi Kleen <[email-masked]>
Date: Wed, 7 Feb 2007 11:27:42 +0100

> Well, this could explain but unfortunatly I dont have this option set :
> 
> # grep OPTIMIZE .config
> # CONFIG_CC_OPTIMIZE_FOR_SIZE is not set
> 
> # gcc -v
> Using built-in specs.
> Target: x86_64-unknown-linux-gnu
> Configured with: ../gcc-4.1.1/configure --enable-languages=c,c++

Ah. Looking more closely the problem is different (sorry for the wrong
explanation earlier, it fit too well and it really happened elsewhere)

The code is 

#define TG3_RX_RCB_RING_SIZE(tp)        \
        ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ?  512 : 1024)

...
sw_idx %= TG3_RX_RCB_RING_SIZE(tp);

The problem is that gcc doesn't rewrite the x % (cond ? const1 : const2) expression
to the more optimizable cond ? (x % const1 : x % const2). For the first it sees
a full variable division, for which div* is the best code to generate.

Your patch is probably correct or alternatively rewrite the expression 
to the optimizable form by hand.

Arguably gcc should do this by itself. Perhaps file a gcc bug about this missing optimization. 

-Andi