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/Utils.h"
38#include <optional>
39using namespace llvm;
40
41#define DEBUG_TYPE "wasm"
42
43// A command-line option to keep implicit locals
44// for the purpose of testing with lit/llc ONLY.
45// This produces output which is not valid WebAssembly, and is not supported
46// by assemblers/disassemblers and other MC based tools.
47cl::opt<bool> WebAssembly::WasmDisableExplicitLocals(
48 "wasm-disable-explicit-locals", cl::Hidden,
49 cl::desc("WebAssembly: output implicit locals in"
50 " instruction output for test purposes only."),
51 cl::init(Val: false));
52
53// Exception handling & setjmp-longjmp handling related options.
54
55// Emscripten's asm.js-style setjmp/longjmp handling
56cl::opt<bool> WebAssembly::WasmEnableEmSjLj(
57 "enable-emscripten-sjlj",
58 cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
59 cl::init(Val: false));
60// setjmp/longjmp handling using wasm EH instructions
61cl::opt<bool> WebAssembly::WasmEnableSjLj(
62 "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling"));
63// If true, use the legacy Wasm EH proposal:
64// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/legacy/Exceptions.md
65// And if false, use the standardized Wasm EH proposal:
66// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md
67// Currently set to true by default because not all major web browsers turn on
68// the new standard proposal by default, but will later change to false.
69cl::opt<bool> WebAssembly::WasmUseLegacyEH(
70 "wasm-use-legacy-eh", cl::desc("WebAssembly exception handling (legacy)"),
71 cl::init(Val: true));
72
73extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
74LLVMInitializeWebAssemblyTarget() {
75 // Register the target.
76 RegisterTargetMachine<WebAssemblyTargetMachine> X(
77 getTheWebAssemblyTarget32());
78 RegisterTargetMachine<WebAssemblyTargetMachine> Y(
79 getTheWebAssemblyTarget64());
80
81 // Register backend passes
82 auto &PR = *PassRegistry::getPassRegistry();
83 initializeGlobalISel(PR);
84 initializeWebAssemblyPreLegalizerCombinerLegacyPass(PR);
85 initializeWebAssemblyPostLegalizerCombinerLegacyPass(PR);
86 initializeWebAssemblyAddMissingPrototypesLegacyPass(PR);
87 initializeWebAssemblyLowerEmscriptenEHSjLjLegacyPass(PR);
88 initializeLowerGlobalDtorsLegacyPassPass(PR);
89 initializeWebAssemblyFixFunctionBitcastsLegacyPass(PR);
90 initializeWebAssemblyOptimizeReturnedLegacyPass(PR);
91 initializeWebAssemblyRefTypeMem2LocalLegacyPass(PR);
92 initializeWebAssemblyArgumentMoveLegacyPass(PR);
93 initializeWebAssemblyAsmPrinterPass(PR);
94 initializeWebAssemblySetP2AlignOperandsLegacyPass(PR);
95 initializeWebAssemblyReplacePhysRegsLegacyPass(PR);
96 initializeWebAssemblyOptimizeLiveIntervalsLegacyPass(PR);
97 initializeWebAssemblyMemIntrinsicResultsLegacyPass(PR);
98 initializeWebAssemblyRegStackifyLegacyPass(PR);
99 initializeWebAssemblyRegColoringLegacyPass(PR);
100 initializeWebAssemblyNullifyDebugValueListsLegacyPass(PR);
101 initializeWebAssemblyFixIrreducibleControlFlowLegacyPass(PR);
102 initializeWebAssemblyLateEHPrepareLegacyPass(PR);
103 initializeWebAssemblyExceptionInfoWrapperPassPass(PR);
104 initializeWebAssemblyCFGSortLegacyPass(PR);
105 initializeWebAssemblyCFGStackifyLegacyPass(PR);
106 initializeWebAssemblyExplicitLocalsLegacyPass(PR);
107 initializeWebAssemblyLowerBrUnlessLegacyPass(PR);
108 initializeWebAssemblyRegNumberingLegacyPass(PR);
109 initializeWebAssemblyDebugFixupLegacyPass(PR);
110 initializeWebAssemblyPeepholeLegacyPass(PR);
111 initializeWebAssemblyMCLowerPreLegacyPass(PR);
112 initializeWebAssemblyFixBrTableDefaultsLegacyPass(PR);
113 initializeWebAssemblyDAGToDAGISelLegacyPass(PR);
114}
115
116//===----------------------------------------------------------------------===//
117// WebAssembly Lowering public interface.
118//===----------------------------------------------------------------------===//
119
120static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
121 // Default to static relocation model. This should always be more optimal
122 // than PIC since the static linker can determine all global addresses and
123 // assume direct function calls.
124 return RM.value_or(u: Reloc::Static);
125}
126
127using WebAssembly::WasmDisableExplicitLocals;
128using WebAssembly::WasmEnableEmSjLj;
129using WebAssembly::WasmEnableSjLj;
130
131static void basicCheckForEHAndSjLj() {
132 // You can't enable two modes of SjLj at the same time
133 if (WasmEnableEmSjLj && WasmEnableSjLj)
134 reportFatalUsageError(
135 reason: "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
136}
137
138/// Create an WebAssembly architecture model.
139///
140WebAssemblyTargetMachine::WebAssemblyTargetMachine(
141 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
142 const TargetOptions &Options, std::optional<Reloc::Model> RM,
143 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
144 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
145 getEffectiveRelocModel(RM),
146 getEffectiveCodeModel(CM, Default: CodeModel::Large), OL),
147 TLOF(new WebAssemblyTargetObjectFile()) {
148 // WebAssembly type-checks instructions, but a noreturn function with a return
149 // type that doesn't match the context will cause a check failure. So we lower
150 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
151 // 'unreachable' instructions which is meant for that case. Formerly, we also
152 // needed to add checks to SP failure emission in the instruction selection
153 // backends, but this has since been tied to TrapUnreachable and is no longer
154 // necessary.
155 this->Options.TrapUnreachable = true;
156 this->Options.NoTrapAfterNoreturn = false;
157
158 // WebAssembly treats each function as an independent unit. Force
159 // -ffunction-sections, effectively, so that we can emit them independently.
160 this->Options.FunctionSections = true;
161 this->Options.DataSections = true;
162 this->Options.UniqueSectionNames = true;
163
164 basicCheckForEHAndSjLj();
165 initAsmInfo();
166
167 // WebAssembly does not run RegAllocFast; tied operands must be lowered by
168 // TwoAddressInstructionPass.
169 setEnableTiedFastRegAlloc(false);
170
171 LLT::setUseExtended(true);
172
173 // Note that we don't use setRequiresStructuredCFG(true). It disables
174 // optimizations than we're ok with, and want, such as critical edge
175 // splitting and tail merging.
176}
177
178WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
179
180const WebAssemblySubtarget *
181WebAssemblyTargetMachine::getSubtargetImpl(StringRef CPU, StringRef FS,
182 StringRef ABIName) const {
183 auto &I = SubtargetMap[CPU.str() + FS.str() + ABIName.str()];
184 if (!I) {
185 I = std::make_unique<WebAssemblySubtarget>(args: TargetTriple, args&: CPU, args&: FS, args: *this,
186 args&: ABIName);
187 }
188 return I.get();
189}
190
191const WebAssemblySubtarget *
192WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
193 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
194 const Module *M = F.getParent();
195 if (const WebAssemblySubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M))
196 return ST;
197
198 SubtargetAttrs Attrs;
199 Attrs.CPU = FnAttrs.getAttribute(Kind: "target-cpu");
200 Attrs.FS = FnAttrs.getAttribute(Kind: "target-features");
201 Attrs.ABIName = getTargetABIName(M: *M);
202
203 if (const WebAssemblySubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs))
204 return ST;
205
206 StringRef CPU =
207 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString() : TargetCPU;
208 StringRef FS = Attrs.FS.isValid() ? Attrs.FS.getValueAsString() : TargetFS;
209
210 const WebAssemblySubtarget *ST = getSubtargetImpl(CPU, FS, ABIName: Attrs.ABIName);
211 LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: ST);
212 return ST;
213}
214
215namespace {
216
217/// WebAssembly Code Generator Pass Configuration Options.
218class WebAssemblyPassConfig final : public TargetPassConfig {
219public:
220 WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
221 : TargetPassConfig(TM, PM) {}
222
223 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
224 return getTM<WebAssemblyTargetMachine>();
225 }
226
227 FunctionPass *createTargetRegisterAllocator(bool) override;
228
229 void addIRPasses() override;
230 void addISelPrepare() override;
231 bool addInstSelector() override;
232 void addOptimizedRegAlloc() override;
233 void addPostRegAlloc() override;
234 bool addGCPasses() override { return false; }
235 void addPreEmitPass() override;
236 bool addPreISel() override;
237
238 // No reg alloc
239 bool addRegAssignAndRewriteFast() override { return false; }
240
241 // No reg alloc
242 bool addRegAssignAndRewriteOptimized() override { return false; }
243
244 bool addIRTranslator() override;
245 void addPreLegalizeMachineIR() override;
246 bool addLegalizeMachineIR() override;
247 void addPreRegBankSelect() override;
248 bool addRegBankSelect() override;
249 bool addGlobalInstructionSelect() override;
250};
251} // end anonymous namespace
252
253MachineFunctionInfo *WebAssemblyTargetMachine::createMachineFunctionInfo(
254 BumpPtrAllocator &Allocator, const Function &F,
255 const TargetSubtargetInfo *STI) const {
256 return WebAssemblyFunctionInfo::create<WebAssemblyFunctionInfo>(Allocator, F,
257 STI);
258}
259
260TargetTransformInfo
261WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) const {
262 return TargetTransformInfo(std::make_unique<WebAssemblyTTIImpl>(args: this, args: F));
263}
264
265TargetPassConfig *
266WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
267 return new WebAssemblyPassConfig(*this, PM);
268}
269
270FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
271 return nullptr; // No reg alloc
272}
273
274//===----------------------------------------------------------------------===//
275// The following functions are called from lib/CodeGen/Passes.cpp to modify
276// the CodeGen pass sequence.
277//===----------------------------------------------------------------------===//
278
279void WebAssemblyPassConfig::addIRPasses() {
280 // Add signatures to prototype-less function declarations
281 addPass(P: createWebAssemblyAddMissingPrototypesLegacyPass());
282
283 // Lower .llvm.global_dtors into .llvm.global_ctors with __cxa_atexit calls.
284 addPass(P: createLowerGlobalDtorsLegacyPass());
285
286 // Fix function bitcasts, as WebAssembly requires caller and callee signatures
287 // to match.
288 addPass(P: createWebAssemblyFixFunctionBitcastsLegacyPass());
289
290 // Optimize "returned" function attributes.
291 if (getOptLevel() != CodeGenOptLevel::None)
292 addPass(P: createWebAssemblyOptimizeReturnedLegacyPass());
293
294 // Wasm SjLj shares Emscripten SjLj's transformation, so it is handled here.
295 // Without Wasm or Emscripten EH, this also lowers invokes.
296 addPass(P: createWebAssemblyLowerEmscriptenEHSjLjLegacyPass());
297
298 // Expand indirectbr instructions to switches.
299 addPass(P: createIndirectBrExpandPass(OptLevel: getOptLevel()));
300
301 // Try to expand `vecreduce_{and, or}` into `{any, all}_true`.
302 addPass(P: createWebAssemblyReduceToAnyAllTrueLegacyPass(
303 TM&: getWebAssemblyTargetMachine()));
304
305 TargetPassConfig::addIRPasses();
306}
307
308void WebAssemblyPassConfig::addISelPrepare() {
309 // We need to move reference type allocas to WASM_ADDRESS_SPACE_VAR so that
310 // loads and stores are promoted to local.gets/local.sets.
311 addPass(P: createWebAssemblyRefTypeMem2LocalLegacyPass());
312 // Lower atomics and TLS if necessary
313 addPass(P: createWebAssemblyCoalesceFeaturesAndStripAtomicsLegacyPass(
314 TM&: getWebAssemblyTargetMachine()));
315
316 // This is a no-op if atomics are not used in the module
317 addPass(P: createAtomicExpandLegacyPass());
318
319 TargetPassConfig::addISelPrepare();
320}
321
322bool WebAssemblyPassConfig::addInstSelector() {
323 (void)TargetPassConfig::addInstSelector();
324 addPass(P: createWebAssemblyISelDagLegacyPass(TM&: getWebAssemblyTargetMachine(),
325 OptLevel: getOptLevel()));
326 // Run the argument-move pass immediately after the ScheduleDAG scheduler
327 // so that we can fix up the ARGUMENT instructions before anything else
328 // sees them in the wrong place.
329 addPass(P: createWebAssemblyArgumentMoveLegacyPass());
330 // Set the p2align operands. This information is present during ISel, however
331 // it's inconvenient to collect. Collect it now, and update the immediate
332 // operands.
333 addPass(P: createWebAssemblySetP2AlignOperandsLegacyPass());
334
335 // Eliminate range checks and add default targets to br_table instructions.
336 addPass(P: createWebAssemblyFixBrTableDefaultsLegacyPass());
337
338 // unreachable is terminator, non-terminator instruction after it is not
339 // allowed.
340 addPass(P: createWebAssemblyCleanCodeAfterTrapLegacyPass());
341
342 return false;
343}
344
345void WebAssemblyPassConfig::addOptimizedRegAlloc() {
346 // Currently RegisterCoalesce degrades wasm debug info quality by a
347 // significant margin. As a quick fix, disable this for -O1, which is often
348 // used for debugging large applications. Disabling this increases code size
349 // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is
350 // usually not used for production builds.
351 // TODO Investigate why RegisterCoalesce degrades debug info quality and fix
352 // it properly
353 if (getOptLevel() == CodeGenOptLevel::Less)
354 disablePass(PassID: &RegisterCoalescerID);
355 TargetPassConfig::addOptimizedRegAlloc();
356}
357
358void WebAssemblyPassConfig::addPostRegAlloc() {
359 // TODO: The following CodeGen passes don't currently support code containing
360 // virtual registers. Consider removing their restrictions and re-enabling
361 // them.
362
363 // These functions all require the NoVRegs property.
364 disablePass(PassID: &MachineLateInstrsCleanupID);
365 disablePass(PassID: &MachineCopyPropagationID);
366 disablePass(PassID: &PostRAMachineSinkingID);
367 disablePass(PassID: &PostRASchedulerID);
368 disablePass(PassID: &FuncletLayoutID);
369 disablePass(PassID: &StackMapLivenessID);
370 disablePass(PassID: &PatchableFunctionID);
371 disablePass(PassID: &ShrinkWrapID);
372 disablePass(PassID: &RemoveLoadsIntoFakeUsesID);
373
374 // This pass hurts code size for wasm because it can generate irreducible
375 // control flow.
376 disablePass(PassID: &MachineBlockPlacementID);
377
378 TargetPassConfig::addPostRegAlloc();
379}
380
381void WebAssemblyPassConfig::addPreEmitPass() {
382 TargetPassConfig::addPreEmitPass();
383
384 // Nullify DBG_VALUE_LISTs that we cannot handle.
385 addPass(P: createWebAssemblyNullifyDebugValueListsLegacyPass());
386
387 // Remove any unreachable blocks that may be left floating around.
388 // Rare, but possible. Needed for WebAssemblyFixIrreducibleControlFlow.
389 addPass(PassID: &UnreachableMachineBlockElimID);
390
391 // Eliminate multiple-entry loops.
392 addPass(P: createWebAssemblyFixIrreducibleControlFlowLegacyPass());
393
394 // Do various transformations for exception handling.
395 // Every CFG-changing optimizations should come before this.
396 addPass(P: createWebAssemblyLateEHPrepareLegacyPass());
397
398 // Now that we have a prologue and epilogue and all frame indices are
399 // rewritten, eliminate SP and FP. This allows them to be stackified,
400 // colored, and numbered with the rest of the registers.
401 addPass(P: createWebAssemblyReplacePhysRegsLegacyPass());
402
403 // Preparations and optimizations related to register stackification.
404 if (getOptLevel() != CodeGenOptLevel::None) {
405 // Depend on LiveIntervals and perform some optimizations on it.
406 addPass(P: createWebAssemblyOptimizeLiveIntervalsLegacyPass());
407
408 // Prepare memory intrinsic calls for register stackifying.
409 addPass(P: createWebAssemblyMemIntrinsicResultsLegacyPass());
410 }
411
412 // Mark registers as representing wasm's value stack. This is a key
413 // code-compression technique in WebAssembly. We run this pass (and
414 // MemIntrinsicResults above) very late, so that it sees as much code as
415 // possible, including code emitted by PEI and expanded by late tail
416 // duplication.
417 addPass(P: createWebAssemblyRegStackifyLegacyPass(OptLevel: getOptLevel()));
418
419 if (getOptLevel() != CodeGenOptLevel::None) {
420 // Run the register coloring pass to reduce the total number of registers.
421 // This runs after stackification so that it doesn't consider registers
422 // that become stackified.
423 addPass(P: createWebAssemblyRegColoringLegacyPass());
424 }
425
426 // Sort the blocks of the CFG into topological order, a prerequisite for
427 // BLOCK and LOOP markers.
428 addPass(P: createWebAssemblyCFGSortLegacyPass());
429
430 // Insert BLOCK and LOOP markers.
431 addPass(P: createWebAssemblyCFGStackifyLegacyPass());
432
433 // Insert explicit local.get and local.set operators.
434 if (!WasmDisableExplicitLocals)
435 addPass(P: createWebAssemblyExplicitLocalsLegacyPass());
436
437 // Lower br_unless into br_if.
438 addPass(P: createWebAssemblyLowerBrUnlessLegacyPass());
439
440 // Perform the very last peephole optimizations on the code.
441 if (getOptLevel() != CodeGenOptLevel::None)
442 addPass(P: createWebAssemblyPeepholeLegacyPass());
443
444 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
445 addPass(P: createWebAssemblyRegNumberingLegacyPass());
446
447 // Fix debug_values whose defs have been stackified.
448 if (!WasmDisableExplicitLocals)
449 addPass(P: createWebAssemblyDebugFixupLegacyPass());
450
451 // Collect information to prepare for MC lowering / asm printing.
452 addPass(P: createWebAssemblyMCLowerPreLegacyPass());
453}
454
455bool WebAssemblyPassConfig::addPreISel() {
456 TargetPassConfig::addPreISel();
457 return false;
458}
459
460bool WebAssemblyPassConfig::addIRTranslator() {
461 addPass(P: new IRTranslatorLegacy());
462 return false;
463}
464
465void WebAssemblyPassConfig::addPreLegalizeMachineIR() {
466 if (getOptLevel() != CodeGenOptLevel::None) {
467 addPass(P: createWebAssemblyPreLegalizerCombinerLegacyPass());
468 }
469}
470bool WebAssemblyPassConfig::addLegalizeMachineIR() {
471 addPass(P: new LegalizerLegacy());
472 return false;
473}
474
475void WebAssemblyPassConfig::addPreRegBankSelect() {
476 if (getOptLevel() != CodeGenOptLevel::None) {
477 addPass(P: createWebAssemblyPostLegalizerCombinerLegacyPass());
478 }
479}
480
481bool WebAssemblyPassConfig::addRegBankSelect() {
482 addPass(P: new RegBankSelectLegacy());
483 return false;
484}
485
486bool WebAssemblyPassConfig::addGlobalInstructionSelect() {
487 addPass(P: new InstructionSelectLegacy(getOptLevel()));
488
489 // We insert only if ISelDAG won't insert these at a later point.
490 if (isGlobalISelAbortEnabled()) {
491 addPass(P: createWebAssemblyArgumentMoveLegacyPass());
492 addPass(P: createWebAssemblySetP2AlignOperandsLegacyPass());
493 addPass(P: createWebAssemblyFixBrTableDefaultsLegacyPass());
494 addPass(P: createWebAssemblyCleanCodeAfterTrapLegacyPass());
495 }
496
497 return false;
498}
499
500yaml::MachineFunctionInfo *
501WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
502 return new yaml::WebAssemblyFunctionInfo();
503}
504
505yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
506 const MachineFunction &MF) const {
507 const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
508 return new yaml::WebAssemblyFunctionInfo(MF, *MFI);
509}
510
511bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
512 const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
513 SMDiagnostic &Error, SMRange &SourceRange) const {
514 const auto &YamlMFI = static_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
515 MachineFunction &MF = PFS.MF;
516 MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(MF, YamlMFI);
517 return false;
518}
519