LLVM  8.0.1
NVPTXProxyRegErasure.cpp
Go to the documentation of this file.
1 //===- NVPTXProxyRegErasure.cpp - NVPTX Proxy Register Instruction Erasure -==//
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 // The pass is needed to remove ProxyReg instructions and restore related
11 // registers. The instructions were needed at instruction selection stage to
12 // make sure that callseq_end nodes won't be removed as "dead nodes". This can
13 // happen when we expand instructions into libcalls and the call site doesn't
14 // care about the libcall chain. Call site cares about data flow only, and the
15 // latest data flow node happens to be before callseq_end. Therefore the node
16 // becomes dangling and "dead". The ProxyReg acts like an additional data flow
17 // node *after* the callseq_end in the chain and ensures that everything will be
18 // preserved.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "NVPTX.h"
28 
29 using namespace llvm;
30 
31 namespace llvm {
33 }
34 
35 namespace {
36 
37 struct NVPTXProxyRegErasure : public MachineFunctionPass {
38 public:
39  static char ID;
40  NVPTXProxyRegErasure() : MachineFunctionPass(ID) {
42  }
43 
44  bool runOnMachineFunction(MachineFunction &MF) override;
45 
46  StringRef getPassName() const override {
47  return "NVPTX Proxy Register Instruction Erasure";
48  }
49 
50  void getAnalysisUsage(AnalysisUsage &AU) const override {
52  }
53 
54 private:
55  void replaceMachineInstructionUsage(MachineFunction &MF, MachineInstr &MI);
56 
57  void replaceRegisterUsage(MachineInstr &Instr, MachineOperand &From,
58  MachineOperand &To);
59 };
60 
61 } // namespace
62 
64 
65 INITIALIZE_PASS(NVPTXProxyRegErasure, "nvptx-proxyreg-erasure", "NVPTX ProxyReg Erasure", false, false)
66 
67 bool NVPTXProxyRegErasure::runOnMachineFunction(MachineFunction &MF) {
69 
70  for (auto &BB : MF) {
71  for (auto &MI : BB) {
72  switch (MI.getOpcode()) {
73  case NVPTX::ProxyRegI1:
74  case NVPTX::ProxyRegI16:
75  case NVPTX::ProxyRegI32:
76  case NVPTX::ProxyRegI64:
77  case NVPTX::ProxyRegF16:
78  case NVPTX::ProxyRegF16x2:
79  case NVPTX::ProxyRegF32:
80  case NVPTX::ProxyRegF64:
81  replaceMachineInstructionUsage(MF, MI);
82  RemoveList.push_back(&MI);
83  break;
84  }
85  }
86  }
87 
88  for (auto *MI : RemoveList) {
89  MI->eraseFromParent();
90  }
91 
92  return !RemoveList.empty();
93 }
94 
95 void NVPTXProxyRegErasure::replaceMachineInstructionUsage(MachineFunction &MF,
96  MachineInstr &MI) {
97  auto &InOp = *MI.uses().begin();
98  auto &OutOp = *MI.defs().begin();
99 
100  assert(InOp.isReg() && "ProxyReg input operand should be a register.");
101  assert(OutOp.isReg() && "ProxyReg output operand should be a register.");
102 
103  for (auto &BB : MF) {
104  for (auto &I : BB) {
105  replaceRegisterUsage(I, OutOp, InOp);
106  }
107  }
108 }
109 
110 void NVPTXProxyRegErasure::replaceRegisterUsage(MachineInstr &Instr,
112  MachineOperand &To) {
113  for (auto &Op : Instr.uses()) {
114  if (Op.isReg() && Op.getReg() == From.getReg()) {
115  Op.setReg(To.getReg());
116  }
117  }
118 }
119 
121  return new NVPTXProxyRegErasure();
122 }
MachineFunctionPass * createNVPTXProxyRegErasurePass()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
Definition: MachineInstr.h:492
unsigned getReg() const
getReg - Returns the register number.
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.
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
Definition: MachineInstr.h:481
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:34
BlockVerifier::State From
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
void initializeNVPTXProxyRegErasurePass(PassRegistry &)
Representation of each machine instruction.
Definition: MachineInstr.h:64
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39