mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-26 12:43:36 +01:00
1770a4b5f8
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
36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
//===-- BPF.h - Top-level interface for BPF representation ------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_TARGET_BPF_BPF_H
|
|
#define LLVM_LIB_TARGET_BPF_BPF_H
|
|
|
|
#include "MCTargetDesc/BPFMCTargetDesc.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
namespace llvm {
|
|
class BPFTargetMachine;
|
|
|
|
ModulePass *createBPFAbstractMemberAccess(BPFTargetMachine *TM);
|
|
|
|
FunctionPass *createBPFISelDag(BPFTargetMachine &TM);
|
|
FunctionPass *createBPFMISimplifyPatchablePass();
|
|
FunctionPass *createBPFMIPeepholePass();
|
|
FunctionPass *createBPFMIPeepholeTruncElimPass();
|
|
FunctionPass *createBPFMIPreEmitPeepholePass();
|
|
FunctionPass *createBPFMIPreEmitCheckingPass();
|
|
|
|
void initializeBPFAbstractMemberAccessPass(PassRegistry&);
|
|
void initializeBPFMISimplifyPatchablePass(PassRegistry&);
|
|
void initializeBPFMIPeepholePass(PassRegistry&);
|
|
void initializeBPFMIPeepholeTruncElimPass(PassRegistry&);
|
|
void initializeBPFMIPreEmitPeepholePass(PassRegistry&);
|
|
void initializeBPFMIPreEmitCheckingPass(PassRegistry&);
|
|
}
|
|
|
|
#endif
|