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
21#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
22#include "WebAssembly.h"
23#include "WebAssemblyMachineFunctionInfo.h"
24#include "WebAssemblySubtarget.h"
25#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachinePassManager.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/Passes.h"
30#include "llvm/IR/Analysis.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34
35#define DEBUG_TYPE "wasm-replace-phys-regs"
36
37namespace {
38class WebAssemblyReplacePhysRegsLegacy final : public MachineFunctionPass {
39public:
40 static char ID; // Pass identification, replacement for typeid
41 WebAssemblyReplacePhysRegsLegacy() : MachineFunctionPass(ID) {}
42
43private:
44 StringRef getPassName() const override {
45 return "WebAssembly Replace Physical Registers";
46 }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.setPreservesCFG();
50 MachineFunctionPass::getAnalysisUsage(AU);
51 }
52
53 bool runOnMachineFunction(MachineFunction &MF) override;
54};
55} // end anonymous namespace
56
57char WebAssemblyReplacePhysRegsLegacy::ID = 0;
58INITIALIZE_PASS(WebAssemblyReplacePhysRegsLegacy, DEBUG_TYPE,
59 "Replace physical registers with virtual registers", false,
60 false)
61
62FunctionPass *llvm::createWebAssemblyReplacePhysRegsLegacyPass() {
63 return new WebAssemblyReplacePhysRegsLegacy();
64}
65
66static bool replacePhysRegs(MachineFunction &MF) {
67 LLVM_DEBUG({
68 dbgs() << "********** Replace Physical Registers **********\n"
69 << "********** Function: " << MF.getName() << '\n';
70 });
71
72 MachineRegisterInfo &MRI = MF.getRegInfo();
73 auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
74 bool Changed = false;
75
76 for (unsigned PReg = WebAssembly::NoRegister + 1;
77 PReg < WebAssembly::NUM_TARGET_REGS; ++PReg) {
78 // Skip fake registers that are never used explicitly.
79 if (PReg == WebAssembly::VALUE_STACK || PReg == WebAssembly::ARGUMENTS)
80 continue;
81
82 // Replace explicit uses of the physical register with a virtual register.
83 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg: PReg);
84 unsigned VReg = WebAssembly::NoRegister;
85 for (MachineOperand &MO :
86 llvm::make_early_inc_range(Range: MRI.reg_operands(Reg: PReg))) {
87 if (!MO.isImplicit()) {
88 if (VReg == WebAssembly::NoRegister) {
89 VReg = MRI.createVirtualRegister(RegClass: RC);
90 if (PReg == TRI.getFrameRegister(MF)) {
91 auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
92 assert(!FI->isFrameBaseVirtual());
93 FI->setFrameBaseVreg(VReg);
94 LLVM_DEBUG({
95 dbgs() << "replacing preg " << PReg << " with " << VReg << " ("
96 << Register(VReg).virtRegIndex() << ")\n";
97 });
98 }
99 }
100 MO.setReg(VReg);
101 Changed = true;
102 }
103 }
104 }
105
106 return Changed;
107}
108
109bool WebAssemblyReplacePhysRegsLegacy::runOnMachineFunction(
110 MachineFunction &MF) {
111 assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
112 "LiveIntervals shouldn't be active yet!");
113
114 return replacePhysRegs(MF);
115}
116
117PreservedAnalyses
118WebAssemblyReplacePhysRegsPass::run(MachineFunction &MF,
119 MachineFunctionAnalysisManager &MFAM) {
120 return replacePhysRegs(MF) ? getMachineFunctionPassPreservedAnalyses()
121 .preserveSet<CFGAnalyses>()
122 : PreservedAnalyses::all();
123}
124