blob: 32154af3c1c2c515dc92c01e015fd9802660e9a1 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===- 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 "WebAssembly.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17#include "WebAssemblyTargetMachine.h"
Dan Gohman5bf22fc2015-12-17 04:55:44 +000018#include "WebAssemblyTargetObjectFile.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000019#include "WebAssemblyTargetTransformInfo.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/RegAllocRegistry.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000023#include "llvm/CodeGen/TargetPassConfig.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000024#include "llvm/IR/Function.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000025#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Target/TargetOptions.h"
JF Bastien03855df2015-07-01 23:41:25 +000027#include "llvm/Transforms/Scalar.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000028using namespace llvm;
29
30#define DEBUG_TYPE "wasm"
31
32extern "C" void LLVMInitializeWebAssemblyTarget() {
33 // Register the target.
Dan Gohmand82494b2015-07-01 21:42:34 +000034 RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
35 RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
Dan Gohman10e730a2015-06-29 23:51:55 +000036}
37
38//===----------------------------------------------------------------------===//
39// WebAssembly Lowering public interface.
40//===----------------------------------------------------------------------===//
41
Dan Gohman41133a32016-05-19 03:00:05 +000042static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
43 if (!RM.hasValue())
44 return Reloc::PIC_;
45 return *RM;
46}
47
Dan Gohman10e730a2015-06-29 23:51:55 +000048/// Create an WebAssembly architecture model.
49///
50WebAssemblyTargetMachine::WebAssemblyTargetMachine(
51 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Dan Gohman41133a32016-05-19 03:00:05 +000052 const TargetOptions &Options, Optional<Reloc::Model> RM,
53 CodeModel::Model CM, CodeGenOpt::Level OL)
Dan Gohman0c6f5ac2016-01-07 03:19:23 +000054 : LLVMTargetMachine(T,
55 TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
56 : "e-m:e-p:32:32-i64:64-n32:64-S128",
Dan Gohman41133a32016-05-19 03:00:05 +000057 TT, CPU, FS, Options, getEffectiveRelocModel(RM),
58 CM, OL),
Dan Gohman5bf22fc2015-12-17 04:55:44 +000059 TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
Derek Schuffffa143c2015-11-10 00:30:57 +000060 // WebAssembly type-checks expressions, but a noreturn function with a return
61 // type that doesn't match the context will cause a check failure. So we lower
62 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
63 // 'unreachable' expression which is meant for that case.
64 this->Options.TrapUnreachable = true;
65
Dan Gohman10e730a2015-06-29 23:51:55 +000066 initAsmInfo();
67
Dan Gohmand85ab7f2016-02-18 06:32:53 +000068 // Note that we don't use setRequiresStructuredCFG(true). It disables
69 // optimizations than we're ok with, and want, such as critical edge
70 // splitting and tail merging.
Dan Gohman10e730a2015-06-29 23:51:55 +000071}
72
73WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
74
75const WebAssemblySubtarget *
76WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
77 Attribute CPUAttr = F.getFnAttribute("target-cpu");
78 Attribute FSAttr = F.getFnAttribute("target-features");
79
80 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
81 ? CPUAttr.getValueAsString().str()
82 : TargetCPU;
83 std::string FS = !FSAttr.hasAttribute(Attribute::None)
84 ? FSAttr.getValueAsString().str()
85 : TargetFS;
86
87 auto &I = SubtargetMap[CPU + FS];
88 if (!I) {
89 // This needs to be done before we create a new subtarget since any
90 // creation will depend on the TM and the code generation flags on the
91 // function that reside in TargetOptions.
92 resetTargetOptions(F);
Rafael Espindola3adc7ce2015-08-11 18:11:17 +000093 I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
Dan Gohman10e730a2015-06-29 23:51:55 +000094 }
95 return I.get();
96}
97
98namespace {
99/// WebAssembly Code Generator Pass Configuration Options.
100class WebAssemblyPassConfig final : public TargetPassConfig {
101public:
102 WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
103 : TargetPassConfig(TM, PM) {}
104
105 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
106 return getTM<WebAssemblyTargetMachine>();
107 }
108
109 FunctionPass *createTargetRegisterAllocator(bool) override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000110
111 void addIRPasses() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000112 bool addInstSelector() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000113 void addPostRegAlloc() override;
Derek Schuffad154c82016-03-28 17:05:30 +0000114 bool addGCPasses() override { return false; }
Dan Gohman10e730a2015-06-29 23:51:55 +0000115 void addPreEmitPass() override;
116};
117} // end anonymous namespace
118
119TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
Hans Wennborg9099b5e62015-09-16 23:59:57 +0000120 return TargetIRAnalysis([this](const Function &F) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000121 return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
122 });
123}
124
125TargetPassConfig *
126WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
127 return new WebAssemblyPassConfig(this, PM);
128}
129
130FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
131 return nullptr; // No reg alloc
132}
133
Dan Gohman10e730a2015-06-29 23:51:55 +0000134//===----------------------------------------------------------------------===//
135// The following functions are called from lib/CodeGen/Passes.cpp to modify
136// the CodeGen pass sequence.
137//===----------------------------------------------------------------------===//
138
139void WebAssemblyPassConfig::addIRPasses() {
JF Bastien03855df2015-07-01 23:41:25 +0000140 if (TM->Options.ThreadModel == ThreadModel::Single)
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000141 // In "single" mode, atomics get lowered to non-atomics.
JF Bastien03855df2015-07-01 23:41:25 +0000142 addPass(createLowerAtomicPass());
143 else
144 // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
145 // control specifically what gets lowered.
146 addPass(createAtomicExpandPass(TM));
Dan Gohman10e730a2015-06-29 23:51:55 +0000147
Dan Gohman81719f82015-11-25 16:55:01 +0000148 // Optimize "returned" function attributes.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000149 if (getOptLevel() != CodeGenOpt::None)
150 addPass(createWebAssemblyOptimizeReturned());
Dan Gohman81719f82015-11-25 16:55:01 +0000151
Dan Gohman10e730a2015-06-29 23:51:55 +0000152 TargetPassConfig::addIRPasses();
153}
154
Dan Gohman10e730a2015-06-29 23:51:55 +0000155bool WebAssemblyPassConfig::addInstSelector() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000156 (void)TargetPassConfig::addInstSelector();
Dan Gohman10e730a2015-06-29 23:51:55 +0000157 addPass(
158 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
Dan Gohman1cf96c02015-12-09 16:23:59 +0000159 // Run the argument-move pass immediately after the ScheduleDAG scheduler
160 // so that we can fix up the ARGUMENT instructions before anything else
161 // sees them in the wrong place.
162 addPass(createWebAssemblyArgumentMove());
Dan Gohmanbb372242016-01-26 03:39:31 +0000163 // Set the p2align operands. This information is present during ISel, however
164 // it's inconvenient to collect. Collect it now, and update the immediate
165 // operands.
166 addPass(createWebAssemblySetP2AlignOperands());
Dan Gohman10e730a2015-06-29 23:51:55 +0000167 return false;
168}
169
JF Bastien600aee92015-07-31 17:53:38 +0000170void WebAssemblyPassConfig::addPostRegAlloc() {
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000171 // TODO: The following CodeGen passes don't currently support code containing
172 // virtual registers. Consider removing their restrictions and re-enabling
173 // them.
Derek Schuffad154c82016-03-28 17:05:30 +0000174
175 // Has no asserts of its own, but was not written to handle virtual regs.
176 disablePass(&ShrinkWrapID);
Derek Schuffecabac62016-03-28 22:52:20 +0000177
178 // These functions all require the AllVRegsAllocated property.
JF Bastien600aee92015-07-31 17:53:38 +0000179 disablePass(&MachineCopyPropagationID);
Derek Schuffecabac62016-03-28 22:52:20 +0000180 disablePass(&PostRASchedulerID);
181 disablePass(&FuncletLayoutID);
182 disablePass(&StackMapLivenessID);
183 disablePass(&LiveDebugValuesID);
Sanjoy Dasfe71ec72016-04-19 06:24:58 +0000184 disablePass(&PatchableFunctionID);
Dan Gohman950a13c2015-09-16 16:51:30 +0000185
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000186 TargetPassConfig::addPostRegAlloc();
JF Bastien600aee92015-07-31 17:53:38 +0000187}
Dan Gohman10e730a2015-06-29 23:51:55 +0000188
Dan Gohman950a13c2015-09-16 16:51:30 +0000189void WebAssemblyPassConfig::addPreEmitPass() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000190 TargetPassConfig::addPreEmitPass();
Dan Gohman05ac43f2015-12-17 01:39:00 +0000191
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000192 // Now that we have a prologue and epilogue and all frame indices are
193 // rewritten, eliminate SP and FP. This allows them to be stackified,
194 // colored, and numbered with the rest of the registers.
195 addPass(createWebAssemblyReplacePhysRegs());
196
197 if (getOptLevel() != CodeGenOpt::None) {
198 // LiveIntervals isn't commonly run this late. Re-establish preconditions.
199 addPass(createWebAssemblyPrepareForLiveIntervals());
200
201 // Depend on LiveIntervals and perform some optimizations on it.
202 addPass(createWebAssemblyOptimizeLiveIntervals());
203
204 // Prepare store instructions for register stackifying.
205 addPass(createWebAssemblyStoreResults());
206
207 // Mark registers as representing wasm's expression stack. This is a key
208 // code-compression technique in WebAssembly. We run this pass (and
209 // StoreResults above) very late, so that it sees as much code as possible,
210 // including code emitted by PEI and expanded by late tail duplication.
211 addPass(createWebAssemblyRegStackify());
212
213 // Run the register coloring pass to reduce the total number of registers.
214 // This runs after stackification so that it doesn't consider registers
215 // that become stackified.
216 addPass(createWebAssemblyRegColoring());
217 }
218
Dan Gohmand7a2eea2016-03-09 02:01:14 +0000219 // Eliminate multiple-entry loops.
220 addPass(createWebAssemblyFixIrreducibleControlFlow());
221
Dan Gohman5941bde2015-11-25 21:32:06 +0000222 // Put the CFG in structured form; insert BLOCK and LOOP markers.
Dan Gohman950a13c2015-09-16 16:51:30 +0000223 addPass(createWebAssemblyCFGStackify());
Dan Gohman5941bde2015-11-25 21:32:06 +0000224
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000225 // Lower br_unless into br_if.
226 addPass(createWebAssemblyLowerBrUnless());
227
Dan Gohman5941bde2015-11-25 21:32:06 +0000228 // Perform the very last peephole optimizations on the code.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000229 if (getOptLevel() != CodeGenOpt::None)
230 addPass(createWebAssemblyPeephole());
Dan Gohmanb7c24002016-05-21 00:21:56 +0000231
232 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
233 addPass(createWebAssemblyRegNumbering());
Dan Gohman950a13c2015-09-16 16:51:30 +0000234}