WebAssemblyRegNumbering.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===-- WebAssemblyRegNumbering.cpp - Register Numbering ------------------===//
  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. /// \file
  10. /// This file implements a pass which assigns WebAssembly register
  11. /// numbers for CodeGen virtual registers.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  15. #include "Utils/WebAssemblyUtilities.h"
  16. #include "WebAssembly.h"
  17. #include "WebAssemblyMachineFunctionInfo.h"
  18. #include "WebAssemblySubtarget.h"
  19. #include "llvm/ADT/SCCIterator.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineInstrBuilder.h"
  23. #include "llvm/CodeGen/MachineLoopInfo.h"
  24. #include "llvm/CodeGen/MachineRegisterInfo.h"
  25. #include "llvm/CodeGen/Passes.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. using namespace llvm;
  29. #define DEBUG_TYPE "wasm-reg-numbering"
  30. namespace {
  31. class WebAssemblyRegNumbering final : public MachineFunctionPass {
  32. StringRef getPassName() const override {
  33. return "WebAssembly Register Numbering";
  34. }
  35. void getAnalysisUsage(AnalysisUsage &AU) const override {
  36. AU.setPreservesCFG();
  37. MachineFunctionPass::getAnalysisUsage(AU);
  38. }
  39. bool runOnMachineFunction(MachineFunction &MF) override;
  40. public:
  41. static char ID; // Pass identification, replacement for typeid
  42. WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
  43. };
  44. } // end anonymous namespace
  45. char WebAssemblyRegNumbering::ID = 0;
  46. INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE,
  47. "Assigns WebAssembly register numbers for virtual registers",
  48. false, false)
  49. FunctionPass *llvm::createWebAssemblyRegNumbering() {
  50. return new WebAssemblyRegNumbering();
  51. }
  52. bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
  53. LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n"
  54. "********** Function: "
  55. << MF.getName() << '\n');
  56. WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
  57. MachineRegisterInfo &MRI = MF.getRegInfo();
  58. MFI.initWARegs(MRI);
  59. // WebAssembly argument registers are in the same index space as local
  60. // variables. Assign the numbers for them first.
  61. MachineBasicBlock &EntryMBB = MF.front();
  62. for (MachineInstr &MI : EntryMBB) {
  63. if (!WebAssembly::isArgument(MI.getOpcode()))
  64. break;
  65. int64_t Imm = MI.getOperand(1).getImm();
  66. LLVM_DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg()
  67. << " -> WAReg " << Imm << "\n");
  68. MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
  69. }
  70. // Then assign regular WebAssembly registers for all remaining used
  71. // virtual registers. TODO: Consider sorting the registers by frequency of
  72. // use, to maximize usage of small immediate fields.
  73. unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
  74. unsigned NumStackRegs = 0;
  75. // Start the numbering for locals after the arg regs
  76. unsigned CurReg = MFI.getParams().size();
  77. for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
  78. Register VReg = Register::index2VirtReg(VRegIdx);
  79. // Skip unused registers.
  80. if (MRI.use_empty(VReg))
  81. continue;
  82. // Handle stackified registers.
  83. if (MFI.isVRegStackified(VReg)) {
  84. LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
  85. << (INT32_MIN | NumStackRegs) << "\n");
  86. MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
  87. continue;
  88. }
  89. if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
  90. LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
  91. MFI.setWAReg(VReg, CurReg++);
  92. }
  93. }
  94. return true;
  95. }