10b57cec5SDimitry Andric //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file defines the WebAssembly-specific subclass of TargetMachine.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h"
150b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
160b57cec5SDimitry Andric #include "TargetInfo/WebAssemblyTargetInfo.h"
170b57cec5SDimitry Andric #include "WebAssembly.h"
1806c3fb27SDimitry Andric #include "WebAssemblyISelLowering.h"
190b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
200b57cec5SDimitry Andric #include "WebAssemblyTargetObjectFile.h"
210b57cec5SDimitry Andric #include "WebAssemblyTargetTransformInfo.h"
225f757f3fSDimitry Andric #include "WebAssemblyUtilities.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/RegAllocRegistry.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
280b57cec5SDimitry Andric #include "llvm/IR/Function.h"
2981ad6265SDimitry Andric #include "llvm/InitializePasses.h"
300eae32dcSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
31349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
320b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
330b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
3481ad6265SDimitry Andric #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
350b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h"
36bdd1243dSDimitry Andric #include <optional>
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric #define DEBUG_TYPE "wasm"
400b57cec5SDimitry Andric
415ffd83dbSDimitry Andric // A command-line option to keep implicit locals
425ffd83dbSDimitry Andric // for the purpose of testing with lit/llc ONLY.
435ffd83dbSDimitry Andric // This produces output which is not valid WebAssembly, and is not supported
445ffd83dbSDimitry Andric // by assemblers/disassemblers and other MC based tools.
455ffd83dbSDimitry Andric static cl::opt<bool> WasmDisableExplicitLocals(
465ffd83dbSDimitry Andric "wasm-disable-explicit-locals", cl::Hidden,
475ffd83dbSDimitry Andric cl::desc("WebAssembly: output implicit locals in"
485ffd83dbSDimitry Andric " instruction output for test purposes only."),
495ffd83dbSDimitry Andric cl::init(false));
505ffd83dbSDimitry Andric
515f757f3fSDimitry Andric static cl::opt<bool> WasmDisableFixIrreducibleControlFlowPass(
525f757f3fSDimitry Andric "wasm-disable-fix-irreducible-control-flow-pass", cl::Hidden,
535f757f3fSDimitry Andric cl::desc("webassembly: disables the fix "
545f757f3fSDimitry Andric " irreducible control flow optimization pass"),
555f757f3fSDimitry Andric cl::init(false));
565f757f3fSDimitry Andric
LLVMInitializeWebAssemblyTarget()57480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
580b57cec5SDimitry Andric // Register the target.
590b57cec5SDimitry Andric RegisterTargetMachine<WebAssemblyTargetMachine> X(
600b57cec5SDimitry Andric getTheWebAssemblyTarget32());
610b57cec5SDimitry Andric RegisterTargetMachine<WebAssemblyTargetMachine> Y(
620b57cec5SDimitry Andric getTheWebAssemblyTarget64());
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric // Register backend passes
650b57cec5SDimitry Andric auto &PR = *PassRegistry::getPassRegistry();
660b57cec5SDimitry Andric initializeWebAssemblyAddMissingPrototypesPass(PR);
670b57cec5SDimitry Andric initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
6881ad6265SDimitry Andric initializeLowerGlobalDtorsLegacyPassPass(PR);
690b57cec5SDimitry Andric initializeFixFunctionBitcastsPass(PR);
700b57cec5SDimitry Andric initializeOptimizeReturnedPass(PR);
710fca6ea1SDimitry Andric initializeWebAssemblyRefTypeMem2LocalPass(PR);
720b57cec5SDimitry Andric initializeWebAssemblyArgumentMovePass(PR);
730b57cec5SDimitry Andric initializeWebAssemblySetP2AlignOperandsPass(PR);
740b57cec5SDimitry Andric initializeWebAssemblyReplacePhysRegsPass(PR);
750b57cec5SDimitry Andric initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
760b57cec5SDimitry Andric initializeWebAssemblyMemIntrinsicResultsPass(PR);
770b57cec5SDimitry Andric initializeWebAssemblyRegStackifyPass(PR);
780b57cec5SDimitry Andric initializeWebAssemblyRegColoringPass(PR);
79fe6060f1SDimitry Andric initializeWebAssemblyNullifyDebugValueListsPass(PR);
800b57cec5SDimitry Andric initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
810b57cec5SDimitry Andric initializeWebAssemblyLateEHPreparePass(PR);
820b57cec5SDimitry Andric initializeWebAssemblyExceptionInfoPass(PR);
830b57cec5SDimitry Andric initializeWebAssemblyCFGSortPass(PR);
840b57cec5SDimitry Andric initializeWebAssemblyCFGStackifyPass(PR);
850b57cec5SDimitry Andric initializeWebAssemblyExplicitLocalsPass(PR);
860b57cec5SDimitry Andric initializeWebAssemblyLowerBrUnlessPass(PR);
870b57cec5SDimitry Andric initializeWebAssemblyRegNumberingPass(PR);
885ffd83dbSDimitry Andric initializeWebAssemblyDebugFixupPass(PR);
890b57cec5SDimitry Andric initializeWebAssemblyPeepholePass(PR);
90fe6060f1SDimitry Andric initializeWebAssemblyMCLowerPrePassPass(PR);
91bdd1243dSDimitry Andric initializeWebAssemblyLowerRefTypesIntPtrConvPass(PR);
92bdd1243dSDimitry Andric initializeWebAssemblyFixBrTableDefaultsPass(PR);
930fca6ea1SDimitry Andric initializeWebAssemblyDAGToDAGISelLegacyPass(PR);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
970b57cec5SDimitry Andric // WebAssembly Lowering public interface.
980b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
990b57cec5SDimitry Andric
getEffectiveRelocModel(std::optional<Reloc::Model> RM,const Triple & TT)100bdd1243dSDimitry Andric static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM,
1010b57cec5SDimitry Andric const Triple &TT) {
10281ad6265SDimitry Andric if (!RM) {
1030b57cec5SDimitry Andric // Default to static relocation model. This should always be more optimial
1040b57cec5SDimitry Andric // than PIC since the static linker can determine all global addresses and
1050b57cec5SDimitry Andric // assume direct function calls.
1060b57cec5SDimitry Andric return Reloc::Static;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric return *RM;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric /// Create an WebAssembly architecture model.
1130b57cec5SDimitry Andric ///
WebAssemblyTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,std::optional<Reloc::Model> RM,std::optional<CodeModel::Model> CM,CodeGenOptLevel OL,bool JIT)1140b57cec5SDimitry Andric WebAssemblyTargetMachine::WebAssemblyTargetMachine(
1150b57cec5SDimitry Andric const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
116bdd1243dSDimitry Andric const TargetOptions &Options, std::optional<Reloc::Model> RM,
1175f757f3fSDimitry Andric std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
118fe6060f1SDimitry Andric : LLVMTargetMachine(
119fe6060f1SDimitry Andric T,
120fe6060f1SDimitry Andric TT.isArch64Bit()
121349cc55cSDimitry Andric ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
122349cc55cSDimitry Andric "f128:64-n32:64-S128-ni:1:10:20"
123349cc55cSDimitry Andric : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
124349cc55cSDimitry Andric "n32:64-S128-ni:1:10:20")
125349cc55cSDimitry Andric : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
126349cc55cSDimitry Andric "f128:64-n32:64-S128-ni:1:10:20"
127349cc55cSDimitry Andric : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
128349cc55cSDimitry Andric "n32:64-S128-ni:1:10:20"),
1290b57cec5SDimitry Andric TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
1300b57cec5SDimitry Andric getEffectiveCodeModel(CM, CodeModel::Large), OL),
1310fca6ea1SDimitry Andric TLOF(new WebAssemblyTargetObjectFile()),
1320fca6ea1SDimitry Andric UsesMultivalueABI(Options.MCOptions.getABIName() == "experimental-mv") {
1330b57cec5SDimitry Andric // WebAssembly type-checks instructions, but a noreturn function with a return
1340b57cec5SDimitry Andric // type that doesn't match the context will cause a check failure. So we lower
1350b57cec5SDimitry Andric // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
1360b57cec5SDimitry Andric // 'unreachable' instructions which is meant for that case.
1370b57cec5SDimitry Andric this->Options.TrapUnreachable = true;
1385f757f3fSDimitry Andric this->Options.NoTrapAfterNoreturn = false;
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric // WebAssembly treats each function as an independent unit. Force
1410b57cec5SDimitry Andric // -ffunction-sections, effectively, so that we can emit them independently.
1420b57cec5SDimitry Andric this->Options.FunctionSections = true;
1430b57cec5SDimitry Andric this->Options.DataSections = true;
1440b57cec5SDimitry Andric this->Options.UniqueSectionNames = true;
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric initAsmInfo();
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric // Note that we don't use setRequiresStructuredCFG(true). It disables
1490b57cec5SDimitry Andric // optimizations than we're ok with, and want, such as critical edge
1500b57cec5SDimitry Andric // splitting and tail merging.
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
1540b57cec5SDimitry Andric
getSubtargetImpl() const155e8d8bef9SDimitry Andric const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const {
156e8d8bef9SDimitry Andric return getSubtargetImpl(std::string(getTargetCPU()),
157e8d8bef9SDimitry Andric std::string(getTargetFeatureString()));
158e8d8bef9SDimitry Andric }
159e8d8bef9SDimitry Andric
1600b57cec5SDimitry Andric const WebAssemblySubtarget *
getSubtargetImpl(std::string CPU,std::string FS) const1610b57cec5SDimitry Andric WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
1620b57cec5SDimitry Andric std::string FS) const {
1630b57cec5SDimitry Andric auto &I = SubtargetMap[CPU + FS];
1640b57cec5SDimitry Andric if (!I) {
1658bcb0991SDimitry Andric I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric return I.get();
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric const WebAssemblySubtarget *
getSubtargetImpl(const Function & F) const1710b57cec5SDimitry Andric WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
1720b57cec5SDimitry Andric Attribute CPUAttr = F.getFnAttribute("target-cpu");
1730b57cec5SDimitry Andric Attribute FSAttr = F.getFnAttribute("target-features");
1740b57cec5SDimitry Andric
175e8d8bef9SDimitry Andric std::string CPU =
176e8d8bef9SDimitry Andric CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
177e8d8bef9SDimitry Andric std::string FS =
178e8d8bef9SDimitry Andric FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric // This needs to be done before we create a new subtarget since any
1810b57cec5SDimitry Andric // creation will depend on the TM and the code generation flags on the
1820b57cec5SDimitry Andric // function that reside in TargetOptions.
1830b57cec5SDimitry Andric resetTargetOptions(F);
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric return getSubtargetImpl(CPU, FS);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric namespace {
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric class CoalesceFeaturesAndStripAtomics final : public ModulePass {
1910b57cec5SDimitry Andric // Take the union of all features used in the module and use it for each
1920b57cec5SDimitry Andric // function individually, since having multiple feature sets in one module
1930b57cec5SDimitry Andric // currently does not make sense for WebAssembly. If atomics are not enabled,
1940b57cec5SDimitry Andric // also strip atomic operations and thread local storage.
1950b57cec5SDimitry Andric static char ID;
1960b57cec5SDimitry Andric WebAssemblyTargetMachine *WasmTM;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric public:
CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine * WasmTM)1990b57cec5SDimitry Andric CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
2000b57cec5SDimitry Andric : ModulePass(ID), WasmTM(WasmTM) {}
2010b57cec5SDimitry Andric
runOnModule(Module & M)2020b57cec5SDimitry Andric bool runOnModule(Module &M) override {
2030b57cec5SDimitry Andric FeatureBitset Features = coalesceFeatures(M);
2040b57cec5SDimitry Andric
205*d686ce93SDimitry Andric std::string FeatureStr = getFeatureString(Features);
206e8d8bef9SDimitry Andric WasmTM->setTargetFeatureString(FeatureStr);
2070b57cec5SDimitry Andric for (auto &F : M)
2080b57cec5SDimitry Andric replaceFeatures(F, FeatureStr);
2090b57cec5SDimitry Andric
2100b57cec5SDimitry Andric bool StrippedAtomics = false;
2110b57cec5SDimitry Andric bool StrippedTLS = false;
2120b57cec5SDimitry Andric
21381ad6265SDimitry Andric if (!Features[WebAssembly::FeatureAtomics]) {
2140b57cec5SDimitry Andric StrippedAtomics = stripAtomics(M);
2150b57cec5SDimitry Andric StrippedTLS = stripThreadLocals(M);
21681ad6265SDimitry Andric } else if (!Features[WebAssembly::FeatureBulkMemory]) {
21781ad6265SDimitry Andric StrippedTLS |= stripThreadLocals(M);
21881ad6265SDimitry Andric }
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric if (StrippedAtomics && !StrippedTLS)
2210b57cec5SDimitry Andric stripThreadLocals(M);
2220b57cec5SDimitry Andric else if (StrippedTLS && !StrippedAtomics)
2230b57cec5SDimitry Andric stripAtomics(M);
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric // Conservatively assume we have made some change
2280b57cec5SDimitry Andric return true;
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric private:
coalesceFeatures(const Module & M)2320b57cec5SDimitry Andric FeatureBitset coalesceFeatures(const Module &M) {
2330b57cec5SDimitry Andric FeatureBitset Features =
2340b57cec5SDimitry Andric WasmTM
2355ffd83dbSDimitry Andric ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
2365ffd83dbSDimitry Andric std::string(WasmTM->getTargetFeatureString()))
2370b57cec5SDimitry Andric ->getFeatureBits();
2380b57cec5SDimitry Andric for (auto &F : M)
2390b57cec5SDimitry Andric Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
2400b57cec5SDimitry Andric return Features;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric
getFeatureString(const FeatureBitset & Features)243*d686ce93SDimitry Andric static std::string getFeatureString(const FeatureBitset &Features) {
2440b57cec5SDimitry Andric std::string Ret;
2450b57cec5SDimitry Andric for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
2460b57cec5SDimitry Andric if (Features[KV.Value])
2470b57cec5SDimitry Andric Ret += (StringRef("+") + KV.Key + ",").str();
248*d686ce93SDimitry Andric else
249*d686ce93SDimitry Andric Ret += (StringRef("-") + KV.Key + ",").str();
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric return Ret;
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric
replaceFeatures(Function & F,const std::string & Features)2540b57cec5SDimitry Andric void replaceFeatures(Function &F, const std::string &Features) {
2550b57cec5SDimitry Andric F.removeFnAttr("target-features");
2560b57cec5SDimitry Andric F.removeFnAttr("target-cpu");
2570b57cec5SDimitry Andric F.addFnAttr("target-features", Features);
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric
stripAtomics(Module & M)2600b57cec5SDimitry Andric bool stripAtomics(Module &M) {
2610b57cec5SDimitry Andric // Detect whether any atomics will be lowered, since there is no way to tell
2620b57cec5SDimitry Andric // whether the LowerAtomic pass lowers e.g. stores.
2630b57cec5SDimitry Andric bool Stripped = false;
2640b57cec5SDimitry Andric for (auto &F : M) {
2650b57cec5SDimitry Andric for (auto &B : F) {
2660b57cec5SDimitry Andric for (auto &I : B) {
2670b57cec5SDimitry Andric if (I.isAtomic()) {
2680b57cec5SDimitry Andric Stripped = true;
2690b57cec5SDimitry Andric goto done;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric done:
2760b57cec5SDimitry Andric if (!Stripped)
2770b57cec5SDimitry Andric return false;
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric LowerAtomicPass Lowerer;
2800b57cec5SDimitry Andric FunctionAnalysisManager FAM;
2810b57cec5SDimitry Andric for (auto &F : M)
2820b57cec5SDimitry Andric Lowerer.run(F, FAM);
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric return true;
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
stripThreadLocals(Module & M)2870b57cec5SDimitry Andric bool stripThreadLocals(Module &M) {
2880b57cec5SDimitry Andric bool Stripped = false;
2890b57cec5SDimitry Andric for (auto &GV : M.globals()) {
290e8d8bef9SDimitry Andric if (GV.isThreadLocal()) {
2910fca6ea1SDimitry Andric // replace `@llvm.threadlocal.address.pX(GV)` with `GV`.
2920fca6ea1SDimitry Andric for (Use &U : make_early_inc_range(GV.uses())) {
2930fca6ea1SDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U.getUser())) {
2940fca6ea1SDimitry Andric if (II->getIntrinsicID() == Intrinsic::threadlocal_address &&
2950fca6ea1SDimitry Andric II->getArgOperand(0) == &GV) {
2960fca6ea1SDimitry Andric II->replaceAllUsesWith(&GV);
2970fca6ea1SDimitry Andric II->eraseFromParent();
2980fca6ea1SDimitry Andric }
2990fca6ea1SDimitry Andric }
3000fca6ea1SDimitry Andric }
3010fca6ea1SDimitry Andric
3020b57cec5SDimitry Andric Stripped = true;
303e8d8bef9SDimitry Andric GV.setThreadLocal(false);
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric return Stripped;
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric
recordFeatures(Module & M,const FeatureBitset & Features,bool Stripped)3090b57cec5SDimitry Andric void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
3100b57cec5SDimitry Andric for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
3115ffd83dbSDimitry Andric if (Features[KV.Value]) {
3125ffd83dbSDimitry Andric // Mark features as used
3130b57cec5SDimitry Andric std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
3140b57cec5SDimitry Andric M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
3150b57cec5SDimitry Andric wasm::WASM_FEATURE_PREFIX_USED);
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric }
3185ffd83dbSDimitry Andric // Code compiled without atomics or bulk-memory may have had its atomics or
3195ffd83dbSDimitry Andric // thread-local data lowered to nonatomic operations or non-thread-local
3205ffd83dbSDimitry Andric // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
3215ffd83dbSDimitry Andric // to tell the linker that it would be unsafe to allow this code ot be used
3225ffd83dbSDimitry Andric // in a module with shared memory.
3235ffd83dbSDimitry Andric if (Stripped) {
3245ffd83dbSDimitry Andric M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
3255ffd83dbSDimitry Andric wasm::WASM_FEATURE_PREFIX_DISALLOWED);
3265ffd83dbSDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric };
3290b57cec5SDimitry Andric char CoalesceFeaturesAndStripAtomics::ID = 0;
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric /// WebAssembly Code Generator Pass Configuration Options.
3320b57cec5SDimitry Andric class WebAssemblyPassConfig final : public TargetPassConfig {
3330b57cec5SDimitry Andric public:
WebAssemblyPassConfig(WebAssemblyTargetMachine & TM,PassManagerBase & PM)3340b57cec5SDimitry Andric WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
3350b57cec5SDimitry Andric : TargetPassConfig(TM, PM) {}
3360b57cec5SDimitry Andric
getWebAssemblyTargetMachine() const3370b57cec5SDimitry Andric WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
3380b57cec5SDimitry Andric return getTM<WebAssemblyTargetMachine>();
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric FunctionPass *createTargetRegisterAllocator(bool) override;
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric void addIRPasses() override;
34481ad6265SDimitry Andric void addISelPrepare() override;
3450b57cec5SDimitry Andric bool addInstSelector() override;
346bdd1243dSDimitry Andric void addOptimizedRegAlloc() override;
3470b57cec5SDimitry Andric void addPostRegAlloc() override;
addGCPasses()3480b57cec5SDimitry Andric bool addGCPasses() override { return false; }
3490b57cec5SDimitry Andric void addPreEmitPass() override;
350349cc55cSDimitry Andric bool addPreISel() override;
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric // No reg alloc
addRegAssignAndRewriteFast()353e8d8bef9SDimitry Andric bool addRegAssignAndRewriteFast() override { return false; }
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric // No reg alloc
addRegAssignAndRewriteOptimized()356e8d8bef9SDimitry Andric bool addRegAssignAndRewriteOptimized() override { return false; }
3570b57cec5SDimitry Andric };
3580b57cec5SDimitry Andric } // end anonymous namespace
3590b57cec5SDimitry Andric
createMachineFunctionInfo(BumpPtrAllocator & Allocator,const Function & F,const TargetSubtargetInfo * STI) const360bdd1243dSDimitry Andric MachineFunctionInfo *WebAssemblyTargetMachine::createMachineFunctionInfo(
361bdd1243dSDimitry Andric BumpPtrAllocator &Allocator, const Function &F,
362bdd1243dSDimitry Andric const TargetSubtargetInfo *STI) const {
363bdd1243dSDimitry Andric return WebAssemblyFunctionInfo::create<WebAssemblyFunctionInfo>(Allocator, F,
364bdd1243dSDimitry Andric STI);
365bdd1243dSDimitry Andric }
366bdd1243dSDimitry Andric
3670b57cec5SDimitry Andric TargetTransformInfo
getTargetTransformInfo(const Function & F) const36881ad6265SDimitry Andric WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) const {
3690b57cec5SDimitry Andric return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric TargetPassConfig *
createPassConfig(PassManagerBase & PM)3730b57cec5SDimitry Andric WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
3740b57cec5SDimitry Andric return new WebAssemblyPassConfig(*this, PM);
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric
createTargetRegisterAllocator(bool)3770b57cec5SDimitry Andric FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
3780b57cec5SDimitry Andric return nullptr; // No reg alloc
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric
3810eae32dcSDimitry Andric using WebAssembly::WasmEnableEH;
3820eae32dcSDimitry Andric using WebAssembly::WasmEnableEmEH;
3830eae32dcSDimitry Andric using WebAssembly::WasmEnableEmSjLj;
3840fca6ea1SDimitry Andric using WebAssembly::WasmEnableExnref;
3850eae32dcSDimitry Andric using WebAssembly::WasmEnableSjLj;
3860eae32dcSDimitry Andric
basicCheckForEHAndSjLj(TargetMachine * TM)3870eae32dcSDimitry Andric static void basicCheckForEHAndSjLj(TargetMachine *TM) {
3880fca6ea1SDimitry Andric
3890fca6ea1SDimitry Andric // You can't enable two modes of EH at the same time
3900fca6ea1SDimitry Andric if (WasmEnableEmEH && WasmEnableEH)
3910fca6ea1SDimitry Andric report_fatal_error(
3920fca6ea1SDimitry Andric "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
3930fca6ea1SDimitry Andric // You can't enable two modes of SjLj at the same time
3940fca6ea1SDimitry Andric if (WasmEnableEmSjLj && WasmEnableSjLj)
3950fca6ea1SDimitry Andric report_fatal_error(
3960fca6ea1SDimitry Andric "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
3970fca6ea1SDimitry Andric // You can't mix Emscripten EH with Wasm SjLj.
3980fca6ea1SDimitry Andric if (WasmEnableEmEH && WasmEnableSjLj)
3990fca6ea1SDimitry Andric report_fatal_error(
4000fca6ea1SDimitry Andric "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
4010fca6ea1SDimitry Andric if (WasmEnableExnref && !WasmEnableEH)
4020fca6ea1SDimitry Andric report_fatal_error(
4030fca6ea1SDimitry Andric "-wasm-enable-exnref should be used with -wasm-enable-eh");
4040fca6ea1SDimitry Andric
4050fca6ea1SDimitry Andric // Here we make sure TargetOptions.ExceptionModel is the same as
4060eae32dcSDimitry Andric // MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
4070eae32dcSDimitry Andric // stores the exception model info in LangOptions, which is later transferred
4080eae32dcSDimitry Andric // to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
4090eae32dcSDimitry Andric // clang's LangOptions is not used and thus the exception model info is not
4100eae32dcSDimitry Andric // correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
4110fca6ea1SDimitry Andric // have the correct exception model in WebAssemblyMCAsmInfo constructor. But
4120fca6ea1SDimitry Andric // in this case TargetOptions is still not updated, so we make sure they are
4130fca6ea1SDimitry Andric // the same.
4140eae32dcSDimitry Andric TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType();
4150eae32dcSDimitry Andric
4164824e7fdSDimitry Andric // Basic Correctness checking related to -exception-model
417349cc55cSDimitry Andric if (TM->Options.ExceptionModel != ExceptionHandling::None &&
418349cc55cSDimitry Andric TM->Options.ExceptionModel != ExceptionHandling::Wasm)
419349cc55cSDimitry Andric report_fatal_error("-exception-model should be either 'none' or 'wasm'");
420349cc55cSDimitry Andric if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
421349cc55cSDimitry Andric report_fatal_error("-exception-model=wasm not allowed with "
422349cc55cSDimitry Andric "-enable-emscripten-cxx-exceptions");
423349cc55cSDimitry Andric if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
424349cc55cSDimitry Andric report_fatal_error(
425349cc55cSDimitry Andric "-wasm-enable-eh only allowed with -exception-model=wasm");
426349cc55cSDimitry Andric if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
427349cc55cSDimitry Andric report_fatal_error(
428349cc55cSDimitry Andric "-wasm-enable-sjlj only allowed with -exception-model=wasm");
429349cc55cSDimitry Andric if ((!WasmEnableEH && !WasmEnableSjLj) &&
430349cc55cSDimitry Andric TM->Options.ExceptionModel == ExceptionHandling::Wasm)
431349cc55cSDimitry Andric report_fatal_error(
432349cc55cSDimitry Andric "-exception-model=wasm only allowed with at least one of "
4330fca6ea1SDimitry Andric "-wasm-enable-eh or -wasm-enable-sjlj");
434349cc55cSDimitry Andric
435349cc55cSDimitry Andric // Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
436349cc55cSDimitry Andric // measure, but some code will error out at compile time in this combination.
437349cc55cSDimitry Andric // See WebAssemblyLowerEmscriptenEHSjLj pass for details.
438349cc55cSDimitry Andric }
439349cc55cSDimitry Andric
4400b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4410b57cec5SDimitry Andric // The following functions are called from lib/CodeGen/Passes.cpp to modify
4420b57cec5SDimitry Andric // the CodeGen pass sequence.
4430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
4440b57cec5SDimitry Andric
addIRPasses()4450b57cec5SDimitry Andric void WebAssemblyPassConfig::addIRPasses() {
4460b57cec5SDimitry Andric // Add signatures to prototype-less function declarations
4470b57cec5SDimitry Andric addPass(createWebAssemblyAddMissingPrototypes());
4480b57cec5SDimitry Andric
449bdd1243dSDimitry Andric // Lower .llvm.global_dtors into .llvm.global_ctors with __cxa_atexit calls.
45081ad6265SDimitry Andric addPass(createLowerGlobalDtorsLegacyPass());
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andric // Fix function bitcasts, as WebAssembly requires caller and callee signatures
4530b57cec5SDimitry Andric // to match.
4540b57cec5SDimitry Andric addPass(createWebAssemblyFixFunctionBitcasts());
4550b57cec5SDimitry Andric
4560b57cec5SDimitry Andric // Optimize "returned" function attributes.
4575f757f3fSDimitry Andric if (getOptLevel() != CodeGenOptLevel::None)
4580b57cec5SDimitry Andric addPass(createWebAssemblyOptimizeReturned());
4590b57cec5SDimitry Andric
4604824e7fdSDimitry Andric basicCheckForEHAndSjLj(TM);
461349cc55cSDimitry Andric
4620b57cec5SDimitry Andric // If exception handling is not enabled and setjmp/longjmp handling is
4630b57cec5SDimitry Andric // enabled, we lower invokes into calls and delete unreachable landingpad
4640b57cec5SDimitry Andric // blocks. Lowering invokes when there is no EH support is done in
465349cc55cSDimitry Andric // TargetPassConfig::addPassesToHandleExceptions, but that runs after these IR
466349cc55cSDimitry Andric // passes and Emscripten SjLj handling expects all invokes to be lowered
467349cc55cSDimitry Andric // before.
468349cc55cSDimitry Andric if (!WasmEnableEmEH && !WasmEnableEH) {
4690b57cec5SDimitry Andric addPass(createLowerInvokePass());
4700b57cec5SDimitry Andric // The lower invoke pass may create unreachable code. Remove it in order not
4710b57cec5SDimitry Andric // to process dead blocks in setjmp/longjmp handling.
4720b57cec5SDimitry Andric addPass(createUnreachableBlockEliminationPass());
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric
475349cc55cSDimitry Andric // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation
476349cc55cSDimitry Andric // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and
477349cc55cSDimitry Andric // transformation algorithms with Emscripten SjLj, so we run
478349cc55cSDimitry Andric // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled.
479349cc55cSDimitry Andric if (WasmEnableEmEH || WasmEnableEmSjLj || WasmEnableSjLj)
480349cc55cSDimitry Andric addPass(createWebAssemblyLowerEmscriptenEHSjLj());
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric // Expand indirectbr instructions to switches.
4830b57cec5SDimitry Andric addPass(createIndirectBrExpandPass());
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric TargetPassConfig::addIRPasses();
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric
addISelPrepare()48881ad6265SDimitry Andric void WebAssemblyPassConfig::addISelPrepare() {
4890fca6ea1SDimitry Andric // We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
4900fca6ea1SDimitry Andric // loads and stores are promoted to local.gets/local.sets.
4910fca6ea1SDimitry Andric addPass(createWebAssemblyRefTypeMem2Local());
49281ad6265SDimitry Andric // Lower atomics and TLS if necessary
49381ad6265SDimitry Andric addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
49481ad6265SDimitry Andric
49581ad6265SDimitry Andric // This is a no-op if atomics are not used in the module
4960fca6ea1SDimitry Andric addPass(createAtomicExpandLegacyPass());
49781ad6265SDimitry Andric
49881ad6265SDimitry Andric TargetPassConfig::addISelPrepare();
49981ad6265SDimitry Andric }
50081ad6265SDimitry Andric
addInstSelector()5010b57cec5SDimitry Andric bool WebAssemblyPassConfig::addInstSelector() {
5020b57cec5SDimitry Andric (void)TargetPassConfig::addInstSelector();
5030b57cec5SDimitry Andric addPass(
5040b57cec5SDimitry Andric createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
5050b57cec5SDimitry Andric // Run the argument-move pass immediately after the ScheduleDAG scheduler
5060b57cec5SDimitry Andric // so that we can fix up the ARGUMENT instructions before anything else
5070b57cec5SDimitry Andric // sees them in the wrong place.
5080b57cec5SDimitry Andric addPass(createWebAssemblyArgumentMove());
5090b57cec5SDimitry Andric // Set the p2align operands. This information is present during ISel, however
5100b57cec5SDimitry Andric // it's inconvenient to collect. Collect it now, and update the immediate
5110b57cec5SDimitry Andric // operands.
5120b57cec5SDimitry Andric addPass(createWebAssemblySetP2AlignOperands());
5135ffd83dbSDimitry Andric
5145ffd83dbSDimitry Andric // Eliminate range checks and add default targets to br_table instructions.
5155ffd83dbSDimitry Andric addPass(createWebAssemblyFixBrTableDefaults());
5165ffd83dbSDimitry Andric
5170fca6ea1SDimitry Andric // unreachable is terminator, non-terminator instruction after it is not
5180fca6ea1SDimitry Andric // allowed.
5190fca6ea1SDimitry Andric addPass(createWebAssemblyCleanCodeAfterTrap());
5200fca6ea1SDimitry Andric
5210b57cec5SDimitry Andric return false;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric
addOptimizedRegAlloc()524bdd1243dSDimitry Andric void WebAssemblyPassConfig::addOptimizedRegAlloc() {
525bdd1243dSDimitry Andric // Currently RegisterCoalesce degrades wasm debug info quality by a
526bdd1243dSDimitry Andric // significant margin. As a quick fix, disable this for -O1, which is often
527bdd1243dSDimitry Andric // used for debugging large applications. Disabling this increases code size
528bdd1243dSDimitry Andric // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is
529bdd1243dSDimitry Andric // usually not used for production builds.
530bdd1243dSDimitry Andric // TODO Investigate why RegisterCoalesce degrades debug info quality and fix
531bdd1243dSDimitry Andric // it properly
5325f757f3fSDimitry Andric if (getOptLevel() == CodeGenOptLevel::Less)
533bdd1243dSDimitry Andric disablePass(&RegisterCoalescerID);
534bdd1243dSDimitry Andric TargetPassConfig::addOptimizedRegAlloc();
535bdd1243dSDimitry Andric }
536bdd1243dSDimitry Andric
addPostRegAlloc()5370b57cec5SDimitry Andric void WebAssemblyPassConfig::addPostRegAlloc() {
5380b57cec5SDimitry Andric // TODO: The following CodeGen passes don't currently support code containing
5390b57cec5SDimitry Andric // virtual registers. Consider removing their restrictions and re-enabling
5400b57cec5SDimitry Andric // them.
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric // These functions all require the NoVRegs property.
543bdd1243dSDimitry Andric disablePass(&MachineLateInstrsCleanupID);
5440b57cec5SDimitry Andric disablePass(&MachineCopyPropagationID);
5450b57cec5SDimitry Andric disablePass(&PostRAMachineSinkingID);
5460b57cec5SDimitry Andric disablePass(&PostRASchedulerID);
5470b57cec5SDimitry Andric disablePass(&FuncletLayoutID);
5480b57cec5SDimitry Andric disablePass(&StackMapLivenessID);
5490b57cec5SDimitry Andric disablePass(&PatchableFunctionID);
5500b57cec5SDimitry Andric disablePass(&ShrinkWrapID);
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric // This pass hurts code size for wasm because it can generate irreducible
5530b57cec5SDimitry Andric // control flow.
5540b57cec5SDimitry Andric disablePass(&MachineBlockPlacementID);
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric TargetPassConfig::addPostRegAlloc();
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric
addPreEmitPass()5590b57cec5SDimitry Andric void WebAssemblyPassConfig::addPreEmitPass() {
5600b57cec5SDimitry Andric TargetPassConfig::addPreEmitPass();
5610b57cec5SDimitry Andric
562fe6060f1SDimitry Andric // Nullify DBG_VALUE_LISTs that we cannot handle.
563fe6060f1SDimitry Andric addPass(createWebAssemblyNullifyDebugValueLists());
564fe6060f1SDimitry Andric
5650b57cec5SDimitry Andric // Eliminate multiple-entry loops.
5665f757f3fSDimitry Andric if (!WasmDisableFixIrreducibleControlFlowPass)
5670b57cec5SDimitry Andric addPass(createWebAssemblyFixIrreducibleControlFlow());
5680b57cec5SDimitry Andric
5690b57cec5SDimitry Andric // Do various transformations for exception handling.
5700b57cec5SDimitry Andric // Every CFG-changing optimizations should come before this.
571e8d8bef9SDimitry Andric if (TM->Options.ExceptionModel == ExceptionHandling::Wasm)
5720b57cec5SDimitry Andric addPass(createWebAssemblyLateEHPrepare());
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andric // Now that we have a prologue and epilogue and all frame indices are
5750b57cec5SDimitry Andric // rewritten, eliminate SP and FP. This allows them to be stackified,
5760b57cec5SDimitry Andric // colored, and numbered with the rest of the registers.
5770b57cec5SDimitry Andric addPass(createWebAssemblyReplacePhysRegs());
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric // Preparations and optimizations related to register stackification.
5805f757f3fSDimitry Andric if (getOptLevel() != CodeGenOptLevel::None) {
5810b57cec5SDimitry Andric // Depend on LiveIntervals and perform some optimizations on it.
5820b57cec5SDimitry Andric addPass(createWebAssemblyOptimizeLiveIntervals());
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric // Prepare memory intrinsic calls for register stackifying.
5850b57cec5SDimitry Andric addPass(createWebAssemblyMemIntrinsicResults());
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric // Mark registers as representing wasm's value stack. This is a key
5880b57cec5SDimitry Andric // code-compression technique in WebAssembly. We run this pass (and
5890b57cec5SDimitry Andric // MemIntrinsicResults above) very late, so that it sees as much code as
5900b57cec5SDimitry Andric // possible, including code emitted by PEI and expanded by late tail
5910b57cec5SDimitry Andric // duplication.
5920b57cec5SDimitry Andric addPass(createWebAssemblyRegStackify());
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andric // Run the register coloring pass to reduce the total number of registers.
5950b57cec5SDimitry Andric // This runs after stackification so that it doesn't consider registers
5960b57cec5SDimitry Andric // that become stackified.
5970b57cec5SDimitry Andric addPass(createWebAssemblyRegColoring());
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andric // Sort the blocks of the CFG into topological order, a prerequisite for
6010b57cec5SDimitry Andric // BLOCK and LOOP markers.
6020b57cec5SDimitry Andric addPass(createWebAssemblyCFGSort());
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric // Insert BLOCK and LOOP markers.
6050b57cec5SDimitry Andric addPass(createWebAssemblyCFGStackify());
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andric // Insert explicit local.get and local.set operators.
6085ffd83dbSDimitry Andric if (!WasmDisableExplicitLocals)
6090b57cec5SDimitry Andric addPass(createWebAssemblyExplicitLocals());
6100b57cec5SDimitry Andric
6110b57cec5SDimitry Andric // Lower br_unless into br_if.
6120b57cec5SDimitry Andric addPass(createWebAssemblyLowerBrUnless());
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andric // Perform the very last peephole optimizations on the code.
6155f757f3fSDimitry Andric if (getOptLevel() != CodeGenOptLevel::None)
6160b57cec5SDimitry Andric addPass(createWebAssemblyPeephole());
6170b57cec5SDimitry Andric
6180b57cec5SDimitry Andric // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
6190b57cec5SDimitry Andric addPass(createWebAssemblyRegNumbering());
6205ffd83dbSDimitry Andric
6215ffd83dbSDimitry Andric // Fix debug_values whose defs have been stackified.
6225ffd83dbSDimitry Andric if (!WasmDisableExplicitLocals)
6235ffd83dbSDimitry Andric addPass(createWebAssemblyDebugFixup());
624fe6060f1SDimitry Andric
625fe6060f1SDimitry Andric // Collect information to prepare for MC lowering / asm printing.
626fe6060f1SDimitry Andric addPass(createWebAssemblyMCLowerPrePass());
6270b57cec5SDimitry Andric }
6280b57cec5SDimitry Andric
addPreISel()629349cc55cSDimitry Andric bool WebAssemblyPassConfig::addPreISel() {
630349cc55cSDimitry Andric TargetPassConfig::addPreISel();
631349cc55cSDimitry Andric addPass(createWebAssemblyLowerRefTypesIntPtrConv());
632349cc55cSDimitry Andric return false;
633349cc55cSDimitry Andric }
634349cc55cSDimitry Andric
6350b57cec5SDimitry Andric yaml::MachineFunctionInfo *
createDefaultFuncInfoYAML() const6360b57cec5SDimitry Andric WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
6370b57cec5SDimitry Andric return new yaml::WebAssemblyFunctionInfo();
6380b57cec5SDimitry Andric }
6390b57cec5SDimitry Andric
convertFuncInfoToYAML(const MachineFunction & MF) const6400b57cec5SDimitry Andric yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
6410b57cec5SDimitry Andric const MachineFunction &MF) const {
6420b57cec5SDimitry Andric const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
643bdd1243dSDimitry Andric return new yaml::WebAssemblyFunctionInfo(MF, *MFI);
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric
parseMachineFunctionInfo(const yaml::MachineFunctionInfo & MFI,PerFunctionMIParsingState & PFS,SMDiagnostic & Error,SMRange & SourceRange) const6460b57cec5SDimitry Andric bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
6470b57cec5SDimitry Andric const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
6480b57cec5SDimitry Andric SMDiagnostic &Error, SMRange &SourceRange) const {
64981ad6265SDimitry Andric const auto &YamlMFI = static_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
6500b57cec5SDimitry Andric MachineFunction &MF = PFS.MF;
651bdd1243dSDimitry Andric MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(MF, YamlMFI);
6520b57cec5SDimitry Andric return false;
6530b57cec5SDimitry Andric }
654