LLVM 24.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"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/Analysis.h"
26#include "llvm/Support/Debug.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "wasm-reg-numbering"
31
32namespace {
33class WebAssemblyRegNumberingLegacy final : public MachineFunctionPass {
34 StringRef getPassName() const override {
35 return "WebAssembly Register Numbering";
36 }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
41 }
42
43 bool runOnMachineFunction(MachineFunction &MF) override;
44
45public:
46 static char ID; // Pass identification, replacement for typeid
47 WebAssemblyRegNumberingLegacy() : MachineFunctionPass(ID) {}
48};
49} // end anonymous namespace
50
51char WebAssemblyRegNumberingLegacy::ID = 0;
52INITIALIZE_PASS(WebAssemblyRegNumberingLegacy, DEBUG_TYPE,
53 "Assigns WebAssembly register numbers for virtual registers",
54 false, false)
55
57 return new WebAssemblyRegNumberingLegacy();
58}
59
60static bool regNumbering(MachineFunction &MF) {
61 LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n"
62 "********** Function: "
63 << MF.getName() << '\n');
64
67
68 MFI.initWARegs(MRI);
69
70 // WebAssembly argument registers are in the same index space as local
71 // variables. Assign the numbers for them first.
72 MachineBasicBlock &EntryMBB = MF.front();
73 for (MachineInstr &MI : EntryMBB) {
74 if (!WebAssembly::isArgument(MI.getOpcode()))
75 break;
76
77 int64_t Imm = MI.getOperand(1).getImm();
78 LLVM_DEBUG(dbgs() << "Arg VReg " << printReg(MI.getOperand(0).getReg())
79 << " -> WAReg " << Imm << "\n");
80 MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
81 }
82
83 // Then assign regular WebAssembly registers for all remaining used
84 // virtual registers. TODO: Consider sorting the registers by frequency of
85 // use, to maximize usage of small immediate fields.
86 unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
87 unsigned NumStackRegs = 0;
88 // Start the numbering for locals after the arg regs
89 unsigned CurReg = MFI.getParams().size();
90 for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
91 Register VReg = Register::index2VirtReg(VRegIdx);
92 // Skip unused registers.
93 if (MRI.use_empty(VReg))
94 continue;
95 // Handle stackified registers.
96 if (MFI.isVRegStackified(VReg)) {
97 LLVM_DEBUG(dbgs() << "VReg " << printReg(VReg) << " -> WAReg "
98 << (INT32_MIN | NumStackRegs) << "\n");
99 MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
100 continue;
101 }
102 if (MFI.getWAReg(VReg) == WebAssembly::UnusedReg) {
103 LLVM_DEBUG(dbgs() << "VReg " << printReg(VReg) << " -> WAReg " << CurReg
104 << "\n");
105 MFI.setWAReg(VReg, CurReg++);
106 }
107 }
108
109 return true;
110}
111
112bool WebAssemblyRegNumberingLegacy::runOnMachineFunction(MachineFunction &MF) {
113 return regNumbering(MF);
114}
115
116PreservedAnalyses
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
static bool regNumbering(MachineFunction &MF)
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
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.
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
void initWARegs(MachineRegisterInfo &MRI)
void setWAReg(Register VReg, unsigned WAReg)
const std::vector< MVT > & getParams() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Pass manager infrastructure for declaring and invalidating analyses.
bool isArgument(unsigned Opc)
static const unsigned UnusedReg
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createWebAssemblyRegNumberingLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI 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.