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
115f8f34e4SAdrian Prantl /// 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"
28a373d18eSDavid 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 
5240926451SJacob Gravelle   // Register backend passes
5340926451SJacob Gravelle   auto &PR = *PassRegistry::getPassRegistry();
5492617559SSam Clegg   initializeWebAssemblyAddMissingPrototypesPass(PR);
5540926451SJacob Gravelle   initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
5640926451SJacob Gravelle   initializeLowerGlobalDtorsPass(PR);
5740926451SJacob Gravelle   initializeFixFunctionBitcastsPass(PR);
5840926451SJacob Gravelle   initializeOptimizeReturnedPass(PR);
5940926451SJacob Gravelle   initializeWebAssemblyArgumentMovePass(PR);
6040926451SJacob Gravelle   initializeWebAssemblySetP2AlignOperandsPass(PR);
6178d19108SHeejin Ahn   initializeWebAssemblyEHRestoreStackPointerPass(PR);
6240926451SJacob Gravelle   initializeWebAssemblyReplacePhysRegsPass(PR);
6340926451SJacob Gravelle   initializeWebAssemblyPrepareForLiveIntervalsPass(PR);
6440926451SJacob Gravelle   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
6540926451SJacob Gravelle   initializeWebAssemblyStoreResultsPass(PR);
6640926451SJacob Gravelle   initializeWebAssemblyRegStackifyPass(PR);
6740926451SJacob Gravelle   initializeWebAssemblyRegColoringPass(PR);
6840926451SJacob Gravelle   initializeWebAssemblyExplicitLocalsPass(PR);
6940926451SJacob Gravelle   initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
704934f76bSHeejin Ahn   initializeWebAssemblyLateEHPreparePass(PR);
7104c48949SHeejin Ahn   initializeWebAssemblyExceptionInfoPass(PR);
7240926451SJacob Gravelle   initializeWebAssemblyCFGSortPass(PR);
7340926451SJacob Gravelle   initializeWebAssemblyCFGStackifyPass(PR);
7440926451SJacob Gravelle   initializeWebAssemblyLowerBrUnlessPass(PR);
7540926451SJacob Gravelle   initializeWebAssemblyRegNumberingPass(PR);
7640926451SJacob Gravelle   initializeWebAssemblyPeepholePass(PR);
7740926451SJacob Gravelle   initializeWebAssemblyCallIndirectFixupPass(PR);
7810e730a2SDan Gohman }
7910e730a2SDan Gohman 
8010e730a2SDan Gohman //===----------------------------------------------------------------------===//
8110e730a2SDan Gohman // WebAssembly Lowering public interface.
8210e730a2SDan Gohman //===----------------------------------------------------------------------===//
8310e730a2SDan Gohman 
8441133a3eSDan Gohman static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
85*74f5fd4eSSam Clegg   if (!RM.hasValue()) {
86*74f5fd4eSSam Clegg     // Default to static relocation model.  This should always be more optimial
87*74f5fd4eSSam Clegg     // than PIC since the static linker can determine all global addresses and
88*74f5fd4eSSam Clegg     // assume direct function calls.
89*74f5fd4eSSam Clegg     return Reloc::Static;
90*74f5fd4eSSam Clegg   }
9141133a3eSDan Gohman   return *RM;
9241133a3eSDan Gohman }
9341133a3eSDan Gohman 
9410e730a2SDan Gohman /// Create an WebAssembly architecture model.
9510e730a2SDan Gohman ///
9610e730a2SDan Gohman WebAssemblyTargetMachine::WebAssemblyTargetMachine(
9710e730a2SDan Gohman     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
9841133a3eSDan Gohman     const TargetOptions &Options, Optional<Reloc::Model> RM,
99314ed201SDaniel Jasper     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
100bb8507e6SMatthias Braun     : LLVMTargetMachine(T,
101bb8507e6SMatthias Braun                         TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
1020c6f5ac5SDan Gohman                                          : "e-m:e-p:32:32-i64:64-n32:64-S128",
10341133a3eSDan Gohman                         TT, CPU, FS, Options, getEffectiveRelocModel(RM),
104314ed201SDaniel Jasper                         CM ? *CM : CodeModel::Large, OL),
105cf2a9e28SSam Clegg       TLOF(new WebAssemblyTargetObjectFile()) {
106e040533eSDan Gohman   // WebAssembly type-checks instructions, but a noreturn function with a return
107ffa143ceSDerek Schuff   // type that doesn't match the context will cause a check failure. So we lower
108ffa143ceSDerek Schuff   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
109e040533eSDan Gohman   // 'unreachable' instructions which is meant for that case.
110ffa143ceSDerek Schuff   this->Options.TrapUnreachable = true;
111ffa143ceSDerek Schuff 
112d934cb88SDan Gohman   // WebAssembly treats each function as an independent unit. Force
113d934cb88SDan Gohman   // -ffunction-sections, effectively, so that we can emit them independently.
114d934cb88SDan Gohman   this->Options.FunctionSections = true;
115d934cb88SDan Gohman   this->Options.DataSections = true;
116d934cb88SDan Gohman   this->Options.UniqueSectionNames = true;
117d934cb88SDan Gohman 
11810e730a2SDan Gohman   initAsmInfo();
11910e730a2SDan Gohman 
120d85ab7fcSDan Gohman   // Note that we don't use setRequiresStructuredCFG(true). It disables
121d85ab7fcSDan Gohman   // optimizations than we're ok with, and want, such as critical edge
122d85ab7fcSDan Gohman   // splitting and tail merging.
12310e730a2SDan Gohman }
12410e730a2SDan Gohman 
12510e730a2SDan Gohman WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
12610e730a2SDan Gohman 
12710e730a2SDan Gohman const WebAssemblySubtarget *
12810e730a2SDan Gohman WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
12910e730a2SDan Gohman   Attribute CPUAttr = F.getFnAttribute("target-cpu");
13010e730a2SDan Gohman   Attribute FSAttr = F.getFnAttribute("target-features");
13110e730a2SDan Gohman 
13210e730a2SDan Gohman   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
13310e730a2SDan Gohman                         ? CPUAttr.getValueAsString().str()
13410e730a2SDan Gohman                         : TargetCPU;
13510e730a2SDan Gohman   std::string FS = !FSAttr.hasAttribute(Attribute::None)
13610e730a2SDan Gohman                        ? FSAttr.getValueAsString().str()
13710e730a2SDan Gohman                        : TargetFS;
13810e730a2SDan Gohman 
13910e730a2SDan Gohman   auto &I = SubtargetMap[CPU + FS];
14010e730a2SDan Gohman   if (!I) {
14110e730a2SDan Gohman     // This needs to be done before we create a new subtarget since any
14210e730a2SDan Gohman     // creation will depend on the TM and the code generation flags on the
14310e730a2SDan Gohman     // function that reside in TargetOptions.
14410e730a2SDan Gohman     resetTargetOptions(F);
1453adc7ce9SRafael Espindola     I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
14610e730a2SDan Gohman   }
14710e730a2SDan Gohman   return I.get();
14810e730a2SDan Gohman }
14910e730a2SDan Gohman 
15010e730a2SDan Gohman namespace {
15139b5367cSDerek Schuff class StripThreadLocal final : public ModulePass {
15239b5367cSDerek Schuff   // The default thread model for wasm is single, where thread-local variables
15339b5367cSDerek Schuff   // are identical to regular globals and should be treated the same. So this
15439b5367cSDerek Schuff   // pass just converts all GlobalVariables to NotThreadLocal
15539b5367cSDerek Schuff   static char ID;
15639b5367cSDerek Schuff 
15739b5367cSDerek Schuff public:
15839b5367cSDerek Schuff   StripThreadLocal() : ModulePass(ID) {}
15939b5367cSDerek Schuff   bool runOnModule(Module &M) override {
16039b5367cSDerek Schuff     for (auto &GV : M.globals())
16139b5367cSDerek Schuff       GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
16239b5367cSDerek Schuff     return true;
16339b5367cSDerek Schuff   }
16439b5367cSDerek Schuff };
16539b5367cSDerek Schuff char StripThreadLocal::ID = 0;
16639b5367cSDerek Schuff 
16710e730a2SDan Gohman /// WebAssembly Code Generator Pass Configuration Options.
16810e730a2SDan Gohman class WebAssemblyPassConfig final : public TargetPassConfig {
16910e730a2SDan Gohman public:
1705e394c3dSMatthias Braun   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
17110e730a2SDan Gohman       : TargetPassConfig(TM, PM) {}
17210e730a2SDan Gohman 
17310e730a2SDan Gohman   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
17410e730a2SDan Gohman     return getTM<WebAssemblyTargetMachine>();
17510e730a2SDan Gohman   }
17610e730a2SDan Gohman 
17710e730a2SDan Gohman   FunctionPass *createTargetRegisterAllocator(bool) override;
17810e730a2SDan Gohman 
17910e730a2SDan Gohman   void addIRPasses() override;
18010e730a2SDan Gohman   bool addInstSelector() override;
18110e730a2SDan Gohman   void addPostRegAlloc() override;
182ad154c83SDerek Schuff   bool addGCPasses() override { return false; }
18310e730a2SDan Gohman   void addPreEmitPass() override;
18410e730a2SDan Gohman };
18510e730a2SDan Gohman } // end anonymous namespace
18610e730a2SDan Gohman 
18726d11ca4SSanjoy Das TargetTransformInfo
18826d11ca4SSanjoy Das WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
18910e730a2SDan Gohman   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
19010e730a2SDan Gohman }
19110e730a2SDan Gohman 
19210e730a2SDan Gohman TargetPassConfig *
19310e730a2SDan Gohman WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
1945e394c3dSMatthias Braun   return new WebAssemblyPassConfig(*this, PM);
19510e730a2SDan Gohman }
19610e730a2SDan Gohman 
19710e730a2SDan Gohman FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
19810e730a2SDan Gohman   return nullptr; // No reg alloc
19910e730a2SDan Gohman }
20010e730a2SDan Gohman 
20110e730a2SDan Gohman //===----------------------------------------------------------------------===//
20210e730a2SDan Gohman // The following functions are called from lib/CodeGen/Passes.cpp to modify
20310e730a2SDan Gohman // the CodeGen pass sequence.
20410e730a2SDan Gohman //===----------------------------------------------------------------------===//
20510e730a2SDan Gohman 
20610e730a2SDan Gohman void WebAssemblyPassConfig::addIRPasses() {
20739b5367cSDerek Schuff   if (TM->Options.ThreadModel == ThreadModel::Single) {
2089c54d3b4SDan Gohman     // In "single" mode, atomics get lowered to non-atomics.
20903855df1SJF Bastien     addPass(createLowerAtomicPass());
21039b5367cSDerek Schuff     addPass(new StripThreadLocal());
21139b5367cSDerek Schuff   } else {
21210e730a2SDan Gohman     // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
21310e730a2SDan Gohman     // control specifically what gets lowered.
2148b61764cSFrancis Visoiu Mistrih     addPass(createAtomicExpandPass());
21539b5367cSDerek Schuff   }
21610e730a2SDan Gohman 
21792617559SSam Clegg   // Add signatures to prototype-less function declarations
21892617559SSam Clegg   addPass(createWebAssemblyAddMissingPrototypes());
21992617559SSam Clegg 
220bafe6902SSam Clegg   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
221bafe6902SSam Clegg   addPass(createWebAssemblyLowerGlobalDtors());
222bafe6902SSam Clegg 
2231b637458SDan Gohman   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
2241b637458SDan Gohman   // to match.
2251b637458SDan Gohman   addPass(createWebAssemblyFixFunctionBitcasts());
2261b637458SDan Gohman 
22781719f85SDan Gohman   // Optimize "returned" function attributes.
228b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
22981719f85SDan Gohman     addPass(createWebAssemblyOptimizeReturned());
23081719f85SDan Gohman 
231c0f18172SHeejin Ahn   // If exception handling is not enabled and setjmp/longjmp handling is
232c0f18172SHeejin Ahn   // enabled, we lower invokes into calls and delete unreachable landingpad
233c0f18172SHeejin Ahn   // blocks. Lowering invokes when there is no EH support is done in
234c0f18172SHeejin Ahn   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
235c0f18172SHeejin Ahn   // function and SjLj handling expects all invokes to be lowered before.
2369386bde1SHeejin Ahn   if (!EnableEmException &&
2379386bde1SHeejin Ahn       TM->Options.ExceptionModel == ExceptionHandling::None) {
238c0f18172SHeejin Ahn     addPass(createLowerInvokePass());
239c0f18172SHeejin Ahn     // The lower invoke pass may create unreachable code. Remove it in order not
240c0f18172SHeejin Ahn     // to process dead blocks in setjmp/longjmp handling.
241c0f18172SHeejin Ahn     addPass(createUnreachableBlockEliminationPass());
242c0f18172SHeejin Ahn   }
243c0f18172SHeejin Ahn 
244c0f18172SHeejin Ahn   // Handle exceptions and setjmp/longjmp if enabled.
245ccdceda1SDerek Schuff   if (EnableEmException || EnableEmSjLj)
246ccdceda1SDerek Schuff     addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
247ccdceda1SDerek Schuff                                                    EnableEmSjLj));
248f41f67d3SDerek Schuff 
24910e730a2SDan Gohman   TargetPassConfig::addIRPasses();
25010e730a2SDan Gohman }
25110e730a2SDan Gohman 
25210e730a2SDan Gohman bool WebAssemblyPassConfig::addInstSelector() {
253b0921ca9SDan Gohman   (void)TargetPassConfig::addInstSelector();
25410e730a2SDan Gohman   addPass(
25510e730a2SDan Gohman       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
2561cf96c0cSDan Gohman   // Run the argument-move pass immediately after the ScheduleDAG scheduler
2571cf96c0cSDan Gohman   // so that we can fix up the ARGUMENT instructions before anything else
2581cf96c0cSDan Gohman   // sees them in the wrong place.
2591cf96c0cSDan Gohman   addPass(createWebAssemblyArgumentMove());
260bb372243SDan Gohman   // Set the p2align operands. This information is present during ISel, however
261bb372243SDan Gohman   // it's inconvenient to collect. Collect it now, and update the immediate
262bb372243SDan Gohman   // operands.
263bb372243SDan Gohman   addPass(createWebAssemblySetP2AlignOperands());
26410e730a2SDan Gohman   return false;
26510e730a2SDan Gohman }
26610e730a2SDan Gohman 
267600aee98SJF Bastien void WebAssemblyPassConfig::addPostRegAlloc() {
2689c54d3b4SDan Gohman   // TODO: The following CodeGen passes don't currently support code containing
2699c54d3b4SDan Gohman   // virtual registers. Consider removing their restrictions and re-enabling
2709c54d3b4SDan Gohman   // them.
271ad154c83SDerek Schuff 
2721eb47368SMatthias Braun   // These functions all require the NoVRegs property.
273600aee98SJF Bastien   disablePass(&MachineCopyPropagationID);
2747ab1b32bSJun Bum Lim   disablePass(&PostRAMachineSinkingID);
275ecabac62SDerek Schuff   disablePass(&PostRASchedulerID);
276ecabac62SDerek Schuff   disablePass(&FuncletLayoutID);
277ecabac62SDerek Schuff   disablePass(&StackMapLivenessID);
278ecabac62SDerek Schuff   disablePass(&LiveDebugValuesID);
279fe71ec77SSanjoy Das   disablePass(&PatchableFunctionID);
2807ab1b32bSJun Bum Lim   disablePass(&ShrinkWrapID);
281950a13cfSDan Gohman 
282b0921ca9SDan Gohman   TargetPassConfig::addPostRegAlloc();
283600aee98SJF Bastien }
28410e730a2SDan Gohman 
285950a13cfSDan Gohman void WebAssemblyPassConfig::addPreEmitPass() {
286b0921ca9SDan Gohman   TargetPassConfig::addPreEmitPass();
287b0921ca9SDan Gohman 
28878d19108SHeejin Ahn   // Restore __stack_pointer global after an exception is thrown.
28978d19108SHeejin Ahn   addPass(createWebAssemblyEHRestoreStackPointer());
29078d19108SHeejin Ahn 
2910cfb5f85SDan Gohman   // Now that we have a prologue and epilogue and all frame indices are
2920cfb5f85SDan Gohman   // rewritten, eliminate SP and FP. This allows them to be stackified,
2930cfb5f85SDan Gohman   // colored, and numbered with the rest of the registers.
2940cfb5f85SDan Gohman   addPass(createWebAssemblyReplacePhysRegs());
2950cfb5f85SDan Gohman 
2966f69783fSDerek Schuff   // Rewrite pseudo call_indirect instructions as real instructions.
2976f69783fSDerek Schuff   // This needs to run before register stackification, because we change the
2986f69783fSDerek Schuff   // order of the arguments.
2996f69783fSDerek Schuff   addPass(createWebAssemblyCallIndirectFixup());
3006f69783fSDerek Schuff 
3010cfb5f85SDan Gohman   if (getOptLevel() != CodeGenOpt::None) {
3020cfb5f85SDan Gohman     // LiveIntervals isn't commonly run this late. Re-establish preconditions.
3030cfb5f85SDan Gohman     addPass(createWebAssemblyPrepareForLiveIntervals());
3040cfb5f85SDan Gohman 
3050cfb5f85SDan Gohman     // Depend on LiveIntervals and perform some optimizations on it.
3060cfb5f85SDan Gohman     addPass(createWebAssemblyOptimizeLiveIntervals());
3070cfb5f85SDan Gohman 
3080cfb5f85SDan Gohman     // Prepare store instructions for register stackifying.
3090cfb5f85SDan Gohman     addPass(createWebAssemblyStoreResults());
3100cfb5f85SDan Gohman 
311e040533eSDan Gohman     // Mark registers as representing wasm's value stack. This is a key
3120cfb5f85SDan Gohman     // code-compression technique in WebAssembly. We run this pass (and
3130cfb5f85SDan Gohman     // StoreResults above) very late, so that it sees as much code as possible,
3140cfb5f85SDan Gohman     // including code emitted by PEI and expanded by late tail duplication.
3150cfb5f85SDan Gohman     addPass(createWebAssemblyRegStackify());
3160cfb5f85SDan Gohman 
3170cfb5f85SDan Gohman     // Run the register coloring pass to reduce the total number of registers.
3180cfb5f85SDan Gohman     // This runs after stackification so that it doesn't consider registers
3190cfb5f85SDan Gohman     // that become stackified.
3200cfb5f85SDan Gohman     addPass(createWebAssemblyRegColoring());
3210cfb5f85SDan Gohman   }
3220cfb5f85SDan Gohman 
323d934cb88SDan Gohman   // Eliminate multiple-entry loops. Do this before inserting explicit get_local
324d934cb88SDan Gohman   // and set_local operators because we create a new variable that we want
325d934cb88SDan Gohman   // converted into a local.
326d934cb88SDan Gohman   addPass(createWebAssemblyFixIrreducibleControlFlow());
327d934cb88SDan Gohman 
328a7be3755SWouter van Oortmerssen   // Insert explicit get_local and set_local operators.
329a7be3755SWouter van Oortmerssen   addPass(createWebAssemblyExplicitLocals());
330a7be3755SWouter van Oortmerssen 
3315ef4d5f9SHeejin Ahn   // Do various transformations for exception handling
3324934f76bSHeejin Ahn   addPass(createWebAssemblyLateEHPrepare());
3335ef4d5f9SHeejin Ahn 
334f52ee17aSDan Gohman   // Sort the blocks of the CFG into topological order, a prerequisite for
335f52ee17aSDan Gohman   // BLOCK and LOOP markers.
336f52ee17aSDan Gohman   addPass(createWebAssemblyCFGSort());
337f52ee17aSDan Gohman 
338f52ee17aSDan Gohman   // Insert BLOCK and LOOP markers.
339950a13cfSDan Gohman   addPass(createWebAssemblyCFGStackify());
3405941bde0SDan Gohman 
341f0b165a7SDan Gohman   // Lower br_unless into br_if.
342f0b165a7SDan Gohman   addPass(createWebAssemblyLowerBrUnless());
343f0b165a7SDan Gohman 
3445941bde0SDan Gohman   // Perform the very last peephole optimizations on the code.
345b13c91f1SDan Gohman   if (getOptLevel() != CodeGenOpt::None)
34681719f85SDan Gohman     addPass(createWebAssemblyPeephole());
347b7c2400fSDan Gohman 
348b7c2400fSDan Gohman   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
349b7c2400fSDan Gohman   addPass(createWebAssemblyRegNumbering());
350950a13cfSDan Gohman }
351