Path: blob/main/contrib/llvm-project/llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp
35294 views
//===----- BPFMISimplifyPatchable.cpp - MI Simplify Patchable Insts -------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass targets a subset of instructions like below9// ld_imm64 r1, @global10// ldd r2, r1, 011// add r3, struct_base_reg, r212//13// Here @global should represent an AMA (abstruct member access).14// Such an access is subject to bpf load time patching. After this pass, the15// code becomes16// ld_imm64 r1, @global17// add r3, struct_base_reg, r118//19// Eventually, at BTF output stage, a relocation record will be generated20// for ld_imm64 which should be replaced later by bpf loader:21// r1 = <calculated field_info>22// add r3, struct_base_reg, r123//24// This pass also removes the intermediate load generated in IR pass for25// __builtin_btf_type_id() intrinsic.26//27//===----------------------------------------------------------------------===//2829#include "BPF.h"30#include "BPFCORE.h"31#include "BPFInstrInfo.h"32#include "BPFTargetMachine.h"33#include "llvm/CodeGen/MachineFunctionPass.h"34#include "llvm/CodeGen/MachineInstrBuilder.h"35#include "llvm/CodeGen/MachineRegisterInfo.h"36#include "llvm/IR/GlobalVariable.h"37#include "llvm/Support/Debug.h"38#include <set>3940using namespace llvm;4142#define DEBUG_TYPE "bpf-mi-simplify-patchable"4344namespace {4546struct BPFMISimplifyPatchable : public MachineFunctionPass {4748static char ID;49const BPFInstrInfo *TII;50MachineFunction *MF;5152BPFMISimplifyPatchable() : MachineFunctionPass(ID) {53initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());54}5556private:57std::set<MachineInstr *> SkipInsts;5859// Initialize class variables.60void initialize(MachineFunction &MFParm);6162bool isLoadInst(unsigned Opcode);63bool removeLD();64void processCandidate(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,65MachineInstr &MI, Register &SrcReg, Register &DstReg,66const GlobalValue *GVal, bool IsAma);67void processDstReg(MachineRegisterInfo *MRI, Register &DstReg,68Register &SrcReg, const GlobalValue *GVal,69bool doSrcRegProp, bool IsAma);70void processInst(MachineRegisterInfo *MRI, MachineInstr *Inst,71MachineOperand *RelocOp, const GlobalValue *GVal);72void checkADDrr(MachineRegisterInfo *MRI, MachineOperand *RelocOp,73const GlobalValue *GVal);74void checkShift(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,75MachineOperand *RelocOp, const GlobalValue *GVal,76unsigned Opcode);7778public:79// Main entry point for this pass.80bool runOnMachineFunction(MachineFunction &MF) override {81if (skipFunction(MF.getFunction()))82return false;8384initialize(MF);85return removeLD();86}87};8889// Initialize class variables.90void BPFMISimplifyPatchable::initialize(MachineFunction &MFParm) {91MF = &MFParm;92TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();93LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");94}9596static bool isST(unsigned Opcode) {97return Opcode == BPF::STB_imm || Opcode == BPF::STH_imm ||98Opcode == BPF::STW_imm || Opcode == BPF::STD_imm;99}100101static bool isSTX32(unsigned Opcode) {102return Opcode == BPF::STB32 || Opcode == BPF::STH32 || Opcode == BPF::STW32;103}104105static bool isSTX64(unsigned Opcode) {106return Opcode == BPF::STB || Opcode == BPF::STH || Opcode == BPF::STW ||107Opcode == BPF::STD;108}109110static bool isLDX32(unsigned Opcode) {111return Opcode == BPF::LDB32 || Opcode == BPF::LDH32 || Opcode == BPF::LDW32;112}113114static bool isLDX64(unsigned Opcode) {115return Opcode == BPF::LDB || Opcode == BPF::LDH || Opcode == BPF::LDW ||116Opcode == BPF::LDD;117}118119static bool isLDSX(unsigned Opcode) {120return Opcode == BPF::LDBSX || Opcode == BPF::LDHSX || Opcode == BPF::LDWSX;121}122123bool BPFMISimplifyPatchable::isLoadInst(unsigned Opcode) {124return isLDX32(Opcode) || isLDX64(Opcode) || isLDSX(Opcode);125}126127void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,128MachineOperand *RelocOp, const GlobalValue *GVal) {129const MachineInstr *Inst = RelocOp->getParent();130const MachineOperand *Op1 = &Inst->getOperand(1);131const MachineOperand *Op2 = &Inst->getOperand(2);132const MachineOperand *BaseOp = (RelocOp == Op1) ? Op2 : Op1;133134// Go through all uses of %1 as in %1 = ADD_rr %2, %3135const MachineOperand Op0 = Inst->getOperand(0);136for (MachineOperand &MO :137llvm::make_early_inc_range(MRI->use_operands(Op0.getReg()))) {138// The candidate needs to have a unique definition.139if (!MRI->getUniqueVRegDef(MO.getReg()))140continue;141142MachineInstr *DefInst = MO.getParent();143unsigned Opcode = DefInst->getOpcode();144unsigned COREOp;145if (isLDX64(Opcode) || isLDSX(Opcode))146COREOp = BPF::CORE_LD64;147else if (isLDX32(Opcode))148COREOp = BPF::CORE_LD32;149else if (isSTX64(Opcode) || isSTX32(Opcode) || isST(Opcode))150COREOp = BPF::CORE_ST;151else152continue;153154// It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.155const MachineOperand &ImmOp = DefInst->getOperand(2);156if (!ImmOp.isImm() || ImmOp.getImm() != 0)157continue;158159// Reject the form:160// %1 = ADD_rr %2, %3161// *(type *)(%2 + 0) = %1162if (isSTX64(Opcode) || isSTX32(Opcode)) {163const MachineOperand &Opnd = DefInst->getOperand(0);164if (Opnd.isReg() && Opnd.getReg() == MO.getReg())165continue;166}167168BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))169.add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)170.addGlobalAddress(GVal);171DefInst->eraseFromParent();172}173}174175void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo *MRI,176MachineBasicBlock &MBB, MachineOperand *RelocOp, const GlobalValue *GVal,177unsigned Opcode) {178// Relocation operand should be the operand #2.179MachineInstr *Inst = RelocOp->getParent();180if (RelocOp != &Inst->getOperand(2))181return;182183BuildMI(MBB, *Inst, Inst->getDebugLoc(), TII->get(BPF::CORE_SHIFT))184.add(Inst->getOperand(0)).addImm(Opcode)185.add(Inst->getOperand(1)).addGlobalAddress(GVal);186Inst->eraseFromParent();187}188189void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo *MRI,190MachineBasicBlock &MBB, MachineInstr &MI, Register &SrcReg,191Register &DstReg, const GlobalValue *GVal, bool IsAma) {192if (MRI->getRegClass(DstReg) == &BPF::GPR32RegClass) {193if (IsAma) {194// We can optimize such a pattern:195// %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"196// %2:gpr32 = LDW32 %1:gpr, 0197// %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32198// %4:gpr = ADD_rr %0:gpr, %3:gpr199// or similar patterns below for non-alu32 case.200auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();201decltype(End) NextI;202for (auto I = Begin; I != End; I = NextI) {203NextI = std::next(I);204if (!MRI->getUniqueVRegDef(I->getReg()))205continue;206207unsigned Opcode = I->getParent()->getOpcode();208if (Opcode == BPF::SUBREG_TO_REG) {209Register TmpReg = I->getParent()->getOperand(0).getReg();210processDstReg(MRI, TmpReg, DstReg, GVal, false, IsAma);211}212}213}214215BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::COPY), DstReg)216.addReg(SrcReg, 0, BPF::sub_32);217return;218}219220// All uses of DstReg replaced by SrcReg221processDstReg(MRI, DstReg, SrcReg, GVal, true, IsAma);222}223224void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo *MRI,225Register &DstReg, Register &SrcReg, const GlobalValue *GVal,226bool doSrcRegProp, bool IsAma) {227auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();228decltype(End) NextI;229for (auto I = Begin; I != End; I = NextI) {230NextI = std::next(I);231if (doSrcRegProp) {232// In situations like below it is not known if usage is a kill233// after setReg():234//235// .-> %2:gpr = LD_imm64 @"llvm.t:0:0$0:0"236// |237// |`----------------.238// | %3:gpr = LDD %2:gpr, 0239// | %4:gpr = ADD_rr %0:gpr(tied-def 0), killed %3:gpr <--- (1)240// | %5:gpr = LDD killed %4:gpr, 0 ^^^^^^^^^^^^^241// | STD killed %5:gpr, %1:gpr, 0 this is I242// `----------------.243// %6:gpr = LDD %2:gpr, 0244// %7:gpr = ADD_rr %0:gpr(tied-def 0), killed %6:gpr <--- (2)245// %8:gpr = LDD killed %7:gpr, 0 ^^^^^^^^^^^^^246// STD killed %8:gpr, %1:gpr, 0 this is I247//248// Instructions (1) and (2) would be updated by setReg() to:249//250// ADD_rr %0:gpr(tied-def 0), %2:gpr251//252// %2:gpr is not killed at (1), so it is necessary to remove kill flag253// from I.254I->setReg(SrcReg);255I->setIsKill(false);256}257258// The candidate needs to have a unique definition.259if (IsAma && MRI->getUniqueVRegDef(I->getReg()))260processInst(MRI, I->getParent(), &*I, GVal);261}262}263264// Check to see whether we could do some optimization265// to attach relocation to downstream dependent instructions.266// Two kinds of patterns are recognized below:267// Pattern 1:268// %1 = LD_imm64 @"llvm.b:0:4$0:1" <== patch_imm = 4269// %2 = LDD %1, 0 <== this insn will be removed270// %3 = ADD_rr %0, %2271// %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0272// The `%4 = ...` will be transformed to273// CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")274// and later on, BTF emit phase will translate to275// %4 = LDW[32] %0, 4 STW[32] %4, %0, 4276// and attach a relocation to it.277// Pattern 2:278// %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5279// %16 = LDD %15, 0 <== this insn will be removed280// %17 = SRA_rr %14, %16281// The `%17 = ...` will be transformed to282// %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")283// and later on, BTF emit phase will translate to284// %r4 = SRA_ri %r4, 63285void BPFMISimplifyPatchable::processInst(MachineRegisterInfo *MRI,286MachineInstr *Inst, MachineOperand *RelocOp, const GlobalValue *GVal) {287unsigned Opcode = Inst->getOpcode();288if (isLoadInst(Opcode)) {289SkipInsts.insert(Inst);290return;291}292293if (Opcode == BPF::ADD_rr)294checkADDrr(MRI, RelocOp, GVal);295else if (Opcode == BPF::SLL_rr)296checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SLL_ri);297else if (Opcode == BPF::SRA_rr)298checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRA_ri);299else if (Opcode == BPF::SRL_rr)300checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRL_ri);301}302303/// Remove unneeded Load instructions.304bool BPFMISimplifyPatchable::removeLD() {305MachineRegisterInfo *MRI = &MF->getRegInfo();306MachineInstr *ToErase = nullptr;307bool Changed = false;308309for (MachineBasicBlock &MBB : *MF) {310for (MachineInstr &MI : MBB) {311if (ToErase) {312ToErase->eraseFromParent();313ToErase = nullptr;314}315316// Ensure the register format is LOAD <reg>, <reg>, 0317if (!isLoadInst(MI.getOpcode()))318continue;319320if (SkipInsts.find(&MI) != SkipInsts.end())321continue;322323if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg())324continue;325326if (!MI.getOperand(2).isImm() || MI.getOperand(2).getImm())327continue;328329Register DstReg = MI.getOperand(0).getReg();330Register SrcReg = MI.getOperand(1).getReg();331332MachineInstr *DefInst = MRI->getUniqueVRegDef(SrcReg);333if (!DefInst)334continue;335336if (DefInst->getOpcode() != BPF::LD_imm64)337continue;338339const MachineOperand &MO = DefInst->getOperand(1);340if (!MO.isGlobal())341continue;342343const GlobalValue *GVal = MO.getGlobal();344auto *GVar = dyn_cast<GlobalVariable>(GVal);345if (!GVar)346continue;347348// Global variables representing structure offset or type id.349bool IsAma = false;350if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr))351IsAma = true;352else if (!GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))353continue;354355processCandidate(MRI, MBB, MI, SrcReg, DstReg, GVal, IsAma);356357ToErase = &MI;358Changed = true;359}360}361362return Changed;363}364365} // namespace366367INITIALIZE_PASS(BPFMISimplifyPatchable, DEBUG_TYPE,368"BPF PreEmit SimplifyPatchable", false, false)369370char BPFMISimplifyPatchable::ID = 0;371FunctionPass *llvm::createBPFMISimplifyPatchablePass() {372return new BPFMISimplifyPatchable();373}374375376