Re: Compiler loop optimizations

Waldek Hebisch <hebisch@math.uni.wroc.pl>
11 Jan 2007 09:01:05 -0500

          From comp.compilers

Related articles
[2 earlier articles]
Re: Compiler loop optimizations robert.hundt@gmail.com (Robert H) (2007-01-01)
Re: Compiler loop optimizations robert.hundt@gmail.com (Robert H) (2007-01-01)
Re: Compiler loop optimizations torbenm@app-0.diku.dk (2007-01-05)
Re: Compiler loop optimizations linuxkaffee@gmx.net (Christian Sturz) (2007-01-05)
Re: Compiler loop optimizations emailamit@gmail.com (Amit Gupta) (2007-01-07)
Compiler loop optimizations Heiko.Falk@udo.edu (Heiko Falk) (2007-01-10)
Re: Compiler loop optimizations hebisch@math.uni.wroc.pl (Waldek Hebisch) (2007-01-11)
Compiler Loop Optimizations plfriko@yahoo.de (Tim Frink) (2008-02-28)
| List of all articles for this month |

From: Waldek Hebisch <hebisch@math.uni.wroc.pl>
Newsgroups: comp.compilers
Date: 11 Jan 2007 09:01:05 -0500
Organization: Politechnika Wroclawska
References: 06-12-109
Keywords: optimize, GCC
Posted-Date: 11 Jan 2007 09:01:05 EST

Christian Sturz <linuxkaffee@gmx.net> wrote:
> Hi,
>
> I was curious if there are any gcc compiler optimizations that can improve
> this code:
>
> void foo10( )
> {
> for ( int i = 0; i < 10; ++i )
> {
> [...]
> if( i == 15 ) { [BLOCK1] }
> }
> }
>


gcc 4.1.1 optimizes the loop above to nothing (since the loop produces
no result). On slightly more complicated function:


int
f10(char * s)
{
        int i, ss = 0;
        for (i=0; i<1000; i++) {
              ss += *s;
              s++;
              if (i == 1500) {
                        ss += i;
              }
        }
        return ss;
}


gcc 4.1.1 deletes the "if (i == 15)" block. In general gcc tracks
ranges of variables and uses this information to decide results of
tests (if possible). Once result of comparison is known to be
false gcc deletes the if-block.


BTW, with loop count of 10 at '-O3' gcc completly unrolled the loop.


> void foo100( )
> {
> for ( int i = 0; i < 100; ++i )
> {
> [...]
> if( i == 15 ) { [BLOCK2] }
> }
> }
>


Here gcc keeps test in the loop (provided that you do something usefull
in the loop, so that gcc can not delete the whole loop).


--
                                                            Waldek Hebisch
hebisch@math.uni.wroc.pl


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.