WebAssemblyMemIntrinsicResults.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //== WebAssemblyMemIntrinsicResults.cpp - Optimize memory intrinsic results ==//
  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 implements an optimization pass using memory intrinsic results.
  11. ///
  12. /// Calls to memory intrinsics (memcpy, memmove, memset) return the destination
  13. /// address. They are in the form of
  14. /// %dst_new = call @memcpy %dst, %src, %len
  15. /// where %dst and %dst_new registers contain the same value.
  16. ///
  17. /// This is to enable an optimization wherein uses of the %dst register used in
  18. /// the parameter can be replaced by uses of the %dst_new register used in the
  19. /// result, making the %dst register more likely to be single-use, thus more
  20. /// likely to be useful to register stackifying, and potentially also exposing
  21. /// the call instruction itself to register stackifying. These both can reduce
  22. /// local.get/local.set traffic.
  23. ///
  24. /// The LLVM intrinsics for these return void so they can't use the returned
  25. /// attribute and consequently aren't handled by the OptimizeReturned pass.
  26. ///
  27. //===----------------------------------------------------------------------===//
  28. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  29. #include "WebAssembly.h"
  30. #include "WebAssemblyMachineFunctionInfo.h"
  31. #include "WebAssemblySubtarget.h"
  32. #include "llvm/Analysis/TargetLibraryInfo.h"
  33. #include "llvm/CodeGen/LiveIntervals.h"
  34. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  35. #include "llvm/CodeGen/MachineDominators.h"
  36. #include "llvm/CodeGen/MachineRegisterInfo.h"
  37. #include "llvm/CodeGen/Passes.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. using namespace llvm;
  41. #define DEBUG_TYPE "wasm-mem-intrinsic-results"
  42. namespace {
  43. class WebAssemblyMemIntrinsicResults final : public MachineFunctionPass {
  44. public:
  45. static char ID; // Pass identification, replacement for typeid
  46. WebAssemblyMemIntrinsicResults() : MachineFunctionPass(ID) {}
  47. StringRef getPassName() const override {
  48. return "WebAssembly Memory Intrinsic Results";
  49. }
  50. void getAnalysisUsage(AnalysisUsage &AU) const override {
  51. AU.setPreservesCFG();
  52. AU.addRequired<MachineBlockFrequencyInfo>();
  53. AU.addPreserved<MachineBlockFrequencyInfo>();
  54. AU.addRequired<MachineDominatorTree>();
  55. AU.addPreserved<MachineDominatorTree>();
  56. AU.addRequired<LiveIntervals>();
  57. AU.addPreserved<SlotIndexes>();
  58. AU.addPreserved<LiveIntervals>();
  59. AU.addRequired<TargetLibraryInfoWrapperPass>();
  60. MachineFunctionPass::getAnalysisUsage(AU);
  61. }
  62. bool runOnMachineFunction(MachineFunction &MF) override;
  63. private:
  64. };
  65. } // end anonymous namespace
  66. char WebAssemblyMemIntrinsicResults::ID = 0;
  67. INITIALIZE_PASS(WebAssemblyMemIntrinsicResults, DEBUG_TYPE,
  68. "Optimize memory intrinsic result values for WebAssembly",
  69. false, false)
  70. FunctionPass *llvm::createWebAssemblyMemIntrinsicResults() {
  71. return new WebAssemblyMemIntrinsicResults();
  72. }
  73. // Replace uses of FromReg with ToReg if they are dominated by MI.
  74. static bool replaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI,
  75. unsigned FromReg, unsigned ToReg,
  76. const MachineRegisterInfo &MRI,
  77. MachineDominatorTree &MDT,
  78. LiveIntervals &LIS) {
  79. bool Changed = false;
  80. LiveInterval *FromLI = &LIS.getInterval(FromReg);
  81. LiveInterval *ToLI = &LIS.getInterval(ToReg);
  82. SlotIndex FromIdx = LIS.getInstructionIndex(MI).getRegSlot();
  83. VNInfo *FromVNI = FromLI->getVNInfoAt(FromIdx);
  84. SmallVector<SlotIndex, 4> Indices;
  85. for (MachineOperand &O :
  86. llvm::make_early_inc_range(MRI.use_nodbg_operands(FromReg))) {
  87. MachineInstr *Where = O.getParent();
  88. // Check that MI dominates the instruction in the normal way.
  89. if (&MI == Where || !MDT.dominates(&MI, Where))
  90. continue;
  91. // If this use gets a different value, skip it.
  92. SlotIndex WhereIdx = LIS.getInstructionIndex(*Where);
  93. VNInfo *WhereVNI = FromLI->getVNInfoAt(WhereIdx);
  94. if (WhereVNI && WhereVNI != FromVNI)
  95. continue;
  96. // Make sure ToReg isn't clobbered before it gets there.
  97. VNInfo *ToVNI = ToLI->getVNInfoAt(WhereIdx);
  98. if (ToVNI && ToVNI != FromVNI)
  99. continue;
  100. Changed = true;
  101. LLVM_DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from "
  102. << MI << "\n");
  103. O.setReg(ToReg);
  104. // If the store's def was previously dead, it is no longer.
  105. if (!O.isUndef()) {
  106. MI.getOperand(0).setIsDead(false);
  107. Indices.push_back(WhereIdx.getRegSlot());
  108. }
  109. }
  110. if (Changed) {
  111. // Extend ToReg's liveness.
  112. LIS.extendToIndices(*ToLI, Indices);
  113. // Shrink FromReg's liveness.
  114. LIS.shrinkToUses(FromLI);
  115. // If we replaced all dominated uses, FromReg is now killed at MI.
  116. if (!FromLI->liveAt(FromIdx.getDeadSlot()))
  117. MI.addRegisterKilled(FromReg, MBB.getParent()
  118. ->getSubtarget<WebAssemblySubtarget>()
  119. .getRegisterInfo());
  120. }
  121. return Changed;
  122. }
  123. static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI,
  124. const MachineRegisterInfo &MRI,
  125. MachineDominatorTree &MDT, LiveIntervals &LIS,
  126. const WebAssemblyTargetLowering &TLI,
  127. const TargetLibraryInfo &LibInfo) {
  128. MachineOperand &Op1 = MI.getOperand(1);
  129. if (!Op1.isSymbol())
  130. return false;
  131. StringRef Name(Op1.getSymbolName());
  132. bool CallReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
  133. Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
  134. Name == TLI.getLibcallName(RTLIB::MEMSET);
  135. if (!CallReturnsInput)
  136. return false;
  137. LibFunc Func;
  138. if (!LibInfo.getLibFunc(Name, Func))
  139. return false;
  140. Register FromReg = MI.getOperand(2).getReg();
  141. Register ToReg = MI.getOperand(0).getReg();
  142. if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg))
  143. report_fatal_error("Memory Intrinsic results: call to builtin function "
  144. "with wrong signature, from/to mismatch");
  145. return replaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT, LIS);
  146. }
  147. bool WebAssemblyMemIntrinsicResults::runOnMachineFunction(MachineFunction &MF) {
  148. LLVM_DEBUG({
  149. dbgs() << "********** Memory Intrinsic Results **********\n"
  150. << "********** Function: " << MF.getName() << '\n';
  151. });
  152. MachineRegisterInfo &MRI = MF.getRegInfo();
  153. auto &MDT = getAnalysis<MachineDominatorTree>();
  154. const WebAssemblyTargetLowering &TLI =
  155. *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
  156. const auto &LibInfo =
  157. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(MF.getFunction());
  158. auto &LIS = getAnalysis<LiveIntervals>();
  159. bool Changed = false;
  160. // We don't preserve SSA form.
  161. MRI.leaveSSA();
  162. assert(MRI.tracksLiveness() &&
  163. "MemIntrinsicResults expects liveness tracking");
  164. for (auto &MBB : MF) {
  165. LLVM_DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
  166. for (auto &MI : MBB)
  167. switch (MI.getOpcode()) {
  168. default:
  169. break;
  170. case WebAssembly::CALL:
  171. Changed |= optimizeCall(MBB, MI, MRI, MDT, LIS, TLI, LibInfo);
  172. break;
  173. }
  174. }
  175. return Changed;
  176. }