[BPF] Fix a recursion bug in BPF Peephole ZEXT optimization
authorYonghong Song <yhs@fb.com>
Fri, 22 Nov 2019 08:20:10 +0000 (00:20 -0800)
committerYonghong Song <yhs@fb.com>
Fri, 22 Nov 2019 16:05:43 +0000 (08:05 -0800)
commit9e6aa81588505461e06c807c567b073224f1e817
tree79f163325131a733fd7749bfb06069f15babcbd9
parentaf8576ff9d5a66d30ce27176aff808b688c3ec85
[BPF] Fix a recursion bug in BPF Peephole ZEXT optimization

Commit a0841dfe8594 ("[BPF] Fix a bug in peephole optimization")
fixed a bug in peephole optimization. Recursion is introduced
to handle COPY and PHI instructions.

Unfortunately, multiple PHI instructions may form a cycle
and this will cause infinite recursion, eventual segfault.
For Commit a0841dfe8594, I indeed tried a few loops to ensure
that I won't see the recursion, but I did not try with
complex control flows, which, as demonstrated with the test case
in this patch, may introduce PHI cycles.

This patch fixed the issue by introducing a set to remember
visited PHI instructions. This way, cycles can be properly
detected and handled.

Differential Revision: https://reviews.llvm.org/D70586
llvm/lib/Target/BPF/BPFMIPeephole.cpp
llvm/test/CodeGen/BPF/32-bit-subreg-peephole-phi-3.ll [new file with mode: 0644]