LLVM 24.0.0git
WebAssemblyArgumentMove.cpp
Go to the documentation of this file.
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
29#include "WebAssembly.h"
35#include "llvm/CodeGen/Passes.h"
36#include "llvm/IR/Analysis.h"
37#include "llvm/Support/Debug.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();
54 }
55
56 bool runOnMachineFunction(MachineFunction &MF) override;
57};
58} // end anonymous namespace
59
60char WebAssemblyArgumentMoveLegacy::ID = 0;
61INITIALIZE_PASS(WebAssemblyArgumentMoveLegacy, DEBUG_TYPE,
62 "Move ARGUMENT instructions for WebAssembly", false, false)
63
65 return new WebAssemblyArgumentMoveLegacy();
66}
67
68static bool argumentMove(MachineFunction &MF) {
70 dbgs() << "********** Argument Move **********\n"
71 << "********** Function: " << MF.getName() << '\n';
72 });
73
74 bool Changed = false;
75 MachineBasicBlock &EntryMBB = MF.front();
76 MachineBasicBlock::iterator InsertPt = EntryMBB.end();
77
78 // Look for the first NonArg instruction.
79 for (MachineInstr &MI : EntryMBB) {
80 if (!WebAssembly::isArgument(MI.getOpcode())) {
81 InsertPt = MI;
82 break;
83 }
84 }
85
86 // Now move any argument instructions later in the block
87 // to before our first NonArg instruction.
88 for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
89 if (WebAssembly::isArgument(MI.getOpcode())) {
90 EntryMBB.insert(InsertPt, MI.removeFromParent());
91 Changed = true;
92 }
93 }
94
95 return Changed;
96}
97
98bool WebAssemblyArgumentMoveLegacy::runOnMachineFunction(MachineFunction &MF) {
99 return argumentMove(MF);
100}
101
102PreservedAnalyses
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool argumentMove(MachineFunction &MF)
This file provides WebAssembly-specific target descriptions.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineBasicBlock & front() const
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Changed
Pass manager infrastructure for declaring and invalidating analyses.
bool isArgument(unsigned Opc)
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createWebAssemblyArgumentMoveLegacyPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209