llvm-project/llvm/test/CodeGen/BPF
Jiong Wang ec51851026 bpf: fix wrong truncation elimination when there is back-edge/loop
Currently, BPF backend is doing truncation elimination. If one truncation
is performed on a value defined by narrow loads, then it could be redundant
given BPF loads zero extend the destination register implicitly.

When the definition of the truncated value is a merging value (PHI node)
that could come from different code paths, then checks need to be done on
all possible code paths.

Above described optimization was introduced as r306685, however it doesn't
work when there is back-edge, for example when loop is used inside BPF
code.

For example for the following code, a zero-extended value should be stored
into b[i], but the "and reg, 0xffff" is wrongly eliminated which then
generates corrupted data.

void cal1(unsigned short *a, unsigned long *b, unsigned int k)
{
  unsigned short e;

  e = *a;
  for (unsigned int i = 0; i < k; i++) {
    b[i] = e;
    e = ~e;
  }
}

The reason is r306685 was trying to do the PHI node checks inside isel
DAG2DAG phase, and the checks are done on MachineInstr. This is actually
wrong, because MachineInstr is being built during isel phase and the
associated information is not completed yet. A quick search shows none
target other than BPF is access MachineInstr info during isel phase.

For an PHI node, when you reached it during isel phase, it may have all
predecessors linked, but not successors. It seems successors are linked to
PHI node only when doing SelectionDAGISel::FinishBasicBlock and this
happens later than PreprocessISelDAG hook.

Previously, BPF program doesn't allow loop, there is probably the reason
why this bug was not exposed.

This patch therefore fixes the bug by the following approach:
 - The existing truncation elimination code and the associated
   "load_to_vreg_" records are removed.
 - Instead, implement truncation elimination using MachineSSA pass, this
   is where all information are built, and keep the pass together with other
   similar peephole optimizations inside BPFMIPeephole.cpp. Redundant move
   elimination logic is updated accordingly.
 - Unit testcase included + no compilation errors for kernel BPF selftest.

Patch Review
===
Patch was sent to and reviewed by BPF community at:

  https://lore.kernel.org/bpf

Reported-by: David Beckett <david.beckett@netronome.com>
Reviewed-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Jiong Wang <jiong.wang@netronome.com>
llvm-svn: 375007
2019-10-16 15:27:59 +00:00
..
BTF [BPF] Remove relocation for patchable externs 2019-10-10 15:33:09 +00:00
CORE [BPF] Remove relocation for patchable externs 2019-10-10 15:33:09 +00:00
32-bit-subreg-alu.ll
32-bit-subreg-cond-select.ll
32-bit-subreg-load-store.ll
32-bit-subreg-peephole.ll
alu8.ll
atomics.ll
basictest.ll
byval.ll
cc_args.ll
cc_args_be.ll
cc_ret.ll
cmp.ll
dwarfdump.ll
elf-symbol-information.ll
ex1.ll
fi_ri.ll
inline_asm.ll
inlineasm-output-template.ll [AsmPrinter] refactor to support %c w/ GlobalAddress' 2019-04-26 18:45:04 +00:00
intrinsics.ll
lit.local.cfg
load.ll
loops.ll
many_args1.ll
many_args2.ll
mem_offset.ll
mem_offset_be.ll
memcpy-expand-in-order.ll
objdump_atomics.ll
objdump_cond_op.ll
objdump_cond_op_2.ll
objdump_imm_hex.ll
objdump_intrinsics.ll
objdump_static_var.ll [BPF] Don't fail for static variables 2019-02-27 05:36:15 +00:00
objdump_trivial.ll
objdump_two_funcs.ll [BPF] Fix bpf llvm-objdump issues. 2019-08-17 22:12:00 +00:00
reloc-btf-2.ll [BPF] Support for compile once and run everywhere 2019-07-09 15:28:41 +00:00
reloc-btf.ll [llvm-objdump] - Print relocation record in a GNU format. 2019-05-07 13:14:18 +00:00
reloc.ll [llvm-objdump] - Print relocation record in a GNU format. 2019-05-07 13:14:18 +00:00
remove_truncate_1.ll
remove_truncate_2.ll
remove_truncate_3.ll
remove_truncate_4.ll
remove_truncate_5.ll
remove_truncate_6.ll bpf: fix wrong truncation elimination when there is back-edge/loop 2019-10-16 15:27:59 +00:00
rodata_1.ll
rodata_2.ll
rodata_3.ll
rodata_4.ll
sanity.ll
sdiv_error.ll
select_ri.ll
setcc.ll
shifts.ll
sockex2.ll
struct_ret1.ll
struct_ret2.ll
undef.ll [DAGCombiner] If a TokenFactor would be merged into its user, consider the user later. 2019-03-13 17:07:09 +00:00
vararg1.ll
warn-call.ll
warn-stack.ll
xadd.ll bpf: enable sub-register code-gen for XADD 2019-02-28 19:21:28 +00:00
xadd_legal.ll bpf: enable sub-register code-gen for XADD 2019-02-28 19:21:28 +00:00