Index: llvm/include/llvm/CodeGen/MachineBasicBlock.h =================================================================== --- llvm/include/llvm/CodeGen/MachineBasicBlock.h +++ llvm/include/llvm/CodeGen/MachineBasicBlock.h @@ -40,6 +40,7 @@ class SlotIndexes; class StringRef; class raw_ostream; +class LiveIntervals; class TargetRegisterClass; class TargetRegisterInfo; @@ -675,6 +676,17 @@ return !empty() && back().isEHScopeReturn(); } + /// Split a basic block into 2 pieces at \p SplitPoint. A new block will be + /// inserted after this block, and all instructions after \p SplitInst moved + /// to it (\p SplitInst will be in the original block). If \p LIS is provided, + /// LiveIntervals will be appropriately updated. \return the newly inserted + /// block. + /// + /// If \p UpdateLiveIns is true, this will ensure the live ins list is + /// accurate, including for physreg uses/defs in the original block. + MachineBasicBlock *splitAt(MachineInstr &SplitInst, bool UpdateLiveIns = true, + LiveIntervals *LIS = nullptr); + /// Split the critical edge from this block to the given successor block, and /// return the newly created block, or null if splitting is not possible. /// Index: llvm/lib/CodeGen/MachineBasicBlock.cpp =================================================================== --- llvm/lib/CodeGen/MachineBasicBlock.cpp +++ llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -944,6 +944,46 @@ return getFallThrough() != nullptr; } +MachineBasicBlock *MachineBasicBlock::splitAt(MachineInstr &MI, + bool UpdateLiveIns, + LiveIntervals *LIS) { + MachineBasicBlock::iterator SplitPoint(&MI); + ++SplitPoint; + + if (SplitPoint == end()) { + // Don't bother with a new block. + return this; + } + + MachineFunction *MF = getParent(); + + LivePhysRegs LiveRegs; + if (UpdateLiveIns) { + // Make sure we add any physregs we define in the block as liveins to the + // new block. + LiveRegs.init(*MF->getSubtarget().getRegisterInfo()); + LiveRegs.addLiveOuts(*this); + for (auto I = rbegin(), E = SplitPoint.getReverse(); I != E; ++I) + LiveRegs.stepBackward(*I); + } + + MachineBasicBlock *SplitBB = MF->CreateMachineBasicBlock(getBasicBlock()); + + MF->insert(++MachineFunction::iterator(this), SplitBB); + SplitBB->splice(SplitBB->begin(), this, SplitPoint, end()); + + SplitBB->transferSuccessorsAndUpdatePHIs(this); + addSuccessor(SplitBB); + + if (UpdateLiveIns) + addLiveIns(*SplitBB, LiveRegs); + + if (LIS) + LIS->insertMBBInMaps(SplitBB, &MI); + + return SplitBB; +} + MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge( MachineBasicBlock *Succ, Pass &P, std::vector> *LiveInSets) { Index: llvm/lib/Target/AMDGPU/SIISelLowering.cpp =================================================================== --- llvm/lib/Target/AMDGPU/SIISelLowering.cpp +++ llvm/lib/Target/AMDGPU/SIISelLowering.cpp @@ -3304,29 +3304,11 @@ // If kill is not the last instruction, split the block so kill is always a // proper terminator. -MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, - MachineBasicBlock *BB) const { +MachineBasicBlock * +SITargetLowering::splitKillBlock(MachineInstr &MI, + MachineBasicBlock *BB) const { + MachineBasicBlock *SplitBB = BB->splitAt(MI, false /*UpdateLiveIns*/); const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); - - MachineBasicBlock::iterator SplitPoint(&MI); - ++SplitPoint; - - if (SplitPoint == BB->end()) { - // Don't bother with a new block. - MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); - return BB; - } - - MachineFunction *MF = BB->getParent(); - MachineBasicBlock *SplitBB - = MF->CreateMachineBasicBlock(BB->getBasicBlock()); - - MF->insert(++MachineFunction::iterator(BB), SplitBB); - SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); - - SplitBB->transferSuccessorsAndUpdatePHIs(BB); - BB->addSuccessor(SplitBB); - MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); return SplitBB; } Index: llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp =================================================================== --- llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp +++ llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp @@ -108,8 +108,6 @@ void emitIfBreak(MachineInstr &MI); void emitLoop(MachineInstr &MI); - MachineBasicBlock *splitBlock(MachineInstr &MI, MachineBasicBlock *BB, - LiveIntervals *LIS); MachineBasicBlock *emitEndCf(MachineInstr &MI); void findMaskOperands(MachineInstr &MI, unsigned OpNo, @@ -493,42 +491,6 @@ } while (true); } -MachineBasicBlock *SILowerControlFlow::splitBlock(MachineInstr &MI, - MachineBasicBlock *BB, - LiveIntervals *LIS) { - MachineBasicBlock::iterator SplitPoint(&MI); - ++SplitPoint; - - if (SplitPoint == BB->end()) { - // Don't bother with a new block. - return BB; - } - - // Make sure we add any physregs we define in the block as liveins to the new - // block. - LivePhysRegs LiveRegs(*TRI); - LiveRegs.addLiveOuts(*BB); - for (auto I = BB->rbegin(), E = SplitPoint.getReverse(); I != E; ++I) - LiveRegs.stepBackward(*I); - - MachineFunction *MF = BB->getParent(); - MachineBasicBlock *SplitBB - = MF->CreateMachineBasicBlock(BB->getBasicBlock()); - - MF->insert(++MachineFunction::iterator(BB), SplitBB); - SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); - - SplitBB->transferSuccessorsAndUpdatePHIs(BB); - BB->addSuccessor(SplitBB); - - addLiveIns(*SplitBB, LiveRegs); - - if (LIS) - LIS->insertMBBInMaps(SplitBB, &MI); - - return SplitBB; -} - MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) { MachineBasicBlock &MBB = *MI.getParent(); const DebugLoc &DL = MI.getDebugLoc(); @@ -551,7 +513,7 @@ unsigned Opcode = OrOpc; MachineBasicBlock *SplitBB = &MBB; if (NeedBlockSplit) { - SplitBB = splitBlock(MI, &MBB, LIS); + SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS); Opcode = OrTermrOpc; InsPt = MI; }