LLVM  9.0.0svn
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"
31 #include "WebAssemblySubtarget.h"
32 #include "WebAssemblyUtilities.h"
35 #include "llvm/CodeGen/Passes.h"
36 #include "llvm/Support/Debug.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();
55  }
56 
57  bool runOnMachineFunction(MachineFunction &MF) override;
58 };
59 } // end anonymous namespace
60 
62 INITIALIZE_PASS(WebAssemblyArgumentMove, DEBUG_TYPE,
63  "Move ARGUMENT instructions for WebAssembly", false, false)
64 
66  return new WebAssemblyArgumentMove();
67 }
68 
69 bool WebAssemblyArgumentMove::runOnMachineFunction(MachineFunction &MF) {
70  LLVM_DEBUG({
71  dbgs() << "********** Argument Move **********\n"
72  << "********** Function: " << MF.getName() << '\n';
73  });
74 
75  bool Changed = false;
76  MachineBasicBlock &EntryMBB = MF.front();
77  MachineBasicBlock::iterator InsertPt = EntryMBB.end();
78 
79  // Look for the first NonArg instruction.
80  for (MachineInstr &MI : EntryMBB) {
81  if (!WebAssembly::isArgument(MI.getOpcode())) {
82  InsertPt = MI;
83  break;
84  }
85  }
86 
87  // Now move any argument instructions later in the block
88  // to before our first NonArg instruction.
89  for (MachineInstr &MI : llvm::make_range(InsertPt, EntryMBB.end())) {
90  if (WebAssembly::isArgument(MI.getOpcode())) {
91  EntryMBB.insert(InsertPt, MI.removeFromParent());
92  Changed = true;
93  }
94  }
95 
96  return Changed;
97 }
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool isArgument(unsigned Opc)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FunctionPass * createWebAssemblyArgumentMove()
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
#define DEBUG_TYPE
AnalysisUsage & addPreservedID(const void *ID)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
This file contains the declaration of the WebAssembly-specific utility functions. ...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
const MachineBasicBlock & front() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Representation of each machine instruction.
Definition: MachineInstr.h:64
This file declares WebAssembly-specific per-machine-function information.
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122
INITIALIZE_PASS(WebAssemblyArgumentMove, DEBUG_TYPE, "Move ARGUMENT instructions for WebAssembly", false, false) FunctionPass *llvm