1 //===-- WebAssemblyArgumentMove.cpp - Argument instruction moving ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file moves ARGUMENT instructions after ScheduleDAG scheduling.
13 /// Arguments are really live-in registers, however, since we use virtual
14 /// registers and LLVM doesn't support live-in virtual registers, we're
15 /// currently making do with ARGUMENT instructions which are placed at the top
16 /// of the entry block. The trick is to get them to *stay* at the top of the
19 /// The ARGUMENTS physical register keeps these instructions pinned in place
20 /// during liveness-aware CodeGen passes, however one thing which does not
21 /// respect this is the ScheduleDAG scheduler. This pass is therefore run
22 /// immediately after that.
24 /// This is all hopefully a temporary solution until we find a better solution
25 /// for describing the live-in nature of arguments.
27 //===----------------------------------------------------------------------===//
29 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
30 #include "WebAssembly.h"
31 #include "WebAssemblyMachineFunctionInfo.h"
32 #include "WebAssemblySubtarget.h"
33 #include "WebAssemblyUtilities.h"
34 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/Passes.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
41 #define DEBUG_TYPE "wasm-argument-move"
44 class WebAssemblyArgumentMove final : public MachineFunctionPass {
46 static char ID; // Pass identification, replacement for typeid
47 WebAssemblyArgumentMove() : MachineFunctionPass(ID) {}
49 StringRef getPassName() const override { return "WebAssembly Argument Move"; }
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.addPreserved<MachineBlockFrequencyInfo>();
54 AU.addPreservedID(MachineDominatorsID);
55 MachineFunctionPass::getAnalysisUsage(AU);
58 bool runOnMachineFunction(MachineFunction &MF) override;
60 } // end anonymous namespace
62 char WebAssemblyArgumentMove::ID = 0;
63 INITIALIZE_PASS(WebAssemblyArgumentMove, DEBUG_TYPE,
64 "Move ARGUMENT instructions for WebAssembly", false, false)
66 FunctionPass *llvm::createWebAssemblyArgumentMove() {
67 return new WebAssemblyArgumentMove();
70 bool WebAssemblyArgumentMove::runOnMachineFunction(MachineFunction &MF) {
72 dbgs() << "********** Argument Move **********\n"
73 << "********** Function: " << MF.getName() << '\n';
77 MachineBasicBlock &EntryMBB = MF.front();
78 MachineBasicBlock::iterator InsertPt = EntryMBB.end();
80 // Look for the first NonArg instruction.
81 for (MachineInstr &MI : EntryMBB) {
82 if (!WebAssembly::isArgument(MI)) {
88 // Now move any argument instructions later in the block
89 // to before our first NonArg instruction.
90 for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
91 if (WebAssembly::isArgument(MI)) {
92 EntryMBB.insert(InsertPt, MI.removeFromParent());