For Programmers: Free Programming Magazines  


Home > Archive > Compilers > September 2007 > optimizing address calculation?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author optimizing address calculation?
joggingsong@gmail.com

2007-09-18, 8:11 am

Hi, everyone.I encountered the problem and hope to get help from you.

Here is some structs definition.
typedef struct test{
int a[20];
int b[20];

}test1;

typedef struct test{
int c[20];
int d[20];
test1 test1a[20];
}test2;

test2 test2a;
test2 *pTest2a = &test2a;

in the program, I refer to the variable test2a by the pointer
for example

for(i = 0; i <20; i++)
{
pTest2a->test1a[x].b[i] = i;
}

Although in the loop pTest2a->test1a[x] is fixed, it seems the
generated codes calculate
the address from pTest2a. If it is changed to the following:

int pInt = pTest2a->test1a[x].b;
for(i = 0; i < 20; i++)
{
*pInt++ = i;
}
The new generated code is much faster.
Is it difficult for c compiler to generate the faster code?

Thanks
Jogging

[I don't think it's difficult, I think nested structures of arrays are
rare enough that the people who write optimizer in whatever compiler
you're using didn't worry about it. -John]

Hans-Peter Diettrich

2007-09-19, 7:16 pm

joggingsong@gmail.com wrote:

> in the program, I refer to the variable test2a by the pointer
> for example
>
> for(i = 0; i <20; i++)
> {
> pTest2a->test1a[x].b[i] = i;
> }
>
> Although in the loop pTest2a->test1a[x] is fixed, it seems the
> generated codes calculate
> the address from pTest2a.


Perhaps the compiler couldn't figure out, how x and i are related to
the loop (invariant, loop index, side effects...).

That's why e.g. Pascal doesn't allow to modify the loop index variable
inside the loop, so that the compiler can safely optimize all for
loops.

DoDi
rupesh0508@gmail.com

2007-09-24, 7:19 pm

Which compiler are you using, which platform, and what command line
options? A simple optimization flag (O or O2) should have taken the
invariant outside the loop.

-rupesh.

Alex Colvin

2007-09-24, 10:15 pm

>in the program, I refer to the variable test2a by the pointer
>for example


>for(i = 0; i <20; i++)
>{
> pTest2a->test1a[x].b[i] = i;
>}


>Although in the loop pTest2a->test1a[x] is fixed, it seems the
>generated codes calculate
>the address from pTest2a. If it is changed to the following:


Any code take the address of pTest2a, or make it external? Perhaps the
compiler is afraid that the loop assignment might also change pTest2a, in
which case it needs the addresses need to be recalculated.

Try making it a const, or at least static.
--
mac the naof
joggingsong@gmail.com

2007-09-25, 7:18 pm

On Sep 24, 5:07 pm, rupesh0...@gmail.com wrote:
> Which compiler are you using, which platform, and what command line
> options? A simple optimization flag (O or O2) should have taken the
> invariant outside the loop.
>
> -rupesh.


I set the optimization level O3, and the c compiler is from
freescale The platform is MSC8144, in which the core is SC3400.
Jogging

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com