Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
bindings
cmake
docs
examples
include
lib
Analysis
AsmParser
BinaryFormat
Bitcode
CodeGen
DebugInfo
Demangle
ExecutionEngine
FuzzMutate
Fuzzer
IR
IRReader
LTO
LineEditor
Linker
MC
Object
ObjectYAML
Option
Passes
ProfileData
Support
TableGen
Target
AArch64
AMDGPU
ARC
ARM
AVR
BPF
Hexagon
Lanai
MSP430
Mips
NVPTX
Nios2
PowerPC
RISCV
Sparc
SystemZ
WebAssembly
Disassembler
InstPrinter
MCTargetDesc
TargetInfo
CMakeLists.txt
LLVMBuild.txt
README.txt
WebAssembly.h
WebAssembly.td
WebAssemblyArgumentMove.cpp
WebAssemblyAsmPrinter.cpp
WebAssemblyAsmPrinter.h
WebAssemblyCFGSort.cpp
WebAssemblyCFGStackify.cpp
WebAssemblyCallIndirectFixup.cpp
WebAssemblyExplicitLocals.cpp
WebAssemblyFastISel.cpp
WebAssemblyFixFunctionBitcasts.cpp
WebAssemblyFixIrreducibleControlFlow.cpp
WebAssemblyFrameLowering.cpp
WebAssemblyFrameLowering.h
WebAssemblyISD.def
WebAssemblyISelDAGToDAG.cpp
WebAssemblyISelLowering.cpp
WebAssemblyISelLowering.h
WebAssemblyInstrAtomics.td
WebAssemblyInstrCall.td
WebAssemblyInstrControl.td
WebAssemblyInstrConv.td
WebAssemblyInstrFloat.td
WebAssemblyInstrFormats.td
WebAssemblyInstrInfo.cpp
WebAssemblyInstrInfo.h
WebAssemblyInstrInfo.td
WebAssemblyInstrInteger.td
WebAssemblyInstrMemory.td
WebAssemblyInstrSIMD.td
WebAssemblyLowerBrUnless.cpp
WebAssemblyLowerEmscriptenEHSjLj.cpp
WebAssemblyLowerGlobalDtors.cpp
WebAssemblyMCInstLower.cpp
WebAssemblyMCInstLower.h
WebAssemblyMachineFunctionInfo.cpp
WebAssemblyMachineFunctionInfo.h
WebAssemblyOptimizeLiveIntervals.cpp
WebAssemblyOptimizeReturned.cpp
WebAssemblyPeephole.cpp
WebAssemblyPrepareForLiveIntervals.cpp
WebAssemblyRegColoring.cpp
WebAssemblyRegNumbering.cpp
WebAssemblyRegStackify.cpp
WebAssemblyRegisterInfo.cpp
WebAssemblyRegisterInfo.h
WebAssemblyRegisterInfo.td
WebAssemblyReplacePhysRegs.cpp
WebAssemblyRuntimeLibcallSignatures.cpp
WebAssemblyRuntimeLibcallSignatures.h
WebAssemblySelectionDAGInfo.cpp
WebAssemblySelectionDAGInfo.h
WebAssemblySetP2AlignOperands.cpp
WebAssemblyStoreResults.cpp
WebAssemblySubtarget.cpp
WebAssemblySubtarget.h
WebAssemblyTargetMachine.cpp
WebAssemblyTargetMachine.h
WebAssemblyTargetObjectFile.cpp
WebAssemblyTargetObjectFile.h
WebAssemblyTargetTransformInfo.cpp
WebAssemblyTargetTransformInfo.h
WebAssemblyUtilities.cpp
WebAssemblyUtilities.h
known_gcc_test_failures.txt
X86
XCore
CMakeLists.txt
LLVMBuild.txt
README.txt
Target.cpp
TargetIntrinsicInfo.cpp
TargetLoweringObjectFile.cpp
TargetMachine.cpp
TargetMachineC.cpp
Testing
ToolDrivers
Transforms
WindowsManifest
XRay
CMakeLists.txt
LLVMBuild.txt
projects
resources
runtimes
scripts
test
tools
unittests
utils
.arcconfig
.clang-format
.clang-tidy
.gitattributes
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
CREDITS.TXT
LICENSE.TXT
LLVMBuild.txt
README.txt
RELEASE_TESTERS.TXT
configure
llvm.spec.in
version.txt.in
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyCallIndirectFixup.cpp

