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
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file contains the WebAssembly implementation of
|
2015-06-30 07:51:55 +08:00
|
|
|
/// 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"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "WebAssemblyUtilities.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-25 07:46:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2018-08-22 05:23:07 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#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
|
|
|
|
2016-11-08 06:00:48 +08:00
|
|
|
/// We need a base pointer in the case of having items on the stack that
|
|
|
|
/// require stricter alignment than the stack pointer itself. Because we need
|
|
|
|
/// to shift the stack pointer by some unknown amount to force the alignment,
|
|
|
|
/// we need to record the value of the stack pointer on entry to the function.
|
|
|
|
bool WebAssemblyFrameLowering::hasBP(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
const auto *RegInfo =
|
|
|
|
MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
|
|
|
|
return RegInfo->needsStackRealignment(MF);
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2016-11-08 06:00:48 +08:00
|
|
|
|
|
|
|
// When we have var-sized objects, we move the stack pointer by an unknown
|
|
|
|
// amount, and need to emit a frame pointer to restore the stack to where we
|
|
|
|
// were on function entry.
|
|
|
|
// If we already need a base pointer, we use that to fix up the stack pointer.
|
|
|
|
// If there are no fixed-size objects, we would have no use of a frame
|
|
|
|
// pointer, and thus should not emit one.
|
|
|
|
bool HasFixedSizedObjects = MFI.getStackSize() > 0;
|
|
|
|
bool NeedsFixedReference = !hasBP(MF) || HasFixedSizedObjects;
|
|
|
|
|
|
|
|
return MFI.isFrameAddressTaken() ||
|
|
|
|
(MFI.hasVarSizedObjects() && NeedsFixedReference) ||
|
|
|
|
MFI.hasStackMap() || MFI.hasPatchPoint();
|
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 {
|
2016-07-29 02:40:00 +08:00
|
|
|
return !MF.getFrameInfo().hasVarSizedObjects();
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 05:13:49 +08:00
|
|
|
// Returns true if this function needs a local user-space stack pointer for its
|
|
|
|
// local frame (not for exception handling).
|
|
|
|
bool WebAssemblyFrameLowering::needsSPForLocalFrame(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
auto &MFI = MF.getFrameInfo();
|
|
|
|
return MFI.getStackSize() || MFI.adjustsStack() || hasFP(MF);
|
|
|
|
}
|
|
|
|
|
2018-08-22 05:23:07 +08:00
|
|
|
// In function with EH pads, we need to make a copy of the value of
|
|
|
|
// __stack_pointer global in SP32 register, in order to use it when restoring
|
|
|
|
// __stack_pointer after an exception is caught.
|
|
|
|
bool WebAssemblyFrameLowering::needsPrologForEH(
|
|
|
|
const MachineFunction &MF) const {
|
|
|
|
auto EHType = MF.getTarget().getMCAsmInfo()->getExceptionHandlingType();
|
|
|
|
return EHType == ExceptionHandling::Wasm &&
|
|
|
|
MF.getFunction().hasPersonalityFn() && MF.getFrameInfo().hasCalls();
|
|
|
|
}
|
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.
|
2018-08-23 02:53:48 +08:00
|
|
|
bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
|
2018-08-23 05:13:49 +08:00
|
|
|
return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
|
2016-02-24 02:13:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the local user-space stack pointer needs to be written back
|
2018-08-22 08:20:02 +08:00
|
|
|
/// to __stack_pointer global 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.
|
2016-02-24 02:13:07 +08:00
|
|
|
bool WebAssemblyFrameLowering::needsSPWriteback(
|
2018-08-23 02:53:48 +08:00
|
|
|
const MachineFunction &MF) const {
|
|
|
|
auto &MFI = MF.getFrameInfo();
|
|
|
|
assert(needsSP(MF));
|
2018-08-23 05:13:49 +08:00
|
|
|
// When we don't need a local stack pointer for its local frame but only to
|
|
|
|
// support EH, we don't need to write SP back in the epilog, because we don't
|
|
|
|
// bump down the stack pointer in the prolog. We need to write SP back in the
|
|
|
|
// epilog only if
|
|
|
|
// 1. We need SP not only for EH support but also because we actually use
|
|
|
|
// stack or we have a frame address taken.
|
|
|
|
// 2. We cannot use the red zone.
|
|
|
|
bool CanUseRedZone = MFI.getStackSize() <= RedZoneSize && !MFI.hasCalls() &&
|
|
|
|
!MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
|
|
|
|
return needsSPForLocalFrame(MF) && !CanUseRedZone;
|
2016-02-24 02:13:07 +08:00
|
|
|
}
|
|
|
|
|
2018-08-22 05:23:07 +08:00
|
|
|
void WebAssemblyFrameLowering::writeSPToGlobal(
|
|
|
|
unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
|
2016-02-23 05:57:17 +08:00
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
|
2017-06-17 07:59:10 +08:00
|
|
|
const char *ES = "__stack_pointer";
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName(ES);
|
2018-07-17 07:09:29 +08:00
|
|
|
BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32))
|
2018-08-03 22:33:37 +08:00
|
|
|
.addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL)
|
2018-07-17 07:09:29 +08:00
|
|
|
.addReg(SrcReg);
|
2016-02-23 05:57:17 +08:00
|
|
|
}
|
|
|
|
|
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-11-08 06:00:48 +08:00
|
|
|
assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
|
2016-02-23 05:57:17 +08:00
|
|
|
"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() &&
|
2018-08-23 02:53:48 +08:00
|
|
|
needsSPWriteback(MF)) {
|
2016-02-23 05:57:17 +08:00
|
|
|
DebugLoc DL = I->getDebugLoc();
|
2018-08-22 03:52:19 +08:00
|
|
|
writeSPToGlobal(WebAssembly::SP32, MF, MBB, 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
|
2016-07-29 02:40:00 +08:00
|
|
|
auto &MFI = MF.getFrameInfo();
|
|
|
|
assert(MFI.getCalleeSavedInfo().empty() &&
|
2015-12-17 07:21:30 +08:00
|
|
|
"WebAssembly should not have callee-saved registers");
|
2016-01-30 02:37:49 +08:00
|
|
|
|
2018-08-23 02:53:48 +08:00
|
|
|
if (!needsSP(MF)) return;
|
2016-07-29 02:40:00 +08:00
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2015-12-17 07:21:30 +08:00
|
|
|
|
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();
|
2017-02-25 07:18:00 +08:00
|
|
|
while (InsertPt != MBB.end() && WebAssembly::isArgument(*InsertPt))
|
|
|
|
++InsertPt;
|
2015-12-17 07:21:30 +08:00
|
|
|
DebugLoc DL;
|
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
const TargetRegisterClass *PtrRC =
|
|
|
|
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
|
2016-11-08 06:00:48 +08:00
|
|
|
unsigned SPReg = WebAssembly::SP32;
|
|
|
|
if (StackSize)
|
|
|
|
SPReg = MRI.createVirtualRegister(PtrRC);
|
2017-06-17 07:59:10 +08:00
|
|
|
|
|
|
|
const char *ES = "__stack_pointer";
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName(ES);
|
2018-07-17 07:09:29 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg)
|
2018-08-03 22:33:37 +08:00
|
|
|
.addExternalSymbol(SPSymbol, WebAssemblyII::MO_SYMBOL_GLOBAL);
|
2016-01-30 02:37:49 +08:00
|
|
|
|
2016-11-08 06:00:48 +08:00
|
|
|
bool HasBP = hasBP(MF);
|
|
|
|
if (HasBP) {
|
|
|
|
auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
unsigned BasePtr = MRI.createVirtualRegister(PtrRC);
|
|
|
|
FI->setBasePointerVreg(BasePtr);
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
|
|
|
|
.addReg(SPReg);
|
|
|
|
}
|
2016-01-30 02:37:49 +08:00
|
|
|
if (StackSize) {
|
|
|
|
// Subtract the frame size
|
2016-05-10 12:24:02 +08:00
|
|
|
unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
|
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-11-08 06:00:48 +08:00
|
|
|
if (HasBP) {
|
|
|
|
unsigned BitmaskReg = MRI.createVirtualRegister(PtrRC);
|
|
|
|
unsigned Alignment = MFI.getMaxAlignment();
|
2016-12-03 04:13:05 +08:00
|
|
|
assert((1u << countTrailingZeros(Alignment)) == Alignment &&
|
2016-11-08 06:00:48 +08:00
|
|
|
"Alignment must be a power of 2");
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), BitmaskReg)
|
|
|
|
.addImm((int)~(Alignment - 1));
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::AND_I32),
|
|
|
|
WebAssembly::SP32)
|
|
|
|
.addReg(WebAssembly::SP32)
|
|
|
|
.addReg(BitmaskReg);
|
|
|
|
}
|
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.
|
2016-05-10 12:24:02 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY),
|
2016-01-30 02:37:49 +08:00
|
|
|
WebAssembly::FP32)
|
|
|
|
.addReg(WebAssembly::SP32);
|
|
|
|
}
|
2018-08-23 02:53:48 +08:00
|
|
|
if (StackSize && needsSPWriteback(MF)) {
|
2018-08-22 03:52:19 +08:00
|
|
|
writeSPToGlobal(WebAssembly::SP32, MF, MBB, 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 {
|
2018-08-23 02:53:48 +08:00
|
|
|
uint64_t StackSize = MF.getFrameInfo().getStackSize();
|
|
|
|
if (!needsSP(MF) || !needsSPWriteback(MF)) return;
|
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;
|
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
if (InsertPt != MBB.end())
|
2015-12-12 07:49:46 +08:00
|
|
|
DL = InsertPt->getDebugLoc();
|
2016-05-06 04:41:15 +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;
|
2016-11-08 06:00:48 +08:00
|
|
|
if (hasBP(MF)) {
|
|
|
|
auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
SPReg = FI->getBasePointerVreg();
|
|
|
|
} else if (StackSize) {
|
2016-05-10 12:24:02 +08:00
|
|
|
const TargetRegisterClass *PtrRC =
|
|
|
|
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
|
|
|
|
unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
|
2018-08-21 07:02:15 +08:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
|
|
|
|
.addImm(StackSize);
|
2016-03-18 01:00:29 +08:00
|
|
|
// 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.
|
2016-05-10 12:24:02 +08:00
|
|
|
SPReg = MRI.createVirtualRegister(PtrRC);
|
2016-03-18 01:00:29 +08:00
|
|
|
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-03-18 01:00:29 +08:00
|
|
|
} else {
|
|
|
|
SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
|
2016-01-30 02:37:49 +08:00
|
|
|
}
|
|
|
|
|
2018-08-22 03:52:19 +08:00
|
|
|
writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|