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
28#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
29#include "WebAssembly.h"
30#include "WebAssemblyUtilities.h"
31#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
34#include "llvm/CodeGen/MachinePassManager.h"
35#include "llvm/CodeGen/Passes.h"
36#include "llvm/IR/Analysis.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/raw_ostream.h"
39using namespace llvm;
40
41#define DEBUG_TYPE "wasm-argument-move"
42
43namespace {
44class WebAssemblyArgumentMoveLegacy final : public MachineFunctionPass {
45public:
46 static char ID; // Pass identification, replacement for typeid
47 WebAssemblyArgumentMoveLegacy() : MachineFunctionPass(ID) {}
48
49 StringRef getPassName() const override { return "WebAssembly Argument Move"; }
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesCFG();
53 AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>();
54 AU.addPreservedID(ID&: MachineDominatorsID);
55 MachineFunctionPass::getAnalysisUsage(AU);
56 }
57
58 bool runOnMachineFunction(MachineFunction &MF) override;
59};
60} // end anonymous namespace
61
62char WebAssemblyArgumentMoveLegacy::ID = 0;
63INITIALIZE_PASS(WebAssemblyArgumentMoveLegacy, DEBUG_TYPE,
64 "Move ARGUMENT instructions for WebAssembly", false, false)
65
66FunctionPass *llvm::createWebAssemblyArgumentMoveLegacyPass() {
67 return new WebAssemblyArgumentMoveLegacy();
68}
69
70static bool argumentMove(MachineFunction &MF) {
71 LLVM_DEBUG({
72 dbgs() << "********** Argument Move **********\n"
73 << "********** Function: " << MF.getName() << '\n';
74 });
75
76 bool Changed = false;
77 MachineBasicBlock &EntryMBB = MF.front();
78 MachineBasicBlock::iterator InsertPt = EntryMBB.end();
79
80 // Look for the first NonArg instruction.
81 for (MachineInstr &MI : EntryMBB) {
82 if (!WebAssembly::isArgument(Opc: MI.getOpcode())) {
83 InsertPt = MI;
84 break;
85 }
86 }
87
88 // Now move any argument instructions later in the block
89 // to before our first NonArg instruction.
90 for (MachineInstr &MI : llvm::make_range(x: InsertPt, y: EntryMBB.end())) {
91 if (WebAssembly::isArgument(Opc: MI.getOpcode())) {
92 EntryMBB.insert(I: InsertPt, MI: MI.removeFromParent());
93 Changed = true;
94 }
95 }
96
97 return Changed;
98}
99
100bool WebAssemblyArgumentMoveLegacy::runOnMachineFunction(MachineFunction &MF) {
101 return argumentMove(MF);
102}
103
104PreservedAnalyses
105WebAssemblyArgumentMovePass::run(MachineFunction &MF,
106 MachineFunctionAnalysisManager &MFAM) {
107 return argumentMove(MF) ? getMachineFunctionPassPreservedAnalyses()
108 .preserveSet<CFGAnalyses>()
109 : PreservedAnalyses::all();
110}
111