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" 180b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h" 190b57cec5SDimitry Andric #include "WebAssemblyTargetObjectFile.h" 200b57cec5SDimitry Andric #include "WebAssemblyTargetTransformInfo.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/RegAllocRegistry.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 260b57cec5SDimitry Andric #include "llvm/IR/Function.h" 270b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 280b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 290b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h" 300b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LowerAtomic.h" 310b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h" 320b57cec5SDimitry Andric using namespace llvm; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric #define DEBUG_TYPE "wasm" 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // Emscripten's asm.js-style exception handling 370b57cec5SDimitry Andric static cl::opt<bool> EnableEmException( 380b57cec5SDimitry Andric "enable-emscripten-cxx-exceptions", 390b57cec5SDimitry Andric cl::desc("WebAssembly Emscripten-style exception handling"), 400b57cec5SDimitry Andric cl::init(false)); 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric // Emscripten's asm.js-style setjmp/longjmp handling 430b57cec5SDimitry Andric static cl::opt<bool> EnableEmSjLj( 440b57cec5SDimitry Andric "enable-emscripten-sjlj", 450b57cec5SDimitry Andric cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), 460b57cec5SDimitry Andric cl::init(false)); 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric extern "C" void LLVMInitializeWebAssemblyTarget() { 490b57cec5SDimitry Andric // Register the target. 500b57cec5SDimitry Andric RegisterTargetMachine<WebAssemblyTargetMachine> X( 510b57cec5SDimitry Andric getTheWebAssemblyTarget32()); 520b57cec5SDimitry Andric RegisterTargetMachine<WebAssemblyTargetMachine> Y( 530b57cec5SDimitry Andric getTheWebAssemblyTarget64()); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Register backend passes 560b57cec5SDimitry Andric auto &PR = *PassRegistry::getPassRegistry(); 570b57cec5SDimitry Andric initializeWebAssemblyAddMissingPrototypesPass(PR); 580b57cec5SDimitry Andric initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR); 590b57cec5SDimitry Andric initializeLowerGlobalDtorsPass(PR); 600b57cec5SDimitry Andric initializeFixFunctionBitcastsPass(PR); 610b57cec5SDimitry Andric initializeOptimizeReturnedPass(PR); 620b57cec5SDimitry Andric initializeWebAssemblyArgumentMovePass(PR); 630b57cec5SDimitry Andric initializeWebAssemblySetP2AlignOperandsPass(PR); 640b57cec5SDimitry Andric initializeWebAssemblyReplacePhysRegsPass(PR); 650b57cec5SDimitry Andric initializeWebAssemblyPrepareForLiveIntervalsPass(PR); 660b57cec5SDimitry Andric initializeWebAssemblyOptimizeLiveIntervalsPass(PR); 670b57cec5SDimitry Andric initializeWebAssemblyMemIntrinsicResultsPass(PR); 680b57cec5SDimitry Andric initializeWebAssemblyRegStackifyPass(PR); 690b57cec5SDimitry Andric initializeWebAssemblyRegColoringPass(PR); 700b57cec5SDimitry Andric initializeWebAssemblyFixIrreducibleControlFlowPass(PR); 710b57cec5SDimitry Andric initializeWebAssemblyLateEHPreparePass(PR); 720b57cec5SDimitry Andric initializeWebAssemblyExceptionInfoPass(PR); 730b57cec5SDimitry Andric initializeWebAssemblyCFGSortPass(PR); 740b57cec5SDimitry Andric initializeWebAssemblyCFGStackifyPass(PR); 750b57cec5SDimitry Andric initializeWebAssemblyExplicitLocalsPass(PR); 760b57cec5SDimitry Andric initializeWebAssemblyLowerBrUnlessPass(PR); 770b57cec5SDimitry Andric initializeWebAssemblyRegNumberingPass(PR); 780b57cec5SDimitry Andric initializeWebAssemblyPeepholePass(PR); 790b57cec5SDimitry Andric initializeWebAssemblyCallIndirectFixupPass(PR); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 830b57cec5SDimitry Andric // WebAssembly Lowering public interface. 840b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM, 870b57cec5SDimitry Andric const Triple &TT) { 880b57cec5SDimitry Andric if (!RM.hasValue()) { 890b57cec5SDimitry Andric // Default to static relocation model. This should always be more optimial 900b57cec5SDimitry Andric // than PIC since the static linker can determine all global addresses and 910b57cec5SDimitry Andric // assume direct function calls. 920b57cec5SDimitry Andric return Reloc::Static; 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric if (!TT.isOSEmscripten()) { 960b57cec5SDimitry Andric // Relocation modes other than static are currently implemented in a way 970b57cec5SDimitry Andric // that only works for Emscripten, so disable them if we aren't targeting 980b57cec5SDimitry Andric // Emscripten. 990b57cec5SDimitry Andric return Reloc::Static; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric return *RM; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric /// Create an WebAssembly architecture model. 1060b57cec5SDimitry Andric /// 1070b57cec5SDimitry Andric WebAssemblyTargetMachine::WebAssemblyTargetMachine( 1080b57cec5SDimitry Andric const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 1090b57cec5SDimitry Andric const TargetOptions &Options, Optional<Reloc::Model> RM, 1100b57cec5SDimitry Andric Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) 1110b57cec5SDimitry Andric : LLVMTargetMachine(T, 1120b57cec5SDimitry Andric TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128" 1130b57cec5SDimitry Andric : "e-m:e-p:32:32-i64:64-n32:64-S128", 1140b57cec5SDimitry Andric TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT), 1150b57cec5SDimitry Andric getEffectiveCodeModel(CM, CodeModel::Large), OL), 1160b57cec5SDimitry Andric TLOF(new WebAssemblyTargetObjectFile()) { 1170b57cec5SDimitry Andric // WebAssembly type-checks instructions, but a noreturn function with a return 1180b57cec5SDimitry Andric // type that doesn't match the context will cause a check failure. So we lower 1190b57cec5SDimitry Andric // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's 1200b57cec5SDimitry Andric // 'unreachable' instructions which is meant for that case. 1210b57cec5SDimitry Andric this->Options.TrapUnreachable = true; 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // WebAssembly treats each function as an independent unit. Force 1240b57cec5SDimitry Andric // -ffunction-sections, effectively, so that we can emit them independently. 1250b57cec5SDimitry Andric this->Options.FunctionSections = true; 1260b57cec5SDimitry Andric this->Options.DataSections = true; 1270b57cec5SDimitry Andric this->Options.UniqueSectionNames = true; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric initAsmInfo(); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Note that we don't use setRequiresStructuredCFG(true). It disables 1320b57cec5SDimitry Andric // optimizations than we're ok with, and want, such as critical edge 1330b57cec5SDimitry Andric // splitting and tail merging. 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor. 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric const WebAssemblySubtarget * 1390b57cec5SDimitry Andric WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU, 1400b57cec5SDimitry Andric std::string FS) const { 1410b57cec5SDimitry Andric auto &I = SubtargetMap[CPU + FS]; 1420b57cec5SDimitry Andric if (!I) { 143*8bcb0991SDimitry Andric I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric return I.get(); 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric const WebAssemblySubtarget * 1490b57cec5SDimitry Andric WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { 1500b57cec5SDimitry Andric Attribute CPUAttr = F.getFnAttribute("target-cpu"); 1510b57cec5SDimitry Andric Attribute FSAttr = F.getFnAttribute("target-features"); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 1540b57cec5SDimitry Andric ? CPUAttr.getValueAsString().str() 1550b57cec5SDimitry Andric : TargetCPU; 1560b57cec5SDimitry Andric std::string FS = !FSAttr.hasAttribute(Attribute::None) 1570b57cec5SDimitry Andric ? FSAttr.getValueAsString().str() 1580b57cec5SDimitry Andric : TargetFS; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // This needs to be done before we create a new subtarget since any 1610b57cec5SDimitry Andric // creation will depend on the TM and the code generation flags on the 1620b57cec5SDimitry Andric // function that reside in TargetOptions. 1630b57cec5SDimitry Andric resetTargetOptions(F); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric return getSubtargetImpl(CPU, FS); 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric namespace { 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric class CoalesceFeaturesAndStripAtomics final : public ModulePass { 1710b57cec5SDimitry Andric // Take the union of all features used in the module and use it for each 1720b57cec5SDimitry Andric // function individually, since having multiple feature sets in one module 1730b57cec5SDimitry Andric // currently does not make sense for WebAssembly. If atomics are not enabled, 1740b57cec5SDimitry Andric // also strip atomic operations and thread local storage. 1750b57cec5SDimitry Andric static char ID; 1760b57cec5SDimitry Andric WebAssemblyTargetMachine *WasmTM; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric public: 1790b57cec5SDimitry Andric CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM) 1800b57cec5SDimitry Andric : ModulePass(ID), WasmTM(WasmTM) {} 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric bool runOnModule(Module &M) override { 1830b57cec5SDimitry Andric FeatureBitset Features = coalesceFeatures(M); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric std::string FeatureStr = getFeatureString(Features); 1860b57cec5SDimitry Andric for (auto &F : M) 1870b57cec5SDimitry Andric replaceFeatures(F, FeatureStr); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric bool StrippedAtomics = false; 1900b57cec5SDimitry Andric bool StrippedTLS = false; 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric if (!Features[WebAssembly::FeatureAtomics]) 1930b57cec5SDimitry Andric StrippedAtomics = stripAtomics(M); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric if (!Features[WebAssembly::FeatureBulkMemory]) 1960b57cec5SDimitry Andric StrippedTLS = stripThreadLocals(M); 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric if (StrippedAtomics && !StrippedTLS) 1990b57cec5SDimitry Andric stripThreadLocals(M); 2000b57cec5SDimitry Andric else if (StrippedTLS && !StrippedAtomics) 2010b57cec5SDimitry Andric stripAtomics(M); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric recordFeatures(M, Features, StrippedAtomics || StrippedTLS); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Conservatively assume we have made some change 2060b57cec5SDimitry Andric return true; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric private: 2100b57cec5SDimitry Andric FeatureBitset coalesceFeatures(const Module &M) { 2110b57cec5SDimitry Andric FeatureBitset Features = 2120b57cec5SDimitry Andric WasmTM 2130b57cec5SDimitry Andric ->getSubtargetImpl(WasmTM->getTargetCPU(), 2140b57cec5SDimitry Andric WasmTM->getTargetFeatureString()) 2150b57cec5SDimitry Andric ->getFeatureBits(); 2160b57cec5SDimitry Andric for (auto &F : M) 2170b57cec5SDimitry Andric Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits(); 2180b57cec5SDimitry Andric return Features; 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric std::string getFeatureString(const FeatureBitset &Features) { 2220b57cec5SDimitry Andric std::string Ret; 2230b57cec5SDimitry Andric for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 2240b57cec5SDimitry Andric if (Features[KV.Value]) 2250b57cec5SDimitry Andric Ret += (StringRef("+") + KV.Key + ",").str(); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric return Ret; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric void replaceFeatures(Function &F, const std::string &Features) { 2310b57cec5SDimitry Andric F.removeFnAttr("target-features"); 2320b57cec5SDimitry Andric F.removeFnAttr("target-cpu"); 2330b57cec5SDimitry Andric F.addFnAttr("target-features", Features); 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric bool stripAtomics(Module &M) { 2370b57cec5SDimitry Andric // Detect whether any atomics will be lowered, since there is no way to tell 2380b57cec5SDimitry Andric // whether the LowerAtomic pass lowers e.g. stores. 2390b57cec5SDimitry Andric bool Stripped = false; 2400b57cec5SDimitry Andric for (auto &F : M) { 2410b57cec5SDimitry Andric for (auto &B : F) { 2420b57cec5SDimitry Andric for (auto &I : B) { 2430b57cec5SDimitry Andric if (I.isAtomic()) { 2440b57cec5SDimitry Andric Stripped = true; 2450b57cec5SDimitry Andric goto done; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric done: 2520b57cec5SDimitry Andric if (!Stripped) 2530b57cec5SDimitry Andric return false; 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric LowerAtomicPass Lowerer; 2560b57cec5SDimitry Andric FunctionAnalysisManager FAM; 2570b57cec5SDimitry Andric for (auto &F : M) 2580b57cec5SDimitry Andric Lowerer.run(F, FAM); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric return true; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric bool stripThreadLocals(Module &M) { 2640b57cec5SDimitry Andric bool Stripped = false; 2650b57cec5SDimitry Andric for (auto &GV : M.globals()) { 2660b57cec5SDimitry Andric if (GV.getThreadLocalMode() != 2670b57cec5SDimitry Andric GlobalValue::ThreadLocalMode::NotThreadLocal) { 2680b57cec5SDimitry Andric Stripped = true; 2690b57cec5SDimitry Andric GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric return Stripped; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) { 2760b57cec5SDimitry Andric for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 2770b57cec5SDimitry Andric std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str(); 2780b57cec5SDimitry Andric if (KV.Value == WebAssembly::FeatureAtomics && Stripped) { 2790b57cec5SDimitry Andric // "atomics" is special: code compiled without atomics may have had its 2800b57cec5SDimitry Andric // atomics lowered to nonatomic operations. In that case, atomics is 2810b57cec5SDimitry Andric // disallowed to prevent unsafe linking with atomics-enabled objects. 2820b57cec5SDimitry Andric assert(!Features[WebAssembly::FeatureAtomics] || 2830b57cec5SDimitry Andric !Features[WebAssembly::FeatureBulkMemory]); 2840b57cec5SDimitry Andric M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, 2850b57cec5SDimitry Andric wasm::WASM_FEATURE_PREFIX_DISALLOWED); 2860b57cec5SDimitry Andric } else if (Features[KV.Value]) { 2870b57cec5SDimitry Andric // Otherwise features are marked Used or not mentioned 2880b57cec5SDimitry Andric M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, 2890b57cec5SDimitry Andric wasm::WASM_FEATURE_PREFIX_USED); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric }; 2940b57cec5SDimitry Andric char CoalesceFeaturesAndStripAtomics::ID = 0; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric /// WebAssembly Code Generator Pass Configuration Options. 2970b57cec5SDimitry Andric class WebAssemblyPassConfig final : public TargetPassConfig { 2980b57cec5SDimitry Andric public: 2990b57cec5SDimitry Andric WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM) 3000b57cec5SDimitry Andric : TargetPassConfig(TM, PM) {} 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { 3030b57cec5SDimitry Andric return getTM<WebAssemblyTargetMachine>(); 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric FunctionPass *createTargetRegisterAllocator(bool) override; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric void addIRPasses() override; 3090b57cec5SDimitry Andric bool addInstSelector() override; 3100b57cec5SDimitry Andric void addPostRegAlloc() override; 3110b57cec5SDimitry Andric bool addGCPasses() override { return false; } 3120b57cec5SDimitry Andric void addPreEmitPass() override; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric // No reg alloc 3150b57cec5SDimitry Andric bool addRegAssignmentFast() override { return false; } 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // No reg alloc 3180b57cec5SDimitry Andric bool addRegAssignmentOptimized() override { return false; } 3190b57cec5SDimitry Andric }; 3200b57cec5SDimitry Andric } // end anonymous namespace 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric TargetTransformInfo 3230b57cec5SDimitry Andric WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) { 3240b57cec5SDimitry Andric return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric TargetPassConfig * 3280b57cec5SDimitry Andric WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { 3290b57cec5SDimitry Andric return new WebAssemblyPassConfig(*this, PM); 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { 3330b57cec5SDimitry Andric return nullptr; // No reg alloc 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3370b57cec5SDimitry Andric // The following functions are called from lib/CodeGen/Passes.cpp to modify 3380b57cec5SDimitry Andric // the CodeGen pass sequence. 3390b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric void WebAssemblyPassConfig::addIRPasses() { 3420b57cec5SDimitry Andric // Runs LowerAtomicPass if necessary 3430b57cec5SDimitry Andric addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine())); 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric // This is a no-op if atomics are not used in the module 3460b57cec5SDimitry Andric addPass(createAtomicExpandPass()); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Add signatures to prototype-less function declarations 3490b57cec5SDimitry Andric addPass(createWebAssemblyAddMissingPrototypes()); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls. 3520b57cec5SDimitry Andric addPass(createWebAssemblyLowerGlobalDtors()); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Fix function bitcasts, as WebAssembly requires caller and callee signatures 3550b57cec5SDimitry Andric // to match. 3560b57cec5SDimitry Andric addPass(createWebAssemblyFixFunctionBitcasts()); 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // Optimize "returned" function attributes. 3590b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 3600b57cec5SDimitry Andric addPass(createWebAssemblyOptimizeReturned()); 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // If exception handling is not enabled and setjmp/longjmp handling is 3630b57cec5SDimitry Andric // enabled, we lower invokes into calls and delete unreachable landingpad 3640b57cec5SDimitry Andric // blocks. Lowering invokes when there is no EH support is done in 3650b57cec5SDimitry Andric // TargetPassConfig::addPassesToHandleExceptions, but this runs after this 3660b57cec5SDimitry Andric // function and SjLj handling expects all invokes to be lowered before. 3670b57cec5SDimitry Andric if (!EnableEmException && 3680b57cec5SDimitry Andric TM->Options.ExceptionModel == ExceptionHandling::None) { 3690b57cec5SDimitry Andric addPass(createLowerInvokePass()); 3700b57cec5SDimitry Andric // The lower invoke pass may create unreachable code. Remove it in order not 3710b57cec5SDimitry Andric // to process dead blocks in setjmp/longjmp handling. 3720b57cec5SDimitry Andric addPass(createUnreachableBlockEliminationPass()); 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric // Handle exceptions and setjmp/longjmp if enabled. 3760b57cec5SDimitry Andric if (EnableEmException || EnableEmSjLj) 3770b57cec5SDimitry Andric addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException, 3780b57cec5SDimitry Andric EnableEmSjLj)); 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // Expand indirectbr instructions to switches. 3810b57cec5SDimitry Andric addPass(createIndirectBrExpandPass()); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric TargetPassConfig::addIRPasses(); 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric bool WebAssemblyPassConfig::addInstSelector() { 3870b57cec5SDimitry Andric (void)TargetPassConfig::addInstSelector(); 3880b57cec5SDimitry Andric addPass( 3890b57cec5SDimitry Andric createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); 3900b57cec5SDimitry Andric // Run the argument-move pass immediately after the ScheduleDAG scheduler 3910b57cec5SDimitry Andric // so that we can fix up the ARGUMENT instructions before anything else 3920b57cec5SDimitry Andric // sees them in the wrong place. 3930b57cec5SDimitry Andric addPass(createWebAssemblyArgumentMove()); 3940b57cec5SDimitry Andric // Set the p2align operands. This information is present during ISel, however 3950b57cec5SDimitry Andric // it's inconvenient to collect. Collect it now, and update the immediate 3960b57cec5SDimitry Andric // operands. 3970b57cec5SDimitry Andric addPass(createWebAssemblySetP2AlignOperands()); 3980b57cec5SDimitry Andric return false; 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric void WebAssemblyPassConfig::addPostRegAlloc() { 4020b57cec5SDimitry Andric // TODO: The following CodeGen passes don't currently support code containing 4030b57cec5SDimitry Andric // virtual registers. Consider removing their restrictions and re-enabling 4040b57cec5SDimitry Andric // them. 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric // These functions all require the NoVRegs property. 4070b57cec5SDimitry Andric disablePass(&MachineCopyPropagationID); 4080b57cec5SDimitry Andric disablePass(&PostRAMachineSinkingID); 4090b57cec5SDimitry Andric disablePass(&PostRASchedulerID); 4100b57cec5SDimitry Andric disablePass(&FuncletLayoutID); 4110b57cec5SDimitry Andric disablePass(&StackMapLivenessID); 4120b57cec5SDimitry Andric disablePass(&LiveDebugValuesID); 4130b57cec5SDimitry Andric disablePass(&PatchableFunctionID); 4140b57cec5SDimitry Andric disablePass(&ShrinkWrapID); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric // This pass hurts code size for wasm because it can generate irreducible 4170b57cec5SDimitry Andric // control flow. 4180b57cec5SDimitry Andric disablePass(&MachineBlockPlacementID); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric TargetPassConfig::addPostRegAlloc(); 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric void WebAssemblyPassConfig::addPreEmitPass() { 4240b57cec5SDimitry Andric TargetPassConfig::addPreEmitPass(); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric // Rewrite pseudo call_indirect instructions as real instructions. 4270b57cec5SDimitry Andric // This needs to run before register stackification, because we change the 4280b57cec5SDimitry Andric // order of the arguments. 4290b57cec5SDimitry Andric addPass(createWebAssemblyCallIndirectFixup()); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric // Eliminate multiple-entry loops. 4320b57cec5SDimitry Andric addPass(createWebAssemblyFixIrreducibleControlFlow()); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric // Do various transformations for exception handling. 4350b57cec5SDimitry Andric // Every CFG-changing optimizations should come before this. 4360b57cec5SDimitry Andric addPass(createWebAssemblyLateEHPrepare()); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // Now that we have a prologue and epilogue and all frame indices are 4390b57cec5SDimitry Andric // rewritten, eliminate SP and FP. This allows them to be stackified, 4400b57cec5SDimitry Andric // colored, and numbered with the rest of the registers. 4410b57cec5SDimitry Andric addPass(createWebAssemblyReplacePhysRegs()); 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric // Preparations and optimizations related to register stackification. 4440b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) { 4450b57cec5SDimitry Andric // LiveIntervals isn't commonly run this late. Re-establish preconditions. 4460b57cec5SDimitry Andric addPass(createWebAssemblyPrepareForLiveIntervals()); 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric // Depend on LiveIntervals and perform some optimizations on it. 4490b57cec5SDimitry Andric addPass(createWebAssemblyOptimizeLiveIntervals()); 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric // Prepare memory intrinsic calls for register stackifying. 4520b57cec5SDimitry Andric addPass(createWebAssemblyMemIntrinsicResults()); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric // Mark registers as representing wasm's value stack. This is a key 4550b57cec5SDimitry Andric // code-compression technique in WebAssembly. We run this pass (and 4560b57cec5SDimitry Andric // MemIntrinsicResults above) very late, so that it sees as much code as 4570b57cec5SDimitry Andric // possible, including code emitted by PEI and expanded by late tail 4580b57cec5SDimitry Andric // duplication. 4590b57cec5SDimitry Andric addPass(createWebAssemblyRegStackify()); 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric // Run the register coloring pass to reduce the total number of registers. 4620b57cec5SDimitry Andric // This runs after stackification so that it doesn't consider registers 4630b57cec5SDimitry Andric // that become stackified. 4640b57cec5SDimitry Andric addPass(createWebAssemblyRegColoring()); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric // Sort the blocks of the CFG into topological order, a prerequisite for 4680b57cec5SDimitry Andric // BLOCK and LOOP markers. 4690b57cec5SDimitry Andric addPass(createWebAssemblyCFGSort()); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Insert BLOCK and LOOP markers. 4720b57cec5SDimitry Andric addPass(createWebAssemblyCFGStackify()); 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric // Insert explicit local.get and local.set operators. 4750b57cec5SDimitry Andric addPass(createWebAssemblyExplicitLocals()); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric // Lower br_unless into br_if. 4780b57cec5SDimitry Andric addPass(createWebAssemblyLowerBrUnless()); 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric // Perform the very last peephole optimizations on the code. 4810b57cec5SDimitry Andric if (getOptLevel() != CodeGenOpt::None) 4820b57cec5SDimitry Andric addPass(createWebAssemblyPeephole()); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric // Create a mapping from LLVM CodeGen virtual registers to wasm registers. 4850b57cec5SDimitry Andric addPass(createWebAssemblyRegNumbering()); 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric yaml::MachineFunctionInfo * 4890b57cec5SDimitry Andric WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const { 4900b57cec5SDimitry Andric return new yaml::WebAssemblyFunctionInfo(); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML( 4940b57cec5SDimitry Andric const MachineFunction &MF) const { 4950b57cec5SDimitry Andric const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); 4960b57cec5SDimitry Andric return new yaml::WebAssemblyFunctionInfo(*MFI); 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric bool WebAssemblyTargetMachine::parseMachineFunctionInfo( 5000b57cec5SDimitry Andric const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS, 5010b57cec5SDimitry Andric SMDiagnostic &Error, SMRange &SourceRange) const { 5020b57cec5SDimitry Andric const auto &YamlMFI = 5030b57cec5SDimitry Andric reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI); 5040b57cec5SDimitry Andric MachineFunction &MF = PFS.MF; 5050b57cec5SDimitry Andric MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI); 5060b57cec5SDimitry Andric return false; 5070b57cec5SDimitry Andric } 508