1 //===----- BPFMISimplifyPatchable.cpp - MI Simplify Patchable Insts -------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass targets a subset of instructions like below
10 // ld_imm64 r1, @global
12 // add r3, struct_base_reg, r2
14 // Here @global should represent an AMA (abstruct member access).
15 // Such an access is subject to bpf load time patching. After this pass, the
17 // ld_imm64 r1, @global
18 // add r3, struct_base_reg, r1
20 // Eventually, at BTF output stage, a relocation record will be generated
21 // for ld_imm64 which should be replaced later by bpf loader:
22 // r1 = <calculated field_info>
23 // add r3, struct_base_reg, r1
25 // This pass also removes the intermediate load generated in IR pass for
26 // __builtin_btf_type_id() intrinsic.
28 //===----------------------------------------------------------------------===//
32 #include "BPFInstrInfo.h"
33 #include "BPFTargetMachine.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/IR/GlobalVariable.h"
38 #include "llvm/Support/Debug.h"
43 #define DEBUG_TYPE "bpf-mi-simplify-patchable"
47 struct BPFMISimplifyPatchable
: public MachineFunctionPass
{
50 const BPFInstrInfo
*TII
;
53 BPFMISimplifyPatchable() : MachineFunctionPass(ID
) {
54 initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());
58 std::set
<MachineInstr
*> SkipInsts
;
60 // Initialize class variables.
61 void initialize(MachineFunction
&MFParm
);
63 bool isLoadInst(unsigned Opcode
);
65 void processCandidate(MachineRegisterInfo
*MRI
, MachineBasicBlock
&MBB
,
66 MachineInstr
&MI
, Register
&SrcReg
, Register
&DstReg
,
67 const GlobalValue
*GVal
, bool IsAma
);
68 void processDstReg(MachineRegisterInfo
*MRI
, Register
&DstReg
,
69 Register
&SrcReg
, const GlobalValue
*GVal
,
70 bool doSrcRegProp
, bool IsAma
);
71 void processInst(MachineRegisterInfo
*MRI
, MachineInstr
*Inst
,
72 MachineOperand
*RelocOp
, const GlobalValue
*GVal
);
73 void checkADDrr(MachineRegisterInfo
*MRI
, MachineOperand
*RelocOp
,
74 const GlobalValue
*GVal
);
75 void checkShift(MachineRegisterInfo
*MRI
, MachineBasicBlock
&MBB
,
76 MachineOperand
*RelocOp
, const GlobalValue
*GVal
,
80 // Main entry point for this pass.
81 bool runOnMachineFunction(MachineFunction
&MF
) override
{
82 if (skipFunction(MF
.getFunction()))
90 // Initialize class variables.
91 void BPFMISimplifyPatchable::initialize(MachineFunction
&MFParm
) {
93 TII
= MF
->getSubtarget
<BPFSubtarget
>().getInstrInfo();
94 LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");
97 static bool isST(unsigned Opcode
) {
98 return Opcode
== BPF::STB_imm
|| Opcode
== BPF::STH_imm
||
99 Opcode
== BPF::STW_imm
|| Opcode
== BPF::STD_imm
;
102 static bool isSTX32(unsigned Opcode
) {
103 return Opcode
== BPF::STB32
|| Opcode
== BPF::STH32
|| Opcode
== BPF::STW32
;
106 static bool isSTX64(unsigned Opcode
) {
107 return Opcode
== BPF::STB
|| Opcode
== BPF::STH
|| Opcode
== BPF::STW
||
111 static bool isLDX32(unsigned Opcode
) {
112 return Opcode
== BPF::LDB32
|| Opcode
== BPF::LDH32
|| Opcode
== BPF::LDW32
;
115 static bool isLDX64(unsigned Opcode
) {
116 return Opcode
== BPF::LDB
|| Opcode
== BPF::LDH
|| Opcode
== BPF::LDW
||
120 static bool isLDSX(unsigned Opcode
) {
121 return Opcode
== BPF::LDBSX
|| Opcode
== BPF::LDHSX
|| Opcode
== BPF::LDWSX
;
124 bool BPFMISimplifyPatchable::isLoadInst(unsigned Opcode
) {
125 return isLDX32(Opcode
) || isLDX64(Opcode
) || isLDSX(Opcode
);
128 void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo
*MRI
,
129 MachineOperand
*RelocOp
, const GlobalValue
*GVal
) {
130 const MachineInstr
*Inst
= RelocOp
->getParent();
131 const MachineOperand
*Op1
= &Inst
->getOperand(1);
132 const MachineOperand
*Op2
= &Inst
->getOperand(2);
133 const MachineOperand
*BaseOp
= (RelocOp
== Op1
) ? Op2
: Op1
;
135 // Go through all uses of %1 as in %1 = ADD_rr %2, %3
136 const MachineOperand Op0
= Inst
->getOperand(0);
137 for (MachineOperand
&MO
:
138 llvm::make_early_inc_range(MRI
->use_operands(Op0
.getReg()))) {
139 // The candidate needs to have a unique definition.
140 if (!MRI
->getUniqueVRegDef(MO
.getReg()))
143 MachineInstr
*DefInst
= MO
.getParent();
144 unsigned Opcode
= DefInst
->getOpcode();
146 if (isLDX64(Opcode
) || isLDSX(Opcode
))
147 COREOp
= BPF::CORE_LD64
;
148 else if (isLDX32(Opcode
))
149 COREOp
= BPF::CORE_LD32
;
150 else if (isSTX64(Opcode
) || isSTX32(Opcode
) || isST(Opcode
))
151 COREOp
= BPF::CORE_ST
;
155 // It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.
156 const MachineOperand
&ImmOp
= DefInst
->getOperand(2);
157 if (!ImmOp
.isImm() || ImmOp
.getImm() != 0)
161 // %1 = ADD_rr %2, %3
162 // *(type *)(%2 + 0) = %1
163 if (isSTX64(Opcode
) || isSTX32(Opcode
)) {
164 const MachineOperand
&Opnd
= DefInst
->getOperand(0);
165 if (Opnd
.isReg() && Opnd
.getReg() == MO
.getReg())
169 BuildMI(*DefInst
->getParent(), *DefInst
, DefInst
->getDebugLoc(), TII
->get(COREOp
))
170 .add(DefInst
->getOperand(0)).addImm(Opcode
).add(*BaseOp
)
171 .addGlobalAddress(GVal
);
172 DefInst
->eraseFromParent();
176 void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo
*MRI
,
177 MachineBasicBlock
&MBB
, MachineOperand
*RelocOp
, const GlobalValue
*GVal
,
179 // Relocation operand should be the operand #2.
180 MachineInstr
*Inst
= RelocOp
->getParent();
181 if (RelocOp
!= &Inst
->getOperand(2))
184 BuildMI(MBB
, *Inst
, Inst
->getDebugLoc(), TII
->get(BPF::CORE_SHIFT
))
185 .add(Inst
->getOperand(0)).addImm(Opcode
)
186 .add(Inst
->getOperand(1)).addGlobalAddress(GVal
);
187 Inst
->eraseFromParent();
190 void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo
*MRI
,
191 MachineBasicBlock
&MBB
, MachineInstr
&MI
, Register
&SrcReg
,
192 Register
&DstReg
, const GlobalValue
*GVal
, bool IsAma
) {
193 if (MRI
->getRegClass(DstReg
) == &BPF::GPR32RegClass
) {
195 // We can optimize such a pattern:
196 // %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"
197 // %2:gpr32 = LDW32 %1:gpr, 0
198 // %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32
199 // %4:gpr = ADD_rr %0:gpr, %3:gpr
200 // or similar patterns below for non-alu32 case.
201 auto Begin
= MRI
->use_begin(DstReg
), End
= MRI
->use_end();
203 for (auto I
= Begin
; I
!= End
; I
= NextI
) {
204 NextI
= std::next(I
);
205 if (!MRI
->getUniqueVRegDef(I
->getReg()))
208 unsigned Opcode
= I
->getParent()->getOpcode();
209 if (Opcode
== BPF::SUBREG_TO_REG
) {
210 Register TmpReg
= I
->getParent()->getOperand(0).getReg();
211 processDstReg(MRI
, TmpReg
, DstReg
, GVal
, false, IsAma
);
216 BuildMI(MBB
, MI
, MI
.getDebugLoc(), TII
->get(BPF::COPY
), DstReg
)
217 .addReg(SrcReg
, 0, BPF::sub_32
);
221 // All uses of DstReg replaced by SrcReg
222 processDstReg(MRI
, DstReg
, SrcReg
, GVal
, true, IsAma
);
225 void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo
*MRI
,
226 Register
&DstReg
, Register
&SrcReg
, const GlobalValue
*GVal
,
227 bool doSrcRegProp
, bool IsAma
) {
228 auto Begin
= MRI
->use_begin(DstReg
), End
= MRI
->use_end();
230 for (auto I
= Begin
; I
!= End
; I
= NextI
) {
231 NextI
= std::next(I
);
233 // In situations like below it is not known if usage is a kill
236 // .-> %2:gpr = LD_imm64 @"llvm.t:0:0$0:0"
238 // |`----------------.
239 // | %3:gpr = LDD %2:gpr, 0
240 // | %4:gpr = ADD_rr %0:gpr(tied-def 0), killed %3:gpr <--- (1)
241 // | %5:gpr = LDD killed %4:gpr, 0 ^^^^^^^^^^^^^
242 // | STD killed %5:gpr, %1:gpr, 0 this is I
243 // `----------------.
244 // %6:gpr = LDD %2:gpr, 0
245 // %7:gpr = ADD_rr %0:gpr(tied-def 0), killed %6:gpr <--- (2)
246 // %8:gpr = LDD killed %7:gpr, 0 ^^^^^^^^^^^^^
247 // STD killed %8:gpr, %1:gpr, 0 this is I
249 // Instructions (1) and (2) would be updated by setReg() to:
251 // ADD_rr %0:gpr(tied-def 0), %2:gpr
253 // %2:gpr is not killed at (1), so it is necessary to remove kill flag
259 // The candidate needs to have a unique definition.
260 if (IsAma
&& MRI
->getUniqueVRegDef(I
->getReg()))
261 processInst(MRI
, I
->getParent(), &*I
, GVal
);
265 // Check to see whether we could do some optimization
266 // to attach relocation to downstream dependent instructions.
267 // Two kinds of patterns are recognized below:
269 // %1 = LD_imm64 @"llvm.b:0:4$0:1" <== patch_imm = 4
270 // %2 = LDD %1, 0 <== this insn will be removed
271 // %3 = ADD_rr %0, %2
272 // %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0
273 // The `%4 = ...` will be transformed to
274 // CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")
275 // and later on, BTF emit phase will translate to
276 // %4 = LDW[32] %0, 4 STW[32] %4, %0, 4
277 // and attach a relocation to it.
279 // %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5
280 // %16 = LDD %15, 0 <== this insn will be removed
281 // %17 = SRA_rr %14, %16
282 // The `%17 = ...` will be transformed to
283 // %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")
284 // and later on, BTF emit phase will translate to
285 // %r4 = SRA_ri %r4, 63
286 void BPFMISimplifyPatchable::processInst(MachineRegisterInfo
*MRI
,
287 MachineInstr
*Inst
, MachineOperand
*RelocOp
, const GlobalValue
*GVal
) {
288 unsigned Opcode
= Inst
->getOpcode();
289 if (isLoadInst(Opcode
)) {
290 SkipInsts
.insert(Inst
);
294 if (Opcode
== BPF::ADD_rr
)
295 checkADDrr(MRI
, RelocOp
, GVal
);
296 else if (Opcode
== BPF::SLL_rr
)
297 checkShift(MRI
, *Inst
->getParent(), RelocOp
, GVal
, BPF::SLL_ri
);
298 else if (Opcode
== BPF::SRA_rr
)
299 checkShift(MRI
, *Inst
->getParent(), RelocOp
, GVal
, BPF::SRA_ri
);
300 else if (Opcode
== BPF::SRL_rr
)
301 checkShift(MRI
, *Inst
->getParent(), RelocOp
, GVal
, BPF::SRL_ri
);
304 /// Remove unneeded Load instructions.
305 bool BPFMISimplifyPatchable::removeLD() {
306 MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
307 MachineInstr
*ToErase
= nullptr;
308 bool Changed
= false;
310 for (MachineBasicBlock
&MBB
: *MF
) {
311 for (MachineInstr
&MI
: MBB
) {
313 ToErase
->eraseFromParent();
317 // Ensure the register format is LOAD <reg>, <reg>, 0
318 if (!isLoadInst(MI
.getOpcode()))
321 if (SkipInsts
.find(&MI
) != SkipInsts
.end())
324 if (!MI
.getOperand(0).isReg() || !MI
.getOperand(1).isReg())
327 if (!MI
.getOperand(2).isImm() || MI
.getOperand(2).getImm())
330 Register DstReg
= MI
.getOperand(0).getReg();
331 Register SrcReg
= MI
.getOperand(1).getReg();
333 MachineInstr
*DefInst
= MRI
->getUniqueVRegDef(SrcReg
);
337 if (DefInst
->getOpcode() != BPF::LD_imm64
)
340 const MachineOperand
&MO
= DefInst
->getOperand(1);
344 const GlobalValue
*GVal
= MO
.getGlobal();
345 auto *GVar
= dyn_cast
<GlobalVariable
>(GVal
);
349 // Global variables representing structure offset or type id.
351 if (GVar
->hasAttribute(BPFCoreSharedInfo::AmaAttr
))
353 else if (!GVar
->hasAttribute(BPFCoreSharedInfo::TypeIdAttr
))
356 processCandidate(MRI
, MBB
, MI
, SrcReg
, DstReg
, GVal
, IsAma
);
368 INITIALIZE_PASS(BPFMISimplifyPatchable
, DEBUG_TYPE
,
369 "BPF PreEmit SimplifyPatchable", false, false)
371 char BPFMISimplifyPatchable::ID
= 0;
372 FunctionPass
*llvm::createBPFMISimplifyPatchablePass() {
373 return new BPFMISimplifyPatchable();