1 //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines the WebAssembly-specific subclass of TargetMachine.
12 //===----------------------------------------------------------------------===//
14 #include "WebAssemblyTargetMachine.h"
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "TargetInfo/WebAssemblyTargetInfo.h"
17 #include "WebAssembly.h"
18 #include "WebAssemblyISelLowering.h"
19 #include "WebAssemblyMachineFunctionInfo.h"
20 #include "WebAssemblyTargetObjectFile.h"
21 #include "WebAssemblyTargetTransformInfo.h"
22 #include "WebAssemblyUtilities.h"
23 #include "llvm/CodeGen/MIRParser/MIParser.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"
38 #define DEBUG_TYPE "wasm"
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."),
50 static cl::opt
<bool> WasmDisableFixIrreducibleControlFlowPass(
51 "wasm-disable-fix-irreducible-control-flow-pass", cl::Hidden
,
52 cl::desc("webassembly: disables the fix "
53 " irreducible control flow optimization pass"),
56 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeWebAssemblyTarget() {
57 // Register the target.
58 RegisterTargetMachine
<WebAssemblyTargetMachine
> X(
59 getTheWebAssemblyTarget32());
60 RegisterTargetMachine
<WebAssemblyTargetMachine
> Y(
61 getTheWebAssemblyTarget64());
63 // Register backend passes
64 auto &PR
= *PassRegistry::getPassRegistry();
65 initializeWebAssemblyAddMissingPrototypesPass(PR
);
66 initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR
);
67 initializeLowerGlobalDtorsLegacyPassPass(PR
);
68 initializeFixFunctionBitcastsPass(PR
);
69 initializeOptimizeReturnedPass(PR
);
70 initializeWebAssemblyRefTypeMem2LocalPass(PR
);
71 initializeWebAssemblyArgumentMovePass(PR
);
72 initializeWebAssemblySetP2AlignOperandsPass(PR
);
73 initializeWebAssemblyReplacePhysRegsPass(PR
);
74 initializeWebAssemblyOptimizeLiveIntervalsPass(PR
);
75 initializeWebAssemblyMemIntrinsicResultsPass(PR
);
76 initializeWebAssemblyRegStackifyPass(PR
);
77 initializeWebAssemblyRegColoringPass(PR
);
78 initializeWebAssemblyNullifyDebugValueListsPass(PR
);
79 initializeWebAssemblyFixIrreducibleControlFlowPass(PR
);
80 initializeWebAssemblyLateEHPreparePass(PR
);
81 initializeWebAssemblyExceptionInfoPass(PR
);
82 initializeWebAssemblyCFGSortPass(PR
);
83 initializeWebAssemblyCFGStackifyPass(PR
);
84 initializeWebAssemblyExplicitLocalsPass(PR
);
85 initializeWebAssemblyLowerBrUnlessPass(PR
);
86 initializeWebAssemblyRegNumberingPass(PR
);
87 initializeWebAssemblyDebugFixupPass(PR
);
88 initializeWebAssemblyPeepholePass(PR
);
89 initializeWebAssemblyMCLowerPrePassPass(PR
);
90 initializeWebAssemblyLowerRefTypesIntPtrConvPass(PR
);
91 initializeWebAssemblyFixBrTableDefaultsPass(PR
);
92 initializeWebAssemblyDAGToDAGISelLegacyPass(PR
);
95 //===----------------------------------------------------------------------===//
96 // WebAssembly Lowering public interface.
97 //===----------------------------------------------------------------------===//
99 static Reloc::Model
getEffectiveRelocModel(std::optional
<Reloc::Model
> RM
,
102 // Default to static relocation model. This should always be more optimial
103 // than PIC since the static linker can determine all global addresses and
104 // assume direct function calls.
105 return Reloc::Static
;
111 /// Create an WebAssembly architecture model.
113 WebAssemblyTargetMachine::WebAssemblyTargetMachine(
114 const Target
&T
, const Triple
&TT
, StringRef CPU
, StringRef FS
,
115 const TargetOptions
&Options
, std::optional
<Reloc::Model
> RM
,
116 std::optional
<CodeModel::Model
> CM
, CodeGenOptLevel OL
, bool JIT
)
117 : CodeGenTargetMachineImpl(
120 ? (TT
.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
121 "f128:64-n32:64-S128-ni:1:10:20"
122 : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
123 "n32:64-S128-ni:1:10:20")
124 : (TT
.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
125 "f128:64-n32:64-S128-ni:1:10:20"
126 : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
127 "n32:64-S128-ni:1:10:20"),
128 TT
, CPU
, FS
, Options
, getEffectiveRelocModel(RM
, TT
),
129 getEffectiveCodeModel(CM
, CodeModel::Large
), OL
),
130 TLOF(new WebAssemblyTargetObjectFile()),
131 UsesMultivalueABI(Options
.MCOptions
.getABIName() == "experimental-mv") {
132 // WebAssembly type-checks instructions, but a noreturn function with a return
133 // type that doesn't match the context will cause a check failure. So we lower
134 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
135 // 'unreachable' instructions which is meant for that case. Formerly, we also
136 // needed to add checks to SP failure emission in the instruction selection
137 // backends, but this has since been tied to TrapUnreachable and is no longer
139 this->Options
.TrapUnreachable
= true;
140 this->Options
.NoTrapAfterNoreturn
= false;
142 // WebAssembly treats each function as an independent unit. Force
143 // -ffunction-sections, effectively, so that we can emit them independently.
144 this->Options
.FunctionSections
= true;
145 this->Options
.DataSections
= true;
146 this->Options
.UniqueSectionNames
= true;
150 // Note that we don't use setRequiresStructuredCFG(true). It disables
151 // optimizations than we're ok with, and want, such as critical edge
152 // splitting and tail merging.
155 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
157 const WebAssemblySubtarget
*WebAssemblyTargetMachine::getSubtargetImpl() const {
158 return getSubtargetImpl(std::string(getTargetCPU()),
159 std::string(getTargetFeatureString()));
162 const WebAssemblySubtarget
*
163 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU
,
164 std::string FS
) const {
165 auto &I
= SubtargetMap
[CPU
+ FS
];
167 I
= std::make_unique
<WebAssemblySubtarget
>(TargetTriple
, CPU
, FS
, *this);
172 const WebAssemblySubtarget
*
173 WebAssemblyTargetMachine::getSubtargetImpl(const Function
&F
) const {
174 Attribute CPUAttr
= F
.getFnAttribute("target-cpu");
175 Attribute FSAttr
= F
.getFnAttribute("target-features");
178 CPUAttr
.isValid() ? CPUAttr
.getValueAsString().str() : TargetCPU
;
180 FSAttr
.isValid() ? FSAttr
.getValueAsString().str() : TargetFS
;
182 // This needs to be done before we create a new subtarget since any
183 // creation will depend on the TM and the code generation flags on the
184 // function that reside in TargetOptions.
185 resetTargetOptions(F
);
187 return getSubtargetImpl(CPU
, FS
);
192 class CoalesceFeaturesAndStripAtomics final
: public ModulePass
{
193 // Take the union of all features used in the module and use it for each
194 // function individually, since having multiple feature sets in one module
195 // currently does not make sense for WebAssembly. If atomics are not enabled,
196 // also strip atomic operations and thread local storage.
198 WebAssemblyTargetMachine
*WasmTM
;
201 CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine
*WasmTM
)
202 : ModulePass(ID
), WasmTM(WasmTM
) {}
204 bool runOnModule(Module
&M
) override
{
205 FeatureBitset Features
= coalesceFeatures(M
);
207 std::string FeatureStr
= getFeatureString(Features
);
208 WasmTM
->setTargetFeatureString(FeatureStr
);
210 replaceFeatures(F
, FeatureStr
);
212 bool StrippedAtomics
= false;
213 bool StrippedTLS
= false;
215 if (!Features
[WebAssembly::FeatureAtomics
]) {
216 StrippedAtomics
= stripAtomics(M
);
217 StrippedTLS
= stripThreadLocals(M
);
218 } else if (!Features
[WebAssembly::FeatureBulkMemory
]) {
219 StrippedTLS
|= stripThreadLocals(M
);
222 if (StrippedAtomics
&& !StrippedTLS
)
223 stripThreadLocals(M
);
224 else if (StrippedTLS
&& !StrippedAtomics
)
227 recordFeatures(M
, Features
, StrippedAtomics
|| StrippedTLS
);
229 // Conservatively assume we have made some change
234 FeatureBitset
coalesceFeatures(const Module
&M
) {
235 // Union the features of all defined functions. Start with an empty set, so
236 // that if a feature is disabled in every function, we'll compute it as
237 // disabled. If any function lacks a target-features attribute, it'll
238 // default to the target CPU from the `TargetMachine`.
239 FeatureBitset Features
;
240 bool AnyDefinedFuncs
= false;
242 if (F
.isDeclaration())
245 Features
|= WasmTM
->getSubtargetImpl(F
)->getFeatureBits();
246 AnyDefinedFuncs
= true;
249 // If we have no defined functions, use the target CPU from the
251 if (!AnyDefinedFuncs
) {
254 ->getSubtargetImpl(std::string(WasmTM
->getTargetCPU()),
255 std::string(WasmTM
->getTargetFeatureString()))
262 static std::string
getFeatureString(const FeatureBitset
&Features
) {
264 for (const SubtargetFeatureKV
&KV
: WebAssemblyFeatureKV
) {
265 if (Features
[KV
.Value
])
266 Ret
+= (StringRef("+") + KV
.Key
+ ",").str();
268 Ret
+= (StringRef("-") + KV
.Key
+ ",").str();
273 void replaceFeatures(Function
&F
, const std::string
&Features
) {
274 F
.removeFnAttr("target-features");
275 F
.removeFnAttr("target-cpu");
276 F
.addFnAttr("target-features", Features
);
279 bool stripAtomics(Module
&M
) {
280 // Detect whether any atomics will be lowered, since there is no way to tell
281 // whether the LowerAtomic pass lowers e.g. stores.
282 bool Stripped
= false;
298 LowerAtomicPass Lowerer
;
299 FunctionAnalysisManager FAM
;
306 bool stripThreadLocals(Module
&M
) {
307 bool Stripped
= false;
308 for (auto &GV
: M
.globals()) {
309 if (GV
.isThreadLocal()) {
310 // replace `@llvm.threadlocal.address.pX(GV)` with `GV`.
311 for (Use
&U
: make_early_inc_range(GV
.uses())) {
312 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(U
.getUser())) {
313 if (II
->getIntrinsicID() == Intrinsic::threadlocal_address
&&
314 II
->getArgOperand(0) == &GV
) {
315 II
->replaceAllUsesWith(&GV
);
316 II
->eraseFromParent();
322 GV
.setThreadLocal(false);
328 void recordFeatures(Module
&M
, const FeatureBitset
&Features
, bool Stripped
) {
329 for (const SubtargetFeatureKV
&KV
: WebAssemblyFeatureKV
) {
330 if (Features
[KV
.Value
]) {
331 // Mark features as used
332 std::string MDKey
= (StringRef("wasm-feature-") + KV
.Key
).str();
333 M
.addModuleFlag(Module::ModFlagBehavior::Error
, MDKey
,
334 wasm::WASM_FEATURE_PREFIX_USED
);
337 // Code compiled without atomics or bulk-memory may have had its atomics or
338 // thread-local data lowered to nonatomic operations or non-thread-local
339 // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
340 // to tell the linker that it would be unsafe to allow this code ot be used
341 // in a module with shared memory.
343 M
.addModuleFlag(Module::ModFlagBehavior::Error
, "wasm-feature-shared-mem",
344 wasm::WASM_FEATURE_PREFIX_DISALLOWED
);
348 char CoalesceFeaturesAndStripAtomics::ID
= 0;
350 /// WebAssembly Code Generator Pass Configuration Options.
351 class WebAssemblyPassConfig final
: public TargetPassConfig
{
353 WebAssemblyPassConfig(WebAssemblyTargetMachine
&TM
, PassManagerBase
&PM
)
354 : TargetPassConfig(TM
, PM
) {}
356 WebAssemblyTargetMachine
&getWebAssemblyTargetMachine() const {
357 return getTM
<WebAssemblyTargetMachine
>();
360 FunctionPass
*createTargetRegisterAllocator(bool) override
;
362 void addIRPasses() override
;
363 void addISelPrepare() override
;
364 bool addInstSelector() override
;
365 void addOptimizedRegAlloc() override
;
366 void addPostRegAlloc() override
;
367 bool addGCPasses() override
{ return false; }
368 void addPreEmitPass() override
;
369 bool addPreISel() override
;
372 bool addRegAssignAndRewriteFast() override
{ return false; }
375 bool addRegAssignAndRewriteOptimized() override
{ return false; }
377 } // end anonymous namespace
379 MachineFunctionInfo
*WebAssemblyTargetMachine::createMachineFunctionInfo(
380 BumpPtrAllocator
&Allocator
, const Function
&F
,
381 const TargetSubtargetInfo
*STI
) const {
382 return WebAssemblyFunctionInfo::create
<WebAssemblyFunctionInfo
>(Allocator
, F
,
387 WebAssemblyTargetMachine::getTargetTransformInfo(const Function
&F
) const {
388 return TargetTransformInfo(WebAssemblyTTIImpl(this, F
));
392 WebAssemblyTargetMachine::createPassConfig(PassManagerBase
&PM
) {
393 return new WebAssemblyPassConfig(*this, PM
);
396 FunctionPass
*WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
397 return nullptr; // No reg alloc
400 using WebAssembly::WasmEnableEH
;
401 using WebAssembly::WasmEnableEmEH
;
402 using WebAssembly::WasmEnableEmSjLj
;
403 using WebAssembly::WasmEnableExnref
;
404 using WebAssembly::WasmEnableSjLj
;
406 static void basicCheckForEHAndSjLj(TargetMachine
*TM
) {
408 // You can't enable two modes of EH at the same time
409 if (WasmEnableEmEH
&& WasmEnableEH
)
411 "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
412 // You can't enable two modes of SjLj at the same time
413 if (WasmEnableEmSjLj
&& WasmEnableSjLj
)
415 "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
416 // You can't mix Emscripten EH with Wasm SjLj.
417 if (WasmEnableEmEH
&& WasmEnableSjLj
)
419 "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
420 if (WasmEnableExnref
&& !WasmEnableEH
)
422 "-wasm-enable-exnref should be used with -wasm-enable-eh");
424 // Here we make sure TargetOptions.ExceptionModel is the same as
425 // MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
426 // stores the exception model info in LangOptions, which is later transferred
427 // to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
428 // clang's LangOptions is not used and thus the exception model info is not
429 // correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
430 // have the correct exception model in WebAssemblyMCAsmInfo constructor. But
431 // in this case TargetOptions is still not updated, so we make sure they are
433 TM
->Options
.ExceptionModel
= TM
->getMCAsmInfo()->getExceptionHandlingType();
435 // Basic Correctness checking related to -exception-model
436 if (TM
->Options
.ExceptionModel
!= ExceptionHandling::None
&&
437 TM
->Options
.ExceptionModel
!= ExceptionHandling::Wasm
)
438 report_fatal_error("-exception-model should be either 'none' or 'wasm'");
439 if (WasmEnableEmEH
&& TM
->Options
.ExceptionModel
== ExceptionHandling::Wasm
)
440 report_fatal_error("-exception-model=wasm not allowed with "
441 "-enable-emscripten-cxx-exceptions");
442 if (WasmEnableEH
&& TM
->Options
.ExceptionModel
!= ExceptionHandling::Wasm
)
444 "-wasm-enable-eh only allowed with -exception-model=wasm");
445 if (WasmEnableSjLj
&& TM
->Options
.ExceptionModel
!= ExceptionHandling::Wasm
)
447 "-wasm-enable-sjlj only allowed with -exception-model=wasm");
448 if ((!WasmEnableEH
&& !WasmEnableSjLj
) &&
449 TM
->Options
.ExceptionModel
== ExceptionHandling::Wasm
)
451 "-exception-model=wasm only allowed with at least one of "
452 "-wasm-enable-eh or -wasm-enable-sjlj");
454 // Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
455 // measure, but some code will error out at compile time in this combination.
456 // See WebAssemblyLowerEmscriptenEHSjLj pass for details.
459 //===----------------------------------------------------------------------===//
460 // The following functions are called from lib/CodeGen/Passes.cpp to modify
461 // the CodeGen pass sequence.
462 //===----------------------------------------------------------------------===//
464 void WebAssemblyPassConfig::addIRPasses() {
465 // Add signatures to prototype-less function declarations
466 addPass(createWebAssemblyAddMissingPrototypes());
468 // Lower .llvm.global_dtors into .llvm.global_ctors with __cxa_atexit calls.
469 addPass(createLowerGlobalDtorsLegacyPass());
471 // Fix function bitcasts, as WebAssembly requires caller and callee signatures
473 addPass(createWebAssemblyFixFunctionBitcasts());
475 // Optimize "returned" function attributes.
476 if (getOptLevel() != CodeGenOptLevel::None
)
477 addPass(createWebAssemblyOptimizeReturned());
479 basicCheckForEHAndSjLj(TM
);
481 // If exception handling is not enabled and setjmp/longjmp handling is
482 // enabled, we lower invokes into calls and delete unreachable landingpad
483 // blocks. Lowering invokes when there is no EH support is done in
484 // TargetPassConfig::addPassesToHandleExceptions, but that runs after these IR
485 // passes and Emscripten SjLj handling expects all invokes to be lowered
487 if (!WasmEnableEmEH
&& !WasmEnableEH
) {
488 addPass(createLowerInvokePass());
489 // The lower invoke pass may create unreachable code. Remove it in order not
490 // to process dead blocks in setjmp/longjmp handling.
491 addPass(createUnreachableBlockEliminationPass());
494 // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation
495 // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and
496 // transformation algorithms with Emscripten SjLj, so we run
497 // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled.
498 if (WasmEnableEmEH
|| WasmEnableEmSjLj
|| WasmEnableSjLj
)
499 addPass(createWebAssemblyLowerEmscriptenEHSjLj());
501 // Expand indirectbr instructions to switches.
502 addPass(createIndirectBrExpandPass());
504 TargetPassConfig::addIRPasses();
507 void WebAssemblyPassConfig::addISelPrepare() {
508 // We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
509 // loads and stores are promoted to local.gets/local.sets.
510 addPass(createWebAssemblyRefTypeMem2Local());
511 // Lower atomics and TLS if necessary
512 addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
514 // This is a no-op if atomics are not used in the module
515 addPass(createAtomicExpandLegacyPass());
517 TargetPassConfig::addISelPrepare();
520 bool WebAssemblyPassConfig::addInstSelector() {
521 (void)TargetPassConfig::addInstSelector();
523 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
524 // Run the argument-move pass immediately after the ScheduleDAG scheduler
525 // so that we can fix up the ARGUMENT instructions before anything else
526 // sees them in the wrong place.
527 addPass(createWebAssemblyArgumentMove());
528 // Set the p2align operands. This information is present during ISel, however
529 // it's inconvenient to collect. Collect it now, and update the immediate
531 addPass(createWebAssemblySetP2AlignOperands());
533 // Eliminate range checks and add default targets to br_table instructions.
534 addPass(createWebAssemblyFixBrTableDefaults());
536 // unreachable is terminator, non-terminator instruction after it is not
538 addPass(createWebAssemblyCleanCodeAfterTrap());
543 void WebAssemblyPassConfig::addOptimizedRegAlloc() {
544 // Currently RegisterCoalesce degrades wasm debug info quality by a
545 // significant margin. As a quick fix, disable this for -O1, which is often
546 // used for debugging large applications. Disabling this increases code size
547 // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is
548 // usually not used for production builds.
549 // TODO Investigate why RegisterCoalesce degrades debug info quality and fix
551 if (getOptLevel() == CodeGenOptLevel::Less
)
552 disablePass(&RegisterCoalescerID
);
553 TargetPassConfig::addOptimizedRegAlloc();
556 void WebAssemblyPassConfig::addPostRegAlloc() {
557 // TODO: The following CodeGen passes don't currently support code containing
558 // virtual registers. Consider removing their restrictions and re-enabling
561 // These functions all require the NoVRegs property.
562 disablePass(&MachineLateInstrsCleanupID
);
563 disablePass(&MachineCopyPropagationID
);
564 disablePass(&PostRAMachineSinkingID
);
565 disablePass(&PostRASchedulerID
);
566 disablePass(&FuncletLayoutID
);
567 disablePass(&StackMapLivenessID
);
568 disablePass(&PatchableFunctionID
);
569 disablePass(&ShrinkWrapID
);
570 disablePass(&RemoveLoadsIntoFakeUsesID
);
572 // This pass hurts code size for wasm because it can generate irreducible
574 disablePass(&MachineBlockPlacementID
);
576 TargetPassConfig::addPostRegAlloc();
579 void WebAssemblyPassConfig::addPreEmitPass() {
580 TargetPassConfig::addPreEmitPass();
582 // Nullify DBG_VALUE_LISTs that we cannot handle.
583 addPass(createWebAssemblyNullifyDebugValueLists());
585 // Eliminate multiple-entry loops.
586 if (!WasmDisableFixIrreducibleControlFlowPass
)
587 addPass(createWebAssemblyFixIrreducibleControlFlow());
589 // Do various transformations for exception handling.
590 // Every CFG-changing optimizations should come before this.
591 if (TM
->Options
.ExceptionModel
== ExceptionHandling::Wasm
)
592 addPass(createWebAssemblyLateEHPrepare());
594 // Now that we have a prologue and epilogue and all frame indices are
595 // rewritten, eliminate SP and FP. This allows them to be stackified,
596 // colored, and numbered with the rest of the registers.
597 addPass(createWebAssemblyReplacePhysRegs());
599 // Preparations and optimizations related to register stackification.
600 if (getOptLevel() != CodeGenOptLevel::None
) {
601 // Depend on LiveIntervals and perform some optimizations on it.
602 addPass(createWebAssemblyOptimizeLiveIntervals());
604 // Prepare memory intrinsic calls for register stackifying.
605 addPass(createWebAssemblyMemIntrinsicResults());
607 // Mark registers as representing wasm's value stack. This is a key
608 // code-compression technique in WebAssembly. We run this pass (and
609 // MemIntrinsicResults above) very late, so that it sees as much code as
610 // possible, including code emitted by PEI and expanded by late tail
612 addPass(createWebAssemblyRegStackify());
614 // Run the register coloring pass to reduce the total number of registers.
615 // This runs after stackification so that it doesn't consider registers
616 // that become stackified.
617 addPass(createWebAssemblyRegColoring());
620 // Sort the blocks of the CFG into topological order, a prerequisite for
621 // BLOCK and LOOP markers.
622 addPass(createWebAssemblyCFGSort());
624 // Insert BLOCK and LOOP markers.
625 addPass(createWebAssemblyCFGStackify());
627 // Insert explicit local.get and local.set operators.
628 if (!WasmDisableExplicitLocals
)
629 addPass(createWebAssemblyExplicitLocals());
631 // Lower br_unless into br_if.
632 addPass(createWebAssemblyLowerBrUnless());
634 // Perform the very last peephole optimizations on the code.
635 if (getOptLevel() != CodeGenOptLevel::None
)
636 addPass(createWebAssemblyPeephole());
638 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
639 addPass(createWebAssemblyRegNumbering());
641 // Fix debug_values whose defs have been stackified.
642 if (!WasmDisableExplicitLocals
)
643 addPass(createWebAssemblyDebugFixup());
645 // Collect information to prepare for MC lowering / asm printing.
646 addPass(createWebAssemblyMCLowerPrePass());
649 bool WebAssemblyPassConfig::addPreISel() {
650 TargetPassConfig::addPreISel();
651 addPass(createWebAssemblyLowerRefTypesIntPtrConv());
655 yaml::MachineFunctionInfo
*
656 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
657 return new yaml::WebAssemblyFunctionInfo();
660 yaml::MachineFunctionInfo
*WebAssemblyTargetMachine::convertFuncInfoToYAML(
661 const MachineFunction
&MF
) const {
662 const auto *MFI
= MF
.getInfo
<WebAssemblyFunctionInfo
>();
663 return new yaml::WebAssemblyFunctionInfo(MF
, *MFI
);
666 bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
667 const yaml::MachineFunctionInfo
&MFI
, PerFunctionMIParsingState
&PFS
,
668 SMDiagnostic
&Error
, SMRange
&SourceRange
) const {
669 const auto &YamlMFI
= static_cast<const yaml::WebAssemblyFunctionInfo
&>(MFI
);
670 MachineFunction
&MF
= PFS
.MF
;
671 MF
.getInfo
<WebAssemblyFunctionInfo
>()->initializeBaseYamlFields(MF
, YamlMFI
);