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 // Try to expand `vecreduce_{and, or}` into `{any, all}_true`.
521 addPass(P: createWebAssemblyReduceToAnyAllTrue(TM&: getWebAssemblyTargetMachine()));
522
523 TargetPassConfig::addIRPasses();
524}
525
526void WebAssemblyPassConfig::addISelPrepare() {
527 // We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
528 // loads and stores are promoted to local.gets/local.sets.
529 addPass(P: createWebAssemblyRefTypeMem2Local());
530 // Lower atomics and TLS if necessary
531 addPass(P: new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
532
533 // This is a no-op if atomics are not used in the module
534 addPass(P: createAtomicExpandLegacyPass());
535
536 TargetPassConfig::addISelPrepare();
537}
538
539bool WebAssemblyPassConfig::addInstSelector() {
540 (void)TargetPassConfig::addInstSelector();
541 addPass(
542 P: createWebAssemblyISelDag(TM&: getWebAssemblyTargetMachine(), OptLevel: getOptLevel()));
543 // Run the argument-move pass immediately after the ScheduleDAG scheduler
544 // so that we can fix up the ARGUMENT instructions before anything else
545 // sees them in the wrong place.
546 addPass(P: createWebAssemblyArgumentMove());
547 // Set the p2align operands. This information is present during ISel, however
548 // it's inconvenient to collect. Collect it now, and update the immediate
549 // operands.
550 addPass(P: createWebAssemblySetP2AlignOperands());
551
552 // Eliminate range checks and add default targets to br_table instructions.
553 addPass(P: createWebAssemblyFixBrTableDefaults());
554
555 // unreachable is terminator, non-terminator instruction after it is not
556 // allowed.
557 addPass(P: createWebAssemblyCleanCodeAfterTrap());
558
559 return false;
560}
561
562void WebAssemblyPassConfig::addOptimizedRegAlloc() {
563 // Currently RegisterCoalesce degrades wasm debug info quality by a
564 // significant margin. As a quick fix, disable this for -O1, which is often
565 // used for debugging large applications. Disabling this increases code size
566 // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is
567 // usually not used for production builds.
568 // TODO Investigate why RegisterCoalesce degrades debug info quality and fix
569 // it properly
570 if (getOptLevel() == CodeGenOptLevel::Less)
571 disablePass(PassID: &RegisterCoalescerID);
572 TargetPassConfig::addOptimizedRegAlloc();
573}
574
575void WebAssemblyPassConfig::addPostRegAlloc() {
576 // TODO: The following CodeGen passes don't currently support code containing
577 // virtual registers. Consider removing their restrictions and re-enabling
578 // them.
579
580 // These functions all require the NoVRegs property.
581 disablePass(PassID: &MachineLateInstrsCleanupID);
582 disablePass(PassID: &MachineCopyPropagationID);
583 disablePass(PassID: &PostRAMachineSinkingID);
584 disablePass(PassID: &PostRASchedulerID);
585 disablePass(PassID: &FuncletLayoutID);
586 disablePass(PassID: &StackMapLivenessID);
587 disablePass(PassID: &PatchableFunctionID);
588 disablePass(PassID: &ShrinkWrapID);
589 disablePass(PassID: &RemoveLoadsIntoFakeUsesID);
590
591 // This pass hurts code size for wasm because it can generate irreducible
592 // control flow.
593 disablePass(PassID: &MachineBlockPlacementID);
594
595 TargetPassConfig::addPostRegAlloc();
596}
597
598void WebAssemblyPassConfig::addPreEmitPass() {
599 TargetPassConfig::addPreEmitPass();
600
601 // Nullify DBG_VALUE_LISTs that we cannot handle.
602 addPass(P: createWebAssemblyNullifyDebugValueLists());
603
604 // Remove any unreachable blocks that may be left floating around.
605 // Rare, but possible. Needed for WebAssemblyFixIrreducibleControlFlow.
606 addPass(PassID: &UnreachableMachineBlockElimID);
607
608 // Eliminate multiple-entry loops.
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