BPFMISimplifyPatchable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //===----- BPFMISimplifyPatchable.cpp - MI Simplify Patchable Insts -------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass targets a subset of instructions like below
  10. // ld_imm64 r1, @global
  11. // ldd r2, r1, 0
  12. // add r3, struct_base_reg, r2
  13. //
  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
  16. // code becomes
  17. // ld_imm64 r1, @global
  18. // add r3, struct_base_reg, r1
  19. //
  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
  24. //
  25. // This pass also removes the intermediate load generated in IR pass for
  26. // __builtin_btf_type_id() intrinsic.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #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/Support/Debug.h"
  37. #include <set>
  38. using namespace llvm;
  39. #define DEBUG_TYPE "bpf-mi-simplify-patchable"
  40. namespace {
  41. struct BPFMISimplifyPatchable : public MachineFunctionPass {
  42. static char ID;
  43. const BPFInstrInfo *TII;
  44. MachineFunction *MF;
  45. BPFMISimplifyPatchable() : MachineFunctionPass(ID) {
  46. initializeBPFMISimplifyPatchablePass(*PassRegistry::getPassRegistry());
  47. }
  48. private:
  49. std::set<MachineInstr *> SkipInsts;
  50. // Initialize class variables.
  51. void initialize(MachineFunction &MFParm);
  52. bool isLoadInst(unsigned Opcode);
  53. bool removeLD();
  54. void processCandidate(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
  55. MachineInstr &MI, Register &SrcReg, Register &DstReg,
  56. const GlobalValue *GVal, bool IsAma);
  57. void processDstReg(MachineRegisterInfo *MRI, Register &DstReg,
  58. Register &SrcReg, const GlobalValue *GVal,
  59. bool doSrcRegProp, bool IsAma);
  60. void processInst(MachineRegisterInfo *MRI, MachineInstr *Inst,
  61. MachineOperand *RelocOp, const GlobalValue *GVal);
  62. void checkADDrr(MachineRegisterInfo *MRI, MachineOperand *RelocOp,
  63. const GlobalValue *GVal);
  64. void checkShift(MachineRegisterInfo *MRI, MachineBasicBlock &MBB,
  65. MachineOperand *RelocOp, const GlobalValue *GVal,
  66. unsigned Opcode);
  67. public:
  68. // Main entry point for this pass.
  69. bool runOnMachineFunction(MachineFunction &MF) override {
  70. if (skipFunction(MF.getFunction()))
  71. return false;
  72. initialize(MF);
  73. return removeLD();
  74. }
  75. };
  76. // Initialize class variables.
  77. void BPFMISimplifyPatchable::initialize(MachineFunction &MFParm) {
  78. MF = &MFParm;
  79. TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
  80. LLVM_DEBUG(dbgs() << "*** BPF simplify patchable insts pass ***\n\n");
  81. }
  82. bool BPFMISimplifyPatchable::isLoadInst(unsigned Opcode) {
  83. return Opcode == BPF::LDD || Opcode == BPF::LDW || Opcode == BPF::LDH ||
  84. Opcode == BPF::LDB || Opcode == BPF::LDW32 || Opcode == BPF::LDH32 ||
  85. Opcode == BPF::LDB32;
  86. }
  87. void BPFMISimplifyPatchable::checkADDrr(MachineRegisterInfo *MRI,
  88. MachineOperand *RelocOp, const GlobalValue *GVal) {
  89. const MachineInstr *Inst = RelocOp->getParent();
  90. const MachineOperand *Op1 = &Inst->getOperand(1);
  91. const MachineOperand *Op2 = &Inst->getOperand(2);
  92. const MachineOperand *BaseOp = (RelocOp == Op1) ? Op2 : Op1;
  93. // Go through all uses of %1 as in %1 = ADD_rr %2, %3
  94. const MachineOperand Op0 = Inst->getOperand(0);
  95. for (MachineOperand &MO :
  96. llvm::make_early_inc_range(MRI->use_operands(Op0.getReg()))) {
  97. // The candidate needs to have a unique definition.
  98. if (!MRI->getUniqueVRegDef(MO.getReg()))
  99. continue;
  100. MachineInstr *DefInst = MO.getParent();
  101. unsigned Opcode = DefInst->getOpcode();
  102. unsigned COREOp;
  103. if (Opcode == BPF::LDB || Opcode == BPF::LDH || Opcode == BPF::LDW ||
  104. Opcode == BPF::LDD || Opcode == BPF::STB || Opcode == BPF::STH ||
  105. Opcode == BPF::STW || Opcode == BPF::STD)
  106. COREOp = BPF::CORE_MEM;
  107. else if (Opcode == BPF::LDB32 || Opcode == BPF::LDH32 ||
  108. Opcode == BPF::LDW32 || Opcode == BPF::STB32 ||
  109. Opcode == BPF::STH32 || Opcode == BPF::STW32)
  110. COREOp = BPF::CORE_ALU32_MEM;
  111. else
  112. continue;
  113. // It must be a form of %2 = *(type *)(%1 + 0) or *(type *)(%1 + 0) = %2.
  114. const MachineOperand &ImmOp = DefInst->getOperand(2);
  115. if (!ImmOp.isImm() || ImmOp.getImm() != 0)
  116. continue;
  117. // Reject the form:
  118. // %1 = ADD_rr %2, %3
  119. // *(type *)(%2 + 0) = %1
  120. if (Opcode == BPF::STB || Opcode == BPF::STH || Opcode == BPF::STW ||
  121. Opcode == BPF::STD || Opcode == BPF::STB32 || Opcode == BPF::STH32 ||
  122. Opcode == BPF::STW32) {
  123. const MachineOperand &Opnd = DefInst->getOperand(0);
  124. if (Opnd.isReg() && Opnd.getReg() == MO.getReg())
  125. continue;
  126. }
  127. BuildMI(*DefInst->getParent(), *DefInst, DefInst->getDebugLoc(), TII->get(COREOp))
  128. .add(DefInst->getOperand(0)).addImm(Opcode).add(*BaseOp)
  129. .addGlobalAddress(GVal);
  130. DefInst->eraseFromParent();
  131. }
  132. }
  133. void BPFMISimplifyPatchable::checkShift(MachineRegisterInfo *MRI,
  134. MachineBasicBlock &MBB, MachineOperand *RelocOp, const GlobalValue *GVal,
  135. unsigned Opcode) {
  136. // Relocation operand should be the operand #2.
  137. MachineInstr *Inst = RelocOp->getParent();
  138. if (RelocOp != &Inst->getOperand(2))
  139. return;
  140. BuildMI(MBB, *Inst, Inst->getDebugLoc(), TII->get(BPF::CORE_SHIFT))
  141. .add(Inst->getOperand(0)).addImm(Opcode)
  142. .add(Inst->getOperand(1)).addGlobalAddress(GVal);
  143. Inst->eraseFromParent();
  144. }
  145. void BPFMISimplifyPatchable::processCandidate(MachineRegisterInfo *MRI,
  146. MachineBasicBlock &MBB, MachineInstr &MI, Register &SrcReg,
  147. Register &DstReg, const GlobalValue *GVal, bool IsAma) {
  148. if (MRI->getRegClass(DstReg) == &BPF::GPR32RegClass) {
  149. if (IsAma) {
  150. // We can optimize such a pattern:
  151. // %1:gpr = LD_imm64 @"llvm.s:0:4$0:2"
  152. // %2:gpr32 = LDW32 %1:gpr, 0
  153. // %3:gpr = SUBREG_TO_REG 0, %2:gpr32, %subreg.sub_32
  154. // %4:gpr = ADD_rr %0:gpr, %3:gpr
  155. // or similar patterns below for non-alu32 case.
  156. auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
  157. decltype(End) NextI;
  158. for (auto I = Begin; I != End; I = NextI) {
  159. NextI = std::next(I);
  160. if (!MRI->getUniqueVRegDef(I->getReg()))
  161. continue;
  162. unsigned Opcode = I->getParent()->getOpcode();
  163. if (Opcode == BPF::SUBREG_TO_REG) {
  164. Register TmpReg = I->getParent()->getOperand(0).getReg();
  165. processDstReg(MRI, TmpReg, DstReg, GVal, false, IsAma);
  166. }
  167. }
  168. }
  169. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::COPY), DstReg)
  170. .addReg(SrcReg, 0, BPF::sub_32);
  171. return;
  172. }
  173. // All uses of DstReg replaced by SrcReg
  174. processDstReg(MRI, DstReg, SrcReg, GVal, true, IsAma);
  175. }
  176. void BPFMISimplifyPatchable::processDstReg(MachineRegisterInfo *MRI,
  177. Register &DstReg, Register &SrcReg, const GlobalValue *GVal,
  178. bool doSrcRegProp, bool IsAma) {
  179. auto Begin = MRI->use_begin(DstReg), End = MRI->use_end();
  180. decltype(End) NextI;
  181. for (auto I = Begin; I != End; I = NextI) {
  182. NextI = std::next(I);
  183. if (doSrcRegProp)
  184. I->setReg(SrcReg);
  185. // The candidate needs to have a unique definition.
  186. if (IsAma && MRI->getUniqueVRegDef(I->getReg()))
  187. processInst(MRI, I->getParent(), &*I, GVal);
  188. }
  189. }
  190. // Check to see whether we could do some optimization
  191. // to attach relocation to downstream dependent instructions.
  192. // Two kinds of patterns are recognized below:
  193. // Pattern 1:
  194. // %1 = LD_imm64 @"llvm.b:0:4$0:1" <== patch_imm = 4
  195. // %2 = LDD %1, 0 <== this insn will be removed
  196. // %3 = ADD_rr %0, %2
  197. // %4 = LDW[32] %3, 0 OR STW[32] %4, %3, 0
  198. // The `%4 = ...` will be transformed to
  199. // CORE_[ALU32_]MEM(%4, mem_opcode, %0, @"llvm.b:0:4$0:1")
  200. // and later on, BTF emit phase will translate to
  201. // %4 = LDW[32] %0, 4 STW[32] %4, %0, 4
  202. // and attach a relocation to it.
  203. // Pattern 2:
  204. // %15 = LD_imm64 @"llvm.t:5:63$0:2" <== relocation type 5
  205. // %16 = LDD %15, 0 <== this insn will be removed
  206. // %17 = SRA_rr %14, %16
  207. // The `%17 = ...` will be transformed to
  208. // %17 = CORE_SHIFT(SRA_ri, %14, @"llvm.t:5:63$0:2")
  209. // and later on, BTF emit phase will translate to
  210. // %r4 = SRA_ri %r4, 63
  211. void BPFMISimplifyPatchable::processInst(MachineRegisterInfo *MRI,
  212. MachineInstr *Inst, MachineOperand *RelocOp, const GlobalValue *GVal) {
  213. unsigned Opcode = Inst->getOpcode();
  214. if (isLoadInst(Opcode)) {
  215. SkipInsts.insert(Inst);
  216. return;
  217. }
  218. if (Opcode == BPF::ADD_rr)
  219. checkADDrr(MRI, RelocOp, GVal);
  220. else if (Opcode == BPF::SLL_rr)
  221. checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SLL_ri);
  222. else if (Opcode == BPF::SRA_rr)
  223. checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRA_ri);
  224. else if (Opcode == BPF::SRL_rr)
  225. checkShift(MRI, *Inst->getParent(), RelocOp, GVal, BPF::SRL_ri);
  226. }
  227. /// Remove unneeded Load instructions.
  228. bool BPFMISimplifyPatchable::removeLD() {
  229. MachineRegisterInfo *MRI = &MF->getRegInfo();
  230. MachineInstr *ToErase = nullptr;
  231. bool Changed = false;
  232. for (MachineBasicBlock &MBB : *MF) {
  233. for (MachineInstr &MI : MBB) {
  234. if (ToErase) {
  235. ToErase->eraseFromParent();
  236. ToErase = nullptr;
  237. }
  238. // Ensure the register format is LOAD <reg>, <reg>, 0
  239. if (!isLoadInst(MI.getOpcode()))
  240. continue;
  241. if (SkipInsts.find(&MI) != SkipInsts.end())
  242. continue;
  243. if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg())
  244. continue;
  245. if (!MI.getOperand(2).isImm() || MI.getOperand(2).getImm())
  246. continue;
  247. Register DstReg = MI.getOperand(0).getReg();
  248. Register SrcReg = MI.getOperand(1).getReg();
  249. MachineInstr *DefInst = MRI->getUniqueVRegDef(SrcReg);
  250. if (!DefInst)
  251. continue;
  252. if (DefInst->getOpcode() != BPF::LD_imm64)
  253. continue;
  254. const MachineOperand &MO = DefInst->getOperand(1);
  255. if (!MO.isGlobal())
  256. continue;
  257. const GlobalValue *GVal = MO.getGlobal();
  258. auto *GVar = dyn_cast<GlobalVariable>(GVal);
  259. if (!GVar)
  260. continue;
  261. // Global variables representing structure offset or type id.
  262. bool IsAma = false;
  263. if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr))
  264. IsAma = true;
  265. else if (!GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
  266. continue;
  267. processCandidate(MRI, MBB, MI, SrcReg, DstReg, GVal, IsAma);
  268. ToErase = &MI;
  269. Changed = true;
  270. }
  271. }
  272. return Changed;
  273. }
  274. } // namespace
  275. INITIALIZE_PASS(BPFMISimplifyPatchable, DEBUG_TYPE,
  276. "BPF PreEmit SimplifyPatchable", false, false)
  277. char BPFMISimplifyPatchable::ID = 0;
  278. FunctionPass *llvm::createBPFMISimplifyPatchablePass() {
  279. return new BPFMISimplifyPatchable();
  280. }