110e730a2SDan Gohman //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
210e730a2SDan Gohman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610e730a2SDan Gohman //
710e730a2SDan Gohman //===----------------------------------------------------------------------===//
810e730a2SDan Gohman ///
910e730a2SDan Gohman /// \file
105f8f34e4SAdrian Prantl /// This file defines the WebAssembly-specific subclass of TargetMachine.
1110e730a2SDan Gohman ///
1210e730a2SDan Gohman //===----------------------------------------------------------------------===//
1310e730a2SDan Gohman 
1410e730a2SDan Gohman #include "WebAssemblyTargetMachine.h"
156bda14b3SChandler Carruth #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16c6c42137SRichard Trieu #include "TargetInfo/WebAssemblyTargetInfo.h"
174625b848SHeejin Ahn #include "Utils/WebAssemblyUtilities.h"
186bda14b3SChandler Carruth #include "WebAssembly.h"
1952221d56SHeejin Ahn #include "WebAssemblyMachineFunctionInfo.h"
205bf22fc8SDan Gohman #include "WebAssemblyTargetObjectFile.h"
2110e730a2SDan Gohman #include "WebAssemblyTargetTransformInfo.h"
2252221d56SHeejin Ahn #include "llvm/CodeGen/MIRParser/MIParser.h"
2310e730a2SDan Gohman #include "llvm/CodeGen/MachineFunctionPass.h"
2410e730a2SDan Gohman #include "llvm/CodeGen/Passes.h"
2510e730a2SDan Gohman #include "llvm/CodeGen/RegAllocRegistry.h"
2631d19d43SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h"
2710e730a2SDan Gohman #include "llvm/IR/Function.h"
2864902d33SJulian Lettner #include "llvm/InitializePasses.h"
294625b848SHeejin Ahn #include "llvm/MC/MCAsmInfo.h"
3089b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
3110e730a2SDan Gohman #include "llvm/Target/TargetOptions.h"
3203855df1SJF Bastien #include "llvm/Transforms/Scalar.h"
3339f15686SMatt Arsenault #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
34a373d18eSDavid Blaikie #include "llvm/Transforms/Utils.h"
3510e730a2SDan Gohman using namespace llvm;
3610e730a2SDan Gohman 
3710e730a2SDan Gohman #define DEBUG_TYPE "wasm"
3810e730a2SDan Gohman 
392b7fe086SWouter van Oortmerssen // A command-line option to keep implicit locals
402b7fe086SWouter van Oortmerssen // for the purpose of testing with lit/llc ONLY.
412b7fe086SWouter van Oortmerssen // This produces output which is not valid WebAssembly, and is not supported
422b7fe086SWouter van Oortmerssen // by assemblers/disassemblers and other MC based tools.
432b7fe086SWouter van Oortmerssen static cl::opt<bool> WasmDisableExplicitLocals(
442b7fe086SWouter van Oortmerssen     "wasm-disable-explicit-locals", cl::Hidden,
452b7fe086SWouter van Oortmerssen     cl::desc("WebAssembly: output implicit locals in"
462b7fe086SWouter van Oortmerssen              " instruction output for test purposes only."),
472b7fe086SWouter van Oortmerssen     cl::init(false));
482b7fe086SWouter van Oortmerssen 
LLVMInitializeWebAssemblyTarget()490dbcb363STom Stellard extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
5010e730a2SDan Gohman   // Register the target.
51f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> X(
52f42454b9SMehdi Amini       getTheWebAssemblyTarget32());
53f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
54f42454b9SMehdi Amini       getTheWebAssemblyTarget64());
55f41f67d3SDerek Schuff 
5640926451SJacob Gravelle   // Register backend passes
5740926451SJacob Gravelle   auto &PR = *PassRegistry::getPassRegistry();
5892617559SSam Clegg   initializeWebAssemblyAddMissingPrototypesPass(PR);
5940926451SJacob Gravelle   initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
6064902d33SJulian Lettner   initializeLowerGlobalDtorsLegacyPassPass(PR);
6140926451SJacob Gravelle   initializeFixFunctionBitcastsPass(PR);
6240926451SJacob Gravelle   initializeOptimizeReturnedPass(PR);
6340926451SJacob Gravelle   initializeWebAssemblyArgumentMovePass(PR);
6440926451SJacob Gravelle   initializeWebAssemblySetP2AlignOperandsPass(PR);
6540926451SJacob Gravelle   initializeWebAssemblyReplacePhysRegsPass(PR);
6640926451SJacob Gravelle   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
67321d5220SHeejin Ahn   initializeWebAssemblyMemIntrinsicResultsPass(PR);
6840926451SJacob Gravelle   initializeWebAssemblyRegStackifyPass(PR);
6940926451SJacob Gravelle   initializeWebAssemblyRegColoringPass(PR);
70a64ebb86SHeejin Ahn   initializeWebAssemblyNullifyDebugValueListsPass(PR);
7140926451SJacob Gravelle   initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
724934f76bSHeejin Ahn   initializeWebAssemblyLateEHPreparePass(PR);
7304c48949SHeejin Ahn   initializeWebAssemblyExceptionInfoPass(PR);
7440926451SJacob Gravelle   initializeWebAssemblyCFGSortPass(PR);
7540926451SJacob Gravelle   initializeWebAssemblyCFGStackifyPass(PR);
76e9fd9073SHeejin Ahn   initializeWebAssemblyExplicitLocalsPass(PR);
7740926451SJacob Gravelle   initializeWebAssemblyLowerBrUnlessPass(PR);
7840926451SJacob Gravelle   initializeWebAssemblyRegNumberingPass(PR);
792b7fe086SWouter van Oortmerssen   initializeWebAssemblyDebugFixupPass(PR);
8040926451SJacob Gravelle   initializeWebAssemblyPeepholePass(PR);
819647a6f7SWouter van Oortmerssen   initializeWebAssemblyMCLowerPrePassPass(PR);
8210e730a2SDan Gohman }
8310e730a2SDan Gohman 
8410e730a2SDan Gohman //===----------------------------------------------------------------------===//
8510e730a2SDan Gohman // WebAssembly Lowering public interface.
8610e730a2SDan Gohman //===----------------------------------------------------------------------===//
8710e730a2SDan Gohman 
getEffectiveRelocModel(Optional<Reloc::Model> RM,const Triple & TT)8853572d04SDan Gohman static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM,
8953572d04SDan Gohman                                            const Triple &TT) {
90*e0e687a6SKazu Hirata   if (!RM) {
9174f5fd4eSSam Clegg     // Default to static relocation model.  This should always be more optimial
9274f5fd4eSSam Clegg     // than PIC since the static linker can determine all global addresses and
9374f5fd4eSSam Clegg     // assume direct function calls.
9474f5fd4eSSam Clegg     return Reloc::Static;
9574f5fd4eSSam Clegg   }
9653572d04SDan Gohman 
9753572d04SDan Gohman   if (!TT.isOSEmscripten()) {
9853572d04SDan Gohman     // Relocation modes other than static are currently implemented in a way
9953572d04SDan Gohman     // that only works for Emscripten, so disable them if we aren't targeting
10053572d04SDan Gohman     // Emscripten.
10153572d04SDan Gohman     return Reloc::Static;
10253572d04SDan Gohman   }
10353572d04SDan Gohman 
10441133a3eSDan Gohman   return *RM;
10541133a3eSDan Gohman }
10641133a3eSDan Gohman 
10710e730a2SDan Gohman /// Create an WebAssembly architecture model.
10810e730a2SDan Gohman ///
WebAssemblyTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)10910e730a2SDan Gohman WebAssemblyTargetMachine::WebAssemblyTargetMachine(
11010e730a2SDan Gohman     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
11141133a3eSDan Gohman     const TargetOptions &Options, Optional<Reloc::Model> RM,
112314ed201SDaniel Jasper     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
113ac02baabSDerek Schuff     : LLVMTargetMachine(
114ac02baabSDerek Schuff           T,
115d7086af2SPaulo Matos           TT.isArch64Bit()
1166d0c7bc1SPaulo Matos               ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
1176d0c7bc1SPaulo Matos                                        "f128:64-n32:64-S128-ni:1:10:20"
1186d0c7bc1SPaulo Matos                                      : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
1196d0c7bc1SPaulo Matos                                        "n32:64-S128-ni:1:10:20")
1206d0c7bc1SPaulo Matos               : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
1216d0c7bc1SPaulo Matos                                        "f128:64-n32:64-S128-ni:1:10:20"
1226d0c7bc1SPaulo Matos                                      : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
1236d0c7bc1SPaulo Matos                                        "n32:64-S128-ni:1:10:20"),
12453572d04SDan Gohman           TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
125ca29c271SDavid Green           getEffectiveCodeModel(CM, CodeModel::Large), OL),
126cf2a9e28SSam Clegg       TLOF(new WebAssemblyTargetObjectFile()) {
127e040533eSDan Gohman   // WebAssembly type-checks instructions, but a noreturn function with a return
128ffa143ceSDerek Schuff   // type that doesn't match the context will cause a check failure. So we lower
129ffa143ceSDerek Schuff   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
130e040533eSDan Gohman   // 'unreachable' instructions which is meant for that case.
131ffa143ceSDerek Schuff   this->Options.TrapUnreachable = true;
132ffa143ceSDerek Schuff 
133d934cb88SDan Gohman   // WebAssembly treats each function as an independent unit. Force
134d934cb88SDan Gohman   // -ffunction-sections, effectively, so that we can emit them independently.
135d934cb88SDan Gohman   this->Options.FunctionSections = true;
136d934cb88SDan Gohman   this->Options.DataSections = true;
137d934cb88SDan Gohman   this->Options.UniqueSectionNames = true;
138d934cb88SDan Gohman 
13910e730a2SDan Gohman   initAsmInfo();
14010e730a2SDan Gohman 
141d85ab7fcSDan Gohman   // Note that we don't use setRequiresStructuredCFG(true). It disables
142d85ab7fcSDan Gohman   // optimizations than we're ok with, and want, such as critical edge
143d85ab7fcSDan Gohman   // splitting and tail merging.
14410e730a2SDan Gohman }
14510e730a2SDan Gohman 
14618c56a07SHeejin Ahn WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
14710e730a2SDan Gohman 
getSubtargetImpl() const14889fe083cSThomas Lively const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const {
14989fe083cSThomas Lively   return getSubtargetImpl(std::string(getTargetCPU()),
15089fe083cSThomas Lively                           std::string(getTargetFeatureString()));
15189fe083cSThomas Lively }
15289fe083cSThomas Lively 
15310e730a2SDan Gohman const WebAssemblySubtarget *
getSubtargetImpl(std::string CPU,std::string FS) const154f3b4f990SThomas Lively WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
155f3b4f990SThomas Lively                                            std::string FS) const {
156f3b4f990SThomas Lively   auto &I = SubtargetMap[CPU + FS];
157f3b4f990SThomas Lively   if (!I) {
1580eaee545SJonas Devlieghere     I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
159f3b4f990SThomas Lively   }
160f3b4f990SThomas Lively   return I.get();
161f3b4f990SThomas Lively }
162f3b4f990SThomas Lively 
163f3b4f990SThomas Lively const WebAssemblySubtarget *
getSubtargetImpl(const Function & F) const16410e730a2SDan Gohman WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
16510e730a2SDan Gohman   Attribute CPUAttr = F.getFnAttribute("target-cpu");
16610e730a2SDan Gohman   Attribute FSAttr = F.getFnAttribute("target-features");
16710e730a2SDan Gohman 
168aab90384SCraig Topper   std::string CPU =
169aab90384SCraig Topper       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
170aab90384SCraig Topper   std::string FS =
171aab90384SCraig Topper       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
17210e730a2SDan Gohman 
17310e730a2SDan Gohman   // This needs to be done before we create a new subtarget since any
17410e730a2SDan Gohman   // creation will depend on the TM and the code generation flags on the
17510e730a2SDan Gohman   // function that reside in TargetOptions.
17610e730a2SDan Gohman   resetTargetOptions(F);
177f3b4f990SThomas Lively 
178f3b4f990SThomas Lively   return getSubtargetImpl(CPU, FS);
17910e730a2SDan Gohman }
18010e730a2SDan Gohman 
18110e730a2SDan Gohman namespace {
1823f34e1b8SThomas Lively 
1833f34e1b8SThomas Lively class CoalesceFeaturesAndStripAtomics final : public ModulePass {
1843f34e1b8SThomas Lively   // Take the union of all features used in the module and use it for each
1853f34e1b8SThomas Lively   // function individually, since having multiple feature sets in one module
1863f34e1b8SThomas Lively   // currently does not make sense for WebAssembly. If atomics are not enabled,
1873f34e1b8SThomas Lively   // also strip atomic operations and thread local storage.
18839b5367cSDerek Schuff   static char ID;
1893f34e1b8SThomas Lively   WebAssemblyTargetMachine *WasmTM;
19039b5367cSDerek Schuff 
19139b5367cSDerek Schuff public:
CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine * WasmTM)1923f34e1b8SThomas Lively   CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
1933f34e1b8SThomas Lively       : ModulePass(ID), WasmTM(WasmTM) {}
1943f34e1b8SThomas Lively 
runOnModule(Module & M)19539b5367cSDerek Schuff   bool runOnModule(Module &M) override {
1963f34e1b8SThomas Lively     FeatureBitset Features = coalesceFeatures(M);
1973f34e1b8SThomas Lively 
1983f34e1b8SThomas Lively     std::string FeatureStr = getFeatureString(Features);
19989fe083cSThomas Lively     WasmTM->setTargetFeatureString(FeatureStr);
2003f34e1b8SThomas Lively     for (auto &F : M)
2013f34e1b8SThomas Lively       replaceFeatures(F, FeatureStr);
2023f34e1b8SThomas Lively 
20342bba4b8SGuanzhong Chen     bool StrippedAtomics = false;
20442bba4b8SGuanzhong Chen     bool StrippedTLS = false;
2053f34e1b8SThomas Lively 
20659726668SDan Gohman     if (!Features[WebAssembly::FeatureAtomics]) {
20742bba4b8SGuanzhong Chen       StrippedAtomics = stripAtomics(M);
20842bba4b8SGuanzhong Chen       StrippedTLS = stripThreadLocals(M);
20959726668SDan Gohman     } else if (!Features[WebAssembly::FeatureBulkMemory]) {
21059726668SDan Gohman       StrippedTLS |= stripThreadLocals(M);
21159726668SDan Gohman     }
21242bba4b8SGuanzhong Chen 
21342bba4b8SGuanzhong Chen     if (StrippedAtomics && !StrippedTLS)
21442bba4b8SGuanzhong Chen       stripThreadLocals(M);
21542bba4b8SGuanzhong Chen     else if (StrippedTLS && !StrippedAtomics)
21642bba4b8SGuanzhong Chen       stripAtomics(M);
21742bba4b8SGuanzhong Chen 
21842bba4b8SGuanzhong Chen     recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
2193f34e1b8SThomas Lively 
2203f34e1b8SThomas Lively     // Conservatively assume we have made some change
22139b5367cSDerek Schuff     return true;
22239b5367cSDerek Schuff   }
2233f34e1b8SThomas Lively 
2243f34e1b8SThomas Lively private:
coalesceFeatures(const Module & M)2253f34e1b8SThomas Lively   FeatureBitset coalesceFeatures(const Module &M) {
2263f34e1b8SThomas Lively     FeatureBitset Features =
2273f34e1b8SThomas Lively         WasmTM
228adcd0268SBenjamin Kramer             ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
229adcd0268SBenjamin Kramer                                std::string(WasmTM->getTargetFeatureString()))
2303f34e1b8SThomas Lively             ->getFeatureBits();
2313f34e1b8SThomas Lively     for (auto &F : M)
2323f34e1b8SThomas Lively       Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
2333f34e1b8SThomas Lively     return Features;
2343f34e1b8SThomas Lively   }
2353f34e1b8SThomas Lively 
getFeatureString(const FeatureBitset & Features)2363f34e1b8SThomas Lively   std::string getFeatureString(const FeatureBitset &Features) {
2373f34e1b8SThomas Lively     std::string Ret;
2383f34e1b8SThomas Lively     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
2393f34e1b8SThomas Lively       if (Features[KV.Value])
2403f34e1b8SThomas Lively         Ret += (StringRef("+") + KV.Key + ",").str();
2413f34e1b8SThomas Lively     }
2423f34e1b8SThomas Lively     return Ret;
2433f34e1b8SThomas Lively   }
2443f34e1b8SThomas Lively 
replaceFeatures(Function & F,const std::string & Features)2453f34e1b8SThomas Lively   void replaceFeatures(Function &F, const std::string &Features) {
2463f34e1b8SThomas Lively     F.removeFnAttr("target-features");
2473f34e1b8SThomas Lively     F.removeFnAttr("target-cpu");
2483f34e1b8SThomas Lively     F.addFnAttr("target-features", Features);
2493f34e1b8SThomas Lively   }
2503f34e1b8SThomas Lively 
stripAtomics(Module & M)2513f34e1b8SThomas Lively   bool stripAtomics(Module &M) {
2523f34e1b8SThomas Lively     // Detect whether any atomics will be lowered, since there is no way to tell
2533f34e1b8SThomas Lively     // whether the LowerAtomic pass lowers e.g. stores.
2543f34e1b8SThomas Lively     bool Stripped = false;
2553f34e1b8SThomas Lively     for (auto &F : M) {
2563f34e1b8SThomas Lively       for (auto &B : F) {
2573f34e1b8SThomas Lively         for (auto &I : B) {
2583f34e1b8SThomas Lively           if (I.isAtomic()) {
2593f34e1b8SThomas Lively             Stripped = true;
2603f34e1b8SThomas Lively             goto done;
2613f34e1b8SThomas Lively           }
2623f34e1b8SThomas Lively         }
2633f34e1b8SThomas Lively       }
2643f34e1b8SThomas Lively     }
2653f34e1b8SThomas Lively 
2663f34e1b8SThomas Lively   done:
2673f34e1b8SThomas Lively     if (!Stripped)
2683f34e1b8SThomas Lively       return false;
2693f34e1b8SThomas Lively 
2703f34e1b8SThomas Lively     LowerAtomicPass Lowerer;
2713f34e1b8SThomas Lively     FunctionAnalysisManager FAM;
2723f34e1b8SThomas Lively     for (auto &F : M)
2733f34e1b8SThomas Lively       Lowerer.run(F, FAM);
2743f34e1b8SThomas Lively 
2753f34e1b8SThomas Lively     return true;
2763f34e1b8SThomas Lively   }
2773f34e1b8SThomas Lively 
stripThreadLocals(Module & M)2783f34e1b8SThomas Lively   bool stripThreadLocals(Module &M) {
2793f34e1b8SThomas Lively     bool Stripped = false;
2803f34e1b8SThomas Lively     for (auto &GV : M.globals()) {
281a28a4662SSam Clegg       if (GV.isThreadLocal()) {
2823f34e1b8SThomas Lively         Stripped = true;
283a28a4662SSam Clegg         GV.setThreadLocal(false);
2843f34e1b8SThomas Lively       }
2853f34e1b8SThomas Lively     }
2863f34e1b8SThomas Lively     return Stripped;
2873f34e1b8SThomas Lively   }
2883f34e1b8SThomas Lively 
recordFeatures(Module & M,const FeatureBitset & Features,bool Stripped)2893f34e1b8SThomas Lively   void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
2903f34e1b8SThomas Lively     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
291a1ae9566SThomas Lively       if (Features[KV.Value]) {
292a1ae9566SThomas Lively         // Mark features as used
2933f34e1b8SThomas Lively         std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
2943f34e1b8SThomas Lively         M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
2953f34e1b8SThomas Lively                         wasm::WASM_FEATURE_PREFIX_USED);
2963f34e1b8SThomas Lively       }
2973f34e1b8SThomas Lively     }
298a1ae9566SThomas Lively     // Code compiled without atomics or bulk-memory may have had its atomics or
299a1ae9566SThomas Lively     // thread-local data lowered to nonatomic operations or non-thread-local
300a1ae9566SThomas Lively     // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
301a1ae9566SThomas Lively     // to tell the linker that it would be unsafe to allow this code ot be used
302a1ae9566SThomas Lively     // in a module with shared memory.
303a1ae9566SThomas Lively     if (Stripped) {
304a1ae9566SThomas Lively       M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
305a1ae9566SThomas Lively                       wasm::WASM_FEATURE_PREFIX_DISALLOWED);
306a1ae9566SThomas Lively     }
3073f34e1b8SThomas Lively   }
30839b5367cSDerek Schuff };
3093f34e1b8SThomas Lively char CoalesceFeaturesAndStripAtomics::ID = 0;
31039b5367cSDerek Schuff 
31110e730a2SDan Gohman /// WebAssembly Code Generator Pass Configuration Options.
31210e730a2SDan Gohman class WebAssemblyPassConfig final : public TargetPassConfig {
31310e730a2SDan Gohman public:
WebAssemblyPassConfig(WebAssemblyTargetMachine & TM,PassManagerBase & PM)3145e394c3dSMatthias Braun   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
31510e730a2SDan Gohman       : TargetPassConfig(TM, PM) {}
31610e730a2SDan Gohman 
getWebAssemblyTargetMachine() const31710e730a2SDan Gohman   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
31810e730a2SDan Gohman     return getTM<WebAssemblyTargetMachine>();
31910e730a2SDan Gohman   }
32010e730a2SDan Gohman 
32110e730a2SDan Gohman   FunctionPass *createTargetRegisterAllocator(bool) override;
32210e730a2SDan Gohman 
32310e730a2SDan Gohman   void addIRPasses() override;
3244f9b8397SHeejin Ahn   void addISelPrepare() override;
32510e730a2SDan Gohman   bool addInstSelector() override;
32610e730a2SDan Gohman   void addPostRegAlloc() override;
addGCPasses()327ad154c83SDerek Schuff   bool addGCPasses() override { return false; }
32810e730a2SDan Gohman   void addPreEmitPass() override;
329d3a0a65bSPaulo Matos   bool addPreISel() override;
330cf55a657SMatt Arsenault 
331cf55a657SMatt Arsenault   // No reg alloc
addRegAssignAndRewriteFast()332c9122ddeSMatt Arsenault   bool addRegAssignAndRewriteFast() override { return false; }
333cf55a657SMatt Arsenault 
334cf55a657SMatt Arsenault   // No reg alloc
addRegAssignAndRewriteOptimized()335c9122ddeSMatt Arsenault   bool addRegAssignAndRewriteOptimized() override { return false; }
33610e730a2SDan Gohman };
33710e730a2SDan Gohman } // end anonymous namespace
33810e730a2SDan Gohman 
33926d11ca4SSanjoy Das TargetTransformInfo
getTargetTransformInfo(const Function & F) const340c4b1a63aSJameson Nash WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) const {
34110e730a2SDan Gohman   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
34210e730a2SDan Gohman }
34310e730a2SDan Gohman 
34410e730a2SDan Gohman TargetPassConfig *
createPassConfig(PassManagerBase & PM)34510e730a2SDan Gohman WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
3465e394c3dSMatthias Braun   return new WebAssemblyPassConfig(*this, PM);
34710e730a2SDan Gohman }
34810e730a2SDan Gohman 
createTargetRegisterAllocator(bool)34910e730a2SDan Gohman FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
35010e730a2SDan Gohman   return nullptr; // No reg alloc
35110e730a2SDan Gohman }
35210e730a2SDan Gohman 
3534625b848SHeejin Ahn using WebAssembly::WasmEnableEH;
3544625b848SHeejin Ahn using WebAssembly::WasmEnableEmEH;
3554625b848SHeejin Ahn using WebAssembly::WasmEnableEmSjLj;
3564625b848SHeejin Ahn using WebAssembly::WasmEnableSjLj;
3574625b848SHeejin Ahn 
basicCheckForEHAndSjLj(TargetMachine * TM)3584625b848SHeejin Ahn static void basicCheckForEHAndSjLj(TargetMachine *TM) {
3594625b848SHeejin Ahn   // Before checking, we make sure TargetOptions.ExceptionModel is the same as
3604625b848SHeejin Ahn   // MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
3614625b848SHeejin Ahn   // stores the exception model info in LangOptions, which is later transferred
3624625b848SHeejin Ahn   // to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
3634625b848SHeejin Ahn   // clang's LangOptions is not used and thus the exception model info is not
3644625b848SHeejin Ahn   // correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
3654625b848SHeejin Ahn   // have the correct exception model in in WebAssemblyMCAsmInfo constructor.
3664625b848SHeejin Ahn   // But in this case TargetOptions is still not updated, so we make sure they
3674625b848SHeejin Ahn   // are the same.
3684625b848SHeejin Ahn   TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType();
3694625b848SHeejin Ahn 
37095875d24SZarko Todorovski   // Basic Correctness checking related to -exception-model
37177b921b8SHeejin Ahn   if (TM->Options.ExceptionModel != ExceptionHandling::None &&
37277b921b8SHeejin Ahn       TM->Options.ExceptionModel != ExceptionHandling::Wasm)
37377b921b8SHeejin Ahn     report_fatal_error("-exception-model should be either 'none' or 'wasm'");
37477b921b8SHeejin Ahn   if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
37577b921b8SHeejin Ahn     report_fatal_error("-exception-model=wasm not allowed with "
37677b921b8SHeejin Ahn                        "-enable-emscripten-cxx-exceptions");
37777b921b8SHeejin Ahn   if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
37877b921b8SHeejin Ahn     report_fatal_error(
37977b921b8SHeejin Ahn         "-wasm-enable-eh only allowed with -exception-model=wasm");
38077b921b8SHeejin Ahn   if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
38177b921b8SHeejin Ahn     report_fatal_error(
38277b921b8SHeejin Ahn         "-wasm-enable-sjlj only allowed with -exception-model=wasm");
38377b921b8SHeejin Ahn   if ((!WasmEnableEH && !WasmEnableSjLj) &&
38477b921b8SHeejin Ahn       TM->Options.ExceptionModel == ExceptionHandling::Wasm)
38577b921b8SHeejin Ahn     report_fatal_error(
38677b921b8SHeejin Ahn         "-exception-model=wasm only allowed with at least one of "
38777b921b8SHeejin Ahn         "-wasm-enable-eh or -wasm-enable-sjj");
38877b921b8SHeejin Ahn 
38977b921b8SHeejin Ahn   // You can't enable two modes of EH at the same time
39077b921b8SHeejin Ahn   if (WasmEnableEmEH && WasmEnableEH)
39177b921b8SHeejin Ahn     report_fatal_error(
39277b921b8SHeejin Ahn         "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
39377b921b8SHeejin Ahn   // You can't enable two modes of SjLj at the same time
39477b921b8SHeejin Ahn   if (WasmEnableEmSjLj && WasmEnableSjLj)
39577b921b8SHeejin Ahn     report_fatal_error(
39677b921b8SHeejin Ahn         "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
39777b921b8SHeejin Ahn   // You can't mix Emscripten EH with Wasm SjLj.
39877b921b8SHeejin Ahn   if (WasmEnableEmEH && WasmEnableSjLj)
39977b921b8SHeejin Ahn     report_fatal_error(
40077b921b8SHeejin Ahn         "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
40177b921b8SHeejin Ahn   // Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
40277b921b8SHeejin Ahn   // measure, but some code will error out at compile time in this combination.
40377b921b8SHeejin Ahn   // See WebAssemblyLowerEmscriptenEHSjLj pass for details.
40477b921b8SHeejin Ahn }
40577b921b8SHeejin Ahn 
40610e730a2SDan Gohman //===----------------------------------------------------------------------===//
40710e730a2SDan Gohman // The following functions are called from lib/CodeGen/Passes.cpp to modify
40810e730a2SDan Gohman // the CodeGen pass sequence.
40910e730a2SDan Gohman //===----------------------------------------------------------------------===//
41010e730a2SDan Gohman 
addIRPasses()41110e730a2SDan Gohman void WebAssemblyPassConfig::addIRPasses() {
41292617559SSam Clegg   // Add signatures to prototype-less function declarations
41392617559SSam Clegg   addPass(createWebAssemblyAddMissingPrototypes());
41492617559SSam Clegg 
415bafe6902SSam Clegg   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
41664902d33SJulian Lettner   addPass(createLowerGlobalDtorsLegacyPass());
417bafe6902SSam Clegg 
4181b637458SDan Gohman   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
4191b637458SDan Gohman   // to match.
4201b637458SDan Gohman   addPass(createWebAssemblyFixFunctionBitcasts());
4211b637458SDan Gohman 
42281719f85SDan Gohman   // Optimize "returned" function attributes.
423b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
42481719f85SDan Gohman     addPass(createWebAssemblyOptimizeReturned());
42581719f85SDan Gohman 
42695875d24SZarko Todorovski   basicCheckForEHAndSjLj(TM);
42777b921b8SHeejin Ahn 
428c0f18172SHeejin Ahn   // If exception handling is not enabled and setjmp/longjmp handling is
429c0f18172SHeejin Ahn   // enabled, we lower invokes into calls and delete unreachable landingpad
430c0f18172SHeejin Ahn   // blocks. Lowering invokes when there is no EH support is done in
43177b921b8SHeejin Ahn   // TargetPassConfig::addPassesToHandleExceptions, but that runs after these IR
43277b921b8SHeejin Ahn   // passes and Emscripten SjLj handling expects all invokes to be lowered
43377b921b8SHeejin Ahn   // before.
43477b921b8SHeejin Ahn   if (!WasmEnableEmEH && !WasmEnableEH) {
435c0f18172SHeejin Ahn     addPass(createLowerInvokePass());
436c0f18172SHeejin Ahn     // The lower invoke pass may create unreachable code. Remove it in order not
437c0f18172SHeejin Ahn     // to process dead blocks in setjmp/longjmp handling.
438c0f18172SHeejin Ahn     addPass(createUnreachableBlockEliminationPass());
439c0f18172SHeejin Ahn   }
440c0f18172SHeejin Ahn 
44177b921b8SHeejin Ahn   // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation
44277b921b8SHeejin Ahn   // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and
44377b921b8SHeejin Ahn   // transformation algorithms with Emscripten SjLj, so we run
44477b921b8SHeejin Ahn   // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled.
44577b921b8SHeejin Ahn   if (WasmEnableEmEH || WasmEnableEmSjLj || WasmEnableSjLj)
44677b921b8SHeejin Ahn     addPass(createWebAssemblyLowerEmscriptenEHSjLj());
447f41f67d3SDerek Schuff 
448ec4be576SDerek Schuff   // Expand indirectbr instructions to switches.
449ec4be576SDerek Schuff   addPass(createIndirectBrExpandPass());
450ec4be576SDerek Schuff 
45110e730a2SDan Gohman   TargetPassConfig::addIRPasses();
45210e730a2SDan Gohman }
45310e730a2SDan Gohman 
addISelPrepare()4544f9b8397SHeejin Ahn void WebAssemblyPassConfig::addISelPrepare() {
4554f9b8397SHeejin Ahn   // Lower atomics and TLS if necessary
4564f9b8397SHeejin Ahn   addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
4574f9b8397SHeejin Ahn 
4584f9b8397SHeejin Ahn   // This is a no-op if atomics are not used in the module
4594f9b8397SHeejin Ahn   addPass(createAtomicExpandPass());
4604f9b8397SHeejin Ahn 
4614f9b8397SHeejin Ahn   TargetPassConfig::addISelPrepare();
4624f9b8397SHeejin Ahn }
4634f9b8397SHeejin Ahn 
addInstSelector()46410e730a2SDan Gohman bool WebAssemblyPassConfig::addInstSelector() {
465b0921ca9SDan Gohman   (void)TargetPassConfig::addInstSelector();
46610e730a2SDan Gohman   addPass(
46710e730a2SDan Gohman       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
4681cf96c0cSDan Gohman   // Run the argument-move pass immediately after the ScheduleDAG scheduler
4691cf96c0cSDan Gohman   // so that we can fix up the ARGUMENT instructions before anything else
4701cf96c0cSDan Gohman   // sees them in the wrong place.
4711cf96c0cSDan Gohman   addPass(createWebAssemblyArgumentMove());
472bb372243SDan Gohman   // Set the p2align operands. This information is present during ISel, however
473bb372243SDan Gohman   // it's inconvenient to collect. Collect it now, and update the immediate
474bb372243SDan Gohman   // operands.
475bb372243SDan Gohman   addPass(createWebAssemblySetP2AlignOperands());
4767f50c15bSThomas Lively 
4777f50c15bSThomas Lively   // Eliminate range checks and add default targets to br_table instructions.
4787f50c15bSThomas Lively   addPass(createWebAssemblyFixBrTableDefaults());
4797f50c15bSThomas Lively 
48010e730a2SDan Gohman   return false;
48110e730a2SDan Gohman }
48210e730a2SDan Gohman 
addPostRegAlloc()483600aee98SJF Bastien void WebAssemblyPassConfig::addPostRegAlloc() {
4849c54d3b4SDan Gohman   // TODO: The following CodeGen passes don't currently support code containing
4859c54d3b4SDan Gohman   // virtual registers. Consider removing their restrictions and re-enabling
4869c54d3b4SDan Gohman   // them.
487ad154c83SDerek Schuff 
4881eb47368SMatthias Braun   // These functions all require the NoVRegs property.
489600aee98SJF Bastien   disablePass(&MachineCopyPropagationID);
4907ab1b32bSJun Bum Lim   disablePass(&PostRAMachineSinkingID);
491ecabac62SDerek Schuff   disablePass(&PostRASchedulerID);
492ecabac62SDerek Schuff   disablePass(&FuncletLayoutID);
493ecabac62SDerek Schuff   disablePass(&StackMapLivenessID);
494ecabac62SDerek Schuff   disablePass(&LiveDebugValuesID);
495fe71ec77SSanjoy Das   disablePass(&PatchableFunctionID);
4967ab1b32bSJun Bum Lim   disablePass(&ShrinkWrapID);
497950a13cfSDan Gohman 
498ef9d6aeaSHeejin Ahn   // This pass hurts code size for wasm because it can generate irreducible
499ef9d6aeaSHeejin Ahn   // control flow.
500ef9d6aeaSHeejin Ahn   disablePass(&MachineBlockPlacementID);
501ef9d6aeaSHeejin Ahn 
502b0921ca9SDan Gohman   TargetPassConfig::addPostRegAlloc();
503600aee98SJF Bastien }
50410e730a2SDan Gohman 
addPreEmitPass()505950a13cfSDan Gohman void WebAssemblyPassConfig::addPreEmitPass() {
506b0921ca9SDan Gohman   TargetPassConfig::addPreEmitPass();
507b0921ca9SDan Gohman 
508a64ebb86SHeejin Ahn   // Nullify DBG_VALUE_LISTs that we cannot handle.
509a64ebb86SHeejin Ahn   addPass(createWebAssemblyNullifyDebugValueLists());
510a64ebb86SHeejin Ahn 
511e95056d6SHeejin Ahn   // Eliminate multiple-entry loops.
512e95056d6SHeejin Ahn   addPass(createWebAssemblyFixIrreducibleControlFlow());
513e95056d6SHeejin Ahn 
514e95056d6SHeejin Ahn   // Do various transformations for exception handling.
515d6f48786SHeejin Ahn   // Every CFG-changing optimizations should come before this.
5169e4eadebSHeejin Ahn   if (TM->Options.ExceptionModel == ExceptionHandling::Wasm)
517e95056d6SHeejin Ahn     addPass(createWebAssemblyLateEHPrepare());
518e95056d6SHeejin Ahn 
5190bb98650SHeejin Ahn   // Now that we have a prologue and epilogue and all frame indices are
5200bb98650SHeejin Ahn   // rewritten, eliminate SP and FP. This allows them to be stackified,
5210bb98650SHeejin Ahn   // colored, and numbered with the rest of the registers.
5220bb98650SHeejin Ahn   addPass(createWebAssemblyReplacePhysRegs());
5230bb98650SHeejin Ahn 
524d6f48786SHeejin Ahn   // Preparations and optimizations related to register stackification.
5250cfb5f85SDan Gohman   if (getOptLevel() != CodeGenOpt::None) {
5260cfb5f85SDan Gohman     // Depend on LiveIntervals and perform some optimizations on it.
5270cfb5f85SDan Gohman     addPass(createWebAssemblyOptimizeLiveIntervals());
5280cfb5f85SDan Gohman 
529321d5220SHeejin Ahn     // Prepare memory intrinsic calls for register stackifying.
530321d5220SHeejin Ahn     addPass(createWebAssemblyMemIntrinsicResults());
5310cfb5f85SDan Gohman 
532e040533eSDan Gohman     // Mark registers as representing wasm's value stack. This is a key
5330cfb5f85SDan Gohman     // code-compression technique in WebAssembly. We run this pass (and
534321d5220SHeejin Ahn     // MemIntrinsicResults above) very late, so that it sees as much code as
535321d5220SHeejin Ahn     // possible, including code emitted by PEI and expanded by late tail
536321d5220SHeejin Ahn     // duplication.
5370cfb5f85SDan Gohman     addPass(createWebAssemblyRegStackify());
5380cfb5f85SDan Gohman 
5390cfb5f85SDan Gohman     // Run the register coloring pass to reduce the total number of registers.
5400cfb5f85SDan Gohman     // This runs after stackification so that it doesn't consider registers
5410cfb5f85SDan Gohman     // that become stackified.
5420cfb5f85SDan Gohman     addPass(createWebAssemblyRegColoring());
5430cfb5f85SDan Gohman   }
5440cfb5f85SDan Gohman 
545f52ee17aSDan Gohman   // Sort the blocks of the CFG into topological order, a prerequisite for
546f52ee17aSDan Gohman   // BLOCK and LOOP markers.
547f52ee17aSDan Gohman   addPass(createWebAssemblyCFGSort());
548f52ee17aSDan Gohman 
549f52ee17aSDan Gohman   // Insert BLOCK and LOOP markers.
550950a13cfSDan Gohman   addPass(createWebAssemblyCFGStackify());
5515941bde0SDan Gohman 
552e9fd9073SHeejin Ahn   // Insert explicit local.get and local.set operators.
5532b7fe086SWouter van Oortmerssen   if (!WasmDisableExplicitLocals)
554e9fd9073SHeejin Ahn     addPass(createWebAssemblyExplicitLocals());
555e9fd9073SHeejin Ahn 
556f0b165a7SDan Gohman   // Lower br_unless into br_if.
557f0b165a7SDan Gohman   addPass(createWebAssemblyLowerBrUnless());
558f0b165a7SDan Gohman 
5595941bde0SDan Gohman   // Perform the very last peephole optimizations on the code.
560b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
56181719f85SDan Gohman     addPass(createWebAssemblyPeephole());
562b7c2400fSDan Gohman 
563b7c2400fSDan Gohman   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
564b7c2400fSDan Gohman   addPass(createWebAssemblyRegNumbering());
5652b7fe086SWouter van Oortmerssen 
5662b7fe086SWouter van Oortmerssen   // Fix debug_values whose defs have been stackified.
5672b7fe086SWouter van Oortmerssen   if (!WasmDisableExplicitLocals)
5682b7fe086SWouter van Oortmerssen     addPass(createWebAssemblyDebugFixup());
5699647a6f7SWouter van Oortmerssen 
5709647a6f7SWouter van Oortmerssen   // Collect information to prepare for MC lowering / asm printing.
5719647a6f7SWouter van Oortmerssen   addPass(createWebAssemblyMCLowerPrePass());
572950a13cfSDan Gohman }
57352221d56SHeejin Ahn 
addPreISel()574d3a0a65bSPaulo Matos bool WebAssemblyPassConfig::addPreISel() {
575d3a0a65bSPaulo Matos   TargetPassConfig::addPreISel();
576d3a0a65bSPaulo Matos   addPass(createWebAssemblyLowerRefTypesIntPtrConv());
577d3a0a65bSPaulo Matos   return false;
578d3a0a65bSPaulo Matos }
579d3a0a65bSPaulo Matos 
58052221d56SHeejin Ahn yaml::MachineFunctionInfo *
createDefaultFuncInfoYAML() const58152221d56SHeejin Ahn WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
58252221d56SHeejin Ahn   return new yaml::WebAssemblyFunctionInfo();
58352221d56SHeejin Ahn }
58452221d56SHeejin Ahn 
convertFuncInfoToYAML(const MachineFunction & MF) const58552221d56SHeejin Ahn yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
58652221d56SHeejin Ahn     const MachineFunction &MF) const {
58752221d56SHeejin Ahn   const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
58852221d56SHeejin Ahn   return new yaml::WebAssemblyFunctionInfo(*MFI);
58952221d56SHeejin Ahn }
59052221d56SHeejin Ahn 
parseMachineFunctionInfo(const yaml::MachineFunctionInfo & MFI,PerFunctionMIParsingState & PFS,SMDiagnostic & Error,SMRange & SourceRange) const59152221d56SHeejin Ahn bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
59252221d56SHeejin Ahn     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
59352221d56SHeejin Ahn     SMDiagnostic &Error, SMRange &SourceRange) const {
5941235aaefSCraig Topper   const auto &YamlMFI = static_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
59552221d56SHeejin Ahn   MachineFunction &MF = PFS.MF;
59652221d56SHeejin Ahn   MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
59752221d56SHeejin Ahn   return false;
59852221d56SHeejin Ahn }
599