[PATCH] D45146: [x86] Introduce a pass to begin more systematically fixing PR36028 and similar issues.

Chandler Carruth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 1 02:21:11 PDT 2018


chandlerc created this revision.
chandlerc added reviewers: rnk, craig.topper.
Herald added subscribers: hiraditya, mgorny, mcrosier, dschuff, sanjoy.

The key idea is to lower COPY nodes populating EFLAGS by scanning the
uses of EFLAGS and introducing dedicated code to preserve the necessary
state in a GPR. In the vast majority of cases, these uses are cmovCC and
jCC instructions. For such cases, we can very easily save and restore
the necessary information by simply inserting a setCC into a GPR where
the original flags are live, and then testing that GPR directly to feed
the cmov or conditional branch.

However, things are a bit more tricky if arithmetic is using the flags.
This patch handles the vast majority of cases that seem to come up in
practice: adc, adcx, adox, rcl, and rcr; all without taking advantage of
partially preserved EFLAGS as LLVM doesn't currently model that at all.

There are a large number of operations that techinaclly observe EFLAGS
currently but shouldn't in this case -- they typically are using DF.
Currently, they will not be handled by this approach. However, I have
never seen this issue come up in practice. It is already pretty rare to
have these patterns come up in practical code with LLVM. I had to resort
to writing MIR tests to cover most of the logic in this pass already.
I suspect even with its current amount of coverage of arithmetic users
of EFLAGS it will be a significant improvement over the current use of
pushf/popf. It will also produce substantially faster code in most of
the common patterns.

The patch isn't complete yet. I at least want to add an optimization
which looks for opposite conditions and when present simply inverts
whatever test is being done.

I also have a MIR test file I've been using to test this stuff. I need
to add CHECK lines to it and add it to the patch before it can go in.

However, I wanted to get some early feedback on the approach and how I'm
doing this, so I'm sending the patch on out.

Lots of thanks to Reid who came up with several aspects of this
approach, and Craig who helped me work out a couple of things tripping
me up while working on this.


Repository:
  rL LLVM

https://reviews.llvm.org/D45146

Files:
  llvm/include/llvm/CodeGen/MachineBasicBlock.h
  llvm/lib/CodeGen/MachineBasicBlock.cpp
  llvm/lib/Target/X86/CMakeLists.txt
  llvm/lib/Target/X86/X86.h
  llvm/lib/Target/X86/X86FlagsCopyLowering.cpp
  llvm/lib/Target/X86/X86InstrArithmetic.td
  llvm/lib/Target/X86/X86InstrInfo.cpp
  llvm/lib/Target/X86/X86InstrInfo.h
  llvm/lib/Target/X86/X86TargetMachine.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45146.140575.patch
Type: text/x-patch
Size: 34089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180401/5c571ec7/attachment.bin>


More information about the llvm-commits mailing list