110e730a2SDan Gohman //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
210e730a2SDan Gohman //
310e730a2SDan Gohman //                     The LLVM Compiler Infrastructure
410e730a2SDan Gohman //
510e730a2SDan Gohman // This file is distributed under the University of Illinois Open Source
610e730a2SDan Gohman // License. See LICENSE.TXT for details.
710e730a2SDan Gohman //
810e730a2SDan Gohman //===----------------------------------------------------------------------===//
910e730a2SDan Gohman ///
1010e730a2SDan Gohman /// \file
1110e730a2SDan Gohman /// \brief This file defines the WebAssembly-specific subclass of TargetMachine.
1210e730a2SDan Gohman ///
1310e730a2SDan Gohman //===----------------------------------------------------------------------===//
1410e730a2SDan Gohman 
1510e730a2SDan Gohman #include "WebAssemblyTargetMachine.h"
166bda14b3SChandler Carruth #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
176bda14b3SChandler Carruth #include "WebAssembly.h"
185bf22fc8SDan Gohman #include "WebAssemblyTargetObjectFile.h"
1910e730a2SDan Gohman #include "WebAssemblyTargetTransformInfo.h"
2010e730a2SDan Gohman #include "llvm/CodeGen/MachineFunctionPass.h"
2110e730a2SDan Gohman #include "llvm/CodeGen/Passes.h"
2210e730a2SDan Gohman #include "llvm/CodeGen/RegAllocRegistry.h"
2331d19d43SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h"
2410e730a2SDan Gohman #include "llvm/IR/Function.h"
2510e730a2SDan Gohman #include "llvm/Support/TargetRegistry.h"
2610e730a2SDan Gohman #include "llvm/Target/TargetOptions.h"
2703855df1SJF Bastien #include "llvm/Transforms/Scalar.h"
28*a373d18eSDavid Blaikie #include "llvm/Transforms/Utils.h"
2910e730a2SDan Gohman using namespace llvm;
3010e730a2SDan Gohman 
3110e730a2SDan Gohman #define DEBUG_TYPE "wasm"
3210e730a2SDan Gohman 
33f41f67d3SDerek Schuff // Emscripten's asm.js-style exception handling
34ccdceda1SDerek Schuff static cl::opt<bool> EnableEmException(
3553b9af02SDerek Schuff     "enable-emscripten-cxx-exceptions",
36f41f67d3SDerek Schuff     cl::desc("WebAssembly Emscripten-style exception handling"),
37f41f67d3SDerek Schuff     cl::init(false));
38f41f67d3SDerek Schuff 
39ccdceda1SDerek Schuff // Emscripten's asm.js-style setjmp/longjmp handling
40ccdceda1SDerek Schuff static cl::opt<bool> EnableEmSjLj(
41ccdceda1SDerek Schuff     "enable-emscripten-sjlj",
42ccdceda1SDerek Schuff     cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
43ccdceda1SDerek Schuff     cl::init(false));
44ccdceda1SDerek Schuff 
4510e730a2SDan Gohman extern "C" void LLVMInitializeWebAssemblyTarget() {
4610e730a2SDan Gohman   // Register the target.
47f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> X(
48f42454b9SMehdi Amini       getTheWebAssemblyTarget32());
49f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
50f42454b9SMehdi Amini       getTheWebAssemblyTarget64());
51f41f67d3SDerek Schuff 
52f41f67d3SDerek Schuff   // Register exception handling pass to opt
53ccdceda1SDerek Schuff   initializeWebAssemblyLowerEmscriptenEHSjLjPass(
54f41f67d3SDerek Schuff       *PassRegistry::getPassRegistry());
5510e730a2SDan Gohman }
5610e730a2SDan Gohman 
5710e730a2SDan Gohman //===----------------------------------------------------------------------===//
5810e730a2SDan Gohman // WebAssembly Lowering public interface.
5910e730a2SDan Gohman //===----------------------------------------------------------------------===//
6010e730a2SDan Gohman 
6141133a3eSDan Gohman static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
6241133a3eSDan Gohman   if (!RM.hasValue())
6341133a3eSDan Gohman     return Reloc::PIC_;
6441133a3eSDan Gohman   return *RM;
6541133a3eSDan Gohman }
6641133a3eSDan Gohman 
6710e730a2SDan Gohman /// Create an WebAssembly architecture model.
6810e730a2SDan Gohman ///
6910e730a2SDan Gohman WebAssemblyTargetMachine::WebAssemblyTargetMachine(
7010e730a2SDan Gohman     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
7141133a3eSDan Gohman     const TargetOptions &Options, Optional<Reloc::Model> RM,
72314ed201SDaniel Jasper     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
73bb8507e6SMatthias Braun     : LLVMTargetMachine(T,
74bb8507e6SMatthias Braun                         TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
750c6f5ac5SDan Gohman                                          : "e-m:e-p:32:32-i64:64-n32:64-S128",
7641133a3eSDan Gohman                         TT, CPU, FS, Options, getEffectiveRelocModel(RM),
77314ed201SDaniel Jasper                         CM ? *CM : CodeModel::Large, OL),
7818eafb6cSDan Gohman       TLOF(TT.isOSBinFormatELF() ?
7918eafb6cSDan Gohman               static_cast<TargetLoweringObjectFile*>(
8018eafb6cSDan Gohman                   new WebAssemblyTargetObjectFileELF()) :
8118eafb6cSDan Gohman               static_cast<TargetLoweringObjectFile*>(
8218eafb6cSDan Gohman                   new WebAssemblyTargetObjectFile())) {
83e040533eSDan Gohman   // WebAssembly type-checks instructions, but a noreturn function with a return
84ffa143ceSDerek Schuff   // type that doesn't match the context will cause a check failure. So we lower
85ffa143ceSDerek Schuff   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
86e040533eSDan Gohman   // 'unreachable' instructions which is meant for that case.
87ffa143ceSDerek Schuff   this->Options.TrapUnreachable = true;
88ffa143ceSDerek Schuff 
89d934cb88SDan Gohman   // WebAssembly treats each function as an independent unit. Force
90d934cb88SDan Gohman   // -ffunction-sections, effectively, so that we can emit them independently.
91d934cb88SDan Gohman   if (!TT.isOSBinFormatELF()) {
92d934cb88SDan Gohman     this->Options.FunctionSections = true;
93d934cb88SDan Gohman     this->Options.DataSections = true;
94d934cb88SDan Gohman     this->Options.UniqueSectionNames = true;
95d934cb88SDan Gohman   }
96d934cb88SDan Gohman 
9710e730a2SDan Gohman   initAsmInfo();
9810e730a2SDan Gohman 
99d85ab7fcSDan Gohman   // Note that we don't use setRequiresStructuredCFG(true). It disables
100d85ab7fcSDan Gohman   // optimizations than we're ok with, and want, such as critical edge
101d85ab7fcSDan Gohman   // splitting and tail merging.
10210e730a2SDan Gohman }
10310e730a2SDan Gohman 
10410e730a2SDan Gohman WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
10510e730a2SDan Gohman 
10610e730a2SDan Gohman const WebAssemblySubtarget *
10710e730a2SDan Gohman WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
10810e730a2SDan Gohman   Attribute CPUAttr = F.getFnAttribute("target-cpu");
10910e730a2SDan Gohman   Attribute FSAttr = F.getFnAttribute("target-features");
11010e730a2SDan Gohman 
11110e730a2SDan Gohman   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
11210e730a2SDan Gohman                         ? CPUAttr.getValueAsString().str()
11310e730a2SDan Gohman                         : TargetCPU;
11410e730a2SDan Gohman   std::string FS = !FSAttr.hasAttribute(Attribute::None)
11510e730a2SDan Gohman                        ? FSAttr.getValueAsString().str()
11610e730a2SDan Gohman                        : TargetFS;
11710e730a2SDan Gohman 
11810e730a2SDan Gohman   auto &I = SubtargetMap[CPU + FS];
11910e730a2SDan Gohman   if (!I) {
12010e730a2SDan Gohman     // This needs to be done before we create a new subtarget since any
12110e730a2SDan Gohman     // creation will depend on the TM and the code generation flags on the
12210e730a2SDan Gohman     // function that reside in TargetOptions.
12310e730a2SDan Gohman     resetTargetOptions(F);
1243adc7ce9SRafael Espindola     I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
12510e730a2SDan Gohman   }
12610e730a2SDan Gohman   return I.get();
12710e730a2SDan Gohman }
12810e730a2SDan Gohman 
12910e730a2SDan Gohman namespace {
13039b5367cSDerek Schuff class StripThreadLocal final : public ModulePass {
13139b5367cSDerek Schuff   // The default thread model for wasm is single, where thread-local variables
13239b5367cSDerek Schuff   // are identical to regular globals and should be treated the same. So this
13339b5367cSDerek Schuff   // pass just converts all GlobalVariables to NotThreadLocal
13439b5367cSDerek Schuff   static char ID;
13539b5367cSDerek Schuff 
13639b5367cSDerek Schuff  public:
13739b5367cSDerek Schuff   StripThreadLocal() : ModulePass(ID) {}
13839b5367cSDerek Schuff   bool runOnModule(Module &M) override {
13939b5367cSDerek Schuff     for (auto &GV : M.globals())
14039b5367cSDerek Schuff       GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
14139b5367cSDerek Schuff     return true;
14239b5367cSDerek Schuff   }
14339b5367cSDerek Schuff };
14439b5367cSDerek Schuff char StripThreadLocal::ID = 0;
14539b5367cSDerek Schuff 
14610e730a2SDan Gohman /// WebAssembly Code Generator Pass Configuration Options.
14710e730a2SDan Gohman class WebAssemblyPassConfig final : public TargetPassConfig {
14810e730a2SDan Gohman public:
1495e394c3dSMatthias Braun   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
15010e730a2SDan Gohman       : TargetPassConfig(TM, PM) {}
15110e730a2SDan Gohman 
15210e730a2SDan Gohman   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
15310e730a2SDan Gohman     return getTM<WebAssemblyTargetMachine>();
15410e730a2SDan Gohman   }
15510e730a2SDan Gohman 
15610e730a2SDan Gohman   FunctionPass *createTargetRegisterAllocator(bool) override;
15710e730a2SDan Gohman 
15810e730a2SDan Gohman   void addIRPasses() override;
15910e730a2SDan Gohman   bool addInstSelector() override;
16010e730a2SDan Gohman   void addPostRegAlloc() override;
161ad154c83SDerek Schuff   bool addGCPasses() override { return false; }
16210e730a2SDan Gohman   void addPreEmitPass() override;
16310e730a2SDan Gohman };
16410e730a2SDan Gohman } // end anonymous namespace
16510e730a2SDan Gohman 
16626d11ca4SSanjoy Das TargetTransformInfo
16726d11ca4SSanjoy Das WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
16810e730a2SDan Gohman   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
16910e730a2SDan Gohman }
17010e730a2SDan Gohman 
17110e730a2SDan Gohman TargetPassConfig *
17210e730a2SDan Gohman WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
1735e394c3dSMatthias Braun   return new WebAssemblyPassConfig(*this, PM);
17410e730a2SDan Gohman }
17510e730a2SDan Gohman 
17610e730a2SDan Gohman FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
17710e730a2SDan Gohman   return nullptr; // No reg alloc
17810e730a2SDan Gohman }
17910e730a2SDan Gohman 
18010e730a2SDan Gohman //===----------------------------------------------------------------------===//
18110e730a2SDan Gohman // The following functions are called from lib/CodeGen/Passes.cpp to modify
18210e730a2SDan Gohman // the CodeGen pass sequence.
18310e730a2SDan Gohman //===----------------------------------------------------------------------===//
18410e730a2SDan Gohman 
18510e730a2SDan Gohman void WebAssemblyPassConfig::addIRPasses() {
18639b5367cSDerek Schuff   if (TM->Options.ThreadModel == ThreadModel::Single) {
1879c54d3b4SDan Gohman     // In "single" mode, atomics get lowered to non-atomics.
18803855df1SJF Bastien     addPass(createLowerAtomicPass());
18939b5367cSDerek Schuff     addPass(new StripThreadLocal());
19039b5367cSDerek Schuff   } else {
19110e730a2SDan Gohman     // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
19210e730a2SDan Gohman     // control specifically what gets lowered.
1938b61764cSFrancis Visoiu Mistrih     addPass(createAtomicExpandPass());
19439b5367cSDerek Schuff   }
19510e730a2SDan Gohman 
196bafe6902SSam Clegg   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
197bafe6902SSam Clegg   addPass(createWebAssemblyLowerGlobalDtors());
198bafe6902SSam Clegg 
1991b637458SDan Gohman   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
2001b637458SDan Gohman   // to match.
2011b637458SDan Gohman   addPass(createWebAssemblyFixFunctionBitcasts());
2021b637458SDan Gohman 
20381719f85SDan Gohman   // Optimize "returned" function attributes.
204b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
20581719f85SDan Gohman     addPass(createWebAssemblyOptimizeReturned());
20681719f85SDan Gohman 
207c0f18172SHeejin Ahn   // If exception handling is not enabled and setjmp/longjmp handling is
208c0f18172SHeejin Ahn   // enabled, we lower invokes into calls and delete unreachable landingpad
209c0f18172SHeejin Ahn   // blocks. Lowering invokes when there is no EH support is done in
210c0f18172SHeejin Ahn   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
211c0f18172SHeejin Ahn   // function and SjLj handling expects all invokes to be lowered before.
2129386bde1SHeejin Ahn   if (!EnableEmException &&
2139386bde1SHeejin Ahn       TM->Options.ExceptionModel == ExceptionHandling::None) {
214c0f18172SHeejin Ahn     addPass(createLowerInvokePass());
215c0f18172SHeejin Ahn     // The lower invoke pass may create unreachable code. Remove it in order not
216c0f18172SHeejin Ahn     // to process dead blocks in setjmp/longjmp handling.
217c0f18172SHeejin Ahn     addPass(createUnreachableBlockEliminationPass());
218c0f18172SHeejin Ahn   }
219c0f18172SHeejin Ahn 
220c0f18172SHeejin Ahn   // Handle exceptions and setjmp/longjmp if enabled.
221ccdceda1SDerek Schuff   if (EnableEmException || EnableEmSjLj)
222ccdceda1SDerek Schuff     addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
223ccdceda1SDerek Schuff                                                    EnableEmSjLj));
224f41f67d3SDerek Schuff 
22510e730a2SDan Gohman   TargetPassConfig::addIRPasses();
22610e730a2SDan Gohman }
22710e730a2SDan Gohman 
22810e730a2SDan Gohman bool WebAssemblyPassConfig::addInstSelector() {
229b0921ca9SDan Gohman   (void)TargetPassConfig::addInstSelector();
23010e730a2SDan Gohman   addPass(
23110e730a2SDan Gohman       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
2321cf96c0cSDan Gohman   // Run the argument-move pass immediately after the ScheduleDAG scheduler
2331cf96c0cSDan Gohman   // so that we can fix up the ARGUMENT instructions before anything else
2341cf96c0cSDan Gohman   // sees them in the wrong place.
2351cf96c0cSDan Gohman   addPass(createWebAssemblyArgumentMove());
236bb372243SDan Gohman   // Set the p2align operands. This information is present during ISel, however
237bb372243SDan Gohman   // it's inconvenient to collect. Collect it now, and update the immediate
238bb372243SDan Gohman   // operands.
239bb372243SDan Gohman   addPass(createWebAssemblySetP2AlignOperands());
24010e730a2SDan Gohman   return false;
24110e730a2SDan Gohman }
24210e730a2SDan Gohman 
243600aee98SJF Bastien void WebAssemblyPassConfig::addPostRegAlloc() {
2449c54d3b4SDan Gohman   // TODO: The following CodeGen passes don't currently support code containing
2459c54d3b4SDan Gohman   // virtual registers. Consider removing their restrictions and re-enabling
2469c54d3b4SDan Gohman   // them.
247ad154c83SDerek Schuff 
248ad154c83SDerek Schuff   // Has no asserts of its own, but was not written to handle virtual regs.
249ad154c83SDerek Schuff   disablePass(&ShrinkWrapID);
250ecabac62SDerek Schuff 
2511eb47368SMatthias Braun   // These functions all require the NoVRegs property.
252600aee98SJF Bastien   disablePass(&MachineCopyPropagationID);
253ecabac62SDerek Schuff   disablePass(&PostRASchedulerID);
254ecabac62SDerek Schuff   disablePass(&FuncletLayoutID);
255ecabac62SDerek Schuff   disablePass(&StackMapLivenessID);
256ecabac62SDerek Schuff   disablePass(&LiveDebugValuesID);
257fe71ec77SSanjoy Das   disablePass(&PatchableFunctionID);
258950a13cfSDan Gohman 
259b0921ca9SDan Gohman   TargetPassConfig::addPostRegAlloc();
260600aee98SJF Bastien }
26110e730a2SDan Gohman 
262950a13cfSDan Gohman void WebAssemblyPassConfig::addPreEmitPass() {
263b0921ca9SDan Gohman   TargetPassConfig::addPreEmitPass();
264b0921ca9SDan Gohman 
2650cfb5f85SDan Gohman   // Now that we have a prologue and epilogue and all frame indices are
2660cfb5f85SDan Gohman   // rewritten, eliminate SP and FP. This allows them to be stackified,
2670cfb5f85SDan Gohman   // colored, and numbered with the rest of the registers.
2680cfb5f85SDan Gohman   addPass(createWebAssemblyReplacePhysRegs());
2690cfb5f85SDan Gohman 
2706f69783fSDerek Schuff   // Rewrite pseudo call_indirect instructions as real instructions.
2716f69783fSDerek Schuff   // This needs to run before register stackification, because we change the
2726f69783fSDerek Schuff   // order of the arguments.
2736f69783fSDerek Schuff   addPass(createWebAssemblyCallIndirectFixup());
2746f69783fSDerek Schuff 
2750cfb5f85SDan Gohman   if (getOptLevel() != CodeGenOpt::None) {
2760cfb5f85SDan Gohman     // LiveIntervals isn't commonly run this late. Re-establish preconditions.
2770cfb5f85SDan Gohman     addPass(createWebAssemblyPrepareForLiveIntervals());
2780cfb5f85SDan Gohman 
2790cfb5f85SDan Gohman     // Depend on LiveIntervals and perform some optimizations on it.
2800cfb5f85SDan Gohman     addPass(createWebAssemblyOptimizeLiveIntervals());
2810cfb5f85SDan Gohman 
2820cfb5f85SDan Gohman     // Prepare store instructions for register stackifying.
2830cfb5f85SDan Gohman     addPass(createWebAssemblyStoreResults());
2840cfb5f85SDan Gohman 
285e040533eSDan Gohman     // Mark registers as representing wasm's value stack. This is a key
2860cfb5f85SDan Gohman     // code-compression technique in WebAssembly. We run this pass (and
2870cfb5f85SDan Gohman     // StoreResults above) very late, so that it sees as much code as possible,
2880cfb5f85SDan Gohman     // including code emitted by PEI and expanded by late tail duplication.
2890cfb5f85SDan Gohman     addPass(createWebAssemblyRegStackify());
2900cfb5f85SDan Gohman 
2910cfb5f85SDan Gohman     // Run the register coloring pass to reduce the total number of registers.
2920cfb5f85SDan Gohman     // This runs after stackification so that it doesn't consider registers
2930cfb5f85SDan Gohman     // that become stackified.
2940cfb5f85SDan Gohman     addPass(createWebAssemblyRegColoring());
2950cfb5f85SDan Gohman   }
2960cfb5f85SDan Gohman 
297d934cb88SDan Gohman   // Eliminate multiple-entry loops. Do this before inserting explicit get_local
298d934cb88SDan Gohman   // and set_local operators because we create a new variable that we want
299d934cb88SDan Gohman   // converted into a local.
300d934cb88SDan Gohman   addPass(createWebAssemblyFixIrreducibleControlFlow());
301d934cb88SDan Gohman 
3024fc4e42dSDan Gohman   // Insert explicit get_local and set_local operators.
3034fc4e42dSDan Gohman   addPass(createWebAssemblyExplicitLocals());
3044fc4e42dSDan Gohman 
305f52ee17aSDan Gohman   // Sort the blocks of the CFG into topological order, a prerequisite for
306f52ee17aSDan Gohman   // BLOCK and LOOP markers.
307f52ee17aSDan Gohman   addPass(createWebAssemblyCFGSort());
308f52ee17aSDan Gohman 
309f52ee17aSDan Gohman   // Insert BLOCK and LOOP markers.
310950a13cfSDan Gohman   addPass(createWebAssemblyCFGStackify());
3115941bde0SDan Gohman 
312f0b165a7SDan Gohman   // Lower br_unless into br_if.
313f0b165a7SDan Gohman   addPass(createWebAssemblyLowerBrUnless());
314f0b165a7SDan Gohman 
3155941bde0SDan Gohman   // Perform the very last peephole optimizations on the code.
316b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
31781719f85SDan Gohman     addPass(createWebAssemblyPeephole());
318b7c2400fSDan Gohman 
319b7c2400fSDan Gohman   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
320b7c2400fSDan Gohman   addPass(createWebAssemblyRegNumbering());
321950a13cfSDan Gohman }
322