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 "WebAssembly.h"
17 #include "WebAssemblyTargetObjectFile.h"
18 #include "WebAssemblyTargetTransformInfo.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/RegAllocRegistry.h"
22 #include "llvm/CodeGen/TargetPassConfig.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Utils.h"
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "wasm"
31 
32 // Emscripten's asm.js-style exception handling
33 static cl::opt<bool> EnableEmException(
34     "enable-emscripten-cxx-exceptions",
35     cl::desc("WebAssembly Emscripten-style exception handling"),
36     cl::init(false));
37 
38 // Emscripten's asm.js-style setjmp/longjmp handling
39 static cl::opt<bool> EnableEmSjLj(
40     "enable-emscripten-sjlj",
41     cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
42     cl::init(false));
43 
44 extern "C" void LLVMInitializeWebAssemblyTarget() {
45   // Register the target.
46   RegisterTargetMachine<WebAssemblyTargetMachine> X(
47       getTheWebAssemblyTarget32());
48   RegisterTargetMachine<WebAssemblyTargetMachine> Y(
49       getTheWebAssemblyTarget64());
50 
51   // Register backend passes
52   auto &PR = *PassRegistry::getPassRegistry();
53   initializeWebAssemblyAddMissingPrototypesPass(PR);
54   initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
55   initializeLowerGlobalDtorsPass(PR);
56   initializeFixFunctionBitcastsPass(PR);
57   initializeOptimizeReturnedPass(PR);
58   initializeWebAssemblyArgumentMovePass(PR);
59   initializeWebAssemblySetP2AlignOperandsPass(PR);
60   initializeWebAssemblyReplacePhysRegsPass(PR);
61   initializeWebAssemblyPrepareForLiveIntervalsPass(PR);
62   initializeWebAssemblyOptimizeLiveIntervalsPass(PR);
63   initializeWebAssemblyMemIntrinsicResultsPass(PR);
64   initializeWebAssemblyRegStackifyPass(PR);
65   initializeWebAssemblyRegColoringPass(PR);
66   initializeWebAssemblyExplicitLocalsPass(PR);
67   initializeWebAssemblyFixIrreducibleControlFlowPass(PR);
68   initializeWebAssemblyLateEHPreparePass(PR);
69   initializeWebAssemblyExceptionInfoPass(PR);
70   initializeWebAssemblyCFGSortPass(PR);
71   initializeWebAssemblyCFGStackifyPass(PR);
72   initializeWebAssemblyLowerBrUnlessPass(PR);
73   initializeWebAssemblyRegNumberingPass(PR);
74   initializeWebAssemblyPeepholePass(PR);
75   initializeWebAssemblyCallIndirectFixupPass(PR);
76 }
77 
78 //===----------------------------------------------------------------------===//
79 // WebAssembly Lowering public interface.
80 //===----------------------------------------------------------------------===//
81 
82 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
83   if (!RM.hasValue()) {
84     // Default to static relocation model.  This should always be more optimial
85     // than PIC since the static linker can determine all global addresses and
86     // assume direct function calls.
87     return Reloc::Static;
88   }
89   return *RM;
90 }
91 
92 /// Create an WebAssembly architecture model.
93 ///
94 WebAssemblyTargetMachine::WebAssemblyTargetMachine(
95     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
96     const TargetOptions &Options, Optional<Reloc::Model> RM,
97     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
98     : LLVMTargetMachine(T,
99                         TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
100                                          : "e-m:e-p:32:32-i64:64-n32:64-S128",
101                         TT, CPU, FS, Options, getEffectiveRelocModel(RM),
102                         getEffectiveCodeModel(CM, CodeModel::Large), OL),
103       TLOF(new WebAssemblyTargetObjectFile()) {
104   // WebAssembly type-checks instructions, but a noreturn function with a return
105   // type that doesn't match the context will cause a check failure. So we lower
106   // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
107   // 'unreachable' instructions which is meant for that case.
108   this->Options.TrapUnreachable = true;
109 
110   // WebAssembly treats each function as an independent unit. Force
111   // -ffunction-sections, effectively, so that we can emit them independently.
112   this->Options.FunctionSections = true;
113   this->Options.DataSections = true;
114   this->Options.UniqueSectionNames = true;
115 
116   initAsmInfo();
117 
118   // Create a subtarget using the unmodified target machine features to
119   // initialize the used feature set with explicitly enabled features.
120   getSubtargetImpl(getTargetCPU(), getTargetFeatureString());
121 
122   // Note that we don't use setRequiresStructuredCFG(true). It disables
123   // optimizations than we're ok with, and want, such as critical edge
124   // splitting and tail merging.
125 }
126 
127 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
128 
129 const WebAssemblySubtarget *
130 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
131                                            std::string FS) const {
132   auto &I = SubtargetMap[CPU + FS];
133   if (!I) {
134     I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
135     UsedFeatures |= I->getFeatureBits();
136   }
137   return I.get();
138 }
139 
140 const WebAssemblySubtarget *
141 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
142   Attribute CPUAttr = F.getFnAttribute("target-cpu");
143   Attribute FSAttr = F.getFnAttribute("target-features");
144 
145   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
146                         ? CPUAttr.getValueAsString().str()
147                         : TargetCPU;
148   std::string FS = !FSAttr.hasAttribute(Attribute::None)
149                        ? FSAttr.getValueAsString().str()
150                        : TargetFS;
151 
152   // This needs to be done before we create a new subtarget since any
153   // creation will depend on the TM and the code generation flags on the
154   // function that reside in TargetOptions.
155   resetTargetOptions(F);
156 
157   return getSubtargetImpl(CPU, FS);
158 }
159 
160 namespace {
161 class StripThreadLocal final : public ModulePass {
162   // The default thread model for wasm is single, where thread-local variables
163   // are identical to regular globals and should be treated the same. So this
164   // pass just converts all GlobalVariables to NotThreadLocal
165   static char ID;
166 
167 public:
168   StripThreadLocal() : ModulePass(ID) {}
169   bool runOnModule(Module &M) override {
170     for (auto &GV : M.globals())
171       GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal);
172     return true;
173   }
174 };
175 char StripThreadLocal::ID = 0;
176 
177 /// WebAssembly Code Generator Pass Configuration Options.
178 class WebAssemblyPassConfig final : public TargetPassConfig {
179 public:
180   WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM)
181       : TargetPassConfig(TM, PM) {}
182 
183   WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
184     return getTM<WebAssemblyTargetMachine>();
185   }
186 
187   FunctionPass *createTargetRegisterAllocator(bool) override;
188 
189   void addIRPasses() override;
190   bool addInstSelector() override;
191   void addPostRegAlloc() override;
192   bool addGCPasses() override { return false; }
193   void addPreEmitPass() override;
194 
195   // No reg alloc
196   bool addRegAssignmentFast() override { return false; }
197 
198   // No reg alloc
199   bool addRegAssignmentOptimized() override { return false; }
200 };
201 } // end anonymous namespace
202 
203 TargetTransformInfo
204 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) {
205   return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
206 }
207 
208 TargetPassConfig *
209 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
210   return new WebAssemblyPassConfig(*this, PM);
211 }
212 
213 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
214   return nullptr; // No reg alloc
215 }
216 
217 //===----------------------------------------------------------------------===//
218 // The following functions are called from lib/CodeGen/Passes.cpp to modify
219 // the CodeGen pass sequence.
220 //===----------------------------------------------------------------------===//
221 
222 void WebAssemblyPassConfig::addIRPasses() {
223   if (static_cast<WebAssemblyTargetMachine *>(TM)
224           ->getUsedFeatures()[WebAssembly::FeatureAtomics]) {
225     // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
226     // control specifically what gets lowered.
227     addPass(createAtomicExpandPass());
228   } else {
229     // If atomics are not enabled, they get lowered to non-atomics.
230     addPass(createLowerAtomicPass());
231     addPass(new StripThreadLocal());
232   }
233 
234   // Add signatures to prototype-less function declarations
235   addPass(createWebAssemblyAddMissingPrototypes());
236 
237   // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls.
238   addPass(createWebAssemblyLowerGlobalDtors());
239 
240   // Fix function bitcasts, as WebAssembly requires caller and callee signatures
241   // to match.
242   addPass(createWebAssemblyFixFunctionBitcasts());
243 
244   // Optimize "returned" function attributes.
245   if (getOptLevel() != CodeGenOpt::None)
246     addPass(createWebAssemblyOptimizeReturned());
247 
248   // If exception handling is not enabled and setjmp/longjmp handling is
249   // enabled, we lower invokes into calls and delete unreachable landingpad
250   // blocks. Lowering invokes when there is no EH support is done in
251   // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
252   // function and SjLj handling expects all invokes to be lowered before.
253   if (!EnableEmException &&
254       TM->Options.ExceptionModel == ExceptionHandling::None) {
255     addPass(createLowerInvokePass());
256     // The lower invoke pass may create unreachable code. Remove it in order not
257     // to process dead blocks in setjmp/longjmp handling.
258     addPass(createUnreachableBlockEliminationPass());
259   }
260 
261   // Handle exceptions and setjmp/longjmp if enabled.
262   if (EnableEmException || EnableEmSjLj)
263     addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
264                                                    EnableEmSjLj));
265 
266   TargetPassConfig::addIRPasses();
267 }
268 
269 bool WebAssemblyPassConfig::addInstSelector() {
270   (void)TargetPassConfig::addInstSelector();
271   addPass(
272       createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
273   // Run the argument-move pass immediately after the ScheduleDAG scheduler
274   // so that we can fix up the ARGUMENT instructions before anything else
275   // sees them in the wrong place.
276   addPass(createWebAssemblyArgumentMove());
277   // Set the p2align operands. This information is present during ISel, however
278   // it's inconvenient to collect. Collect it now, and update the immediate
279   // operands.
280   addPass(createWebAssemblySetP2AlignOperands());
281   return false;
282 }
283 
284 void WebAssemblyPassConfig::addPostRegAlloc() {
285   // TODO: The following CodeGen passes don't currently support code containing
286   // virtual registers. Consider removing their restrictions and re-enabling
287   // them.
288 
289   // These functions all require the NoVRegs property.
290   disablePass(&MachineCopyPropagationID);
291   disablePass(&PostRAMachineSinkingID);
292   disablePass(&PostRASchedulerID);
293   disablePass(&FuncletLayoutID);
294   disablePass(&StackMapLivenessID);
295   disablePass(&LiveDebugValuesID);
296   disablePass(&PatchableFunctionID);
297   disablePass(&ShrinkWrapID);
298 
299   // This pass hurts code size for wasm because it can generate irreducible
300   // control flow.
301   disablePass(&MachineBlockPlacementID);
302 
303   TargetPassConfig::addPostRegAlloc();
304 }
305 
306 void WebAssemblyPassConfig::addPreEmitPass() {
307   TargetPassConfig::addPreEmitPass();
308 
309   // Rewrite pseudo call_indirect instructions as real instructions.
310   // This needs to run before register stackification, because we change the
311   // order of the arguments.
312   addPass(createWebAssemblyCallIndirectFixup());
313 
314   // Eliminate multiple-entry loops.
315   addPass(createWebAssemblyFixIrreducibleControlFlow());
316 
317   // Do various transformations for exception handling.
318   // Every CFG-changing optimizations should come before this.
319   addPass(createWebAssemblyLateEHPrepare());
320 
321   // Now that we have a prologue and epilogue and all frame indices are
322   // rewritten, eliminate SP and FP. This allows them to be stackified,
323   // colored, and numbered with the rest of the registers.
324   addPass(createWebAssemblyReplacePhysRegs());
325 
326   // Preparations and optimizations related to register stackification.
327   if (getOptLevel() != CodeGenOpt::None) {
328     // LiveIntervals isn't commonly run this late. Re-establish preconditions.
329     addPass(createWebAssemblyPrepareForLiveIntervals());
330 
331     // Depend on LiveIntervals and perform some optimizations on it.
332     addPass(createWebAssemblyOptimizeLiveIntervals());
333 
334     // Prepare memory intrinsic calls for register stackifying.
335     addPass(createWebAssemblyMemIntrinsicResults());
336 
337     // Mark registers as representing wasm's value stack. This is a key
338     // code-compression technique in WebAssembly. We run this pass (and
339     // MemIntrinsicResults above) very late, so that it sees as much code as
340     // possible, including code emitted by PEI and expanded by late tail
341     // duplication.
342     addPass(createWebAssemblyRegStackify());
343 
344     // Run the register coloring pass to reduce the total number of registers.
345     // This runs after stackification so that it doesn't consider registers
346     // that become stackified.
347     addPass(createWebAssemblyRegColoring());
348   }
349 
350   // Insert explicit local.get and local.set operators.
351   addPass(createWebAssemblyExplicitLocals());
352 
353   // Sort the blocks of the CFG into topological order, a prerequisite for
354   // BLOCK and LOOP markers.
355   addPass(createWebAssemblyCFGSort());
356 
357   // Insert BLOCK and LOOP markers.
358   addPass(createWebAssemblyCFGStackify());
359 
360   // Lower br_unless into br_if.
361   addPass(createWebAssemblyLowerBrUnless());
362 
363   // Perform the very last peephole optimizations on the code.
364   if (getOptLevel() != CodeGenOpt::None)
365     addPass(createWebAssemblyPeephole());
366 
367   // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
368   addPass(createWebAssemblyRegNumbering());
369 }
370