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