switch index optimization

Andi Kleen ak@muc.de
Tue Jan 27 13:09:00 GMT 1998


"Lassi A. Tuura" <Lassi.Tuura@cern.ch> writes:

> On Mon, 26 Jan 1998, Jeffrey A Law wrote:
> >   > 	if (x < 7) { if (x == 9) { } }
> > Yup.  However, it's become pretty clear to me that the direction I
> > was heading needs some serious work -- like full blown range checking
> > code as found in fold_const.c, except on RTL instead of trees (yuk).
> > 
> > It's also not clear how useful that would be in practice; my hacked
> > up version of cse.c only caught a small number of cases where it could
> > improve real code instead of contrived example code.
> 
> I would think this kind of an optimisation would pay off for C++ code
> where there are multitudes of small inlined methods.  The reason is that
> the methods cannot always tell what state the object is in and hence
> the body must be guarded with checks like this:
> 
>   if (! m_data) {
>      // initialise `m_data' to something
>   }
>   // real code here
> 
> As the methods get inlined, these checks can repeat several times even if
> it may be possible to prove that none of them (or only the first one)
> needs to be evaluated.  This may, again, result in significant dead code
> elimination and opportunities for further optimisation. 
> 
> It may well be that in C this optimisation does not produce any
> significant benefits because of the way code gets written: small inlines
> just aren't used all that much. 

Another example where it could benefit: gotoless exits from nested loops:

int flag = 0;
while (X > Y) {
	while (W > Z) {
		if (SOMETHING) {
			flag = 1;
			break;
		}
		....
	}
	if (flag)
		break;
}

That happens often in real-world code.

-Andi




More information about the Gcc mailing list