134 lines
4.7 KiB
C++
Raw Normal View History

//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
//
// 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 converts pseudo call_indirect instructions into real
/// call_indirects.
///
/// The order of arguments for a call_indirect is the arguments to the function
/// call, followed by the function pointer. There's no natural way to express
/// a machineinstr with varargs followed by one more arg, so we express it as
/// the function pointer followed by varargs, then rewrite it here.
///
/// We need to rewrite the order of the arguments on the machineinstrs
/// themselves so that register stackification knows the order they'll be
/// executed in.
///
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
#include "WebAssembly.h"
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblySubtarget.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineInstrBuilder.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-call-indirect-fixup"
namespace {
class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
StringRef getPassName() const override {
return "WebAssembly CallIndirect Fixup";
}
bool runOnMachineFunction(MachineFunction &MF) override;
public:
static char ID; // Pass identification, replacement for typeid
WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
};
} // end anonymous namespace
char WebAssemblyCallIndirectFixup::ID = 0;
FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
return new WebAssemblyCallIndirectFixup();
}
static unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
switch (MI.getOpcode()) {
using namespace WebAssembly;
case PCALL_INDIRECT_VOID: return CALL_INDIRECT_VOID;
case PCALL_INDIRECT_I32: return CALL_INDIRECT_I32;
case PCALL_INDIRECT_I64: return CALL_INDIRECT_I64;
case PCALL_INDIRECT_F32: return CALL_INDIRECT_F32;
case PCALL_INDIRECT_F64: return CALL_INDIRECT_F64;
case PCALL_INDIRECT_v16i8: return CALL_INDIRECT_v16i8;
case PCALL_INDIRECT_v8i16: return CALL_INDIRECT_v8i16;
case PCALL_INDIRECT_v4i32: return CALL_INDIRECT_v4i32;
case PCALL_INDIRECT_v4f32: return CALL_INDIRECT_v4f32;
default: return INSTRUCTION_LIST_END;
}
}
static bool IsPseudoCallIndirect(const MachineInstr &MI) {
return GetNonPseudoCallIndirectOpcode(MI) !=
WebAssembly::INSTRUCTION_LIST_END;
}
bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
<< MF.getName() << '\n');
bool Changed = false;
const WebAssemblyInstrInfo *TII =
MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
for (MachineBasicBlock &MBB : MF) {
for (MachineInstr &MI : MBB) {
if (IsPseudoCallIndirect(MI)) {
DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
// Rewrite pseudo to non-pseudo
const MCInstrDesc &Desc = TII->get(GetNonPseudoCallIndirectOpcode(MI));
MI.setDesc(Desc);
// Rewrite argument order
SmallVector<MachineOperand, 8> Ops;
// Set up a placeholder for the type signature immediate.
Ops.push_back(MachineOperand::CreateImm(0));
// Set up the flags immediate, which currently has no defined flags
// so it's always zero.
Ops.push_back(MachineOperand::CreateImm(0));
for (const MachineOperand &MO :
make_range(MI.operands_begin() +
MI.getDesc().getNumDefs() + 1,
MI.operands_begin() +
MI.getNumExplicitOperands()))
Ops.push_back(MO);
Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
// Replace the instructions operands.
while (MI.getNumOperands() > MI.getDesc().getNumDefs())
MI.RemoveOperand(MI.getNumOperands() - 1);
for (const MachineOperand &MO : Ops)
MI.addOperand(MO);
DEBUG(dbgs() << " After transform: " << MI);
Changed = true;
}
}
}
DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
return Changed;
}