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