blob: aaa1e4eee3fdf349504d8b1846ef66a29121d413 [file] [log] [blame]
Dan Gohman1cf96c02015-12-09 16:23:59 +00001//===-- 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
Dan Gohman1cf96c02015-12-09 16:23:59 +000029#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Derek Schuff39bf39f2016-08-02 23:16:09 +000030#include "WebAssembly.h"
Dan Gohman1cf96c02015-12-09 16:23:59 +000031#include "WebAssemblyMachineFunctionInfo.h"
Derek Schuff39bf39f2016-08-02 23:16:09 +000032#include "WebAssemblySubtarget.h"
Dan Gohman4fc4e422016-10-24 19:49:43 +000033#include "WebAssemblyUtilities.h"
Dan Gohman1cf96c02015-12-09 16:23:59 +000034#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"
39using namespace llvm;
40
41#define DEBUG_TYPE "wasm-argument-move"
42
43namespace {
44class WebAssemblyArgumentMove final : public MachineFunctionPass {
45public:
46 static char ID; // Pass identification, replacement for typeid
47 WebAssemblyArgumentMove() : MachineFunctionPass(ID) {}
48
Mehdi Amini117296c2016-10-01 02:56:57 +000049 StringRef getPassName() const override { return "WebAssembly Argument Move"; }
Dan Gohman1cf96c02015-12-09 16:23:59 +000050
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesCFG();
53 AU.addPreserved<MachineBlockFrequencyInfo>();
54 AU.addPreservedID(MachineDominatorsID);
55 MachineFunctionPass::getAnalysisUsage(AU);
56 }
57
58 bool runOnMachineFunction(MachineFunction &MF) override;
59};
60} // end anonymous namespace
61
62char WebAssemblyArgumentMove::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000063INITIALIZE_PASS(WebAssemblyArgumentMove, DEBUG_TYPE,
64 "Move ARGUMENT instructions for WebAssembly", false, false)
65
Dan Gohman1cf96c02015-12-09 16:23:59 +000066FunctionPass *llvm::createWebAssemblyArgumentMove() {
67 return new WebAssemblyArgumentMove();
68}
69
Dan Gohman1cf96c02015-12-09 16:23:59 +000070bool WebAssemblyArgumentMove::runOnMachineFunction(MachineFunction &MF) {
71 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.
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +000081 for (MachineInstr &MI : EntryMBB) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000082 if (!WebAssembly::isArgument(MI)) {
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +000083 InsertPt = MI;
Dan Gohman1cf96c02015-12-09 16:23:59 +000084 break;
85 }
86 }
87
88 // Now move any argument instructions later in the block
89 // to before our first NonArg instruction.
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +000090 for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000091 if (WebAssembly::isArgument(MI)) {
Duncan P. N. Exon Smith500d0462016-07-08 19:36:40 +000092 EntryMBB.insert(InsertPt, MI.removeFromParent());
Dan Gohman1cf96c02015-12-09 16:23:59 +000093 Changed = true;
94 }
95 }
96
97 return Changed;
98}