BPFInstrInfo.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //===-- BPFInstrInfo.cpp - BPF Instruction Information ----------*- C++ -*-===//
  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 file contains the BPF implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "BPFInstrInfo.h"
  13. #include "BPF.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineInstrBuilder.h"
  17. #include "llvm/IR/DebugLoc.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include <cassert>
  20. #include <iterator>
  21. #define GET_INSTRINFO_CTOR_DTOR
  22. #include "BPFGenInstrInfo.inc"
  23. using namespace llvm;
  24. BPFInstrInfo::BPFInstrInfo()
  25. : BPFGenInstrInfo(BPF::ADJCALLSTACKDOWN, BPF::ADJCALLSTACKUP) {}
  26. void BPFInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
  27. MachineBasicBlock::iterator I,
  28. const DebugLoc &DL, MCRegister DestReg,
  29. MCRegister SrcReg, bool KillSrc) const {
  30. if (BPF::GPRRegClass.contains(DestReg, SrcReg))
  31. BuildMI(MBB, I, DL, get(BPF::MOV_rr), DestReg)
  32. .addReg(SrcReg, getKillRegState(KillSrc));
  33. else if (BPF::GPR32RegClass.contains(DestReg, SrcReg))
  34. BuildMI(MBB, I, DL, get(BPF::MOV_rr_32), DestReg)
  35. .addReg(SrcReg, getKillRegState(KillSrc));
  36. else
  37. llvm_unreachable("Impossible reg-to-reg copy");
  38. }
  39. void BPFInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
  40. Register DstReg = MI->getOperand(0).getReg();
  41. Register SrcReg = MI->getOperand(1).getReg();
  42. uint64_t CopyLen = MI->getOperand(2).getImm();
  43. uint64_t Alignment = MI->getOperand(3).getImm();
  44. Register ScratchReg = MI->getOperand(4).getReg();
  45. MachineBasicBlock *BB = MI->getParent();
  46. DebugLoc dl = MI->getDebugLoc();
  47. unsigned LdOpc, StOpc;
  48. switch (Alignment) {
  49. case 1:
  50. LdOpc = BPF::LDB;
  51. StOpc = BPF::STB;
  52. break;
  53. case 2:
  54. LdOpc = BPF::LDH;
  55. StOpc = BPF::STH;
  56. break;
  57. case 4:
  58. LdOpc = BPF::LDW;
  59. StOpc = BPF::STW;
  60. break;
  61. case 8:
  62. LdOpc = BPF::LDD;
  63. StOpc = BPF::STD;
  64. break;
  65. default:
  66. llvm_unreachable("unsupported memcpy alignment");
  67. }
  68. unsigned IterationNum = CopyLen >> Log2_64(Alignment);
  69. for(unsigned I = 0; I < IterationNum; ++I) {
  70. BuildMI(*BB, MI, dl, get(LdOpc))
  71. .addReg(ScratchReg, RegState::Define).addReg(SrcReg)
  72. .addImm(I * Alignment);
  73. BuildMI(*BB, MI, dl, get(StOpc))
  74. .addReg(ScratchReg, RegState::Kill).addReg(DstReg)
  75. .addImm(I * Alignment);
  76. }
  77. unsigned BytesLeft = CopyLen & (Alignment - 1);
  78. unsigned Offset = IterationNum * Alignment;
  79. bool Hanging4Byte = BytesLeft & 0x4;
  80. bool Hanging2Byte = BytesLeft & 0x2;
  81. bool Hanging1Byte = BytesLeft & 0x1;
  82. if (Hanging4Byte) {
  83. BuildMI(*BB, MI, dl, get(BPF::LDW))
  84. .addReg(ScratchReg, RegState::Define).addReg(SrcReg).addImm(Offset);
  85. BuildMI(*BB, MI, dl, get(BPF::STW))
  86. .addReg(ScratchReg, RegState::Kill).addReg(DstReg).addImm(Offset);
  87. Offset += 4;
  88. }
  89. if (Hanging2Byte) {
  90. BuildMI(*BB, MI, dl, get(BPF::LDH))
  91. .addReg(ScratchReg, RegState::Define).addReg(SrcReg).addImm(Offset);
  92. BuildMI(*BB, MI, dl, get(BPF::STH))
  93. .addReg(ScratchReg, RegState::Kill).addReg(DstReg).addImm(Offset);
  94. Offset += 2;
  95. }
  96. if (Hanging1Byte) {
  97. BuildMI(*BB, MI, dl, get(BPF::LDB))
  98. .addReg(ScratchReg, RegState::Define).addReg(SrcReg).addImm(Offset);
  99. BuildMI(*BB, MI, dl, get(BPF::STB))
  100. .addReg(ScratchReg, RegState::Kill).addReg(DstReg).addImm(Offset);
  101. }
  102. BB->erase(MI);
  103. }
  104. bool BPFInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
  105. if (MI.getOpcode() == BPF::MEMCPY) {
  106. expandMEMCPY(MI);
  107. return true;
  108. }
  109. return false;
  110. }
  111. void BPFInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
  112. MachineBasicBlock::iterator I,
  113. Register SrcReg, bool IsKill, int FI,
  114. const TargetRegisterClass *RC,
  115. const TargetRegisterInfo *TRI) const {
  116. DebugLoc DL;
  117. if (I != MBB.end())
  118. DL = I->getDebugLoc();
  119. if (RC == &BPF::GPRRegClass)
  120. BuildMI(MBB, I, DL, get(BPF::STD))
  121. .addReg(SrcReg, getKillRegState(IsKill))
  122. .addFrameIndex(FI)
  123. .addImm(0);
  124. else if (RC == &BPF::GPR32RegClass)
  125. BuildMI(MBB, I, DL, get(BPF::STW32))
  126. .addReg(SrcReg, getKillRegState(IsKill))
  127. .addFrameIndex(FI)
  128. .addImm(0);
  129. else
  130. llvm_unreachable("Can't store this register to stack slot");
  131. }
  132. void BPFInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
  133. MachineBasicBlock::iterator I,
  134. Register DestReg, int FI,
  135. const TargetRegisterClass *RC,
  136. const TargetRegisterInfo *TRI) const {
  137. DebugLoc DL;
  138. if (I != MBB.end())
  139. DL = I->getDebugLoc();
  140. if (RC == &BPF::GPRRegClass)
  141. BuildMI(MBB, I, DL, get(BPF::LDD), DestReg).addFrameIndex(FI).addImm(0);
  142. else if (RC == &BPF::GPR32RegClass)
  143. BuildMI(MBB, I, DL, get(BPF::LDW32), DestReg).addFrameIndex(FI).addImm(0);
  144. else
  145. llvm_unreachable("Can't load this register from stack slot");
  146. }
  147. bool BPFInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
  148. MachineBasicBlock *&TBB,
  149. MachineBasicBlock *&FBB,
  150. SmallVectorImpl<MachineOperand> &Cond,
  151. bool AllowModify) const {
  152. // Start from the bottom of the block and work up, examining the
  153. // terminator instructions.
  154. MachineBasicBlock::iterator I = MBB.end();
  155. while (I != MBB.begin()) {
  156. --I;
  157. if (I->isDebugInstr())
  158. continue;
  159. // Working from the bottom, when we see a non-terminator
  160. // instruction, we're done.
  161. if (!isUnpredicatedTerminator(*I))
  162. break;
  163. // A terminator that isn't a branch can't easily be handled
  164. // by this analysis.
  165. if (!I->isBranch())
  166. return true;
  167. // Handle unconditional branches.
  168. if (I->getOpcode() == BPF::JMP) {
  169. if (!AllowModify) {
  170. TBB = I->getOperand(0).getMBB();
  171. continue;
  172. }
  173. // If the block has any instructions after a J, delete them.
  174. while (std::next(I) != MBB.end())
  175. std::next(I)->eraseFromParent();
  176. Cond.clear();
  177. FBB = nullptr;
  178. // Delete the J if it's equivalent to a fall-through.
  179. if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
  180. TBB = nullptr;
  181. I->eraseFromParent();
  182. I = MBB.end();
  183. continue;
  184. }
  185. // TBB is used to indicate the unconditinal destination.
  186. TBB = I->getOperand(0).getMBB();
  187. continue;
  188. }
  189. // Cannot handle conditional branches
  190. return true;
  191. }
  192. return false;
  193. }
  194. unsigned BPFInstrInfo::insertBranch(MachineBasicBlock &MBB,
  195. MachineBasicBlock *TBB,
  196. MachineBasicBlock *FBB,
  197. ArrayRef<MachineOperand> Cond,
  198. const DebugLoc &DL,
  199. int *BytesAdded) const {
  200. assert(!BytesAdded && "code size not handled");
  201. // Shouldn't be a fall through.
  202. assert(TBB && "insertBranch must not be told to insert a fallthrough");
  203. if (Cond.empty()) {
  204. // Unconditional branch
  205. assert(!FBB && "Unconditional branch with multiple successors!");
  206. BuildMI(&MBB, DL, get(BPF::JMP)).addMBB(TBB);
  207. return 1;
  208. }
  209. llvm_unreachable("Unexpected conditional branch");
  210. }
  211. unsigned BPFInstrInfo::removeBranch(MachineBasicBlock &MBB,
  212. int *BytesRemoved) const {
  213. assert(!BytesRemoved && "code size not handled");
  214. MachineBasicBlock::iterator I = MBB.end();
  215. unsigned Count = 0;
  216. while (I != MBB.begin()) {
  217. --I;
  218. if (I->isDebugInstr())
  219. continue;
  220. if (I->getOpcode() != BPF::JMP)
  221. break;
  222. // Remove the branch.
  223. I->eraseFromParent();
  224. I = MBB.end();
  225. ++Count;
  226. }
  227. return Count;
  228. }