1*9880d681SAndroid Build Coastguard Worker //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief This file defines the WebAssembly-specific subclass of TargetMachine.
12*9880d681SAndroid Build Coastguard Worker ///
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "WebAssembly.h"
16*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyTargetMachine.h"
18*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyTargetObjectFile.h"
19*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyTargetTransformInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocRegistry.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetPassConfig.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
28*9880d681SAndroid Build Coastguard Worker using namespace llvm;
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "wasm"
31*9880d681SAndroid Build Coastguard Worker
LLVMInitializeWebAssemblyTarget()32*9880d681SAndroid Build Coastguard Worker extern "C" void LLVMInitializeWebAssemblyTarget() {
33*9880d681SAndroid Build Coastguard Worker // Register the target.
34*9880d681SAndroid Build Coastguard Worker RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
35*9880d681SAndroid Build Coastguard Worker RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
36*9880d681SAndroid Build Coastguard Worker }
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
39*9880d681SAndroid Build Coastguard Worker // WebAssembly Lowering public interface.
40*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
41*9880d681SAndroid Build Coastguard Worker
getEffectiveRelocModel(Optional<Reloc::Model> RM)42*9880d681SAndroid Build Coastguard Worker static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
43*9880d681SAndroid Build Coastguard Worker if (!RM.hasValue())
44*9880d681SAndroid Build Coastguard Worker return Reloc::PIC_;
45*9880d681SAndroid Build Coastguard Worker return *RM;
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker /// Create an WebAssembly architecture model.
49*9880d681SAndroid Build Coastguard Worker ///
WebAssemblyTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)50*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetMachine::WebAssemblyTargetMachine(
51*9880d681SAndroid Build Coastguard Worker const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
52*9880d681SAndroid Build Coastguard Worker const TargetOptions &Options, Optional<Reloc::Model> RM,
53*9880d681SAndroid Build Coastguard Worker CodeModel::Model CM, CodeGenOpt::Level OL)
54*9880d681SAndroid Build Coastguard Worker : LLVMTargetMachine(T,
55*9880d681SAndroid Build Coastguard Worker TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
56*9880d681SAndroid Build Coastguard Worker : "e-m:e-p:32:32-i64:64-n32:64-S128",
57*9880d681SAndroid Build Coastguard Worker TT, CPU, FS, Options, getEffectiveRelocModel(RM),
58*9880d681SAndroid Build Coastguard Worker CM, OL),
59*9880d681SAndroid Build Coastguard Worker TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
60*9880d681SAndroid Build Coastguard Worker // WebAssembly type-checks expressions, but a noreturn function with a return
61*9880d681SAndroid Build Coastguard Worker // type that doesn't match the context will cause a check failure. So we lower
62*9880d681SAndroid Build Coastguard Worker // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
63*9880d681SAndroid Build Coastguard Worker // 'unreachable' expression which is meant for that case.
64*9880d681SAndroid Build Coastguard Worker this->Options.TrapUnreachable = true;
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker initAsmInfo();
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker // Note that we don't use setRequiresStructuredCFG(true). It disables
69*9880d681SAndroid Build Coastguard Worker // optimizations than we're ok with, and want, such as critical edge
70*9880d681SAndroid Build Coastguard Worker // splitting and tail merging.
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
~WebAssemblyTargetMachine()73*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker const WebAssemblySubtarget *
getSubtargetImpl(const Function & F) const76*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
77*9880d681SAndroid Build Coastguard Worker Attribute CPUAttr = F.getFnAttribute("target-cpu");
78*9880d681SAndroid Build Coastguard Worker Attribute FSAttr = F.getFnAttribute("target-features");
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
81*9880d681SAndroid Build Coastguard Worker ? CPUAttr.getValueAsString().str()
82*9880d681SAndroid Build Coastguard Worker : TargetCPU;
83*9880d681SAndroid Build Coastguard Worker std::string FS = !FSAttr.hasAttribute(Attribute::None)
84*9880d681SAndroid Build Coastguard Worker ? FSAttr.getValueAsString().str()
85*9880d681SAndroid Build Coastguard Worker : TargetFS;
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker auto &I = SubtargetMap[CPU + FS];
88*9880d681SAndroid Build Coastguard Worker if (!I) {
89*9880d681SAndroid Build Coastguard Worker // This needs to be done before we create a new subtarget since any
90*9880d681SAndroid Build Coastguard Worker // creation will depend on the TM and the code generation flags on the
91*9880d681SAndroid Build Coastguard Worker // function that reside in TargetOptions.
92*9880d681SAndroid Build Coastguard Worker resetTargetOptions(F);
93*9880d681SAndroid Build Coastguard Worker I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker return I.get();
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker namespace {
99*9880d681SAndroid Build Coastguard Worker /// WebAssembly Code Generator Pass Configuration Options.
100*9880d681SAndroid Build Coastguard Worker class WebAssemblyPassConfig final : public TargetPassConfig {
101*9880d681SAndroid Build Coastguard Worker public:
WebAssemblyPassConfig(WebAssemblyTargetMachine * TM,PassManagerBase & PM)102*9880d681SAndroid Build Coastguard Worker WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
103*9880d681SAndroid Build Coastguard Worker : TargetPassConfig(TM, PM) {}
104*9880d681SAndroid Build Coastguard Worker
getWebAssemblyTargetMachine() const105*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
106*9880d681SAndroid Build Coastguard Worker return getTM<WebAssemblyTargetMachine>();
107*9880d681SAndroid Build Coastguard Worker }
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker FunctionPass *createTargetRegisterAllocator(bool) override;
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker void addIRPasses() override;
112*9880d681SAndroid Build Coastguard Worker bool addInstSelector() override;
113*9880d681SAndroid Build Coastguard Worker void addPostRegAlloc() override;
addGCPasses()114*9880d681SAndroid Build Coastguard Worker bool addGCPasses() override { return false; }
115*9880d681SAndroid Build Coastguard Worker void addPreEmitPass() override;
116*9880d681SAndroid Build Coastguard Worker };
117*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
118*9880d681SAndroid Build Coastguard Worker
getTargetIRAnalysis()119*9880d681SAndroid Build Coastguard Worker TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
120*9880d681SAndroid Build Coastguard Worker return TargetIRAnalysis([this](const Function &F) {
121*9880d681SAndroid Build Coastguard Worker return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
122*9880d681SAndroid Build Coastguard Worker });
123*9880d681SAndroid Build Coastguard Worker }
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker TargetPassConfig *
createPassConfig(PassManagerBase & PM)126*9880d681SAndroid Build Coastguard Worker WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
127*9880d681SAndroid Build Coastguard Worker return new WebAssemblyPassConfig(this, PM);
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
createTargetRegisterAllocator(bool)130*9880d681SAndroid Build Coastguard Worker FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
131*9880d681SAndroid Build Coastguard Worker return nullptr; // No reg alloc
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
135*9880d681SAndroid Build Coastguard Worker // The following functions are called from lib/CodeGen/Passes.cpp to modify
136*9880d681SAndroid Build Coastguard Worker // the CodeGen pass sequence.
137*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
138*9880d681SAndroid Build Coastguard Worker
addIRPasses()139*9880d681SAndroid Build Coastguard Worker void WebAssemblyPassConfig::addIRPasses() {
140*9880d681SAndroid Build Coastguard Worker if (TM->Options.ThreadModel == ThreadModel::Single)
141*9880d681SAndroid Build Coastguard Worker // In "single" mode, atomics get lowered to non-atomics.
142*9880d681SAndroid Build Coastguard Worker addPass(createLowerAtomicPass());
143*9880d681SAndroid Build Coastguard Worker else
144*9880d681SAndroid Build Coastguard Worker // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
145*9880d681SAndroid Build Coastguard Worker // control specifically what gets lowered.
146*9880d681SAndroid Build Coastguard Worker addPass(createAtomicExpandPass(TM));
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker // Optimize "returned" function attributes.
149*9880d681SAndroid Build Coastguard Worker if (getOptLevel() != CodeGenOpt::None)
150*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyOptimizeReturned());
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker TargetPassConfig::addIRPasses();
153*9880d681SAndroid Build Coastguard Worker }
154*9880d681SAndroid Build Coastguard Worker
addInstSelector()155*9880d681SAndroid Build Coastguard Worker bool WebAssemblyPassConfig::addInstSelector() {
156*9880d681SAndroid Build Coastguard Worker (void)TargetPassConfig::addInstSelector();
157*9880d681SAndroid Build Coastguard Worker addPass(
158*9880d681SAndroid Build Coastguard Worker createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
159*9880d681SAndroid Build Coastguard Worker // Run the argument-move pass immediately after the ScheduleDAG scheduler
160*9880d681SAndroid Build Coastguard Worker // so that we can fix up the ARGUMENT instructions before anything else
161*9880d681SAndroid Build Coastguard Worker // sees them in the wrong place.
162*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyArgumentMove());
163*9880d681SAndroid Build Coastguard Worker // Set the p2align operands. This information is present during ISel, however
164*9880d681SAndroid Build Coastguard Worker // it's inconvenient to collect. Collect it now, and update the immediate
165*9880d681SAndroid Build Coastguard Worker // operands.
166*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblySetP2AlignOperands());
167*9880d681SAndroid Build Coastguard Worker return false;
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker
addPostRegAlloc()170*9880d681SAndroid Build Coastguard Worker void WebAssemblyPassConfig::addPostRegAlloc() {
171*9880d681SAndroid Build Coastguard Worker // TODO: The following CodeGen passes don't currently support code containing
172*9880d681SAndroid Build Coastguard Worker // virtual registers. Consider removing their restrictions and re-enabling
173*9880d681SAndroid Build Coastguard Worker // them.
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker // Has no asserts of its own, but was not written to handle virtual regs.
176*9880d681SAndroid Build Coastguard Worker disablePass(&ShrinkWrapID);
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker // These functions all require the AllVRegsAllocated property.
179*9880d681SAndroid Build Coastguard Worker disablePass(&MachineCopyPropagationID);
180*9880d681SAndroid Build Coastguard Worker disablePass(&PostRASchedulerID);
181*9880d681SAndroid Build Coastguard Worker disablePass(&FuncletLayoutID);
182*9880d681SAndroid Build Coastguard Worker disablePass(&StackMapLivenessID);
183*9880d681SAndroid Build Coastguard Worker disablePass(&LiveDebugValuesID);
184*9880d681SAndroid Build Coastguard Worker disablePass(&PatchableFunctionID);
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker TargetPassConfig::addPostRegAlloc();
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker
addPreEmitPass()189*9880d681SAndroid Build Coastguard Worker void WebAssemblyPassConfig::addPreEmitPass() {
190*9880d681SAndroid Build Coastguard Worker TargetPassConfig::addPreEmitPass();
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker // Now that we have a prologue and epilogue and all frame indices are
193*9880d681SAndroid Build Coastguard Worker // rewritten, eliminate SP and FP. This allows them to be stackified,
194*9880d681SAndroid Build Coastguard Worker // colored, and numbered with the rest of the registers.
195*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyReplacePhysRegs());
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker if (getOptLevel() != CodeGenOpt::None) {
198*9880d681SAndroid Build Coastguard Worker // LiveIntervals isn't commonly run this late. Re-establish preconditions.
199*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyPrepareForLiveIntervals());
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker // Depend on LiveIntervals and perform some optimizations on it.
202*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyOptimizeLiveIntervals());
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker // Prepare store instructions for register stackifying.
205*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyStoreResults());
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker // Mark registers as representing wasm's expression stack. This is a key
208*9880d681SAndroid Build Coastguard Worker // code-compression technique in WebAssembly. We run this pass (and
209*9880d681SAndroid Build Coastguard Worker // StoreResults above) very late, so that it sees as much code as possible,
210*9880d681SAndroid Build Coastguard Worker // including code emitted by PEI and expanded by late tail duplication.
211*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyRegStackify());
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker // Run the register coloring pass to reduce the total number of registers.
214*9880d681SAndroid Build Coastguard Worker // This runs after stackification so that it doesn't consider registers
215*9880d681SAndroid Build Coastguard Worker // that become stackified.
216*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyRegColoring());
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker // Eliminate multiple-entry loops.
220*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyFixIrreducibleControlFlow());
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker // Put the CFG in structured form; insert BLOCK and LOOP markers.
223*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyCFGStackify());
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker // Lower br_unless into br_if.
226*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyLowerBrUnless());
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker // Perform the very last peephole optimizations on the code.
229*9880d681SAndroid Build Coastguard Worker if (getOptLevel() != CodeGenOpt::None)
230*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyPeephole());
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
233*9880d681SAndroid Build Coastguard Worker addPass(createWebAssemblyRegNumbering());
234*9880d681SAndroid Build Coastguard Worker }
235