BPFMIPeephole.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. //===-------------- BPFMIPeephole.cpp - MI Peephole Cleanups -------------===//
  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 performs peephole optimizations to cleanup ugly code sequences at
  10. // MachineInstruction layer.
  11. //
  12. // Currently, there are two optimizations implemented:
  13. // - One pre-RA MachineSSA pass to eliminate type promotion sequences, those
  14. // zero extend 32-bit subregisters to 64-bit registers, if the compiler
  15. // could prove the subregisters is defined by 32-bit operations in which
  16. // case the upper half of the underlying 64-bit registers were zeroed
  17. // implicitly.
  18. //
  19. // - One post-RA PreEmit pass to do final cleanup on some redundant
  20. // instructions generated due to bad RA on subregister.
  21. //===----------------------------------------------------------------------===//
  22. #include "BPF.h"
  23. #include "BPFInstrInfo.h"
  24. #include "BPFTargetMachine.h"
  25. #include "llvm/ADT/Statistic.h"
  26. #include "llvm/CodeGen/MachineInstrBuilder.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/Support/Debug.h"
  29. #include <set>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "bpf-mi-zext-elim"
  32. STATISTIC(ZExtElemNum, "Number of zero extension shifts eliminated");
  33. namespace {
  34. struct BPFMIPeephole : public MachineFunctionPass {
  35. static char ID;
  36. const BPFInstrInfo *TII;
  37. MachineFunction *MF;
  38. MachineRegisterInfo *MRI;
  39. BPFMIPeephole() : MachineFunctionPass(ID) {
  40. initializeBPFMIPeepholePass(*PassRegistry::getPassRegistry());
  41. }
  42. private:
  43. // Initialize class variables.
  44. void initialize(MachineFunction &MFParm);
  45. bool isCopyFrom32Def(MachineInstr *CopyMI);
  46. bool isInsnFrom32Def(MachineInstr *DefInsn);
  47. bool isPhiFrom32Def(MachineInstr *MovMI);
  48. bool isMovFrom32Def(MachineInstr *MovMI);
  49. bool eliminateZExtSeq();
  50. bool eliminateZExt();
  51. std::set<MachineInstr *> PhiInsns;
  52. public:
  53. // Main entry point for this pass.
  54. bool runOnMachineFunction(MachineFunction &MF) override {
  55. if (skipFunction(MF.getFunction()))
  56. return false;
  57. initialize(MF);
  58. // First try to eliminate (zext, lshift, rshift) and then
  59. // try to eliminate zext.
  60. bool ZExtSeqExist, ZExtExist;
  61. ZExtSeqExist = eliminateZExtSeq();
  62. ZExtExist = eliminateZExt();
  63. return ZExtSeqExist || ZExtExist;
  64. }
  65. };
  66. // Initialize class variables.
  67. void BPFMIPeephole::initialize(MachineFunction &MFParm) {
  68. MF = &MFParm;
  69. MRI = &MF->getRegInfo();
  70. TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
  71. LLVM_DEBUG(dbgs() << "*** BPF MachineSSA ZEXT Elim peephole pass ***\n\n");
  72. }
  73. bool BPFMIPeephole::isCopyFrom32Def(MachineInstr *CopyMI)
  74. {
  75. MachineOperand &opnd = CopyMI->getOperand(1);
  76. if (!opnd.isReg())
  77. return false;
  78. // Return false if getting value from a 32bit physical register.
  79. // Most likely, this physical register is aliased to
  80. // function call return value or current function parameters.
  81. Register Reg = opnd.getReg();
  82. if (!Register::isVirtualRegister(Reg))
  83. return false;
  84. if (MRI->getRegClass(Reg) == &BPF::GPRRegClass)
  85. return false;
  86. MachineInstr *DefInsn = MRI->getVRegDef(Reg);
  87. if (!isInsnFrom32Def(DefInsn))
  88. return false;
  89. return true;
  90. }
  91. bool BPFMIPeephole::isPhiFrom32Def(MachineInstr *PhiMI)
  92. {
  93. for (unsigned i = 1, e = PhiMI->getNumOperands(); i < e; i += 2) {
  94. MachineOperand &opnd = PhiMI->getOperand(i);
  95. if (!opnd.isReg())
  96. return false;
  97. MachineInstr *PhiDef = MRI->getVRegDef(opnd.getReg());
  98. if (!PhiDef)
  99. return false;
  100. if (PhiDef->isPHI()) {
  101. if (PhiInsns.find(PhiDef) != PhiInsns.end())
  102. return false;
  103. PhiInsns.insert(PhiDef);
  104. if (!isPhiFrom32Def(PhiDef))
  105. return false;
  106. }
  107. if (PhiDef->getOpcode() == BPF::COPY && !isCopyFrom32Def(PhiDef))
  108. return false;
  109. }
  110. return true;
  111. }
  112. // The \p DefInsn instruction defines a virtual register.
  113. bool BPFMIPeephole::isInsnFrom32Def(MachineInstr *DefInsn)
  114. {
  115. if (!DefInsn)
  116. return false;
  117. if (DefInsn->isPHI()) {
  118. if (PhiInsns.find(DefInsn) != PhiInsns.end())
  119. return false;
  120. PhiInsns.insert(DefInsn);
  121. if (!isPhiFrom32Def(DefInsn))
  122. return false;
  123. } else if (DefInsn->getOpcode() == BPF::COPY) {
  124. if (!isCopyFrom32Def(DefInsn))
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool BPFMIPeephole::isMovFrom32Def(MachineInstr *MovMI)
  130. {
  131. MachineInstr *DefInsn = MRI->getVRegDef(MovMI->getOperand(1).getReg());
  132. LLVM_DEBUG(dbgs() << " Def of Mov Src:");
  133. LLVM_DEBUG(DefInsn->dump());
  134. PhiInsns.clear();
  135. if (!isInsnFrom32Def(DefInsn))
  136. return false;
  137. LLVM_DEBUG(dbgs() << " One ZExt elim sequence identified.\n");
  138. return true;
  139. }
  140. bool BPFMIPeephole::eliminateZExtSeq() {
  141. MachineInstr* ToErase = nullptr;
  142. bool Eliminated = false;
  143. for (MachineBasicBlock &MBB : *MF) {
  144. for (MachineInstr &MI : MBB) {
  145. // If the previous instruction was marked for elimination, remove it now.
  146. if (ToErase) {
  147. ToErase->eraseFromParent();
  148. ToErase = nullptr;
  149. }
  150. // Eliminate the 32-bit to 64-bit zero extension sequence when possible.
  151. //
  152. // MOV_32_64 rB, wA
  153. // SLL_ri rB, rB, 32
  154. // SRL_ri rB, rB, 32
  155. if (MI.getOpcode() == BPF::SRL_ri &&
  156. MI.getOperand(2).getImm() == 32) {
  157. Register DstReg = MI.getOperand(0).getReg();
  158. Register ShfReg = MI.getOperand(1).getReg();
  159. MachineInstr *SllMI = MRI->getVRegDef(ShfReg);
  160. LLVM_DEBUG(dbgs() << "Starting SRL found:");
  161. LLVM_DEBUG(MI.dump());
  162. if (!SllMI ||
  163. SllMI->isPHI() ||
  164. SllMI->getOpcode() != BPF::SLL_ri ||
  165. SllMI->getOperand(2).getImm() != 32)
  166. continue;
  167. LLVM_DEBUG(dbgs() << " SLL found:");
  168. LLVM_DEBUG(SllMI->dump());
  169. MachineInstr *MovMI = MRI->getVRegDef(SllMI->getOperand(1).getReg());
  170. if (!MovMI ||
  171. MovMI->isPHI() ||
  172. MovMI->getOpcode() != BPF::MOV_32_64)
  173. continue;
  174. LLVM_DEBUG(dbgs() << " Type cast Mov found:");
  175. LLVM_DEBUG(MovMI->dump());
  176. Register SubReg = MovMI->getOperand(1).getReg();
  177. if (!isMovFrom32Def(MovMI)) {
  178. LLVM_DEBUG(dbgs()
  179. << " One ZExt elim sequence failed qualifying elim.\n");
  180. continue;
  181. }
  182. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::SUBREG_TO_REG), DstReg)
  183. .addImm(0).addReg(SubReg).addImm(BPF::sub_32);
  184. SllMI->eraseFromParent();
  185. MovMI->eraseFromParent();
  186. // MI is the right shift, we can't erase it in it's own iteration.
  187. // Mark it to ToErase, and erase in the next iteration.
  188. ToErase = &MI;
  189. ZExtElemNum++;
  190. Eliminated = true;
  191. }
  192. }
  193. }
  194. return Eliminated;
  195. }
  196. bool BPFMIPeephole::eliminateZExt() {
  197. MachineInstr* ToErase = nullptr;
  198. bool Eliminated = false;
  199. for (MachineBasicBlock &MBB : *MF) {
  200. for (MachineInstr &MI : MBB) {
  201. // If the previous instruction was marked for elimination, remove it now.
  202. if (ToErase) {
  203. ToErase->eraseFromParent();
  204. ToErase = nullptr;
  205. }
  206. if (MI.getOpcode() != BPF::MOV_32_64)
  207. continue;
  208. // Eliminate MOV_32_64 if possible.
  209. // MOV_32_64 rA, wB
  210. //
  211. // If wB has been zero extended, replace it with a SUBREG_TO_REG.
  212. // This is to workaround BPF programs where pkt->{data, data_end}
  213. // is encoded as u32, but actually the verifier populates them
  214. // as 64bit pointer. The MOV_32_64 will zero out the top 32 bits.
  215. LLVM_DEBUG(dbgs() << "Candidate MOV_32_64 instruction:");
  216. LLVM_DEBUG(MI.dump());
  217. if (!isMovFrom32Def(&MI))
  218. continue;
  219. LLVM_DEBUG(dbgs() << "Removing the MOV_32_64 instruction\n");
  220. Register dst = MI.getOperand(0).getReg();
  221. Register src = MI.getOperand(1).getReg();
  222. // Build a SUBREG_TO_REG instruction.
  223. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::SUBREG_TO_REG), dst)
  224. .addImm(0).addReg(src).addImm(BPF::sub_32);
  225. ToErase = &MI;
  226. Eliminated = true;
  227. }
  228. }
  229. return Eliminated;
  230. }
  231. } // end default namespace
  232. INITIALIZE_PASS(BPFMIPeephole, DEBUG_TYPE,
  233. "BPF MachineSSA Peephole Optimization For ZEXT Eliminate",
  234. false, false)
  235. char BPFMIPeephole::ID = 0;
  236. FunctionPass* llvm::createBPFMIPeepholePass() { return new BPFMIPeephole(); }
  237. STATISTIC(RedundantMovElemNum, "Number of redundant moves eliminated");
  238. namespace {
  239. struct BPFMIPreEmitPeephole : public MachineFunctionPass {
  240. static char ID;
  241. MachineFunction *MF;
  242. const TargetRegisterInfo *TRI;
  243. BPFMIPreEmitPeephole() : MachineFunctionPass(ID) {
  244. initializeBPFMIPreEmitPeepholePass(*PassRegistry::getPassRegistry());
  245. }
  246. private:
  247. // Initialize class variables.
  248. void initialize(MachineFunction &MFParm);
  249. bool eliminateRedundantMov();
  250. public:
  251. // Main entry point for this pass.
  252. bool runOnMachineFunction(MachineFunction &MF) override {
  253. if (skipFunction(MF.getFunction()))
  254. return false;
  255. initialize(MF);
  256. return eliminateRedundantMov();
  257. }
  258. };
  259. // Initialize class variables.
  260. void BPFMIPreEmitPeephole::initialize(MachineFunction &MFParm) {
  261. MF = &MFParm;
  262. TRI = MF->getSubtarget<BPFSubtarget>().getRegisterInfo();
  263. LLVM_DEBUG(dbgs() << "*** BPF PreEmit peephole pass ***\n\n");
  264. }
  265. bool BPFMIPreEmitPeephole::eliminateRedundantMov() {
  266. MachineInstr* ToErase = nullptr;
  267. bool Eliminated = false;
  268. for (MachineBasicBlock &MBB : *MF) {
  269. for (MachineInstr &MI : MBB) {
  270. // If the previous instruction was marked for elimination, remove it now.
  271. if (ToErase) {
  272. LLVM_DEBUG(dbgs() << " Redundant Mov Eliminated:");
  273. LLVM_DEBUG(ToErase->dump());
  274. ToErase->eraseFromParent();
  275. ToErase = nullptr;
  276. }
  277. // Eliminate identical move:
  278. //
  279. // MOV rA, rA
  280. //
  281. // Note that we cannot remove
  282. // MOV_32_64 rA, wA
  283. // MOV_rr_32 wA, wA
  284. // as these two instructions having side effects, zeroing out
  285. // top 32 bits of rA.
  286. unsigned Opcode = MI.getOpcode();
  287. if (Opcode == BPF::MOV_rr) {
  288. Register dst = MI.getOperand(0).getReg();
  289. Register src = MI.getOperand(1).getReg();
  290. if (dst != src)
  291. continue;
  292. ToErase = &MI;
  293. RedundantMovElemNum++;
  294. Eliminated = true;
  295. }
  296. }
  297. }
  298. return Eliminated;
  299. }
  300. } // end default namespace
  301. INITIALIZE_PASS(BPFMIPreEmitPeephole, "bpf-mi-pemit-peephole",
  302. "BPF PreEmit Peephole Optimization", false, false)
  303. char BPFMIPreEmitPeephole::ID = 0;
  304. FunctionPass* llvm::createBPFMIPreEmitPeepholePass()
  305. {
  306. return new BPFMIPreEmitPeephole();
  307. }
  308. STATISTIC(TruncElemNum, "Number of truncation eliminated");
  309. namespace {
  310. struct BPFMIPeepholeTruncElim : public MachineFunctionPass {
  311. static char ID;
  312. const BPFInstrInfo *TII;
  313. MachineFunction *MF;
  314. MachineRegisterInfo *MRI;
  315. BPFMIPeepholeTruncElim() : MachineFunctionPass(ID) {
  316. initializeBPFMIPeepholeTruncElimPass(*PassRegistry::getPassRegistry());
  317. }
  318. private:
  319. // Initialize class variables.
  320. void initialize(MachineFunction &MFParm);
  321. bool eliminateTruncSeq();
  322. public:
  323. // Main entry point for this pass.
  324. bool runOnMachineFunction(MachineFunction &MF) override {
  325. if (skipFunction(MF.getFunction()))
  326. return false;
  327. initialize(MF);
  328. return eliminateTruncSeq();
  329. }
  330. };
  331. static bool TruncSizeCompatible(int TruncSize, unsigned opcode)
  332. {
  333. if (TruncSize == 1)
  334. return opcode == BPF::LDB || opcode == BPF::LDB32;
  335. if (TruncSize == 2)
  336. return opcode == BPF::LDH || opcode == BPF::LDH32;
  337. if (TruncSize == 4)
  338. return opcode == BPF::LDW || opcode == BPF::LDW32;
  339. return false;
  340. }
  341. // Initialize class variables.
  342. void BPFMIPeepholeTruncElim::initialize(MachineFunction &MFParm) {
  343. MF = &MFParm;
  344. MRI = &MF->getRegInfo();
  345. TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
  346. LLVM_DEBUG(dbgs() << "*** BPF MachineSSA TRUNC Elim peephole pass ***\n\n");
  347. }
  348. // Reg truncating is often the result of 8/16/32bit->64bit or
  349. // 8/16bit->32bit conversion. If the reg value is loaded with
  350. // masked byte width, the AND operation can be removed since
  351. // BPF LOAD already has zero extension.
  352. //
  353. // This also solved a correctness issue.
  354. // In BPF socket-related program, e.g., __sk_buff->{data, data_end}
  355. // are 32-bit registers, but later on, kernel verifier will rewrite
  356. // it with 64-bit value. Therefore, truncating the value after the
  357. // load will result in incorrect code.
  358. bool BPFMIPeepholeTruncElim::eliminateTruncSeq() {
  359. MachineInstr* ToErase = nullptr;
  360. bool Eliminated = false;
  361. for (MachineBasicBlock &MBB : *MF) {
  362. for (MachineInstr &MI : MBB) {
  363. // The second insn to remove if the eliminate candidate is a pair.
  364. MachineInstr *MI2 = nullptr;
  365. Register DstReg, SrcReg;
  366. MachineInstr *DefMI;
  367. int TruncSize = -1;
  368. // If the previous instruction was marked for elimination, remove it now.
  369. if (ToErase) {
  370. ToErase->eraseFromParent();
  371. ToErase = nullptr;
  372. }
  373. // AND A, 0xFFFFFFFF will be turned into SLL/SRL pair due to immediate
  374. // for BPF ANDI is i32, and this case only happens on ALU64.
  375. if (MI.getOpcode() == BPF::SRL_ri &&
  376. MI.getOperand(2).getImm() == 32) {
  377. SrcReg = MI.getOperand(1).getReg();
  378. if (!MRI->hasOneNonDBGUse(SrcReg))
  379. continue;
  380. MI2 = MRI->getVRegDef(SrcReg);
  381. DstReg = MI.getOperand(0).getReg();
  382. if (!MI2 ||
  383. MI2->getOpcode() != BPF::SLL_ri ||
  384. MI2->getOperand(2).getImm() != 32)
  385. continue;
  386. // Update SrcReg.
  387. SrcReg = MI2->getOperand(1).getReg();
  388. DefMI = MRI->getVRegDef(SrcReg);
  389. if (DefMI)
  390. TruncSize = 4;
  391. } else if (MI.getOpcode() == BPF::AND_ri ||
  392. MI.getOpcode() == BPF::AND_ri_32) {
  393. SrcReg = MI.getOperand(1).getReg();
  394. DstReg = MI.getOperand(0).getReg();
  395. DefMI = MRI->getVRegDef(SrcReg);
  396. if (!DefMI)
  397. continue;
  398. int64_t imm = MI.getOperand(2).getImm();
  399. if (imm == 0xff)
  400. TruncSize = 1;
  401. else if (imm == 0xffff)
  402. TruncSize = 2;
  403. }
  404. if (TruncSize == -1)
  405. continue;
  406. // The definition is PHI node, check all inputs.
  407. if (DefMI->isPHI()) {
  408. bool CheckFail = false;
  409. for (unsigned i = 1, e = DefMI->getNumOperands(); i < e; i += 2) {
  410. MachineOperand &opnd = DefMI->getOperand(i);
  411. if (!opnd.isReg()) {
  412. CheckFail = true;
  413. break;
  414. }
  415. MachineInstr *PhiDef = MRI->getVRegDef(opnd.getReg());
  416. if (!PhiDef || PhiDef->isPHI() ||
  417. !TruncSizeCompatible(TruncSize, PhiDef->getOpcode())) {
  418. CheckFail = true;
  419. break;
  420. }
  421. }
  422. if (CheckFail)
  423. continue;
  424. } else if (!TruncSizeCompatible(TruncSize, DefMI->getOpcode())) {
  425. continue;
  426. }
  427. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::MOV_rr), DstReg)
  428. .addReg(SrcReg);
  429. if (MI2)
  430. MI2->eraseFromParent();
  431. // Mark it to ToErase, and erase in the next iteration.
  432. ToErase = &MI;
  433. TruncElemNum++;
  434. Eliminated = true;
  435. }
  436. }
  437. return Eliminated;
  438. }
  439. } // end default namespace
  440. INITIALIZE_PASS(BPFMIPeepholeTruncElim, "bpf-mi-trunc-elim",
  441. "BPF MachineSSA Peephole Optimization For TRUNC Eliminate",
  442. false, false)
  443. char BPFMIPeepholeTruncElim::ID = 0;
  444. FunctionPass* llvm::createBPFMIPeepholeTruncElimPass()
  445. {
  446. return new BPFMIPeepholeTruncElim();
  447. }