110e730a2SDan Gohman //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==// 210e730a2SDan Gohman // 310e730a2SDan Gohman // The LLVM Compiler Infrastructure 410e730a2SDan Gohman // 510e730a2SDan Gohman // This file is distributed under the University of Illinois Open Source 610e730a2SDan Gohman // License. See LICENSE.TXT for details. 710e730a2SDan Gohman // 810e730a2SDan Gohman //===----------------------------------------------------------------------===// 910e730a2SDan Gohman /// 1010e730a2SDan Gohman /// \file 1110e730a2SDan Gohman /// \brief This file defines the WebAssembly-specific subclass of TargetMachine. 1210e730a2SDan Gohman /// 1310e730a2SDan Gohman //===----------------------------------------------------------------------===// 1410e730a2SDan Gohman 1510e730a2SDan Gohman #include "WebAssembly.h" 1610e730a2SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 1710e730a2SDan Gohman #include "WebAssemblyTargetMachine.h" 1810e730a2SDan Gohman #include "WebAssemblyTargetObjectFile.h" 1910e730a2SDan Gohman #include "WebAssemblyTargetTransformInfo.h" 2010e730a2SDan Gohman #include "llvm/CodeGen/MachineFunctionPass.h" 2110e730a2SDan Gohman #include "llvm/CodeGen/Passes.h" 2210e730a2SDan Gohman #include "llvm/CodeGen/RegAllocRegistry.h" 2310e730a2SDan Gohman #include "llvm/IR/Function.h" 2410e730a2SDan Gohman #include "llvm/Support/CommandLine.h" 2510e730a2SDan Gohman #include "llvm/Support/TargetRegistry.h" 2610e730a2SDan Gohman #include "llvm/Target/TargetOptions.h" 2703855df1SJF Bastien #include "llvm/Transforms/Scalar.h" 2810e730a2SDan Gohman using namespace llvm; 2910e730a2SDan Gohman 3010e730a2SDan Gohman #define DEBUG_TYPE "wasm" 3110e730a2SDan Gohman 3210e730a2SDan Gohman extern "C" void LLVMInitializeWebAssemblyTarget() { 3310e730a2SDan Gohman // Register the target. 34d82494bbSDan Gohman RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32); 35d82494bbSDan Gohman RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64); 3610e730a2SDan Gohman } 3710e730a2SDan Gohman 3810e730a2SDan Gohman //===----------------------------------------------------------------------===// 3910e730a2SDan Gohman // WebAssembly Lowering public interface. 4010e730a2SDan Gohman //===----------------------------------------------------------------------===// 4110e730a2SDan Gohman 4210e730a2SDan Gohman /// Create an WebAssembly architecture model. 4310e730a2SDan Gohman /// 4410e730a2SDan Gohman WebAssemblyTargetMachine::WebAssemblyTargetMachine( 4510e730a2SDan Gohman const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 4610e730a2SDan Gohman const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, 4710e730a2SDan Gohman CodeGenOpt::Level OL) 4810e730a2SDan Gohman : LLVMTargetMachine(T, TT.isArch64Bit() 49*dde8dce6SDan Gohman ? "e-p:64:64-i64:64-n32:64-S128" 50*dde8dce6SDan Gohman : "e-p:32:32-i64:64-n32:64-S128", 5110e730a2SDan Gohman TT, CPU, FS, Options, RM, CM, OL), 5210e730a2SDan Gohman TLOF(make_unique<WebAssemblyTargetObjectFile>()) { 5310e730a2SDan Gohman initAsmInfo(); 5410e730a2SDan Gohman 5510e730a2SDan Gohman // We need a reducible CFG, so disable some optimizations which tend to 5610e730a2SDan Gohman // introduce irreducibility. 5710e730a2SDan Gohman setRequiresStructuredCFG(true); 5810e730a2SDan Gohman } 5910e730a2SDan Gohman 6010e730a2SDan Gohman WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {} 6110e730a2SDan Gohman 6210e730a2SDan Gohman const WebAssemblySubtarget * 6310e730a2SDan Gohman WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { 6410e730a2SDan Gohman Attribute CPUAttr = F.getFnAttribute("target-cpu"); 6510e730a2SDan Gohman Attribute FSAttr = F.getFnAttribute("target-features"); 6610e730a2SDan Gohman 6710e730a2SDan Gohman std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 6810e730a2SDan Gohman ? CPUAttr.getValueAsString().str() 6910e730a2SDan Gohman : TargetCPU; 7010e730a2SDan Gohman std::string FS = !FSAttr.hasAttribute(Attribute::None) 7110e730a2SDan Gohman ? FSAttr.getValueAsString().str() 7210e730a2SDan Gohman : TargetFS; 7310e730a2SDan Gohman 7410e730a2SDan Gohman auto &I = SubtargetMap[CPU + FS]; 7510e730a2SDan Gohman if (!I) { 7610e730a2SDan Gohman // This needs to be done before we create a new subtarget since any 7710e730a2SDan Gohman // creation will depend on the TM and the code generation flags on the 7810e730a2SDan Gohman // function that reside in TargetOptions. 7910e730a2SDan Gohman resetTargetOptions(F); 803adc7ce9SRafael Espindola I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); 8110e730a2SDan Gohman } 8210e730a2SDan Gohman return I.get(); 8310e730a2SDan Gohman } 8410e730a2SDan Gohman 8510e730a2SDan Gohman namespace { 8610e730a2SDan Gohman /// WebAssembly Code Generator Pass Configuration Options. 8710e730a2SDan Gohman class WebAssemblyPassConfig final : public TargetPassConfig { 8810e730a2SDan Gohman public: 8910e730a2SDan Gohman WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM) 9010e730a2SDan Gohman : TargetPassConfig(TM, PM) {} 9110e730a2SDan Gohman 9210e730a2SDan Gohman WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { 9310e730a2SDan Gohman return getTM<WebAssemblyTargetMachine>(); 9410e730a2SDan Gohman } 9510e730a2SDan Gohman 9610e730a2SDan Gohman FunctionPass *createTargetRegisterAllocator(bool) override; 9710e730a2SDan Gohman void addFastRegAlloc(FunctionPass *RegAllocPass) override; 9810e730a2SDan Gohman void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override; 9910e730a2SDan Gohman 10010e730a2SDan Gohman void addIRPasses() override; 10110e730a2SDan Gohman bool addPreISel() override; 10210e730a2SDan Gohman bool addInstSelector() override; 10310e730a2SDan Gohman bool addILPOpts() override; 10410e730a2SDan Gohman void addPreRegAlloc() override; 10510e730a2SDan Gohman void addRegAllocPasses(bool Optimized); 10610e730a2SDan Gohman void addPostRegAlloc() override; 10710e730a2SDan Gohman void addPreSched2() override; 10810e730a2SDan Gohman void addPreEmitPass() override; 10910e730a2SDan Gohman }; 11010e730a2SDan Gohman } // end anonymous namespace 11110e730a2SDan Gohman 11210e730a2SDan Gohman TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() { 11310e730a2SDan Gohman return TargetIRAnalysis([this](Function &F) { 11410e730a2SDan Gohman return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); 11510e730a2SDan Gohman }); 11610e730a2SDan Gohman } 11710e730a2SDan Gohman 11810e730a2SDan Gohman TargetPassConfig * 11910e730a2SDan Gohman WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { 12010e730a2SDan Gohman return new WebAssemblyPassConfig(this, PM); 12110e730a2SDan Gohman } 12210e730a2SDan Gohman 12310e730a2SDan Gohman FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { 12410e730a2SDan Gohman return nullptr; // No reg alloc 12510e730a2SDan Gohman } 12610e730a2SDan Gohman 12710e730a2SDan Gohman void WebAssemblyPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) { 12810e730a2SDan Gohman assert(!RegAllocPass && "WebAssembly uses no regalloc!"); 12910e730a2SDan Gohman addRegAllocPasses(false); 13010e730a2SDan Gohman } 13110e730a2SDan Gohman 13210e730a2SDan Gohman void WebAssemblyPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) { 13310e730a2SDan Gohman assert(!RegAllocPass && "WebAssembly uses no regalloc!"); 13410e730a2SDan Gohman addRegAllocPasses(true); 13510e730a2SDan Gohman } 13610e730a2SDan Gohman 13710e730a2SDan Gohman //===----------------------------------------------------------------------===// 13810e730a2SDan Gohman // The following functions are called from lib/CodeGen/Passes.cpp to modify 13910e730a2SDan Gohman // the CodeGen pass sequence. 14010e730a2SDan Gohman //===----------------------------------------------------------------------===// 14110e730a2SDan Gohman 14210e730a2SDan Gohman void WebAssemblyPassConfig::addIRPasses() { 14303855df1SJF Bastien // FIXME: the default for this option is currently POSIX, whereas 14403855df1SJF Bastien // WebAssembly's MVP should default to Single. 14503855df1SJF Bastien if (TM->Options.ThreadModel == ThreadModel::Single) 14603855df1SJF Bastien addPass(createLowerAtomicPass()); 14703855df1SJF Bastien else 14810e730a2SDan Gohman // Expand some atomic operations. WebAssemblyTargetLowering has hooks which 14910e730a2SDan Gohman // control specifically what gets lowered. 15003855df1SJF Bastien addPass(createAtomicExpandPass(TM)); 15110e730a2SDan Gohman 15210e730a2SDan Gohman TargetPassConfig::addIRPasses(); 15310e730a2SDan Gohman } 15410e730a2SDan Gohman 15510e730a2SDan Gohman bool WebAssemblyPassConfig::addPreISel() { return false; } 15610e730a2SDan Gohman 15710e730a2SDan Gohman bool WebAssemblyPassConfig::addInstSelector() { 15810e730a2SDan Gohman addPass( 15910e730a2SDan Gohman createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); 16010e730a2SDan Gohman return false; 16110e730a2SDan Gohman } 16210e730a2SDan Gohman 16310e730a2SDan Gohman bool WebAssemblyPassConfig::addILPOpts() { return true; } 16410e730a2SDan Gohman 16510e730a2SDan Gohman void WebAssemblyPassConfig::addPreRegAlloc() {} 16610e730a2SDan Gohman 16710e730a2SDan Gohman void WebAssemblyPassConfig::addRegAllocPasses(bool Optimized) {} 16810e730a2SDan Gohman 169600aee98SJF Bastien void WebAssemblyPassConfig::addPostRegAlloc() { 170600aee98SJF Bastien // FIXME: the following passes dislike virtual registers. Disable them for now 171600aee98SJF Bastien // so that basic tests can pass. Future patches will remedy this. 172600aee98SJF Bastien // 173600aee98SJF Bastien // Fails with: Regalloc must assign all vregs. 174600aee98SJF Bastien disablePass(&PrologEpilogCodeInserterID); 175600aee98SJF Bastien // Fails with: should be run after register allocation. 176600aee98SJF Bastien disablePass(&MachineCopyPropagationID); 177600aee98SJF Bastien } 17810e730a2SDan Gohman 17910e730a2SDan Gohman void WebAssemblyPassConfig::addPreSched2() {} 18010e730a2SDan Gohman 18110e730a2SDan Gohman void WebAssemblyPassConfig::addPreEmitPass() {} 182