[all-commits] [llvm/llvm-project] a0841d: [BPF] Fix a bug in peephole optimization

yonghong-song via All-commits all-commits at lists.llvm.org
Wed Nov 20 15:20:19 PST 2019


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: a0841dfe8594f189d79f3612fec019eda4824474
      https://github.com/llvm/llvm-project/commit/a0841dfe8594f189d79f3612fec019eda4824474
  Author: Yonghong Song <yhs at fb.com>
  Date:   2019-11-20 (Wed, 20 Nov 2019)

  Changed paths:
    M llvm/lib/Target/BPF/BPFMIPeephole.cpp
    M llvm/test/CodeGen/BPF/32-bit-subreg-cond-select.ll
    A llvm/test/CodeGen/BPF/32-bit-subreg-peephole-phi-1.ll
    A llvm/test/CodeGen/BPF/32-bit-subreg-peephole-phi-2.ll
    M llvm/test/CodeGen/BPF/32-bit-subreg-peephole.ll

  Log Message:
  -----------
  [BPF] Fix a bug in peephole optimization

One of current peephole optimiations is to remove SLL/SRL if
the sub register has been zero extended. This phase has two bugs
and one limitations.

First, for the physical subregister used in pseudo insn COPY
like below, it permits incorrect optimization.
    %0:gpr32 = COPY $w0
    ...
    %4:gpr = MOV_32_64 %0:gpr32
    %5:gpr = SLL_ri %4:gpr(tied-def 0), 32
    %6:gpr = SRA_ri %5:gpr(tied-def 0), 32
The $w0 could be from the return value of a previous function call
and its upper 32-bit value might contain some non-zero values.
The same applies to function arguments.

Second, the current code may permits removing SLL/SRA like below:
    %0:gpr32 = COPY $w0
    %1:gpr32 = COPY %0:gpr32
    ...
    %4:gpr = MOV_32_64 %1:gpr32
    %5:gpr = SLL_ri %4:gpr(tied-def 0), 32
    %6:gpr = SRA_ri %5:gpr(tied-def 0), 32
The reason is that it did not follow def-use chain to skip all
intermediate 32bit-to-32bit COPY instructions.

The current implementation is also very conservative for PHI
instructions. If any PHI insn component is another PHI or COPY insn,
it will just permit SLL/SRA.

This patch fixed the issue as follows:
 - During def/use chain traversal, if any physical register is read,
   SLL/SRA will be preserved as these physical registers are mostly
   from function return values or current function arguments.
 - Recursively visit all COPY and PHI instructions.




More information about the All-commits mailing list