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