Skip to content

Commit bc5b640

Browse files
committed
Merge all changes to SimpleRegisterCoalescing to the release.
llvm-svn: 64785
1 parent 34b7c16 commit bc5b640

File tree

2 files changed

+130
-80
lines changed

2 files changed

+130
-80
lines changed

llvm/lib/CodeGen/SimpleRegisterCoalescing.cpp

Lines changed: 121 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ STATISTIC(numExtends , "Number of copies extended");
4242
STATISTIC(NumReMats , "Number of instructions re-materialized");
4343
STATISTIC(numPeep , "Number of identity moves eliminated after coalescing");
4444
STATISTIC(numAborts , "Number of times interval joining aborted");
45+
STATISTIC(numDeadValNo, "Number of valno def marked dead");
4546

4647
char SimpleRegisterCoalescing::ID = 0;
4748
static cl::opt<bool>
@@ -450,6 +451,97 @@ bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
450451
return true;
451452
}
452453

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+
453545
/// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
454546
/// computation, replace the copy by rematerialize the definition.
455547
bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
@@ -467,6 +559,9 @@ bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
467559
const TargetInstrDesc &TID = DefMI->getDesc();
468560
if (!TID.isAsCheapAsAMove())
469561
return false;
562+
if (!DefMI->getDesc().isRematerializable() ||
563+
!tii_->isTriviallyReMaterializable(DefMI))
564+
return false;
470565
bool SawStore = false;
471566
if (!DefMI->isSafeToMove(tii_, SawStore))
472567
return false;
@@ -485,7 +580,12 @@ bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
485580
}
486581
}
487582

583+
// If copy kills the source register, find the last use and propagate
584+
// kill.
488585
MachineBasicBlock *MBB = CopyMI->getParent();
586+
if (CopyMI->killsRegister(SrcInt.reg))
587+
TrimLiveIntervalToLastUse(CopyIdx, MBB, SrcInt, SrcLR);
588+
489589
MachineBasicBlock::iterator MII = next(MachineBasicBlock::iterator(CopyMI));
490590
CopyMI->removeFromParent();
491591
tii_->reMaterialize(*MBB, MII, DstReg, DefMI);
@@ -660,30 +760,6 @@ void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg,
660760
}
661761
}
662762

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-
687763
/// removeIntervalIfEmpty - Check if the live interval of a physical register
688764
/// is empty, if so remove it and also remove the empty intervals of its
689765
/// sub-registers. Return true if live interval is removed.
@@ -752,19 +828,6 @@ static void PropagateDeadness(LiveInterval &li, MachineInstr *CopyMI,
752828
}
753829
}
754830

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-
768831
/// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
769832
/// extended by a dead copy. Mark the last use (if any) of the val# as kill as
770833
/// ends the live range there. If there isn't another use, then this live range
@@ -796,55 +859,31 @@ SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li,
796859
// More uses past this copy? Nothing to do.
797860
return false;
798861

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.
799864
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))
830866
return false;
831-
}
832867

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;
839881
}
840-
// FIXME: Shorten intervals in BBs that reaches this BB.
882+
if (li.isKill(LR->valno, RemoveEnd))
883+
li.removeKill(LR->valno, RemoveEnd);
841884
}
842885

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_);
848887
return removeIntervalIfEmpty(li, li_, tri_);
849888
}
850889

@@ -2460,6 +2499,8 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
24602499
if (!MO.isReg())
24612500
continue;
24622501
unsigned Reg = MO.getReg();
2502+
if (!Reg)
2503+
continue;
24632504
if (TargetRegisterInfo::isVirtualRegister(Reg))
24642505
DeadDefs.push_back(Reg);
24652506
if (MO.isDead())

llvm/lib/CodeGen/SimpleRegisterCoalescing.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ namespace llvm {
196196
bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB,
197197
MachineInstr *CopyMI);
198198

199+
/// TrimLiveIntervalToLastUse - If there is a last use in the same basic
200+
/// block as the copy instruction, trim the ive interval to the last use
201+
/// and return true.
202+
bool TrimLiveIntervalToLastUse(unsigned CopyIdx,
203+
MachineBasicBlock *CopyMBB,
204+
LiveInterval &li, const LiveRange *LR);
205+
206+
/// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
207+
/// computation, replace the copy by rematerialize the definition.
199208
bool ReMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg,
200209
MachineInstr *CopyMI);
201210

0 commit comments

Comments
 (0)