LLVM  10.0.0svn
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"
18 #include "WebAssemblySubtarget.h"
19 #include "WebAssemblyUtilities.h"
20 #include "llvm/ADT/SCCIterator.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/Support/Debug.h"
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "wasm-reg-numbering"
32 
33 namespace {
34 class WebAssemblyRegNumbering final : public MachineFunctionPass {
35  StringRef getPassName() const override {
36  return "WebAssembly Register Numbering";
37  }
38 
39  void getAnalysisUsage(AnalysisUsage &AU) const override {
40  AU.setPreservesCFG();
42  }
43 
44  bool runOnMachineFunction(MachineFunction &MF) override;
45 
46 public:
47  static char ID; // Pass identification, replacement for typeid
48  WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
49 };
50 } // end anonymous namespace
51 
53 INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE,
54  "Assigns WebAssembly register numbers for virtual registers",
55  false, false)
56 
58  return new WebAssemblyRegNumbering();
59 }
60 
61 bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
62  LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n"
63  "********** Function: "
64  << MF.getName() << '\n');
65 
68 
69  MFI.initWARegs();
70 
71  // WebAssembly argument registers are in the same index space as local
72  // variables. Assign the numbers for them first.
73  MachineBasicBlock &EntryMBB = MF.front();
74  for (MachineInstr &MI : EntryMBB) {
75  if (!WebAssembly::isArgument(MI.getOpcode()))
76  break;
77 
78  int64_t Imm = MI.getOperand(1).getImm();
79  LLVM_DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg()
80  << " -> WAReg " << Imm << "\n");
81  MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
82  }
83 
84  // Then assign regular WebAssembly registers for all remaining used
85  // virtual registers. TODO: Consider sorting the registers by frequency of
86  // use, to maximize usage of small immediate fields.
87  unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
88  unsigned NumStackRegs = 0;
89  // Start the numbering for locals after the arg regs
90  unsigned CurReg = MFI.getParams().size();
91  for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
92  unsigned VReg = Register::index2VirtReg(VRegIdx);
93  // Skip unused registers.
94  if (MRI.use_empty(VReg))
95  continue;
96  // Handle stackified registers.
97  if (MFI.isVRegStackified(VReg)) {
98  LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
99  << (INT32_MIN | NumStackRegs) << "\n");
100  MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
101  continue;
102  }
103  if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
104  LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
105  MFI.setWAReg(VReg, CurReg++);
106  }
107  }
108 
109  return true;
110 }
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
bool isArgument(unsigned Opc)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:83
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
FunctionPass * createWebAssemblyRegNumbering()
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
This file contains the declaration of the WebAssembly-specific utility functions. ...
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
const MachineBasicBlock & front() const
This file declares the WebAssembly-specific subclass of TargetSubtarget.
#define DEBUG_TYPE
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
This file declares WebAssembly-specific per-machine-function information.
INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE, "Assigns WebAssembly register numbers for virtual registers", false, false) FunctionPass *llvm
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122