xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
10b57cec5SDimitry Andric //===----- BPFMISimplifyPatchable.cpp - MI Simplify Patchable Insts -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass targets a subset of instructions like below
100b57cec5SDimitry Andric //    ld_imm64 r1, @global
110b57cec5SDimitry Andric //    ldd r2, r1, 0
120b57cec5SDimitry Andric //    add r3, struct_base_reg, r2
130b57cec5SDimitry Andric //
148bcb0991SDimitry Andric // Here @global should represent an AMA (abstruct member access).
158bcb0991SDimitry Andric // Such an access is subject to bpf load time patching. After this pass, the
160b57cec5SDimitry Andric // code becomes
170b57cec5SDimitry Andric //    ld_imm64 r1, @global
180b57cec5SDimitry Andric //    add r3, struct_base_reg, r1
190b57cec5SDimitry Andric //
200b57cec5SDimitry Andric // Eventually, at BTF output stage, a relocation record will be generated
210b57cec5SDimitry Andric // for ld_imm64 which should be replaced later by bpf loader:
228bcb0991SDimitry Andric //    r1 = <calculated field_info>
230b57cec5SDimitry Andric //    add r3, struct_base_reg, r1
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #include "BPF.h"
280b57cec5SDimitry Andric #include "BPFCORE.h"
290b57cec5SDimitry Andric #include "BPFInstrInfo.h"
300b57cec5SDimitry Andric #include "BPFTargetMachine.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
338bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #define DEBUG_TYPE "bpf-mi-simplify-patchable"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace {
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric struct BPFMISimplifyPatchable : public MachineFunctionPass {
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   static char ID;
440b57cec5SDimitry Andric   const BPFInstrInfo *TII;
450b57cec5SDimitry Andric   MachineFunction *MF;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   BPFMISimplifyPatchable() : MachineFunctionPass(ID) {
480b57cec5SDimitry Andric     initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric private:
520b57cec5SDimitry Andric   // Initialize class variables.
530b57cec5SDimitry Andric   void initialize(MachineFunction &MFParm);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   bool removeLD(void);
56*480093f4SDimitry Andric   void processCandidate(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
57*480093f4SDimitry Andric                         MachineInstr &MI, Register &SrcReg, Register &DstReg,
58*480093f4SDimitry Andric                         const GlobalValue *GVal);
59*480093f4SDimitry Andric   void processDstReg(MachineRegisterInfo *MRI, Register &DstReg,
60*480093f4SDimitry Andric                      Register &SrcReg, const GlobalValue *GVal,
61*480093f4SDimitry Andric                      bool doSrcRegProp);
62*480093f4SDimitry Andric   void processInst(MachineRegisterInfo *MRI, MachineInstr *Inst,
63*480093f4SDimitry Andric                    MachineOperand *RelocOp, const GlobalValue *GVal);
64*480093f4SDimitry Andric   void checkADDrr(MachineRegisterInfo *MRI, MachineOperand *RelocOp,
65*480093f4SDimitry Andric                   const GlobalValue *GVal);
66*480093f4SDimitry Andric   void checkShift(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
67*480093f4SDimitry Andric                   MachineOperand *RelocOp, const GlobalValue *GVal,
68*480093f4SDimitry Andric                   unsigned Opcode);
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric public:
710b57cec5SDimitry Andric   // Main entry point for this pass.
720b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
730b57cec5SDimitry Andric     if (!skipFunction(MF.getFunction())) {
740b57cec5SDimitry Andric       initialize(MF);
750b57cec5SDimitry Andric     }
760b57cec5SDimitry Andric     return removeLD();
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric // Initialize class variables.
810b57cec5SDimitry Andric void BPFMISimplifyPatchable::initialize(MachineFunction &MFParm) {
820b57cec5SDimitry Andric   MF = &MFParm;
830b57cec5SDimitry Andric   TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
840b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
87*480093f4SDimitry Andric void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,
88*480093f4SDimitry Andric     MachineOperand *RelocOp, const GlobalValue *GVal) {
89*480093f4SDimitry Andric   const MachineInstr *Inst = RelocOp->getParent();
90*480093f4SDimitry Andric   const MachineOperand *Op1 = &Inst->getOperand(1);
91*480093f4SDimitry Andric   const MachineOperand *Op2 = &Inst->getOperand(2);
92*480093f4SDimitry Andric   const MachineOperand *BaseOp = (RelocOp == Op1) ? Op2 : Op1;
93*480093f4SDimitry Andric 
94*480093f4SDimitry Andric   // Go through all uses of %1 as in %1 = ADD_rr %2, %3
95*480093f4SDimitry Andric   const MachineOperand Op0 = Inst->getOperand(0);
96*480093f4SDimitry Andric   auto Begin = MRI->use_begin(Op0.getReg()), End = MRI->use_end();
97*480093f4SDimitry Andric   decltype(End) NextI;
98*480093f4SDimitry Andric   for (auto I = Begin; I != End; I = NextI) {
99*480093f4SDimitry Andric     NextI = std::next(I);
100*480093f4SDimitry Andric     // The candidate needs to have a unique definition.
101*480093f4SDimitry Andric     if (!MRI->getUniqueVRegDef(I->getReg()))
102*480093f4SDimitry Andric       continue;
103*480093f4SDimitry Andric 
104*480093f4SDimitry Andric     MachineInstr *DefInst = I->getParent();
105*480093f4SDimitry Andric     unsigned Opcode = DefInst->getOpcode();
106*480093f4SDimitry Andric     unsigned COREOp;
107*480093f4SDimitry Andric     if (Opcode == BPF::LDB || Opcode == BPF::LDH || Opcode == BPF::LDW ||
108*480093f4SDimitry Andric         Opcode == BPF::LDD || Opcode == BPF::STB || Opcode == BPF::STH ||
109*480093f4SDimitry Andric         Opcode == BPF::STW || Opcode == BPF::STD)
110*480093f4SDimitry Andric       COREOp = BPF::CORE_MEM;
111*480093f4SDimitry Andric     else if (Opcode == BPF::LDB32 || Opcode == BPF::LDH32 ||
112*480093f4SDimitry Andric              Opcode == BPF::LDW32 || Opcode == BPF::STB32 ||
113*480093f4SDimitry Andric              Opcode == BPF::STH32 || Opcode == BPF::STW32)
114*480093f4SDimitry Andric       COREOp = BPF::CORE_ALU32_MEM;
115*480093f4SDimitry Andric     else
116*480093f4SDimitry Andric       continue;
117*480093f4SDimitry Andric 
118*480093f4SDimitry Andric     // It must be a form of %1 = *(type *)(%2 + 0) or *(type *)(%2 + 0) = %1.
119*480093f4SDimitry Andric     const MachineOperand &ImmOp = DefInst->getOperand(2);
120*480093f4SDimitry Andric     if (!ImmOp.isImm() || ImmOp.getImm() != 0)
121*480093f4SDimitry Andric       continue;
122*480093f4SDimitry Andric 
123*480093f4SDimitry Andric     BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))
124*480093f4SDimitry Andric         .add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)
125*480093f4SDimitry Andric         .addGlobalAddress(GVal);
126*480093f4SDimitry Andric     DefInst->eraseFromParent();
127*480093f4SDimitry Andric   }
128*480093f4SDimitry Andric }
129*480093f4SDimitry Andric 
130*480093f4SDimitry Andric void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo *MRI,
131*480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineOperand *RelocOp, const GlobalValue *GVal,
132*480093f4SDimitry Andric     unsigned Opcode) {
133*480093f4SDimitry Andric   // Relocation operand should be the operand #2.
134*480093f4SDimitry Andric   MachineInstr *Inst = RelocOp->getParent();
135*480093f4SDimitry Andric   if (RelocOp != &Inst->getOperand(2))
136*480093f4SDimitry Andric     return;
137*480093f4SDimitry Andric 
138*480093f4SDimitry Andric   BuildMI(MBB, *Inst, Inst->getDebugLoc(), TII->get(BPF::CORE_SHIFT))
139*480093f4SDimitry Andric       .add(Inst->getOperand(0)).addImm(Opcode)
140*480093f4SDimitry Andric       .add(Inst->getOperand(1)).addGlobalAddress(GVal);
141*480093f4SDimitry Andric   Inst->eraseFromParent();
142*480093f4SDimitry Andric }
143*480093f4SDimitry Andric 
144*480093f4SDimitry Andric void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo *MRI,
145*480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineInstr &MI, Register &SrcReg,
146*480093f4SDimitry Andric     Register &DstReg, const GlobalValue *GVal) {
147*480093f4SDimitry Andric   if (MRI->getRegClass(DstReg) == &BPF::GPR32RegClass) {
148*480093f4SDimitry Andric     // We can optimize such a pattern:
149*480093f4SDimitry Andric     //  %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"
150*480093f4SDimitry Andric     //  %2:gpr32 = LDW32 %1:gpr, 0
151*480093f4SDimitry Andric     //  %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32
152*480093f4SDimitry Andric     //  %4:gpr = ADD_rr %0:gpr, %3:gpr
153*480093f4SDimitry Andric     //  or similar patterns below for non-alu32 case.
154*480093f4SDimitry Andric     auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
155*480093f4SDimitry Andric     decltype(End) NextI;
156*480093f4SDimitry Andric     for (auto I = Begin; I != End; I = NextI) {
157*480093f4SDimitry Andric       NextI = std::next(I);
158*480093f4SDimitry Andric       if (!MRI->getUniqueVRegDef(I->getReg()))
159*480093f4SDimitry Andric         continue;
160*480093f4SDimitry Andric 
161*480093f4SDimitry Andric       unsigned Opcode = I->getParent()->getOpcode();
162*480093f4SDimitry Andric       if (Opcode == BPF::SUBREG_TO_REG) {
163*480093f4SDimitry Andric         Register TmpReg = I->getParent()->getOperand(0).getReg();
164*480093f4SDimitry Andric         processDstReg(MRI, TmpReg, DstReg, GVal, false);
165*480093f4SDimitry Andric       }
166*480093f4SDimitry Andric     }
167*480093f4SDimitry Andric 
168*480093f4SDimitry Andric     BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::COPY), DstReg)
169*480093f4SDimitry Andric         .addReg(SrcReg, 0, BPF::sub_32);
170*480093f4SDimitry Andric     return;
171*480093f4SDimitry Andric   }
172*480093f4SDimitry Andric 
173*480093f4SDimitry Andric   // All uses of DstReg replaced by SrcReg
174*480093f4SDimitry Andric   processDstReg(MRI, DstReg, SrcReg, GVal, true);
175*480093f4SDimitry Andric }
176*480093f4SDimitry Andric 
177*480093f4SDimitry Andric void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo *MRI,
178*480093f4SDimitry Andric     Register &DstReg, Register &SrcReg, const GlobalValue *GVal,
179*480093f4SDimitry Andric     bool doSrcRegProp) {
180*480093f4SDimitry Andric   auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
181*480093f4SDimitry Andric   decltype(End) NextI;
182*480093f4SDimitry Andric   for (auto I = Begin; I != End; I = NextI) {
183*480093f4SDimitry Andric     NextI = std::next(I);
184*480093f4SDimitry Andric     if (doSrcRegProp)
185*480093f4SDimitry Andric       I->setReg(SrcReg);
186*480093f4SDimitry Andric 
187*480093f4SDimitry Andric     // The candidate needs to have a unique definition.
188*480093f4SDimitry Andric     if (MRI->getUniqueVRegDef(I->getReg()))
189*480093f4SDimitry Andric       processInst(MRI, I->getParent(), &*I, GVal);
190*480093f4SDimitry Andric   }
191*480093f4SDimitry Andric }
192*480093f4SDimitry Andric 
193*480093f4SDimitry Andric // Check to see whether we could do some optimization
194*480093f4SDimitry Andric // to attach relocation to downstream dependent instructions.
195*480093f4SDimitry Andric // Two kinds of patterns are recognized below:
196*480093f4SDimitry Andric // Pattern 1:
197*480093f4SDimitry Andric //   %1 = LD_imm64 @"llvm.b:0:4$0:1"  <== patch_imm = 4
198*480093f4SDimitry Andric //   %2 = LDD %1, 0  <== this insn will be removed
199*480093f4SDimitry Andric //   %3 = ADD_rr %0, %2
200*480093f4SDimitry Andric //   %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0
201*480093f4SDimitry Andric //   The `%4 = ...` will be transformed to
202*480093f4SDimitry Andric //      CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")
203*480093f4SDimitry Andric //   and later on, BTF emit phase will translate to
204*480093f4SDimitry Andric //      %4 = LDW[32] %0, 4 STW[32] %4, %0, 4
205*480093f4SDimitry Andric //   and attach a relocation to it.
206*480093f4SDimitry Andric // Pattern 2:
207*480093f4SDimitry Andric //    %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5
208*480093f4SDimitry Andric //    %16 = LDD %15, 0   <== this insn will be removed
209*480093f4SDimitry Andric //    %17 = SRA_rr %14, %16
210*480093f4SDimitry Andric //    The `%17 = ...` will be transformed to
211*480093f4SDimitry Andric //       %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")
212*480093f4SDimitry Andric //    and later on, BTF emit phase will translate to
213*480093f4SDimitry Andric //       %r4 = SRA_ri %r4, 63
214*480093f4SDimitry Andric void BPFMISimplifyPatchable::processInst(MachineRegisterInfo *MRI,
215*480093f4SDimitry Andric     MachineInstr *Inst, MachineOperand *RelocOp, const GlobalValue *GVal) {
216*480093f4SDimitry Andric   unsigned Opcode = Inst->getOpcode();
217*480093f4SDimitry Andric   if (Opcode == BPF::ADD_rr)
218*480093f4SDimitry Andric     checkADDrr(MRI, RelocOp, GVal);
219*480093f4SDimitry Andric   else if (Opcode == BPF::SLL_rr)
220*480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SLL_ri);
221*480093f4SDimitry Andric   else if (Opcode == BPF::SRA_rr)
222*480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRA_ri);
223*480093f4SDimitry Andric   else if (Opcode == BPF::SRL_rr)
224*480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRL_ri);
225*480093f4SDimitry Andric }
226*480093f4SDimitry Andric 
2270b57cec5SDimitry Andric /// Remove unneeded Load instructions.
2280b57cec5SDimitry Andric bool BPFMISimplifyPatchable::removeLD() {
2290b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
2300b57cec5SDimitry Andric   MachineInstr *ToErase = nullptr;
2310b57cec5SDimitry Andric   bool Changed = false;
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : *MF) {
2340b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
2350b57cec5SDimitry Andric       if (ToErase) {
2360b57cec5SDimitry Andric         ToErase->eraseFromParent();
2370b57cec5SDimitry Andric         ToErase = nullptr;
2380b57cec5SDimitry Andric       }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric       // Ensure the register format is LOAD <reg>, <reg>, 0
2410b57cec5SDimitry Andric       if (MI.getOpcode() != BPF::LDD && MI.getOpcode() != BPF::LDW &&
2420b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDH && MI.getOpcode() != BPF::LDB &&
2430b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDW32 && MI.getOpcode() != BPF::LDH32 &&
2440b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDB32)
2450b57cec5SDimitry Andric         continue;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric       if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg())
2480b57cec5SDimitry Andric         continue;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric       if (!MI.getOperand(2).isImm() || MI.getOperand(2).getImm())
2510b57cec5SDimitry Andric         continue;
2520b57cec5SDimitry Andric 
2538bcb0991SDimitry Andric       Register DstReg = MI.getOperand(0).getReg();
2548bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(1).getReg();
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric       MachineInstr *DefInst = MRI->getUniqueVRegDef(SrcReg);
2570b57cec5SDimitry Andric       if (!DefInst)
2580b57cec5SDimitry Andric         continue;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric       bool IsCandidate = false;
261*480093f4SDimitry Andric       const GlobalValue *GVal = nullptr;
2620b57cec5SDimitry Andric       if (DefInst->getOpcode() == BPF::LD_imm64) {
2630b57cec5SDimitry Andric         const MachineOperand &MO = DefInst->getOperand(1);
2640b57cec5SDimitry Andric         if (MO.isGlobal()) {
265*480093f4SDimitry Andric           GVal = MO.getGlobal();
2660b57cec5SDimitry Andric           auto *GVar = dyn_cast<GlobalVariable>(GVal);
2670b57cec5SDimitry Andric           if (GVar) {
2680b57cec5SDimitry Andric             // Global variables representing structure offset or
2690b57cec5SDimitry Andric             // patchable extern globals.
2700b57cec5SDimitry Andric             if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
2718bcb0991SDimitry Andric               assert(MI.getOperand(2).getImm() == 0);
2720b57cec5SDimitry Andric               IsCandidate = true;
2730b57cec5SDimitry Andric             }
2740b57cec5SDimitry Andric           }
2750b57cec5SDimitry Andric         }
2760b57cec5SDimitry Andric       }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric       if (!IsCandidate)
2790b57cec5SDimitry Andric         continue;
2800b57cec5SDimitry Andric 
281*480093f4SDimitry Andric       processCandidate(MRI, MBB, MI, SrcReg, DstReg, GVal);
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric       ToErase = &MI;
2840b57cec5SDimitry Andric       Changed = true;
2850b57cec5SDimitry Andric     }
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   return Changed;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric } // namespace
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric INITIALIZE_PASS(BPFMISimplifyPatchable, DEBUG_TYPE,
2940b57cec5SDimitry Andric                 "BPF PreEmit SimplifyPatchable", false, false)
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric char BPFMISimplifyPatchable::ID = 0;
2970b57cec5SDimitry Andric FunctionPass *llvm::createBPFMISimplifyPatchablePass() {
2980b57cec5SDimitry Andric   return new BPFMISimplifyPatchable();
2990b57cec5SDimitry Andric }
300