Skip to content

Commit 3105d0f

Browse files
committed
CodeGen: Move split block utility to MachineBasicBlock
AMDGPU needs this in several places, so consolidate them here.
1 parent c8757ff commit 3105d0f

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Printable;
4040
class SlotIndexes;
4141
class StringRef;
4242
class raw_ostream;
43+
class LiveIntervals;
4344
class TargetRegisterClass;
4445
class TargetRegisterInfo;
4546

@@ -675,6 +676,17 @@ class MachineBasicBlock
675676
return !empty() && back().isEHScopeReturn();
676677
}
677678

679+
/// Split a basic block into 2 pieces at \p SplitPoint. A new block will be
680+
/// inserted after this block, and all instructions after \p SplitInst moved
681+
/// to it (\p SplitInst will be in the original block). If \p LIS is provided,
682+
/// LiveIntervals will be appropriately updated. \return the newly inserted
683+
/// block.
684+
///
685+
/// If \p UpdateLiveIns is true, this will ensure the live ins list is
686+
/// accurate, including for physreg uses/defs in the original block.
687+
MachineBasicBlock *splitAt(MachineInstr &SplitInst, bool UpdateLiveIns = true,
688+
LiveIntervals *LIS = nullptr);
689+
678690
/// Split the critical edge from this block to the given successor block, and
679691
/// return the newly created block, or null if splitting is not possible.
680692
///

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,46 @@ bool MachineBasicBlock::canFallThrough() {
944944
return getFallThrough() != nullptr;
945945
}
946946

947+
MachineBasicBlock *MachineBasicBlock::splitAt(MachineInstr &MI,
948+
bool UpdateLiveIns,
949+
LiveIntervals *LIS) {
950+
MachineBasicBlock::iterator SplitPoint(&MI);
951+
++SplitPoint;
952+
953+
if (SplitPoint == end()) {
954+
// Don't bother with a new block.
955+
return this;
956+
}
957+
958+
MachineFunction *MF = getParent();
959+
960+
LivePhysRegs LiveRegs;
961+
if (UpdateLiveIns) {
962+
// Make sure we add any physregs we define in the block as liveins to the
963+
// new block.
964+
LiveRegs.init(*MF->getSubtarget().getRegisterInfo());
965+
LiveRegs.addLiveOuts(*this);
966+
for (auto I = rbegin(), E = SplitPoint.getReverse(); I != E; ++I)
967+
LiveRegs.stepBackward(*I);
968+
}
969+
970+
MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock());
971+
972+
MF->insert(++MachineFunction::iterator(this), SplitBB);
973+
SplitBB->splice(SplitBB->begin(), this, SplitPoint, end());
974+
975+
SplitBB->transferSuccessorsAndUpdatePHIs(this);
976+
addSuccessor(SplitBB);
977+
978+
if (UpdateLiveIns)
979+
addLiveIns(*SplitBB, LiveRegs);
980+
981+
if (LIS)
982+
LIS->insertMBBInMaps(SplitBB, &MI);
983+
984+
return SplitBB;
985+
}
986+
947987
MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
948988
MachineBasicBlock *Succ, Pass &P,
949989
std::vector<SparseBitVector<>> *LiveInSets) {

llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,29 +3334,11 @@ Register SITargetLowering::getRegisterByName(const char* RegName, LLT VT,
33343334

33353335
// If kill is not the last instruction, split the block so kill is always a
33363336
// proper terminator.
3337-
MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
3338-
MachineBasicBlock *BB) const {
3337+
MachineBasicBlock *
3338+
SITargetLowering::splitKillBlock(MachineInstr &MI,
3339+
MachineBasicBlock *BB) const {
3340+
MachineBasicBlock *SplitBB = BB->splitAt(MI, false /*UpdateLiveIns*/);
33393341
const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3340-
3341-
MachineBasicBlock::iterator SplitPoint(&MI);
3342-
++SplitPoint;
3343-
3344-
if (SplitPoint == BB->end()) {
3345-
// Don't bother with a new block.
3346-
MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode()));
3347-
return BB;
3348-
}
3349-
3350-
MachineFunction *MF = BB->getParent();
3351-
MachineBasicBlock *SplitBB
3352-
= MF->CreateMachineBasicBlock(BB->getBasicBlock());
3353-
3354-
MF->insert(++MachineFunction::iterator(BB), SplitBB);
3355-
SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
3356-
3357-
SplitBB->transferSuccessorsAndUpdatePHIs(BB);
3358-
BB->addSuccessor(SplitBB);
3359-
33603342
MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode()));
33613343
return SplitBB;
33623344
}

llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ class SILowerControlFlow : public MachineFunctionPass {
108108
void emitIfBreak(MachineInstr &MI);
109109
void emitLoop(MachineInstr &MI);
110110

111-
MachineBasicBlock *splitBlock(MachineInstr &MI, MachineBasicBlock *BB,
112-
LiveIntervals *LIS);
113111
MachineBasicBlock *emitEndCf(MachineInstr &MI);
114112

115113
void findMaskOperands(MachineInstr &MI, unsigned OpNo,
@@ -493,42 +491,6 @@ SILowerControlFlow::skipIgnoreExecInstsTrivialSucc(
493491
} while (true);
494492
}
495493

496-
MachineBasicBlock *SILowerControlFlow::splitBlock(MachineInstr &MI,
497-
MachineBasicBlock *BB,
498-
LiveIntervals *LIS) {
499-
MachineBasicBlock::iterator SplitPoint(&MI);
500-
++SplitPoint;
501-
502-
if (SplitPoint == BB->end()) {
503-
// Don't bother with a new block.
504-
return BB;
505-
}
506-
507-
// Make sure we add any physregs we define in the block as liveins to the new
508-
// block.
509-
LivePhysRegs LiveRegs(*TRI);
510-
LiveRegs.addLiveOuts(*BB);
511-
for (auto I = BB->rbegin(), E = SplitPoint.getReverse(); I != E; ++I)
512-
LiveRegs.stepBackward(*I);
513-
514-
MachineFunction *MF = BB->getParent();
515-
MachineBasicBlock *SplitBB
516-
= MF->CreateMachineBasicBlock(BB->getBasicBlock());
517-
518-
MF->insert(++MachineFunction::iterator(BB), SplitBB);
519-
SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
520-
521-
SplitBB->transferSuccessorsAndUpdatePHIs(BB);
522-
BB->addSuccessor(SplitBB);
523-
524-
addLiveIns(*SplitBB, LiveRegs);
525-
526-
if (LIS)
527-
LIS->insertMBBInMaps(SplitBB, &MI);
528-
529-
return SplitBB;
530-
}
531-
532494
MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
533495
MachineBasicBlock &MBB = *MI.getParent();
534496
const DebugLoc &DL = MI.getDebugLoc();
@@ -551,7 +513,7 @@ MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
551513
unsigned Opcode = OrOpc;
552514
MachineBasicBlock *SplitBB = &MBB;
553515
if (NeedBlockSplit) {
554-
SplitBB = splitBlock(MI, &MBB, LIS);
516+
SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
555517
Opcode = OrTermrOpc;
556518
InsPt = MI;
557519
}

0 commit comments

Comments
 (0)