LLVM  8.0.1
WebAssemblyReplacePhysRegs.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyReplacePhysRegs.cpp - Replace phys regs with virt regs -===//
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 /// This file implements a pass that replaces physical registers with
12 /// virtual registers.
13 ///
14 /// LLVM expects certain physical registers, such as a stack pointer. However,
15 /// WebAssembly doesn't actually have such physical registers. This pass is run
16 /// once LLVM no longer needs these registers, and replaces them with virtual
17 /// registers, so they can participate in register stackifying and coloring in
18 /// the normal way.
19 ///
20 //===----------------------------------------------------------------------===//
21 
23 #include "WebAssembly.h"
25 #include "WebAssemblySubtarget.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Support/Debug.h"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "wasm-replace-phys-regs"
34 
35 namespace {
36 class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
37 public:
38  static char ID; // Pass identification, replacement for typeid
39  WebAssemblyReplacePhysRegs() : MachineFunctionPass(ID) {}
40 
41 private:
42  StringRef getPassName() const override {
43  return "WebAssembly Replace Physical Registers";
44  }
45 
46  void getAnalysisUsage(AnalysisUsage &AU) const override {
47  AU.setPreservesCFG();
49  }
50 
51  bool runOnMachineFunction(MachineFunction &MF) override;
52 };
53 } // end anonymous namespace
54 
56 INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE,
57  "Replace physical registers with virtual registers", false,
58  false)
59 
61  return new WebAssemblyReplacePhysRegs();
62 }
63 
64 bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
65  LLVM_DEBUG({
66  dbgs() << "********** Replace Physical Registers **********\n"
67  << "********** Function: " << MF.getName() << '\n';
68  });
69 
71  const auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
72  bool Changed = false;
73 
74  assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
75  "LiveIntervals shouldn't be active yet!");
76  // We don't preserve SSA or liveness.
77  MRI.leaveSSA();
78  MRI.invalidateLiveness();
79 
80  for (unsigned PReg = WebAssembly::NoRegister + 1;
81  PReg < WebAssembly::NUM_TARGET_REGS; ++PReg) {
82  // Skip fake registers that are never used explicitly.
83  if (PReg == WebAssembly::VALUE_STACK || PReg == WebAssembly::ARGUMENTS)
84  continue;
85 
86  // Replace explicit uses of the physical register with a virtual register.
87  const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PReg);
88  unsigned VReg = WebAssembly::NoRegister;
89  for (auto I = MRI.reg_begin(PReg), E = MRI.reg_end(); I != E;) {
90  MachineOperand &MO = *I++;
91  if (!MO.isImplicit()) {
92  if (VReg == WebAssembly::NoRegister)
93  VReg = MRI.createVirtualRegister(RC);
94  MO.setReg(VReg);
95  if (MO.getParent()->isDebugValue())
96  MO.setIsDebug();
97  Changed = true;
98  }
99  }
100  }
101 
102  return Changed;
103 }
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned const TargetRegisterInfo * TRI
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE, "Replace physical registers with virtual registers", false, false) FunctionPass *llvm
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
char & LiveIntervalsID
LiveIntervals - This analysis keeps track of the live ranges of virtual and physical registers...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:285
This file declares the WebAssembly-specific subclass of TargetSubtarget.
bool isDebugValue() const
Definition: MachineInstr.h:997
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares WebAssembly-specific per-machine-function information.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define DEBUG_TYPE
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void setIsDebug(bool Val=true)
FunctionPass * createWebAssemblyReplacePhysRegs()
#define LLVM_DEBUG(X)
Definition: Debug.h:123
bool isImplicit() const