xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/BPF/BPFMISimplifyPatchable.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 //
255ffd83dbSDimitry Andric // This pass also removes the intermediate load generated in IR pass for
265ffd83dbSDimitry Andric // __builtin_btf_type_id() intrinsic.
275ffd83dbSDimitry 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"
3481ad6265SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
37*0fca6ea1SDimitry Andric #include "llvm/IR/GlobalVariable.h"
388bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
3981ad6265SDimitry Andric #include <set>
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric #define DEBUG_TYPE "bpf-mi-simplify-patchable"
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric namespace {
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric struct BPFMISimplifyPatchable : public MachineFunctionPass {
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   static char ID;
500b57cec5SDimitry Andric   const BPFInstrInfo *TII;
510b57cec5SDimitry Andric   MachineFunction *MF;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   BPFMISimplifyPatchable() : MachineFunctionPass(ID) {
540b57cec5SDimitry Andric     initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric private:
5881ad6265SDimitry Andric   std::set<MachineInstr *> SkipInsts;
5981ad6265SDimitry Andric 
600b57cec5SDimitry Andric   // Initialize class variables.
610b57cec5SDimitry Andric   void initialize(MachineFunction &MFParm);
620b57cec5SDimitry Andric 
6381ad6265SDimitry Andric   bool isLoadInst(unsigned Opcode);
6404eeddc0SDimitry Andric   bool removeLD();
65480093f4SDimitry Andric   void processCandidate(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
66480093f4SDimitry Andric                         MachineInstr &MI, Register &SrcReg, Register &DstReg,
675ffd83dbSDimitry Andric                         const GlobalValue *GVal, bool IsAma);
68480093f4SDimitry Andric   void processDstReg(MachineRegisterInfo *MRI, Register &DstReg,
69480093f4SDimitry Andric                      Register &SrcReg, const GlobalValue *GVal,
705ffd83dbSDimitry Andric                      bool doSrcRegProp, bool IsAma);
71480093f4SDimitry Andric   void processInst(MachineRegisterInfo *MRI, MachineInstr *Inst,
72480093f4SDimitry Andric                    MachineOperand *RelocOp, const GlobalValue *GVal);
73480093f4SDimitry Andric   void checkADDrr(MachineRegisterInfo *MRI, MachineOperand *RelocOp,
74480093f4SDimitry Andric                   const GlobalValue *GVal);
75480093f4SDimitry Andric   void checkShift(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
76480093f4SDimitry Andric                   MachineOperand *RelocOp, const GlobalValue *GVal,
77480093f4SDimitry Andric                   unsigned Opcode);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric public:
800b57cec5SDimitry Andric   // Main entry point for this pass.
810b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
8213138422SDimitry Andric     if (skipFunction(MF.getFunction()))
8313138422SDimitry Andric       return false;
8413138422SDimitry Andric 
850b57cec5SDimitry Andric     initialize(MF);
860b57cec5SDimitry Andric     return removeLD();
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric };
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric // Initialize class variables.
910b57cec5SDimitry Andric void BPFMISimplifyPatchable::initialize(MachineFunction &MFParm) {
920b57cec5SDimitry Andric   MF = &MFParm;
930b57cec5SDimitry Andric   TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
940b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
975f757f3fSDimitry Andric static bool isST(unsigned Opcode) {
985f757f3fSDimitry Andric   return Opcode == BPF::STB_imm || Opcode == BPF::STH_imm ||
995f757f3fSDimitry Andric          Opcode == BPF::STW_imm || Opcode == BPF::STD_imm;
1005f757f3fSDimitry Andric }
1015f757f3fSDimitry Andric 
1025f757f3fSDimitry Andric static bool isSTX32(unsigned Opcode) {
1035f757f3fSDimitry Andric   return Opcode == BPF::STB32 || Opcode == BPF::STH32 || Opcode == BPF::STW32;
1045f757f3fSDimitry Andric }
1055f757f3fSDimitry Andric 
1065f757f3fSDimitry Andric static bool isSTX64(unsigned Opcode) {
1075f757f3fSDimitry Andric   return Opcode == BPF::STB || Opcode == BPF::STH || Opcode == BPF::STW ||
1085f757f3fSDimitry Andric          Opcode == BPF::STD;
1095f757f3fSDimitry Andric }
1105f757f3fSDimitry Andric 
1115f757f3fSDimitry Andric static bool isLDX32(unsigned Opcode) {
1125f757f3fSDimitry Andric   return Opcode == BPF::LDB32 || Opcode == BPF::LDH32 || Opcode == BPF::LDW32;
1135f757f3fSDimitry Andric }
1145f757f3fSDimitry Andric 
1155f757f3fSDimitry Andric static bool isLDX64(unsigned Opcode) {
1165f757f3fSDimitry Andric   return Opcode == BPF::LDB || Opcode == BPF::LDH || Opcode == BPF::LDW ||
1175f757f3fSDimitry Andric          Opcode == BPF::LDD;
1185f757f3fSDimitry Andric }
1195f757f3fSDimitry Andric 
1205f757f3fSDimitry Andric static bool isLDSX(unsigned Opcode) {
1215f757f3fSDimitry Andric   return Opcode == BPF::LDBSX || Opcode == BPF::LDHSX || Opcode == BPF::LDWSX;
1225f757f3fSDimitry Andric }
1235f757f3fSDimitry Andric 
12481ad6265SDimitry Andric bool BPFMISimplifyPatchable::isLoadInst(unsigned Opcode) {
1255f757f3fSDimitry Andric   return isLDX32(Opcode) || isLDX64(Opcode) || isLDSX(Opcode);
12681ad6265SDimitry Andric }
12781ad6265SDimitry Andric 
128480093f4SDimitry Andric void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,
129480093f4SDimitry Andric     MachineOperand *RelocOp, const GlobalValue *GVal) {
130480093f4SDimitry Andric   const MachineInstr *Inst = RelocOp->getParent();
131480093f4SDimitry Andric   const MachineOperand *Op1 = &Inst->getOperand(1);
132480093f4SDimitry Andric   const MachineOperand *Op2 = &Inst->getOperand(2);
133480093f4SDimitry Andric   const MachineOperand *BaseOp = (RelocOp == Op1) ? Op2 : Op1;
134480093f4SDimitry Andric 
135480093f4SDimitry Andric   // Go through all uses of %1 as in %1 = ADD_rr %2, %3
136480093f4SDimitry Andric   const MachineOperand Op0 = Inst->getOperand(0);
137349cc55cSDimitry Andric   for (MachineOperand &MO :
138349cc55cSDimitry Andric        llvm::make_early_inc_range(MRI->use_operands(Op0.getReg()))) {
139480093f4SDimitry Andric     // The candidate needs to have a unique definition.
140349cc55cSDimitry Andric     if (!MRI->getUniqueVRegDef(MO.getReg()))
141480093f4SDimitry Andric       continue;
142480093f4SDimitry Andric 
143349cc55cSDimitry Andric     MachineInstr *DefInst = MO.getParent();
144480093f4SDimitry Andric     unsigned Opcode = DefInst->getOpcode();
145480093f4SDimitry Andric     unsigned COREOp;
1465f757f3fSDimitry Andric     if (isLDX64(Opcode) || isLDSX(Opcode))
1475f757f3fSDimitry Andric       COREOp = BPF::CORE_LD64;
1485f757f3fSDimitry Andric     else if (isLDX32(Opcode))
1495f757f3fSDimitry Andric       COREOp = BPF::CORE_LD32;
1505f757f3fSDimitry Andric     else if (isSTX64(Opcode) || isSTX32(Opcode) || isST(Opcode))
1515f757f3fSDimitry Andric       COREOp = BPF::CORE_ST;
152480093f4SDimitry Andric     else
153480093f4SDimitry Andric       continue;
154480093f4SDimitry Andric 
155d65cd7a5SDimitry Andric     // It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.
156480093f4SDimitry Andric     const MachineOperand &ImmOp = DefInst->getOperand(2);
157480093f4SDimitry Andric     if (!ImmOp.isImm() || ImmOp.getImm() != 0)
158480093f4SDimitry Andric       continue;
159480093f4SDimitry Andric 
160d65cd7a5SDimitry Andric     // Reject the form:
161d65cd7a5SDimitry Andric     //   %1 = ADD_rr %2, %3
162d65cd7a5SDimitry Andric     //   *(type *)(%2 + 0) = %1
1635f757f3fSDimitry Andric     if (isSTX64(Opcode) || isSTX32(Opcode)) {
164d65cd7a5SDimitry Andric       const MachineOperand &Opnd = DefInst->getOperand(0);
165349cc55cSDimitry Andric       if (Opnd.isReg() && Opnd.getReg() == MO.getReg())
166d65cd7a5SDimitry Andric         continue;
167d65cd7a5SDimitry Andric     }
168d65cd7a5SDimitry Andric 
169480093f4SDimitry Andric     BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))
170480093f4SDimitry Andric         .add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)
171480093f4SDimitry Andric         .addGlobalAddress(GVal);
172480093f4SDimitry Andric     DefInst->eraseFromParent();
173480093f4SDimitry Andric   }
174480093f4SDimitry Andric }
175480093f4SDimitry Andric 
176480093f4SDimitry Andric void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo *MRI,
177480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineOperand *RelocOp, const GlobalValue *GVal,
178480093f4SDimitry Andric     unsigned Opcode) {
179480093f4SDimitry Andric   // Relocation operand should be the operand #2.
180480093f4SDimitry Andric   MachineInstr *Inst = RelocOp->getParent();
181480093f4SDimitry Andric   if (RelocOp != &Inst->getOperand(2))
182480093f4SDimitry Andric     return;
183480093f4SDimitry Andric 
184480093f4SDimitry Andric   BuildMI(MBB, *Inst, Inst->getDebugLoc(), TII->get(BPF::CORE_SHIFT))
185480093f4SDimitry Andric       .add(Inst->getOperand(0)).addImm(Opcode)
186480093f4SDimitry Andric       .add(Inst->getOperand(1)).addGlobalAddress(GVal);
187480093f4SDimitry Andric   Inst->eraseFromParent();
188480093f4SDimitry Andric }
189480093f4SDimitry Andric 
190480093f4SDimitry Andric void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo *MRI,
191480093f4SDimitry Andric     MachineBasicBlock &MBB, MachineInstr &MI, Register &SrcReg,
1925ffd83dbSDimitry Andric     Register &DstReg, const GlobalValue *GVal, bool IsAma) {
193480093f4SDimitry Andric   if (MRI->getRegClass(DstReg) == &BPF::GPR32RegClass) {
1945ffd83dbSDimitry Andric     if (IsAma) {
195480093f4SDimitry Andric       // We can optimize such a pattern:
196480093f4SDimitry Andric       //  %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"
197480093f4SDimitry Andric       //  %2:gpr32 = LDW32 %1:gpr, 0
198480093f4SDimitry Andric       //  %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32
199480093f4SDimitry Andric       //  %4:gpr = ADD_rr %0:gpr, %3:gpr
200480093f4SDimitry Andric       //  or similar patterns below for non-alu32 case.
201480093f4SDimitry Andric       auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
202480093f4SDimitry Andric       decltype(End) NextI;
203480093f4SDimitry Andric       for (auto I = Begin; I != End; I = NextI) {
204480093f4SDimitry Andric         NextI = std::next(I);
205480093f4SDimitry Andric         if (!MRI->getUniqueVRegDef(I->getReg()))
206480093f4SDimitry Andric           continue;
207480093f4SDimitry Andric 
208480093f4SDimitry Andric         unsigned Opcode = I->getParent()->getOpcode();
209480093f4SDimitry Andric         if (Opcode == BPF::SUBREG_TO_REG) {
210480093f4SDimitry Andric           Register TmpReg = I->getParent()->getOperand(0).getReg();
2115ffd83dbSDimitry Andric           processDstReg(MRI, TmpReg, DstReg, GVal, false, IsAma);
2125ffd83dbSDimitry Andric         }
213480093f4SDimitry Andric       }
214480093f4SDimitry Andric     }
215480093f4SDimitry Andric 
216480093f4SDimitry Andric     BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::COPY), DstReg)
217480093f4SDimitry Andric         .addReg(SrcReg, 0, BPF::sub_32);
218480093f4SDimitry Andric     return;
219480093f4SDimitry Andric   }
220480093f4SDimitry Andric 
221480093f4SDimitry Andric   // All uses of DstReg replaced by SrcReg
2225ffd83dbSDimitry Andric   processDstReg(MRI, DstReg, SrcReg, GVal, true, IsAma);
223480093f4SDimitry Andric }
224480093f4SDimitry Andric 
225480093f4SDimitry Andric void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo *MRI,
226480093f4SDimitry Andric     Register &DstReg, Register &SrcReg, const GlobalValue *GVal,
2275ffd83dbSDimitry Andric     bool doSrcRegProp, bool IsAma) {
228480093f4SDimitry Andric   auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
229480093f4SDimitry Andric   decltype(End) NextI;
230480093f4SDimitry Andric   for (auto I = Begin; I != End; I = NextI) {
231480093f4SDimitry Andric     NextI = std::next(I);
2328a4dda33SDimitry Andric     if (doSrcRegProp) {
2338a4dda33SDimitry Andric       // In situations like below it is not known if usage is a kill
2348a4dda33SDimitry Andric       // after setReg():
2358a4dda33SDimitry Andric       //
2368a4dda33SDimitry Andric       // .-> %2:gpr = LD_imm64 @"llvm.t:0:0$0:0"
2378a4dda33SDimitry Andric       // |
2388a4dda33SDimitry Andric       // |`----------------.
2398a4dda33SDimitry Andric       // |   %3:gpr = LDD %2:gpr, 0
2408a4dda33SDimitry Andric       // |   %4:gpr = ADD_rr %0:gpr(tied-def 0), killed %3:gpr <--- (1)
2418a4dda33SDimitry Andric       // |   %5:gpr = LDD killed %4:gpr, 0       ^^^^^^^^^^^^^
2428a4dda33SDimitry Andric       // |   STD killed %5:gpr, %1:gpr, 0         this is I
2438a4dda33SDimitry Andric       //  `----------------.
2448a4dda33SDimitry Andric       //     %6:gpr = LDD %2:gpr, 0
2458a4dda33SDimitry Andric       //     %7:gpr = ADD_rr %0:gpr(tied-def 0), killed %6:gpr <--- (2)
2468a4dda33SDimitry Andric       //     %8:gpr = LDD killed %7:gpr, 0       ^^^^^^^^^^^^^
2478a4dda33SDimitry Andric       //     STD killed %8:gpr, %1:gpr, 0         this is I
2488a4dda33SDimitry Andric       //
2498a4dda33SDimitry Andric       // Instructions (1) and (2) would be updated by setReg() to:
2508a4dda33SDimitry Andric       //
2518a4dda33SDimitry Andric       //     ADD_rr %0:gpr(tied-def 0), %2:gpr
2528a4dda33SDimitry Andric       //
2538a4dda33SDimitry Andric       // %2:gpr is not killed at (1), so it is necessary to remove kill flag
2548a4dda33SDimitry Andric       // from I.
255480093f4SDimitry Andric       I->setReg(SrcReg);
2568a4dda33SDimitry Andric       I->setIsKill(false);
2578a4dda33SDimitry Andric     }
258480093f4SDimitry Andric 
259480093f4SDimitry Andric     // The candidate needs to have a unique definition.
2605ffd83dbSDimitry Andric     if (IsAma && MRI->getUniqueVRegDef(I->getReg()))
261480093f4SDimitry Andric       processInst(MRI, I->getParent(), &*I, GVal);
262480093f4SDimitry Andric   }
263480093f4SDimitry Andric }
264480093f4SDimitry Andric 
265480093f4SDimitry Andric // Check to see whether we could do some optimization
266480093f4SDimitry Andric // to attach relocation to downstream dependent instructions.
267480093f4SDimitry Andric // Two kinds of patterns are recognized below:
268480093f4SDimitry Andric // Pattern 1:
269480093f4SDimitry Andric //   %1 = LD_imm64 @"llvm.b:0:4$0:1"  <== patch_imm = 4
270480093f4SDimitry Andric //   %2 = LDD %1, 0  <== this insn will be removed
271480093f4SDimitry Andric //   %3 = ADD_rr %0, %2
272480093f4SDimitry Andric //   %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0
273480093f4SDimitry Andric //   The `%4 = ...` will be transformed to
274480093f4SDimitry Andric //      CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")
275480093f4SDimitry Andric //   and later on, BTF emit phase will translate to
276480093f4SDimitry Andric //      %4 = LDW[32] %0, 4 STW[32] %4, %0, 4
277480093f4SDimitry Andric //   and attach a relocation to it.
278480093f4SDimitry Andric // Pattern 2:
279480093f4SDimitry Andric //    %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5
280480093f4SDimitry Andric //    %16 = LDD %15, 0   <== this insn will be removed
281480093f4SDimitry Andric //    %17 = SRA_rr %14, %16
282480093f4SDimitry Andric //    The `%17 = ...` will be transformed to
283480093f4SDimitry Andric //       %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")
284480093f4SDimitry Andric //    and later on, BTF emit phase will translate to
285480093f4SDimitry Andric //       %r4 = SRA_ri %r4, 63
286480093f4SDimitry Andric void BPFMISimplifyPatchable::processInst(MachineRegisterInfo *MRI,
287480093f4SDimitry Andric     MachineInstr *Inst, MachineOperand *RelocOp, const GlobalValue *GVal) {
288480093f4SDimitry Andric   unsigned Opcode = Inst->getOpcode();
28981ad6265SDimitry Andric   if (isLoadInst(Opcode)) {
29081ad6265SDimitry Andric     SkipInsts.insert(Inst);
29181ad6265SDimitry Andric     return;
29281ad6265SDimitry Andric   }
29381ad6265SDimitry Andric 
294480093f4SDimitry Andric   if (Opcode == BPF::ADD_rr)
295480093f4SDimitry Andric     checkADDrr(MRI, RelocOp, GVal);
296480093f4SDimitry Andric   else if (Opcode == BPF::SLL_rr)
297480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SLL_ri);
298480093f4SDimitry Andric   else if (Opcode == BPF::SRA_rr)
299480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRA_ri);
300480093f4SDimitry Andric   else if (Opcode == BPF::SRL_rr)
301480093f4SDimitry Andric     checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRL_ri);
302480093f4SDimitry Andric }
303480093f4SDimitry Andric 
3040b57cec5SDimitry Andric /// Remove unneeded Load instructions.
3050b57cec5SDimitry Andric bool BPFMISimplifyPatchable::removeLD() {
3060b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
3070b57cec5SDimitry Andric   MachineInstr *ToErase = nullptr;
3080b57cec5SDimitry Andric   bool Changed = false;
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : *MF) {
3110b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
3120b57cec5SDimitry Andric       if (ToErase) {
3130b57cec5SDimitry Andric         ToErase->eraseFromParent();
3140b57cec5SDimitry Andric         ToErase = nullptr;
3150b57cec5SDimitry Andric       }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric       // Ensure the register format is LOAD <reg>, <reg>, 0
31881ad6265SDimitry Andric       if (!isLoadInst(MI.getOpcode()))
31981ad6265SDimitry Andric         continue;
32081ad6265SDimitry Andric 
32181ad6265SDimitry Andric       if (SkipInsts.find(&MI) != SkipInsts.end())
3220b57cec5SDimitry Andric         continue;
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric       if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg())
3250b57cec5SDimitry Andric         continue;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric       if (!MI.getOperand(2).isImm() || MI.getOperand(2).getImm())
3280b57cec5SDimitry Andric         continue;
3290b57cec5SDimitry Andric 
3308bcb0991SDimitry Andric       Register DstReg = MI.getOperand(0).getReg();
3318bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(1).getReg();
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric       MachineInstr *DefInst = MRI->getUniqueVRegDef(SrcReg);
3340b57cec5SDimitry Andric       if (!DefInst)
3350b57cec5SDimitry Andric         continue;
3360b57cec5SDimitry Andric 
3375ffd83dbSDimitry Andric       if (DefInst->getOpcode() != BPF::LD_imm64)
3380b57cec5SDimitry Andric         continue;
3390b57cec5SDimitry Andric 
3405ffd83dbSDimitry Andric       const MachineOperand &MO = DefInst->getOperand(1);
3415ffd83dbSDimitry Andric       if (!MO.isGlobal())
3425ffd83dbSDimitry Andric         continue;
3435ffd83dbSDimitry Andric 
3445ffd83dbSDimitry Andric       const GlobalValue *GVal = MO.getGlobal();
3455ffd83dbSDimitry Andric       auto *GVar = dyn_cast<GlobalVariable>(GVal);
3465ffd83dbSDimitry Andric       if (!GVar)
3475ffd83dbSDimitry Andric         continue;
3485ffd83dbSDimitry Andric 
3495ffd83dbSDimitry Andric       // Global variables representing structure offset or type id.
3505ffd83dbSDimitry Andric       bool IsAma = false;
3515ffd83dbSDimitry Andric       if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr))
3525ffd83dbSDimitry Andric         IsAma = true;
3535ffd83dbSDimitry Andric       else if (!GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
3545ffd83dbSDimitry Andric         continue;
3555ffd83dbSDimitry Andric 
3565ffd83dbSDimitry Andric       processCandidate(MRI, MBB, MI, SrcReg, DstReg, GVal, IsAma);
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric       ToErase = &MI;
3590b57cec5SDimitry Andric       Changed = true;
3600b57cec5SDimitry Andric     }
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   return Changed;
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric } // namespace
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric INITIALIZE_PASS(BPFMISimplifyPatchable, DEBUG_TYPE,
3690b57cec5SDimitry Andric                 "BPF PreEmit SimplifyPatchable", false, false)
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric char BPFMISimplifyPatchable::ID = 0;
3720b57cec5SDimitry Andric FunctionPass *llvm::createBPFMISimplifyPatchablePass() {
3730b57cec5SDimitry Andric   return new BPFMISimplifyPatchable();
3740b57cec5SDimitry Andric }
375