5555#include " llvm/ADT/StringRef.h"
5656#include " llvm/CodeGen/LiveIntervals.h"
5757#include " llvm/CodeGen/MachineBasicBlock.h"
58- #include " llvm/CodeGen/MachineDominators.h"
5958#include " llvm/CodeGen/MachineFunction.h"
6059#include " llvm/CodeGen/MachineFunctionPass.h"
6160#include " llvm/CodeGen/MachineInstr.h"
@@ -80,11 +79,8 @@ class SILowerControlFlow : public MachineFunctionPass {
8079private:
8180 const SIRegisterInfo *TRI = nullptr ;
8281 const SIInstrInfo *TII = nullptr ;
83- MachineRegisterInfo *MRI = nullptr ;
8482 LiveIntervals *LIS = nullptr ;
85- MachineDominatorTree *DT = nullptr ;
86- MachineLoopInfo *MLI = nullptr ;
87-
83+ MachineRegisterInfo *MRI = nullptr ;
8884
8985 void emitIf (MachineInstr &MI);
9086 void emitElse (MachineInstr &MI);
@@ -115,7 +111,7 @@ class SILowerControlFlow : public MachineFunctionPass {
115111 AU.addPreservedID (LiveVariablesID);
116112 AU.addPreservedID (MachineLoopInfoID);
117113 AU.addPreservedID (MachineDominatorsID);
118-
114+ AU. setPreservesCFG ();
119115 MachineFunctionPass::getAnalysisUsage (AU);
120116 }
121117};
@@ -392,99 +388,23 @@ void SILowerControlFlow::emitLoop(MachineInstr &MI) {
392388 MI.eraseFromParent ();
393389}
394390
395- // Insert \p Inst (which modifies exec) at \p InsPt in \p MBB, such that \p MBB
396- // is split as necessary to keep the exec modification in its own block.
397- static MachineBasicBlock *insertInstWithExecFallthrough (MachineBasicBlock &MBB,
398- MachineInstr &MI,
399- MachineInstr *NewMI,
400- MachineDominatorTree *DT,
401- LiveIntervals *LIS,
402- MachineLoopInfo *MLI) {
403- assert (NewMI->isTerminator ());
404-
405- MachineBasicBlock::iterator InsPt = MI.getIterator ();
406- if (std::next (MI.getIterator ()) == MBB.end ()) {
407- // Don't bother with a new block.
408- MBB.insert (InsPt, NewMI);
409- if (LIS)
410- LIS->ReplaceMachineInstrInMaps (MI, *NewMI);
411- MI.eraseFromParent ();
412- return &MBB;
413- }
414-
415- MachineFunction *MF = MBB.getParent ();
416- MachineBasicBlock *SplitMBB
417- = MF->CreateMachineBasicBlock (MBB.getBasicBlock ());
418-
419- MF->insert (++MachineFunction::iterator (MBB), SplitMBB);
420-
421- // FIXME: This is working around a MachineDominatorTree API defect.
422- //
423- // If a previous pass split a critical edge, it may not have been applied to
424- // the DomTree yet. applySplitCriticalEdges is lazily applied, and inspects
425- // the CFG of the given block. Make sure to call a dominator tree method that
426- // will flush this cache before touching the successors of the block.
427- MachineDomTreeNode *NodeMBB = nullptr ;
428- if (DT)
429- NodeMBB = DT->getNode (&MBB);
430-
431- // Move everything to the new block, except the end_cf pseudo.
432- SplitMBB->splice (SplitMBB->begin (), &MBB, MBB.begin (), MBB.end ());
433-
434- SplitMBB->transferSuccessorsAndUpdatePHIs (&MBB);
435- MBB.addSuccessor (SplitMBB, BranchProbability::getOne ());
436-
437- MBB.insert (MBB.end (), NewMI);
438-
439- if (DT) {
440- std::vector<MachineDomTreeNode *> Children = NodeMBB->getChildren ();
441- DT->addNewBlock (SplitMBB, &MBB);
442-
443- // Reparent all of the children to the new block body.
444- auto *SplitNode = DT->getNode (SplitMBB);
445- for (auto *Child : Children)
446- DT->changeImmediateDominator (Child, SplitNode);
447- }
448-
449- if (MLI) {
450- if (MachineLoop *Loop = MLI->getLoopFor (&MBB))
451- Loop->addBasicBlockToLoop (SplitMBB, MLI->getBase ());
452- }
453-
454- if (LIS) {
455- LIS->insertMBBInMaps (SplitMBB);
456- LIS->ReplaceMachineInstrInMaps (MI, *NewMI);
457- }
458-
459- // All live-ins are forwarded.
460- for (auto &LiveIn : MBB.liveins ())
461- SplitMBB->addLiveIn (LiveIn);
462-
463- MI.eraseFromParent ();
464- return SplitMBB;
465- }
466-
467391void SILowerControlFlow::emitEndCf (MachineInstr &MI) {
468392 MachineBasicBlock &MBB = *MI.getParent ();
469393 const DebugLoc &DL = MI.getDebugLoc ();
470394
471395 MachineBasicBlock::iterator InsPt = MBB.begin ();
396+ MachineInstr *NewMI =
397+ BuildMI (MBB, InsPt, DL, TII->get (AMDGPU::S_OR_B64), AMDGPU::EXEC)
398+ .addReg (AMDGPU::EXEC)
399+ .add (MI.getOperand (0 ));
472400
473- // First, move the instruction. It's unnecessarily difficult to update
474- // LiveIntervals when there's a change in control flow, so move the
475- // instruction before changing the blocks.
476- MBB.splice (InsPt, &MBB, MI.getIterator ());
477401 if (LIS)
478- LIS->handleMove (MI);
402+ LIS->ReplaceMachineInstrInMaps (MI, *NewMI );
479403
480- MachineFunction *MF = MBB. getParent ();
404+ MI. eraseFromParent ();
481405
482- // Create instruction without inserting it yet.
483- MachineInstr *NewMI
484- = BuildMI (*MF, DL, TII->get (AMDGPU::S_OR_B64_term), AMDGPU::EXEC)
485- .addReg (AMDGPU::EXEC)
486- .add (MI.getOperand (0 ));
487- insertInstWithExecFallthrough (MBB, MI, NewMI, DT, LIS, MLI);
406+ if (LIS)
407+ LIS->handleMove (*NewMI);
488408}
489409
490410// Returns replace operands for a logical operation, either single result
@@ -550,20 +470,17 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
550470
551471 // This doesn't actually need LiveIntervals, but we can preserve them.
552472 LIS = getAnalysisIfAvailable<LiveIntervals>();
553- DT = getAnalysisIfAvailable<MachineDominatorTree>();
554- MLI = getAnalysisIfAvailable<MachineLoopInfo>();
555-
556473 MRI = &MF.getRegInfo ();
557474
558475 MachineFunction::iterator NextBB;
559476 for (MachineFunction::iterator BI = MF.begin (), BE = MF.end ();
560477 BI != BE; BI = NextBB) {
561478 NextBB = std::next (BI);
562- MachineBasicBlock * MBB = & *BI;
479+ MachineBasicBlock & MBB = *BI;
563480
564481 MachineBasicBlock::iterator I, Next, Last;
565482
566- for (I = MBB-> begin (), Last = MBB-> end (); I != MBB-> end (); I = Next) {
483+ for (I = MBB. begin (), Last = MBB. end (); I != MBB. end (); I = Next) {
567484 Next = std::next (I);
568485 MachineInstr &MI = *I;
569486
@@ -584,24 +501,10 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
584501 emitLoop (MI);
585502 break ;
586503
587- case AMDGPU::SI_END_CF: {
588- MachineInstr *NextMI = nullptr ;
589-
590- if (Next != MBB->end ())
591- NextMI = &*Next;
592-
504+ case AMDGPU::SI_END_CF:
593505 emitEndCf (MI);
594-
595- if (NextMI) {
596- MBB = NextMI->getParent ();
597- Next = NextMI->getIterator ();
598- Last = MBB->end ();
599- }
600-
601- NextBB = std::next (MBB->getIterator ());
602- BE = MF.end ();
603506 break ;
604- }
507+
605508 case AMDGPU::S_AND_B64:
606509 case AMDGPU::S_OR_B64:
607510 // Cleanup bit manipulations on exec mask
@@ -615,7 +518,7 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
615518 }
616519
617520 // Replay newly inserted code to combine masks
618- Next = (Last == MBB-> end ()) ? MBB-> begin () : Last;
521+ Next = (Last == MBB. end ()) ? MBB. begin () : Last;
619522 }
620523 }
621524
0 commit comments