Re: Executing code at compilation time

Andrew Poelstra <apoelstra@localhost.localdomain>
Tue, 16 Mar 2010 21:20:53 GMT

          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)
Re: Executing code at compilation time pat@jantar.org (Patryk Zadarnowski) (2010-03-17)
Re: Executing code at compilation time shreyas76@gmail.com (shrey) (2010-03-16)
Re: Executing code at compilation time pronesto@gmail.com (Fernando Magno Quintao Pereira) (2010-03-16)
Re: Executing code at compilation time pat@jantar.org (Patryk Zadarnowski) (2010-03-17)
Re: Executing code at compilation time gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-03-17)
[7 later articles]
| List of all articles for this month |

From: Andrew Poelstra <apoelstra@localhost.localdomain>
Newsgroups: comp.compilers
Date: Tue, 16 Mar 2010 21:20:53 GMT
Organization: Compilers Central
References: 10-03-038
Keywords: optimize
Posted-Date: 16 Mar 2010 23:37:30 EDT

On 2010-03-16, 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);
> }


To the best of my knowledge there is no gcc switch for
optimization past -O3. But I will speculate on the reason
for this behavior:


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


It likely stores variables and their values for as long as the
value can be determined - and if it is never used (aside from
as a value), it doesn't use any space.


This loop is simple. It counts to ten and contains no control
constructs. (Because it only counts to ten, there's a good
chance it unrolled to loop, and from there calculating i was
easy.)


> 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?
>
> Additionally, GCC does not resolve the loop in the program below:
>
> #include <stdio.h>
>
> int main(int argc, char **argv) {
> int i, j, k, t = 0;
> for(i = 0; i < 1000; i++)
> for(j = 0; j < 1000; j++)


If it didn't stop when it saw '1000', it probably stopped
here. Once you start nesting loops you can't unroll them
without massive code bloat - in this case you'd have
1 000 000 000 if statements!


Even without unrolling, it could /try/ to run through the
loop - but since there's a million counts (thus far) that
would likely result in a long compile time.


> for(k = 0; k < 1000; k++)
> if(i * i + j * j + k * k % 7 == 0)


I doubt gcc has any trouble with this, though to humans
it looks pretty scary. (In fact with optimization on I
think there is a warning about an "expression always
evaluating to true" if it knows it can bypass the if.)


> t++;
> printf("%d\n", t);
> return 0;
> }
>
> But it is "solvable" at compilation time. Is there any sort of non-
> standard optimization that does the job, in this case?
>


If there are hard-coded limits, you can edit the source to get
around them. If the problem is that it can't unroll the loop
and consequently can't resolve the expressions, you're pretty
much hooped unless you want to write a C interpreter.


> Thank you very much,
>
> Fernando


HTH, FWIW. HAND.


--
Andrew Poelstra
http://www.wpsoftware.net/andrew



Post a followup to this message

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