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 "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyTargetObjectFile.h"
20 #include "WebAssemblyTargetTransformInfo.h"
21 #include "llvm/CodeGen/MIRParser/MIParser.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/RegAllocRegistry.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Scalar/LowerAtomic.h"
31 #include "llvm/Transforms/Utils.h"
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "wasm"
35 
36 // Emscripten's asm.js-style exception handling
37 static cl::opt<bool> EnableEmException(
38     "enable-emscripten-cxx-exceptions",
39     cl::desc("WebAssembly Emscripten-style exception handling"),
40     cl::init(false));
41 
42 // Emscripten's asm.js-style setjmp/longjmp handling
43 static cl::opt<bool> EnableEmSjLj(
44     "enable-emscripten-sjlj",
45     cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
46     cl::init(false));
47 
48 // A command-line option to keep implicit locals
49 // for the purpose of testing with lit/llc ONLY.
50 // This produces output which is not valid WebAssembly, and is not supported
51 // by assemblers/disassemblers and other MC based tools.
52 static cl::opt<bool> WasmDisableExplicitLocals(
53     "wasm-disable-explicit-locals", cl::Hidden,
54     cl::desc("WebAssembly: output implicit locals in"
55              " instruction output for test purposes only."),
56     cl::init(false));
57 
LLVMInitializeWebAssemblyTarget()58 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() {
59   // Register the target.
60   RegisterTargetMachine<WebAssemblyTargetMachine> X(
61       getTheWebAssemblyTarget32());
62   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
63       getTheWebAssemblyTarget64());
64 
65   // Register backend passes
66   auto &PR = *PassRegistry::getPassRegistry();
67   initializeWebAssemblyAddMissingPrototypesPass(PR);
68   initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
69   initializeLowerGlobalDtorsPass(PR);
70   initializeFixFunctionBitcastsPass(PR);
71   initializeOptimizeReturnedPass(PR);
72   initializeWebAssemblyArgumentMovePass(PR);
73   initializeWebAssemblySetP2AlignOperandsPass(PR);
74   initializeWebAssemblyReplacePhysRegsPass(PR);
75   initializeWebAssemblyPrepareForLiveIntervalsPass(PR);
76   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
77   initializeWebAssemblyMemIntrinsicResultsPass(PR);
78   initializeWebAssemblyRegStackifyPass(PR);
79   initializeWebAssemblyRegColoringPass(PR);
80   initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
81   initializeWebAssemblyLateEHPreparePass(PR);
82   initializeWebAssemblyExceptionInfoPass(PR);
83   initializeWebAssemblyCFGSortPass(PR);
84   initializeWebAssemblyCFGStackifyPass(PR);
85   initializeWebAssemblyExplicitLocalsPass(PR);
86   initializeWebAssemblyLowerBrUnlessPass(PR);
87   initializeWebAssemblyRegNumberingPass(PR);
88   initializeWebAssemblyDebugFixupPass(PR);
89   initializeWebAssemblyPeepholePass(PR);
90 }
91 
92 //===----------------------------------------------------------------------===//
93 // WebAssembly Lowering public interface.
94 //===----------------------------------------------------------------------===//
95 
getEffectiveRelocModel(Optional<Reloc::Model> RM,const Triple & TT)96 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM,
97                                            const Triple &TT) {
98   if (!RM.hasValue()) {
99     // Default to static relocation model.  This should always be more optimial
100     // than PIC since the static linker can determine all global addresses and
101     // assume direct function calls.
102     return Reloc::Static;
103   }
104 
105   if (!TT.isOSEmscripten()) {
106     // Relocation modes other than static are currently implemented in a way
107     // that only works for Emscripten, so disable them if we aren't targeting
108     // Emscripten.
109     return Reloc::Static;
110   }
111 
112   return *RM;
113 }
114 
115 /// Create an WebAssembly architecture model.
116 ///
WebAssemblyTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)117 WebAssemblyTargetMachine::WebAssemblyTargetMachine(
118     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
119     const TargetOptions &Options, Optional<Reloc::Model> RM,
120     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
121     : LLVMTargetMachine(T,
122                         TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
123                                          : "e-m:e-p:32:32-i64:64-n32:64-S128",
124                         TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
125                         getEffectiveCodeModel(CM, CodeModel::Large), OL),
126       TLOF(new WebAssemblyTargetObjectFile()) {
127   // WebAssembly type-checks instructions, but a noreturn function with a return
128   // type that doesn't match the context will cause a check failure. So we lower
129   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
130   // 'unreachable' instructions which is meant for that case.
131   this->Options.TrapUnreachable = true;
132 
133   // WebAssembly treats each function as an independent unit. Force
134   // -ffunction-sections, effectively, so that we can emit them independently.
135   this->Options.FunctionSections = true;
136   this->Options.DataSections = true;
137   this->Options.UniqueSectionNames = true;
138 
139   initAsmInfo();
140 
141   // Note that we don't use setRequiresStructuredCFG(true). It disables
142   // optimizations than we're ok with, and want, such as critical edge
143   // splitting and tail merging.
144 }
145 
146 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
147 
148 const WebAssemblySubtarget *
getSubtargetImpl(std::string CPU,std::string FS) const149 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
150                                            std::string FS) const {
151   auto &I = SubtargetMap[CPU + FS];
152   if (!I) {
153     I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
154   }
155   return I.get();
156 }
157 
158 const WebAssemblySubtarget *
getSubtargetImpl(const Function & F) const159 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
160   Attribute CPUAttr = F.getFnAttribute("target-cpu");
161   Attribute FSAttr = F.getFnAttribute("target-features");
162 
163   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
164                         ? CPUAttr.getValueAsString().str()
165                         : TargetCPU;
166   std::string FS = !FSAttr.hasAttribute(Attribute::None)
167                        ? FSAttr.getValueAsString().str()
168                        : TargetFS;
169 
170   // This needs to be done before we create a new subtarget since any
171   // creation will depend on the TM and the code generation flags on the
172   // function that reside in TargetOptions.
173   resetTargetOptions(F);
174 
175   return getSubtargetImpl(CPU, FS);
176 }
177 
178 namespace {
179 
180 class CoalesceFeaturesAndStripAtomics final : public ModulePass {
181   // Take the union of all features used in the module and use it for each
182   // function individually, since having multiple feature sets in one module
183   // currently does not make sense for WebAssembly. If atomics are not enabled,
184   // also strip atomic operations and thread local storage.
185   static char ID;
186   WebAssemblyTargetMachine *WasmTM;
187 
188 public:
CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine * WasmTM)189   CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM)
190       : ModulePass(ID), WasmTM(WasmTM) {}
191 
runOnModule(Module & M)192   bool runOnModule(Module &M) override {
193     FeatureBitset Features = coalesceFeatures(M);
194 
195     std::string FeatureStr = getFeatureString(Features);
196     for (auto &F : M)
197       replaceFeatures(F, FeatureStr);
198 
199     bool StrippedAtomics = false;
200     bool StrippedTLS = false;
201 
202     if (!Features[WebAssembly::FeatureAtomics])
203       StrippedAtomics = stripAtomics(M);
204 
205     if (!Features[WebAssembly::FeatureBulkMemory])
206       StrippedTLS = stripThreadLocals(M);
207 
208     if (StrippedAtomics && !StrippedTLS)
209       stripThreadLocals(M);
210     else if (StrippedTLS && !StrippedAtomics)
211       stripAtomics(M);
212 
213     recordFeatures(M, Features, StrippedAtomics || StrippedTLS);
214 
215     // Conservatively assume we have made some change
216     return true;
217   }
218 
219 private:
coalesceFeatures(const Module & M)220   FeatureBitset coalesceFeatures(const Module &M) {
221     FeatureBitset Features =
222         WasmTM
223             ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
224                                std::string(WasmTM->getTargetFeatureString()))
225             ->getFeatureBits();
226     for (auto &F : M)
227       Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
228     return Features;
229   }
230 
getFeatureString(const FeatureBitset & Features)231   std::string getFeatureString(const FeatureBitset &Features) {
232     std::string Ret;
233     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
234       if (Features[KV.Value])
235         Ret += (StringRef("+") + KV.Key + ",").str();
236     }
237     return Ret;
238   }
239 
replaceFeatures(Function & F,const std::string & Features)240   void replaceFeatures(Function &F, const std::string &Features) {
241     F.removeFnAttr("target-features");
242     F.removeFnAttr("target-cpu");
243     F.addFnAttr("target-features", Features);
244   }
245 
stripAtomics(Module & M)246   bool stripAtomics(Module &M) {
247     // Detect whether any atomics will be lowered, since there is no way to tell
248     // whether the LowerAtomic pass lowers e.g. stores.
249     bool Stripped = false;
250     for (auto &F : M) {
251       for (auto &B : F) {
252         for (auto &I : B) {
253           if (I.isAtomic()) {
254             Stripped = true;
255             goto done;
256           }
257         }
258       }
259     }
260 
261   done:
262     if (!Stripped)
263       return false;
264 
265     LowerAtomicPass Lowerer;
266     FunctionAnalysisManager FAM;
267     for (auto &F : M)
268       Lowerer.run(F, FAM);
269 
270     return true;
271   }
272 
stripThreadLocals(Module & M)273   bool stripThreadLocals(Module &M) {
274     bool Stripped = false;
275     for (auto &GV : M.globals()) {
276       if (GV.getThreadLocalMode() !=
277           GlobalValue::ThreadLocalMode::NotThreadLocal) {
278         Stripped = true;
279         GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
280       }
281     }
282     return Stripped;
283   }
284 
recordFeatures(Module & M,const FeatureBitset & Features,bool Stripped)285   void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) {
286     for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
287       if (Features[KV.Value]) {
288         // Mark features as used
289         std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
290         M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey,
291                         wasm::WASM_FEATURE_PREFIX_USED);
292       }
293     }
294     // Code compiled without atomics or bulk-memory may have had its atomics or
295     // thread-local data lowered to nonatomic operations or non-thread-local
296     // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed
297     // to tell the linker that it would be unsafe to allow this code ot be used
298     // in a module with shared memory.
299     if (Stripped) {
300       M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem",
301                       wasm::WASM_FEATURE_PREFIX_DISALLOWED);
302     }
303   }
304 };
305 char CoalesceFeaturesAndStripAtomics::ID = 0;
306 
307 /// WebAssembly Code Generator Pass Configuration Options.
308 class WebAssemblyPassConfig final : public TargetPassConfig {
309 public:
WebAssemblyPassConfig(WebAssemblyTargetMachine & TM,PassManagerBase & PM)310   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
311       : TargetPassConfig(TM, PM) {}
312 
getWebAssemblyTargetMachine() const313   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
314     return getTM<WebAssemblyTargetMachine>();
315   }
316 
317   FunctionPass *createTargetRegisterAllocator(bool) override;
318 
319   void addIRPasses() override;
320   bool addInstSelector() override;
321   void addPostRegAlloc() override;
addGCPasses()322   bool addGCPasses() override { return false; }
323   void addPreEmitPass() override;
324 
325   // No reg alloc
addRegAssignmentFast()326   bool addRegAssignmentFast() override { return false; }
327 
328   // No reg alloc
addRegAssignmentOptimized()329   bool addRegAssignmentOptimized() override { return false; }
330 };
331 } // end anonymous namespace
332 
333 TargetTransformInfo
getTargetTransformInfo(const Function & F)334 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
335   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
336 }
337 
338 TargetPassConfig *
createPassConfig(PassManagerBase & PM)339 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
340   return new WebAssemblyPassConfig(*this, PM);
341 }
342 
createTargetRegisterAllocator(bool)343 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
344   return nullptr; // No reg alloc
345 }
346 
347 //===----------------------------------------------------------------------===//
348 // The following functions are called from lib/CodeGen/Passes.cpp to modify
349 // the CodeGen pass sequence.
350 //===----------------------------------------------------------------------===//
351 
addIRPasses()352 void WebAssemblyPassConfig::addIRPasses() {
353   // Runs LowerAtomicPass if necessary
354   addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine()));
355 
356   // This is a no-op if atomics are not used in the module
357   addPass(createAtomicExpandPass());
358 
359   // Add signatures to prototype-less function declarations
360   addPass(createWebAssemblyAddMissingPrototypes());
361 
362   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
363   addPass(createWebAssemblyLowerGlobalDtors());
364 
365   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
366   // to match.
367   addPass(createWebAssemblyFixFunctionBitcasts());
368 
369   // Optimize "returned" function attributes.
370   if (getOptLevel() != CodeGenOpt::None)
371     addPass(createWebAssemblyOptimizeReturned());
372 
373   // If exception handling is not enabled and setjmp/longjmp handling is
374   // enabled, we lower invokes into calls and delete unreachable landingpad
375   // blocks. Lowering invokes when there is no EH support is done in
376   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
377   // function and SjLj handling expects all invokes to be lowered before.
378   if (!EnableEmException &&
379       TM->Options.ExceptionModel == ExceptionHandling::None) {
380     addPass(createLowerInvokePass());
381     // The lower invoke pass may create unreachable code. Remove it in order not
382     // to process dead blocks in setjmp/longjmp handling.
383     addPass(createUnreachableBlockEliminationPass());
384   }
385 
386   // Handle exceptions and setjmp/longjmp if enabled.
387   if (EnableEmException || EnableEmSjLj)
388     addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
389                                                    EnableEmSjLj));
390 
391   // Expand indirectbr instructions to switches.
392   addPass(createIndirectBrExpandPass());
393 
394   TargetPassConfig::addIRPasses();
395 }
396 
addInstSelector()397 bool WebAssemblyPassConfig::addInstSelector() {
398   (void)TargetPassConfig::addInstSelector();
399   addPass(
400       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
401   // Run the argument-move pass immediately after the ScheduleDAG scheduler
402   // so that we can fix up the ARGUMENT instructions before anything else
403   // sees them in the wrong place.
404   addPass(createWebAssemblyArgumentMove());
405   // Set the p2align operands. This information is present during ISel, however
406   // it's inconvenient to collect. Collect it now, and update the immediate
407   // operands.
408   addPass(createWebAssemblySetP2AlignOperands());
409 
410   // Eliminate range checks and add default targets to br_table instructions.
411   addPass(createWebAssemblyFixBrTableDefaults());
412 
413   return false;
414 }
415 
addPostRegAlloc()416 void WebAssemblyPassConfig::addPostRegAlloc() {
417   // TODO: The following CodeGen passes don't currently support code containing
418   // virtual registers. Consider removing their restrictions and re-enabling
419   // them.
420 
421   // These functions all require the NoVRegs property.
422   disablePass(&MachineCopyPropagationID);
423   disablePass(&PostRAMachineSinkingID);
424   disablePass(&PostRASchedulerID);
425   disablePass(&FuncletLayoutID);
426   disablePass(&StackMapLivenessID);
427   disablePass(&LiveDebugValuesID);
428   disablePass(&PatchableFunctionID);
429   disablePass(&ShrinkWrapID);
430 
431   // This pass hurts code size for wasm because it can generate irreducible
432   // control flow.
433   disablePass(&MachineBlockPlacementID);
434 
435   TargetPassConfig::addPostRegAlloc();
436 }
437 
addPreEmitPass()438 void WebAssemblyPassConfig::addPreEmitPass() {
439   TargetPassConfig::addPreEmitPass();
440 
441   // Eliminate multiple-entry loops.
442   addPass(createWebAssemblyFixIrreducibleControlFlow());
443 
444   // Do various transformations for exception handling.
445   // Every CFG-changing optimizations should come before this.
446   addPass(createWebAssemblyLateEHPrepare());
447 
448   // Now that we have a prologue and epilogue and all frame indices are
449   // rewritten, eliminate SP and FP. This allows them to be stackified,
450   // colored, and numbered with the rest of the registers.
451   addPass(createWebAssemblyReplacePhysRegs());
452 
453   // Preparations and optimizations related to register stackification.
454   if (getOptLevel() != CodeGenOpt::None) {
455     // LiveIntervals isn't commonly run this late. Re-establish preconditions.
456     addPass(createWebAssemblyPrepareForLiveIntervals());
457 
458     // Depend on LiveIntervals and perform some optimizations on it.
459     addPass(createWebAssemblyOptimizeLiveIntervals());
460 
461     // Prepare memory intrinsic calls for register stackifying.
462     addPass(createWebAssemblyMemIntrinsicResults());
463 
464     // Mark registers as representing wasm's value stack. This is a key
465     // code-compression technique in WebAssembly. We run this pass (and
466     // MemIntrinsicResults above) very late, so that it sees as much code as
467     // possible, including code emitted by PEI and expanded by late tail
468     // duplication.
469     addPass(createWebAssemblyRegStackify());
470 
471     // Run the register coloring pass to reduce the total number of registers.
472     // This runs after stackification so that it doesn't consider registers
473     // that become stackified.
474     addPass(createWebAssemblyRegColoring());
475   }
476 
477   // Sort the blocks of the CFG into topological order, a prerequisite for
478   // BLOCK and LOOP markers.
479   addPass(createWebAssemblyCFGSort());
480 
481   // Insert BLOCK and LOOP markers.
482   addPass(createWebAssemblyCFGStackify());
483 
484   // Insert explicit local.get and local.set operators.
485   if (!WasmDisableExplicitLocals)
486     addPass(createWebAssemblyExplicitLocals());
487 
488   // Lower br_unless into br_if.
489   addPass(createWebAssemblyLowerBrUnless());
490 
491   // Perform the very last peephole optimizations on the code.
492   if (getOptLevel() != CodeGenOpt::None)
493     addPass(createWebAssemblyPeephole());
494 
495   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
496   addPass(createWebAssemblyRegNumbering());
497 
498   // Fix debug_values whose defs have been stackified.
499   if (!WasmDisableExplicitLocals)
500     addPass(createWebAssemblyDebugFixup());
501 }
502 
503 yaml::MachineFunctionInfo *
createDefaultFuncInfoYAML() const504 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
505   return new yaml::WebAssemblyFunctionInfo();
506 }
507 
convertFuncInfoToYAML(const MachineFunction & MF) const508 yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML(
509     const MachineFunction &MF) const {
510   const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>();
511   return new yaml::WebAssemblyFunctionInfo(*MFI);
512 }
513 
parseMachineFunctionInfo(const yaml::MachineFunctionInfo & MFI,PerFunctionMIParsingState & PFS,SMDiagnostic & Error,SMRange & SourceRange) const514 bool WebAssemblyTargetMachine::parseMachineFunctionInfo(
515     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
516     SMDiagnostic &Error, SMRange &SourceRange) const {
517   const auto &YamlMFI =
518       reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI);
519   MachineFunction &MF = PFS.MF;
520   MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
521   return false;
522 }
523