llvm.org GIT mirror llvm / 8396e76
Merge all changes to SimpleRegisterCoalescing to the release. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_25@64785 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 11 years ago
2 changed file(s) with 135 addition(s) and 85 deletion(s). Raw diff Collapse all Expand all
4141 STATISTIC(NumReMats , "Number of instructions re-materialized");
4242 STATISTIC(numPeep , "Number of identity moves eliminated after coalescing");
4343 STATISTIC(numAborts , "Number of times interval joining aborted");
44 STATISTIC(numDeadValNo, "Number of valno def marked dead");
4445
4546 char SimpleRegisterCoalescing::ID = 0;
4647 static cl::opt
449450 return true;
450451 }
451452
453 /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
454 /// fallthoughs to SuccMBB.
455 static bool isSameOrFallThroughBB(MachineBasicBlock *MBB,
456 MachineBasicBlock *SuccMBB,
457 const TargetInstrInfo *tii_) {
458 if (MBB == SuccMBB)
459 return true;
460 MachineBasicBlock *TBB = 0, *FBB = 0;
461 SmallVector Cond;
462 return !tii_->AnalyzeBranch(*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
463 MBB->isSuccessor(SuccMBB);
464 }
465
466 /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range
467 /// from a physical register live interval as well as from the live intervals
468 /// of its sub-registers.
469 static void removeRange(LiveInterval &li, unsigned Start, unsigned End,
470 LiveIntervals *li_, const TargetRegisterInfo *tri_) {
471 li.removeRange(Start, End, true);
472 if (TargetRegisterInfo::isPhysicalRegister(li.reg)) {
473 for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
474 if (!li_->hasInterval(*SR))
475 continue;
476 LiveInterval &sli = li_->getInterval(*SR);
477 unsigned RemoveEnd = Start;
478 while (RemoveEnd != End) {
479 LiveInterval::iterator LR = sli.FindLiveRangeContaining(Start);
480 if (LR == sli.end())
481 break;
482 RemoveEnd = (LR->end < End) ? LR->end : End;
483 sli.removeRange(Start, RemoveEnd, true);
484 Start = RemoveEnd;
485 }
486 }
487 }
488 }
489
490 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic block
491 /// as the copy instruction, trim the live interval to the last use and return
492 /// true.
493 bool
494 SimpleRegisterCoalescing::TrimLiveIntervalToLastUse(unsigned CopyIdx,
495 MachineBasicBlock *CopyMBB,
496 LiveInterval &li,
497 const LiveRange *LR) {
498 unsigned MBBStart = li_->getMBBStartIdx(CopyMBB);
499 unsigned LastUseIdx;
500 MachineOperand *LastUse = lastRegisterUse(LR->start, CopyIdx-1, li.reg,
501 LastUseIdx);
502 if (LastUse) {
503 MachineInstr *LastUseMI = LastUse->getParent();
504 if (!isSameOrFallThroughBB(LastUseMI->getParent(), CopyMBB, tii_)) {
505 // r1024 = op
506 // ...
507 // BB1:
508 // = r1024
509 //
510 // BB2:
511 // r1025 = r1024
512 if (MBBStart < LR->end)
513 removeRange(li, MBBStart, LR->end, li_, tri_);
514 return true;
515 }
516
517 // There are uses before the copy, just shorten the live range to the end
518 // of last use.
519 LastUse->setIsKill();
520 removeRange(li, li_->getDefIndex(LastUseIdx), LR->end, li_, tri_);
521 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
522 if (tii_->isMoveInstr(*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
523 DstReg == li.reg) {
524 // Last use is itself an identity code.
525 int DeadIdx = LastUseMI->findRegisterDefOperandIdx(li.reg, false, tri_);
526 LastUseMI->getOperand(DeadIdx).setIsDead();
527 }
528 return true;
529 }
530
531 // Is it livein?
532 if (LR->start <= MBBStart && LR->end > MBBStart) {
533 if (LR->start == 0) {
534 assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
535 // Live-in to the function but dead. Remove it from entry live-in set.
536 mf_->begin()->removeLiveIn(li.reg);
537 }
538 // FIXME: Shorten intervals in BBs that reaches this BB.
539 }
540
541 return false;
542 }
543
452544 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
453545 /// computation, replace the copy by rematerialize the definition.
454546 bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
466558 const TargetInstrDesc &TID = DefMI->getDesc();
467559 if (!TID.isAsCheapAsAMove())
468560 return false;
561 if (!DefMI->getDesc().isRematerializable() ||
562 !tii_->isTriviallyReMaterializable(DefMI))
563 return false;
469564 bool SawStore = false;
470565 if (!DefMI->isSafeToMove(tii_, SawStore))
471566 return false;
484579 }
485580 }
486581
582 // If copy kills the source register, find the last use and propagate
583 // kill.
487584 MachineBasicBlock *MBB = CopyMI->getParent();
585 if (CopyMI->killsRegister(SrcInt.reg))
586 TrimLiveIntervalToLastUse(CopyIdx, MBB, SrcInt, SrcLR);
587
488588 MachineBasicBlock::iterator MII = next(MachineBasicBlock::iterator(CopyMI));
489589 CopyMI->removeFromParent();
490590 tii_->reMaterialize(*MBB, MII, DstReg, DefMI);
659759 }
660760 }
661761
662 /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range
663 /// from a physical register live interval as well as from the live intervals
664 /// of its sub-registers.
665 static void removeRange(LiveInterval &li, unsigned Start, unsigned End,
666 LiveIntervals *li_, const TargetRegisterInfo *tri_) {
667 li.removeRange(Start, End, true);
668 if (TargetRegisterInfo::isPhysicalRegister(li.reg)) {
669 for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
670 if (!li_->hasInterval(*SR))
671 continue;
672 LiveInterval &sli = li_->getInterval(*SR);
673 unsigned RemoveEnd = Start;
674 while (RemoveEnd != End) {
675 LiveInterval::iterator LR = sli.FindLiveRangeContaining(Start);
676 if (LR == sli.end())
677 break;
678 RemoveEnd = (LR->end < End) ? LR->end : End;
679 sli.removeRange(Start, RemoveEnd, true);
680 Start = RemoveEnd;
681 }
682 }
683 }
684 }
685
686762 /// removeIntervalIfEmpty - Check if the live interval of a physical register
687763 /// is empty, if so remove it and also remove the empty intervals of its
688764 /// sub-registers. Return true if live interval is removed.
749825 ++LRStart;
750826 }
751827 }
752 }
753
754 /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
755 /// fallthoughs to SuccMBB.
756 static bool isSameOrFallThroughBB(MachineBasicBlock *MBB,
757 MachineBasicBlock *SuccMBB,
758 const TargetInstrInfo *tii_) {
759 if (MBB == SuccMBB)
760 return true;
761 MachineBasicBlock *TBB = 0, *FBB = 0;
762 SmallVector Cond;
763 return !tii_->AnalyzeBranch(*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
764 MBB->isSuccessor(SuccMBB);
765828 }
766829
767830 /// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
795858 // More uses past this copy? Nothing to do.
796859 return false;
797860
861 // If there is a last use in the same bb, we can't remove the live range.
862 // Shorten the live interval and return.
798863 MachineBasicBlock *CopyMBB = CopyMI->getParent();
799 unsigned MBBStart = li_->getMBBStartIdx(CopyMBB);
800 unsigned LastUseIdx;
801 MachineOperand *LastUse = lastRegisterUse(LR->start, CopyIdx-1, li.reg,
802 LastUseIdx);
803 if (LastUse) {
804 MachineInstr *LastUseMI = LastUse->getParent();
805 if (!isSameOrFallThroughBB(LastUseMI->getParent(), CopyMBB, tii_)) {
806 // r1024 = op
807 // ...
808 // BB1:
809 // = r1024
810 //
811 // BB2:
812 // r1025 = r1024
813 if (MBBStart < LR->end)
814 removeRange(li, MBBStart, LR->end, li_, tri_);
815 return false;
816 }
817
818 // There are uses before the copy, just shorten the live range to the end
819 // of last use.
820 LastUse->setIsKill();
821 removeRange(li, li_->getDefIndex(LastUseIdx), LR->end, li_, tri_);
822 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
823 if (tii_->isMoveInstr(*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
824 DstReg == li.reg) {
825 // Last use is itself an identity code.
826 int DeadIdx = LastUseMI->findRegisterDefOperandIdx(li.reg, false, tri_);
827 LastUseMI->getOperand(DeadIdx).setIsDead();
828 }
829 return false;
830 }
831
832 // Is it livein?
833 if (LR->start <= MBBStart && LR->end > MBBStart) {
834 if (LR->start == 0) {
835 assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
836 // Live-in to the function but dead. Remove it from entry live-in set.
837 mf_->begin()->removeLiveIn(li.reg);
838 }
839 // FIXME: Shorten intervals in BBs that reaches this BB.
840 }
841
842 if (LR->valno->def == RemoveStart)
843 // If the def MI defines the val#, propagate the dead marker.
844 PropagateDeadness(li, CopyMI, RemoveStart, li_, tri_);
845
846 removeRange(li, RemoveStart, LR->end, li_, tri_);
864 if (TrimLiveIntervalToLastUse(CopyIdx, CopyMBB, li, LR))
865 return false;
866
867 MachineBasicBlock *StartMBB = li_->getMBBFromIndex(RemoveStart);
868 if (!isSameOrFallThroughBB(StartMBB, CopyMBB, tii_))
869 // If the live range starts in another mbb and the copy mbb is not a fall
870 // through mbb, then we can only cut the range from the beginning of the
871 // copy mbb.
872 RemoveStart = li_->getMBBStartIdx(CopyMBB) + 1;
873
874 if (LR->valno->def == RemoveStart) {
875 // If the def MI defines the val# and this copy is the only kill of the
876 // val#, then propagate the dead marker.
877 if (li.isOnlyLROfValNo(LR)) {
878 PropagateDeadness(li, CopyMI, RemoveStart, li_, tri_);
879 ++numDeadValNo;
880 }
881 if (li.isKill(LR->valno, RemoveEnd))
882 li.removeKill(LR->valno, RemoveEnd);
883 }
884
885 removeRange(li, RemoveStart, RemoveEnd, li_, tri_);
847886 return removeIntervalIfEmpty(li, li_, tri_);
848887 }
849888
24592498 if (!MO.isReg())
24602499 continue;
24612500 unsigned Reg = MO.getReg();
2501 if (!Reg)
2502 continue;
24622503 if (TargetRegisterInfo::isVirtualRegister(Reg))
24632504 DeadDefs.push_back(Reg);
24642505 if (MO.isDead())
195195 bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB,
196196 MachineInstr *CopyMI);
197197
198 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic
199 /// block as the copy instruction, trim the ive interval to the last use
200 /// and return true.
201 bool TrimLiveIntervalToLastUse(unsigned CopyIdx,
202 MachineBasicBlock *CopyMBB,
203 LiveInterval &li, const LiveRange *LR);
204
205 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
206 /// computation, replace the copy by rematerialize the definition.
198207 bool ReMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg,
199208 MachineInstr *CopyMI);
200209