@@ -42,6 +42,7 @@ STATISTIC(numExtends , "Number of copies extended");
42
42
STATISTIC (NumReMats , " Number of instructions re-materialized" );
43
43
STATISTIC (numPeep , " Number of identity moves eliminated after coalescing" );
44
44
STATISTIC (numAborts , " Number of times interval joining aborted" );
45
+ STATISTIC (numDeadValNo, " Number of valno def marked dead" );
45
46
46
47
char SimpleRegisterCoalescing::ID = 0 ;
47
48
static cl::opt<bool >
@@ -450,6 +451,97 @@ bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
450
451
return true ;
451
452
}
452
453
454
+ // / isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
455
+ // / fallthoughs to SuccMBB.
456
+ static bool isSameOrFallThroughBB (MachineBasicBlock *MBB,
457
+ MachineBasicBlock *SuccMBB,
458
+ const TargetInstrInfo *tii_) {
459
+ if (MBB == SuccMBB)
460
+ return true ;
461
+ MachineBasicBlock *TBB = 0 , *FBB = 0 ;
462
+ SmallVector<MachineOperand, 4 > Cond;
463
+ return !tii_->AnalyzeBranch (*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
464
+ MBB->isSuccessor (SuccMBB);
465
+ }
466
+
467
+ // / removeRange - Wrapper for LiveInterval::removeRange. This removes a range
468
+ // / from a physical register live interval as well as from the live intervals
469
+ // / of its sub-registers.
470
+ static void removeRange (LiveInterval &li, unsigned Start, unsigned End,
471
+ LiveIntervals *li_, const TargetRegisterInfo *tri_) {
472
+ li.removeRange (Start, End, true );
473
+ if (TargetRegisterInfo::isPhysicalRegister (li.reg )) {
474
+ for (const unsigned * SR = tri_->getSubRegisters (li.reg ); *SR; ++SR) {
475
+ if (!li_->hasInterval (*SR))
476
+ continue ;
477
+ LiveInterval &sli = li_->getInterval (*SR);
478
+ unsigned RemoveEnd = Start;
479
+ while (RemoveEnd != End) {
480
+ LiveInterval::iterator LR = sli.FindLiveRangeContaining (Start);
481
+ if (LR == sli.end ())
482
+ break ;
483
+ RemoveEnd = (LR->end < End) ? LR->end : End;
484
+ sli.removeRange (Start, RemoveEnd, true );
485
+ Start = RemoveEnd;
486
+ }
487
+ }
488
+ }
489
+ }
490
+
491
+ // / TrimLiveIntervalToLastUse - If there is a last use in the same basic block
492
+ // / as the copy instruction, trim the live interval to the last use and return
493
+ // / true.
494
+ bool
495
+ SimpleRegisterCoalescing::TrimLiveIntervalToLastUse (unsigned CopyIdx,
496
+ MachineBasicBlock *CopyMBB,
497
+ LiveInterval &li,
498
+ const LiveRange *LR) {
499
+ unsigned MBBStart = li_->getMBBStartIdx (CopyMBB);
500
+ unsigned LastUseIdx;
501
+ MachineOperand *LastUse = lastRegisterUse (LR->start , CopyIdx-1 , li.reg ,
502
+ LastUseIdx);
503
+ if (LastUse) {
504
+ MachineInstr *LastUseMI = LastUse->getParent ();
505
+ if (!isSameOrFallThroughBB (LastUseMI->getParent (), CopyMBB, tii_)) {
506
+ // r1024 = op
507
+ // ...
508
+ // BB1:
509
+ // = r1024
510
+ //
511
+ // BB2:
512
+ // r1025<dead> = r1024<kill>
513
+ if (MBBStart < LR->end )
514
+ removeRange (li, MBBStart, LR->end , li_, tri_);
515
+ return true ;
516
+ }
517
+
518
+ // There are uses before the copy, just shorten the live range to the end
519
+ // of last use.
520
+ LastUse->setIsKill ();
521
+ removeRange (li, li_->getDefIndex (LastUseIdx), LR->end , li_, tri_);
522
+ unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
523
+ if (tii_->isMoveInstr (*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
524
+ DstReg == li.reg ) {
525
+ // Last use is itself an identity code.
526
+ int DeadIdx = LastUseMI->findRegisterDefOperandIdx (li.reg , false , tri_);
527
+ LastUseMI->getOperand (DeadIdx).setIsDead ();
528
+ }
529
+ return true ;
530
+ }
531
+
532
+ // Is it livein?
533
+ if (LR->start <= MBBStart && LR->end > MBBStart) {
534
+ if (LR->start == 0 ) {
535
+ assert (TargetRegisterInfo::isPhysicalRegister (li.reg ));
536
+ // Live-in to the function but dead. Remove it from entry live-in set.
537
+ mf_->begin ()->removeLiveIn (li.reg );
538
+ }
539
+ // FIXME: Shorten intervals in BBs that reaches this BB.
540
+ }
541
+
542
+ return false ;
543
+ }
544
+
453
545
// / ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
454
546
// / computation, replace the copy by rematerialize the definition.
455
547
bool SimpleRegisterCoalescing::ReMaterializeTrivialDef (LiveInterval &SrcInt,
@@ -467,6 +559,9 @@ bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
467
559
const TargetInstrDesc &TID = DefMI->getDesc ();
468
560
if (!TID.isAsCheapAsAMove ())
469
561
return false ;
562
+ if (!DefMI->getDesc ().isRematerializable () ||
563
+ !tii_->isTriviallyReMaterializable (DefMI))
564
+ return false ;
470
565
bool SawStore = false ;
471
566
if (!DefMI->isSafeToMove (tii_, SawStore))
472
567
return false ;
@@ -485,7 +580,12 @@ bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
485
580
}
486
581
}
487
582
583
+ // If copy kills the source register, find the last use and propagate
584
+ // kill.
488
585
MachineBasicBlock *MBB = CopyMI->getParent ();
586
+ if (CopyMI->killsRegister (SrcInt.reg ))
587
+ TrimLiveIntervalToLastUse (CopyIdx, MBB, SrcInt, SrcLR);
588
+
489
589
MachineBasicBlock::iterator MII = next (MachineBasicBlock::iterator (CopyMI));
490
590
CopyMI->removeFromParent ();
491
591
tii_->reMaterialize (*MBB, MII, DstReg, DefMI);
@@ -660,30 +760,6 @@ void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg,
660
760
}
661
761
}
662
762
663
- // / removeRange - Wrapper for LiveInterval::removeRange. This removes a range
664
- // / from a physical register live interval as well as from the live intervals
665
- // / of its sub-registers.
666
- static void removeRange (LiveInterval &li, unsigned Start, unsigned End,
667
- LiveIntervals *li_, const TargetRegisterInfo *tri_) {
668
- li.removeRange (Start, End, true );
669
- if (TargetRegisterInfo::isPhysicalRegister (li.reg )) {
670
- for (const unsigned * SR = tri_->getSubRegisters (li.reg ); *SR; ++SR) {
671
- if (!li_->hasInterval (*SR))
672
- continue ;
673
- LiveInterval &sli = li_->getInterval (*SR);
674
- unsigned RemoveEnd = Start;
675
- while (RemoveEnd != End) {
676
- LiveInterval::iterator LR = sli.FindLiveRangeContaining (Start);
677
- if (LR == sli.end ())
678
- break ;
679
- RemoveEnd = (LR->end < End) ? LR->end : End;
680
- sli.removeRange (Start, RemoveEnd, true );
681
- Start = RemoveEnd;
682
- }
683
- }
684
- }
685
- }
686
-
687
763
// / removeIntervalIfEmpty - Check if the live interval of a physical register
688
764
// / is empty, if so remove it and also remove the empty intervals of its
689
765
// / sub-registers. Return true if live interval is removed.
@@ -752,19 +828,6 @@ static void PropagateDeadness(LiveInterval &li, MachineInstr *CopyMI,
752
828
}
753
829
}
754
830
755
- // / isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
756
- // / fallthoughs to SuccMBB.
757
- static bool isSameOrFallThroughBB (MachineBasicBlock *MBB,
758
- MachineBasicBlock *SuccMBB,
759
- const TargetInstrInfo *tii_) {
760
- if (MBB == SuccMBB)
761
- return true ;
762
- MachineBasicBlock *TBB = 0 , *FBB = 0 ;
763
- SmallVector<MachineOperand, 4 > Cond;
764
- return !tii_->AnalyzeBranch (*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
765
- MBB->isSuccessor (SuccMBB);
766
- }
767
-
768
831
// / ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
769
832
// / extended by a dead copy. Mark the last use (if any) of the val# as kill as
770
833
// / ends the live range there. If there isn't another use, then this live range
@@ -796,55 +859,31 @@ SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li,
796
859
// More uses past this copy? Nothing to do.
797
860
return false ;
798
861
862
+ // If there is a last use in the same bb, we can't remove the live range.
863
+ // Shorten the live interval and return.
799
864
MachineBasicBlock *CopyMBB = CopyMI->getParent ();
800
- unsigned MBBStart = li_->getMBBStartIdx (CopyMBB);
801
- unsigned LastUseIdx;
802
- MachineOperand *LastUse = lastRegisterUse (LR->start , CopyIdx-1 , li.reg ,
803
- LastUseIdx);
804
- if (LastUse) {
805
- MachineInstr *LastUseMI = LastUse->getParent ();
806
- if (!isSameOrFallThroughBB (LastUseMI->getParent (), CopyMBB, tii_)) {
807
- // r1024 = op
808
- // ...
809
- // BB1:
810
- // = r1024
811
- //
812
- // BB2:
813
- // r1025<dead> = r1024<kill>
814
- if (MBBStart < LR->end )
815
- removeRange (li, MBBStart, LR->end , li_, tri_);
816
- return false ;
817
- }
818
-
819
- // There are uses before the copy, just shorten the live range to the end
820
- // of last use.
821
- LastUse->setIsKill ();
822
- removeRange (li, li_->getDefIndex (LastUseIdx), LR->end , li_, tri_);
823
- unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
824
- if (tii_->isMoveInstr (*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
825
- DstReg == li.reg ) {
826
- // Last use is itself an identity code.
827
- int DeadIdx = LastUseMI->findRegisterDefOperandIdx (li.reg , false , tri_);
828
- LastUseMI->getOperand (DeadIdx).setIsDead ();
829
- }
865
+ if (TrimLiveIntervalToLastUse (CopyIdx, CopyMBB, li, LR))
830
866
return false ;
831
- }
832
867
833
- // Is it livein?
834
- if (LR->start <= MBBStart && LR->end > MBBStart) {
835
- if (LR->start == 0 ) {
836
- assert (TargetRegisterInfo::isPhysicalRegister (li.reg ));
837
- // Live-in to the function but dead. Remove it from entry live-in set.
838
- mf_->begin ()->removeLiveIn (li.reg );
868
+ MachineBasicBlock *StartMBB = li_->getMBBFromIndex (RemoveStart);
869
+ if (!isSameOrFallThroughBB (StartMBB, CopyMBB, tii_))
870
+ // If the live range starts in another mbb and the copy mbb is not a fall
871
+ // through mbb, then we can only cut the range from the beginning of the
872
+ // copy mbb.
873
+ RemoveStart = li_->getMBBStartIdx (CopyMBB) + 1 ;
874
+
875
+ if (LR->valno ->def == RemoveStart) {
876
+ // If the def MI defines the val# and this copy is the only kill of the
877
+ // val#, then propagate the dead marker.
878
+ if (li.isOnlyLROfValNo (LR)) {
879
+ PropagateDeadness (li, CopyMI, RemoveStart, li_, tri_);
880
+ ++numDeadValNo;
839
881
}
840
- // FIXME: Shorten intervals in BBs that reaches this BB.
882
+ if (li.isKill (LR->valno , RemoveEnd))
883
+ li.removeKill (LR->valno , RemoveEnd);
841
884
}
842
885
843
- if (LR->valno ->def == RemoveStart)
844
- // If the def MI defines the val#, propagate the dead marker.
845
- PropagateDeadness (li, CopyMI, RemoveStart, li_, tri_);
846
-
847
- removeRange (li, RemoveStart, LR->end , li_, tri_);
886
+ removeRange (li, RemoveStart, RemoveEnd, li_, tri_);
848
887
return removeIntervalIfEmpty (li, li_, tri_);
849
888
}
850
889
@@ -2460,6 +2499,8 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
2460
2499
if (!MO.isReg ())
2461
2500
continue ;
2462
2501
unsigned Reg = MO.getReg ();
2502
+ if (!Reg)
2503
+ continue ;
2463
2504
if (TargetRegisterInfo::isVirtualRegister (Reg))
2464
2505
DeadDefs.push_back (Reg);
2465
2506
if (MO.isDead ())
0 commit comments