WebAssemblyReplacePhysRegs.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===-- WebAssemblyReplacePhysRegs.cpp - Replace phys regs with virt regs -===//
  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 that replaces physical registers with
  11. /// virtual registers.
  12. ///
  13. /// LLVM expects certain physical registers, such as a stack pointer. However,
  14. /// WebAssembly doesn't actually have such physical registers. This pass is run
  15. /// once LLVM no longer needs these registers, and replaces them with virtual
  16. /// registers, so they can participate in register stackifying and coloring in
  17. /// the normal way.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  21. #include "WebAssembly.h"
  22. #include "WebAssemblyMachineFunctionInfo.h"
  23. #include "WebAssemblySubtarget.h"
  24. #include "llvm/CodeGen/MachineFunctionPass.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/Passes.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "wasm-replace-phys-regs"
  31. namespace {
  32. class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
  33. public:
  34. static char ID; // Pass identification, replacement for typeid
  35. WebAssemblyReplacePhysRegs() : MachineFunctionPass(ID) {}
  36. private:
  37. StringRef getPassName() const override {
  38. return "WebAssembly Replace Physical Registers";
  39. }
  40. void getAnalysisUsage(AnalysisUsage &AU) const override {
  41. AU.setPreservesCFG();
  42. MachineFunctionPass::getAnalysisUsage(AU);
  43. }
  44. bool runOnMachineFunction(MachineFunction &MF) override;
  45. };
  46. } // end anonymous namespace
  47. char WebAssemblyReplacePhysRegs::ID = 0;
  48. INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE,
  49. "Replace physical registers with virtual registers", false,
  50. false)
  51. FunctionPass *llvm::createWebAssemblyReplacePhysRegs() {
  52. return new WebAssemblyReplacePhysRegs();
  53. }
  54. bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
  55. LLVM_DEBUG({
  56. dbgs() << "********** Replace Physical Registers **********\n"
  57. << "********** Function: " << MF.getName() << '\n';
  58. });
  59. MachineRegisterInfo &MRI = MF.getRegInfo();
  60. auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
  61. bool Changed = false;
  62. assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
  63. "LiveIntervals shouldn't be active yet!");
  64. for (unsigned PReg = WebAssembly::NoRegister + 1;
  65. PReg < WebAssembly::NUM_TARGET_REGS; ++PReg) {
  66. // Skip fake registers that are never used explicitly.
  67. if (PReg == WebAssembly::VALUE_STACK || PReg == WebAssembly::ARGUMENTS)
  68. continue;
  69. // Replace explicit uses of the physical register with a virtual register.
  70. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PReg);
  71. unsigned VReg = WebAssembly::NoRegister;
  72. for (MachineOperand &MO :
  73. llvm::make_early_inc_range(MRI.reg_operands(PReg))) {
  74. if (!MO.isImplicit()) {
  75. if (VReg == WebAssembly::NoRegister) {
  76. VReg = MRI.createVirtualRegister(RC);
  77. if (PReg == TRI.getFrameRegister(MF)) {
  78. auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
  79. assert(!FI->isFrameBaseVirtual());
  80. FI->setFrameBaseVreg(VReg);
  81. LLVM_DEBUG({
  82. dbgs() << "replacing preg " << PReg << " with " << VReg << " ("
  83. << Register::virtReg2Index(VReg) << ")\n";
  84. });
  85. }
  86. }
  87. MO.setReg(VReg);
  88. Changed = true;
  89. }
  90. }
  91. }
  92. return Changed;
  93. }