diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2021-08-22 19:00:43 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2021-11-13 20:39:49 +0000 |
commit | fe6060f10f634930ff71b7c50291ddc610da2475 (patch) | |
tree | 1483580c790bd4d27b6500a7542b5ee00534d3cc /contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp | |
parent | b61bce17f346d79cecfd8f195a64b10f77be43b1 (diff) | |
parent | 344a3780b2e33f6ca763666c380202b18aab72a3 (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp b/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp index 95669932e73f..c45f7d7176b5 100644 --- a/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp +++ b/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp @@ -19,17 +19,19 @@ #include "WebAssemblyFrameLowering.h" #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" +#include "Utils/WebAssemblyUtilities.h" #include "WebAssembly.h" #include "WebAssemblyInstrInfo.h" #include "WebAssemblyMachineFunctionInfo.h" #include "WebAssemblySubtarget.h" #include "WebAssemblyTargetMachine.h" -#include "WebAssemblyUtilities.h" +#include "llvm/CodeGen/Analysis.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineModuleInfoImpls.h" #include "llvm/CodeGen/MachineRegisterInfo.h" +#include "llvm/IR/Instructions.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/Support/Debug.h" using namespace llvm; @@ -39,6 +41,52 @@ using namespace llvm; // TODO: wasm64 // TODO: Emit TargetOpcode::CFI_INSTRUCTION instructions +// 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); +} + /// 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, @@ -46,7 +94,7 @@ using namespace llvm; bool WebAssemblyFrameLowering::hasBP(const MachineFunction &MF) const { const auto *RegInfo = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); - return RegInfo->needsStackRealignment(MF); + return RegInfo->hasStackRealignment(MF); } /// Return true if the specified function should have a dedicated frame pointer @@ -314,6 +362,16 @@ void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF, writeSPToGlobal(SPReg, MF, MBB, InsertPt, DL); } +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); +} + TargetFrameLowering::DwarfFrameBase WebAssemblyFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const { DwarfFrameBase Loc; |