LLVM 23.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"
18#include "llvm/Pass.h"
19#include "llvm/PassRegistry.h"
20#include "llvm/Support/Debug.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "processimpdefs"
26
27namespace {
28/// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
29/// for each use. Add isUndef marker to implicit_def defs and their uses.
30class ProcessImplicitDefsLegacy : public MachineFunctionPass {
31public:
32 static char ID;
33
34 ProcessImplicitDefsLegacy() : MachineFunctionPass(ID) {}
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override;
37
38 bool runOnMachineFunction(MachineFunction &MF) override;
39
40 MachineFunctionProperties getRequiredProperties() const override {
41 return MachineFunctionProperties().setIsSSA();
42 }
43};
44
45class ProcessImplicitDefs {
46 const TargetInstrInfo *TII = nullptr;
47 const TargetRegisterInfo *TRI = nullptr;
48 MachineRegisterInfo *MRI = nullptr;
49
51
52 void processImplicitDef(MachineInstr *MI);
53 bool canTurnIntoImplicitDef(MachineInstr *MI);
54
55public:
56 bool run(MachineFunction &MF);
57};
58} // end anonymous namespace
59
60char ProcessImplicitDefsLegacy::ID = 0;
61char &llvm::ProcessImplicitDefsID = ProcessImplicitDefsLegacy::ID;
62
63INITIALIZE_PASS(ProcessImplicitDefsLegacy, DEBUG_TYPE,
64 "Process Implicit Definitions", false, false)
65
66void ProcessImplicitDefsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesCFG();
68 AU.addPreserved<AAResultsWrapperPass>();
70}
71
72bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
73 if (!MI->isCopyLike() &&
74 !MI->isInsertSubreg() &&
75 !MI->isRegSequence() &&
76 !MI->isPHI())
77 return false;
78 for (const MachineOperand &MO : MI->all_uses())
79 if (MO.readsReg())
80 return false;
81 return true;
82}
83
84void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
85 LLVM_DEBUG(dbgs() << "Processing " << *MI);
86 Register Reg = MI->getOperand(0).getReg();
87
88 if (Reg.isVirtual()) {
89 // For virtual registers, mark all uses as <undef>, and convert users to
90 // implicit-def when possible.
91 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
92 MO.setIsUndef();
93 MachineInstr *UserMI = MO.getParent();
94 if (!canTurnIntoImplicitDef(UserMI))
95 continue;
96 LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
97 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
98 WorkList.insert(UserMI);
99 }
100 MI->eraseFromParent();
101 return;
102 }
103
104 // This is a physreg implicit-def.
105 // Trim any extra operands.
106 for (unsigned i = MI->getNumOperands() - 1; i; --i)
107 MI->removeOperand(i);
108
109 // Try to add undef flag to all uses. If all uses are updated remove
110 // implicit-def.
111 MachineBasicBlock::instr_iterator SearchMI = MI->getIterator();
112 MachineBasicBlock::instr_iterator SearchE = MI->getParent()->instr_end();
113 bool ImplicitDefIsDead = false;
114 bool SearchedWholeBlock = true;
115 constexpr unsigned SearchLimit = 35;
116 unsigned Count = 0;
117 for (++SearchMI; SearchMI != SearchE; ++SearchMI) {
118 if (SearchMI->isDebugInstr())
119 continue;
120 if (++Count > SearchLimit) {
121 SearchedWholeBlock = false;
122 break;
123 }
124 for (MachineOperand &MO : SearchMI->operands()) {
125 if (!MO.isReg())
126 continue;
127 Register SearchReg = MO.getReg();
128 if (!SearchReg.isPhysical() || !TRI->regsOverlap(Reg, SearchReg))
129 continue;
130 // SearchMI uses or redefines Reg. Set <undef> flags on all uses.
131 if (MO.isUse()) {
132 if (TRI->isSubRegisterEq(Reg, SearchReg)) {
133 MO.setIsUndef();
134 } else {
135 // Use is larger than Reg. It is not safe to add undef to this use.
136 return;
137 }
138 }
139 if (MO.isDef()) {
140 if (TRI->isSubRegisterEq(SearchReg, Reg)) {
141 ImplicitDefIsDead = true;
142 } else {
143 // Reg is larger than definition. It is not safe to add undef to any
144 // subsequent uses of Reg.
145 return;
146 }
147 }
148 }
149 if (ImplicitDefIsDead) {
150 LLVM_DEBUG(dbgs() << "Physreg redefine: " << *SearchMI);
151 break;
152 }
153 }
154
155 // If we have added an undef flag to all uses (i.e. we have found a redefining
156 // MI or there are no successors), we can erase the IMPLICIT_DEF.
157 if (ImplicitDefIsDead ||
158 (SearchedWholeBlock && MI->getParent()->succ_empty())) {
159 MI->eraseFromParent();
160 LLVM_DEBUG(dbgs() << "Deleting implicit-def: " << *MI);
161 }
162}
163
164bool ProcessImplicitDefsLegacy::runOnMachineFunction(MachineFunction &MF) {
165 return ProcessImplicitDefs().run(MF);
166}
167
168PreservedAnalyses
171 if (!ProcessImplicitDefs().run(MF))
172 return PreservedAnalyses::all();
173
176 .preserve<AAManager>();
177}
178
179/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
180/// <undef> operands.
181bool ProcessImplicitDefs::run(MachineFunction &MF) {
182
183 LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
184 << "********** Function: " << MF.getName() << '\n');
185
186 bool Changed = false;
187
190 MRI = &MF.getRegInfo();
191 assert(WorkList.empty() && "Inconsistent worklist state");
192
193 for (MachineBasicBlock &MBB : MF) {
194 // Scan the basic block for implicit defs.
195 for (MachineInstr &MI : MBB)
196 if (MI.isImplicitDef())
197 WorkList.insert(&MI);
198
199 if (WorkList.empty())
200 continue;
201
202 LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
203 << " implicit defs.\n");
204 Changed = true;
205
206 // Drain the WorkList to recursively process any new implicit defs.
207 do processImplicitDef(WorkList.pop_back_val());
208 while (!WorkList.empty());
209 }
210 return Changed;
211}
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:114
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
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:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI char & ProcessImplicitDefsID
ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.