1 //===- 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" 23 #include "llvm/CodeGen/TargetPassConfig.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/Support/TargetRegistry.h" 26 #include "llvm/Target/TargetOptions.h" 27 #include "llvm/Transforms/Scalar.h" 28 using namespace llvm; 29 30 #define DEBUG_TYPE "wasm" 31 32 extern "C" void LLVMInitializeWebAssemblyTarget() { 33 // Register the target. 34 RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32); 35 RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64); 36 } 37 38 //===----------------------------------------------------------------------===// 39 // WebAssembly Lowering public interface. 40 //===----------------------------------------------------------------------===// 41 42 /// Create an WebAssembly architecture model. 43 /// 44 WebAssemblyTargetMachine::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) 48 : 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", 51 TT, CPU, FS, Options, RM, CM, OL), 52 TLOF(make_unique<WebAssemblyTargetObjectFile>()) { 53 // 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 59 initAsmInfo(); 60 61 // 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. 64 } 65 66 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {} 67 68 const WebAssemblySubtarget * 69 WebAssemblyTargetMachine::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); 86 I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); 87 } 88 return I.get(); 89 } 90 91 namespace { 92 /// WebAssembly Code Generator Pass Configuration Options. 93 class WebAssemblyPassConfig final : public TargetPassConfig { 94 public: 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; 103 104 void addIRPasses() override; 105 bool addInstSelector() override; 106 bool addILPOpts() override; 107 void addPreRegAlloc() override; 108 void addPostRegAlloc() override; 109 bool addGCPasses() override { return false; } 110 void addPreEmitPass() override; 111 }; 112 } // end anonymous namespace 113 114 TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() { 115 return TargetIRAnalysis([this](const Function &F) { 116 return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); 117 }); 118 } 119 120 TargetPassConfig * 121 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { 122 return new WebAssemblyPassConfig(this, PM); 123 } 124 125 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { 126 return nullptr; // No reg alloc 127 } 128 129 //===----------------------------------------------------------------------===// 130 // The following functions are called from lib/CodeGen/Passes.cpp to modify 131 // the CodeGen pass sequence. 132 //===----------------------------------------------------------------------===// 133 134 void WebAssemblyPassConfig::addIRPasses() { 135 if (TM->Options.ThreadModel == ThreadModel::Single) 136 // In "single" mode, atomics get lowered to non-atomics. 137 addPass(createLowerAtomicPass()); 138 else 139 // Expand some atomic operations. WebAssemblyTargetLowering has hooks which 140 // control specifically what gets lowered. 141 addPass(createAtomicExpandPass(TM)); 142 143 // Optimize "returned" function attributes. 144 if (getOptLevel() != CodeGenOpt::None) 145 addPass(createWebAssemblyOptimizeReturned()); 146 147 TargetPassConfig::addIRPasses(); 148 } 149 150 bool WebAssemblyPassConfig::addInstSelector() { 151 (void)TargetPassConfig::addInstSelector(); 152 addPass( 153 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); 154 // Run the argument-move pass immediately after the ScheduleDAG scheduler 155 // so that we can fix up the ARGUMENT instructions before anything else 156 // sees them in the wrong place. 157 addPass(createWebAssemblyArgumentMove()); 158 // Set the p2align operands. This information is present during ISel, however 159 // it's inconvenient to collect. Collect it now, and update the immediate 160 // operands. 161 addPass(createWebAssemblySetP2AlignOperands()); 162 return false; 163 } 164 165 bool WebAssemblyPassConfig::addILPOpts() { 166 (void)TargetPassConfig::addILPOpts(); 167 return true; 168 } 169 170 void WebAssemblyPassConfig::addPreRegAlloc() { 171 TargetPassConfig::addPreRegAlloc(); 172 173 // Prepare store instructions for register stackifying. 174 if (getOptLevel() != CodeGenOpt::None) 175 addPass(createWebAssemblyStoreResults()); 176 } 177 178 void WebAssemblyPassConfig::addPostRegAlloc() { 179 // TODO: The following CodeGen passes don't currently support code containing 180 // virtual registers. Consider removing their restrictions and re-enabling 181 // them. 182 183 // Has no asserts of its own, but was not written to handle virtual regs. 184 disablePass(&ShrinkWrapID); 185 // We use our own PrologEpilogInserter which is very slightly modified to 186 // tolerate virtual registers. 187 disablePass(&PrologEpilogCodeInserterID); 188 189 // These functions all require the AllVRegsAllocated property. 190 disablePass(&MachineCopyPropagationID); 191 disablePass(&PostRASchedulerID); 192 disablePass(&FuncletLayoutID); 193 disablePass(&StackMapLivenessID); 194 disablePass(&LiveDebugValuesID); 195 disablePass(&PatchableFunctionID); 196 197 if (getOptLevel() != CodeGenOpt::None) { 198 // Mark registers as representing wasm's expression stack. 199 addPass(createWebAssemblyRegStackify()); 200 201 // Run the register coloring pass to reduce the total number of registers. 202 addPass(createWebAssemblyRegColoring()); 203 } 204 205 TargetPassConfig::addPostRegAlloc(); 206 207 // Run WebAssembly's version of the PrologEpilogInserter. Target-independent 208 // PEI runs after PostRegAlloc and after ShrinkWrap. Putting it here will run 209 // PEI before ShrinkWrap but otherwise in the same position in the order. 210 addPass(createWebAssemblyPEI()); 211 } 212 213 void WebAssemblyPassConfig::addPreEmitPass() { 214 TargetPassConfig::addPreEmitPass(); 215 216 // Eliminate multiple-entry loops. 217 addPass(createWebAssemblyFixIrreducibleControlFlow()); 218 219 // Put the CFG in structured form; insert BLOCK and LOOP markers. 220 addPass(createWebAssemblyCFGStackify()); 221 222 // Lower br_unless into br_if. 223 addPass(createWebAssemblyLowerBrUnless()); 224 225 // Create a mapping from LLVM CodeGen virtual registers to wasm registers. 226 addPass(createWebAssemblyRegNumbering()); 227 228 // Perform the very last peephole optimizations on the code. 229 if (getOptLevel() != CodeGenOpt::None) 230 addPass(createWebAssemblyPeephole()); 231 } 232