55
55
#include " llvm/ADT/StringRef.h"
56
56
#include " llvm/CodeGen/LiveIntervals.h"
57
57
#include " llvm/CodeGen/MachineBasicBlock.h"
58
- #include " llvm/CodeGen/MachineDominators.h"
59
58
#include " llvm/CodeGen/MachineFunction.h"
60
59
#include " llvm/CodeGen/MachineFunctionPass.h"
61
60
#include " llvm/CodeGen/MachineInstr.h"
@@ -80,16 +79,12 @@ class SILowerControlFlow : public MachineFunctionPass {
80
79
private:
81
80
const SIRegisterInfo *TRI = nullptr ;
82
81
const SIInstrInfo *TII = nullptr ;
83
- MachineRegisterInfo *MRI = nullptr ;
84
82
LiveIntervals *LIS = nullptr ;
85
- MachineDominatorTree *DT = nullptr ;
86
- MachineLoopInfo *MLI = nullptr ;
87
-
83
+ MachineRegisterInfo *MRI = nullptr ;
88
84
89
85
const TargetRegisterClass *BoolRC = nullptr ;
90
86
unsigned AndOpc;
91
87
unsigned OrOpc;
92
- unsigned OrTermOpc;
93
88
unsigned XorOpc;
94
89
unsigned MovTermOpc;
95
90
unsigned Andn2TermOpc;
@@ -126,7 +121,7 @@ class SILowerControlFlow : public MachineFunctionPass {
126
121
AU.addPreservedID (LiveVariablesID);
127
122
AU.addPreservedID (MachineLoopInfoID);
128
123
AU.addPreservedID (MachineDominatorsID);
129
-
124
+ AU. setPreservesCFG ();
130
125
MachineFunctionPass::getAnalysisUsage (AU);
131
126
}
132
127
};
@@ -254,7 +249,7 @@ void SILowerControlFlow::emitIf(MachineInstr &MI) {
254
249
LIS->InsertMachineInstrInMaps (*SetExec);
255
250
LIS->InsertMachineInstrInMaps (*NewBr);
256
251
257
- LIS->removeAllRegUnitsForPhysReg (Exec );
252
+ LIS->removeAllRegUnitsForPhysReg (AMDGPU::EXEC );
258
253
MI.eraseFromParent ();
259
254
260
255
// FIXME: Is there a better way of adjusting the liveness? It shouldn't be
@@ -338,7 +333,7 @@ void SILowerControlFlow::emitElse(MachineInstr &MI) {
338
333
LIS->createAndComputeVirtRegInterval (SaveReg);
339
334
340
335
// Let this be recomputed.
341
- LIS->removeAllRegUnitsForPhysReg (Exec );
336
+ LIS->removeAllRegUnitsForPhysReg (AMDGPU::EXEC );
342
337
}
343
338
344
339
void SILowerControlFlow::emitIfBreak (MachineInstr &MI) {
@@ -403,99 +398,23 @@ void SILowerControlFlow::emitLoop(MachineInstr &MI) {
403
398
MI.eraseFromParent ();
404
399
}
405
400
406
- // Insert \p Inst (which modifies exec) at \p InsPt in \p MBB, such that \p MBB
407
- // is split as necessary to keep the exec modification in its own block.
408
- static MachineBasicBlock *insertInstWithExecFallthrough (MachineBasicBlock &MBB,
409
- MachineInstr &MI,
410
- MachineInstr *NewMI,
411
- MachineDominatorTree *DT,
412
- LiveIntervals *LIS,
413
- MachineLoopInfo *MLI) {
414
- assert (NewMI->isTerminator ());
415
-
416
- MachineBasicBlock::iterator InsPt = MI.getIterator ();
417
- if (std::next (MI.getIterator ()) == MBB.end ()) {
418
- // Don't bother with a new block.
419
- MBB.insert (InsPt, NewMI);
420
- if (LIS)
421
- LIS->ReplaceMachineInstrInMaps (MI, *NewMI);
422
- MI.eraseFromParent ();
423
- return &MBB;
424
- }
425
-
426
- MachineFunction *MF = MBB.getParent ();
427
- MachineBasicBlock *SplitMBB
428
- = MF->CreateMachineBasicBlock (MBB.getBasicBlock ());
429
-
430
- MF->insert (++MachineFunction::iterator (MBB), SplitMBB);
431
-
432
- // FIXME: This is working around a MachineDominatorTree API defect.
433
- //
434
- // If a previous pass split a critical edge, it may not have been applied to
435
- // the DomTree yet. applySplitCriticalEdges is lazily applied, and inspects
436
- // the CFG of the given block. Make sure to call a dominator tree method that
437
- // will flush this cache before touching the successors of the block.
438
- MachineDomTreeNode *NodeMBB = nullptr ;
439
- if (DT)
440
- NodeMBB = DT->getNode (&MBB);
441
-
442
- // Move everything to the new block, except the end_cf pseudo.
443
- SplitMBB->splice (SplitMBB->begin (), &MBB, MBB.begin (), MBB.end ());
444
-
445
- SplitMBB->transferSuccessorsAndUpdatePHIs (&MBB);
446
- MBB.addSuccessor (SplitMBB, BranchProbability::getOne ());
447
-
448
- MBB.insert (MBB.end (), NewMI);
449
-
450
- if (DT) {
451
- std::vector<MachineDomTreeNode *> Children = NodeMBB->getChildren ();
452
- DT->addNewBlock (SplitMBB, &MBB);
453
-
454
- // Reparent all of the children to the new block body.
455
- auto *SplitNode = DT->getNode (SplitMBB);
456
- for (auto *Child : Children)
457
- DT->changeImmediateDominator (Child, SplitNode);
458
- }
459
-
460
- if (MLI) {
461
- if (MachineLoop *Loop = MLI->getLoopFor (&MBB))
462
- Loop->addBasicBlockToLoop (SplitMBB, MLI->getBase ());
463
- }
464
-
465
- if (LIS) {
466
- LIS->insertMBBInMaps (SplitMBB);
467
- LIS->ReplaceMachineInstrInMaps (MI, *NewMI);
468
- }
469
-
470
- // All live-ins are forwarded.
471
- for (auto &LiveIn : MBB.liveins ())
472
- SplitMBB->addLiveIn (LiveIn);
473
-
474
- MI.eraseFromParent ();
475
- return SplitMBB;
476
- }
477
-
478
401
void SILowerControlFlow::emitEndCf (MachineInstr &MI) {
479
402
MachineBasicBlock &MBB = *MI.getParent ();
480
403
const DebugLoc &DL = MI.getDebugLoc ();
481
404
482
405
MachineBasicBlock::iterator InsPt = MBB.begin ();
406
+ MachineInstr *NewMI =
407
+ BuildMI (MBB, InsPt, DL, TII->get (OrOpc), Exec)
408
+ .addReg (Exec)
409
+ .add (MI.getOperand (0 ));
483
410
484
- // First, move the instruction. It's unnecessarily difficult to update
485
- // LiveIntervals when there's a change in control flow, so move the
486
- // instruction before changing the blocks.
487
- MBB.splice (InsPt, &MBB, MI.getIterator ());
488
411
if (LIS)
489
- LIS->handleMove (MI);
412
+ LIS->ReplaceMachineInstrInMaps (MI, *NewMI );
490
413
491
- MachineFunction *MF = MBB. getParent ();
414
+ MI. eraseFromParent ();
492
415
493
- // Create instruction without inserting it yet.
494
- MachineInstr *NewMI
495
- = BuildMI (*MF, DL, TII->get (OrTermOpc), Exec)
496
- .addReg (Exec)
497
- .add (MI.getOperand (0 ));
498
- insertInstWithExecFallthrough (MBB, MI, NewMI, DT, LIS, MLI);
416
+ if (LIS)
417
+ LIS->handleMove (*NewMI);
499
418
}
500
419
501
420
// Returns replace operands for a logical operation, either single result
@@ -517,7 +436,7 @@ void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo,
517
436
// A copy with implcitly defined exec inserted earlier is an exclusion, it
518
437
// does not really modify exec.
519
438
for (auto I = Def->getIterator (); I != MI.getIterator (); ++I)
520
- if (I->modifiesRegister (Exec , TRI) &&
439
+ if (I->modifiesRegister (AMDGPU::EXEC , TRI) &&
521
440
!(I->isCopy () && I->getOperand (0 ).getReg () != Exec))
522
441
return ;
523
442
@@ -560,16 +479,12 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
560
479
561
480
// This doesn't actually need LiveIntervals, but we can preserve them.
562
481
LIS = getAnalysisIfAvailable<LiveIntervals>();
563
- DT = getAnalysisIfAvailable<MachineDominatorTree>();
564
- MLI = getAnalysisIfAvailable<MachineLoopInfo>();
565
-
566
482
MRI = &MF.getRegInfo ();
567
483
BoolRC = TRI->getBoolRC ();
568
484
569
485
if (ST.isWave32 ()) {
570
486
AndOpc = AMDGPU::S_AND_B32;
571
487
OrOpc = AMDGPU::S_OR_B32;
572
- OrTermOpc = AMDGPU::S_OR_B32_term;
573
488
XorOpc = AMDGPU::S_XOR_B32;
574
489
MovTermOpc = AMDGPU::S_MOV_B32_term;
575
490
Andn2TermOpc = AMDGPU::S_ANDN2_B32_term;
@@ -579,7 +494,6 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
579
494
} else {
580
495
AndOpc = AMDGPU::S_AND_B64;
581
496
OrOpc = AMDGPU::S_OR_B64;
582
- OrTermOpc = AMDGPU::S_OR_B64_term;
583
497
XorOpc = AMDGPU::S_XOR_B64;
584
498
MovTermOpc = AMDGPU::S_MOV_B64_term;
585
499
Andn2TermOpc = AMDGPU::S_ANDN2_B64_term;
@@ -592,11 +506,11 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
592
506
for (MachineFunction::iterator BI = MF.begin (), BE = MF.end ();
593
507
BI != BE; BI = NextBB) {
594
508
NextBB = std::next (BI);
595
- MachineBasicBlock * MBB = & *BI;
509
+ MachineBasicBlock & MBB = *BI;
596
510
597
511
MachineBasicBlock::iterator I, Next, Last;
598
512
599
- for (I = MBB-> begin (), Last = MBB-> end (); I != MBB-> end (); I = Next) {
513
+ for (I = MBB. begin (), Last = MBB. end (); I != MBB. end (); I = Next) {
600
514
Next = std::next (I);
601
515
MachineInstr &MI = *I;
602
516
@@ -617,24 +531,10 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
617
531
emitLoop (MI);
618
532
break ;
619
533
620
- case AMDGPU::SI_END_CF: {
621
- MachineInstr *NextMI = nullptr ;
622
-
623
- if (Next != MBB->end ())
624
- NextMI = &*Next;
625
-
534
+ case AMDGPU::SI_END_CF:
626
535
emitEndCf (MI);
627
-
628
- if (NextMI) {
629
- MBB = NextMI->getParent ();
630
- Next = NextMI->getIterator ();
631
- Last = MBB->end ();
632
- }
633
-
634
- NextBB = std::next (MBB->getIterator ());
635
- BE = MF.end ();
636
536
break ;
637
- }
537
+
638
538
case AMDGPU::S_AND_B64:
639
539
case AMDGPU::S_OR_B64:
640
540
case AMDGPU::S_AND_B32:
@@ -650,7 +550,7 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
650
550
}
651
551
652
552
// Replay newly inserted code to combine masks
653
- Next = (Last == MBB-> end ()) ? MBB-> begin () : Last;
553
+ Next = (Last == MBB. end ()) ? MBB. begin () : Last;
654
554
}
655
555
}
656
556
0 commit comments