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 // Look for the first instruction to use or define an alias.
106 MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
107 MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
108 bool Found = false;
109 for (++UserMI; UserMI != UserE; ++UserMI) {
110 for (MachineOperand &MO : UserMI->operands()) {
111 if (!MO.isReg())
112 continue;
113 Register UserReg = MO.getReg();
114 if (!UserReg.isPhysical() || !TRI->regsOverlap(Reg, UserReg))
115 continue;
116 // UserMI uses or redefines Reg. Set <undef> flags on all uses.
117 Found = true;
118 if (MO.isUse())
119 MO.setIsUndef();
120 }
121 if (Found)
122 break;
123 }
124
125 // If we found the using MI, we can erase the IMPLICIT_DEF.
126 if (Found) {
127 LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
128 MI->eraseFromParent();
129 return;
130 }
131
132 // Using instr wasn't found, it could be in another block.
133 // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
134 for (unsigned i = MI->getNumOperands() - 1; i; --i)
135 MI->removeOperand(i);
136 LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
137}
138
139bool ProcessImplicitDefsLegacy::runOnMachineFunction(MachineFunction &MF) {
140 return ProcessImplicitDefs().run(MF);
141}
142
143PreservedAnalyses
146 if (!ProcessImplicitDefs().run(MF))
147 return PreservedAnalyses::all();
148
151 .preserve<AAManager>();
152}
153
154/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
155/// <undef> operands.
156bool ProcessImplicitDefs::run(MachineFunction &MF) {
157
158 LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
159 << "********** Function: " << MF.getName() << '\n');
160
161 bool Changed = false;
162
165 MRI = &MF.getRegInfo();
166 assert(WorkList.empty() && "Inconsistent worklist state");
167
168 for (MachineBasicBlock &MBB : MF) {
169 // Scan the basic block for implicit defs.
170 for (MachineInstr &MI : MBB)
171 if (MI.isImplicitDef())
172 WorkList.insert(&MI);
173
174 if (WorkList.empty())
175 continue;
176
177 LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
178 << " implicit defs.\n");
179 Changed = true;
180
181 // Drain the WorkList to recursively process any new implicit defs.
182 do processImplicitDef(WorkList.pop_back_val());
183 while (!WorkList.empty());
184 }
185 return Changed;
186}
unsigned const MachineRegisterInfo * MRI
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,...
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.
Definition Types.h:26
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
LLVM_ABI char & ProcessImplicitDefsID
ProcessImpicitDefs pass - This pass removes IMPLICIT_DEFs.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.