Re: Executing code at compilation time

Quinn Tyler Jackson <quinn_jackson2004@yahoo.ca>
Tue, 16 Mar 2010 12:52:15 -0700

          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)
[8 later articles]
| List of all articles for this month |
From: Quinn Tyler Jackson <quinn_jackson2004@yahoo.ca>
Newsgroups: comp.compilers
Date: Tue, 16 Mar 2010 12:52:15 -0700
Organization: Compilers Central
References: 10-03-038
Keywords: optimize
Posted-Date: 16 Mar 2010 23:36:27 EDT

On Mon, Mar 15, 2010 at 6:53 PM, Fernando <pronesto@gmail.com> wrote:


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


Typically, such a loop is unwound to 10 invariant increment
instructions and then an iterative peephole optimization pass is used
to determine that final constant value. Loop unwinding and peephole
optimization are covered in various texts on compiler construction.


You then asked:


> int main(int argc, char **argv) {
> 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?


While the above could be theoretically achieved using those same above
optimization techniques, compilers do not unwind beyond a reasonable
limit (for some value of "reasonable").


Quinn



Post a followup to this message

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