-malign-double oddities

Andy Polyakov appro@fy.chalmers.se
Wed Sep 16 14:58:00 GMT 1998


Hi there!

First of all I'd like to express my gratefulness and point out that you
guys do really terrific job. Thanks a lot!!! But there is a small
problem... Well, it's probably inherited all the way from gcc 2.7.2 as
far as I can tell and isn't really your "fault." But since you're the
ones who do terrific job I turn to you with this problem, not to
somebody else:-)

As you do know access to misaligned double precision variables might
have notable performance impact on Intel Pentium processors. Now problem
is that -malign-double option that is supposed to align all doubles to
8*N addresses doesn't seem to assure alignment of local variables under
certain circumstances. Consider following program:

	int func (int i)
	{ int k; double d; return (int)&d; }

	main ()
	{ int k; double d;
	   printf ("%saligned\n",(int)&d & 7 ? "mis" : "");
	   printf ("%saligned\n",func(1) & 7 ? "mis" : "");
	}

If compiled without any flags at all, 'd' in both main() and func()
appeared to be
misaligned. So I used -malign-double because as I was dying to see them
both appropriately aligned as this is a piece of real-life
very-high-performance application:-) But seriously speaking I think it's
important. I do realize that the whole mumbo-jumbo depends on initial
seed value for the stack pointer, but let's assume that stack is always
seeded in the very same way, namely first argument to main() is always
aligned to double. At least it's the case for Linux libc. So once again,
I expect 'd' in both main() and func() to be aligned.

The program compiled with -malign-double exhibits perfect alignment in
main(), but not in func()! Latter is caused by the fact that stack isn't
seeded to func() aligned double-vise. Compiler should probably have
spilled a word on top of the stack before passing the only argument to
func() in order to assure that it (first argument) is aligned and thus
provide *controllable* environment for func().

Compiled with -malign-double and -fomit-frame-pointer the program
reports that 'd' in main() isn't aligned! How come? It looks like when
allocating frame for local variables compiler doesn't take into
consideration the fact that stack pointer is only *one* word "above"
first argument, i.e. double-vise misaligned. Compare it to the case when
frame pointer isn't omitted. In this case at the moment of frame
allocation stack pointer is *two* words "above" first argument, i.e.
double-vise aligned.

Summary.

When generating code for Intel with -malign-double option compiler
should:

- pad argument list in order to assure double-vise alignment of the
*first* argument passed to subroutine being called (alignment of second,
third, etc. arguments is concern of application programmer, not
compiler);

- take into consideration the fact that stack frame is "sunk" one word
when frame pointer is omitted;

Cheers. Andy.



More information about the Gcc-bugs mailing list