blob: 4a8fd96f8324f862044a5eaee424c0373dd5d354 [file] [log] [blame]
Dan Gohmancf4748f2015-11-12 17:04:33 +00001//===-- 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"
17#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18#include "WebAssemblyMachineFunctionInfo.h"
19#include "WebAssemblySubtarget.h"
20#include "llvm/ADT/SCCIterator.h"
Derek Schuff9769deb2015-12-11 23:49:46 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman83947562016-01-20 05:54:22 +000022#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
30
31#define DEBUG_TYPE "wasm-reg-numbering"
32
33namespace {
34class WebAssemblyRegNumbering final : public MachineFunctionPass {
35 const char *getPassName() const override {
36 return "WebAssembly Register Numbering";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 bool runOnMachineFunction(MachineFunction &MF) override;
45
46public:
47 static char ID; // Pass identification, replacement for typeid
48 WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
49};
50} // end anonymous namespace
51
52char WebAssemblyRegNumbering::ID = 0;
53FunctionPass *llvm::createWebAssemblyRegNumbering() {
54 return new WebAssemblyRegNumbering();
55}
56
57bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
58 DEBUG(dbgs() << "********** Register Numbering **********\n"
59 "********** Function: "
60 << MF.getName() << '\n');
61
62 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
63 MachineRegisterInfo &MRI = MF.getRegInfo();
64
65 MFI.initWARegs();
66
67 // WebAssembly argument registers are in the same index space as local
68 // variables. Assign the numbers for them first.
Dan Gohman4ba48162015-11-18 16:12:01 +000069 MachineBasicBlock &EntryMBB = MF.front();
70 for (MachineInstr &MI : EntryMBB) {
71 switch (MI.getOpcode()) {
72 case WebAssembly::ARGUMENT_I32:
73 case WebAssembly::ARGUMENT_I64:
74 case WebAssembly::ARGUMENT_F32:
Dan Gohman0cfb5f82016-05-10 04:24:02 +000075 case WebAssembly::ARGUMENT_F64: {
76 int64_t Imm = MI.getOperand(1).getImm();
Derek Schuff65194682016-01-23 01:20:43 +000077 DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg() << " -> WAReg "
Dan Gohman0cfb5f82016-05-10 04:24:02 +000078 << Imm << "\n");
79 MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
Dan Gohman4ba48162015-11-18 16:12:01 +000080 break;
Dan Gohman0cfb5f82016-05-10 04:24:02 +000081 }
Dan Gohman4ba48162015-11-18 16:12:01 +000082 default:
83 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +000084 }
85 }
86
87 // Then assign regular WebAssembly registers for all remaining used
Dan Gohman08d58bc2015-12-23 00:22:04 +000088 // virtual registers. TODO: Consider sorting the registers by frequency of
89 // use, to maximize usage of small immediate fields.
Dan Gohmancf4748f2015-11-12 17:04:33 +000090 unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
Dan Gohman4ba48162015-11-18 16:12:01 +000091 unsigned NumStackRegs = 0;
Derek Schuff65194682016-01-23 01:20:43 +000092 // Start the numbering for locals after the arg regs
93 unsigned CurReg = MFI.getParams().size();
Dan Gohmancf4748f2015-11-12 17:04:33 +000094 for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
95 unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx);
Dan Gohman899cb5a2016-01-25 16:48:44 +000096 // Skip unused registers.
97 if (MRI.use_empty(VReg))
98 continue;
Dan Gohman4ba48162015-11-18 16:12:01 +000099 // Handle stackified registers.
100 if (MFI.isVRegStackified(VReg)) {
Derek Schuff65194682016-01-23 01:20:43 +0000101 DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
102 << (INT32_MIN | NumStackRegs) << "\n");
Dan Gohman4ba48162015-11-18 16:12:01 +0000103 MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
104 continue;
105 }
Derek Schuff65194682016-01-23 01:20:43 +0000106 if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
107 DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
108 MFI.setWAReg(VReg, CurReg++);
109 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000110 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000111
112 return true;
113}