2015-06-30 07:51:55 +08:00
|
|
|
//===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file contains the WebAssembly implementation of
|
|
|
|
/// TargetFrameLowering class.
|
|
|
|
///
|
|
|
|
/// On WebAssembly, there aren't a lot of things to do here. There are no
|
|
|
|
/// callee-saved registers to save, and no spill slots.
|
|
|
|
///
|
|
|
|
/// The stack grows downward.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssemblyFrameLowering.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
|
|
|
#include "WebAssemblyInstrInfo.h"
|
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "WebAssemblyTargetMachine.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-07-02 07:41:25 +08:00
|
|
|
#define DEBUG_TYPE "wasm-frame-info"
|
2015-06-30 07:51:55 +08:00
|
|
|
|
2015-12-12 07:49:46 +08:00
|
|
|
// TODO: wasm64
|
|
|
|
// TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
|
2015-06-30 07:51:55 +08:00
|
|
|
|
|
|
|
/// Return true if the specified function should have a dedicated frame pointer
|
|
|
|
/// register.
|
|
|
|
bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
|
2015-07-23 05:28:15 +08:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2015-08-25 00:46:31 +08:00
|
|
|
const auto *RegInfo =
|
|
|
|
MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
|
2016-02-17 07:48:04 +08:00
|
|
|
return MFI->isFrameAddressTaken() || MFI->hasVarSizedObjects() ||
|
|
|
|
MFI->hasStackMap() || MFI->hasPatchPoint() ||
|
|
|
|
RegInfo->needsStackRealignment(MF);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Under normal circumstances, when a frame pointer is not required, we reserve
|
|
|
|
/// argument space for call sites in the function immediately on entry to the
|
|
|
|
/// current function. This eliminates the need for add/sub sp brackets around
|
|
|
|
/// call sites. Returns true if the call frame is included as part of the stack
|
|
|
|
/// frame.
|
|
|
|
bool WebAssemblyFrameLowering::hasReservedCallFrame(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
|
|
|
}
|
|
|
|
|
2016-02-24 02:13:07 +08:00
|
|
|
|
|
|
|
/// Returns true if this function needs a local user-space stack pointer.
|
|
|
|
/// Unlike a machine stack pointer, the wasm user stack pointer is a global
|
|
|
|
/// variable, so it is loaded into a register in the prolog.
|
|
|
|
bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF,
|
|
|
|
const MachineFrameInfo &MFI) const {
|
|
|
|
return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the local user-space stack pointer needs to be written back
|
|
|
|
/// to memory by this function (this is not meaningful if needsSP is false). If
|
|
|
|
/// false, the stack red zone can be used and only a local SP is needed.
|
|
|
|
bool WebAssemblyFrameLowering::needsSPWriteback(
|
|
|
|
const MachineFunction &MF, const MachineFrameInfo &MFI) const {
|
2016-05-06 04:41:15 +08:00
|
|
|
assert(needsSP(MF, MFI));
|
2016-02-24 02:13:07 +08:00
|
|
|
return MFI.getStackSize() > RedZoneSize || MFI.hasCalls() ||
|
|
|
|
MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:57:17 +08:00
|
|
|
static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB,
|
2016-03-18 01:00:29 +08:00
|
|
|
MachineBasicBlock::iterator &InsertAddr,
|
|
|
|
MachineBasicBlock::iterator &InsertStore,
|
2016-02-23 05:57:17 +08:00
|
|
|
DebugLoc DL) {
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
|
|
|
|
unsigned SPAddr =
|
|
|
|
MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
|
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
|
2016-03-18 01:00:29 +08:00
|
|
|
BuildMI(MBB, InsertAddr, DL, TII->get(WebAssembly::CONST_I32), SPAddr)
|
2016-02-23 05:57:17 +08:00
|
|
|
.addExternalSymbol(SPSymbol);
|
|
|
|
auto *MMO = new MachineMemOperand(MachinePointerInfo(),
|
|
|
|
MachineMemOperand::MOStore, 4, 4);
|
2016-03-18 01:00:29 +08:00
|
|
|
BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::STORE_I32),
|
|
|
|
SrcReg)
|
2016-02-23 05:57:17 +08:00
|
|
|
.addImm(0)
|
|
|
|
.addReg(SPAddr)
|
|
|
|
.addImm(2) // p2align
|
|
|
|
.addReg(SrcReg)
|
|
|
|
.addMemOperand(MMO);
|
|
|
|
MF.getInfo<WebAssemblyFunctionInfo>()->stackifyVReg(SPAddr);
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:33:38 +08:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
|
2015-12-17 07:21:30 +08:00
|
|
|
MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2016-02-23 05:57:17 +08:00
|
|
|
assert(!I->getOperand(0).getImm() && hasFP(MF) &&
|
|
|
|
"Call frame pseudos should only be used for dynamic stack adjustment");
|
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
2016-02-24 02:13:07 +08:00
|
|
|
if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
|
|
|
|
needsSPWriteback(MF, *MF.getFrameInfo())) {
|
2016-02-23 05:57:17 +08:00
|
|
|
DebugLoc DL = I->getDebugLoc();
|
2016-03-18 01:00:29 +08:00
|
|
|
writeSPToMemory(WebAssembly::SP32, MF, MBB, I, I, DL);
|
2016-02-23 05:57:17 +08:00
|
|
|
}
|
2016-04-01 02:33:38 +08:00
|
|
|
return MBB.erase(I);
|
2015-12-17 07:21:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
// TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
|
|
|
|
auto *MFI = MF.getFrameInfo();
|
|
|
|
assert(MFI->getCalleeSavedInfo().empty() &&
|
|
|
|
"WebAssembly should not have callee-saved registers");
|
2016-02-21 05:46:50 +08:00
|
|
|
auto *WFI = MF.getInfo<WebAssemblyFunctionInfo>();
|
2016-01-30 02:37:49 +08:00
|
|
|
|
2016-02-24 02:13:07 +08:00
|
|
|
if (!needsSP(MF, *MFI)) return;
|
2015-12-17 07:21:30 +08:00
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
|
2016-01-19 22:53:19 +08:00
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
2016-01-30 02:37:49 +08:00
|
|
|
auto &MRI = MF.getRegInfo();
|
2015-12-17 07:21:30 +08:00
|
|
|
|
|
|
|
auto InsertPt = MBB.begin();
|
|
|
|
DebugLoc DL;
|
|
|
|
|
2016-02-21 05:46:50 +08:00
|
|
|
unsigned SPAddr = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
2016-01-30 02:37:49 +08:00
|
|
|
unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
|
2016-02-21 05:46:50 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPAddr)
|
2016-01-30 02:37:49 +08:00
|
|
|
.addExternalSymbol(SPSymbol);
|
|
|
|
// This MachinePointerInfo should reference __stack_pointer as well but
|
|
|
|
// doesn't because MachinePointerInfo() takes a GV which we don't have for
|
|
|
|
// __stack_pointer. TODO: check if PseudoSourceValue::ExternalSymbolCallEntry
|
|
|
|
// is appropriate instead. (likewise for EmitEpologue below)
|
|
|
|
auto *LoadMMO = new MachineMemOperand(MachinePointerInfo(),
|
|
|
|
MachineMemOperand::MOLoad, 4, 4);
|
|
|
|
// Load the SP value.
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::LOAD_I32),
|
2016-01-30 19:19:26 +08:00
|
|
|
StackSize ? SPReg : (unsigned)WebAssembly::SP32)
|
2016-02-21 05:46:50 +08:00
|
|
|
.addImm(0) // offset
|
|
|
|
.addReg(SPAddr) // addr
|
|
|
|
.addImm(2) // p2align
|
2016-01-30 02:37:49 +08:00
|
|
|
.addMemOperand(LoadMMO);
|
2016-02-21 05:46:50 +08:00
|
|
|
WFI->stackifyVReg(SPAddr);
|
2016-01-30 02:37:49 +08:00
|
|
|
|
|
|
|
if (StackSize) {
|
|
|
|
// Subtract the frame size
|
2016-02-21 05:46:50 +08:00
|
|
|
unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
2016-01-30 02:37:49 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
|
|
|
|
.addImm(StackSize);
|
2016-02-12 04:57:09 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
|
2016-01-30 02:37:49 +08:00
|
|
|
WebAssembly::SP32)
|
|
|
|
.addReg(SPReg)
|
|
|
|
.addReg(OffsetReg);
|
2016-02-21 05:46:50 +08:00
|
|
|
WFI->stackifyVReg(OffsetReg);
|
|
|
|
WFI->stackifyVReg(SPReg);
|
2016-01-30 02:37:49 +08:00
|
|
|
}
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
// Unlike most conventional targets (where FP points to the saved FP),
|
|
|
|
// FP points to the bottom of the fixed-size locals, so we can use positive
|
|
|
|
// offsets in load/store instructions.
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY_LOCAL_I32),
|
|
|
|
WebAssembly::FP32)
|
|
|
|
.addReg(WebAssembly::SP32);
|
|
|
|
}
|
2016-02-24 02:13:07 +08:00
|
|
|
if (StackSize && needsSPWriteback(MF, *MFI)) {
|
2016-03-18 01:00:29 +08:00
|
|
|
writeSPToMemory(WebAssembly::SP32, MF, MBB, InsertPt, InsertPt, DL);
|
2016-01-30 02:37:49 +08:00
|
|
|
}
|
2015-12-17 07:21:30 +08:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:49:46 +08:00
|
|
|
void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2016-01-30 02:37:49 +08:00
|
|
|
auto *MFI = MF.getFrameInfo();
|
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
2016-02-24 02:13:07 +08:00
|
|
|
if (!needsSP(MF, *MFI) || !needsSPWriteback(MF, *MFI)) return;
|
2016-02-21 05:46:50 +08:00
|
|
|
auto *WFI = MF.getInfo<WebAssemblyFunctionInfo>();
|
2016-01-19 22:53:19 +08:00
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
2015-12-12 07:49:46 +08:00
|
|
|
auto &MRI = MF.getRegInfo();
|
|
|
|
auto InsertPt = MBB.getFirstTerminator();
|
|
|
|
DebugLoc DL;
|
|
|
|
|
|
|
|
if (InsertPt != MBB.end()) {
|
|
|
|
DL = InsertPt->getDebugLoc();
|
2016-05-06 04:41:15 +08:00
|
|
|
|
|
|
|
// If code has been stackified with the return, disconnect it so that we
|
|
|
|
// don't break the tree when we insert code just before the return.
|
|
|
|
if (InsertPt->isReturn() && InsertPt->getNumExplicitOperands() != 0) {
|
|
|
|
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
MFI.unstackifyVReg(InsertPt->getOperand(0).getReg());
|
|
|
|
}
|
2015-12-12 07:49:46 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 02:37:49 +08:00
|
|
|
// Restore the stack pointer. If we had fixed-size locals, add the offset
|
|
|
|
// subtracted in the prolog.
|
2016-03-18 01:00:29 +08:00
|
|
|
unsigned SPReg = 0;
|
|
|
|
MachineBasicBlock::iterator InsertAddr = InsertPt;
|
2016-01-30 02:37:49 +08:00
|
|
|
if (StackSize) {
|
2016-02-21 05:46:50 +08:00
|
|
|
unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
2016-03-18 01:00:29 +08:00
|
|
|
InsertAddr =
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
|
|
|
|
.addImm(StackSize);
|
|
|
|
// In the epilog we don't need to write the result back to the SP32 physreg
|
|
|
|
// because it won't be used again. We can use a stackified register instead.
|
|
|
|
SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
|
2016-01-30 02:37:49 +08:00
|
|
|
.addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
|
|
|
|
.addReg(OffsetReg);
|
2016-02-21 05:46:50 +08:00
|
|
|
WFI->stackifyVReg(OffsetReg);
|
2016-03-18 01:00:29 +08:00
|
|
|
WFI->stackifyVReg(SPReg);
|
|
|
|
} else {
|
|
|
|
SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
|
2016-01-30 02:37:49 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 01:00:29 +08:00
|
|
|
writeSPToMemory(SPReg, MF, MBB, InsertAddr, InsertPt, DL);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|