Rockbox.org home
release
dev builds
extras
themes manual
wiki
device status forums
mailing lists
IRC bugs
patches
dev guide



Rockbox mail archive

Subject: Re: arrays & pointers

Re: arrays & pointers

From: Nix <nix_at_esperi.org.uk>
Date: 2005-01-29

[sorry for response delay, catching up with vast mail backlog]

On Mon, 03 Jan 2005, BlueChip mused:
> At 22:51 03/01/2005, you wrote:
>>Wow. What compiler are you using?
>
> afaik, the same version as the other RockBox devs ...In fact, when I
> created the RockBox DevKit v3 I recall including a more recent version
> of the sh-1 gcc (recalled due to a problem it caused when it
> over-optimised the grey-scale lib functions) ...I know that at least
> two core devs followed me in the upgrade (obviously one of those being
> Jens) - so I guess we are all having the same problems.
>
> And yes, I agree, the compiler SHOULD be doing that - but I have to
> date found several major bugs in gcc, so oversights such as optimising
> array access really doesn't surprise me.

I have trouble understanding what GCC could be doing wrong from this
description. While GCC does have internal understanding of arrays (even
arrays with variable bounds, for Ada), it decays ARRAY_REFs (i.e., array
dereferences) into pointer dereferences whenever necessary, and it does
this just late enough in optimization that it can consider the array as
an array for alias-optimization and `sizeof'/`typeof' purposes. (The C
standard treats arrays and pointers differently in these areas.)

So there should be little difference between its treatment of arrays and
its treatment of pointers in regard to generated code: the code
generators can't even tell which you used, and neither can most of the
optimizers.

(See gcc/c-typeck.c:default_function_array_conversion() for the code
which does the decaying, and its callers in that file for an indication
of when that happens.)

Could you describe the inefficiency you see in more detail? Some sample
C that demonstrates the inefficiency would be great. I've done some
simple tests here with GCC-3.3.1, 3.4.2, and 4.0-to-be, and can see no
difference between the code generated for array dereferences and pointer
dereferences, not even with -O0 (and we don't care about inefficient
code at -O0.)

I'd expect that unless you're making a habit of using `restrict', the
code you get with arrays will be slightly *better* than that you get
with plain pointers, because more aliasing information is available if
the array declaration is in scope rather than decayed into a pointer.
(You'd probably only see an improvement if you were manipulating
multiple arrays simultaneously.)

If there's a problem, I'll raise a bug in GCC bugzilla and see what I
can do to fix it (probably only in 3.4-to-be, because the changes in
this area between 3.4 and 4.0-to-be are drastic).

-- 
`Blish is clearly in love with language. Unfortunately,
 language dislikes him intensely.' --- Russ Allbery
_______________________________________________
http://cool.haxx.se/mailman/listinfo/rockbox
Received on Sat Jan 29 13:16:02 2005

Page was last modified "Jan 10 2012" The Rockbox Crew
aaa