BPFMIChecking.cpp 8.2 KB

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