blob: a333fb3055e9fc1fde87c0cb08daf626a3fb677d [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"
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"
Dan Gohman53828fd2015-11-23 16:50:18 +000023#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000024#include "llvm/IR/Function.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Target/TargetOptions.h"
JF Bastien03855df2015-07-01 23:41:25 +000028#include "llvm/Transforms/Scalar.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000029using namespace llvm;
30
31#define DEBUG_TYPE "wasm"
32
33extern "C" void LLVMInitializeWebAssemblyTarget() {
34 // Register the target.
Dan Gohmand82494b2015-07-01 21:42:34 +000035 RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
36 RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
Dan Gohman10e730a2015-06-29 23:51:55 +000037}
38
39//===----------------------------------------------------------------------===//
40// WebAssembly Lowering public interface.
41//===----------------------------------------------------------------------===//
42
43/// Create an WebAssembly architecture model.
44///
45WebAssemblyTargetMachine::WebAssemblyTargetMachine(
46 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
47 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
48 CodeGenOpt::Level OL)
Dan Gohman7a6b9822015-11-29 22:32:02 +000049 : LLVMTargetMachine(T, TT.isArch64Bit() ? "e-p:64:64-i64:64-n32:64-S128"
50 : "e-p:32:32-i64:64-n32:64-S128",
Dan Gohman10e730a2015-06-29 23:51:55 +000051 TT, CPU, FS, Options, RM, CM, OL),
52 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
61 // We need a reducible CFG, so disable some optimizations which tend to
62 // introduce irreducibility.
63 setRequiresStructuredCFG(true);
64}
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;
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.
143 addPass(createWebAssemblyOptimizeReturned());
144
Dan Gohman10e730a2015-06-29 23:51:55 +0000145 TargetPassConfig::addIRPasses();
146}
147
Dan Gohman10e730a2015-06-29 23:51:55 +0000148bool WebAssemblyPassConfig::addInstSelector() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000149 (void)TargetPassConfig::addInstSelector();
Dan Gohman10e730a2015-06-29 23:51:55 +0000150 addPass(
151 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
Dan Gohman1cf96c02015-12-09 16:23:59 +0000152 // Run the argument-move pass immediately after the ScheduleDAG scheduler
153 // so that we can fix up the ARGUMENT instructions before anything else
154 // sees them in the wrong place.
155 addPass(createWebAssemblyArgumentMove());
Dan Gohman10e730a2015-06-29 23:51:55 +0000156 return false;
157}
158
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000159bool WebAssemblyPassConfig::addILPOpts() {
160 (void)TargetPassConfig::addILPOpts();
161 return true;
162}
Dan Gohman10e730a2015-06-29 23:51:55 +0000163
Dan Gohman4ba48162015-11-18 16:12:01 +0000164void WebAssemblyPassConfig::addPreRegAlloc() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000165 TargetPassConfig::addPreRegAlloc();
166
Dan Gohman81719f82015-11-25 16:55:01 +0000167 // Prepare store instructions for register stackifying.
168 addPass(createWebAssemblyStoreResults());
169
Dan Gohman4ba48162015-11-18 16:12:01 +0000170 // Mark registers as representing wasm's expression stack.
171 addPass(createWebAssemblyRegStackify());
172}
Dan Gohman10e730a2015-06-29 23:51:55 +0000173
JF Bastien600aee92015-07-31 17:53:38 +0000174void WebAssemblyPassConfig::addPostRegAlloc() {
Dan Gohman9c54d3b2015-11-25 18:13:18 +0000175 // TODO: The following CodeGen passes don't currently support code containing
176 // virtual registers. Consider removing their restrictions and re-enabling
177 // them.
JF Bastien600aee92015-07-31 17:53:38 +0000178 //
179 // Fails with: Regalloc must assign all vregs.
180 disablePass(&PrologEpilogCodeInserterID);
181 // Fails with: should be run after register allocation.
182 disablePass(&MachineCopyPropagationID);
Dan Gohman950a13c2015-09-16 16:51:30 +0000183
Dan Gohman4ba48162015-11-18 16:12:01 +0000184 // Run the register coloring pass to reduce the total number of registers.
185 addPass(createWebAssemblyRegColoring());
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000186
187 TargetPassConfig::addPostRegAlloc();
JF Bastien600aee92015-07-31 17:53:38 +0000188}
Dan Gohman10e730a2015-06-29 23:51:55 +0000189
Dan Gohman950a13c2015-09-16 16:51:30 +0000190void WebAssemblyPassConfig::addPreEmitPass() {
Dan Gohmanb0921ca2015-12-05 19:24:17 +0000191 TargetPassConfig::addPreEmitPass();
192
Dan Gohman5941bde2015-11-25 21:32:06 +0000193 // Put the CFG in structured form; insert BLOCK and LOOP markers.
Dan Gohman950a13c2015-09-16 16:51:30 +0000194 addPass(createWebAssemblyCFGStackify());
Dan Gohman5941bde2015-11-25 21:32:06 +0000195
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000196 // Lower br_unless into br_if.
197 addPass(createWebAssemblyLowerBrUnless());
198
Dan Gohman5941bde2015-11-25 21:32:06 +0000199 // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
Dan Gohmancf4748f2015-11-12 17:04:33 +0000200 addPass(createWebAssemblyRegNumbering());
Dan Gohman5941bde2015-11-25 21:32:06 +0000201
202 // Perform the very last peephole optimizations on the code.
Dan Gohman81719f82015-11-25 16:55:01 +0000203 addPass(createWebAssemblyPeephole());
Dan Gohman950a13c2015-09-16 16:51:30 +0000204}