Skip to content

Commit 6d3027e

Browse files
committed
MachineSink: Move helper function and use more const
1 parent 9115f18 commit 6d3027e

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ class MachineBasicBlock
816816
/// the first instruction, which might be PHI.
817817
/// Returns end() is there's no non-PHI instruction.
818818
iterator getFirstNonPHI();
819+
const_iterator getFirstNonPHI() const {
820+
return const_cast<MachineBasicBlock *>(this)->getFirstNonPHI();
821+
}
819822

820823
/// Return the first instruction in MBB after I that is not a PHI or a label.
821824
/// This is the correct point to insert lowered copies at the beginning of a

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,47 @@ INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
268268
INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE,
269269
"Machine code sinking", false, false)
270270

271+
/// Return true if a target defined block prologue instruction interferes
272+
/// with a sink candidate.
273+
static bool blockPrologueInterferes(const MachineBasicBlock *BB,
274+
MachineBasicBlock::const_iterator End,
275+
const MachineInstr &MI,
276+
const TargetRegisterInfo *TRI,
277+
const TargetInstrInfo *TII,
278+
const MachineRegisterInfo *MRI) {
279+
if (BB->begin() == End)
280+
return false; // no prologue
281+
for (MachineBasicBlock::const_iterator PI = BB->getFirstNonPHI(); PI != End;
282+
++PI) {
283+
// Only check target defined prologue instructions
284+
if (!TII->isBasicBlockPrologue(*PI))
285+
continue;
286+
for (auto &MO : MI.operands()) {
287+
if (!MO.isReg())
288+
continue;
289+
Register Reg = MO.getReg();
290+
if (!Reg)
291+
continue;
292+
if (MO.isUse()) {
293+
if (Reg.isPhysical() &&
294+
(TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
295+
continue;
296+
if (PI->modifiesRegister(Reg, TRI))
297+
return true;
298+
} else {
299+
if (PI->readsRegister(Reg, TRI))
300+
return true;
301+
// Check for interference with non-dead defs
302+
auto *DefOp = PI->findRegisterDefOperand(Reg, false, true, TRI);
303+
if (DefOp && !DefOp->isDead())
304+
return true;
305+
}
306+
}
307+
}
308+
309+
return false;
310+
}
311+
271312
bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
272313
MachineBasicBlock *MBB) {
273314
if (!MI.isCopy())
@@ -1298,45 +1339,6 @@ bool MachineSinking::SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I) {
12981339
return true;
12991340
}
13001341

1301-
/// Return true if a target defined block prologue instruction interferes
1302-
/// with a sink candidate.
1303-
static bool blockPrologueInterferes(MachineBasicBlock *BB,
1304-
MachineBasicBlock::iterator End,
1305-
MachineInstr &MI,
1306-
const TargetRegisterInfo *TRI,
1307-
const TargetInstrInfo *TII,
1308-
const MachineRegisterInfo *MRI) {
1309-
if (BB->begin() == End)
1310-
return false; // no prologue
1311-
for (MachineBasicBlock::iterator PI = BB->getFirstNonPHI(); PI != End; ++PI) {
1312-
// Only check target defined prologue instructions
1313-
if (!TII->isBasicBlockPrologue(*PI))
1314-
continue;
1315-
for (auto &MO : MI.operands()) {
1316-
if (!MO.isReg())
1317-
continue;
1318-
Register Reg = MO.getReg();
1319-
if (!Reg)
1320-
continue;
1321-
if (MO.isUse()) {
1322-
if (Reg.isPhysical() &&
1323-
(TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
1324-
continue;
1325-
if (PI->modifiesRegister(Reg, TRI))
1326-
return true;
1327-
} else {
1328-
if (PI->readsRegister(Reg, TRI))
1329-
return true;
1330-
// Check for interference with non-dead defs
1331-
auto *DefOp = PI->findRegisterDefOperand(Reg, false, true, TRI);
1332-
if (DefOp && !DefOp->isDead())
1333-
return true;
1334-
}
1335-
}
1336-
}
1337-
return false;
1338-
}
1339-
13401342
/// SinkInstruction - Determine whether it is safe to sink the specified machine
13411343
/// instruction out of its current block into a successor.
13421344
bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,

0 commit comments

Comments
 (0)