2015-06-30 01:51:55 +02:00
|
|
|
//===-- WebAssemblyFrameLowering.cpp - WebAssembly Frame Lowering ----------==//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-06-30 01:51:55 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// This file contains the WebAssembly implementation of
|
2015-06-30 01:51:55 +02: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"
|
2021-04-21 20:44:40 +02:00
|
|
|
#include "Utils/WebAssemblyUtilities.h"
|
2019-12-18 23:50:19 +01:00
|
|
|
#include "WebAssembly.h"
|
2015-06-30 01:51:55 +02:00
|
|
|
#include "WebAssemblyInstrInfo.h"
|
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "WebAssemblyTargetMachine.h"
|
2021-04-21 15:41:48 +02:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2015-06-30 01:51:55 +02:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-25 00:46:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
2015-06-30 01:51:55 +02:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2021-04-21 15:41:48 +02:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2018-08-21 23:23:07 +02:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2015-06-30 01:51:55 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-07-02 01:41:25 +02:00
|
|
|
#define DEBUG_TYPE "wasm-frame-info"
|
2015-06-30 01:51:55 +02:00
|
|
|
|
2015-12-12 00:49:46 +01:00
|
|
|
// TODO: wasm64
|
|
|
|
// TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions
|
2015-06-30 01:51:55 +02:00
|
|
|
|
2021-04-21 15:41:48 +02:00
|
|
|
// In an ideal world, when objects are added to the MachineFrameInfo by
|
|
|
|
// FunctionLoweringInfo::set, we could somehow hook into target-specific code to
|
|
|
|
// ensure they are assigned the right stack ID. However there isn't a hook that
|
|
|
|
// runs between then and DAG building time, though, so instead we hoist stack
|
|
|
|
// objects lazily when they are first used, and comprehensively after the DAG is
|
|
|
|
// built via the PreprocessISelDAG hook, called by the
|
|
|
|
// SelectionDAGISel::runOnMachineFunction. We have to do it in two places
|
|
|
|
// because we want to do it while building the selection DAG for uses of alloca,
|
|
|
|
// but not all alloca instructions are used so we have to follow up afterwards.
|
|
|
|
Optional<unsigned>
|
|
|
|
WebAssemblyFrameLowering::getLocalForStackObject(MachineFunction &MF,
|
|
|
|
int FrameIndex) {
|
|
|
|
MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
|
|
|
|
// If already hoisted to a local, done.
|
|
|
|
if (MFI.getStackID(FrameIndex) == TargetStackID::WasmLocal)
|
|
|
|
return static_cast<unsigned>(MFI.getObjectOffset(FrameIndex));
|
|
|
|
|
|
|
|
// If not allocated in the object address space, this object will be in
|
|
|
|
// linear memory.
|
|
|
|
const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
|
|
|
|
if (!AI ||
|
|
|
|
!WebAssembly::isWasmVarAddressSpace(AI->getType()->getAddressSpace()))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
// Otherwise, allocate this object in the named value stack, outside of linear
|
|
|
|
// memory.
|
|
|
|
SmallVector<EVT, 4> ValueVTs;
|
|
|
|
const WebAssemblyTargetLowering &TLI =
|
|
|
|
*MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
|
|
|
|
WebAssemblyFunctionInfo *FuncInfo = MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
ComputeValueVTs(TLI, MF.getDataLayout(), AI->getAllocatedType(), ValueVTs);
|
|
|
|
MFI.setStackID(FrameIndex, TargetStackID::WasmLocal);
|
|
|
|
// Abuse SP offset to record the index of the first local in the object.
|
|
|
|
unsigned Local = FuncInfo->getParams().size() + FuncInfo->getLocals().size();
|
|
|
|
MFI.setObjectOffset(FrameIndex, Local);
|
|
|
|
// Allocate WebAssembly locals for each non-aggregate component of the
|
|
|
|
// allocation.
|
|
|
|
for (EVT ValueVT : ValueVTs)
|
|
|
|
FuncInfo->addLocal(ValueVT.getSimpleVT());
|
|
|
|
// Abuse object size to record number of WebAssembly locals allocated to
|
|
|
|
// this object.
|
|
|
|
MFI.setObjectSize(FrameIndex, ValueVTs.size());
|
|
|
|
return static_cast<unsigned>(Local);
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:00:48 +01: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.
|
2018-09-05 03:27:38 +02:00
|
|
|
bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const {
|
2016-11-07 23:00:48 +01:00
|
|
|
const auto *RegInfo =
|
|
|
|
MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
|
2021-03-15 14:01:34 +01:00
|
|
|
return RegInfo->hasStackRealignment(MF);
|
2016-11-07 23:00:48 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 01:51:55 +02:00
|
|
|
/// Return true if the specified function should have a dedicated frame pointer
|
|
|
|
/// register.
|
|
|
|
bool WebAssemblyFrameLowering::hasFP(const MachineFunction &MF) const {
|
2016-07-28 20:40:00 +02:00
|
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
2016-11-07 23:00:48 +01: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 01:51:55 +02: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-28 20:40:00 +02:00
|
|
|
return !MF.getFrameInfo().hasVarSizedObjects();
|
2015-06-30 01:51:55 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:13:49 +02: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-21 23:23:07 +02:00
|
|
|
// In function with EH pads, we need to make a copy of the value of
|
2020-06-16 00:31:10 +02:00
|
|
|
// __stack_pointer global in SP32/64 register, in order to use it when
|
|
|
|
// restoring __stack_pointer after an exception is caught.
|
2018-08-21 23:23:07 +02:00
|
|
|
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-23 19:13:07 +01: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-22 20:53:48 +02:00
|
|
|
bool WebAssemblyFrameLowering::needsSP(const MachineFunction &MF) const {
|
2018-08-22 23:13:49 +02:00
|
|
|
return needsSPForLocalFrame(MF) || needsPrologForEH(MF);
|
2016-02-23 19:13:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the local user-space stack pointer needs to be written back
|
2018-08-22 02:20:02 +02: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-23 19:13:07 +01:00
|
|
|
bool WebAssemblyFrameLowering::needsSPWriteback(
|
2018-08-22 20:53:48 +02:00
|
|
|
const MachineFunction &MF) const {
|
|
|
|
auto &MFI = MF.getFrameInfo();
|
|
|
|
assert(needsSP(MF));
|
2018-08-22 23:13:49 +02: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-23 19:13:07 +01:00
|
|
|
}
|
|
|
|
|
2020-06-16 00:31:10 +02:00
|
|
|
unsigned WebAssemblyFrameLowering::getSPReg(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::SP64
|
|
|
|
: WebAssembly::SP32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned WebAssemblyFrameLowering::getFPReg(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::FP64
|
|
|
|
: WebAssembly::FP32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
WebAssemblyFrameLowering::getOpcConst(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::CONST_I64
|
|
|
|
: WebAssembly::CONST_I32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned WebAssemblyFrameLowering::getOpcAdd(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::ADD_I64
|
|
|
|
: WebAssembly::ADD_I32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned WebAssemblyFrameLowering::getOpcSub(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::SUB_I64
|
|
|
|
: WebAssembly::SUB_I32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned WebAssemblyFrameLowering::getOpcAnd(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::AND_I64
|
|
|
|
: WebAssembly::AND_I32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
WebAssemblyFrameLowering::getOpcGlobGet(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::GLOBAL_GET_I64
|
|
|
|
: WebAssembly::GLOBAL_GET_I32;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
WebAssemblyFrameLowering::getOpcGlobSet(const MachineFunction &MF) {
|
|
|
|
return MF.getSubtarget<WebAssemblySubtarget>().hasAddr64()
|
|
|
|
? WebAssembly::GLOBAL_SET_I64
|
|
|
|
: WebAssembly::GLOBAL_SET_I32;
|
|
|
|
}
|
|
|
|
|
2018-08-21 23:23:07 +02:00
|
|
|
void WebAssemblyFrameLowering::writeSPToGlobal(
|
|
|
|
unsigned SrcReg, MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &InsertStore, const DebugLoc &DL) const {
|
2016-02-22 22:57:17 +01:00
|
|
|
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
|
2017-06-17 01:59:10 +02:00
|
|
|
const char *ES = "__stack_pointer";
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName(ES);
|
2020-06-16 00:31:10 +02:00
|
|
|
|
|
|
|
BuildMI(MBB, InsertStore, DL, TII->get(getOpcGlobSet(MF)))
|
2019-04-03 02:17:29 +02:00
|
|
|
.addExternalSymbol(SPSymbol)
|
2018-07-17 01:09:29 +02:00
|
|
|
.addReg(SrcReg);
|
2016-02-22 22:57:17 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 20:33:38 +02:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
WebAssemblyFrameLowering::eliminateCallFramePseudoInstr(
|
2015-12-17 00:21:30 +01:00
|
|
|
MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2016-11-07 23:00:48 +01:00
|
|
|
assert(!I->getOperand(0).getImm() && (hasFP(MF) || hasBP(MF)) &&
|
2016-02-22 22:57:17 +01:00
|
|
|
"Call frame pseudos should only be used for dynamic stack adjustment");
|
2020-06-16 00:31:10 +02:00
|
|
|
auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
|
|
|
|
const auto *TII = ST.getInstrInfo();
|
2016-02-23 19:13:07 +01:00
|
|
|
if (I->getOpcode() == TII->getCallFrameDestroyOpcode() &&
|
2018-08-22 20:53:48 +02:00
|
|
|
needsSPWriteback(MF)) {
|
2016-02-22 22:57:17 +01:00
|
|
|
DebugLoc DL = I->getDebugLoc();
|
2020-06-16 00:31:10 +02:00
|
|
|
writeSPToGlobal(getSPReg(MF), MF, MBB, I, DL);
|
2016-02-22 22:57:17 +01:00
|
|
|
}
|
2016-03-31 20:33:38 +02:00
|
|
|
return MBB.erase(I);
|
2015-12-17 00:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
|
|
|
// TODO: Do ".setMIFlag(MachineInstr::FrameSetup)" on emitted instructions
|
2016-07-28 20:40:00 +02:00
|
|
|
auto &MFI = MF.getFrameInfo();
|
|
|
|
assert(MFI.getCalleeSavedInfo().empty() &&
|
2015-12-17 00:21:30 +01:00
|
|
|
"WebAssembly should not have callee-saved registers");
|
2016-01-29 19:37:49 +01:00
|
|
|
|
2018-09-05 03:27:38 +02:00
|
|
|
if (!needsSP(MF))
|
|
|
|
return;
|
2016-07-28 20:40:00 +02:00
|
|
|
uint64_t StackSize = MFI.getStackSize();
|
2015-12-17 00:21:30 +01:00
|
|
|
|
2020-06-16 00:31:10 +02:00
|
|
|
auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
|
|
|
|
const auto *TII = ST.getInstrInfo();
|
2016-01-29 19:37:49 +01:00
|
|
|
auto &MRI = MF.getRegInfo();
|
2015-12-17 00:21:30 +01:00
|
|
|
|
|
|
|
auto InsertPt = MBB.begin();
|
2019-07-13 00:08:25 +02:00
|
|
|
while (InsertPt != MBB.end() &&
|
|
|
|
WebAssembly::isArgument(InsertPt->getOpcode()))
|
2017-02-25 00:18:00 +01:00
|
|
|
++InsertPt;
|
2015-12-17 00:21:30 +01:00
|
|
|
DebugLoc DL;
|
|
|
|
|
2016-05-10 06:24:02 +02:00
|
|
|
const TargetRegisterClass *PtrRC =
|
|
|
|
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
|
2020-06-16 00:31:10 +02:00
|
|
|
unsigned SPReg = getSPReg(MF);
|
2016-11-07 23:00:48 +01:00
|
|
|
if (StackSize)
|
|
|
|
SPReg = MRI.createVirtualRegister(PtrRC);
|
2017-06-17 01:59:10 +02:00
|
|
|
|
|
|
|
const char *ES = "__stack_pointer";
|
|
|
|
auto *SPSymbol = MF.createExternalSymbolName(ES);
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcGlobGet(MF)), SPReg)
|
2019-04-03 02:17:29 +02:00
|
|
|
.addExternalSymbol(SPSymbol);
|
2016-01-29 19:37:49 +01:00
|
|
|
|
2016-11-07 23:00:48 +01:00
|
|
|
bool HasBP = hasBP(MF);
|
|
|
|
if (HasBP) {
|
|
|
|
auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 00:40:45 +02:00
|
|
|
Register BasePtr = MRI.createVirtualRegister(PtrRC);
|
2016-11-07 23:00:48 +01:00
|
|
|
FI->setBasePointerVreg(BasePtr);
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), BasePtr)
|
|
|
|
.addReg(SPReg);
|
|
|
|
}
|
2016-01-29 19:37:49 +01:00
|
|
|
if (StackSize) {
|
|
|
|
// Subtract the frame size
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 00:40:45 +02:00
|
|
|
Register OffsetReg = MRI.createVirtualRegister(PtrRC);
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
|
2016-01-29 19:37:49 +01:00
|
|
|
.addImm(StackSize);
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcSub(MF)), getSPReg(MF))
|
2016-01-29 19:37:49 +01:00
|
|
|
.addReg(SPReg)
|
|
|
|
.addReg(OffsetReg);
|
|
|
|
}
|
2016-11-07 23:00:48 +01:00
|
|
|
if (HasBP) {
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 00:40:45 +02:00
|
|
|
Register BitmaskReg = MRI.createVirtualRegister(PtrRC);
|
[Alignment][NFC] Deprecate getMaxAlignment
Summary:
This is patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790
Reviewers: courbet
Subscribers: jholewinski, arsenm, dschuff, jyknight, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76348
2020-03-18 10:50:38 +01:00
|
|
|
Align Alignment = MFI.getMaxAlign();
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), BitmaskReg)
|
|
|
|
.addImm((int64_t) ~(Alignment.value() - 1));
|
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcAnd(MF)), getSPReg(MF))
|
|
|
|
.addReg(getSPReg(MF))
|
2016-11-07 23:00:48 +01:00
|
|
|
.addReg(BitmaskReg);
|
|
|
|
}
|
2016-01-29 19:37:49 +01: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.
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY), getFPReg(MF))
|
|
|
|
.addReg(getSPReg(MF));
|
2016-01-29 19:37:49 +01:00
|
|
|
}
|
2018-08-22 20:53:48 +02:00
|
|
|
if (StackSize && needsSPWriteback(MF)) {
|
2020-06-16 00:31:10 +02:00
|
|
|
writeSPToGlobal(getSPReg(MF), MF, MBB, InsertPt, DL);
|
2016-01-29 19:37:49 +01:00
|
|
|
}
|
2015-12-17 00:21:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-12 00:49:46 +01:00
|
|
|
void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2018-08-22 20:53:48 +02:00
|
|
|
uint64_t StackSize = MF.getFrameInfo().getStackSize();
|
2018-09-05 03:27:38 +02:00
|
|
|
if (!needsSP(MF) || !needsSPWriteback(MF))
|
|
|
|
return;
|
2020-06-16 00:31:10 +02:00
|
|
|
auto &ST = MF.getSubtarget<WebAssemblySubtarget>();
|
|
|
|
const auto *TII = ST.getInstrInfo();
|
2015-12-12 00:49:46 +01:00
|
|
|
auto &MRI = MF.getRegInfo();
|
|
|
|
auto InsertPt = MBB.getFirstTerminator();
|
|
|
|
DebugLoc DL;
|
|
|
|
|
2016-05-10 06:24:02 +02:00
|
|
|
if (InsertPt != MBB.end())
|
2015-12-12 00:49:46 +01:00
|
|
|
DL = InsertPt->getDebugLoc();
|
2016-05-05 22:41:15 +02:00
|
|
|
|
2016-01-29 19:37:49 +01:00
|
|
|
// Restore the stack pointer. If we had fixed-size locals, add the offset
|
|
|
|
// subtracted in the prolog.
|
2016-03-17 18:00:29 +01:00
|
|
|
unsigned SPReg = 0;
|
2020-06-16 00:31:10 +02:00
|
|
|
unsigned SPFPReg = hasFP(MF) ? getFPReg(MF) : getSPReg(MF);
|
2016-11-07 23:00:48 +01:00
|
|
|
if (hasBP(MF)) {
|
|
|
|
auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
SPReg = FI->getBasePointerVreg();
|
|
|
|
} else if (StackSize) {
|
2016-05-10 06:24:02 +02:00
|
|
|
const TargetRegisterClass *PtrRC =
|
|
|
|
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 00:40:45 +02:00
|
|
|
Register OffsetReg = MRI.createVirtualRegister(PtrRC);
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcConst(MF)), OffsetReg)
|
2018-08-21 01:02:15 +02:00
|
|
|
.addImm(StackSize);
|
2020-06-16 00:31:10 +02:00
|
|
|
// In the epilog we don't need to write the result back to the SP32/64
|
|
|
|
// physreg because it won't be used again. We can use a stackified register
|
|
|
|
// instead.
|
2016-05-10 06:24:02 +02:00
|
|
|
SPReg = MRI.createVirtualRegister(PtrRC);
|
2020-06-16 00:31:10 +02:00
|
|
|
BuildMI(MBB, InsertPt, DL, TII->get(getOpcAdd(MF)), SPReg)
|
|
|
|
.addReg(SPFPReg)
|
2016-01-29 19:37:49 +01:00
|
|
|
.addReg(OffsetReg);
|
2016-03-17 18:00:29 +01:00
|
|
|
} else {
|
2020-06-16 00:31:10 +02:00
|
|
|
SPReg = SPFPReg;
|
2016-01-29 19:37:49 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 21:52:19 +02:00
|
|
|
writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL);
|
2015-06-30 01:51:55 +02:00
|
|
|
}
|
2019-12-18 23:50:19 +01:00
|
|
|
|
2021-04-21 15:41:48 +02:00
|
|
|
bool WebAssemblyFrameLowering::isSupportedStackID(
|
|
|
|
TargetStackID::Value ID) const {
|
|
|
|
// Use the Object stack for WebAssembly locals which can only be accessed
|
|
|
|
// by name, not via an address in linear memory.
|
|
|
|
if (ID == TargetStackID::WasmLocal)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return TargetFrameLowering::isSupportedStackID(ID);
|
|
|
|
}
|
|
|
|
|
2019-12-18 23:50:19 +01:00
|
|
|
TargetFrameLowering::DwarfFrameBase
|
|
|
|
WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const {
|
|
|
|
DwarfFrameBase Loc;
|
|
|
|
Loc.Kind = DwarfFrameBase::WasmFrameBase;
|
|
|
|
const WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
|
2020-01-28 22:46:03 +01:00
|
|
|
if (needsSP(MF) && MFI.isFrameBaseVirtual()) {
|
2019-12-18 23:50:19 +01:00
|
|
|
unsigned LocalNum = MFI.getFrameBaseLocal();
|
2020-03-20 03:53:51 +01:00
|
|
|
Loc.Location.WasmLoc = {WebAssembly::TI_LOCAL, LocalNum};
|
2019-12-18 23:50:19 +01:00
|
|
|
} else {
|
|
|
|
// TODO: This should work on a breakpoint at a function with no frame,
|
|
|
|
// but probably won't work for traversing up the stack.
|
2020-03-20 03:53:51 +01:00
|
|
|
Loc.Location.WasmLoc = {WebAssembly::TI_GLOBAL_RELOC, 0};
|
2019-12-18 23:50:19 +01:00
|
|
|
}
|
|
|
|
return Loc;
|
|
|
|
}
|