blob: ba13c2f97affdbcae4b99a5636c033cb3b52d366 [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"
24#include "llvm/Support/CommandLine.h"
25#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
42/// Create an WebAssembly architecture model.
43///
44WebAssemblyTargetMachine::WebAssemblyTargetMachine(
45 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
46 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
47 CodeGenOpt::Level OL)
Dan Gohman0c6f5ac2016-01-07 03:19:23 +000048 : LLVMTargetMachine(T,
49 TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
50 : "e-m:e-p:32:32-i64:64-n32:64-S128",
Dan Gohman10e730a2015-06-29 23:51:55 +000051 TT, CPU, FS, Options, RM, CM, OL),
Dan Gohman5bf22fc2015-12-17 04:55:44 +000052 TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
Derek Schuffffa143c2015-11-10 00:30:57 +000053 // WebAssembly type-checks expressions, but a noreturn function with a return
54 // type that doesn't match the context will cause a check failure. So we lower
55 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
56 // 'unreachable' expression which is meant for that case.
57 this->Options.TrapUnreachable = true;
58
Dan Gohman10e730a2015-06-29 23:51:55 +000059 initAsmInfo();
60
Dan Gohmand85ab7f2016-02-18 06:32:53 +000061 // Note that we don't use setRequiresStructuredCFG(true). It disables
62 // optimizations than we're ok with, and want, such as critical edge
63 // splitting and tail merging.
Dan Gohman10e730a2015-06-29 23:51:55 +000064}
65
66WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
67
68const WebAssemblySubtarget *
69WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
70 Attribute CPUAttr = F.getFnAttribute("target-cpu");
71 Attribute FSAttr = F.getFnAttribute("target-features");
72
73 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
74 ? CPUAttr.getValueAsString().str()
75 : TargetCPU;
76 std::string FS = !FSAttr.hasAttribute(Attribute::None)
77 ? FSAttr.getValueAsString().str()
78 : TargetFS;
79
80 auto &I = SubtargetMap[CPU + FS];
81 if (!I) {
82 // This needs to be done before we create a new subtarget since any
83 // creation will depend on the TM and the code generation flags on the
84 // function that reside in TargetOptions.
85 resetTargetOptions(F);
Rafael Espindola3adc7ce2015-08-11 18:11:17 +000086 I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
Dan Gohman10e730a2015-06-29 23:51:55 +000087 }
88 return I.get();
89}
90
91namespace {
92/// WebAssembly Code Generator Pass Configuration Options.
93class WebAssemblyPassConfig final : public TargetPassConfig {
94public:
95 WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
96 : TargetPassConfig(TM, PM) {}
97
98 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
99 return getTM<WebAssemblyTargetMachine>();
100 }
101
102 FunctionPass *createTargetRegisterAllocator(bool) override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000103
104 void addIRPasses() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000105 bool addInstSelector() override;
106 bool addILPOpts() override;
107 void addPreRegAlloc() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000108 void addPostRegAlloc() override;
Derek Schuffad154c82016-03-28 17:05:30 +0000109 void addMachineLateOptimization() override;
110 bool addGCPasses() override { return false; }
Dan Gohman10e730a2015-06-29 23:51:55 +0000111 void addPreEmitPass() override;
112};
113} // end anonymous namespace
114
115TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
Hans Wennborg9099b5e62015-09-16 23:59:57 +0000116 return TargetIRAnalysis([this](const Function &F) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000117 return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
118 });
119}
120
121TargetPassConfig *
122WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
123 return new WebAssemblyPassConfig(this, PM);
124}
125
126FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
127 return nullptr; // No reg alloc
128}
129
Dan Gohman10e730a2015-06-29 23:51:55 +0000130//===----------------------------------------------------------------------===//
131// The following functions are called from lib/CodeGen/Passes.cpp to modify
132// the CodeGen pass sequence.
133//===----------------------------------------------------------------------===//
134
135void WebAssemblyPassConfig::addIRPasses() {
JF Bastien03855df2015-07-01 23:41:25 +0000136 if (TM->Options.ThreadModel == ThreadModel::Single)
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000137 // In "single" mode, atomics get lowered to non-atomics.
JF Bastien03855df2015-07-01 23:41:25 +0000138 addPass(createLowerAtomicPass());
139 else
140 // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
141 // control specifically what gets lowered.
142 addPass(createAtomicExpandPass(TM));
Dan Gohman10e730a2015-06-29 23:51:55 +0000143
Dan Gohman81719f82015-11-25 16:55:01 +0000144 // Optimize "returned" function attributes.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000145 if (getOptLevel() != CodeGenOpt::None)
146 addPass(createWebAssemblyOptimizeReturned());
Dan Gohman81719f82015-11-25 16:55:01 +0000147
Dan Gohman10e730a2015-06-29 23:51:55 +0000148 TargetPassConfig::addIRPasses();
149}
150
Dan Gohman10e730a2015-06-29 23:51:55 +0000151bool WebAssemblyPassConfig::addInstSelector() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000152 (void)TargetPassConfig::addInstSelector();
Dan Gohman10e730a2015-06-29 23:51:55 +0000153 addPass(
154 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
Dan Gohman1cf96c02015-12-09 16:23:59 +0000155 // Run the argument-move pass immediately after the ScheduleDAG scheduler
156 // so that we can fix up the ARGUMENT instructions before anything else
157 // sees them in the wrong place.
158 addPass(createWebAssemblyArgumentMove());
Dan Gohmanbb372242016-01-26 03:39:31 +0000159 // Set the p2align operands. This information is present during ISel, however
160 // it's inconvenient to collect. Collect it now, and update the immediate
161 // operands.
162 addPass(createWebAssemblySetP2AlignOperands());
Dan Gohman10e730a2015-06-29 23:51:55 +0000163 return false;
164}
165
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000166bool WebAssemblyPassConfig::addILPOpts() {
167 (void)TargetPassConfig::addILPOpts();
168 return true;
169}
Dan Gohman10e730a2015-06-29 23:51:55 +0000170
Dan Gohman4ba48162015-11-18 16:12:01 +0000171void WebAssemblyPassConfig::addPreRegAlloc() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000172 TargetPassConfig::addPreRegAlloc();
173
Dan Gohman81719f82015-11-25 16:55:01 +0000174 // Prepare store instructions for register stackifying.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000175 if (getOptLevel() != CodeGenOpt::None)
176 addPass(createWebAssemblyStoreResults());
Dan Gohman4ba48162015-11-18 16:12:01 +0000177}
Dan Gohman10e730a2015-06-29 23:51:55 +0000178
JF Bastien600aee92015-07-31 17:53:38 +0000179void WebAssemblyPassConfig::addPostRegAlloc() {
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000180 // TODO: The following CodeGen passes don't currently support code containing
181 // virtual registers. Consider removing their restrictions and re-enabling
182 // them.
JF Bastien600aee92015-07-31 17:53:38 +0000183 //
Derek Schuffad154c82016-03-28 17:05:30 +0000184
185 // Has no asserts of its own, but was not written to handle virtual regs.
186 disablePass(&ShrinkWrapID);
Derek Schuff9769deb2015-12-11 23:49:46 +0000187 // We use our own PrologEpilogInserter which is very slightly modified to
188 // tolerate virtual registers.
JF Bastien600aee92015-07-31 17:53:38 +0000189 disablePass(&PrologEpilogCodeInserterID);
190 // Fails with: should be run after register allocation.
191 disablePass(&MachineCopyPropagationID);
Dan Gohman950a13c2015-09-16 16:51:30 +0000192
Derek Schuff71434ff2016-02-17 23:20:43 +0000193 if (getOptLevel() != CodeGenOpt::None) {
194 // Mark registers as representing wasm's expression stack.
195 addPass(createWebAssemblyRegStackify());
Dan Gohman8887d1f2015-12-25 00:31:02 +0000196
Derek Schuff71434ff2016-02-17 23:20:43 +0000197 // Run the register coloring pass to reduce the total number of registers.
198 addPass(createWebAssemblyRegColoring());
199 }
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000200
201 TargetPassConfig::addPostRegAlloc();
Derek Schuff9769deb2015-12-11 23:49:46 +0000202
203 // Run WebAssembly's version of the PrologEpilogInserter. Target-independent
204 // PEI runs after PostRegAlloc and after ShrinkWrap. Putting it here will run
205 // PEI before ShrinkWrap but otherwise in the same position in the order.
206 addPass(createWebAssemblyPEI());
JF Bastien600aee92015-07-31 17:53:38 +0000207}
Dan Gohman10e730a2015-06-29 23:51:55 +0000208
Derek Schuffad154c82016-03-28 17:05:30 +0000209void WebAssemblyPassConfig::addMachineLateOptimization() {
210 disablePass(&MachineCopyPropagationID);
211 disablePass(&PostRASchedulerID);
212 TargetPassConfig::addMachineLateOptimization();
213}
214
Dan Gohman950a13c2015-09-16 16:51:30 +0000215void WebAssemblyPassConfig::addPreEmitPass() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000216 TargetPassConfig::addPreEmitPass();
Dan Gohman05ac43f2015-12-17 01:39:00 +0000217
Dan Gohmand7a2eea2016-03-09 02:01:14 +0000218 // Eliminate multiple-entry loops.
219 addPass(createWebAssemblyFixIrreducibleControlFlow());
Derek Schuffad154c82016-03-28 17:05:30 +0000220 disablePass(&FuncletLayoutID);
221 disablePass(&StackMapLivenessID);
222 disablePass(&LiveDebugValuesID);
Dan Gohmand7a2eea2016-03-09 02:01:14 +0000223
Dan Gohman5941bde2015-11-25 21:32:06 +0000224 // Put the CFG in structured form; insert BLOCK and LOOP markers.
Dan Gohman950a13c2015-09-16 16:51:30 +0000225 addPass(createWebAssemblyCFGStackify());
Dan Gohman5941bde2015-11-25 21:32:06 +0000226
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000227 // Lower br_unless into br_if.
228 addPass(createWebAssemblyLowerBrUnless());
229
Dan Gohman5941bde2015-11-25 21:32:06 +0000230 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000231 addPass(createWebAssemblyRegNumbering());
Dan Gohman5941bde2015-11-25 21:32:06 +0000232
233 // Perform the very last peephole optimizations on the code.
Dan Gohmanb13c91f2016-01-19 14:55:02 +0000234 if (getOptLevel() != CodeGenOpt::None)
235 addPass(createWebAssemblyPeephole());
Dan Gohman950a13c2015-09-16 16:51:30 +0000236}