This is an archive of the discontinued LLVM Phabricator instance.

[X86] When optimizing for size, use POP for small post-call stack clean-up
ClosedPublic

Authored by mkuper on Aug 4 2015, 1:17 AM.

Details

Summary

When optimizing for size, replace "addl $4, %esp" and "addl $8, %esp" following a call by one or two pops, respectively.

This patch doesn't try to do it in general, but only when the stack adjustment immediately follows a call - which is the most common case.
That allows taking a short-cut when trying to find a free register to pop into, instead of a full-blown liveness check. If the adjustment immediately follows a call, then every register the call clobbers but doesn't define should be dead at that point, and can be used.

Diff Detail

Repository
rL LLVM

Event Timeline

mkuper updated this revision to Diff 31301.Aug 4 2015, 1:17 AM
mkuper retitled this revision from to [X86] When optimizing for size, use POP for small post-call stack clean-up.
mkuper updated this object.
mkuper added reviewers: rnk, majnemer.
mkuper added a subscriber: llvm-commits.
majnemer added inline comments.Aug 4 2015, 1:28 AM
lib/Target/X86/X86FrameLowering.cpp
1857–1858 ↗(On Diff #31301)

Is PopSize interestingly different from SlotSize?

1875 ↗(On Diff #31301)

SmallVector seems like a little overkill, would an array of type unsigned [2] make more sense?

1880–1881 ↗(On Diff #31301)

Can this be a range-based for loop?

1908–1910 ↗(On Diff #31301)

Why bother looking for two free registers if we can count on being able to reuse the first one? Does it cause a false dependency or something? More curiosity than anything else.

mkuper added a comment.EditedAug 4 2015, 1:42 AM

Thanks, David!

Do you see any issues with the way I'm choosing the registers?
I need a sanity check on the "if it's clobbered and not imp-def'ed, it's free" thing.

lib/Target/X86/X86FrameLowering.cpp
1857–1858 ↗(On Diff #31301)

No, should be the same. :)

1875 ↗(On Diff #31301)

Force of habit more than anything else, will change to an array if you prefer that.

1880–1881 ↗(On Diff #31301)

Yes.

1908–1910 ↗(On Diff #31301)

I'm afraid of a false dependency, yes. I haven't actually measured it.

mkuper added a subscriber: mbodart.Aug 4 2015, 1:43 AM
mkuper updated this revision to Diff 31311.Aug 4 2015, 2:23 AM

Updated with David's comments.

Honestly, I liked the SmallVector version better. Are you ok with reverting to that?

mkuper added a subscriber: zansari.Aug 4 2015, 8:15 AM
rnk accepted this revision.Aug 10 2015, 8:43 AM
rnk edited edge metadata.

lgtm

Updated with David's comments.

Honestly, I liked the SmallVector version better. Are you ok with reverting to that?

I'd rather not. If the STL accessors make things more readable, how about std::array?

test/CodeGen/X86/pop-stack-cleanup.ll
4 ↗(On Diff #31311)

Add a test that returns i64 in EAX:EDX and see what we get.

This revision is now accepted and ready to land.Aug 10 2015, 8:43 AM

std::array doesn't really help readability, I think.
Will keep it as is, then.

Also, on second thought, I'd rather be safe and only enable this for minsize, not optsize, for now.

This revision was automatically updated to reflect the committed changes.