WebAssemblyArgumentMove.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===-- WebAssemblyArgumentMove.cpp - Argument instruction moving ---------===//
  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 moves ARGUMENT instructions after ScheduleDAG scheduling.
  11. ///
  12. /// Arguments are really live-in registers, however, since we use virtual
  13. /// registers and LLVM doesn't support live-in virtual registers, we're
  14. /// currently making do with ARGUMENT instructions which are placed at the top
  15. /// of the entry block. The trick is to get them to *stay* at the top of the
  16. /// entry block.
  17. ///
  18. /// The ARGUMENTS physical register keeps these instructions pinned in place
  19. /// during liveness-aware CodeGen passes, however one thing which does not
  20. /// respect this is the ScheduleDAG scheduler. This pass is therefore run
  21. /// immediately after that.
  22. ///
  23. /// This is all hopefully a temporary solution until we find a better solution
  24. /// for describing the live-in nature of arguments.
  25. ///
  26. //===----------------------------------------------------------------------===//
  27. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  28. #include "Utils/WebAssemblyUtilities.h"
  29. #include "WebAssembly.h"
  30. #include "WebAssemblyMachineFunctionInfo.h"
  31. #include "WebAssemblySubtarget.h"
  32. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  33. #include "llvm/CodeGen/MachineRegisterInfo.h"
  34. #include "llvm/CodeGen/Passes.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. using namespace llvm;
  38. #define DEBUG_TYPE "wasm-argument-move"
  39. namespace {
  40. class WebAssemblyArgumentMove final : public MachineFunctionPass {
  41. public:
  42. static char ID; // Pass identification, replacement for typeid
  43. WebAssemblyArgumentMove() : MachineFunctionPass(ID) {}
  44. StringRef getPassName() const override { return "WebAssembly Argument Move"; }
  45. void getAnalysisUsage(AnalysisUsage &AU) const override {
  46. AU.setPreservesCFG();
  47. AU.addPreserved<MachineBlockFrequencyInfo>();
  48. AU.addPreservedID(MachineDominatorsID);
  49. MachineFunctionPass::getAnalysisUsage(AU);
  50. }
  51. bool runOnMachineFunction(MachineFunction &MF) override;
  52. };
  53. } // end anonymous namespace
  54. char WebAssemblyArgumentMove::ID = 0;
  55. INITIALIZE_PASS(WebAssemblyArgumentMove, DEBUG_TYPE,
  56. "Move ARGUMENT instructions for WebAssembly", false, false)
  57. FunctionPass *llvm::createWebAssemblyArgumentMove() {
  58. return new WebAssemblyArgumentMove();
  59. }
  60. bool WebAssemblyArgumentMove::runOnMachineFunction(MachineFunction &MF) {
  61. LLVM_DEBUG({
  62. dbgs() << "********** Argument Move **********\n"
  63. << "********** Function: " << MF.getName() << '\n';
  64. });
  65. bool Changed = false;
  66. MachineBasicBlock &EntryMBB = MF.front();
  67. MachineBasicBlock::iterator InsertPt = EntryMBB.end();
  68. // Look for the first NonArg instruction.
  69. for (MachineInstr &MI : EntryMBB) {
  70. if (!WebAssembly::isArgument(MI.getOpcode())) {
  71. InsertPt = MI;
  72. break;
  73. }
  74. }
  75. // Now move any argument instructions later in the block
  76. // to before our first NonArg instruction.
  77. for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
  78. if (WebAssembly::isArgument(MI.getOpcode())) {
  79. EntryMBB.insert(InsertPt, MI.removeFromParent());
  80. Changed = true;
  81. }
  82. }
  83. return Changed;
  84. }