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