This is an archive of the discontinued LLVM Phabricator instance.

x86: check hasOpaqueSPAdjustment in canRealignStack
ClosedPublic

Authored by jfb on Jul 20 2015, 5:10 PM.

Details

Summary

@rnk pointed out in [1] that x86's canRealignStack logic should match that in CantUseSP from hasBasePointer.

[1]: http://reviews.llvm.org/D11160?id=29713#inline-89350

Diff Detail

Repository
rL LLVM

Event Timeline

jfb updated this revision to Diff 30218.Jul 20 2015, 5:10 PM
jfb retitled this revision from to x86: check hasOpaqueSPAdjustment in canRealignStack.
jfb updated this object.
jfb added a reviewer: rnk.
jfb added subscribers: llvm-commits, rnk.
jfb added a comment.Jul 20 2015, 5:12 PM

I'm not sure how to test this. I tried something similar to test/CodeGen/X86/inline-asm-stack-realign3.ll but without success:

; RUN: llc -march=x86 -no-integrated-as < %s | FileCheck %s

declare void @bar(i32* %junk)

define i32 @foo(i1 %cond) {
entry:
  %r = alloca i32, align 128
  store i32 -1, i32* %r, align 128
  br i1 %cond, label %doit, label %skip

doit:
  call void asm sideeffect "xor %esi, %esi\0A\09mov %esi, $0", "=*m,~{esi},~{flags}"(i32* %r)
  %junk = alloca i32
  call void @bar(i32* %junk)
  br label %skip

skip:
  %0 = load i32, i32* %r, align 128
  ret i32 %0
}

This generates the following code:

_foo:                                   # @foo
# BB#0:                                 # %entry
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%esi
	andl	$-128, %esp
	subl	$128, %esp
	movl	%esp, %esi
	movl	$-1, (%esi)
	testb	$1, 8(%ebp)
	je	LBB0_2
# BB#1:                                 # %doit
	#APP
	xorl	%esi, %esi
	movl	%esi, (%esi)
	#NO_APP
	movl	$4, %eax
	calll	__chkstk
	movl	%esp, %eax
	pushl	%eax
	calll	_bar
	addl	$4, %esp
LBB0_2:                                 # %skip
	movl	(%esi), %eax
	leal	-4(%ebp), %esp
	popl	%esi
	popl	%ebp
	retl

Which seems wrong, but is the same as before this patch, so I'm assuming it doesn't trigger my change. Any ideas on how to trigger the change?

rnk edited edge metadata.Jul 22 2015, 9:45 AM

Right, your test case is essentially
https://llvm.org/bugs/show_bug.cgi?id=16830.

To get into a case where canRealignStack matters, I think you need:

  • No dynamic alloca (otherwise we short-circuit out due to

hasVarSizedObjects())

  • No highly-aligned alloca (otherwise we commit to stack realignment early)
  • A highly-aligned SSA value (a vector) that must be spilled during

register allocation (is live across a call)

If that doesn't do it, then I think this code is dynamically dead, at least
for now. =/

My understanding is that the original purpose of canRealignStack was to
figure out, part way through register allocation, if the aligned or
unaligned vector load and store instructions should be used to spill to the
stack. In some circumstances, ESI may have already been reserved by inline
asm, a custom calling convention, or something else, so we needed this
check. I think Anton added it as part of
https://llvm.org/bugs/show_bug.cgi?id=951.

Then, at some point, someone decided that realigning the stack was too
expensive, since even if you don't have dynamic allocas, it burns EBP,
wasting a precious x86 GPR. I can't find the PRs or revisions for this, but
I think Chad Rosier tweaked the x86 backend so that it doesn't try to
realign the stack anymore. Instead it just uses the unaligned vector loads
and stores.

So, it sounds like this change is untestable, but I feel like we should
throw it in there, because this condition should really be identical to
CantUseSP in hasBasePointer.

jfb added a comment.Jul 22 2015, 10:13 AM

Ah that makes sense. I'm OK with no test if you are, considering I couldn't generate one!

jfb added a subscriber: mcrosier.Jul 30 2015, 9:22 AM

@rnk: ping :-)

I see @mcrosier added reviewers @grosbach, @stoklund. Any particular worry?

rnk accepted this revision.Jul 30 2015, 9:47 AM
rnk edited edge metadata.

yep, lgtm

This revision is now accepted and ready to land.Jul 30 2015, 9:47 AM
This revision was automatically updated to reflect the committed changes.

@jfb - no worries; I added Jim and Jakob because I knew they were somewhat familiar with this code (at some point in time many years ago).