xref: /llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp (revision bac974278c5e5a3d6dea40d2d22cb36bcc487cee)
1 //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines the WebAssembly-specific subclass of TargetMachine.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "WebAssemblyTargetMachine.h"
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "TargetInfo/WebAssemblyTargetInfo.h"
17 #include "Utils/WebAssemblyUtilities.h"
18 #include "WebAssembly.h"
19 #include "WebAssemblyMachineFunctionInfo.h"
20 #include "WebAssemblyTargetObjectFile.h"
21 #include "WebAssemblyTargetTransformInfo.h"
22 #include "llvm/CodeGen/MIRParser/MIParser.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/RegAllocRegistry.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/InitializePasses.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/TargetRegistry.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Transforms/Scalar.h"
33 #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
34 #include "llvm/Transforms/Utils.h"
35 #include <optional>
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "wasm"
39 
40 // A command-line option to keep implicit locals
41 // for the purpose of testing with lit/llc ONLY.
42 // This produces output which is not valid WebAssembly, and is not supported
43 // by assemblers/disassemblers and other MC based tools.
44 static cl::opt<bool> WasmDisableExplicitLocals(
45     "wasm-disable-explicit-locals", cl::Hidden,
46     cl::desc("WebAssembly: output implicit locals in"
47              " instruction output for test purposes only."),
48     cl::init(false));
49 
50 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
51   // Register the target.
52   RegisterTargetMachine<WebAssemblyTargetMachine> X(
53       getTheWebAssemblyTarget32());
54   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
55       getTheWebAssemblyTarget64());
56 
57   // Register backend passes
58   auto &PR = *PassRegistry::getPassRegistry();
59   initializeWebAssemblyAddMissingPrototypesPass(PR);
60   initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
61   initializeLowerGlobalDtorsLegacyPassPass(PR);
62   initializeFixFunctionBitcastsPass(PR);
63   initializeOptimizeReturnedPass(PR);
64   initializeWebAssemblyArgumentMovePass(PR);
65   initializeWebAssemblySetP2AlignOperandsPass(PR);
66   initializeWebAssemblyReplacePhysRegsPass(PR);
67   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
68   initializeWebAssemblyMemIntrinsicResultsPass(PR);
69   initializeWebAssemblyRegStackifyPass(PR);
70   initializeWebAssemblyRegColoringPass(PR);
71   initializeWebAssemblyNullifyDebugValueListsPass(PR);
72   initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
73   initializeWebAssemblyLateEHPreparePass(PR);
74   initializeWebAssemblyExceptionInfoPass(PR);
75   initializeWebAssemblyCFGSortPass(PR);
76   initializeWebAssemblyCFGStackifyPass(PR);
77   initializeWebAssemblyExplicitLocalsPass(PR);
78   initializeWebAssemblyLowerBrUnlessPass(PR);
79   initializeWebAssemblyRegNumberingPass(PR);
80   initializeWebAssemblyDebugFixupPass(PR);
81   initializeWebAssemblyPeepholePass(PR);
82   initializeWebAssemblyMCLowerPrePassPass(PR);
83 }
84 
85 //===----------------------------------------------------------------------===//
86 // WebAssembly Lowering public interface.
87 //===----------------------------------------------------------------------===//
88 
89 static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM,
90                                            const Triple &TT) {
91   if (!RM) {
92     // Default to static relocation model.  This should always be more optimial
93     // than PIC since the static linker can determine all global addresses and
94     // assume direct function calls.
95     return Reloc::Static;
96   }
97 
98   if (!TT.isOSEmscripten()) {
99     // Relocation modes other than static are currently implemented in a way
100     // that only works for Emscripten, so disable them if we aren't targeting
101     // Emscripten.
102     return Reloc::Static;
103   }
104 
105   return *RM;
106 }
107 
108 /// Create an WebAssembly architecture model.
109 ///
110 WebAssemblyTargetMachine::WebAssemblyTargetMachine(
111     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
112     const TargetOptions &Options, std::optional<Reloc::Model> RM,
113     std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
114     : LLVMTargetMachine(
115           T,
116           TT.isArch64Bit()
117               ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
118                                        "f128:64-n32:64-S128-ni:1:10:20"
119                                      : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
120                                        "n32:64-S128-ni:1:10:20")
121               : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
122                                        "f128:64-n32:64-S128-ni:1:10:20"
123                                      : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
124                                        "n32:64-S128-ni:1:10:20"),
125           TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
126           getEffectiveCodeModel(CM, CodeModel::Large), OL),
127       TLOF(new WebAssemblyTargetObjectFile()) {
128   // WebAssembly type-checks instructions, but a noreturn function with a return
129   // type that doesn't match the context will cause a check failure. So we lower
130   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
131   // 'unreachable' instructions which is meant for that case.
132   this->Options.TrapUnreachable = true;
133 
134   // WebAssembly treats each function as an independent unit. Force
135   // -ffunction-sections, effectively, so that we can emit them independently.
136   this->Options.FunctionSections = true;
137   this->Options.DataSections = true;
138   this->Options.UniqueSectionNames = true;
139 
140   initAsmInfo();
141 
142   // Note that we don't use setRequiresStructuredCFG(true). It disables
143   // optimizations than we're ok with, and want, such as critical edge
144   // splitting and tail merging.
145 }
146 
147 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
148 
149 const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const {
150   return getSubtargetImpl(std::string(getTargetCPU()),
151                           std::string(getTargetFeatureString()));
152 }
153 
154 const WebAssemblySubtarget *
155 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
156                                            std::string FS) const {
157   auto &I = SubtargetMap[CPU + FS];
158   if (!I) {
159     I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
160   }
161   return I.get();
162 }
163 
164 const WebAssemblySubtarget *
165 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
166   Attribute CPUAttr = F.getFnAttribute("target-cpu");
167   Attribute FSAttr = F.getFnAttribute("target-features");
168 
169   std::string CPU =
170       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
171   std::string FS =
172       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
173 
174   // This needs to be done before we create a new subtarget since any
175   // creation will depend on the TM and the code generation flags on the
176   // function that reside in TargetOptions.
177   resetTargetOptions(F);
178 
179   return getSubtargetImpl(CPU, FS);
180 }
181 
182 namespace {
183 
184 class CoalesceFeaturesAndStripAtomics final : public ModulePass {
185   // Take the union of all features used in the module and use it for each
186   // function individually, since having multiple feature sets in one module
187   // currently does not make sense for WebAssembly. If atomics are not enabled,
188   // also strip atomic operations and thread local storage.
189   static char ID;
190   WebAssemblyTargetMachine *WasmTM;
191 
192 public:
193   CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
194       : ModulePass(ID), WasmTM(WasmTM) {}
195 
196   bool runOnModule(Module &M) override {
197     FeatureBitset Features = coalesceFeatures(M);
198 
199     std::string FeatureStr = getFeatureString(Features);
200     WasmTM->setTargetFeatureString(FeatureStr);
201     for (auto &F : M)
202       replaceFeatures(F, FeatureStr);
203 
204     bool StrippedAtomics = false;
205     bool StrippedTLS = false;
206 
207     if (!Features[WebAssembly::FeatureAtomics]) {
208       StrippedAtomics = stripAtomics(M);
209       StrippedTLS = stripThreadLocals(M);
210     } else if (!Features[WebAssembly::FeatureBulkMemory]) {
211       StrippedTLS |= stripThreadLocals(M);
212     }
213 
214     if (StrippedAtomics && !StrippedTLS)
215       stripThreadLocals(M);
216     else if (StrippedTLS && !StrippedAtomics)
217       stripAtomics(M);
218 
219     recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
220 
221     // Conservatively assume we have made some change
222     return true;
223   }
224 
225 private:
226   FeatureBitset coalesceFeatures(const Module &M) {
227     FeatureBitset Features =
228         WasmTM
229             ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
230                                std::string(WasmTM->getTargetFeatureString()))
231             ->getFeatureBits();
232     for (auto &F : M)
233       Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
234     return Features;
235   }
236 
237   std::string getFeatureString(const FeatureBitset &Features) {
238     std::string Ret;
239     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
240       if (Features[KV.Value])
241         Ret += (StringRef("+") + KV.Key + ",").str();
242     }
243     return Ret;
244   }
245 
246   void replaceFeatures(Function &F, const std::string &Features) {
247     F.removeFnAttr("target-features");
248     F.removeFnAttr("target-cpu");
249     F.addFnAttr("target-features", Features);
250   }
251 
252   bool stripAtomics(Module &M) {
253     // Detect whether any atomics will be lowered, since there is no way to tell
254     // whether the LowerAtomic pass lowers e.g. stores.
255     bool Stripped = false;
256     for (auto &F : M) {
257       for (auto &B : F) {
258         for (auto &I : B) {
259           if (I.isAtomic()) {
260             Stripped = true;
261             goto done;
262           }
263         }
264       }
265     }
266 
267   done:
268     if (!Stripped)
269       return false;
270 
271     LowerAtomicPass Lowerer;
272     FunctionAnalysisManager FAM;
273     for (auto &F : M)
274       Lowerer.run(F, FAM);
275 
276     return true;
277   }
278 
279   bool stripThreadLocals(Module &M) {
280     bool Stripped = false;
281     for (auto &GV : M.globals()) {
282       if (GV.isThreadLocal()) {
283         Stripped = true;
284         GV.setThreadLocal(false);
285       }
286     }
287     return Stripped;
288   }
289 
290   void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
291     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
292       if (Features[KV.Value]) {
293         // Mark features as used
294         std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
295         M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
296                         wasm::WASM_FEATURE_PREFIX_USED);
297       }
298     }
299     // Code compiled without atomics or bulk-memory may have had its atomics or
300     // thread-local data lowered to nonatomic operations or non-thread-local
301     // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
302     // to tell the linker that it would be unsafe to allow this code ot be used
303     // in a module with shared memory.
304     if (Stripped) {
305       M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
306                       wasm::WASM_FEATURE_PREFIX_DISALLOWED);
307     }
308   }
309 };
310 char CoalesceFeaturesAndStripAtomics::ID = 0;
311 
312 /// WebAssembly Code Generator Pass Configuration Options.
313 class WebAssemblyPassConfig final : public TargetPassConfig {
314 public:
315   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
316       : TargetPassConfig(TM, PM) {}
317 
318   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
319     return getTM<WebAssemblyTargetMachine>();
320   }
321 
322   FunctionPass *createTargetRegisterAllocator(bool) override;
323 
324   void addIRPasses() override;
325   void addISelPrepare() override;
326   bool addInstSelector() override;
327   void addOptimizedRegAlloc() override;
328   void addPostRegAlloc() override;
329   bool addGCPasses() override { return false; }
330   void addPreEmitPass() override;
331   bool addPreISel() override;
332 
333   // No reg alloc
334   bool addRegAssignAndRewriteFast() override { return false; }
335 
336   // No reg alloc
337   bool addRegAssignAndRewriteOptimized() override { return false; }
338 };
339 } // end anonymous namespace
340 
341 TargetTransformInfo
342 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) const {
343   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
344 }
345 
346 TargetPassConfig *
347 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
348   return new WebAssemblyPassConfig(*this, PM);
349 }
350 
351 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
352   return nullptr; // No reg alloc
353 }
354 
355 using WebAssembly::WasmEnableEH;
356 using WebAssembly::WasmEnableEmEH;
357 using WebAssembly::WasmEnableEmSjLj;
358 using WebAssembly::WasmEnableSjLj;
359 
360 static void basicCheckForEHAndSjLj(TargetMachine *TM) {
361   // Before checking, we make sure TargetOptions.ExceptionModel is the same as
362   // MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
363   // stores the exception model info in LangOptions, which is later transferred
364   // to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
365   // clang's LangOptions is not used and thus the exception model info is not
366   // correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
367   // have the correct exception model in in WebAssemblyMCAsmInfo constructor.
368   // But in this case TargetOptions is still not updated, so we make sure they
369   // are the same.
370   TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType();
371 
372   // Basic Correctness checking related to -exception-model
373   if (TM->Options.ExceptionModel != ExceptionHandling::None &&
374       TM->Options.ExceptionModel != ExceptionHandling::Wasm)
375     report_fatal_error("-exception-model should be either 'none' or 'wasm'");
376   if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
377     report_fatal_error("-exception-model=wasm not allowed with "
378                        "-enable-emscripten-cxx-exceptions");
379   if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
380     report_fatal_error(
381         "-wasm-enable-eh only allowed with -exception-model=wasm");
382   if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
383     report_fatal_error(
384         "-wasm-enable-sjlj only allowed with -exception-model=wasm");
385   if ((!WasmEnableEH && !WasmEnableSjLj) &&
386       TM->Options.ExceptionModel == ExceptionHandling::Wasm)
387     report_fatal_error(
388         "-exception-model=wasm only allowed with at least one of "
389         "-wasm-enable-eh or -wasm-enable-sjj");
390 
391   // You can't enable two modes of EH at the same time
392   if (WasmEnableEmEH && WasmEnableEH)
393     report_fatal_error(
394         "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
395   // You can't enable two modes of SjLj at the same time
396   if (WasmEnableEmSjLj && WasmEnableSjLj)
397     report_fatal_error(
398         "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
399   // You can't mix Emscripten EH with Wasm SjLj.
400   if (WasmEnableEmEH && WasmEnableSjLj)
401     report_fatal_error(
402         "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
403   // Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
404   // measure, but some code will error out at compile time in this combination.
405   // See WebAssemblyLowerEmscriptenEHSjLj pass for details.
406 }
407 
408 //===----------------------------------------------------------------------===//
409 // The following functions are called from lib/CodeGen/Passes.cpp to modify
410 // the CodeGen pass sequence.
411 //===----------------------------------------------------------------------===//
412 
413 void WebAssemblyPassConfig::addIRPasses() {
414   // Add signatures to prototype-less function declarations
415   addPass(createWebAssemblyAddMissingPrototypes());
416 
417   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
418   addPass(createLowerGlobalDtorsLegacyPass());
419 
420   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
421   // to match.
422   addPass(createWebAssemblyFixFunctionBitcasts());
423 
424   // Optimize "returned" function attributes.
425   if (getOptLevel() != CodeGenOpt::None)
426     addPass(createWebAssemblyOptimizeReturned());
427 
428   basicCheckForEHAndSjLj(TM);
429 
430   // If exception handling is not enabled and setjmp/longjmp handling is
431   // enabled, we lower invokes into calls and delete unreachable landingpad
432   // blocks. Lowering invokes when there is no EH support is done in
433   // TargetPassConfig::addPassesToHandleExceptions, but that runs after these IR
434   // passes and Emscripten SjLj handling expects all invokes to be lowered
435   // before.
436   if (!WasmEnableEmEH && !WasmEnableEH) {
437     addPass(createLowerInvokePass());
438     // The lower invoke pass may create unreachable code. Remove it in order not
439     // to process dead blocks in setjmp/longjmp handling.
440     addPass(createUnreachableBlockEliminationPass());
441   }
442 
443   // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation
444   // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and
445   // transformation algorithms with Emscripten SjLj, so we run
446   // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled.
447   if (WasmEnableEmEH || WasmEnableEmSjLj || WasmEnableSjLj)
448     addPass(createWebAssemblyLowerEmscriptenEHSjLj());
449 
450   // Expand indirectbr instructions to switches.
451   addPass(createIndirectBrExpandPass());
452 
453   TargetPassConfig::addIRPasses();
454 }
455 
456 void WebAssemblyPassConfig::addISelPrepare() {
457   // Lower atomics and TLS if necessary
458   addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
459 
460   // This is a no-op if atomics are not used in the module
461   addPass(createAtomicExpandPass());
462 
463   TargetPassConfig::addISelPrepare();
464 }
465 
466 bool WebAssemblyPassConfig::addInstSelector() {
467   (void)TargetPassConfig::addInstSelector();
468   addPass(
469       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
470   // Run the argument-move pass immediately after the ScheduleDAG scheduler
471   // so that we can fix up the ARGUMENT instructions before anything else
472   // sees them in the wrong place.
473   addPass(createWebAssemblyArgumentMove());
474   // Set the p2align operands. This information is present during ISel, however
475   // it's inconvenient to collect. Collect it now, and update the immediate
476   // operands.
477   addPass(createWebAssemblySetP2AlignOperands());
478 
479   // Eliminate range checks and add default targets to br_table instructions.
480   addPass(createWebAssemblyFixBrTableDefaults());
481 
482   return false;
483 }
484 
485 void WebAssemblyPassConfig::addOptimizedRegAlloc() {
486   // Currently RegisterCoalesce degrades wasm debug info quality by a
487   // significant margin. As a quick fix, disable this for -O1, which is often
488   // used for debugging large applications. Disabling this increases code size
489   // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is
490   // usually not used for production builds.
491   // TODO Investigate why RegisterCoalesce degrades debug info quality and fix
492   // it properly
493   if (getOptLevel() == CodeGenOpt::Less)
494     disablePass(&RegisterCoalescerID);
495   TargetPassConfig::addOptimizedRegAlloc();
496 }
497 
498 void WebAssemblyPassConfig::addPostRegAlloc() {
499   // TODO: The following CodeGen passes don't currently support code containing
500   // virtual registers. Consider removing their restrictions and re-enabling
501   // them.
502 
503   // These functions all require the NoVRegs property.
504   disablePass(&MachineCopyPropagationID);
505   disablePass(&PostRAMachineSinkingID);
506   disablePass(&PostRASchedulerID);
507   disablePass(&FuncletLayoutID);
508   disablePass(&StackMapLivenessID);
509   disablePass(&LiveDebugValuesID);
510   disablePass(&PatchableFunctionID);
511   disablePass(&ShrinkWrapID);
512 
513   // This pass hurts code size for wasm because it can generate irreducible
514   // control flow.
515   disablePass(&MachineBlockPlacementID);
516 
517   TargetPassConfig::addPostRegAlloc();
518 }
519 
520 void WebAssemblyPassConfig::addPreEmitPass() {
521   TargetPassConfig::addPreEmitPass();
522 
523   // Nullify DBG_VALUE_LISTs that we cannot handle.
524   addPass(createWebAssemblyNullifyDebugValueLists());
525 
526   // Eliminate multiple-entry loops.
527   addPass(createWebAssemblyFixIrreducibleControlFlow());
528 
529   // Do various transformations for exception handling.
530   // Every CFG-changing optimizations should come before this.
531   if (TM->Options.ExceptionModel == ExceptionHandling::Wasm)
532     addPass(createWebAssemblyLateEHPrepare());
533 
534   // Now that we have a prologue and epilogue and all frame indices are
535   // rewritten, eliminate SP and FP. This allows them to be stackified,
536   // colored, and numbered with the rest of the registers.
537   addPass(createWebAssemblyReplacePhysRegs());
538 
539   // Preparations and optimizations related to register stackification.
540   if (getOptLevel() != CodeGenOpt::None) {
541     // Depend on LiveIntervals and perform some optimizations on it.
542     addPass(createWebAssemblyOptimizeLiveIntervals());
543 
544     // Prepare memory intrinsic calls for register stackifying.
545     addPass(createWebAssemblyMemIntrinsicResults());
546 
547     // Mark registers as representing wasm's value stack. This is a key
548     // code-compression technique in WebAssembly. We run this pass (and
549     // MemIntrinsicResults above) very late, so that it sees as much code as
550     // possible, including code emitted by PEI and expanded by late tail
551     // duplication.
552     addPass(createWebAssemblyRegStackify());
553 
554     // Run the register coloring pass to reduce the total number of registers.
555     // This runs after stackification so that it doesn't consider registers
556     // that become stackified.
557     addPass(createWebAssemblyRegColoring());
558   }
559 
560   // Sort the blocks of the CFG into topological order, a prerequisite for
561   // BLOCK and LOOP markers.
562   addPass(createWebAssemblyCFGSort());
563 
564   // Insert BLOCK and LOOP markers.
565   addPass(createWebAssemblyCFGStackify());
566 
567   // Insert explicit local.get and local.set operators.
568   if (!WasmDisableExplicitLocals)
569     addPass(createWebAssemblyExplicitLocals());
570 
571   // Lower br_unless into br_if.
572   addPass(createWebAssemblyLowerBrUnless());
573 
574   // Perform the very last peephole optimizations on the code.
575   if (getOptLevel() != CodeGenOpt::None)
576     addPass(createWebAssemblyPeephole());
577 
578   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
579   addPass(createWebAssemblyRegNumbering());
580 
581   // Fix debug_values whose defs have been stackified.
582   if (!WasmDisableExplicitLocals)
583     addPass(createWebAssemblyDebugFixup());
584 
585   // Collect information to prepare for MC lowering / asm printing.
586   addPass(createWebAssemblyMCLowerPrePass());
587 }
588 
589 bool WebAssemblyPassConfig::addPreISel() {
590   TargetPassConfig::addPreISel();
591   addPass(createWebAssemblyLowerRefTypesIntPtrConv());
592   return false;
593 }
594 
595 yaml::MachineFunctionInfo *
596 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
597   return new yaml::WebAssemblyFunctionInfo();
598 }
599 
600 yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
601     const MachineFunction &MF) const {
602   const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
603   return new yaml::WebAssemblyFunctionInfo(MF, *MFI);
604 }
605 
606 bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
607     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
608     SMDiagnostic &Error, SMRange &SourceRange) const {
609   const auto &YamlMFI = static_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
610   MachineFunction &MF = PFS.MF;
611   MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(MF, YamlMFI);
612   return false;
613 }
614