blob: 1e2a248f097e9f83d9aa8f1fdcd25f0489ace15e [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file implements a pass which assigns WebAssembly register
Dan Gohmancf4748f2015-11-12 17:04:33 +000012/// numbers for CodeGen virtual registers.
13///
14//===----------------------------------------------------------------------===//
15
Dan Gohmancf4748f2015-11-12 17:04:33 +000016#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "WebAssembly.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "WebAssemblyMachineFunctionInfo.h"
19#include "WebAssemblySubtarget.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000020#include "WebAssemblyUtilities.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000021#include "llvm/ADT/SCCIterator.h"
Derek Schuff9769deb2015-12-11 23:49:46 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman83947562016-01-20 05:54:22 +000023#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/Passes.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;
31
32#define DEBUG_TYPE "wasm-reg-numbering"
33
34namespace {
35class WebAssemblyRegNumbering final : public MachineFunctionPass {
Mehdi Amini117296c2016-10-01 02:56:57 +000036 StringRef getPassName() const override {
Dan Gohmancf4748f2015-11-12 17:04:33 +000037 return "WebAssembly Register Numbering";
38 }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.setPreservesCFG();
42 MachineFunctionPass::getAnalysisUsage(AU);
43 }
44
45 bool runOnMachineFunction(MachineFunction &MF) override;
46
47public:
48 static char ID; // Pass identification, replacement for typeid
49 WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
50};
51} // end anonymous namespace
52
53char WebAssemblyRegNumbering::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000054INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE,
55 "Assigns WebAssembly register numbers for virtual registers",
56 false, false)
57
Dan Gohmancf4748f2015-11-12 17:04:33 +000058FunctionPass *llvm::createWebAssemblyRegNumbering() {
59 return new WebAssemblyRegNumbering();
60}
61
62bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000063 LLVM_DEBUG(dbgs() << "********** Register Numbering **********\n"
64 "********** Function: "
65 << MF.getName() << '\n');
Dan Gohmancf4748f2015-11-12 17:04:33 +000066
67 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
68 MachineRegisterInfo &MRI = MF.getRegInfo();
69
70 MFI.initWARegs();
71
72 // WebAssembly argument registers are in the same index space as local
73 // variables. Assign the numbers for them first.
Dan Gohman4ba48162015-11-18 16:12:01 +000074 MachineBasicBlock &EntryMBB = MF.front();
75 for (MachineInstr &MI : EntryMBB) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000076 if (!WebAssembly::isArgument(MI))
Dan Gohman4ba48162015-11-18 16:12:01 +000077 break;
Dan Gohman4fc4e422016-10-24 19:49:43 +000078
79 int64_t Imm = MI.getOperand(1).getImm();
Nicola Zaghend34e60c2018-05-14 12:53:11 +000080 LLVM_DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg()
81 << " -> WAReg " << Imm << "\n");
Dan Gohman4fc4e422016-10-24 19:49:43 +000082 MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
Dan Gohmancf4748f2015-11-12 17:04:33 +000083 }
84
85 // Then assign regular WebAssembly registers for all remaining used
Dan Gohman08d58bc2015-12-23 00:22:04 +000086 // virtual registers. TODO: Consider sorting the registers by frequency of
87 // use, to maximize usage of small immediate fields.
Dan Gohmancf4748f2015-11-12 17:04:33 +000088 unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
Dan Gohman4ba48162015-11-18 16:12:01 +000089 unsigned NumStackRegs = 0;
Derek Schuff65194682016-01-23 01:20:43 +000090 // Start the numbering for locals after the arg regs
91 unsigned CurReg = MFI.getParams().size();
Dan Gohmancf4748f2015-11-12 17:04:33 +000092 for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
93 unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx);
Dan Gohman899cb5a2016-01-25 16:48:44 +000094 // Skip unused registers.
95 if (MRI.use_empty(VReg))
96 continue;
Dan Gohman4ba48162015-11-18 16:12:01 +000097 // Handle stackified registers.
98 if (MFI.isVRegStackified(VReg)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000099 LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
100 << (INT32_MIN | NumStackRegs) << "\n");
Dan Gohman4ba48162015-11-18 16:12:01 +0000101 MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
102 continue;
103 }
Derek Schuff65194682016-01-23 01:20:43 +0000104 if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000105 LLVM_DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
Derek Schuff65194682016-01-23 01:20:43 +0000106 MFI.setWAReg(VReg, CurReg++);
107 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000108 }
Dan Gohmancf4748f2015-11-12 17:04:33 +0000109
110 return true;
111}