[X86] Reuse EFLAGS and form LOCKed ops when only user is SETCC.
authorAhmed Bougacha <ahmed.bougacha@gmail.com>
Thu, 7 Apr 2016 02:07:10 +0000 (02:07 +0000)
committerAhmed Bougacha <ahmed.bougacha@gmail.com>
Thu, 7 Apr 2016 02:07:10 +0000 (02:07 +0000)
commit1cf67fb9cb9354a296cd41cb6d19e56797606237
tree7414057152aa454a48e18b7a8da816be3c109bde
parent70bde5445b867f4cd0e7227f454890c39b0efdb6
[X86] Reuse EFLAGS and form LOCKed ops when only user is SETCC.

Re-apply r265450 which caused PR27245 and was reverted in r265559
because of a wrong generalization: the fetch_and_add->add_and_fetch
combine only works in specific, but pretty common, cases:
  (icmp slt x, 0) -> (icmp sle (add x, 1), 0)
  (icmp sge x, 0) -> (icmp sgt (add x, 1), 0)
  (icmp sle x, 0) -> (icmp slt (sub x, 1), 0)
  (icmp sgt x, 0) -> (icmp sge (sub x, 1), 0)

Original Message:

We only generate LOCKed versions of add/sub when the result is unused.
It often happens that the result is used, but only by a comparison. We
can optimize those out by reusing EFLAGS, which lets us use the proper
instructions, instead of having to fallback to LXADD.

Instead of doing this as an MI peephole (as we do for the other
non-LOCKed (really, non-MR) forms), do it in ISel. It becomes quite
tricky later.

This also makes it eventually possible to stop expanding and/or/xor
if the only user is an icmp (also see D18141).

This uses the LOCK ISD opcodes added by r262244.

Differential Revision: http://reviews.llvm.org/D17633

llvm-svn: 265636
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/atomic-eflags-reuse.ll