Re: Executing code at compilation time

glen herrmannsfeldt <gah@ugcs.caltech.edu>
Tue, 16 Mar 2010 06:29:58 +0000 (UTC)

          From comp.compilers

Related articles
Executing code at compilation time pronesto@gmail.com (Fernando) (2010-03-15)
Re: Executing code at compilation time gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-03-16)
Re: Executing code at compilation time torbenm@diku.dk (2010-03-16)
Re: Executing code at compilation time dnovillo@acm.org (Diego Novillo) (2010-03-16)
Re: Executing code at compilation time bartc@freeuk.com (Bartc) (2010-03-16)
Re: Executing code at compilation time quinn_jackson2004@yahoo.ca (Quinn Tyler Jackson) (2010-03-16)
Re: Executing code at compilation time apoelstra@localhost.localdomain (Andrew Poelstra) (2010-03-16)
Re: Executing code at compilation time bear@sonic.net (Ray) (2010-03-16)
[12 later articles]
| List of all articles for this month |

From: glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups: comp.compilers
Date: Tue, 16 Mar 2010 06:29:58 +0000 (UTC)
Organization: California Institute of Technology, Pasadena
References: 10-03-038
Keywords: optimize
Posted-Date: 16 Mar 2010 23:34:33 EDT

Fernando <pronesto@gmail.com> wrote:


> GCC does a pretty good job at optimizing a program like this one
> below:


> #include <stdio.h>
> int main(int argc, char** argv) {
> int i = 0;
> int sum = 0;
> for (; i < 10; i++) {
> sum += i;
> }
> printf("The sum is %d\n", sum);
> }


> GCC -O1 produces an assembly that simply prints the answer, 45. It
> completely resolves the loop.


> I would like to know what kind of optimizations are used to be able to
> completely resolve the loop. Could you give me some source? E.g,
> chapter in textbook, or paper?


There are many books on compiler technology, most of which should
have some discussion on optimization techniques.


C requires constant expression evaluation, so that part is easy.


One technique that has been known at least since the OS/360
Fortran H days is moving invariant expressions out of loops.
In this case, the expression isn't invariant, but with over
40 years of work it isn't too surprising. Another one is
called strength reduction: converting multplies by the loop
index variable into an addition operation, and exponentiation
into multiplication.


Compiler optimizations like this have been a problem for people
doing run-time benchmarks for many years. One way to avoid this
is to read in important numbers such that the compiler can't
optimize them away.


There was a story many years ago about a complex Fortran benchmark
program evaluating many mathematical expressions all using nested
statement functions. It turned out that the Fortran H compiler
did inline expansion of statement functions, and also did constant
expression evaluation. As with your example, the compiler did the
whole thing at compile time.


Maybe the most surprising thing about your example is that it
does it with the -O1 optimization level. One would expect it
to do even more with -O2 or -O3.


-- glen



Post a followup to this message

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