LLVM  15.0.0git
WebAssemblyReplacePhysRegs.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyReplacePhysRegs.cpp - Replace phys regs with virt regs -===//
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 implements a pass that replaces physical registers with
11 /// virtual registers.
12 ///
13 /// LLVM expects certain physical registers, such as a stack pointer. However,
14 /// WebAssembly doesn't actually have such physical registers. This pass is run
15 /// once LLVM no longer needs these registers, and replaces them with virtual
16 /// registers, so they can participate in register stackifying and coloring in
17 /// the normal way.
18 ///
19 //===----------------------------------------------------------------------===//
20 
22 #include "WebAssembly.h"
24 #include "WebAssemblySubtarget.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/Support/Debug.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "wasm-replace-phys-regs"
33 
34 namespace {
35 class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
36 public:
37  static char ID; // Pass identification, replacement for typeid
38  WebAssemblyReplacePhysRegs() : MachineFunctionPass(ID) {}
39 
40 private:
41  StringRef getPassName() const override {
42  return "WebAssembly Replace Physical Registers";
43  }
44 
45  void getAnalysisUsage(AnalysisUsage &AU) const override {
46  AU.setPreservesCFG();
48  }
49 
50  bool runOnMachineFunction(MachineFunction &MF) override;
51 };
52 } // end anonymous namespace
53 
55 INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE,
56  "Replace physical registers with virtual registers", false,
57  false)
58 
60  return new WebAssemblyReplacePhysRegs();
61 }
62 
63 bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
64  LLVM_DEBUG({
65  dbgs() << "********** Replace Physical Registers **********\n"
66  << "********** Function: " << MF.getName() << '\n';
67  });
68 
70  auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
71  bool Changed = false;
72 
73  assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
74  "LiveIntervals shouldn't be active yet!");
75 
76  for (unsigned PReg = WebAssembly::NoRegister + 1;
77  PReg < WebAssembly::NUM_TARGET_REGS; ++PReg) {
78  // Skip fake registers that are never used explicitly.
79  if (PReg == WebAssembly::VALUE_STACK || PReg == WebAssembly::ARGUMENTS)
80  continue;
81 
82  // Replace explicit uses of the physical register with a virtual register.
84  unsigned VReg = WebAssembly::NoRegister;
85  for (MachineOperand &MO :
87  if (!MO.isImplicit()) {
88  if (VReg == WebAssembly::NoRegister) {
89  VReg = MRI.createVirtualRegister(RC);
90  if (PReg == TRI.getFrameRegister(MF)) {
91  auto FI = MF.getInfo<WebAssemblyFunctionInfo>();
92  assert(!FI->isFrameBaseVirtual());
93  FI->setFrameBaseVreg(VReg);
94  LLVM_DEBUG({
95  dbgs() << "replacing preg " << PReg << " with " << VReg << " ("
96  << Register::virtReg2Index(VReg) << ")\n";
97  });
98  }
99  }
100  MO.setReg(VReg);
101  Changed = true;
102  }
103  }
104  }
105 
106  return Changed;
107 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
WebAssembly.h
llvm::MachineRegisterInfo::createVirtualRegister
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Definition: MachineRegisterInfo.cpp:156
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
llvm::createWebAssemblyReplacePhysRegs
FunctionPass * createWebAssemblyReplacePhysRegs()
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1628
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:103
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
MachineRegisterInfo.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:666
llvm::MachineFunction::getInfo
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Definition: MachineFunction.h:754
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:45
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
WebAssemblyMCTargetDesc.h
Passes.h
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:656
llvm::TargetRegisterInfo::getFrameRegister
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:618
MachineFunctionPass.h
llvm::MachineRegisterInfo::reg_operands
iterator_range< reg_iterator > reg_operands(Register Reg) const
Definition: MachineRegisterInfo.h:294
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:565
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WebAssemblyFunctionInfo
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
Definition: WebAssemblyMachineFunctionInfo.h:33
WebAssemblyMachineFunctionInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
INITIALIZE_PASS
INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE, "Replace physical registers with virtual registers", false, false) FunctionPass *llvm
Definition: WebAssemblyReplacePhysRegs.cpp:55
WebAssemblySubtarget.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::Register::virtReg2Index
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
Definition: Register.h:77
llvm::TargetRegisterInfo::getMinimalPhysRegClass
const TargetRegisterClass * getMinimalPhysRegClass(MCRegister Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
Definition: TargetRegisterInfo.cpp:212
raw_ostream.h
DEBUG_TYPE
#define DEBUG_TYPE
Definition: WebAssemblyReplacePhysRegs.cpp:32
llvm::LiveIntervalsID
char & LiveIntervalsID
LiveIntervals - This analysis keeps track of the live ranges of virtual and physical registers.
Definition: LiveIntervals.cpp:61
Debug.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38