1 //===-- WebAssemblyArgumentMove.cpp - Argument instruction moving ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file moves ARGUMENT instructions after ScheduleDAG scheduling.
12 ///
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
17 /// entry block.
18 ///
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.
23 ///
24 /// This is all hopefully a temporary solution until we find a better solution
25 /// for describing the live-in nature of arguments.
26 ///
27 //===----------------------------------------------------------------------===//
28 
29 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
30 #include "WebAssembly.h"
31 #include "WebAssemblyMachineFunctionInfo.h"
32 #include "WebAssemblySubtarget.h"
33 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/Passes.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "wasm-argument-move"
41 
42 namespace {
43 class WebAssemblyArgumentMove final : public MachineFunctionPass {
44 public:
45   static char ID; // Pass identification, replacement for typeid
46   WebAssemblyArgumentMove() : MachineFunctionPass(ID) {}
47 
48   StringRef getPassName() const override { return "WebAssembly Argument Move"; }
49 
50   void getAnalysisUsage(AnalysisUsage &AU) const override {
51     AU.setPreservesCFG();
52     AU.addPreserved<MachineBlockFrequencyInfo>();
53     AU.addPreservedID(MachineDominatorsID);
54     MachineFunctionPass::getAnalysisUsage(AU);
55   }
56 
57   bool runOnMachineFunction(MachineFunction &MF) override;
58 };
59 } // end anonymous namespace
60 
61 char WebAssemblyArgumentMove::ID = 0;
62 FunctionPass *llvm::createWebAssemblyArgumentMove() {
63   return new WebAssemblyArgumentMove();
64 }
65 
66 /// Test whether the given instruction is an ARGUMENT.
67 static bool IsArgument(const MachineInstr &MI) {
68   switch (MI.getOpcode()) {
69   case WebAssembly::ARGUMENT_I32:
70   case WebAssembly::ARGUMENT_I64:
71   case WebAssembly::ARGUMENT_F32:
72   case WebAssembly::ARGUMENT_F64:
73   case WebAssembly::ARGUMENT_v16i8:
74   case WebAssembly::ARGUMENT_v8i16:
75   case WebAssembly::ARGUMENT_v4i32:
76   case WebAssembly::ARGUMENT_v4f32:
77     return true;
78   default:
79     return false;
80   }
81 }
82 
83 bool WebAssemblyArgumentMove::runOnMachineFunction(MachineFunction &MF) {
84   DEBUG({
85     dbgs() << "********** Argument Move **********\n"
86            << "********** Function: " << MF.getName() << '\n';
87   });
88 
89   bool Changed = false;
90   MachineBasicBlock &EntryMBB = MF.front();
91   MachineBasicBlock::iterator InsertPt = EntryMBB.end();
92 
93   // Look for the first NonArg instruction.
94   for (MachineInstr &MI : EntryMBB) {
95     if (!IsArgument(MI)) {
96       InsertPt = MI;
97       break;
98     }
99   }
100 
101   // Now move any argument instructions later in the block
102   // to before our first NonArg instruction.
103   for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
104     if (IsArgument(MI)) {
105       EntryMBB.insert(InsertPt, MI.removeFromParent());
106       Changed = true;
107     }
108   }
109 
110   return Changed;
111 }
112