svn commit: r242835 - head/contrib/llvm/lib/Target/X86

Konstantin Belousov kostikbel at gmail.com
Sun Nov 11 18:26:28 UTC 2012


On Mon, Nov 12, 2012 at 02:01:47AM +1100, Bruce Evans wrote:
> On Sun, 11 Nov 2012, Dimitry Andric wrote:
> 
> > On 2012-11-11 12:53, Bruce Evans wrote:
> >> 
> >> I'm not sure if either of us knows exactly what this does, but I like
> >> this change.  clang does stack alignment correctly, so it doesn't need
> >> the gcc pessimization of aligning in the caller.  clang aligns in the
> 
> Apparently we did know.
> 
> >> ...
> >> alignment.  gcc's alignment in callers doesn't even work, because gcc
> >> assumes that it is enough and never does it in callees even when it
> >> is necessary:
> >> 
> >>   	auto int32_t foo[8] __aligned[32];
> >> 
> >> gcc fails to do the necessary alignment.  clang does it.
> >> 
> >>   	auto int32_t foo[8] __aligned[16];
> >> 
> >> Both gcc and (hopefully only without the above fix) clang fail to do the
> >> necessary alignment.
> >
> > It works just fine now with clang.  For the first example, I get:
> >
> >        pushl   %ebp
> >        movl    %esp, %ebp
> >        andl    $-32, %esp
> >
> > as prolog, and for the second:
> >
> >        pushl   %ebp
> >        movl    %esp, %ebp
> >        andl    $-16, %esp
> 
> Good.
> 
> The andl executes very fast.  Perhaps not as fast as subl on %esp,
> because subl is normal so more likely to be optimized (they nominally
> have the same speeds, but %esp is magic).  Unfortunately, it seems to
> be impossible to both align the stack and reserve some space on it in
> 1 instruction -- the andl might not reserve any.
I think the biggest hit from the andl instruction is due to the spoiling
of the stack engine presented on all Intel processors starting from
Pentium Pro. Most likely, predictor cannot handle such change of %esp
without throwing the hands up.

> 
> >> special alignment in main(), but assumes that crtso did it.  clang also
> >> doesn't support -mpreferred-stack-boundary, so it is incompatible with
> >> nonstandard ABI's like the one given by always using
> >> -mpreferred-stack-boundary=32.
> >
> > Apparently upstream never saw the need for this option.  I strongly
> > doubt it is used very often outside FreeBSD...
> 
> It is most useful for working around gcc's behaviour.  Hmm, the kernel
> and boot blocks uses -mpreferred-stack-boundary=2 on i386 to save space.
> This must have been turned off for clang, so the versions that did
> 16-bit alignment must have come close to blowing the kernel stack.  In
> boot blocks, I think there is plenty of stack but alignment wastes code
> space.
> 
> I'd like to try -mpreferred-stack-boundary=3 in amd64 kernels, but this
> is an i386-only option.
> 
> >> Yes, we need clang/our libraries to handle different alignments and
> >> not assume that callers do more than the ABI requires and pessimize
> >> on behalf of the libraries.  Outside of libraries, the problem is small
> >> provided -mpreferred-stack-boundary works, since you can compile
> >> everything with the same -mpreferred-stack-boundary.
> >
> > As far as I can see, with the 4 byte stack alignment that has been the
> > default, and is now also clang's default, our libraries should handle
> > any "incoming" stack alignment of >= 4 bytes.  I don't think anybody
> > uses lower stack alignment, except maybe for the special case of boot
> > loaders, or extremely size-optimized code.
The i386 ABI requires 4 byte alignment, but does not require any larger.

I do not see how can we even pretend to support stable ABI while allowing
such gratitious changes.
> 
> -Os could reasonably generate lower stack alignment, but that rarely
> saves space.  -Os generally doesn't give enough control over alignment
> for space-time tradeoffs.  I made some minor changes in FreeBSD's gcc
> to make it _not_ give misaligned 32-bit variables (?) since -Os should
> only optimize for space if it doesn't cost much time for small savings
> in space.  But if you only care about space, it would be useful to
> minimize the alignment of everything.
> 
> Bruce
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/svn-src-head/attachments/20121111/40e85f71/attachment.sig>


More information about the svn-src-head mailing list