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 "WebAssembly.h"
1610e730a2SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
1710e730a2SDan Gohman #include "WebAssemblyTargetMachine.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"
2810e730a2SDan Gohman using namespace llvm;
2910e730a2SDan Gohman 
3010e730a2SDan Gohman #define DEBUG_TYPE "wasm"
3110e730a2SDan Gohman 
32f41f67d3SDerek Schuff // Emscripten's asm.js-style exception handling
33ccdceda1SDerek Schuff static cl::opt<bool> EnableEmException(
3453b9af02SDerek Schuff     "enable-emscripten-cxx-exceptions",
35f41f67d3SDerek Schuff     cl::desc("WebAssembly Emscripten-style exception handling"),
36f41f67d3SDerek Schuff     cl::init(false));
37f41f67d3SDerek Schuff 
38ccdceda1SDerek Schuff // Emscripten's asm.js-style setjmp/longjmp handling
39ccdceda1SDerek Schuff static cl::opt<bool> EnableEmSjLj(
40ccdceda1SDerek Schuff     "enable-emscripten-sjlj",
41ccdceda1SDerek Schuff     cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
42ccdceda1SDerek Schuff     cl::init(false));
43ccdceda1SDerek Schuff 
4410e730a2SDan Gohman extern "C" void LLVMInitializeWebAssemblyTarget() {
4510e730a2SDan Gohman   // Register the target.
46f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> X(
47f42454b9SMehdi Amini       getTheWebAssemblyTarget32());
48f42454b9SMehdi Amini   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
49f42454b9SMehdi Amini       getTheWebAssemblyTarget64());
50f41f67d3SDerek Schuff 
51f41f67d3SDerek Schuff   // Register exception handling pass to opt
52ccdceda1SDerek Schuff   initializeWebAssemblyLowerEmscriptenEHSjLjPass(
53f41f67d3SDerek Schuff       *PassRegistry::getPassRegistry());
5410e730a2SDan Gohman }
5510e730a2SDan Gohman 
5610e730a2SDan Gohman //===----------------------------------------------------------------------===//
5710e730a2SDan Gohman // WebAssembly Lowering public interface.
5810e730a2SDan Gohman //===----------------------------------------------------------------------===//
5910e730a2SDan Gohman 
6041133a3eSDan Gohman static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
6141133a3eSDan Gohman   if (!RM.hasValue())
6241133a3eSDan Gohman     return Reloc::PIC_;
6341133a3eSDan Gohman   return *RM;
6441133a3eSDan Gohman }
6541133a3eSDan Gohman 
6610e730a2SDan Gohman /// Create an WebAssembly architecture model.
6710e730a2SDan Gohman ///
6810e730a2SDan Gohman WebAssemblyTargetMachine::WebAssemblyTargetMachine(
6910e730a2SDan Gohman     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
7041133a3eSDan Gohman     const TargetOptions &Options, Optional<Reloc::Model> RM,
7141133a3eSDan Gohman     CodeModel::Model CM, CodeGenOpt::Level OL)
720c6f5ac5SDan Gohman     : LLVMTargetMachine(T,
730c6f5ac5SDan Gohman                         TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
740c6f5ac5SDan Gohman                                          : "e-m:e-p:32:32-i64:64-n32:64-S128",
7541133a3eSDan Gohman                         TT, CPU, FS, Options, getEffectiveRelocModel(RM),
7641133a3eSDan Gohman                         CM, OL),
77*18eafb6cSDan Gohman       TLOF(TT.isOSBinFormatELF() ?
78*18eafb6cSDan Gohman               static_cast<TargetLoweringObjectFile*>(
79*18eafb6cSDan Gohman                   new WebAssemblyTargetObjectFileELF()) :
80*18eafb6cSDan Gohman               static_cast<TargetLoweringObjectFile*>(
81*18eafb6cSDan Gohman                   new WebAssemblyTargetObjectFile())) {
82e040533eSDan Gohman   // WebAssembly type-checks instructions, but a noreturn function with a return
83ffa143ceSDerek Schuff   // type that doesn't match the context will cause a check failure. So we lower
84ffa143ceSDerek Schuff   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
85e040533eSDan Gohman   // 'unreachable' instructions which is meant for that case.
86ffa143ceSDerek Schuff   this->Options.TrapUnreachable = true;
87ffa143ceSDerek Schuff 
8810e730a2SDan Gohman   initAsmInfo();
8910e730a2SDan Gohman 
90d85ab7fcSDan Gohman   // Note that we don't use setRequiresStructuredCFG(true). It disables
91d85ab7fcSDan Gohman   // optimizations than we're ok with, and want, such as critical edge
92d85ab7fcSDan Gohman   // splitting and tail merging.
9310e730a2SDan Gohman }
9410e730a2SDan Gohman 
9510e730a2SDan Gohman WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
9610e730a2SDan Gohman 
9710e730a2SDan Gohman const WebAssemblySubtarget *
9810e730a2SDan Gohman WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
9910e730a2SDan Gohman   Attribute CPUAttr = F.getFnAttribute("target-cpu");
10010e730a2SDan Gohman   Attribute FSAttr = F.getFnAttribute("target-features");
10110e730a2SDan Gohman 
10210e730a2SDan Gohman   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
10310e730a2SDan Gohman                         ? CPUAttr.getValueAsString().str()
10410e730a2SDan Gohman                         : TargetCPU;
10510e730a2SDan Gohman   std::string FS = !FSAttr.hasAttribute(Attribute::None)
10610e730a2SDan Gohman                        ? FSAttr.getValueAsString().str()
10710e730a2SDan Gohman                        : TargetFS;
10810e730a2SDan Gohman 
10910e730a2SDan Gohman   auto &I = SubtargetMap[CPU + FS];
11010e730a2SDan Gohman   if (!I) {
11110e730a2SDan Gohman     // This needs to be done before we create a new subtarget since any
11210e730a2SDan Gohman     // creation will depend on the TM and the code generation flags on the
11310e730a2SDan Gohman     // function that reside in TargetOptions.
11410e730a2SDan Gohman     resetTargetOptions(F);
1153adc7ce9SRafael Espindola     I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
11610e730a2SDan Gohman   }
11710e730a2SDan Gohman   return I.get();
11810e730a2SDan Gohman }
11910e730a2SDan Gohman 
12010e730a2SDan Gohman namespace {
12110e730a2SDan Gohman /// WebAssembly Code Generator Pass Configuration Options.
12210e730a2SDan Gohman class WebAssemblyPassConfig final : public TargetPassConfig {
12310e730a2SDan Gohman public:
12410e730a2SDan Gohman   WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
12510e730a2SDan Gohman       : TargetPassConfig(TM, PM) {}
12610e730a2SDan Gohman 
12710e730a2SDan Gohman   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
12810e730a2SDan Gohman     return getTM<WebAssemblyTargetMachine>();
12910e730a2SDan Gohman   }
13010e730a2SDan Gohman 
13110e730a2SDan Gohman   FunctionPass *createTargetRegisterAllocator(bool) override;
13210e730a2SDan Gohman 
13310e730a2SDan Gohman   void addIRPasses() override;
13410e730a2SDan Gohman   bool addInstSelector() override;
13510e730a2SDan Gohman   void addPostRegAlloc() override;
136ad154c83SDerek Schuff   bool addGCPasses() override { return false; }
13710e730a2SDan Gohman   void addPreEmitPass() override;
13810e730a2SDan Gohman };
13910e730a2SDan Gohman } // end anonymous namespace
14010e730a2SDan Gohman 
14110e730a2SDan Gohman TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
1429099b5e6SHans Wennborg   return TargetIRAnalysis([this](const Function &F) {
14310e730a2SDan Gohman     return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
14410e730a2SDan Gohman   });
14510e730a2SDan Gohman }
14610e730a2SDan Gohman 
14710e730a2SDan Gohman TargetPassConfig *
14810e730a2SDan Gohman WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
14910e730a2SDan Gohman   return new WebAssemblyPassConfig(this, PM);
15010e730a2SDan Gohman }
15110e730a2SDan Gohman 
15210e730a2SDan Gohman FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
15310e730a2SDan Gohman   return nullptr; // No reg alloc
15410e730a2SDan Gohman }
15510e730a2SDan Gohman 
15610e730a2SDan Gohman //===----------------------------------------------------------------------===//
15710e730a2SDan Gohman // The following functions are called from lib/CodeGen/Passes.cpp to modify
15810e730a2SDan Gohman // the CodeGen pass sequence.
15910e730a2SDan Gohman //===----------------------------------------------------------------------===//
16010e730a2SDan Gohman 
16110e730a2SDan Gohman void WebAssemblyPassConfig::addIRPasses() {
16203855df1SJF Bastien   if (TM->Options.ThreadModel == ThreadModel::Single)
1639c54d3b4SDan Gohman     // In "single" mode, atomics get lowered to non-atomics.
16403855df1SJF Bastien     addPass(createLowerAtomicPass());
16503855df1SJF Bastien   else
16610e730a2SDan Gohman     // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
16710e730a2SDan Gohman     // control specifically what gets lowered.
16803855df1SJF Bastien     addPass(createAtomicExpandPass(TM));
16910e730a2SDan Gohman 
1701b637458SDan Gohman   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
1711b637458SDan Gohman   // to match.
1721b637458SDan Gohman   addPass(createWebAssemblyFixFunctionBitcasts());
1731b637458SDan Gohman 
17481719f85SDan Gohman   // Optimize "returned" function attributes.
175b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
17681719f85SDan Gohman     addPass(createWebAssemblyOptimizeReturned());
17781719f85SDan Gohman 
178c0f18172SHeejin Ahn   // If exception handling is not enabled and setjmp/longjmp handling is
179c0f18172SHeejin Ahn   // enabled, we lower invokes into calls and delete unreachable landingpad
180c0f18172SHeejin Ahn   // blocks. Lowering invokes when there is no EH support is done in
181c0f18172SHeejin Ahn   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
182c0f18172SHeejin Ahn   // function and SjLj handling expects all invokes to be lowered before.
183c0f18172SHeejin Ahn   if (!EnableEmException) {
184c0f18172SHeejin Ahn     addPass(createLowerInvokePass());
185c0f18172SHeejin Ahn     // The lower invoke pass may create unreachable code. Remove it in order not
186c0f18172SHeejin Ahn     // to process dead blocks in setjmp/longjmp handling.
187c0f18172SHeejin Ahn     addPass(createUnreachableBlockEliminationPass());
188c0f18172SHeejin Ahn   }
189c0f18172SHeejin Ahn 
190c0f18172SHeejin Ahn   // Handle exceptions and setjmp/longjmp if enabled.
191ccdceda1SDerek Schuff   if (EnableEmException || EnableEmSjLj)
192ccdceda1SDerek Schuff     addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
193ccdceda1SDerek Schuff                                                    EnableEmSjLj));
194f41f67d3SDerek Schuff 
19510e730a2SDan Gohman   TargetPassConfig::addIRPasses();
19610e730a2SDan Gohman }
19710e730a2SDan Gohman 
19810e730a2SDan Gohman bool WebAssemblyPassConfig::addInstSelector() {
199b0921ca9SDan Gohman   (void)TargetPassConfig::addInstSelector();
20010e730a2SDan Gohman   addPass(
20110e730a2SDan Gohman       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
2021cf96c0cSDan Gohman   // Run the argument-move pass immediately after the ScheduleDAG scheduler
2031cf96c0cSDan Gohman   // so that we can fix up the ARGUMENT instructions before anything else
2041cf96c0cSDan Gohman   // sees them in the wrong place.
2051cf96c0cSDan Gohman   addPass(createWebAssemblyArgumentMove());
206bb372243SDan Gohman   // Set the p2align operands. This information is present during ISel, however
207bb372243SDan Gohman   // it's inconvenient to collect. Collect it now, and update the immediate
208bb372243SDan Gohman   // operands.
209bb372243SDan Gohman   addPass(createWebAssemblySetP2AlignOperands());
21010e730a2SDan Gohman   return false;
21110e730a2SDan Gohman }
21210e730a2SDan Gohman 
213600aee98SJF Bastien void WebAssemblyPassConfig::addPostRegAlloc() {
2149c54d3b4SDan Gohman   // TODO: The following CodeGen passes don't currently support code containing
2159c54d3b4SDan Gohman   // virtual registers. Consider removing their restrictions and re-enabling
2169c54d3b4SDan Gohman   // them.
217ad154c83SDerek Schuff 
218ad154c83SDerek Schuff   // Has no asserts of its own, but was not written to handle virtual regs.
219ad154c83SDerek Schuff   disablePass(&ShrinkWrapID);
220ecabac62SDerek Schuff 
2211eb47368SMatthias Braun   // These functions all require the NoVRegs property.
222600aee98SJF Bastien   disablePass(&MachineCopyPropagationID);
223ecabac62SDerek Schuff   disablePass(&PostRASchedulerID);
224ecabac62SDerek Schuff   disablePass(&FuncletLayoutID);
225ecabac62SDerek Schuff   disablePass(&StackMapLivenessID);
226ecabac62SDerek Schuff   disablePass(&LiveDebugValuesID);
227fe71ec77SSanjoy Das   disablePass(&PatchableFunctionID);
228950a13cfSDan Gohman 
229b0921ca9SDan Gohman   TargetPassConfig::addPostRegAlloc();
230600aee98SJF Bastien }
23110e730a2SDan Gohman 
232950a13cfSDan Gohman void WebAssemblyPassConfig::addPreEmitPass() {
233b0921ca9SDan Gohman   TargetPassConfig::addPreEmitPass();
234b0921ca9SDan Gohman 
2350cfb5f85SDan Gohman   // Now that we have a prologue and epilogue and all frame indices are
2360cfb5f85SDan Gohman   // rewritten, eliminate SP and FP. This allows them to be stackified,
2370cfb5f85SDan Gohman   // colored, and numbered with the rest of the registers.
2380cfb5f85SDan Gohman   addPass(createWebAssemblyReplacePhysRegs());
2390cfb5f85SDan Gohman 
2406f69783fSDerek Schuff   // Rewrite pseudo call_indirect instructions as real instructions.
2416f69783fSDerek Schuff   // This needs to run before register stackification, because we change the
2426f69783fSDerek Schuff   // order of the arguments.
2436f69783fSDerek Schuff   addPass(createWebAssemblyCallIndirectFixup());
2446f69783fSDerek Schuff 
2450cfb5f85SDan Gohman   if (getOptLevel() != CodeGenOpt::None) {
2460cfb5f85SDan Gohman     // LiveIntervals isn't commonly run this late. Re-establish preconditions.
2470cfb5f85SDan Gohman     addPass(createWebAssemblyPrepareForLiveIntervals());
2480cfb5f85SDan Gohman 
2490cfb5f85SDan Gohman     // Depend on LiveIntervals and perform some optimizations on it.
2500cfb5f85SDan Gohman     addPass(createWebAssemblyOptimizeLiveIntervals());
2510cfb5f85SDan Gohman 
2520cfb5f85SDan Gohman     // Prepare store instructions for register stackifying.
2530cfb5f85SDan Gohman     addPass(createWebAssemblyStoreResults());
2540cfb5f85SDan Gohman 
255e040533eSDan Gohman     // Mark registers as representing wasm's value stack. This is a key
2560cfb5f85SDan Gohman     // code-compression technique in WebAssembly. We run this pass (and
2570cfb5f85SDan Gohman     // StoreResults above) very late, so that it sees as much code as possible,
2580cfb5f85SDan Gohman     // including code emitted by PEI and expanded by late tail duplication.
2590cfb5f85SDan Gohman     addPass(createWebAssemblyRegStackify());
2600cfb5f85SDan Gohman 
2610cfb5f85SDan Gohman     // Run the register coloring pass to reduce the total number of registers.
2620cfb5f85SDan Gohman     // This runs after stackification so that it doesn't consider registers
2630cfb5f85SDan Gohman     // that become stackified.
2640cfb5f85SDan Gohman     addPass(createWebAssemblyRegColoring());
2650cfb5f85SDan Gohman   }
2660cfb5f85SDan Gohman 
2674fc4e42dSDan Gohman   // Insert explicit get_local and set_local operators.
2684fc4e42dSDan Gohman   addPass(createWebAssemblyExplicitLocals());
2694fc4e42dSDan Gohman 
270d7a2eea6SDan Gohman   // Eliminate multiple-entry loops.
271d7a2eea6SDan Gohman   addPass(createWebAssemblyFixIrreducibleControlFlow());
272d7a2eea6SDan Gohman 
2735941bde0SDan Gohman   // Put the CFG in structured form; insert BLOCK and LOOP markers.
274950a13cfSDan Gohman   addPass(createWebAssemblyCFGStackify());
2755941bde0SDan Gohman 
276f0b165a7SDan Gohman   // Lower br_unless into br_if.
277f0b165a7SDan Gohman   addPass(createWebAssemblyLowerBrUnless());
278f0b165a7SDan Gohman 
2795941bde0SDan Gohman   // Perform the very last peephole optimizations on the code.
280b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
28181719f85SDan Gohman     addPass(createWebAssemblyPeephole());
282b7c2400fSDan Gohman 
283b7c2400fSDan Gohman   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
284b7c2400fSDan Gohman   addPass(createWebAssemblyRegNumbering());
285950a13cfSDan Gohman }
286