909
925
//===----------------------------------------------------------------------===//
926
// CopyConstrain - DAG post-processing to encourage copy elimination.
927
//===----------------------------------------------------------------------===//
930
/// \brief Post-process the DAG to create weak edges from all uses of a copy to
931
/// the one use that defines the copy's source vreg, most likely an induction
932
/// variable increment.
933
class CopyConstrain : public ScheduleDAGMutation {
935
SlotIndex RegionBeginIdx;
936
// RegionEndIdx is the slot index of the last non-debug instruction in the
937
// scheduling region. So we may have RegionBeginIdx == RegionEndIdx.
938
SlotIndex RegionEndIdx;
940
CopyConstrain(const TargetInstrInfo *, const TargetRegisterInfo *) {}
942
virtual void apply(ScheduleDAGMI *DAG);
945
void constrainLocalCopy(SUnit *CopySU, ScheduleDAGMI *DAG);
949
/// constrainLocalCopy handles two possibilities:
954
/// I3: dst = src (copy)
955
/// (create pred->succ edges I0->I1, I2->I1)
958
/// I0: dst = src (copy)
962
/// (create pred->succ edges I1->I2, I3->I2)
964
/// Although the MachineScheduler is currently constrained to single blocks,
965
/// this algorithm should handle extended blocks. An EBB is a set of
966
/// contiguously numbered blocks such that the previous block in the EBB is
967
/// always the single predecessor.
968
void CopyConstrain::constrainLocalCopy(SUnit *CopySU, ScheduleDAGMI *DAG) {
969
LiveIntervals *LIS = DAG->getLIS();
970
MachineInstr *Copy = CopySU->getInstr();
972
// Check for pure vreg copies.
973
unsigned SrcReg = Copy->getOperand(1).getReg();
974
if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
977
unsigned DstReg = Copy->getOperand(0).getReg();
978
if (!TargetRegisterInfo::isVirtualRegister(DstReg))
981
// Check if either the dest or source is local. If it's live across a back
982
// edge, it's not local. Note that if both vregs are live across the back
983
// edge, we cannot successfully contrain the copy without cyclic scheduling.
984
unsigned LocalReg = DstReg;
985
unsigned GlobalReg = SrcReg;
986
LiveInterval *LocalLI = &LIS->getInterval(LocalReg);
987
if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx)) {
990
LocalLI = &LIS->getInterval(LocalReg);
991
if (!LocalLI->isLocal(RegionBeginIdx, RegionEndIdx))
994
LiveInterval *GlobalLI = &LIS->getInterval(GlobalReg);
996
// Find the global segment after the start of the local LI.
997
LiveInterval::iterator GlobalSegment = GlobalLI->find(LocalLI->beginIndex());
998
// If GlobalLI does not overlap LocalLI->start, then a copy directly feeds a
999
// local live range. We could create edges from other global uses to the local
1000
// start, but the coalescer should have already eliminated these cases, so
1001
// don't bother dealing with it.
1002
if (GlobalSegment == GlobalLI->end())
1005
// If GlobalSegment is killed at the LocalLI->start, the call to find()
1006
// returned the next global segment. But if GlobalSegment overlaps with
1007
// LocalLI->start, then advance to the next segement. If a hole in GlobalLI
1008
// exists in LocalLI's vicinity, GlobalSegment will be the end of the hole.
1009
if (GlobalSegment->contains(LocalLI->beginIndex()))
1012
if (GlobalSegment == GlobalLI->end())
1015
// Check if GlobalLI contains a hole in the vicinity of LocalLI.
1016
if (GlobalSegment != GlobalLI->begin()) {
1017
// Two address defs have no hole.
1018
if (SlotIndex::isSameInstr(llvm::prior(GlobalSegment)->end,
1019
GlobalSegment->start)) {
1022
// If GlobalLI has a prior segment, it must be live into the EBB. Otherwise
1023
// it would be a disconnected component in the live range.
1024
assert(llvm::prior(GlobalSegment)->start < LocalLI->beginIndex() &&
1025
"Disconnected LRG within the scheduling region.");
1027
MachineInstr *GlobalDef = LIS->getInstructionFromIndex(GlobalSegment->start);
1031
SUnit *GlobalSU = DAG->getSUnit(GlobalDef);
1035
// GlobalDef is the bottom of the GlobalLI hole. Open the hole by
1036
// constraining the uses of the last local def to precede GlobalDef.
1037
SmallVector<SUnit*,8> LocalUses;
1038
const VNInfo *LastLocalVN = LocalLI->getVNInfoBefore(LocalLI->endIndex());
1039
MachineInstr *LastLocalDef = LIS->getInstructionFromIndex(LastLocalVN->def);
1040
SUnit *LastLocalSU = DAG->getSUnit(LastLocalDef);
1041
for (SUnit::const_succ_iterator
1042
I = LastLocalSU->Succs.begin(), E = LastLocalSU->Succs.end();
1044
if (I->getKind() != SDep::Data || I->getReg() != LocalReg)
1046
if (I->getSUnit() == GlobalSU)
1048
if (!DAG->canAddEdge(GlobalSU, I->getSUnit()))
1050
LocalUses.push_back(I->getSUnit());
1052
// Open the top of the GlobalLI hole by constraining any earlier global uses
1053
// to precede the start of LocalLI.
1054
SmallVector<SUnit*,8> GlobalUses;
1055
MachineInstr *FirstLocalDef =
1056
LIS->getInstructionFromIndex(LocalLI->beginIndex());
1057
SUnit *FirstLocalSU = DAG->getSUnit(FirstLocalDef);
1058
for (SUnit::const_pred_iterator
1059
I = GlobalSU->Preds.begin(), E = GlobalSU->Preds.end(); I != E; ++I) {
1060
if (I->getKind() != SDep::Anti || I->getReg() != GlobalReg)
1062
if (I->getSUnit() == FirstLocalSU)
1064
if (!DAG->canAddEdge(FirstLocalSU, I->getSUnit()))
1066
GlobalUses.push_back(I->getSUnit());
1068
DEBUG(dbgs() << "Constraining copy SU(" << CopySU->NodeNum << ")\n");
1069
// Add the weak edges.
1070
for (SmallVectorImpl<SUnit*>::const_iterator
1071
I = LocalUses.begin(), E = LocalUses.end(); I != E; ++I) {
1072
DEBUG(dbgs() << " Local use SU(" << (*I)->NodeNum << ") -> SU("
1073
<< GlobalSU->NodeNum << ")\n");
1074
DAG->addEdge(GlobalSU, SDep(*I, SDep::Weak));
1076
for (SmallVectorImpl<SUnit*>::const_iterator
1077
I = GlobalUses.begin(), E = GlobalUses.end(); I != E; ++I) {
1078
DEBUG(dbgs() << " Global use SU(" << (*I)->NodeNum << ") -> SU("
1079
<< FirstLocalSU->NodeNum << ")\n");
1080
DAG->addEdge(FirstLocalSU, SDep(*I, SDep::Weak));
1084
/// \brief Callback from DAG postProcessing to create weak edges to encourage
1085
/// copy elimination.
1086
void CopyConstrain::apply(ScheduleDAGMI *DAG) {
1087
MachineBasicBlock::iterator FirstPos = nextIfDebug(DAG->begin(), DAG->end());
1088
if (FirstPos == DAG->end())
1090
RegionBeginIdx = DAG->getLIS()->getInstructionIndex(&*FirstPos);
1091
RegionEndIdx = DAG->getLIS()->getInstructionIndex(
1092
&*priorNonDebug(DAG->end(), DAG->begin()));
1094
for (unsigned Idx = 0, End = DAG->SUnits.size(); Idx != End; ++Idx) {
1095
SUnit *SU = &DAG->SUnits[Idx];
1096
if (!SU->getInstr()->isCopy())
1099
constrainLocalCopy(SU, DAG);
1103
//===----------------------------------------------------------------------===//
910
1104
// ConvergingScheduler - Implementation of the standard MachineSchedStrategy.
911
1105
//===----------------------------------------------------------------------===//