BPFMIChecking.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //===-------------- BPFMIChecking.cpp - MI Checking Legality -------------===//
  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 checking to signal errors for certain illegal usages at
  10. // MachineInstruction layer. Specially, the result of XADD{32,64} insn should
  11. // not be used. The pass is done at the PreEmit pass right before the
  12. // machine code is emitted at which point the register liveness information
  13. // is still available.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "BPF.h"
  17. #include "BPFInstrInfo.h"
  18. #include "BPFTargetMachine.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/Support/Debug.h"
  23. using namespace llvm;
  24. #define DEBUG_TYPE "bpf-mi-checking"
  25. namespace {
  26. struct BPFMIPreEmitChecking : public MachineFunctionPass {
  27. static char ID;
  28. MachineFunction *MF;
  29. const TargetRegisterInfo *TRI;
  30. BPFMIPreEmitChecking() : MachineFunctionPass(ID) {
  31. initializeBPFMIPreEmitCheckingPass(*PassRegistry::getPassRegistry());
  32. }
  33. private:
  34. // Initialize class variables.
  35. void initialize(MachineFunction &MFParm);
  36. bool processAtomicInsts();
  37. public:
  38. // Main entry point for this pass.
  39. bool runOnMachineFunction(MachineFunction &MF) override {
  40. if (!skipFunction(MF.getFunction())) {
  41. initialize(MF);
  42. return processAtomicInsts();
  43. }
  44. return false;
  45. }
  46. };
  47. // Initialize class variables.
  48. void BPFMIPreEmitChecking::initialize(MachineFunction &MFParm) {
  49. MF = &MFParm;
  50. TRI = MF->getSubtarget<BPFSubtarget>().getRegisterInfo();
  51. LLVM_DEBUG(dbgs() << "*** BPF PreEmit checking pass ***\n\n");
  52. }
  53. // Make sure all Defs of XADD are dead, meaning any result of XADD insn is not
  54. // used.
  55. //
  56. // NOTE: BPF backend hasn't enabled sub-register liveness track, so when the
  57. // source and destination operands of XADD are GPR32, there is no sub-register
  58. // dead info. If we rely on the generic MachineInstr::allDefsAreDead, then we
  59. // will raise false alarm on GPR32 Def.
  60. //
  61. // To support GPR32 Def, ideally we could just enable sub-registr liveness track
  62. // on BPF backend, then allDefsAreDead could work on GPR32 Def. This requires
  63. // implementing TargetSubtargetInfo::enableSubRegLiveness on BPF.
  64. //
  65. // However, sub-register liveness tracking module inside LLVM is actually
  66. // designed for the situation where one register could be split into more than
  67. // one sub-registers for which case each sub-register could have their own
  68. // liveness and kill one of them doesn't kill others. So, tracking liveness for
  69. // each make sense.
  70. //
  71. // For BPF, each 64-bit register could only have one 32-bit sub-register. This
  72. // is exactly the case which LLVM think brings no benefits for doing
  73. // sub-register tracking, because the live range of sub-register must always
  74. // equal to its parent register, therefore liveness tracking is disabled even
  75. // the back-end has implemented enableSubRegLiveness. The detailed information
  76. // is at r232695:
  77. //
  78. // Author: Matthias Braun <matze@braunis.de>
  79. // Date: Thu Mar 19 00:21:58 2015 +0000
  80. // Do not track subregister liveness when it brings no benefits
  81. //
  82. // Hence, for BPF, we enhance MachineInstr::allDefsAreDead. Given the solo
  83. // sub-register always has the same liveness as its parent register, LLVM is
  84. // already attaching a implicit 64-bit register Def whenever the there is
  85. // a sub-register Def. The liveness of the implicit 64-bit Def is available.
  86. // For example, for "lock *(u32 *)(r0 + 4) += w9", the MachineOperand info could
  87. // be:
  88. //
  89. // $w9 = XADDW32 killed $r0, 4, $w9(tied-def 0),
  90. // implicit killed $r9, implicit-def dead $r9
  91. //
  92. // Even though w9 is not marked as Dead, the parent register r9 is marked as
  93. // Dead correctly, and it is safe to use such information or our purpose.
  94. static bool hasLiveDefs(const MachineInstr &MI, const TargetRegisterInfo *TRI) {
  95. const MCRegisterClass *GPR64RegClass =
  96. &BPFMCRegisterClasses[BPF::GPRRegClassID];
  97. std::vector<unsigned> GPR32LiveDefs;
  98. std::vector<unsigned> GPR64DeadDefs;
  99. for (const MachineOperand &MO : MI.operands()) {
  100. bool RegIsGPR64;
  101. if (!MO.isReg() || MO.isUse())
  102. continue;
  103. RegIsGPR64 = GPR64RegClass->contains(MO.getReg());
  104. if (!MO.isDead()) {
  105. // It is a GPR64 live Def, we are sure it is live. */
  106. if (RegIsGPR64)
  107. return true;
  108. // It is a GPR32 live Def, we are unsure whether it is really dead due to
  109. // no sub-register liveness tracking. Push it to vector for deferred
  110. // check.
  111. GPR32LiveDefs.push_back(MO.getReg());
  112. continue;
  113. }
  114. // Record any GPR64 dead Def as some unmarked GPR32 could be alias of its
  115. // low 32-bit.
  116. if (RegIsGPR64)
  117. GPR64DeadDefs.push_back(MO.getReg());
  118. }
  119. // No GPR32 live Def, safe to return false.
  120. if (GPR32LiveDefs.empty())
  121. return false;
  122. // No GPR64 dead Def, so all those GPR32 live Def can't have alias, therefore
  123. // must be truely live, safe to return true.
  124. if (GPR64DeadDefs.empty())
  125. return true;
  126. // Otherwise, return true if any aliased SuperReg of GPR32 is not dead.
  127. for (auto I : GPR32LiveDefs)
  128. for (MCSuperRegIterator SR(I, TRI); SR.isValid(); ++SR)
  129. if (!llvm::is_contained(GPR64DeadDefs, *SR))
  130. return true;
  131. return false;
  132. }
  133. bool BPFMIPreEmitChecking::processAtomicInsts() {
  134. for (MachineBasicBlock &MBB : *MF) {
  135. for (MachineInstr &MI : MBB) {
  136. if (MI.getOpcode() != BPF::XADDW &&
  137. MI.getOpcode() != BPF::XADDD &&
  138. MI.getOpcode() != BPF::XADDW32)
  139. continue;
  140. LLVM_DEBUG(MI.dump());
  141. if (hasLiveDefs(MI, TRI)) {
  142. DebugLoc Empty;
  143. const DebugLoc &DL = MI.getDebugLoc();
  144. if (DL != Empty)
  145. report_fatal_error(Twine("line ") + std::to_string(DL.getLine()) +
  146. ": Invalid usage of the XADD return value", false);
  147. else
  148. report_fatal_error("Invalid usage of the XADD return value", false);
  149. }
  150. }
  151. }
  152. // Check return values of atomic_fetch_and_{add,and,or,xor}.
  153. // If the return is not used, the atomic_fetch_and_<op> instruction
  154. // is replaced with atomic_<op> instruction.
  155. MachineInstr *ToErase = nullptr;
  156. bool Changed = false;
  157. const BPFInstrInfo *TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
  158. for (MachineBasicBlock &MBB : *MF) {
  159. for (MachineInstr &MI : MBB) {
  160. if (ToErase) {
  161. ToErase->eraseFromParent();
  162. ToErase = nullptr;
  163. }
  164. if (MI.getOpcode() != BPF::XFADDW32 && MI.getOpcode() != BPF::XFADDD &&
  165. MI.getOpcode() != BPF::XFANDW32 && MI.getOpcode() != BPF::XFANDD &&
  166. MI.getOpcode() != BPF::XFXORW32 && MI.getOpcode() != BPF::XFXORD &&
  167. MI.getOpcode() != BPF::XFORW32 && MI.getOpcode() != BPF::XFORD)
  168. continue;
  169. if (hasLiveDefs(MI, TRI))
  170. continue;
  171. LLVM_DEBUG(dbgs() << "Transforming "; MI.dump());
  172. unsigned newOpcode;
  173. switch (MI.getOpcode()) {
  174. case BPF::XFADDW32:
  175. newOpcode = BPF::XADDW32;
  176. break;
  177. case BPF::XFADDD:
  178. newOpcode = BPF::XADDD;
  179. break;
  180. case BPF::XFANDW32:
  181. newOpcode = BPF::XANDW32;
  182. break;
  183. case BPF::XFANDD:
  184. newOpcode = BPF::XANDD;
  185. break;
  186. case BPF::XFXORW32:
  187. newOpcode = BPF::XXORW32;
  188. break;
  189. case BPF::XFXORD:
  190. newOpcode = BPF::XXORD;
  191. break;
  192. case BPF::XFORW32:
  193. newOpcode = BPF::XORW32;
  194. break;
  195. case BPF::XFORD:
  196. newOpcode = BPF::XORD;
  197. break;
  198. default:
  199. llvm_unreachable("Incorrect Atomic Instruction Opcode");
  200. }
  201. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(newOpcode))
  202. .add(MI.getOperand(0))
  203. .add(MI.getOperand(1))
  204. .add(MI.getOperand(2))
  205. .add(MI.getOperand(3));
  206. ToErase = &MI;
  207. Changed = true;
  208. }
  209. }
  210. return Changed;
  211. }
  212. } // end default namespace
  213. INITIALIZE_PASS(BPFMIPreEmitChecking, "bpf-mi-pemit-checking",
  214. "BPF PreEmit Checking", false, false)
  215. char BPFMIPreEmitChecking::ID = 0;
  216. FunctionPass* llvm::createBPFMIPreEmitCheckingPass()
  217. {
  218. return new BPFMIPreEmitChecking();
  219. }