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