LLVM 20.0.0git
WebAssemblyRegNumbering.cpp
Go to the documentation of this file.
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
16#include "WebAssembly.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/Support/Debug.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "wasm-reg-numbering"
28
29namespace {
30class WebAssemblyRegNumbering final : public MachineFunctionPass {
31 StringRef getPassName() const override {
32 return "WebAssembly Register Numbering";
33 }
34
35 void getAnalysisUsage(AnalysisUsage &AU) const override {
36 AU.setPreservesCFG();
38 }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42public:
43 static char ID; // Pass identification, replacement for typeid
44 WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
45};
46} // end anonymous namespace
47
48char WebAssemblyRegNumbering::ID = 0;
49INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE,
50 "Assigns WebAssembly register numbers for virtual registers",
51 false, false)
52
54 return new WebAssemblyRegNumbering();
55}
56
57bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
58 LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n"
59 "********** Function: "
60 << MF.getName() << '\n');
61
64
65 MFI.initWARegs(MRI);
66
67 // WebAssembly argument registers are in the same index space as local
68 // variables. Assign the numbers for them first.
69 MachineBasicBlock &EntryMBB = MF.front();
70 for (MachineInstr &MI : EntryMBB) {
71 if (!WebAssembly::isArgument(MI.getOpcode()))
72 break;
73
74 int64_t Imm = MI.getOperand(1).getImm();
75 LLVM_DEBUG(dbgs() << "Arg VReg " << printReg(MI.getOperand(0).getReg())
76 << " -> WAReg " << Imm << "\n");
77 MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
78 }
79
80 // Then assign regular WebAssembly registers for all remaining used
81 // virtual registers. TODO: Consider sorting the registers by frequency of
82 // use, to maximize usage of small immediate fields.
83 unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
84 unsigned NumStackRegs = 0;
85 // Start the numbering for locals after the arg regs
86 unsigned CurReg = MFI.getParams().size();
87 for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
88 Register VReg = Register::index2VirtReg(VRegIdx);
89 // Skip unused registers.
90 if (MRI.use_empty(VReg))
91 continue;
92 // Handle stackified registers.
93 if (MFI.isVRegStackified(VReg)) {
94 LLVM_DEBUG(dbgs() << "VReg " << printReg(VReg) << " -> WAReg "
95 << (INT32_MIN | NumStackRegs) << "\n");
96 MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
97 continue;
98 }
99 if (MFI.getWAReg(VReg) == WebAssembly::UnusedReg) {
100 LLVM_DEBUG(dbgs() << "VReg " << printReg(VReg) << " -> WAReg " << CurReg
101 << "\n");
102 MFI.setWAReg(VReg, CurReg++);
103 }
104 }
105
106 return true;
107}
unsigned const MachineRegisterInfo * MRI
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
#define DEBUG_TYPE
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:84
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
unsigned getWAReg(unsigned VReg) const
void initWARegs(MachineRegisterInfo &MRI)
void setWAReg(unsigned VReg, unsigned WAReg)
const std::vector< MVT > & getParams() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
bool isArgument(unsigned Opc)
static const unsigned UnusedReg
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createWebAssemblyRegNumbering()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.