LLVM  15.0.0git
ProcessImplicitDefs.cpp
Go to the documentation of this file.
1 //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
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 #include "llvm/ADT/SetVector.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Pass.h"
18 #include "llvm/PassRegistry.h"
19 #include "llvm/Support/Debug.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "processimpdefs"
25 
26 namespace {
27 /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
28 /// for each use. Add isUndef marker to implicit_def defs and their uses.
29 class ProcessImplicitDefs : public MachineFunctionPass {
30  const TargetInstrInfo *TII;
31  const TargetRegisterInfo *TRI;
33 
35 
36  void processImplicitDef(MachineInstr *MI);
37  bool canTurnIntoImplicitDef(MachineInstr *MI);
38 
39 public:
40  static char ID;
41 
42  ProcessImplicitDefs() : MachineFunctionPass(ID) {
44  }
45 
46  void getAnalysisUsage(AnalysisUsage &au) const override;
47 
48  bool runOnMachineFunction(MachineFunction &MF) override;
49 
50  virtual MachineFunctionProperties getRequiredProperties() const override {
53  }
54 };
55 } // end anonymous namespace
56 
59 
60 INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE,
61  "Process Implicit Definitions", false, false)
62 
63 void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
64  AU.setPreservesCFG();
65  AU.addPreserved<AAResultsWrapperPass>();
67 }
68 
69 bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
70  if (!MI->isCopyLike() &&
71  !MI->isInsertSubreg() &&
72  !MI->isRegSequence() &&
73  !MI->isPHI())
74  return false;
75  for (const MachineOperand &MO : MI->operands())
76  if (MO.isReg() && MO.isUse() && MO.readsReg())
77  return false;
78  return true;
79 }
80 
81 void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
82  LLVM_DEBUG(dbgs() << "Processing " << *MI);
83  Register Reg = MI->getOperand(0).getReg();
84 
86  // For virtual registers, mark all uses as <undef>, and convert users to
87  // implicit-def when possible.
89  MO.setIsUndef();
90  MachineInstr *UserMI = MO.getParent();
91  if (!canTurnIntoImplicitDef(UserMI))
92  continue;
93  LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
94  UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
95  WorkList.insert(UserMI);
96  }
97  MI->eraseFromParent();
98  return;
99  }
100 
101  // This is a physreg implicit-def.
102  // Look for the first instruction to use or define an alias.
103  MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
104  MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
105  bool Found = false;
106  for (++UserMI; UserMI != UserE; ++UserMI) {
107  for (MachineOperand &MO : UserMI->operands()) {
108  if (!MO.isReg())
109  continue;
110  Register UserReg = MO.getReg();
111  if (!Register::isPhysicalRegister(UserReg) ||
112  !TRI->regsOverlap(Reg, UserReg))
113  continue;
114  // UserMI uses or redefines Reg. Set <undef> flags on all uses.
115  Found = true;
116  if (MO.isUse())
117  MO.setIsUndef();
118  }
119  if (Found)
120  break;
121  }
122 
123  // If we found the using MI, we can erase the IMPLICIT_DEF.
124  if (Found) {
125  LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
126  MI->eraseFromParent();
127  return;
128  }
129 
130  // Using instr wasn't found, it could be in another block.
131  // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
132  for (unsigned i = MI->getNumOperands() - 1; i; --i)
133  MI->removeOperand(i);
134  LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
135 }
136 
137 /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
138 /// <undef> operands.
139 bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
140 
141  LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
142  << "********** Function: " << MF.getName() << '\n');
143 
144  bool Changed = false;
145 
146  TII = MF.getSubtarget().getInstrInfo();
148  MRI = &MF.getRegInfo();
149  assert(WorkList.empty() && "Inconsistent worklist state");
150 
151  for (MachineBasicBlock &MBB : MF) {
152  // Scan the basic block for implicit defs.
153  for (MachineInstr &MI : MBB)
154  if (MI.isImplicitDef())
155  WorkList.insert(&MI);
156 
157  if (WorkList.empty())
158  continue;
159 
160  LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
161  << " implicit defs.\n");
162  Changed = true;
163 
164  // Drain the WorkList to recursively process any new implicit defs.
165  do processImplicitDef(WorkList.pop_back_val());
166  while (!WorkList.empty());
167  }
168  return Changed;
169 }
i
i
Definition: README.txt:29
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
Pass.h
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:93
llvm::printMBBReference
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Definition: MachineBasicBlock.cpp:116
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::TargetSubtargetInfo::getRegisterInfo
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Definition: TargetSubtargetInfo.h:125
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:234
TargetInstrInfo.h
llvm::MachineFunctionProperties::Property::IsSSA
@ IsSSA
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:111
PassRegistry.h
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1620
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
AliasAnalysis.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:650
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
llvm::MachineRegisterInfo::use_nodbg_operands
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
Definition: MachineRegisterInfo.h:534
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::Register::isPhysicalRegister
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:65
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
DEBUG_TYPE
#define DEBUG_TYPE
Definition: ProcessImplicitDefs.cpp:24
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:127
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:180
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:640
llvm::TargetRegisterInfo::regsOverlap
bool regsOverlap(Register RegA, Register RegB) const
Returns true if the two registers are equal or alias each other.
Definition: TargetRegisterInfo.h:419
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
MachineFunctionPass.h
llvm::Register::isVirtualRegister
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:71
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:567
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ProcessImplicitDefsID
char & ProcessImplicitDefsID
ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
Definition: ProcessImplicitDefs.cpp:58
llvm::MachineFunction
Definition: MachineFunction.h:241
llvm::initializeProcessImplicitDefsPass
void initializeProcessImplicitDefsPass(PassRegistry &)
TargetSubtargetInfo.h
llvm::MachineInstr::getParent
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:288
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::ilist_iterator
Iterator for intrusive lists based on ilist_node.
Definition: ilist_iterator.h:57
INITIALIZE_PASS
INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE, "Process Implicit Definitions", false, false) void ProcessImplicitDefs
Definition: ProcessImplicitDefs.cpp:60
llvm::MachineInstr::setDesc
void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
Definition: MachineInstr.h:1720
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:1347
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
raw_ostream.h
InitializePasses.h
Debug.h
SetVector.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37