[all-commits] [llvm/llvm-project] 4db1cf: X86InstrInfo: Optimize more combinations of SUB+CMP

Matthias Braun via All-commits all-commits at lists.llvm.org
Thu Oct 28 10:33:27 PDT 2021


  Branch: refs/heads/optimize_compinstr
  Home:   https://github.com/llvm/llvm-project
  Commit: 4db1cf53740a8770b51ac1c03e8e9cc5e66eb83d
      https://github.com/llvm/llvm-project/commit/4db1cf53740a8770b51ac1c03e8e9cc5e66eb83d
  Author: Matthias Braun <matthiasb at fb.com>
  Date:   2021-10-28 (Thu, 28 Oct 2021)

  Changed paths:
    M llvm/lib/Target/X86/X86InstrInfo.cpp
    M llvm/lib/Target/X86/X86InstrInfo.h
    M llvm/test/CodeGen/X86/2007-02-16-BranchFold.ll
    M llvm/test/CodeGen/X86/optimize-compare.mir
    M llvm/test/CodeGen/X86/postalloc-coalescing.ll

  Log Message:
  -----------
  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


  Commit: 26da34b3238a018af23951b6a559f26aa78c9b6e
      https://github.com/llvm/llvm-project/commit/26da34b3238a018af23951b6a559f26aa78c9b6e
  Author: Matthias Braun <matthiasb at fb.com>
  Date:   2021-10-28 (Thu, 28 Oct 2021)

  Changed paths:
    M llvm/lib/Target/X86/X86InstrInfo.cpp
    M llvm/lib/Target/X86/X86InstrInfo.h
    M llvm/test/CodeGen/X86/optimize-compare.mir
    A llvm/test/CodeGen/X86/peep-test-5.ll
    M llvm/test/CodeGen/X86/use-cr-result-of-dom-icmp-st.ll

  Log Message:
  -----------
  X86InstrInfo: Support immediates that are +1/-1 different in optimizeCompareInstr

This extends `optimizeCompareInstr` to re-use previous comparison
results if the previous comparison was with an immediate that was 1
bigger or smaller. Example:

    CMP x, 13
    ...
    CMP x, 12   ; can be removed if we change the SETg
    SETg ...    ; x > 12  changed to `SETge` (x >= 13) removing CMP

Motivation: This often happens because SelectionDAG canonicalization
tends to add/subtract 1 often when optimizing for fallthrough blocks.
Example for `x > C` the fallthrough optimization switches true/false
blocks with `!(x > C)` --> `x <= C` and canonicalization turns this into
`x < C + 1`.

Differential Revision: https://reviews.llvm.org/D110867


Compare: https://github.com/llvm/llvm-project/compare/4db1cf53740a%5E...26da34b3238a


More information about the All-commits mailing list