LLVM 20.0.0git
HexagonCopyHoisting.cpp
Go to the documentation of this file.
1//===--------- HexagonCopyHoisting.cpp - Hexagon Copy Hoisting ----------===//
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// The purpose of this pass is to move the copy instructions that are
9// present in all the successor of a basic block (BB) to the end of BB.
10//===----------------------------------------------------------------------===//
11
13#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
22#include "llvm/Support/Debug.h"
23
24#define DEBUG_TYPE "CopyHoist"
25
26using namespace llvm;
27
29 cl::init(""));
30
31namespace llvm {
34} // namespace llvm
35
36namespace {
37
38class HexagonCopyHoisting : public MachineFunctionPass {
39
40public:
41 static char ID;
42 HexagonCopyHoisting() : MachineFunctionPass(ID), MFN(nullptr), MRI(nullptr) {
44 }
45
46 StringRef getPassName() const override { return "Hexagon Copy Hoisting"; }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 }
57
58 bool runOnMachineFunction(MachineFunction &Fn) override;
59 void collectCopyInst();
60 void addMItoCopyList(MachineInstr *MI);
61 bool analyzeCopy(MachineBasicBlock *BB);
62 bool isSafetoMove(MachineInstr *CandMI);
63 void moveCopyInstr(MachineBasicBlock *DestBB,
64 std::pair<Register, Register> Key, MachineInstr *MI);
65
66 MachineFunction *MFN;
68 std::vector<DenseMap<std::pair<Register, Register>, MachineInstr *>>
69 CopyMIList;
70};
71
72} // namespace
73
74char HexagonCopyHoisting::ID = 0;
75
76namespace llvm {
77char &HexagonCopyHoistingID = HexagonCopyHoisting::ID;
78} // namespace llvm
79
80bool HexagonCopyHoisting::runOnMachineFunction(MachineFunction &Fn) {
81
82 if ((CPHoistFn != "") && (CPHoistFn != Fn.getFunction().getName()))
83 return false;
84
85 MFN = &Fn;
86 MRI = &Fn.getRegInfo();
87
88 LLVM_DEBUG(dbgs() << "\nCopy Hoisting:" << "\'" << Fn.getName() << "\'\n");
89
90 CopyMIList.clear();
91 CopyMIList.resize(Fn.getNumBlockIDs());
92
93 // Traverse through all basic blocks and collect copy instructions.
94 collectCopyInst();
95
96 // Traverse through the basic blocks again and move the COPY instructions
97 // that are present in all the successors of BB to BB.
98 bool Changed = false;
99 for (MachineBasicBlock *BB : post_order(&Fn)) {
100 if (!BB->empty()) {
101 if (BB->pred_size() != 1)
102 continue;
103 auto &BBCopyInst = CopyMIList[BB->getNumber()];
104 if (BBCopyInst.size() > 0)
105 Changed |= analyzeCopy(*BB->pred_begin());
106 }
107 }
108 // Re-compute liveness
109 if (Changed) {
110 LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
112 SI->reanalyze(Fn);
113 LIS.reanalyze(Fn);
114 }
115 return Changed;
116}
117
118//===----------------------------------------------------------------------===//
119// Save all COPY instructions for each basic block in CopyMIList vector.
120//===----------------------------------------------------------------------===//
121void HexagonCopyHoisting::collectCopyInst() {
122 for (MachineBasicBlock &BB : *MFN) {
123#ifndef NDEBUG
124 auto &BBCopyInst = CopyMIList[BB.getNumber()];
125 LLVM_DEBUG(dbgs() << "Visiting BB#" << BB.getNumber() << ":\n");
126#endif
127
128 for (MachineInstr &MI : BB) {
129 if (MI.getOpcode() == TargetOpcode::COPY)
130 addMItoCopyList(&MI);
131 }
132 LLVM_DEBUG(dbgs() << "\tNumber of copies: " << BBCopyInst.size() << "\n");
133 }
134}
135
136void HexagonCopyHoisting::addMItoCopyList(MachineInstr *MI) {
137 unsigned BBNum = MI->getParent()->getNumber();
138 auto &BBCopyInst = CopyMIList[BBNum];
139 Register DstReg = MI->getOperand(0).getReg();
140 Register SrcReg = MI->getOperand(1).getReg();
141
142 if (!Register::isVirtualRegister(DstReg) ||
144 MRI->getRegClass(DstReg) != &Hexagon::IntRegsRegClass ||
145 MRI->getRegClass(SrcReg) != &Hexagon::IntRegsRegClass)
146 return;
147
148 BBCopyInst.insert(std::pair(std::pair(SrcReg, DstReg), MI));
149#ifndef NDEBUG
150 LLVM_DEBUG(dbgs() << "\tAdding Copy Instr to the list: " << MI << "\n");
151 for (auto II : BBCopyInst) {
152 MachineInstr *TempMI = II.getSecond();
153 LLVM_DEBUG(dbgs() << "\tIn the list: " << TempMI << "\n");
154 }
155#endif
156}
157
158//===----------------------------------------------------------------------===//
159// Look at the COPY instructions of all the successors of BB. If the same
160// instruction is present in every successor and can be safely moved,
161// pull it into BB.
162//===----------------------------------------------------------------------===//
163bool HexagonCopyHoisting::analyzeCopy(MachineBasicBlock *BB) {
164
165 bool Changed = false;
166 if (BB->succ_size() < 2)
167 return false;
168
169 for (MachineBasicBlock *SB : BB->successors()) {
170 if (SB->pred_size() != 1 || SB->isEHPad() || SB->hasAddressTaken())
171 return false;
172 }
173
174 MachineBasicBlock *SBB1 = *BB->succ_begin();
175 auto &BBCopyInst1 = CopyMIList[SBB1->getNumber()];
176
177 for (auto II : BBCopyInst1) {
178 std::pair<Register, Register> Key = II.getFirst();
179 MachineInstr *MI = II.getSecond();
180 bool IsSafetoMove = true;
181 for (MachineBasicBlock *SuccBB : BB->successors()) {
182 auto &SuccBBCopyInst = CopyMIList[SuccBB->getNumber()];
183 if (!SuccBBCopyInst.count(Key)) {
184 // Same copy not present in this successor
185 IsSafetoMove = false;
186 break;
187 }
188 // If present, make sure that it's safe to pull this copy instruction
189 // into the predecessor.
190 MachineInstr *SuccMI = SuccBBCopyInst[Key];
191 if (!isSafetoMove(SuccMI)) {
192 IsSafetoMove = false;
193 break;
194 }
195 }
196 // If we have come this far, this copy instruction can be safely
197 // moved to the predecessor basic block.
198 if (IsSafetoMove) {
199 LLVM_DEBUG(dbgs() << "\t\t Moving instr to BB#" << BB->getNumber() << ": "
200 << MI << "\n");
201 moveCopyInstr(BB, Key, MI);
202 // Add my into BB copyMI list.
203 Changed = true;
204 }
205 }
206
207#ifndef NDEBUG
208 auto &BBCopyInst = CopyMIList[BB->getNumber()];
209 for (auto II : BBCopyInst) {
210 MachineInstr *TempMI = II.getSecond();
211 LLVM_DEBUG(dbgs() << "\tIn the list: " << TempMI << "\n");
212 }
213#endif
214 return Changed;
215}
216
217bool HexagonCopyHoisting::isSafetoMove(MachineInstr *CandMI) {
218 // Make sure that it's safe to move this 'copy' instruction to the predecessor
219 // basic block.
220 assert(CandMI->getOperand(0).isReg() && CandMI->getOperand(1).isReg());
221 Register DefR = CandMI->getOperand(0).getReg();
222 Register UseR = CandMI->getOperand(1).getReg();
223
224 MachineBasicBlock *BB = CandMI->getParent();
225 // There should not be a def/use of DefR between the start of BB and CandMI.
227 for (MII = BB->begin(), MIE = CandMI; MII != MIE; ++MII) {
228 MachineInstr *OtherMI = &*MII;
229 for (const MachineOperand &Mo : OtherMI->operands())
230 if (Mo.isReg() && Mo.getReg() == DefR)
231 return false;
232 }
233 // There should not be a def of UseR between the start of BB and CandMI.
234 for (MII = BB->begin(), MIE = CandMI; MII != MIE; ++MII) {
235 MachineInstr *OtherMI = &*MII;
236 for (const MachineOperand &Mo : OtherMI->operands())
237 if (Mo.isReg() && Mo.isDef() && Mo.getReg() == UseR)
238 return false;
239 }
240 return true;
241}
242
243void HexagonCopyHoisting::moveCopyInstr(MachineBasicBlock *DestBB,
244 std::pair<Register, Register> Key,
245 MachineInstr *MI) {
247 assert(FirstTI != DestBB->end());
248
249 DestBB->splice(FirstTI, MI->getParent(), MI);
250
251 addMItoCopyList(MI);
252 for (MachineBasicBlock *SuccBB : drop_begin(DestBB->successors())) {
253 auto &BBCopyInst = CopyMIList[SuccBB->getNumber()];
254 MachineInstr *SuccMI = BBCopyInst[Key];
255 SuccMI->eraseFromParent();
256 BBCopyInst.erase(Key);
257 }
258}
259
260//===----------------------------------------------------------------------===//
261// Public Constructor Functions
262//===----------------------------------------------------------------------===//
263
264INITIALIZE_PASS(HexagonCopyHoisting, "hexagon-move-phicopy",
265 "Hexagon move phi copy", false, false)
266
268 return new HexagonCopyHoisting();
269}
unsigned const MachineRegisterInfo * MRI
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
static cl::opt< std::string > CPHoistFn("cphoistfn", cl::Hidden, cl::desc(""), cl::init(""))
IRTranslator LLVM IR MI
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
SlotIndexes * getSlotIndexes() const
void reanalyze(MachineFunction &MF)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned succ_size() const
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
Analysis pass which computes a MachineDominatorTree.
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:346
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:685
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:579
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static constexpr bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:71
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
SlotIndexes pass.
Definition: SlotIndexes.h:297
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
Key
PAL metadata keys.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
FunctionPass * createHexagonCopyHoisting()
iterator_range< po_iterator< T > > post_order(const T &G)
void initializeHexagonCopyHoistingPass(PassRegistry &Registry)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
char & HexagonCopyHoistingID