WebAssemblyOptimizeLiveIntervals.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
  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. /// Optimize LiveIntervals for use in a post-RA context.
  11. //
  12. /// LiveIntervals normally runs before register allocation when the code is
  13. /// only recently lowered out of SSA form, so it's uncommon for registers to
  14. /// have multiple defs, and when they do, the defs are usually closely related.
  15. /// Later, after coalescing, tail duplication, and other optimizations, it's
  16. /// more common to see registers with multiple unrelated defs. This pass
  17. /// updates LiveIntervals to distribute the value numbers across separate
  18. /// LiveIntervals.
  19. ///
  20. //===----------------------------------------------------------------------===//
  21. #include "WebAssembly.h"
  22. #include "WebAssemblyMachineFunctionInfo.h"
  23. #include "WebAssemblySubtarget.h"
  24. #include "llvm/CodeGen/LiveIntervals.h"
  25. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/CodeGen/Passes.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "wasm-optimize-live-intervals"
  32. namespace {
  33. class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
  34. StringRef getPassName() const override {
  35. return "WebAssembly Optimize Live Intervals";
  36. }
  37. void getAnalysisUsage(AnalysisUsage &AU) const override {
  38. AU.setPreservesCFG();
  39. AU.addRequired<LiveIntervals>();
  40. AU.addPreserved<MachineBlockFrequencyInfo>();
  41. AU.addPreserved<SlotIndexes>();
  42. AU.addPreserved<LiveIntervals>();
  43. AU.addPreservedID(LiveVariablesID);
  44. AU.addPreservedID(MachineDominatorsID);
  45. MachineFunctionPass::getAnalysisUsage(AU);
  46. }
  47. MachineFunctionProperties getRequiredProperties() const override {
  48. return MachineFunctionProperties().set(
  49. MachineFunctionProperties::Property::TracksLiveness);
  50. }
  51. bool runOnMachineFunction(MachineFunction &MF) override;
  52. public:
  53. static char ID; // Pass identification, replacement for typeid
  54. WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
  55. };
  56. } // end anonymous namespace
  57. char WebAssemblyOptimizeLiveIntervals::ID = 0;
  58. INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE,
  59. "Optimize LiveIntervals for WebAssembly", false, false)
  60. FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
  61. return new WebAssemblyOptimizeLiveIntervals();
  62. }
  63. bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(
  64. MachineFunction &MF) {
  65. LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
  66. "********** Function: "
  67. << MF.getName() << '\n');
  68. MachineRegisterInfo &MRI = MF.getRegInfo();
  69. auto &LIS = getAnalysis<LiveIntervals>();
  70. // We don't preserve SSA form.
  71. MRI.leaveSSA();
  72. assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
  73. // Split multiple-VN LiveIntervals into multiple LiveIntervals.
  74. SmallVector<LiveInterval *, 4> SplitLIs;
  75. for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
  76. Register Reg = Register::index2VirtReg(I);
  77. auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
  78. if (MRI.reg_nodbg_empty(Reg))
  79. continue;
  80. LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
  81. if (Reg == TRI.getFrameRegister(MF) && SplitLIs.size() > 0) {
  82. // The live interval for the frame register was split, resulting in a new
  83. // VReg. For now we only support debug info output for a single frame base
  84. // value for the function, so just use the last one. It will certainly be
  85. // wrong for some part of the function, but until we are able to track
  86. // values through live-range splitting and stackification, it will have to
  87. // do.
  88. MF.getInfo<WebAssemblyFunctionInfo>()->setFrameBaseVreg(
  89. SplitLIs.back()->reg());
  90. }
  91. SplitLIs.clear();
  92. }
  93. // In FixIrreducibleControlFlow, we conservatively inserted IMPLICIT_DEF
  94. // instructions to satisfy LiveIntervals' requirement that all uses be
  95. // dominated by defs. Now that LiveIntervals has computed which of these
  96. // defs are actually needed and which are dead, remove the dead ones.
  97. for (MachineInstr &MI : llvm::make_early_inc_range(MF.front())) {
  98. if (MI.isImplicitDef() && MI.getOperand(0).isDead()) {
  99. LiveInterval &LI = LIS.getInterval(MI.getOperand(0).getReg());
  100. LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(MI).getRegSlot());
  101. LIS.RemoveMachineInstrFromMaps(MI);
  102. MI.eraseFromParent();
  103. }
  104. }
  105. return true;
  106. }