X86InstrInfo: Optimize more combinations of SUB+CMP
authorMatthias Braun <matthiasb@fb.com>
Mon, 27 Sep 2021 22:21:15 +0000 (15:21 -0700)
committerMatthias Braun <matthiasb@fb.com>
Thu, 28 Oct 2021 17:33:56 +0000 (10:33 -0700)
commit97a1570d8c31dc3bff12dd77b1ee824e1872bb69
tree69e59b4d1ace57722f49a7cb766ef882b0fa7040
parente50f02ba7ed846f944a369e6738174a5eabfbdcc
X86InstrInfo: Optimize more combinations of SUB+CMP

`X86InstrInfo::optimizeCompareInstr` would only optimize a `SUB`
followed by a `CMP` in `isRedundantFlagInstr`. This extends the code to
also look for other combinations like `CMP`+`CMP`, `TEST`+`TEST`, `SUB
x,0`+`TEST`.

- Change `isRedundantFlagInstr` to run `analyzeCompareInstr` on the
  candidate instruction and compare the results. This normalizes things
  and gives consistent results for various comparisons (`CMP x, y`,
  `SUB x, y`) and immediate cases (`TEST x, x`, `SUB x, 0`,
  `CMP x, 0`...).
- Turn `isRedundantFlagInstr` into a member function so it can call
  `analyzeCompare`.  - We now also check `isRedundantFlagInstr` even if
  `IsCmpZero` is true, since we now have cases like `TEST`+`TEST`.

Differential Revision: https://reviews.llvm.org/D110865
llvm/lib/Target/X86/X86InstrInfo.cpp
llvm/lib/Target/X86/X86InstrInfo.h
llvm/test/CodeGen/X86/2007-02-16-BranchFold.ll
llvm/test/CodeGen/X86/optimize-compare.mir
llvm/test/CodeGen/X86/postalloc-coalescing.ll