blob: 493e4be18dc6a1aba0298988f6e86ed33365474c [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)
49 : LLVMTargetMachine(T, TT.isArch64Bit()
Dan Gohmandde8dce2015-08-19 20:30:20 +000050 ? "e-p:64:64-i64:64-n32:64-S128"
51 : "e-p:32:32-i64:64-n32:64-S128",
Dan Gohman10e730a2015-06-29 23:51:55 +000052 TT, CPU, FS, Options, RM, CM, OL),
53 TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
Derek Schuffffa143c2015-11-10 00:30:57 +000054 // WebAssembly type-checks expressions, but a noreturn function with a return
55 // type that doesn't match the context will cause a check failure. So we lower
56 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
57 // 'unreachable' expression which is meant for that case.
58 this->Options.TrapUnreachable = true;
59
Dan Gohman10e730a2015-06-29 23:51:55 +000060 initAsmInfo();
61
62 // We need a reducible CFG, so disable some optimizations which tend to
63 // introduce irreducibility.
64 setRequiresStructuredCFG(true);
65}
66
67WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
68
69const WebAssemblySubtarget *
70WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
71 Attribute CPUAttr = F.getFnAttribute("target-cpu");
72 Attribute FSAttr = F.getFnAttribute("target-features");
73
74 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
75 ? CPUAttr.getValueAsString().str()
76 : TargetCPU;
77 std::string FS = !FSAttr.hasAttribute(Attribute::None)
78 ? FSAttr.getValueAsString().str()
79 : TargetFS;
80
81 auto &I = SubtargetMap[CPU + FS];
82 if (!I) {
83 // This needs to be done before we create a new subtarget since any
84 // creation will depend on the TM and the code generation flags on the
85 // function that reside in TargetOptions.
86 resetTargetOptions(F);
Rafael Espindola3adc7ce2015-08-11 18:11:17 +000087 I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
Dan Gohman10e730a2015-06-29 23:51:55 +000088 }
89 return I.get();
90}
91
92namespace {
93/// WebAssembly Code Generator Pass Configuration Options.
94class WebAssemblyPassConfig final : public TargetPassConfig {
95public:
96 WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
97 : TargetPassConfig(TM, PM) {}
98
99 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
100 return getTM<WebAssemblyTargetMachine>();
101 }
102
103 FunctionPass *createTargetRegisterAllocator(bool) override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000104
105 void addIRPasses() override;
106 bool addPreISel() override;
107 bool addInstSelector() override;
108 bool addILPOpts() override;
109 void addPreRegAlloc() override;
Dan Gohman10e730a2015-06-29 23:51:55 +0000110 void addPostRegAlloc() override;
111 void addPreSched2() override;
112 void addPreEmitPass() override;
113};
114} // end anonymous namespace
115
116TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
Hans Wennborg9099b5e62015-09-16 23:59:57 +0000117 return TargetIRAnalysis([this](const Function &F) {
Dan Gohman10e730a2015-06-29 23:51:55 +0000118 return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
119 });
120}
121
122TargetPassConfig *
123WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
124 return new WebAssemblyPassConfig(this, PM);
125}
126
127FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
128 return nullptr; // No reg alloc
129}
130
Dan Gohman10e730a2015-06-29 23:51:55 +0000131//===----------------------------------------------------------------------===//
132// The following functions are called from lib/CodeGen/Passes.cpp to modify
133// the CodeGen pass sequence.
134//===----------------------------------------------------------------------===//
135
136void WebAssemblyPassConfig::addIRPasses() {
JF Bastien03855df2015-07-01 23:41:25 +0000137 // FIXME: the default for this option is currently POSIX, whereas
138 // WebAssembly's MVP should default to Single.
139 if (TM->Options.ThreadModel == ThreadModel::Single)
140 addPass(createLowerAtomicPass());
141 else
142 // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
143 // control specifically what gets lowered.
144 addPass(createAtomicExpandPass(TM));
Dan Gohman10e730a2015-06-29 23:51:55 +0000145
Dan Gohman81719f82015-11-25 16:55:01 +0000146 // Optimize "returned" function attributes.
147 addPass(createWebAssemblyOptimizeReturned());
148
Dan Gohman10e730a2015-06-29 23:51:55 +0000149 TargetPassConfig::addIRPasses();
150}
151
152bool WebAssemblyPassConfig::addPreISel() { return false; }
153
154bool WebAssemblyPassConfig::addInstSelector() {
155 addPass(
156 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
157 return false;
158}
159
160bool WebAssemblyPassConfig::addILPOpts() { return true; }
161
Dan Gohman4ba48162015-11-18 16:12:01 +0000162void WebAssemblyPassConfig::addPreRegAlloc() {
Dan Gohman81719f82015-11-25 16:55:01 +0000163 // Prepare store instructions for register stackifying.
164 addPass(createWebAssemblyStoreResults());
165
Dan Gohman4ba48162015-11-18 16:12:01 +0000166 // Mark registers as representing wasm's expression stack.
167 addPass(createWebAssemblyRegStackify());
168}
Dan Gohman10e730a2015-06-29 23:51:55 +0000169
JF Bastien600aee92015-07-31 17:53:38 +0000170void WebAssemblyPassConfig::addPostRegAlloc() {
171 // FIXME: the following passes dislike virtual registers. Disable them for now
172 // so that basic tests can pass. Future patches will remedy this.
173 //
174 // Fails with: Regalloc must assign all vregs.
175 disablePass(&PrologEpilogCodeInserterID);
176 // Fails with: should be run after register allocation.
177 disablePass(&MachineCopyPropagationID);
Dan Gohman950a13c2015-09-16 16:51:30 +0000178
179 // TODO: Until we get ReverseBranchCondition support, MachineBlockPlacement
180 // can create ugly-looking control flow.
181 disablePass(&MachineBlockPlacementID);
Dan Gohman4ba48162015-11-18 16:12:01 +0000182
183 // Run the register coloring pass to reduce the total number of registers.
184 addPass(createWebAssemblyRegColoring());
JF Bastien600aee92015-07-31 17:53:38 +0000185}
Dan Gohman10e730a2015-06-29 23:51:55 +0000186
187void WebAssemblyPassConfig::addPreSched2() {}
188
Dan Gohman950a13c2015-09-16 16:51:30 +0000189void WebAssemblyPassConfig::addPreEmitPass() {
190 addPass(createWebAssemblyCFGStackify());
Dan Gohmancf4748f2015-11-12 17:04:33 +0000191 addPass(createWebAssemblyRegNumbering());
Dan Gohman81719f82015-11-25 16:55:01 +0000192 addPass(createWebAssemblyPeephole());
Dan Gohman950a13c2015-09-16 16:51:30 +0000193}