WebAssemblyTargetMachine.cpp 24 KB

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