blob: 5b8e19bbd186417c707f4127949046dabb14f40f [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"
23#include "llvm/IR/Function.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000024#include "llvm/Support/TargetRegistry.h"
25#include "llvm/Target/TargetOptions.h"
JF Bastien03855df2015-07-01 23:41:25 +000026#include "llvm/Transforms/Scalar.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000027using namespace llvm;
28
29#define DEBUG_TYPE "wasm"
30
31extern "C" void LLVMInitializeWebAssemblyTarget() {
32 // Register the target.
Dan Gohmand82494b2015-07-01 21:42:34 +000033 RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
34 RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
Dan Gohman10e730a2015-06-29 23:51:55 +000035}
36
37//===----------------------------------------------------------------------===//
38// WebAssembly Lowering public interface.
39//===----------------------------------------------------------------------===//
40
41/// Create an WebAssembly architecture model.
42///
43WebAssemblyTargetMachine::WebAssemblyTargetMachine(
44 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
45 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
46 CodeGenOpt::Level OL)
Dan Gohman0c6f5ac2016-01-07 03:19:23 +000047 : LLVMTargetMachine(T,
48 TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
49 : "e-m:e-p:32:32-i64:64-n32:64-S128",
Dan Gohman10e730a2015-06-29 23:51:55 +000050 TT, CPU, FS, Options, RM, CM, OL),
Dan Gohman5bf22fc2015-12-17 04:55:44 +000051 TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
Derek Schuffffa143c2015-11-10 00:30:57 +000052 // WebAssembly type-checks expressions, but a noreturn function with a return
53 // type that doesn't match the context will cause a check failure. So we lower
54 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
55 // 'unreachable' expression which is meant for that case.
56 this->Options.TrapUnreachable = true;
57
Dan Gohman10e730a2015-06-29 23:51:55 +000058 initAsmInfo();
59
Dan Gohmand85ab7f2016-02-18 06:32:53 +000060 // Note that we don't use setRequiresStructuredCFG(true). It disables
61 // optimizations than we're ok with, and want, such as critical edge
62 // splitting and tail merging.
Dan Gohman10e730a2015-06-29 23:51:55 +000063}
64
65WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
66
67const WebAssemblySubtarget *
68WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
69 Attribute CPUAttr = F.getFnAttribute("target-cpu");
70 Attribute FSAttr = F.getFnAttribute("target-features");
71
72 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
73 ? CPUAttr.getValueAsString().str()
74 : TargetCPU;
75 std::string FS = !FSAttr.hasAttribute(Attribute::None)
76 ? FSAttr.getValueAsString().str()
77 : TargetFS;
78
79 auto &I = SubtargetMap[CPU + FS];
80 if (!I) {
81 // This needs to be done before we create a new subtarget since any
82 // creation will depend on the TM and the code generation flags on the
83 // function that reside in TargetOptions.
84 resetTargetOptions(F);
Rafael Espindola3adc7ce2015-08-11 18:11:17 +000085 I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
Dan Gohman10e730a2015-06-29 23:51:55 +000086 }
87 return I.get();
88}
89
90namespace {
91/// WebAssembly Code Generator Pass Configuration Options.
92class WebAssemblyPassConfig final : public TargetPassConfig {
93public:
94 WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
95 : TargetPassConfig(TM, PM) {}
96
97 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
98 return getTM<WebAssemblyTargetMachine>();
99 }
100
101 FunctionPass *createTargetRegisterAllocator(bool) override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000102
103 void addIRPasses() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000104 bool addInstSelector() override;
105 bool addILPOpts() override;
106 void addPreRegAlloc() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000107 void addPostRegAlloc() override;
Derek Schuffad154c82016-03-28 17:05:30 +0000108 bool addGCPasses() override { return false; }
Dan Gohman10e730a2015-06-29 23:51:55 +0000109 void addPreEmitPass() override;
110};
111} // end anonymous namespace
112
113TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
Hans Wennborg9099b5e62015-09-16 23:59:57 +0000114 return TargetIRAnalysis([this](const Function &F) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000115 return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
116 });
117}
118
119TargetPassConfig *
120WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
121 return new WebAssemblyPassConfig(this, PM);
122}
123
124FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
125 return nullptr; // No reg alloc
126}
127
Dan Gohman10e730a2015-06-29 23:51:55 +0000128//===----------------------------------------------------------------------===//
129// The following functions are called from lib/CodeGen/Passes.cpp to modify
130// the CodeGen pass sequence.
131//===----------------------------------------------------------------------===//
132
133void WebAssemblyPassConfig::addIRPasses() {
JF Bastien03855df2015-07-01 23:41:25 +0000134 if (TM->Options.ThreadModel == ThreadModel::Single)
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000135 // In "single" mode, atomics get lowered to non-atomics.
JF Bastien03855df2015-07-01 23:41:25 +0000136 addPass(createLowerAtomicPass());
137 else
138 // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
139 // control specifically what gets lowered.
140 addPass(createAtomicExpandPass(TM));
Dan Gohman10e730a2015-06-29 23:51:55 +0000141
Dan Gohman81719f82015-11-25 16:55:01 +0000142 // Optimize "returned" function attributes.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000143 if (getOptLevel() != CodeGenOpt::None)
144 addPass(createWebAssemblyOptimizeReturned());
Dan Gohman81719f82015-11-25 16:55:01 +0000145
Dan Gohman10e730a2015-06-29 23:51:55 +0000146 TargetPassConfig::addIRPasses();
147}
148
Dan Gohman10e730a2015-06-29 23:51:55 +0000149bool WebAssemblyPassConfig::addInstSelector() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000150 (void)TargetPassConfig::addInstSelector();
Dan Gohman10e730a2015-06-29 23:51:55 +0000151 addPass(
152 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
Dan Gohman1cf96c02015-12-09 16:23:59 +0000153 // Run the argument-move pass immediately after the ScheduleDAG scheduler
154 // so that we can fix up the ARGUMENT instructions before anything else
155 // sees them in the wrong place.
156 addPass(createWebAssemblyArgumentMove());
Dan Gohmanbb372242016-01-26 03:39:31 +0000157 // Set the p2align operands. This information is present during ISel, however
158 // it's inconvenient to collect. Collect it now, and update the immediate
159 // operands.
160 addPass(createWebAssemblySetP2AlignOperands());
Dan Gohman10e730a2015-06-29 23:51:55 +0000161 return false;
162}
163
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000164bool WebAssemblyPassConfig::addILPOpts() {
165 (void)TargetPassConfig::addILPOpts();
166 return true;
167}
Dan Gohman10e730a2015-06-29 23:51:55 +0000168
Dan Gohman4ba48162015-11-18 16:12:01 +0000169void WebAssemblyPassConfig::addPreRegAlloc() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000170 TargetPassConfig::addPreRegAlloc();
171
Dan Gohman81719f82015-11-25 16:55:01 +0000172 // Prepare store instructions for register stackifying.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000173 if (getOptLevel() != CodeGenOpt::None)
174 addPass(createWebAssemblyStoreResults());
Dan Gohman4ba48162015-11-18 16:12:01 +0000175}
Dan Gohman10e730a2015-06-29 23:51:55 +0000176
JF Bastien600aee92015-07-31 17:53:38 +0000177void WebAssemblyPassConfig::addPostRegAlloc() {
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000178 // TODO: The following CodeGen passes don't currently support code containing
179 // virtual registers. Consider removing their restrictions and re-enabling
180 // them.
Derek Schuffad154c82016-03-28 17:05:30 +0000181
182 // Has no asserts of its own, but was not written to handle virtual regs.
183 disablePass(&ShrinkWrapID);
Derek Schuff9769deb2015-12-11 23:49:46 +0000184 // We use our own PrologEpilogInserter which is very slightly modified to
185 // tolerate virtual registers.
JF Bastien600aee92015-07-31 17:53:38 +0000186 disablePass(&PrologEpilogCodeInserterID);
Derek Schuffecabac62016-03-28 22:52:20 +0000187
188 // These functions all require the AllVRegsAllocated property.
JF Bastien600aee92015-07-31 17:53:38 +0000189 disablePass(&MachineCopyPropagationID);
Derek Schuffecabac62016-03-28 22:52:20 +0000190 disablePass(&PostRASchedulerID);
191 disablePass(&FuncletLayoutID);
192 disablePass(&StackMapLivenessID);
193 disablePass(&LiveDebugValuesID);
Dan Gohman950a13c2015-09-16 16:51:30 +0000194
Derek Schuff71434ff2016-02-17 23:20:43 +0000195 if (getOptLevel() != CodeGenOpt::None) {
196 // Mark registers as representing wasm's expression stack.
197 addPass(createWebAssemblyRegStackify());
Dan Gohman8887d1f2015-12-25 00:31:02 +0000198
Derek Schuff71434ff2016-02-17 23:20:43 +0000199 // Run the register coloring pass to reduce the total number of registers.
200 addPass(createWebAssemblyRegColoring());
201 }
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000202
203 TargetPassConfig::addPostRegAlloc();
Derek Schuff9769deb2015-12-11 23:49:46 +0000204
205 // Run WebAssembly's version of the PrologEpilogInserter. Target-independent
206 // PEI runs after PostRegAlloc and after ShrinkWrap. Putting it here will run
207 // PEI before ShrinkWrap but otherwise in the same position in the order.
208 addPass(createWebAssemblyPEI());
JF Bastien600aee92015-07-31 17:53:38 +0000209}
Dan Gohman10e730a2015-06-29 23:51:55 +0000210
Dan Gohman950a13c2015-09-16 16:51:30 +0000211void WebAssemblyPassConfig::addPreEmitPass() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000212 TargetPassConfig::addPreEmitPass();
Dan Gohman05ac43f2015-12-17 01:39:00 +0000213
Dan Gohmand7a2eea2016-03-09 02:01:14 +0000214 // Eliminate multiple-entry loops.
215 addPass(createWebAssemblyFixIrreducibleControlFlow());
216
Dan Gohman5941bde2015-11-25 21:32:06 +0000217 // Put the CFG in structured form; insert BLOCK and LOOP markers.
Dan Gohman950a13c2015-09-16 16:51:30 +0000218 addPass(createWebAssemblyCFGStackify());
Dan Gohman5941bde2015-11-25 21:32:06 +0000219
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000220 // Lower br_unless into br_if.
221 addPass(createWebAssemblyLowerBrUnless());
222
Dan Gohman5941bde2015-11-25 21:32:06 +0000223 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000224 addPass(createWebAssemblyRegNumbering());
Dan Gohman5941bde2015-11-25 21:32:06 +0000225
226 // Perform the very last peephole optimizations on the code.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000227 if (getOptLevel() != CodeGenOpt::None)
228 addPass(createWebAssemblyPeephole());
Dan Gohman950a13c2015-09-16 16:51:30 +0000229}