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