This is an archive of the discontinued LLVM Phabricator instance.

[x86] Teach the EFLAGS copy lowering to handle much more complex control flow patterns including forks, merges, and even cyles.
ClosedPublic

Authored by chandlerc on Jul 12 2018, 1:11 AM.

Details

Summary

This tries to cover a reasonably comprehensive set of patterns that
still don't require PHIs or PHI placement. The coverage was inspired by
the amazing variety of patterns produced when copy EFLAGS and restoring
it to implement Speculative Load Hardening. Without this patch, we
simply cannot make such complex and invasive changes to x86 instruction
sequences due to EFLAGS.

I've added "just" one test, but this test covers many different
complexities and corner cases of this approach. It is actually more
comprehensive, as far as I can tell, than anything that I have
encountered in the wild on SLH.

Because the test is so complex, I've tried to give somewhat thorough
comments and an ASCII-art diagram of the control flows to make it a bit
easier to read and maintain long-term.

Diff Detail

Repository
rL LLVM

Event Timeline

chandlerc created this revision.Jul 12 2018, 1:11 AM
craig.topper added inline comments.Jul 12 2018, 1:51 PM
llvm/lib/Target/X86/X86FlagsCopyLowering.cpp
433 ↗(On Diff #155127)

Nit - this isn't lined up right with the line above.

llvm/test/CodeGen/X86/flags-copy-lowering.mir
777 ↗(On Diff #155127)

I don't think understand this sentence. I don't see any tests in bb.1 in the CHECKs.

chandlerc updated this revision to Diff 155304.Jul 12 2018, 5:00 PM
chandlerc marked 2 inline comments as done.

Address code review comments.

All done.

llvm/test/CodeGen/X86/flags-copy-lowering.mir
777 ↗(On Diff #155127)

Sorry, "tests" -> the SETcc instructions to capture the flag value into a register. Updated this comment text to make more sense.

Also, this comment wasn't actually accurate. Updated it to actually reflect what the CFG was testing. Sorry for the confusion.

craig.topper accepted this revision.Jul 12 2018, 7:40 PM

LGTM

llvm/test/CodeGen/X86/flags-copy-lowering.mir
917 ↗(On Diff #155304)

Immediately shouldn't be capitalized.

This revision is now accepted and ready to land.Jul 12 2018, 7:40 PM
echristo accepted this revision.Jul 12 2018, 9:19 PM

LGTM, a couple of inline requests. One small and one maybe less so.

-eric

llvm/lib/Target/X86/X86FlagsCopyLowering.cpp
535 ↗(On Diff #155304)

"is the same" -> "is the same block" please.

539 ↗(On Diff #155304)

The logic of these fused loop feels a bit convoluted, but I can't come up with an easy way to split it - can you?

chandlerc marked 2 inline comments as done.Jul 13 2018, 2:43 AM

Thanks all, fixed the issues as requested. One longer-term thing remains with a FIXME, but landing this for now.

llvm/lib/Target/X86/X86FlagsCopyLowering.cpp
539 ↗(On Diff #155304)

I totally agree. I tried a bunch of ways of writing this better and they were all terrible.

I think I know how to clean this up, but it is a really invasive change. This whole thing is just in desperate need of refactoring. If we pull all this apart into separate functions, I think the loop will become more clear and it might become easy to split them apart.

I'll queue that up for a follow-up patch as I don't really want to try and do it while making this functional change and I'd like to get the SLH patch this enables landed. But it definitely should be addressed. I've left a FIXME for now.

This revision was automatically updated to reflect the committed changes.