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/Pass.h"
20 #include "llvm/PassRegistry.h"
25 class UnpackMachineBundles
: public MachineFunctionPass
{
27 static char ID
; // Pass identification
29 std::function
<bool(const MachineFunction
&)> Ftor
= nullptr)
30 : MachineFunctionPass(ID
), PredicateFtor(std::move(Ftor
)) {
31 initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry());
34 bool runOnMachineFunction(MachineFunction
&MF
) override
;
37 std::function
<bool(const MachineFunction
&)> PredicateFtor
;
39 } // end anonymous namespace
41 char UnpackMachineBundles::ID
= 0;
42 char &llvm::UnpackMachineBundlesID
= UnpackMachineBundles::ID
;
43 INITIALIZE_PASS(UnpackMachineBundles
, "unpack-mi-bundles",
44 "Unpack machine instruction bundles", false, false)
46 bool UnpackMachineBundles::runOnMachineFunction(MachineFunction
&MF
) {
47 if (PredicateFtor
&& !PredicateFtor(MF
))
51 for (MachineBasicBlock
&MBB
: MF
) {
52 for (MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin(),
53 MIE
= MBB
.instr_end(); MII
!= MIE
; ) {
54 MachineInstr
*MI
= &*MII
;
56 // Remove BUNDLE instruction and the InsideBundle flags from bundled
59 while (++MII
!= MIE
&& MII
->isBundledWithPred()) {
60 MII
->unbundleFromPred();
61 for (MachineOperand
&MO
: MII
->operands()) {
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())
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 // Debug instructions have no effects to track.
148 if (MII
->isDebugInstr())
151 for (MachineOperand
&MO
: MII
->operands()) {
159 Register Reg
= MO
.getReg();
163 if (LocalDefSet
.count(Reg
)) {
164 MO
.setIsInternalRead();
166 // Internal def is now killed.
167 KilledDefSet
.insert(Reg
);
169 if (ExternUseSet
.insert(Reg
).second
) {
170 ExternUses
.push_back(Reg
);
172 UndefUseSet
.insert(Reg
);
175 // External def is now killed.
176 KilledUseSet
.insert(Reg
);
180 for (unsigned i
= 0, e
= Defs
.size(); i
!= e
; ++i
) {
181 MachineOperand
&MO
= *Defs
[i
];
182 Register Reg
= MO
.getReg();
186 if (LocalDefSet
.insert(Reg
).second
) {
187 LocalDefs
.push_back(Reg
);
189 DeadDefSet
.insert(Reg
);
192 // Re-defined inside the bundle, it's no longer killed.
193 KilledDefSet
.erase(Reg
);
195 // Previously defined but dead.
196 DeadDefSet
.erase(Reg
);
199 if (!MO
.isDead() && Reg
.isPhysical()) {
200 for (MCPhysReg SubReg
: TRI
->subregs(Reg
)) {
201 if (LocalDefSet
.insert(SubReg
).second
)
202 LocalDefs
.push_back(SubReg
);
210 SmallSet
<Register
, 32> Added
;
211 for (unsigned i
= 0, e
= LocalDefs
.size(); i
!= e
; ++i
) {
212 Register Reg
= LocalDefs
[i
];
213 if (Added
.insert(Reg
).second
) {
214 // If it's not live beyond end of the bundle, mark it dead.
215 bool isDead
= DeadDefSet
.count(Reg
) || KilledDefSet
.count(Reg
);
216 MIB
.addReg(Reg
, getDefRegState(true) | getDeadRegState(isDead
) |
217 getImplRegState(true));
221 for (unsigned i
= 0, e
= ExternUses
.size(); i
!= e
; ++i
) {
222 Register Reg
= ExternUses
[i
];
223 bool isKill
= KilledUseSet
.count(Reg
);
224 bool isUndef
= UndefUseSet
.count(Reg
);
225 MIB
.addReg(Reg
, getKillRegState(isKill
) | getUndefRegState(isUndef
) |
226 getImplRegState(true));
229 // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
230 // the property, then also set it on the bundle.
231 for (auto MII
= FirstMI
; MII
!= LastMI
; ++MII
) {
232 if (MII
->getFlag(MachineInstr::FrameSetup
))
233 MIB
.setMIFlag(MachineInstr::FrameSetup
);
234 if (MII
->getFlag(MachineInstr::FrameDestroy
))
235 MIB
.setMIFlag(MachineInstr::FrameDestroy
);
239 /// finalizeBundle - Same functionality as the previous finalizeBundle except
240 /// the last instruction in the bundle is not provided as an input. This is
241 /// used in cases where bundles are pre-determined by marking instructions
242 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
243 /// points to the end of the bundle.
244 MachineBasicBlock::instr_iterator
245 llvm::finalizeBundle(MachineBasicBlock
&MBB
,
246 MachineBasicBlock::instr_iterator FirstMI
) {
247 MachineBasicBlock::instr_iterator E
= MBB
.instr_end();
248 MachineBasicBlock::instr_iterator LastMI
= std::next(FirstMI
);
249 while (LastMI
!= E
&& LastMI
->isInsideBundle())
251 finalizeBundle(MBB
, FirstMI
, LastMI
);
255 /// finalizeBundles - Finalize instruction bundles in the specified
256 /// MachineFunction. Return true if any bundles are finalized.
257 bool llvm::finalizeBundles(MachineFunction
&MF
) {
258 bool Changed
= false;
259 for (MachineBasicBlock
&MBB
: MF
) {
260 MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin();
261 MachineBasicBlock::instr_iterator MIE
= MBB
.instr_end();
264 assert(!MII
->isInsideBundle() &&
265 "First instr cannot be inside bundle before finalization!");
267 for (++MII
; MII
!= MIE
; ) {
268 if (!MII
->isInsideBundle())
271 MII
= finalizeBundle(MBB
, std::prev(MII
));
280 VirtRegInfo
llvm::AnalyzeVirtRegInBundle(
281 MachineInstr
&MI
, Register Reg
,
282 SmallVectorImpl
<std::pair
<MachineInstr
*, unsigned>> *Ops
) {
283 VirtRegInfo RI
= {false, false, false};
284 for (MIBundleOperands
O(MI
); O
.isValid(); ++O
) {
285 MachineOperand
&MO
= *O
;
286 if (!MO
.isReg() || MO
.getReg() != Reg
)
289 // Remember each (MI, OpNo) that refers to Reg.
291 Ops
->push_back(std::make_pair(MO
.getParent(), O
.getOperandNo()));
293 // Both defs and uses can read virtual registers.
300 // Only defs can write.
304 MO
.getParent()->isRegTiedToDefOperand(O
.getOperandNo()))
310 std::pair
<LaneBitmask
, LaneBitmask
>
311 llvm::AnalyzeVirtRegLanesInBundle(const MachineInstr
&MI
, Register Reg
,
312 const MachineRegisterInfo
&MRI
,
313 const TargetRegisterInfo
&TRI
) {
315 LaneBitmask UseMask
, DefMask
;
317 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
318 const MachineOperand
&MO
= *O
;
319 if (!MO
.isReg() || MO
.getReg() != Reg
)
322 unsigned SubReg
= MO
.getSubReg();
323 if (SubReg
== 0 && MO
.isUse() && !MO
.isUndef())
324 UseMask
|= MRI
.getMaxLaneMaskForVReg(Reg
);
326 LaneBitmask SubRegMask
= TRI
.getSubRegIndexLaneMask(SubReg
);
329 UseMask
|= ~SubRegMask
;
330 DefMask
|= SubRegMask
;
331 } else if (!MO
.isUndef())
332 UseMask
|= SubRegMask
;
335 return {UseMask
, DefMask
};
338 PhysRegInfo
llvm::AnalyzePhysRegInBundle(const MachineInstr
&MI
, Register Reg
,
339 const TargetRegisterInfo
*TRI
) {
340 bool AllDefsDead
= true;
341 PhysRegInfo PRI
= {false, false, false, false, false, false, false, false};
343 assert(Reg
.isPhysical() && "analyzePhysReg not given a physical register!");
344 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
345 const MachineOperand
&MO
= *O
;
347 if (MO
.isRegMask() && MO
.clobbersPhysReg(Reg
)) {
348 PRI
.Clobbered
= true;
355 Register MOReg
= MO
.getReg();
356 if (!MOReg
|| !MOReg
.isPhysical())
359 if (!TRI
->regsOverlap(MOReg
, Reg
))
362 bool Covered
= TRI
->isSuperRegisterEq(Reg
, MOReg
);
366 PRI
.FullyRead
= true;
370 } else if (MO
.isDef()) {
373 PRI
.FullyDefined
= true;
380 if (PRI
.FullyDefined
|| PRI
.Clobbered
)
382 else if (PRI
.Defined
)
383 PRI
.PartialDeadDef
= true;