1 //===-- lib/CodeGen/MachineInstrBundle.cpp --------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineInstrBundle.h"
10 #include "llvm/ADT/SmallSet.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/CodeGen/MachineFunctionPass.h"
13 #include "llvm/CodeGen/MachineInstrBuilder.h"
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/CodeGen/TargetInstrInfo.h"
16 #include "llvm/CodeGen/TargetRegisterInfo.h"
17 #include "llvm/CodeGen/TargetSubtargetInfo.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Target/TargetMachine.h"
24 class UnpackMachineBundles
: public MachineFunctionPass
{
26 static char ID
; // Pass identification
28 std::function
<bool(const MachineFunction
&)> Ftor
= nullptr)
29 : MachineFunctionPass(ID
), PredicateFtor(std::move(Ftor
)) {
30 initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
33 bool runOnMachineFunction(MachineFunction
&MF
) override
;
36 std::function
<bool(const MachineFunction
&)> PredicateFtor
;
38 } // end anonymous namespace
40 char UnpackMachineBundles::ID
= 0;
41 char &llvm::UnpackMachineBundlesID
= UnpackMachineBundles::ID
;
42 INITIALIZE_PASS(UnpackMachineBundles
, "unpack-mi-bundles",
43 "Unpack machine instruction bundles", false, false)
45 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction
&MF
) {
46 if (PredicateFtor
&& !PredicateFtor(MF
))
50 for (MachineBasicBlock
&MBB
: MF
) {
51 for (MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin(),
52 MIE
= MBB
.instr_end(); MII
!= MIE
; ) {
53 MachineInstr
*MI
= &*MII
;
55 // Remove BUNDLE instruction and the InsideBundle flags from bundled
58 while (++MII
!= MIE
&& MII
->isBundledWithPred()) {
59 MII
->unbundleFromPred();
60 for (unsigned i
= 0, e
= MII
->getNumOperands(); i
!= e
; ++i
) {
61 MachineOperand
&MO
= MII
->getOperand(i
);
62 if (MO
.isReg() && MO
.isInternalRead())
63 MO
.setIsInternalRead(false);
66 MI
->eraseFromParent();
80 llvm::createUnpackMachineBundles(
81 std::function
<bool(const MachineFunction
&)> Ftor
) {
82 return new UnpackMachineBundles(std::move(Ftor
));
86 class FinalizeMachineBundles
: public MachineFunctionPass
{
88 static char ID
; // Pass identification
89 FinalizeMachineBundles() : MachineFunctionPass(ID
) {
90 initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry());
93 bool runOnMachineFunction(MachineFunction
&MF
) override
;
95 } // end anonymous namespace
97 char FinalizeMachineBundles::ID
= 0;
98 char &llvm::FinalizeMachineBundlesID
= FinalizeMachineBundles::ID
;
99 INITIALIZE_PASS(FinalizeMachineBundles
, "finalize-mi-bundles",
100 "Finalize machine instruction bundles", false, false)
102 bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction
&MF
) {
103 return llvm::finalizeBundles(MF
);
106 /// Return the first found DebugLoc that has a DILocation, given a range of
107 /// instructions. The search range is from FirstMI to LastMI (exclusive). If no
108 /// DILocation is found, then an empty location is returned.
109 static DebugLoc
getDebugLoc(MachineBasicBlock::instr_iterator FirstMI
,
110 MachineBasicBlock::instr_iterator LastMI
) {
111 for (auto MII
= FirstMI
; MII
!= LastMI
; ++MII
)
112 if (MII
->getDebugLoc().get())
113 return MII
->getDebugLoc();
117 /// finalizeBundle - Finalize a machine instruction bundle which includes
118 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
119 /// This routine adds a BUNDLE instruction to represent the bundle, it adds
120 /// IsInternalRead markers to MachineOperands which are defined inside the
121 /// bundle, and it copies externally visible defs and uses to the BUNDLE
123 void llvm::finalizeBundle(MachineBasicBlock
&MBB
,
124 MachineBasicBlock::instr_iterator FirstMI
,
125 MachineBasicBlock::instr_iterator LastMI
) {
126 assert(FirstMI
!= LastMI
&& "Empty bundle?");
127 MIBundleBuilder
Bundle(MBB
, FirstMI
, LastMI
);
129 MachineFunction
&MF
= *MBB
.getParent();
130 const TargetInstrInfo
*TII
= MF
.getSubtarget().getInstrInfo();
131 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
133 MachineInstrBuilder MIB
=
134 BuildMI(MF
, getDebugLoc(FirstMI
, LastMI
), TII
->get(TargetOpcode::BUNDLE
));
137 SmallVector
<Register
, 32> LocalDefs
;
138 SmallSet
<Register
, 32> LocalDefSet
;
139 SmallSet
<Register
, 8> DeadDefSet
;
140 SmallSet
<Register
, 16> KilledDefSet
;
141 SmallVector
<Register
, 8> ExternUses
;
142 SmallSet
<Register
, 8> ExternUseSet
;
143 SmallSet
<Register
, 8> KilledUseSet
;
144 SmallSet
<Register
, 8> UndefUseSet
;
145 SmallVector
<MachineOperand
*, 4> Defs
;
146 for (auto MII
= FirstMI
; MII
!= LastMI
; ++MII
) {
147 for (unsigned i
= 0, e
= MII
->getNumOperands(); i
!= e
; ++i
) {
148 MachineOperand
&MO
= MII
->getOperand(i
);
156 Register Reg
= MO
.getReg();
160 if (LocalDefSet
.count(Reg
)) {
161 MO
.setIsInternalRead();
163 // Internal def is now killed.
164 KilledDefSet
.insert(Reg
);
166 if (ExternUseSet
.insert(Reg
).second
) {
167 ExternUses
.push_back(Reg
);
169 UndefUseSet
.insert(Reg
);
172 // External def is now killed.
173 KilledUseSet
.insert(Reg
);
177 for (unsigned i
= 0, e
= Defs
.size(); i
!= e
; ++i
) {
178 MachineOperand
&MO
= *Defs
[i
];
179 Register Reg
= MO
.getReg();
183 if (LocalDefSet
.insert(Reg
).second
) {
184 LocalDefs
.push_back(Reg
);
186 DeadDefSet
.insert(Reg
);
189 // Re-defined inside the bundle, it's no longer killed.
190 KilledDefSet
.erase(Reg
);
192 // Previously defined but dead.
193 DeadDefSet
.erase(Reg
);
196 if (!MO
.isDead() && Register::isPhysicalRegister(Reg
)) {
197 for (MCSubRegIterator
SubRegs(Reg
, TRI
); SubRegs
.isValid(); ++SubRegs
) {
198 unsigned SubReg
= *SubRegs
;
199 if (LocalDefSet
.insert(SubReg
).second
)
200 LocalDefs
.push_back(SubReg
);
208 SmallSet
<Register
, 32> Added
;
209 for (unsigned i
= 0, e
= LocalDefs
.size(); i
!= e
; ++i
) {
210 Register Reg
= LocalDefs
[i
];
211 if (Added
.insert(Reg
).second
) {
212 // If it's not live beyond end of the bundle, mark it dead.
213 bool isDead
= DeadDefSet
.count(Reg
) || KilledDefSet
.count(Reg
);
214 MIB
.addReg(Reg
, getDefRegState(true) | getDeadRegState(isDead
) |
215 getImplRegState(true));
219 for (unsigned i
= 0, e
= ExternUses
.size(); i
!= e
; ++i
) {
220 Register Reg
= ExternUses
[i
];
221 bool isKill
= KilledUseSet
.count(Reg
);
222 bool isUndef
= UndefUseSet
.count(Reg
);
223 MIB
.addReg(Reg
, getKillRegState(isKill
) | getUndefRegState(isUndef
) |
224 getImplRegState(true));
227 // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
228 // the property, then also set it on the bundle.
229 for (auto MII
= FirstMI
; MII
!= LastMI
; ++MII
) {
230 if (MII
->getFlag(MachineInstr::FrameSetup
))
231 MIB
.setMIFlag(MachineInstr::FrameSetup
);
232 if (MII
->getFlag(MachineInstr::FrameDestroy
))
233 MIB
.setMIFlag(MachineInstr::FrameDestroy
);
237 /// finalizeBundle - Same functionality as the previous finalizeBundle except
238 /// the last instruction in the bundle is not provided as an input. This is
239 /// used in cases where bundles are pre-determined by marking instructions
240 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
241 /// points to the end of the bundle.
242 MachineBasicBlock::instr_iterator
243 llvm::finalizeBundle(MachineBasicBlock
&MBB
,
244 MachineBasicBlock::instr_iterator FirstMI
) {
245 MachineBasicBlock::instr_iterator E
= MBB
.instr_end();
246 MachineBasicBlock::instr_iterator LastMI
= std::next(FirstMI
);
247 while (LastMI
!= E
&& LastMI
->isInsideBundle())
249 finalizeBundle(MBB
, FirstMI
, LastMI
);
253 /// finalizeBundles - Finalize instruction bundles in the specified
254 /// MachineFunction. Return true if any bundles are finalized.
255 bool llvm::finalizeBundles(MachineFunction
&MF
) {
256 bool Changed
= false;
257 for (MachineBasicBlock
&MBB
: MF
) {
258 MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin();
259 MachineBasicBlock::instr_iterator MIE
= MBB
.instr_end();
262 assert(!MII
->isInsideBundle() &&
263 "First instr cannot be inside bundle before finalization!");
265 for (++MII
; MII
!= MIE
; ) {
266 if (!MII
->isInsideBundle())
269 MII
= finalizeBundle(MBB
, std::prev(MII
));
278 VirtRegInfo
llvm::AnalyzeVirtRegInBundle(
279 MachineInstr
&MI
, Register Reg
,
280 SmallVectorImpl
<std::pair
<MachineInstr
*, unsigned>> *Ops
) {
281 VirtRegInfo RI
= {false, false, false};
282 for (MIBundleOperands
O(MI
); O
.isValid(); ++O
) {
283 MachineOperand
&MO
= *O
;
284 if (!MO
.isReg() || MO
.getReg() != Reg
)
287 // Remember each (MI, OpNo) that refers to Reg.
289 Ops
->push_back(std::make_pair(MO
.getParent(), O
.getOperandNo()));
291 // Both defs and uses can read virtual registers.
298 // Only defs can write.
302 MO
.getParent()->isRegTiedToDefOperand(O
.getOperandNo()))
308 PhysRegInfo
llvm::AnalyzePhysRegInBundle(const MachineInstr
&MI
, Register Reg
,
309 const TargetRegisterInfo
*TRI
) {
310 bool AllDefsDead
= true;
311 PhysRegInfo PRI
= {false, false, false, false, false, false, false, false};
313 assert(Reg
.isPhysical() && "analyzePhysReg not given a physical register!");
314 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
315 const MachineOperand
&MO
= *O
;
317 if (MO
.isRegMask() && MO
.clobbersPhysReg(Reg
)) {
318 PRI
.Clobbered
= true;
325 Register MOReg
= MO
.getReg();
326 if (!MOReg
|| !Register::isPhysicalRegister(MOReg
))
329 if (!TRI
->regsOverlap(MOReg
, Reg
))
332 bool Covered
= TRI
->isSuperRegisterEq(Reg
, MOReg
);
336 PRI
.FullyRead
= true;
340 } else if (MO
.isDef()) {
343 PRI
.FullyDefined
= true;
350 if (PRI
.FullyDefined
|| PRI
.Clobbered
)
352 else if (PRI
.Defined
)
353 PRI
.PartialDeadDef
= true;