LLVM  4.0.0
WebAssemblyRegNumbering.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyRegNumbering.cpp - Register Numbering ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements a pass which assigns WebAssembly register
12 /// numbers for CodeGen virtual registers.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "WebAssembly.h"
19 #include "WebAssemblySubtarget.h"
20 #include "WebAssemblyUtilities.h"
21 #include "llvm/ADT/SCCIterator.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/Support/Debug.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "wasm-reg-numbering"
33 
34 namespace {
35 class WebAssemblyRegNumbering final : public MachineFunctionPass {
36  StringRef getPassName() const override {
37  return "WebAssembly Register Numbering";
38  }
39 
40  void getAnalysisUsage(AnalysisUsage &AU) const override {
41  AU.setPreservesCFG();
43  }
44 
45  bool runOnMachineFunction(MachineFunction &MF) override;
46 
47 public:
48  static char ID; // Pass identification, replacement for typeid
49  WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
50 };
51 } // end anonymous namespace
52 
55  return new WebAssemblyRegNumbering();
56 }
57 
58 bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
59  DEBUG(dbgs() << "********** Register Numbering **********\n"
60  "********** Function: "
61  << MF.getName() << '\n');
62 
65 
66  MFI.initWARegs();
67 
68  // WebAssembly argument registers are in the same index space as local
69  // variables. Assign the numbers for them first.
70  MachineBasicBlock &EntryMBB = MF.front();
71  for (MachineInstr &MI : EntryMBB) {
73  break;
74 
75  int64_t Imm = MI.getOperand(1).getImm();
76  DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg() << " -> WAReg "
77  << Imm << "\n");
78  MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
79  }
80 
81  // Then assign regular WebAssembly registers for all remaining used
82  // virtual registers. TODO: Consider sorting the registers by frequency of
83  // use, to maximize usage of small immediate fields.
84  unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
85  unsigned NumStackRegs = 0;
86  // Start the numbering for locals after the arg regs
87  unsigned CurReg = MFI.getParams().size();
88  for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
89  unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx);
90  // Skip unused registers.
91  if (MRI.use_empty(VReg))
92  continue;
93  // Handle stackified registers.
94  if (MFI.isVRegStackified(VReg)) {
95  DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
96  << (INT32_MIN | NumStackRegs) << "\n");
97  MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
98  continue;
99  }
100  if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
101  DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
102  MFI.setWAReg(VReg, CurReg++);
103  }
104  }
105 
106  return true;
107 }
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
FunctionPass * createWebAssemblyRegNumbering()
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const MachineBasicBlock & front() const
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:298
This file declares the WebAssembly-specific subclass of TargetSubtarget.
bool isArgument(const MachineInstr &MI)
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
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:52
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.
#define DEBUG(X)
Definition: Debug.h:100
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
StringRef getName() const
getName - Return the name of the corresponding LLVM function.