1 //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file defines the WebAssembly-specific subclass of TargetMachine. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "WebAssemblyTargetMachine.h" 15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 16 #include "TargetInfo/WebAssemblyTargetInfo.h" 17 #include "Utils/WebAssemblyUtilities.h" 18 #include "WebAssembly.h" 19 #include "WebAssemblyMachineFunctionInfo.h" 20 #include "WebAssemblyTargetObjectFile.h" 21 #include "WebAssemblyTargetTransformInfo.h" 22 #include "llvm/CodeGen/MIRParser/MIParser.h" 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/CodeGen/Passes.h" 25 #include "llvm/CodeGen/RegAllocRegistry.h" 26 #include "llvm/CodeGen/TargetPassConfig.h" 27 #include "llvm/IR/Function.h" 28 #include "llvm/InitializePasses.h" 29 #include "llvm/MC/MCAsmInfo.h" 30 #include "llvm/MC/TargetRegistry.h" 31 #include "llvm/Target/TargetOptions.h" 32 #include "llvm/Transforms/Scalar.h" 33 #include "llvm/Transforms/Scalar/LowerAtomicPass.h" 34 #include "llvm/Transforms/Utils.h" 35 #include <optional> 36 using namespace llvm; 37 38 #define DEBUG_TYPE "wasm" 39 40 // A command-line option to keep implicit locals 41 // for the purpose of testing with lit/llc ONLY. 42 // This produces output which is not valid WebAssembly, and is not supported 43 // by assemblers/disassemblers and other MC based tools. 44 static cl::opt<bool> WasmDisableExplicitLocals( 45 "wasm-disable-explicit-locals", cl::Hidden, 46 cl::desc("WebAssembly: output implicit locals in" 47 " instruction output for test purposes only."), 48 cl::init(false)); 49 50 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() { 51 // Register the target. 52 RegisterTargetMachine<WebAssemblyTargetMachine> X( 53 getTheWebAssemblyTarget32()); 54 RegisterTargetMachine<WebAssemblyTargetMachine> Y( 55 getTheWebAssemblyTarget64()); 56 57 // Register backend passes 58 auto &PR = *PassRegistry::getPassRegistry(); 59 initializeWebAssemblyAddMissingPrototypesPass(PR); 60 initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR); 61 initializeLowerGlobalDtorsLegacyPassPass(PR); 62 initializeFixFunctionBitcastsPass(PR); 63 initializeOptimizeReturnedPass(PR); 64 initializeWebAssemblyArgumentMovePass(PR); 65 initializeWebAssemblySetP2AlignOperandsPass(PR); 66 initializeWebAssemblyReplacePhysRegsPass(PR); 67 initializeWebAssemblyOptimizeLiveIntervalsPass(PR); 68 initializeWebAssemblyMemIntrinsicResultsPass(PR); 69 initializeWebAssemblyRegStackifyPass(PR); 70 initializeWebAssemblyRegColoringPass(PR); 71 initializeWebAssemblyNullifyDebugValueListsPass(PR); 72 initializeWebAssemblyFixIrreducibleControlFlowPass(PR); 73 initializeWebAssemblyLateEHPreparePass(PR); 74 initializeWebAssemblyExceptionInfoPass(PR); 75 initializeWebAssemblyCFGSortPass(PR); 76 initializeWebAssemblyCFGStackifyPass(PR); 77 initializeWebAssemblyExplicitLocalsPass(PR); 78 initializeWebAssemblyLowerBrUnlessPass(PR); 79 initializeWebAssemblyRegNumberingPass(PR); 80 initializeWebAssemblyDebugFixupPass(PR); 81 initializeWebAssemblyPeepholePass(PR); 82 initializeWebAssemblyMCLowerPrePassPass(PR); 83 initializeWebAssemblyLowerRefTypesIntPtrConvPass(PR); 84 initializeWebAssemblyFixBrTableDefaultsPass(PR); 85 } 86 87 //===----------------------------------------------------------------------===// 88 // WebAssembly Lowering public interface. 89 //===----------------------------------------------------------------------===// 90 91 static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM, 92 const Triple &TT) { 93 if (!RM) { 94 // Default to static relocation model. This should always be more optimial 95 // than PIC since the static linker can determine all global addresses and 96 // assume direct function calls. 97 return Reloc::Static; 98 } 99 100 if (!TT.isOSEmscripten()) { 101 // Relocation modes other than static are currently implemented in a way 102 // that only works for Emscripten, so disable them if we aren't targeting 103 // Emscripten. 104 return Reloc::Static; 105 } 106 107 return *RM; 108 } 109 110 /// Create an WebAssembly architecture model. 111 /// 112 WebAssemblyTargetMachine::WebAssemblyTargetMachine( 113 const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 114 const TargetOptions &Options, std::optional<Reloc::Model> RM, 115 std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) 116 : LLVMTargetMachine( 117 T, 118 TT.isArch64Bit() 119 ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-" 120 "f128:64-n32:64-S128-ni:1:10:20" 121 : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-" 122 "n32:64-S128-ni:1:10:20") 123 : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-" 124 "f128:64-n32:64-S128-ni:1:10:20" 125 : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-" 126 "n32:64-S128-ni:1:10:20"), 127 TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT), 128 getEffectiveCodeModel(CM, CodeModel::Large), OL), 129 TLOF(new WebAssemblyTargetObjectFile()) { 130 // WebAssembly type-checks instructions, but a noreturn function with a return 131 // type that doesn't match the context will cause a check failure. So we lower 132 // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's 133 // 'unreachable' instructions which is meant for that case. 134 this->Options.TrapUnreachable = true; 135 136 // WebAssembly treats each function as an independent unit. Force 137 // -ffunction-sections, effectively, so that we can emit them independently. 138 this->Options.FunctionSections = true; 139 this->Options.DataSections = true; 140 this->Options.UniqueSectionNames = true; 141 142 initAsmInfo(); 143 144 // Note that we don't use setRequiresStructuredCFG(true). It disables 145 // optimizations than we're ok with, and want, such as critical edge 146 // splitting and tail merging. 147 } 148 149 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor. 150 151 const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const { 152 return getSubtargetImpl(std::string(getTargetCPU()), 153 std::string(getTargetFeatureString())); 154 } 155 156 const WebAssemblySubtarget * 157 WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU, 158 std::string FS) const { 159 auto &I = SubtargetMap[CPU + FS]; 160 if (!I) { 161 I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); 162 } 163 return I.get(); 164 } 165 166 const WebAssemblySubtarget * 167 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { 168 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 169 Attribute FSAttr = F.getFnAttribute("target-features"); 170 171 std::string CPU = 172 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU; 173 std::string FS = 174 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS; 175 176 // This needs to be done before we create a new subtarget since any 177 // creation will depend on the TM and the code generation flags on the 178 // function that reside in TargetOptions. 179 resetTargetOptions(F); 180 181 return getSubtargetImpl(CPU, FS); 182 } 183 184 namespace { 185 186 class CoalesceFeaturesAndStripAtomics final : public ModulePass { 187 // Take the union of all features used in the module and use it for each 188 // function individually, since having multiple feature sets in one module 189 // currently does not make sense for WebAssembly. If atomics are not enabled, 190 // also strip atomic operations and thread local storage. 191 static char ID; 192 WebAssemblyTargetMachine *WasmTM; 193 194 public: 195 CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM) 196 : ModulePass(ID), WasmTM(WasmTM) {} 197 198 bool runOnModule(Module &M) override { 199 FeatureBitset Features = coalesceFeatures(M); 200 201 std::string FeatureStr = getFeatureString(Features); 202 WasmTM->setTargetFeatureString(FeatureStr); 203 for (auto &F : M) 204 replaceFeatures(F, FeatureStr); 205 206 bool StrippedAtomics = false; 207 bool StrippedTLS = false; 208 209 if (!Features[WebAssembly::FeatureAtomics]) { 210 StrippedAtomics = stripAtomics(M); 211 StrippedTLS = stripThreadLocals(M); 212 } else if (!Features[WebAssembly::FeatureBulkMemory]) { 213 StrippedTLS |= stripThreadLocals(M); 214 } 215 216 if (StrippedAtomics && !StrippedTLS) 217 stripThreadLocals(M); 218 else if (StrippedTLS && !StrippedAtomics) 219 stripAtomics(M); 220 221 recordFeatures(M, Features, StrippedAtomics || StrippedTLS); 222 223 // Conservatively assume we have made some change 224 return true; 225 } 226 227 private: 228 FeatureBitset coalesceFeatures(const Module &M) { 229 FeatureBitset Features = 230 WasmTM 231 ->getSubtargetImpl(std::string(WasmTM->getTargetCPU()), 232 std::string(WasmTM->getTargetFeatureString())) 233 ->getFeatureBits(); 234 for (auto &F : M) 235 Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits(); 236 return Features; 237 } 238 239 std::string getFeatureString(const FeatureBitset &Features) { 240 std::string Ret; 241 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 242 if (Features[KV.Value]) 243 Ret += (StringRef("+") + KV.Key + ",").str(); 244 } 245 return Ret; 246 } 247 248 void replaceFeatures(Function &F, const std::string &Features) { 249 F.removeFnAttr("target-features"); 250 F.removeFnAttr("target-cpu"); 251 F.addFnAttr("target-features", Features); 252 } 253 254 bool stripAtomics(Module &M) { 255 // Detect whether any atomics will be lowered, since there is no way to tell 256 // whether the LowerAtomic pass lowers e.g. stores. 257 bool Stripped = false; 258 for (auto &F : M) { 259 for (auto &B : F) { 260 for (auto &I : B) { 261 if (I.isAtomic()) { 262 Stripped = true; 263 goto done; 264 } 265 } 266 } 267 } 268 269 done: 270 if (!Stripped) 271 return false; 272 273 LowerAtomicPass Lowerer; 274 FunctionAnalysisManager FAM; 275 for (auto &F : M) 276 Lowerer.run(F, FAM); 277 278 return true; 279 } 280 281 bool stripThreadLocals(Module &M) { 282 bool Stripped = false; 283 for (auto &GV : M.globals()) { 284 if (GV.isThreadLocal()) { 285 Stripped = true; 286 GV.setThreadLocal(false); 287 } 288 } 289 return Stripped; 290 } 291 292 void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) { 293 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 294 if (Features[KV.Value]) { 295 // Mark features as used 296 std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str(); 297 M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, 298 wasm::WASM_FEATURE_PREFIX_USED); 299 } 300 } 301 // Code compiled without atomics or bulk-memory may have had its atomics or 302 // thread-local data lowered to nonatomic operations or non-thread-local 303 // data. In that case, we mark the pseudo-feature "shared-mem" as disallowed 304 // to tell the linker that it would be unsafe to allow this code ot be used 305 // in a module with shared memory. 306 if (Stripped) { 307 M.addModuleFlag(Module::ModFlagBehavior::Error, "wasm-feature-shared-mem", 308 wasm::WASM_FEATURE_PREFIX_DISALLOWED); 309 } 310 } 311 }; 312 char CoalesceFeaturesAndStripAtomics::ID = 0; 313 314 /// WebAssembly Code Generator Pass Configuration Options. 315 class WebAssemblyPassConfig final : public TargetPassConfig { 316 public: 317 WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM) 318 : TargetPassConfig(TM, PM) {} 319 320 WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { 321 return getTM<WebAssemblyTargetMachine>(); 322 } 323 324 FunctionPass *createTargetRegisterAllocator(bool) override; 325 326 void addIRPasses() override; 327 void addISelPrepare() override; 328 bool addInstSelector() override; 329 void addOptimizedRegAlloc() override; 330 void addPostRegAlloc() override; 331 bool addGCPasses() override { return false; } 332 void addPreEmitPass() override; 333 bool addPreISel() override; 334 335 // No reg alloc 336 bool addRegAssignAndRewriteFast() override { return false; } 337 338 // No reg alloc 339 bool addRegAssignAndRewriteOptimized() override { return false; } 340 }; 341 } // end anonymous namespace 342 343 TargetTransformInfo 344 WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) const { 345 return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); 346 } 347 348 TargetPassConfig * 349 WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { 350 return new WebAssemblyPassConfig(*this, PM); 351 } 352 353 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { 354 return nullptr; // No reg alloc 355 } 356 357 using WebAssembly::WasmEnableEH; 358 using WebAssembly::WasmEnableEmEH; 359 using WebAssembly::WasmEnableEmSjLj; 360 using WebAssembly::WasmEnableSjLj; 361 362 static void basicCheckForEHAndSjLj(TargetMachine *TM) { 363 // Before checking, we make sure TargetOptions.ExceptionModel is the same as 364 // MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang 365 // stores the exception model info in LangOptions, which is later transferred 366 // to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly, 367 // clang's LangOptions is not used and thus the exception model info is not 368 // correctly transferred to TargetOptions and MCAsmInfo, so we make sure we 369 // have the correct exception model in in WebAssemblyMCAsmInfo constructor. 370 // But in this case TargetOptions is still not updated, so we make sure they 371 // are the same. 372 TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType(); 373 374 // Basic Correctness checking related to -exception-model 375 if (TM->Options.ExceptionModel != ExceptionHandling::None && 376 TM->Options.ExceptionModel != ExceptionHandling::Wasm) 377 report_fatal_error("-exception-model should be either 'none' or 'wasm'"); 378 if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm) 379 report_fatal_error("-exception-model=wasm not allowed with " 380 "-enable-emscripten-cxx-exceptions"); 381 if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm) 382 report_fatal_error( 383 "-wasm-enable-eh only allowed with -exception-model=wasm"); 384 if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm) 385 report_fatal_error( 386 "-wasm-enable-sjlj only allowed with -exception-model=wasm"); 387 if ((!WasmEnableEH && !WasmEnableSjLj) && 388 TM->Options.ExceptionModel == ExceptionHandling::Wasm) 389 report_fatal_error( 390 "-exception-model=wasm only allowed with at least one of " 391 "-wasm-enable-eh or -wasm-enable-sjj"); 392 393 // You can't enable two modes of EH at the same time 394 if (WasmEnableEmEH && WasmEnableEH) 395 report_fatal_error( 396 "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh"); 397 // You can't enable two modes of SjLj at the same time 398 if (WasmEnableEmSjLj && WasmEnableSjLj) 399 report_fatal_error( 400 "-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj"); 401 // You can't mix Emscripten EH with Wasm SjLj. 402 if (WasmEnableEmEH && WasmEnableSjLj) 403 report_fatal_error( 404 "-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj"); 405 // Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim 406 // measure, but some code will error out at compile time in this combination. 407 // See WebAssemblyLowerEmscriptenEHSjLj pass for details. 408 } 409 410 //===----------------------------------------------------------------------===// 411 // The following functions are called from lib/CodeGen/Passes.cpp to modify 412 // the CodeGen pass sequence. 413 //===----------------------------------------------------------------------===// 414 415 void WebAssemblyPassConfig::addIRPasses() { 416 // Add signatures to prototype-less function declarations 417 addPass(createWebAssemblyAddMissingPrototypes()); 418 419 // Lower .llvm.global_dtors into .llvm.global_ctors with __cxa_atexit calls. 420 addPass(createLowerGlobalDtorsLegacyPass()); 421 422 // Fix function bitcasts, as WebAssembly requires caller and callee signatures 423 // to match. 424 addPass(createWebAssemblyFixFunctionBitcasts()); 425 426 // Optimize "returned" function attributes. 427 if (getOptLevel() != CodeGenOpt::None) 428 addPass(createWebAssemblyOptimizeReturned()); 429 430 basicCheckForEHAndSjLj(TM); 431 432 // If exception handling is not enabled and setjmp/longjmp handling is 433 // enabled, we lower invokes into calls and delete unreachable landingpad 434 // blocks. Lowering invokes when there is no EH support is done in 435 // TargetPassConfig::addPassesToHandleExceptions, but that runs after these IR 436 // passes and Emscripten SjLj handling expects all invokes to be lowered 437 // before. 438 if (!WasmEnableEmEH && !WasmEnableEH) { 439 addPass(createLowerInvokePass()); 440 // The lower invoke pass may create unreachable code. Remove it in order not 441 // to process dead blocks in setjmp/longjmp handling. 442 addPass(createUnreachableBlockEliminationPass()); 443 } 444 445 // Handle exceptions and setjmp/longjmp if enabled. Unlike Wasm EH preparation 446 // done in WasmEHPrepare pass, Wasm SjLj preparation shares libraries and 447 // transformation algorithms with Emscripten SjLj, so we run 448 // LowerEmscriptenEHSjLj pass also when Wasm SjLj is enabled. 449 if (WasmEnableEmEH || WasmEnableEmSjLj || WasmEnableSjLj) 450 addPass(createWebAssemblyLowerEmscriptenEHSjLj()); 451 452 // Expand indirectbr instructions to switches. 453 addPass(createIndirectBrExpandPass()); 454 455 TargetPassConfig::addIRPasses(); 456 } 457 458 void WebAssemblyPassConfig::addISelPrepare() { 459 // Lower atomics and TLS if necessary 460 addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine())); 461 462 // This is a no-op if atomics are not used in the module 463 addPass(createAtomicExpandPass()); 464 465 TargetPassConfig::addISelPrepare(); 466 } 467 468 bool WebAssemblyPassConfig::addInstSelector() { 469 (void)TargetPassConfig::addInstSelector(); 470 addPass( 471 createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); 472 // Run the argument-move pass immediately after the ScheduleDAG scheduler 473 // so that we can fix up the ARGUMENT instructions before anything else 474 // sees them in the wrong place. 475 addPass(createWebAssemblyArgumentMove()); 476 // Set the p2align operands. This information is present during ISel, however 477 // it's inconvenient to collect. Collect it now, and update the immediate 478 // operands. 479 addPass(createWebAssemblySetP2AlignOperands()); 480 481 // Eliminate range checks and add default targets to br_table instructions. 482 addPass(createWebAssemblyFixBrTableDefaults()); 483 484 return false; 485 } 486 487 void WebAssemblyPassConfig::addOptimizedRegAlloc() { 488 // Currently RegisterCoalesce degrades wasm debug info quality by a 489 // significant margin. As a quick fix, disable this for -O1, which is often 490 // used for debugging large applications. Disabling this increases code size 491 // of Emscripten core benchmarks by ~5%, which is acceptable for -O1, which is 492 // usually not used for production builds. 493 // TODO Investigate why RegisterCoalesce degrades debug info quality and fix 494 // it properly 495 if (getOptLevel() == CodeGenOpt::Less) 496 disablePass(&RegisterCoalescerID); 497 TargetPassConfig::addOptimizedRegAlloc(); 498 } 499 500 void WebAssemblyPassConfig::addPostRegAlloc() { 501 // TODO: The following CodeGen passes don't currently support code containing 502 // virtual registers. Consider removing their restrictions and re-enabling 503 // them. 504 505 // These functions all require the NoVRegs property. 506 disablePass(&MachineLateInstrsCleanupID); 507 disablePass(&MachineCopyPropagationID); 508 disablePass(&PostRAMachineSinkingID); 509 disablePass(&PostRASchedulerID); 510 disablePass(&FuncletLayoutID); 511 disablePass(&StackMapLivenessID); 512 disablePass(&LiveDebugValuesID); 513 disablePass(&PatchableFunctionID); 514 disablePass(&ShrinkWrapID); 515 516 // This pass hurts code size for wasm because it can generate irreducible 517 // control flow. 518 disablePass(&MachineBlockPlacementID); 519 520 TargetPassConfig::addPostRegAlloc(); 521 } 522 523 void WebAssemblyPassConfig::addPreEmitPass() { 524 TargetPassConfig::addPreEmitPass(); 525 526 // Nullify DBG_VALUE_LISTs that we cannot handle. 527 addPass(createWebAssemblyNullifyDebugValueLists()); 528 529 // Eliminate multiple-entry loops. 530 addPass(createWebAssemblyFixIrreducibleControlFlow()); 531 532 // Do various transformations for exception handling. 533 // Every CFG-changing optimizations should come before this. 534 if (TM->Options.ExceptionModel == ExceptionHandling::Wasm) 535 addPass(createWebAssemblyLateEHPrepare()); 536 537 // Now that we have a prologue and epilogue and all frame indices are 538 // rewritten, eliminate SP and FP. This allows them to be stackified, 539 // colored, and numbered with the rest of the registers. 540 addPass(createWebAssemblyReplacePhysRegs()); 541 542 // Preparations and optimizations related to register stackification. 543 if (getOptLevel() != CodeGenOpt::None) { 544 // Depend on LiveIntervals and perform some optimizations on it. 545 addPass(createWebAssemblyOptimizeLiveIntervals()); 546 547 // Prepare memory intrinsic calls for register stackifying. 548 addPass(createWebAssemblyMemIntrinsicResults()); 549 550 // Mark registers as representing wasm's value stack. This is a key 551 // code-compression technique in WebAssembly. We run this pass (and 552 // MemIntrinsicResults above) very late, so that it sees as much code as 553 // possible, including code emitted by PEI and expanded by late tail 554 // duplication. 555 addPass(createWebAssemblyRegStackify()); 556 557 // Run the register coloring pass to reduce the total number of registers. 558 // This runs after stackification so that it doesn't consider registers 559 // that become stackified. 560 addPass(createWebAssemblyRegColoring()); 561 } 562 563 // Sort the blocks of the CFG into topological order, a prerequisite for 564 // BLOCK and LOOP markers. 565 addPass(createWebAssemblyCFGSort()); 566 567 // Insert BLOCK and LOOP markers. 568 addPass(createWebAssemblyCFGStackify()); 569 570 // Insert explicit local.get and local.set operators. 571 if (!WasmDisableExplicitLocals) 572 addPass(createWebAssemblyExplicitLocals()); 573 574 // Lower br_unless into br_if. 575 addPass(createWebAssemblyLowerBrUnless()); 576 577 // Perform the very last peephole optimizations on the code. 578 if (getOptLevel() != CodeGenOpt::None) 579 addPass(createWebAssemblyPeephole()); 580 581 // Create a mapping from LLVM CodeGen virtual registers to wasm registers. 582 addPass(createWebAssemblyRegNumbering()); 583 584 // Fix debug_values whose defs have been stackified. 585 if (!WasmDisableExplicitLocals) 586 addPass(createWebAssemblyDebugFixup()); 587 588 // Collect information to prepare for MC lowering / asm printing. 589 addPass(createWebAssemblyMCLowerPrePass()); 590 } 591 592 bool WebAssemblyPassConfig::addPreISel() { 593 TargetPassConfig::addPreISel(); 594 addPass(createWebAssemblyLowerRefTypesIntPtrConv()); 595 return false; 596 } 597 598 yaml::MachineFunctionInfo * 599 WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const { 600 return new yaml::WebAssemblyFunctionInfo(); 601 } 602 603 yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML( 604 const MachineFunction &MF) const { 605 const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); 606 return new yaml::WebAssemblyFunctionInfo(MF, *MFI); 607 } 608 609 bool WebAssemblyTargetMachine::parseMachineFunctionInfo( 610 const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS, 611 SMDiagnostic &Error, SMRange &SourceRange) const { 612 const auto &YamlMFI = static_cast<const yaml::WebAssemblyFunctionInfo &>(MFI); 613 MachineFunction &MF = PFS.MF; 614 MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(MF, YamlMFI); 615 return false; 616 } 617