[PATCH] D46662: [X86] condition branches folding for three-way conditional codes

Rong Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 9 14:20:49 PDT 2018


xur created this revision.
xur added reviewers: davidxl, craig.topper.
Herald added a subscriber: mgorny.

This file defines a pass that optimizes condition branches on x86 by taking advantage of the three-way conditional code generated by compare instructions.

Currently, it tries to hoisting EQ and NE conditional branch to a dominant conditional branch condition where the same EQ/NE conditional code is computed. An example:

  `bb_0:
    cmp %0, 19
    jg bb_1
    jmp bb_2
  bb_1:
    cmp %0, 40
    jg bb_3
    jmp bb_4
  bb_4:
    cmp %0, 20
    je bb_5
    jmp bb_6

Here we could combine the two compares in bb_0 and bb_4 and have the
following code:

  bb_0:
    cmp %0, 20
    jg bb_1
    jl bb_2
    jmp bb_5
  bb_1:
    cmp %0, 40
    jg bb_3
    jmp bb_6`

For the case of %0 == 20 (bb_5), we eliminate two jumps, and the control height for bb_6 is also reduced. bb_4 is gone after the optimization.

This optimization is motivated by the branch pattern generated by the switch lowering: we always have pivot-1 compare for the inner nodes and we do a pivot compare again the leaf (like above pattern).

My test on Haswell shows that this optimization improves the tight nest loop that consisted of a evenly distributed switch cases by 8% to 10%.

Thanks,

-Rong


https://reviews.llvm.org/D46662

Files:
  lib/Target/X86/CMakeLists.txt
  lib/Target/X86/X86.h
  lib/Target/X86/X86CondBrFolding.cpp
  lib/Target/X86/X86TargetMachine.cpp
  test/CodeGen/X86/2007-02-16-BranchFold.ll
  test/CodeGen/X86/O3-pipeline.ll
  test/CodeGen/X86/condbr_if.ll
  test/CodeGen/X86/condbr_switch.ll
  test/CodeGen/X86/switch-bt.ll
  test/CodeGen/X86/switch-density.ll
  test/CodeGen/X86/switch.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46662.146001.patch
Type: text/x-patch
Size: 32361 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180509/b2dc382a/attachment-0001.bin>


More information about the llvm-commits mailing list