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>();
70 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
72}
73
74bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
75 if (!MI->isCopyLike() &&
76 !MI->isInsertSubreg() &&
77 !MI->isRegSequence() &&
78 !MI->isPHI())
79 return false;
80 for (const MachineOperand &MO : MI->all_uses())
81 if (MO.readsReg())
82 return false;
83 return true;
84}
85
86void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
87 LLVM_DEBUG(dbgs() << "Processing " << *MI);
88 Register Reg = MI->getOperand(0).getReg();
89
90 if (Reg.isVirtual()) {
91 // For virtual registers, mark all uses as <undef>, and convert users to
92 // implicit-def when possible.
93 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
94 MO.setIsUndef();
95 MachineInstr *UserMI = MO.getParent();
96 if (!canTurnIntoImplicitDef(UserMI))
97 continue;
98 LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
99 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
100 WorkList.insert(UserMI);
101 }
102 MI->eraseFromParent();
103 return;
104 }
105
106 // This is a physreg implicit-def.
107 // Trim any extra operands.
108 for (unsigned i = MI->getNumOperands() - 1; i; --i)
109 MI->removeOperand(i);
110
111 // Try to add undef flag to all uses. If all uses are updated remove
112 // implicit-def.
113 MachineBasicBlock::instr_iterator SearchMI = MI->getIterator();
114 MachineBasicBlock::instr_iterator SearchE = MI->getParent()->instr_end();
115 bool ImplicitDefIsDead = false;
116 bool SearchedWholeBlock = true;
117 constexpr unsigned SearchLimit = 35;
118 unsigned Count = 0;
119 for (++SearchMI; SearchMI != SearchE; ++SearchMI) {
120 if (SearchMI->isDebugInstr())
121 continue;
122 if (++Count > SearchLimit) {
123 SearchedWholeBlock = false;
124 break;
125 }
126 for (MachineOperand &MO : SearchMI->operands()) {
127 if (!MO.isReg())
128 continue;
129 Register SearchReg = MO.getReg();
130 if (!SearchReg.isPhysical() || !TRI->regsOverlap(Reg, SearchReg))
131 continue;
132 // SearchMI uses or redefines Reg. Set <undef> flags on all uses.
133 if (MO.isUse()) {
134 if (TRI->isSubRegisterEq(Reg, SearchReg)) {
135 MO.setIsUndef();
136 } else {
137 // Use is larger than Reg. It is not safe to add undef to this use.
138 return;
139 }
140 }
141 if (MO.isDef()) {
142 if (TRI->isSubRegisterEq(SearchReg, Reg)) {
143 ImplicitDefIsDead = true;
144 } else {
145 // Reg is larger than definition. It is not safe to add undef to any
146 // subsequent uses of Reg.
147 return;
148 }
149 }
150 }
151 if (ImplicitDefIsDead) {
152 LLVM_DEBUG(dbgs() << "Physreg redefine: " << *SearchMI);
153 break;
154 }
155 }
156
157 // If we have added an undef flag to all uses (i.e. we have found a redefining
158 // MI or there are no successors), we can erase the IMPLICIT_DEF.
159 if (ImplicitDefIsDead ||
160 (SearchedWholeBlock && MI->getParent()->succ_empty())) {
161 MI->eraseFromParent();
162 LLVM_DEBUG(dbgs() << "Deleting implicit-def: " << *MI);
163 }
164}
165
166bool ProcessImplicitDefsLegacy::runOnMachineFunction(MachineFunction &MF) {
167 return ProcessImplicitDefs().run(MF);
168}
169
170PreservedAnalyses
173 if (!ProcessImplicitDefs().run(MF))
174 return PreservedAnalyses::all();
175
178 .preserve<AAManager>();
179}
180
181/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
182/// <undef> operands.
183bool ProcessImplicitDefs::run(MachineFunction &MF) {
184
185 LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
186 << "********** Function: " << MF.getName() << '\n');
187
188 bool Changed = false;
189
192 MRI = &MF.getRegInfo();
193 assert(WorkList.empty() && "Inconsistent worklist state");
194
195 for (MachineBasicBlock &MBB : MF) {
196 // Scan the basic block for implicit defs.
197 for (MachineInstr &MI : MBB)
198 if (MI.isImplicitDef())
199 WorkList.insert(&MI);
200
201 if (WorkList.empty())
202 continue;
203
204 LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
205 << " implicit defs.\n");
206 Changed = true;
207
208 // Drain the WorkList to recursively process any new implicit defs.
209 do processImplicitDef(WorkList.pop_back_val());
210 while (!WorkList.empty());
211 }
212 return Changed;
213}
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:339
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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.