2015-11-13 01:04:33 +08:00
|
|
|
//===-- WebAssemblyRegNumbering.cpp - Register Numbering ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file implements a pass which assigns WebAssembly register
|
2015-11-13 01:04:33 +08:00
|
|
|
/// numbers for CodeGen virtual registers.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
2016-10-25 03:49:43 +08:00
|
|
|
#include "WebAssemblyUtilities.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2015-12-12 07:49:46 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2016-01-20 13:54:22 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2015-11-13 01:04:33 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-reg-numbering"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyRegNumbering final : public MachineFunctionPass {
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2015-11-13 01:04:33 +08:00
|
|
|
return "WebAssembly Register Numbering";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyRegNumbering() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyRegNumbering::ID = 0;
|
2018-03-31 04:36:58 +08:00
|
|
|
INITIALIZE_PASS(WebAssemblyRegNumbering, DEBUG_TYPE,
|
|
|
|
"Assigns WebAssembly register numbers for virtual registers",
|
|
|
|
false, false)
|
|
|
|
|
2015-11-13 01:04:33 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyRegNumbering() {
|
|
|
|
return new WebAssemblyRegNumbering();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
DEBUG(dbgs() << "********** Register Numbering **********\n"
|
|
|
|
"********** Function: "
|
|
|
|
<< MF.getName() << '\n');
|
|
|
|
|
|
|
|
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
MFI.initWARegs();
|
|
|
|
|
|
|
|
// WebAssembly argument registers are in the same index space as local
|
|
|
|
// variables. Assign the numbers for them first.
|
2015-11-19 00:12:01 +08:00
|
|
|
MachineBasicBlock &EntryMBB = MF.front();
|
|
|
|
for (MachineInstr &MI : EntryMBB) {
|
2016-10-25 03:49:43 +08:00
|
|
|
if (!WebAssembly::isArgument(MI))
|
2015-11-19 00:12:01 +08:00
|
|
|
break;
|
2016-10-25 03:49:43 +08:00
|
|
|
|
|
|
|
int64_t Imm = MI.getOperand(1).getImm();
|
|
|
|
DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg() << " -> WAReg "
|
|
|
|
<< Imm << "\n");
|
|
|
|
MFI.setWAReg(MI.getOperand(0).getReg(), Imm);
|
2015-11-13 01:04:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then assign regular WebAssembly registers for all remaining used
|
2015-12-23 08:22:04 +08:00
|
|
|
// virtual registers. TODO: Consider sorting the registers by frequency of
|
|
|
|
// use, to maximize usage of small immediate fields.
|
2015-11-13 01:04:33 +08:00
|
|
|
unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs();
|
2015-11-19 00:12:01 +08:00
|
|
|
unsigned NumStackRegs = 0;
|
2016-01-23 09:20:43 +08:00
|
|
|
// Start the numbering for locals after the arg regs
|
|
|
|
unsigned CurReg = MFI.getParams().size();
|
2015-11-13 01:04:33 +08:00
|
|
|
for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) {
|
|
|
|
unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx);
|
2016-01-26 00:48:44 +08:00
|
|
|
// Skip unused registers.
|
|
|
|
if (MRI.use_empty(VReg))
|
|
|
|
continue;
|
2015-11-19 00:12:01 +08:00
|
|
|
// Handle stackified registers.
|
|
|
|
if (MFI.isVRegStackified(VReg)) {
|
2016-01-23 09:20:43 +08:00
|
|
|
DEBUG(dbgs() << "VReg " << VReg << " -> WAReg "
|
|
|
|
<< (INT32_MIN | NumStackRegs) << "\n");
|
2015-11-19 00:12:01 +08:00
|
|
|
MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++);
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-23 09:20:43 +08:00
|
|
|
if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) {
|
|
|
|
DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n");
|
|
|
|
MFI.setWAReg(VReg, CurReg++);
|
|
|
|
}
|
2015-11-13 01:04:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|