xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 //
25*5ffd83dbSDimitry Andric // This pass also removes the intermediate load generated in IR pass for
26*5ffd83dbSDimitry Andric // __builtin_btf_type_id() intrinsic.
27*5ffd83dbSDimitry Andric //
280b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #include "BPF.h"
310b57cec5SDimitry Andric #include "BPFCORE.h"
320b57cec5SDimitry Andric #include "BPFInstrInfo.h"
330b57cec5SDimitry Andric #include "BPFTargetMachine.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
368bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric using namespace llvm;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric #define DEBUG_TYPE "bpf-mi-simplify-patchable"
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric namespace {
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric struct BPFMISimplifyPatchable : public MachineFunctionPass {
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   static char ID;
470b57cec5SDimitry Andric   const BPFInstrInfo *TII;
480b57cec5SDimitry Andric   MachineFunction *MF;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   BPFMISimplifyPatchable() : MachineFunctionPass(ID) {
510b57cec5SDimitry Andric     initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric private:
550b57cec5SDimitry Andric   // Initialize class variables.
560b57cec5SDimitry Andric   void initialize(MachineFunction &MFParm);
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   bool removeLD(void);
59480093f4SDimitry Andric   void processCandidate(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
60480093f4SDimitry Andric                         MachineInstr &MI, Register &SrcReg, Register &DstReg,
61*5ffd83dbSDimitry Andric                         const GlobalValue *GVal, bool IsAma);
62480093f4SDimitry Andric   void processDstReg(MachineRegisterInfo *MRI, Register &DstReg,
63480093f4SDimitry Andric                      Register &SrcReg, const GlobalValue *GVal,
64*5ffd83dbSDimitry Andric                      bool doSrcRegProp, bool IsAma);
65480093f4SDimitry Andric   void processInst(MachineRegisterInfo *MRI, MachineInstr *Inst,
66480093f4SDimitry Andric                    MachineOperand *RelocOp, const GlobalValue *GVal);
67480093f4SDimitry Andric   void checkADDrr(MachineRegisterInfo *MRI, MachineOperand *RelocOp,
68480093f4SDimitry Andric                   const GlobalValue *GVal);
69480093f4SDimitry Andric   void checkShift(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
70480093f4SDimitry Andric                   MachineOperand *RelocOp, const GlobalValue *GVal,
71480093f4SDimitry Andric                   unsigned Opcode);
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric public:
740b57cec5SDimitry Andric   // Main entry point for this pass.
750b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
7613138422SDimitry Andric     if (skipFunction(MF.getFunction()))
7713138422SDimitry Andric       return false;
7813138422SDimitry Andric 
790b57cec5SDimitry Andric     initialize(MF);
800b57cec5SDimitry Andric     return removeLD();
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric // Initialize class variables.
850b57cec5SDimitry Andric void BPFMISimplifyPatchable::initialize(MachineFunction &MFParm) {
860b57cec5SDimitry Andric   MF = &MFParm;
870b57cec5SDimitry Andric   TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
880b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
91480093f4SDimitry Andric void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,
92480093f4SDimitry Andric     MachineOperand *RelocOp, const GlobalValue *GVal) {
93480093f4SDimitry Andric   const MachineInstr *Inst = RelocOp->getParent();
94480093f4SDimitry Andric   const MachineOperand *Op1 = &Inst->getOperand(1);
95480093f4SDimitry Andric   const MachineOperand *Op2 = &Inst->getOperand(2);
96480093f4SDimitry Andric   const MachineOperand *BaseOp = (RelocOp == Op1) ? Op2 : Op1;
97480093f4SDimitry Andric 
98480093f4SDimitry Andric   // Go through all uses of %1 as in %1 = ADD_rr %2, %3
99480093f4SDimitry Andric   const MachineOperand Op0 = Inst->getOperand(0);
100480093f4SDimitry Andric   auto Begin = MRI->use_begin(Op0.getReg()), End = MRI->use_end();
101480093f4SDimitry Andric   decltype(End) NextI;
102480093f4SDimitry Andric   for (auto I = Begin; I != End; I = NextI) {
103480093f4SDimitry Andric     NextI = std::next(I);
104480093f4SDimitry Andric     // The candidate needs to have a unique definition.
105480093f4SDimitry Andric     if (!MRI->getUniqueVRegDef(I->getReg()))
106480093f4SDimitry Andric       continue;
107480093f4SDimitry Andric 
108480093f4SDimitry Andric     MachineInstr *DefInst = I->getParent();
109480093f4SDimitry Andric     unsigned Opcode = DefInst->getOpcode();
110480093f4SDimitry Andric     unsigned COREOp;
111480093f4SDimitry Andric     if (Opcode == BPF::LDB || Opcode == BPF::LDH || Opcode == BPF::LDW ||
112480093f4SDimitry Andric         Opcode == BPF::LDD || Opcode == BPF::STB || Opcode == BPF::STH ||
113480093f4SDimitry Andric         Opcode == BPF::STW || Opcode == BPF::STD)
114480093f4SDimitry Andric       COREOp = BPF::CORE_MEM;
115480093f4SDimitry Andric     else if (Opcode == BPF::LDB32 || Opcode == BPF::LDH32 ||
116480093f4SDimitry Andric              Opcode == BPF::LDW32 || Opcode == BPF::STB32 ||
117480093f4SDimitry Andric              Opcode == BPF::STH32 || Opcode == BPF::STW32)
118480093f4SDimitry Andric       COREOp = BPF::CORE_ALU32_MEM;
119480093f4SDimitry Andric     else
120480093f4SDimitry Andric       continue;
121480093f4SDimitry Andric 
122d65cd7a5SDimitry Andric     // It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.
123480093f4SDimitry Andric     const MachineOperand &ImmOp = DefInst->getOperand(2);
124480093f4SDimitry Andric     if (!ImmOp.isImm() || ImmOp.getImm() != 0)
125480093f4SDimitry Andric       continue;
126480093f4SDimitry Andric 
127d65cd7a5SDimitry Andric     // Reject the form:
128d65cd7a5SDimitry Andric     //   %1 = ADD_rr %2, %3
129d65cd7a5SDimitry Andric     //   *(type *)(%2 + 0) = %1
130d65cd7a5SDimitry Andric     if (Opcode == BPF::STB || Opcode == BPF::STH || Opcode == BPF::STW ||
131d65cd7a5SDimitry Andric         Opcode == BPF::STD || Opcode == BPF::STB32 || Opcode == BPF::STH32 ||
132d65cd7a5SDimitry Andric         Opcode == BPF::STW32) {
133d65cd7a5SDimitry Andric       const MachineOperand &Opnd = DefInst->getOperand(0);
134d65cd7a5SDimitry Andric       if (Opnd.isReg() && Opnd.getReg() == I->getReg())
135d65cd7a5SDimitry Andric         continue;
136d65cd7a5SDimitry Andric     }
137d65cd7a5SDimitry Andric 
138480093f4SDimitry Andric     BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))
139480093f4SDimitry Andric         .add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)
140480093f4SDimitry Andric         .addGlobalAddress(GVal);
141480093f4SDimitry Andric     DefInst->eraseFromParent();
142480093f4SDimitry Andric   }
143480093f4SDimitry Andric }
144480093f4SDimitry Andric 
145480093f4SDimitry Andric void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo *MRI,
146480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineOperand *RelocOp, const GlobalValue *GVal,
147480093f4SDimitry Andric     unsigned Opcode) {
148480093f4SDimitry Andric   // Relocation operand should be the operand #2.
149480093f4SDimitry Andric   MachineInstr *Inst = RelocOp->getParent();
150480093f4SDimitry Andric   if (RelocOp != &Inst->getOperand(2))
151480093f4SDimitry Andric     return;
152480093f4SDimitry Andric 
153480093f4SDimitry Andric   BuildMI(MBB, *Inst, Inst->getDebugLoc(), TII->get(BPF::CORE_SHIFT))
154480093f4SDimitry Andric       .add(Inst->getOperand(0)).addImm(Opcode)
155480093f4SDimitry Andric       .add(Inst->getOperand(1)).addGlobalAddress(GVal);
156480093f4SDimitry Andric   Inst->eraseFromParent();
157480093f4SDimitry Andric }
158480093f4SDimitry Andric 
159480093f4SDimitry Andric void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo *MRI,
160480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineInstr &MI, Register &SrcReg,
161*5ffd83dbSDimitry Andric     Register &DstReg, const GlobalValue *GVal, bool IsAma) {
162480093f4SDimitry Andric   if (MRI->getRegClass(DstReg) == &BPF::GPR32RegClass) {
163*5ffd83dbSDimitry Andric     if (IsAma) {
164480093f4SDimitry Andric       // We can optimize such a pattern:
165480093f4SDimitry Andric       //  %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"
166480093f4SDimitry Andric       //  %2:gpr32 = LDW32 %1:gpr, 0
167480093f4SDimitry Andric       //  %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32
168480093f4SDimitry Andric       //  %4:gpr = ADD_rr %0:gpr, %3:gpr
169480093f4SDimitry Andric       //  or similar patterns below for non-alu32 case.
170480093f4SDimitry Andric       auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
171480093f4SDimitry Andric       decltype(End) NextI;
172480093f4SDimitry Andric       for (auto I = Begin; I != End; I = NextI) {
173480093f4SDimitry Andric         NextI = std::next(I);
174480093f4SDimitry Andric         if (!MRI->getUniqueVRegDef(I->getReg()))
175480093f4SDimitry Andric           continue;
176480093f4SDimitry Andric 
177480093f4SDimitry Andric         unsigned Opcode = I->getParent()->getOpcode();
178480093f4SDimitry Andric         if (Opcode == BPF::SUBREG_TO_REG) {
179480093f4SDimitry Andric           Register TmpReg = I->getParent()->getOperand(0).getReg();
180*5ffd83dbSDimitry Andric           processDstReg(MRI, TmpReg, DstReg, GVal, false, IsAma);
181*5ffd83dbSDimitry Andric         }
182480093f4SDimitry Andric       }
183480093f4SDimitry Andric     }
184480093f4SDimitry Andric 
185480093f4SDimitry Andric     BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::COPY), DstReg)
186480093f4SDimitry Andric         .addReg(SrcReg, 0, BPF::sub_32);
187480093f4SDimitry Andric     return;
188480093f4SDimitry Andric   }
189480093f4SDimitry Andric 
190480093f4SDimitry Andric   // All uses of DstReg replaced by SrcReg
191*5ffd83dbSDimitry Andric   processDstReg(MRI, DstReg, SrcReg, GVal, true, IsAma);
192480093f4SDimitry Andric }
193480093f4SDimitry Andric 
194480093f4SDimitry Andric void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo *MRI,
195480093f4SDimitry Andric     Register &DstReg, Register &SrcReg, const GlobalValue *GVal,
196*5ffd83dbSDimitry Andric     bool doSrcRegProp, bool IsAma) {
197480093f4SDimitry Andric   auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
198480093f4SDimitry Andric   decltype(End) NextI;
199480093f4SDimitry Andric   for (auto I = Begin; I != End; I = NextI) {
200480093f4SDimitry Andric     NextI = std::next(I);
201480093f4SDimitry Andric     if (doSrcRegProp)
202480093f4SDimitry Andric       I->setReg(SrcReg);
203480093f4SDimitry Andric 
204480093f4SDimitry Andric     // The candidate needs to have a unique definition.
205*5ffd83dbSDimitry Andric     if (IsAma && MRI->getUniqueVRegDef(I->getReg()))
206480093f4SDimitry Andric       processInst(MRI, I->getParent(), &*I, GVal);
207480093f4SDimitry Andric   }
208480093f4SDimitry Andric }
209480093f4SDimitry Andric 
210480093f4SDimitry Andric // Check to see whether we could do some optimization
211480093f4SDimitry Andric // to attach relocation to downstream dependent instructions.
212480093f4SDimitry Andric // Two kinds of patterns are recognized below:
213480093f4SDimitry Andric // Pattern 1:
214480093f4SDimitry Andric //   %1 = LD_imm64 @"llvm.b:0:4$0:1"  <== patch_imm = 4
215480093f4SDimitry Andric //   %2 = LDD %1, 0  <== this insn will be removed
216480093f4SDimitry Andric //   %3 = ADD_rr %0, %2
217480093f4SDimitry Andric //   %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0
218480093f4SDimitry Andric //   The `%4 = ...` will be transformed to
219480093f4SDimitry Andric //      CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")
220480093f4SDimitry Andric //   and later on, BTF emit phase will translate to
221480093f4SDimitry Andric //      %4 = LDW[32] %0, 4 STW[32] %4, %0, 4
222480093f4SDimitry Andric //   and attach a relocation to it.
223480093f4SDimitry Andric // Pattern 2:
224480093f4SDimitry Andric //    %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5
225480093f4SDimitry Andric //    %16 = LDD %15, 0   <== this insn will be removed
226480093f4SDimitry Andric //    %17 = SRA_rr %14, %16
227480093f4SDimitry Andric //    The `%17 = ...` will be transformed to
228480093f4SDimitry Andric //       %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")
229480093f4SDimitry Andric //    and later on, BTF emit phase will translate to
230480093f4SDimitry Andric //       %r4 = SRA_ri %r4, 63
231480093f4SDimitry Andric void BPFMISimplifyPatchable::processInst(MachineRegisterInfo *MRI,
232480093f4SDimitry Andric     MachineInstr *Inst, MachineOperand *RelocOp, const GlobalValue *GVal) {
233480093f4SDimitry Andric   unsigned Opcode = Inst->getOpcode();
234480093f4SDimitry Andric   if (Opcode == BPF::ADD_rr)
235480093f4SDimitry Andric     checkADDrr(MRI, RelocOp, GVal);
236480093f4SDimitry Andric   else if (Opcode == BPF::SLL_rr)
237480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SLL_ri);
238480093f4SDimitry Andric   else if (Opcode == BPF::SRA_rr)
239480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRA_ri);
240480093f4SDimitry Andric   else if (Opcode == BPF::SRL_rr)
241480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRL_ri);
242480093f4SDimitry Andric }
243480093f4SDimitry Andric 
2440b57cec5SDimitry Andric /// Remove unneeded Load instructions.
2450b57cec5SDimitry Andric bool BPFMISimplifyPatchable::removeLD() {
2460b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
2470b57cec5SDimitry Andric   MachineInstr *ToErase = nullptr;
2480b57cec5SDimitry Andric   bool Changed = false;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : *MF) {
2510b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
2520b57cec5SDimitry Andric       if (ToErase) {
2530b57cec5SDimitry Andric         ToErase->eraseFromParent();
2540b57cec5SDimitry Andric         ToErase = nullptr;
2550b57cec5SDimitry Andric       }
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric       // Ensure the register format is LOAD <reg>, <reg>, 0
2580b57cec5SDimitry Andric       if (MI.getOpcode() != BPF::LDD && MI.getOpcode() != BPF::LDW &&
2590b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDH && MI.getOpcode() != BPF::LDB &&
2600b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDW32 && MI.getOpcode() != BPF::LDH32 &&
2610b57cec5SDimitry Andric           MI.getOpcode() != BPF::LDB32)
2620b57cec5SDimitry Andric         continue;
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric       if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg())
2650b57cec5SDimitry Andric         continue;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric       if (!MI.getOperand(2).isImm() || MI.getOperand(2).getImm())
2680b57cec5SDimitry Andric         continue;
2690b57cec5SDimitry Andric 
2708bcb0991SDimitry Andric       Register DstReg = MI.getOperand(0).getReg();
2718bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(1).getReg();
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric       MachineInstr *DefInst = MRI->getUniqueVRegDef(SrcReg);
2740b57cec5SDimitry Andric       if (!DefInst)
2750b57cec5SDimitry Andric         continue;
2760b57cec5SDimitry Andric 
277*5ffd83dbSDimitry Andric       if (DefInst->getOpcode() != BPF::LD_imm64)
2780b57cec5SDimitry Andric         continue;
2790b57cec5SDimitry Andric 
280*5ffd83dbSDimitry Andric       const MachineOperand &MO = DefInst->getOperand(1);
281*5ffd83dbSDimitry Andric       if (!MO.isGlobal())
282*5ffd83dbSDimitry Andric         continue;
283*5ffd83dbSDimitry Andric 
284*5ffd83dbSDimitry Andric       const GlobalValue *GVal = MO.getGlobal();
285*5ffd83dbSDimitry Andric       auto *GVar = dyn_cast<GlobalVariable>(GVal);
286*5ffd83dbSDimitry Andric       if (!GVar)
287*5ffd83dbSDimitry Andric         continue;
288*5ffd83dbSDimitry Andric 
289*5ffd83dbSDimitry Andric       // Global variables representing structure offset or type id.
290*5ffd83dbSDimitry Andric       bool IsAma = false;
291*5ffd83dbSDimitry Andric       if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr))
292*5ffd83dbSDimitry Andric         IsAma = true;
293*5ffd83dbSDimitry Andric       else if (!GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
294*5ffd83dbSDimitry Andric         continue;
295*5ffd83dbSDimitry Andric 
296*5ffd83dbSDimitry Andric       processCandidate(MRI, MBB, MI, SrcReg, DstReg, GVal, IsAma);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric       ToErase = &MI;
2990b57cec5SDimitry Andric       Changed = true;
3000b57cec5SDimitry Andric     }
3010b57cec5SDimitry Andric   }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   return Changed;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric } // namespace
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric INITIALIZE_PASS(BPFMISimplifyPatchable, DEBUG_TYPE,
3090b57cec5SDimitry Andric                 "BPF PreEmit SimplifyPatchable", false, false)
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric char BPFMISimplifyPatchable::ID = 0;
3120b57cec5SDimitry Andric FunctionPass *llvm::createBPFMISimplifyPatchablePass() {
3130b57cec5SDimitry Andric   return new BPFMISimplifyPatchable();
3140b57cec5SDimitry Andric }
315