Re: Executing code at compilation time

"Bartc" <bartc@freeuk.com>
Tue, 16 Mar 2010 16:14:10 -0000

          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)
[9 later articles]
| List of all articles for this month |

From: "Bartc" <bartc@freeuk.com>
Newsgroups: comp.compilers
Date: Tue, 16 Mar 2010 16:14:10 -0000
Organization: Netfront http://www.netfront.net/
References: 10-03-038
Keywords: optimize
Posted-Date: 16 Mar 2010 23:35:29 EDT

"Fernando" <pronesto@gmail.com> wrote in message


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


> 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.


(And which is a nuisance when comparing benchmarks with gcc...)


> Additionally, GCC does not resolve the loop in the program below:


> int i, j, k, t = 0;
> for(i = 0; i < 1000; i++)
> for(j = 0; j < 1000; j++)
> for(k = 0; k < 1000; k++)
> if(i * i + j * j + k * k % 7 == 0)
> 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?


I haven't tried 'folding' statements (only expressions).


But I would imagine that if the first loop was unrolled, you'd get 10
assignments, which, if you can fold one, you can fold them all (eg. sum+=0
is a no-op, sum+=1 sets sum to 1, and so on).


The second loop however, involving a billion compound statements, won't
yield so easily to this method.


--
Bartc


Post a followup to this message

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