@@ -79,6 +79,7 @@ class SILowerControlFlow : public MachineFunctionPass {
79
79
SetVector<MachineInstr*> LoweredEndCf;
80
80
DenseSet<Register> LoweredIf;
81
81
SmallSet<MachineBasicBlock *, 4 > KillBlocks;
82
+ SmallSet<Register, 8 > RecomputeRegs;
82
83
83
84
const TargetRegisterClass *BoolRC = nullptr ;
84
85
unsigned AndOpc;
@@ -297,8 +298,7 @@ void SILowerControlFlow::emitIf(MachineInstr &MI) {
297
298
// FIXME: Is there a better way of adjusting the liveness? It shouldn't be
298
299
// hard to add another def here but I'm not sure how to correctly update the
299
300
// valno.
300
- LIS->removeInterval (SaveExecReg);
301
- LIS->createAndComputeVirtRegInterval (SaveExecReg);
301
+ RecomputeRegs.insert (SaveExecReg);
302
302
LIS->createAndComputeVirtRegInterval (Tmp);
303
303
if (!SimpleIf)
304
304
LIS->createAndComputeVirtRegInterval (CopyReg);
@@ -309,6 +309,7 @@ void SILowerControlFlow::emitElse(MachineInstr &MI) {
309
309
const DebugLoc &DL = MI.getDebugLoc ();
310
310
311
311
Register DstReg = MI.getOperand (0 ).getReg ();
312
+ Register SrcReg = MI.getOperand (1 ).getReg ();
312
313
313
314
MachineBasicBlock::iterator Start = MBB.begin ();
314
315
@@ -319,7 +320,7 @@ void SILowerControlFlow::emitElse(MachineInstr &MI) {
319
320
BuildMI (MBB, Start, DL, TII->get (OrSaveExecOpc), SaveReg)
320
321
.add (MI.getOperand (1 )); // Saved EXEC
321
322
if (LV)
322
- LV->replaceKillInstruction (MI. getOperand ( 1 ). getReg () , MI, *OrSaveExec);
323
+ LV->replaceKillInstruction (SrcReg , MI, *OrSaveExec);
323
324
324
325
MachineBasicBlock *DestBB = MI.getOperand (2 ).getMBB ();
325
326
@@ -331,9 +332,6 @@ void SILowerControlFlow::emitElse(MachineInstr &MI) {
331
332
.addReg (Exec)
332
333
.addReg (SaveReg);
333
334
334
- if (LIS)
335
- LIS->InsertMachineInstrInMaps (*And);
336
-
337
335
MachineInstr *Xor =
338
336
BuildMI (MBB, ElsePt, DL, TII->get (XorTermrOpc), Exec)
339
337
.addReg (Exec)
@@ -356,12 +354,13 @@ void SILowerControlFlow::emitElse(MachineInstr &MI) {
356
354
MI.eraseFromParent ();
357
355
358
356
LIS->InsertMachineInstrInMaps (*OrSaveExec);
357
+ LIS->InsertMachineInstrInMaps (*And);
359
358
360
359
LIS->InsertMachineInstrInMaps (*Xor);
361
360
LIS->InsertMachineInstrInMaps (*Branch);
362
361
363
- LIS-> removeInterval (DstReg );
364
- LIS-> createAndComputeVirtRegInterval (DstReg);
362
+ RecomputeRegs. insert (SrcReg );
363
+ RecomputeRegs. insert (DstReg);
365
364
LIS->createAndComputeVirtRegInterval (SaveReg);
366
365
367
366
// Let this be recomputed.
@@ -388,8 +387,9 @@ void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
388
387
// AND the break condition operand with exec, then OR that into the "loop
389
388
// exit" mask.
390
389
MachineInstr *And = nullptr , *Or = nullptr ;
390
+ Register AndReg;
391
391
if (!SkipAnding) {
392
- Register AndReg = MRI->createVirtualRegister (BoolRC);
392
+ AndReg = MRI->createVirtualRegister (BoolRC);
393
393
And = BuildMI (MBB, &MI, DL, TII->get (AndOpc), AndReg)
394
394
.addReg (Exec)
395
395
.add (MI.getOperand (1 ));
@@ -398,8 +398,6 @@ void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
398
398
Or = BuildMI (MBB, &MI, DL, TII->get (OrOpc), Dst)
399
399
.addReg (AndReg)
400
400
.add (MI.getOperand (2 ));
401
- if (LIS)
402
- LIS->createAndComputeVirtRegInterval (AndReg);
403
401
} else {
404
402
Or = BuildMI (MBB, &MI, DL, TII->get (OrOpc), Dst)
405
403
.add (MI.getOperand (1 ))
@@ -411,9 +409,13 @@ void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
411
409
LV->replaceKillInstruction (MI.getOperand (2 ).getReg (), MI, *Or);
412
410
413
411
if (LIS) {
414
- if (And)
415
- LIS->InsertMachineInstrInMaps (*And);
416
412
LIS->ReplaceMachineInstrInMaps (MI, *Or);
413
+ if (And) {
414
+ // Read of original operand 1 is on And now not Or.
415
+ RecomputeRegs.insert (And->getOperand (2 ).getReg ());
416
+ LIS->InsertMachineInstrInMaps (*And);
417
+ LIS->createAndComputeVirtRegInterval (AndReg);
418
+ }
417
419
}
418
420
419
421
MI.eraseFromParent ();
@@ -436,6 +438,7 @@ void SILowerControlFlow::emitLoop(MachineInstr &MI) {
436
438
.add (MI.getOperand (1 ));
437
439
438
440
if (LIS) {
441
+ RecomputeRegs.insert (MI.getOperand (0 ).getReg ());
439
442
LIS->ReplaceMachineInstrInMaps (MI, *AndN2);
440
443
LIS->InsertMachineInstrInMaps (*Branch);
441
444
}
@@ -714,11 +717,13 @@ void SILowerControlFlow::lowerInitExec(MachineBasicBlock *MBB,
714
717
715
718
if (MI.getOpcode () == AMDGPU::SI_INIT_EXEC) {
716
719
// This should be before all vector instructions.
717
- BuildMI (*MBB, MBB->begin (), MI.getDebugLoc (),
720
+ MachineInstr *InitMI = BuildMI (*MBB, MBB->begin (), MI.getDebugLoc (),
718
721
TII->get (IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64), Exec)
719
722
.addImm (MI.getOperand (0 ).getImm ());
720
- if (LIS)
723
+ if (LIS) {
721
724
LIS->RemoveMachineInstrFromMaps (MI);
725
+ LIS->InsertMachineInstrInMaps (*InitMI);
726
+ }
722
727
MI.eraseFromParent ();
723
728
return ;
724
729
}
@@ -789,8 +794,7 @@ void SILowerControlFlow::lowerInitExec(MachineBasicBlock *MBB,
789
794
LIS->InsertMachineInstrInMaps (*CmpMI);
790
795
LIS->InsertMachineInstrInMaps (*CmovMI);
791
796
792
- LIS->removeInterval (InputReg);
793
- LIS->createAndComputeVirtRegInterval (InputReg);
797
+ RecomputeRegs.insert (InputReg);
794
798
LIS->createAndComputeVirtRegInterval (CountReg);
795
799
}
796
800
@@ -807,7 +811,7 @@ bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
807
811
808
812
while (!MBB.predecessors ().empty ()) {
809
813
MachineBasicBlock *P = *MBB.pred_begin ();
810
- if (P->getFallThrough () == &MBB)
814
+ if (P->getFallThrough (false ) == &MBB)
811
815
FallThrough = P;
812
816
P->ReplaceUsesOfBlockWith (&MBB, Succ);
813
817
}
@@ -828,14 +832,13 @@ bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
828
832
MBB.clear ();
829
833
MBB.eraseFromParent ();
830
834
if (FallThrough && !FallThrough->isLayoutSuccessor (Succ)) {
831
- if (!Succ->canFallThrough ()) {
832
- MachineFunction *MF = FallThrough->getParent ();
833
- MachineFunction::iterator FallThroughPos (FallThrough);
834
- MF->splice (std::next (FallThroughPos), Succ);
835
- } else
836
- BuildMI (*FallThrough, FallThrough->end (),
837
- FallThrough->findBranchDebugLoc (), TII->get (AMDGPU::S_BRANCH))
838
- .addMBB (Succ);
835
+ // Note: we cannot update block layout and preserve live intervals;
836
+ // hence we must insert a branch.
837
+ MachineInstr *BranchMI = BuildMI (*FallThrough, FallThrough->end (),
838
+ FallThrough->findBranchDebugLoc (), TII->get (AMDGPU::S_BRANCH))
839
+ .addMBB (Succ);
840
+ if (LIS)
841
+ LIS->InsertMachineInstrInMaps (*BranchMI);
839
842
}
840
843
841
844
return true ;
@@ -947,6 +950,14 @@ bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
947
950
948
951
optimizeEndCf ();
949
952
953
+ if (LIS) {
954
+ for (Register Reg : RecomputeRegs) {
955
+ LIS->removeInterval (Reg);
956
+ LIS->createAndComputeVirtRegInterval (Reg);
957
+ }
958
+ }
959
+
960
+ RecomputeRegs.clear ();
950
961
LoweredEndCf.clear ();
951
962
LoweredIf.clear ();
952
963
KillBlocks.clear ();
0 commit comments