LLVM 24.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
10#include "llvm/ADT/SetVector.h"
19#include "llvm/Pass.h"
20#include "llvm/PassRegistry.h"
21#include "llvm/Support/Debug.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "processimpdefs"
27
28namespace {
29/// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
30/// for each use. Add isUndef marker to implicit_def defs and their uses.
31class ProcessImplicitDefsLegacy : public MachineFunctionPass {
32public:
33 static char ID;
34
35 ProcessImplicitDefsLegacy() : MachineFunctionPass(ID) {}
36
37 void getAnalysisUsage(AnalysisUsage &AU) const override;
38
39 bool runOnMachineFunction(MachineFunction &MF) override;
40
41 MachineFunctionProperties getRequiredProperties() const override {
42 return MachineFunctionProperties().setIsSSA();
43 }
44};
45
46class ProcessImplicitDefs {
47 const TargetInstrInfo *TII = nullptr;
48 const TargetRegisterInfo *TRI = nullptr;
49 MachineRegisterInfo *MRI = nullptr;
50
52
53 void processImplicitDef(MachineInstr *MI);
54 bool canTurnIntoImplicitDef(MachineInstr *MI);
55
56public:
57 bool run(MachineFunction &MF);
58};
59} // end anonymous namespace
60
61char ProcessImplicitDefsLegacy::ID = 0;
62char &llvm::ProcessImplicitDefsID = ProcessImplicitDefsLegacy::ID;
63
64INITIALIZE_PASS(ProcessImplicitDefsLegacy, DEBUG_TYPE,
65 "Process Implicit Definitions", false, false)
66
67void ProcessImplicitDefsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.setPreservesCFG();
69 AU.addPreserved<AAResultsWrapperPass>();
71}
72
73bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
74 if (!MI->isCopyLike() &&
75 !MI->isInsertSubreg() &&
76 !MI->isRegSequence() &&
77 !MI->isPHI())
78 return false;
79 for (const MachineOperand &MO : MI->all_uses())
80 if (MO.readsReg())
81 return false;
82 return true;
83}
84
85void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
86 LLVM_DEBUG(dbgs() << "Processing " << *MI);
87 Register Reg = MI->getOperand(0).getReg();
88
89 if (Reg.isVirtual()) {
90 // For virtual registers, mark all uses as <undef>, and convert users to
91 // implicit-def when possible.
92 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
93 MO.setIsUndef();
94 MachineInstr *UserMI = MO.getParent();
95 if (!canTurnIntoImplicitDef(UserMI))
96 continue;
97 LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
98 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
99 WorkList.insert(UserMI);
100 }
101 MI->eraseFromParent();
102 return;
103 }
104
105 // This is a physreg implicit-def.
106 // Trim any extra operands.
107 for (unsigned i = MI->getNumOperands() - 1; i; --i)
108 MI->removeOperand(i);
109
110 // Try to add undef flag to all uses. If all uses are updated remove
111 // implicit-def.
112 MachineBasicBlock::instr_iterator SearchMI = MI->getIterator();
113 MachineBasicBlock::instr_iterator SearchE = MI->getParent()->instr_end();
114 bool ImplicitDefIsDead = false;
115 bool SearchedWholeBlock = true;
116 constexpr unsigned SearchLimit = 35;
117 unsigned Count = 0;
118 for (++SearchMI; SearchMI != SearchE; ++SearchMI) {
119 if (SearchMI->isDebugInstr())
120 continue;
121 if (++Count > SearchLimit) {
122 SearchedWholeBlock = false;
123 break;
124 }
125 for (MachineOperand &MO : SearchMI->operands()) {
126 if (!MO.isReg())
127 continue;
128 Register SearchReg = MO.getReg();
129 if (!SearchReg.isPhysical() || !TRI->regsOverlap(Reg, SearchReg))
130 continue;
131 // SearchMI uses or redefines Reg. Set <undef> flags on all uses.
132 if (MO.isUse()) {
133 if (TRI->isSubRegisterEq(Reg, SearchReg)) {
134 MO.setIsUndef();
135 } else {
136 // Use is larger than Reg. It is not safe to add undef to this use.
137 return;
138 }
139 }
140 if (MO.isDef()) {
141 if (TRI->isSubRegisterEq(SearchReg, Reg)) {
142 ImplicitDefIsDead = true;
143 } else {
144 // Reg is larger than definition. It is not safe to add undef to any
145 // subsequent uses of Reg.
146 return;
147 }
148 }
149 }
150 if (ImplicitDefIsDead) {
151 LLVM_DEBUG(dbgs() << "Physreg redefine: " << *SearchMI);
152 break;
153 }
154 }
155
156 // If we have added an undef flag to all uses (i.e. we have found a redefining
157 // MI or there are no successors), we can erase the IMPLICIT_DEF.
158 if (ImplicitDefIsDead ||
159 (SearchedWholeBlock && MI->getParent()->succ_empty())) {
160 MI->eraseFromParent();
161 LLVM_DEBUG(dbgs() << "Deleting implicit-def: " << *MI);
162 }
163}
164
165bool ProcessImplicitDefsLegacy::runOnMachineFunction(MachineFunction &MF) {
166 return ProcessImplicitDefs().run(MF);
167}
168
169PreservedAnalyses
172 if (!ProcessImplicitDefs().run(MF))
173 return PreservedAnalyses::all();
174
177 .preserve<AAManager>();
178}
179
180/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
181/// <undef> operands.
182bool ProcessImplicitDefs::run(MachineFunction &MF) {
183
184 LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
185 << "********** Function: " << MF.getName() << '\n');
186
187 bool Changed = false;
188
191 MRI = &MF.getRegInfo();
192 assert(WorkList.empty() && "Inconsistent worklist state");
193
194 for (MachineBasicBlock &MBB : MF) {
195 // Scan the basic block for implicit defs.
196 for (MachineInstr &MI : MBB)
197 if (MI.isImplicitDef())
198 WorkList.insert(&MI);
199
200 if (WorkList.empty())
201 continue;
202
203 LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
204 << " implicit defs.\n");
205 Changed = true;
206
207 // Drain the WorkList to recursively process any new implicit defs.
208 do processImplicitDef(WorkList.pop_back_val());
209 while (!WorkList.empty());
210 }
211 return Changed;
212}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:119
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Represent the analysis usage information of a pass.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Instructions::iterator instr_iterator
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.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Changed
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI char & ProcessImplicitDefsID
ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.