41 #define DEBUG_TYPE "machine-scheduler" 145 case NoCand:
return "NOCAND";
147 case Latency:
return "LATENCY";
149 case Depth:
return "DEPTH";
163 if (TryVal < CandVal) {
167 if (TryVal > CandVal) {
180 if (TryVal > CandVal) {
184 if (TryVal < CandVal) {
198 NodeNum2Index[SU->
NodeNum] = SUnits.size();
199 SUnits.push_back(SU);
203 void SIScheduleBlock::traceCandidate(
const SISchedCandidate &Cand) {
210 void SIScheduleBlock::tryCandidateTopDown(SISchedCandidate &Cand,
211 SISchedCandidate &TryCand) {
213 if (!Cand.isValid()) {
218 if (Cand.SGPRUsage > 60 &&
239 Cand.HasLowLatencyNonWaitedParent,
247 if (TryCand.IsLowLatency &&
257 if (TryCand.SU->NodeNum < Cand.SU->NodeNum) {
262 SUnit* SIScheduleBlock::pickNode() {
263 SISchedCandidate TopCand;
265 for (
SUnit* SU : TopReadySUs) {
266 SISchedCandidate TryCand;
267 std::vector<unsigned> pressure;
268 std::vector<unsigned> MaxPressure;
271 TopRPTracker.getDownwardPressure(SU->getInstr(), pressure, MaxPressure);
272 TryCand.SGPRUsage = pressure[DAG->getSGPRSetID()];
273 TryCand.VGPRUsage = pressure[DAG->getVGPRSetID()];
274 TryCand.IsLowLatency = DAG->IsLowLatencySU[SU->NodeNum];
275 TryCand.LowLatencyOffset = DAG->LowLatencyOffset[SU->NodeNum];
276 TryCand.HasLowLatencyNonWaitedParent =
277 HasLowLatencyNonWaitedParent[NodeNum2Index[SU->NodeNum]];
278 tryCandidateTopDown(TopCand, TryCand);
279 if (TryCand.Reason !=
NoCand)
280 TopCand.setBest(TryCand);
293 for (
SUnit* SU : SUnits) {
294 if (!SU->NumPredsLeft)
295 TopReadySUs.push_back(SU);
298 while (!TopReadySUs.empty()) {
299 SUnit *SU = TopReadySUs[0];
300 ScheduledSUnits.push_back(SU);
319 if (InstSlot >= First && InstSlot <= Last)
331 DAG->initRPTracker(TopRPTracker);
332 DAG->initRPTracker(BotRPTracker);
333 DAG->initRPTracker(RPTracker);
337 for (
SUnit* SU : ScheduledSUnits) {
338 RPTracker.setPos(SU->getInstr());
343 RPTracker.closeRegion();
346 TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs);
347 BotRPTracker.
addLiveRegs(RPTracker.getPressure().LiveOutRegs);
350 for (
const auto &RegMaskPair : RPTracker.getPressure().LiveInRegs) {
352 LiveInRegs.insert(RegMaskPair.RegUnit);
377 for (
const auto &RegMaskPair : RPTracker.getPressure().LiveOutRegs) {
378 unsigned Reg = RegMaskPair.RegUnit;
383 LiveOutRegs.insert(Reg);
391 LiveInPressure = TopPressure.MaxSetPressure;
395 TopRPTracker.closeTop();
404 initRegPressure(BeginBlock, EndBlock);
411 for (
SUnit* SU : SUnits) {
412 if (!SU->NumPredsLeft)
413 TopReadySUs.push_back(SU);
416 while (!TopReadySUs.empty()) {
417 SUnit *SU = pickNode();
418 ScheduledSUnits.push_back(SU);
419 TopRPTracker.setPos(SU->
getInstr());
420 TopRPTracker.advance();
425 InternalAdditionnalPressure.resize(TopPressure.MaxSetPressure.size());
429 assert(SUnits.size() == ScheduledSUnits.size() &&
430 TopReadySUs.empty());
431 for (
SUnit* SU : SUnits) {
433 SU->NumPredsLeft == 0);
440 void SIScheduleBlock::undoSchedule() {
441 for (
SUnit* SU : SUnits) {
442 SU->isScheduled =
false;
443 for (
SDep& Succ : SU->Succs) {
445 undoReleaseSucc(SU, &Succ);
448 HasLowLatencyNonWaitedParent.assign(SUnits.size(), 0);
449 ScheduledSUnits.clear();
453 void SIScheduleBlock::undoReleaseSucc(
SUnit *SU,
SDep *SuccEdge) {
463 void SIScheduleBlock::releaseSucc(
SUnit *SU,
SDep *SuccEdge) {
472 dbgs() <<
"*** Scheduling failed! ***\n";
473 DAG->dumpNode(*SuccSU);
474 dbgs() <<
" has been released too many times!\n";
483 void SIScheduleBlock::releaseSuccessors(
SUnit *SU,
bool InOrOutBlock) {
487 if (SuccSU->
NodeNum >= DAG->SUnits.size())
490 if (BC->isSUInBlock(SuccSU,
ID) != InOrOutBlock)
493 releaseSucc(SU, &Succ);
495 TopReadySUs.push_back(SuccSU);
499 void SIScheduleBlock::nodeScheduled(
SUnit *SU) {
502 std::vector<SUnit *>::iterator
I =
llvm::find(TopReadySUs, SU);
503 if (I == TopReadySUs.end()) {
504 dbgs() <<
"Data Structure Bug in SI Scheduler\n";
507 TopReadySUs.erase(I);
509 releaseSuccessors(SU,
true);
512 if (HasLowLatencyNonWaitedParent[NodeNum2Index[SU->
NodeNum]])
513 HasLowLatencyNonWaitedParent.assign(SUnits.size(), 0);
515 if (DAG->IsLowLatencySU[SU->
NodeNum]) {
517 std::map<unsigned, unsigned>::iterator I =
519 if (I != NodeNum2Index.end())
520 HasLowLatencyNonWaitedParent[I->second] = 1;
528 for (
SUnit* SU : SUnits) {
529 releaseSuccessors(SU,
false);
530 if (DAG->IsHighLatencySU[SU->
NodeNum])
531 HighLatencyBlock =
true;
533 HasLowLatencyNonWaitedParent.resize(SUnits.size(), 0);
538 unsigned PredID = Pred->
getID();
542 if (PredID ==
P->getID())
545 Preds.push_back(Pred);
550 return PredID == S.first->getID();
552 "Loop in the Block Graph!");
557 unsigned SuccID = Succ->
getID();
560 for (std::pair<SIScheduleBlock*, SIScheduleBlockLinkKind> &S : Succs) {
561 if (SuccID == S.first->getID()) {
569 ++NumHighLatencySuccessors;
570 Succs.push_back(std::make_pair(Succ, Kind));
574 "Loop in the Block Graph!");
579 dbgs() <<
"Block (" <<
ID <<
")\n";
583 dbgs() <<
"\nContains High Latency Instruction: " 584 << HighLatencyBlock <<
'\n';
585 dbgs() <<
"\nDepends On:\n";
587 P->printDebug(
false);
590 dbgs() <<
"\nSuccessors:\n";
591 for (std::pair<SIScheduleBlock*, SIScheduleBlockLinkKind> S : Succs) {
593 dbgs() <<
"(Data Dep) ";
594 S.first->printDebug(
false);
598 dbgs() <<
"LiveInPressure " << LiveInPressure[DAG->getSGPRSetID()] <<
' ' 599 << LiveInPressure[DAG->getVGPRSetID()] <<
'\n';
600 dbgs() <<
"LiveOutPressure " << LiveOutPressure[DAG->getSGPRSetID()] <<
' ' 601 << LiveOutPressure[DAG->getVGPRSetID()] <<
"\n\n";
602 dbgs() <<
"LiveIns:\n";
603 for (
unsigned Reg : LiveInRegs)
606 dbgs() <<
"\nLiveOuts:\n";
607 for (
unsigned Reg : LiveOutRegs)
611 dbgs() <<
"\nInstructions:\n";
612 for (
const SUnit* SU : SUnits)
615 dbgs() <<
"///////////////////////\n";
626 std::map<SISchedulerBlockCreatorVariant, SIScheduleBlocks>::iterator
B =
627 Blocks.find(BlockVariant);
628 if (B == Blocks.end()) {
630 createBlocksForVariant(BlockVariant);
632 scheduleInsideBlocks();
634 Res.
Blocks = CurrentBlocks;
637 Blocks[BlockVariant] = Res;
647 return CurrentBlocks[Node2CurrentBlock[SU->
NodeNum]]->getID() ==
ID;
650 void SIScheduleBlockCreator::colorHighLatenciesAlone() {
651 unsigned DAGSize = DAG->
SUnits.size();
653 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
656 CurrentColoring[SU->
NodeNum] = NextReservedID++;
663 for (
const auto &PredDep : SU.
Preds) {
664 if (PredDep.getSUnit() == &FromSU &&
671 void SIScheduleBlockCreator::colorHighLatenciesGroups() {
672 unsigned DAGSize = DAG->
SUnits.size();
673 unsigned NumHighLatencies = 0;
675 int Color = NextReservedID;
677 std::set<unsigned> FormingGroup;
679 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
685 if (NumHighLatencies == 0)
688 if (NumHighLatencies <= 6)
690 else if (NumHighLatencies <= 12)
698 unsigned CompatibleGroup =
true;
699 int ProposedColor =
Color;
700 std::vector<int> AdditionalElements;
712 for (
unsigned j : FormingGroup) {
714 std::vector<int> SubGraph;
727 else if (SubGraph.size() > 5) {
729 CompatibleGroup =
false;
734 for (
unsigned k : SubGraph) {
740 (CurrentColoring[k] != ProposedColor &&
741 CurrentColoring[k] != 0)) {
742 CompatibleGroup =
false;
748 CompatibleGroup =
false;
752 if (!CompatibleGroup)
756 CompatibleGroup =
false;
764 AdditionalElements.insert(AdditionalElements.end(),
765 SubGraph.begin(), SubGraph.end());
768 if (CompatibleGroup) {
769 FormingGroup.insert(SU.
NodeNum);
770 for (
unsigned j : AdditionalElements)
771 CurrentColoring[j] = ProposedColor;
772 CurrentColoring[SU.
NodeNum] = ProposedColor;
778 if (!CompatibleGroup) {
779 FormingGroup.clear();
780 Color = ++NextReservedID;
781 ProposedColor =
Color;
782 FormingGroup.insert(SU.
NodeNum);
783 CurrentColoring[SU.
NodeNum] = ProposedColor;
785 }
else if (Count == GroupSize) {
786 FormingGroup.clear();
787 Color = ++NextReservedID;
788 ProposedColor =
Color;
795 void SIScheduleBlockCreator::colorComputeReservedDependencies() {
796 unsigned DAGSize = DAG->
SUnits.size();
797 std::map<std::set<unsigned>,
unsigned> ColorCombinations;
799 CurrentTopDownReservedDependencyColoring.clear();
800 CurrentBottomUpReservedDependencyColoring.clear();
802 CurrentTopDownReservedDependencyColoring.resize(DAGSize, 0);
803 CurrentBottomUpReservedDependencyColoring.resize(DAGSize, 0);
810 std::set<unsigned> SUColors;
813 if (CurrentColoring[SU->
NodeNum]) {
814 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
823 if (CurrentTopDownReservedDependencyColoring[Pred->
NodeNum] > 0)
824 SUColors.insert(CurrentTopDownReservedDependencyColoring[Pred->
NodeNum]);
827 if (SUColors.empty())
830 if (SUColors.size() == 1 && *SUColors.begin() > DAGSize)
831 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
834 std::map<std::set<unsigned>,
unsigned>::iterator Pos =
835 ColorCombinations.find(SUColors);
836 if (Pos != ColorCombinations.end()) {
837 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] = Pos->second;
839 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
841 ColorCombinations[SUColors] = NextNonReservedID++;
846 ColorCombinations.clear();
852 std::set<unsigned> SUColors;
855 if (CurrentColoring[SU->
NodeNum]) {
856 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
865 if (CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum] > 0)
866 SUColors.insert(CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum]);
869 if (SUColors.empty())
872 if (SUColors.size() == 1 && *SUColors.begin() > DAGSize)
873 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
876 std::map<std::set<unsigned>,
unsigned>::iterator Pos =
877 ColorCombinations.find(SUColors);
878 if (Pos != ColorCombinations.end()) {
879 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] = Pos->second;
881 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
883 ColorCombinations[SUColors] = NextNonReservedID++;
889 void SIScheduleBlockCreator::colorAccordingToReservedDependencies() {
890 unsigned DAGSize = DAG->
SUnits.size();
891 std::map<std::pair<unsigned, unsigned>,
unsigned> ColorCombinations;
896 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
898 std::pair<unsigned, unsigned> SUColors;
901 if (CurrentColoring[SU->
NodeNum])
904 SUColors.first = CurrentTopDownReservedDependencyColoring[SU->
NodeNum];
905 SUColors.second = CurrentBottomUpReservedDependencyColoring[SU->
NodeNum];
907 std::map<std::pair<unsigned, unsigned>,
unsigned>::iterator Pos =
908 ColorCombinations.find(SUColors);
909 if (Pos != ColorCombinations.end()) {
910 CurrentColoring[SU->
NodeNum] = Pos->second;
912 CurrentColoring[SU->
NodeNum] = NextNonReservedID;
913 ColorCombinations[SUColors] = NextNonReservedID++;
918 void SIScheduleBlockCreator::colorEndsAccordingToDependencies() {
919 unsigned DAGSize = DAG->
SUnits.size();
920 std::vector<int> PendingColoring = CurrentColoring;
923 CurrentBottomUpReservedDependencyColoring.size() == DAGSize &&
924 CurrentTopDownReservedDependencyColoring.size() == DAGSize);
927 if (*std::max_element(CurrentBottomUpReservedDependencyColoring.begin(),
928 CurrentBottomUpReservedDependencyColoring.end()) == 0 &&
929 *std::max_element(CurrentTopDownReservedDependencyColoring.begin(),
930 CurrentTopDownReservedDependencyColoring.end()) == 0)
935 std::set<unsigned> SUColors;
936 std::set<unsigned> SUColorsPending;
938 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
941 if (CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] > 0 ||
942 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] > 0)
949 if (CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum] > 0 ||
950 CurrentTopDownReservedDependencyColoring[Succ->
NodeNum] > 0)
951 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
952 SUColorsPending.insert(PendingColoring[Succ->
NodeNum]);
957 if (SUColors.size() == 1 && SUColorsPending.size() == 1)
958 PendingColoring[SU->
NodeNum] = *SUColors.begin();
961 PendingColoring[SU->
NodeNum] = NextNonReservedID++;
963 CurrentColoring = PendingColoring;
967 void SIScheduleBlockCreator::colorForceConsecutiveOrderInGroup() {
968 unsigned DAGSize = DAG->
SUnits.size();
969 unsigned PreviousColor;
970 std::set<unsigned> SeenColors;
975 PreviousColor = CurrentColoring[0];
977 for (
unsigned i = 1,
e = DAGSize; i !=
e; ++i) {
979 unsigned CurrentColor = CurrentColoring[i];
980 unsigned PreviousColorSave = PreviousColor;
983 if (CurrentColor != PreviousColor)
984 SeenColors.insert(PreviousColor);
985 PreviousColor = CurrentColor;
987 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
990 if (SeenColors.find(CurrentColor) == SeenColors.end())
993 if (PreviousColorSave != CurrentColor)
994 CurrentColoring[i] = NextNonReservedID++;
996 CurrentColoring[i] = CurrentColoring[i-1];
1000 void SIScheduleBlockCreator::colorMergeConstantLoadsNextGroup() {
1001 unsigned DAGSize = DAG->
SUnits.size();
1005 std::set<unsigned> SUColors;
1007 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1019 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1021 if (SUColors.size() == 1)
1022 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1026 void SIScheduleBlockCreator::colorMergeIfPossibleNextGroup() {
1027 unsigned DAGSize = DAG->
SUnits.size();
1031 std::set<unsigned> SUColors;
1033 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1040 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1042 if (SUColors.size() == 1)
1043 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1047 void SIScheduleBlockCreator::colorMergeIfPossibleNextGroupOnlyForReserved() {
1048 unsigned DAGSize = DAG->
SUnits.size();
1052 std::set<unsigned> SUColors;
1054 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1061 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1063 if (SUColors.size() == 1 && *SUColors.begin() <= DAGSize)
1064 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1068 void SIScheduleBlockCreator::colorMergeIfPossibleSmallGroupsToNextGroup() {
1069 unsigned DAGSize = DAG->
SUnits.size();
1070 std::map<unsigned, unsigned> ColorCount;
1074 unsigned color = CurrentColoring[SU->
NodeNum];
1075 ++ColorCount[color];
1080 unsigned color = CurrentColoring[SU->
NodeNum];
1081 std::set<unsigned> SUColors;
1083 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1086 if (ColorCount[color] > 1)
1093 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1095 if (SUColors.size() == 1 && *SUColors.begin() != color) {
1096 --ColorCount[color];
1097 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1098 ++ColorCount[*SUColors.begin()];
1103 void SIScheduleBlockCreator::cutHugeBlocks() {
1107 void SIScheduleBlockCreator::regroupNoUserInstructions() {
1108 unsigned DAGSize = DAG->
SUnits.size();
1109 int GroupID = NextNonReservedID++;
1113 bool hasSuccessor =
false;
1115 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1122 hasSuccessor =
true;
1125 CurrentColoring[SU->
NodeNum] = GroupID;
1129 void SIScheduleBlockCreator::colorExports() {
1130 unsigned ExportColor = NextNonReservedID++;
1150 for (
unsigned j : ExpGroup) {
1152 std::vector<int> SubGraph;
1168 for (
unsigned k : SubGraph) {
1176 ExpGroup.push_back(SUNum);
1181 for (
unsigned j : ExpGroup)
1182 CurrentColoring[j] = ExportColor;
1186 unsigned DAGSize = DAG->
SUnits.size();
1187 std::map<unsigned,unsigned> RealID;
1189 CurrentBlocks.clear();
1190 CurrentColoring.clear();
1191 CurrentColoring.resize(DAGSize, 0);
1192 Node2CurrentBlock.clear();
1198 NextNonReservedID = DAGSize + 1;
1203 colorHighLatenciesGroups();
1205 colorHighLatenciesAlone();
1206 colorComputeReservedDependencies();
1207 colorAccordingToReservedDependencies();
1208 colorEndsAccordingToDependencies();
1210 colorForceConsecutiveOrderInGroup();
1211 regroupNoUserInstructions();
1212 colorMergeConstantLoadsNextGroup();
1213 colorMergeIfPossibleNextGroupOnlyForReserved();
1217 Node2CurrentBlock.resize(DAGSize, -1);
1218 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1221 if (RealID.find(Color) == RealID.end()) {
1222 int ID = CurrentBlocks.size();
1223 BlockPtrs.push_back(std::make_unique<SIScheduleBlock>(DAG,
this, ID));
1224 CurrentBlocks.push_back(BlockPtrs.rbegin()->get());
1227 CurrentBlocks[RealID[
Color]]->addUnit(SU);
1232 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1234 int SUID = Node2CurrentBlock[i];
1239 if (Node2CurrentBlock[Succ->
NodeNum] != SUID)
1240 CurrentBlocks[SUID]->addSucc(CurrentBlocks[Node2CurrentBlock[Succ->
NodeNum]],
1247 if (Node2CurrentBlock[Pred->
NodeNum] != SUID)
1248 CurrentBlocks[SUID]->addPred(CurrentBlocks[Node2CurrentBlock[Pred->
NodeNum]]);
1253 for (
unsigned i = 0,
e = CurrentBlocks.size(); i !=
e; ++i) {
1258 for (
unsigned i = 0,
e = CurrentBlocks.size(); i !=
e; ++i) {
1270 for (; I != End; ++
I) {
1271 if (!I->isDebugInstr())
1277 void SIScheduleBlockCreator::topologicalSort() {
1278 unsigned DAGSize = CurrentBlocks.size();
1279 std::vector<int> WorkList;
1283 WorkList.reserve(DAGSize);
1284 TopDownIndex2Block.resize(DAGSize);
1285 TopDownBlock2Index.resize(DAGSize);
1286 BottomUpIndex2Block.resize(DAGSize);
1288 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1290 unsigned Degree = Block->
getSuccs().size();
1291 TopDownBlock2Index[i] = Degree;
1293 WorkList.push_back(i);
1298 while (!WorkList.empty()) {
1299 int i = WorkList.back();
1301 WorkList.pop_back();
1302 TopDownBlock2Index[i] = --
Id;
1303 TopDownIndex2Block[
Id] = i;
1305 if (!--TopDownBlock2Index[Pred->getID()])
1306 WorkList.push_back(Pred->getID());
1312 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1315 assert(TopDownBlock2Index[i] > TopDownBlock2Index[Pred->getID()] &&
1316 "Wrong Top Down topological sorting");
1321 BottomUpIndex2Block = std::vector<int>(TopDownIndex2Block.rbegin(),
1322 TopDownIndex2Block.rend());
1325 void SIScheduleBlockCreator::scheduleInsideBlocks() {
1326 unsigned DAGSize = CurrentBlocks.size();
1332 LLVM_DEBUG(
dbgs() <<
"First phase: Fast scheduling for Reg Liveness\n");
1333 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1343 std::vector<MachineBasicBlock::iterator> PosOld;
1344 std::vector<MachineBasicBlock::iterator> PosNew;
1345 PosOld.reserve(DAG->
SUnits.size());
1346 PosNew.reserve(DAG->
SUnits.size());
1348 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1349 int BlockIndice = TopDownIndex2Block[i];
1353 for (
SUnit* SU : SUs) {
1356 PosOld.push_back(Pos);
1357 if (&*CurrentTopFastSched == MI) {
1358 PosNew.push_back(Pos);
1359 CurrentTopFastSched =
nextIfDebug(++CurrentTopFastSched,
1371 PosNew.push_back(CurrentTopFastSched);
1380 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1383 Block->
schedule((*SUs.begin())->getInstr(), (*SUs.rbegin())->getInstr());
1388 for (
unsigned i = PosOld.size(),
e = 0; i !=
e; --i) {
1400 LLVM_DEBUG(
for (
unsigned i = 0,
e = CurrentBlocks.size(); i !=
e; ++i) {
1406 void SIScheduleBlockCreator::fillStats() {
1407 unsigned DAGSize = CurrentBlocks.size();
1409 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1410 int BlockIndice = TopDownIndex2Block[i];
1417 if (Depth < Pred->
Depth + Pred->getCost())
1418 Depth = Pred->Depth + Pred->getCost();
1424 for (
unsigned i = 0,
e = DAGSize; i !=
e; ++i) {
1425 int BlockIndice = BottomUpIndex2Block[i];
1430 unsigned Height = 0;
1432 Height =
std::max(Height, Succ.first->Height + Succ.first->getCost());
1443 DAG(DAG), Variant(Variant), Blocks(BlocksStruct.Blocks),
1444 LastPosWaitedHighLatency(0), NumBlockScheduled(0), VregCurrentUsage(0),
1445 SregCurrentUsage(0), maxVregUsage(0), maxSregUsage(0) {
1457 LiveOutRegsNumUsages.resize(Blocks.size());
1458 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1464 std::set<unsigned> PredOutRegs = Pred->getOutRegs();
1465 std::set<unsigned>::iterator RegPos = PredOutRegs.find(
Reg);
1467 if (RegPos != PredOutRegs.end()) {
1479 ++LiveOutRegsNumUsages[PredID][
Reg];
1483 LastPosHighLatencyParentScheduled.resize(Blocks.size(), 0);
1484 BlockNumPredsLeft.resize(Blocks.size());
1485 BlockNumSuccsLeft.resize(Blocks.size());
1487 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1489 BlockNumPredsLeft[i] = Block->
getPreds().size();
1490 BlockNumSuccsLeft[i] = Block->
getSuccs().size();
1494 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1500 std::set<unsigned> InRegs = DAG->
getInRegs();
1501 addLiveRegs(InRegs);
1507 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1511 const std::set<unsigned> &OutRegs = Block->
getOutRegs();
1513 if (OutRegs.find(
Reg) == OutRegs.end())
1516 ++LiveOutRegsNumUsages[
ID][
Reg];
1523 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1528 std::set<unsigned> PredOutRegs = Pred->getOutRegs();
1529 std::set<unsigned>::iterator RegPos = PredOutRegs.find(
Reg);
1531 if (RegPos != PredOutRegs.end()) {
1538 ++LiveRegsConsumers[
Reg];
1542 for (
unsigned i = 0,
e = Blocks.size(); i !=
e; ++i) {
1544 if (BlockNumPredsLeft[i] == 0) {
1545 ReadyBlocks.push_back(Block);
1550 BlocksScheduled.push_back(Block);
1551 blockScheduled(Block);
1555 : BlocksScheduled) {
1556 dbgs() <<
' ' << Block->getID();
1560 bool SIScheduleBlockScheduler::tryCandidateLatency(SIBlockSchedCandidate &Cand,
1561 SIBlockSchedCandidate &TryCand) {
1562 if (!Cand.isValid()) {
1569 Cand.LastPosHighLatParentScheduled, TryCand, Cand,
Latency))
1576 TryCand, Cand,
Depth))
1579 Cand.NumHighLatencySuccessors,
1585 bool SIScheduleBlockScheduler::tryCandidateRegUsage(SIBlockSchedCandidate &Cand,
1586 SIBlockSchedCandidate &TryCand) {
1587 if (!Cand.isValid()) {
1596 Cand.NumSuccessors > 0,
1608 SIBlockSchedCandidate Cand;
1609 std::vector<SIScheduleBlock*>::iterator Best;
1611 if (ReadyBlocks.empty())
1615 VregCurrentUsage, SregCurrentUsage);
1616 if (VregCurrentUsage > maxVregUsage)
1617 maxVregUsage = VregCurrentUsage;
1618 if (SregCurrentUsage > maxSregUsage)
1619 maxSregUsage = SregCurrentUsage;
1622 : ReadyBlocks)
dbgs()
1623 << Block->
getID() <<
' ';
1624 dbgs() <<
"\nCurrent Live:\n";
1629 dbgs() <<
"Current VGPRs: " << VregCurrentUsage <<
'\n';
1630 dbgs() <<
"Current SGPRs: " << SregCurrentUsage <<
'\n';);
1632 Cand.Block =
nullptr;
1633 for (std::vector<SIScheduleBlock*>::iterator
I = ReadyBlocks.begin(),
1634 E = ReadyBlocks.end();
I !=
E; ++
I) {
1635 SIBlockSchedCandidate TryCand;
1637 TryCand.IsHighLatency = TryCand.Block->isHighLatencyBlock();
1638 TryCand.VGPRUsageDiff =
1639 checkRegUsageImpact(TryCand.Block->getInRegs(),
1641 TryCand.NumSuccessors = TryCand.Block->getSuccs().size();
1642 TryCand.NumHighLatencySuccessors =
1643 TryCand.Block->getNumHighLatencySuccessors();
1644 TryCand.LastPosHighLatParentScheduled =
1645 (
unsigned int) std::max<int> (0,
1646 LastPosHighLatencyParentScheduled[TryCand.Block->getID()] -
1647 LastPosWaitedHighLatency);
1648 TryCand.Height = TryCand.Block->Height;
1650 if (VregCurrentUsage > 120 ||
1652 if (!tryCandidateRegUsage(Cand, TryCand) &&
1654 tryCandidateLatency(Cand, TryCand);
1656 if (!tryCandidateLatency(Cand, TryCand))
1657 tryCandidateRegUsage(Cand, TryCand);
1659 if (TryCand.Reason !=
NoCand) {
1660 Cand.setBest(TryCand);
1662 LLVM_DEBUG(
dbgs() <<
"Best Current Choice: " << Cand.Block->getID() <<
' ' 1668 dbgs() <<
"Is a block with high latency instruction: " 1669 << (Cand.IsHighLatency ?
"yes\n" :
"no\n");
1670 dbgs() <<
"Position of last high latency dependency: " 1671 << Cand.LastPosHighLatParentScheduled <<
'\n';
1672 dbgs() <<
"VGPRUsageDiff: " << Cand.VGPRUsageDiff <<
'\n';
1676 ReadyBlocks.erase(Best);
1682 void SIScheduleBlockScheduler::addLiveRegs(std::set<unsigned> &Regs) {
1683 for (
unsigned Reg : Regs) {
1688 (void) LiveRegs.insert(
Reg);
1692 void SIScheduleBlockScheduler::decreaseLiveRegs(
SIScheduleBlock *Block,
1693 std::set<unsigned> &Regs) {
1694 for (
unsigned Reg : Regs) {
1696 std::set<unsigned>::iterator Pos = LiveRegs.find(
Reg);
1697 assert (Pos != LiveRegs.end() &&
1698 LiveRegsConsumers.find(
Reg) != LiveRegsConsumers.end() &&
1699 LiveRegsConsumers[
Reg] >= 1);
1700 --LiveRegsConsumers[
Reg];
1701 if (LiveRegsConsumers[
Reg] == 0)
1702 LiveRegs.erase(Pos);
1706 void SIScheduleBlockScheduler::releaseBlockSuccs(
SIScheduleBlock *Parent) {
1707 for (
const auto &Block : Parent->
getSuccs()) {
1708 if (--BlockNumPredsLeft[Block.first->
getID()] == 0)
1709 ReadyBlocks.push_back(Block.first);
1713 LastPosHighLatencyParentScheduled[Block.first->
getID()] = NumBlockScheduled;
1717 void SIScheduleBlockScheduler::blockScheduled(
SIScheduleBlock *Block) {
1718 decreaseLiveRegs(Block, Block->
getInRegs());
1720 releaseBlockSuccs(Block);
1721 for (std::map<unsigned, unsigned>::iterator RegI =
1722 LiveOutRegsNumUsages[Block->
getID()].begin(),
1723 E = LiveOutRegsNumUsages[Block->
getID()].end(); RegI !=
E; ++RegI) {
1724 std::pair<unsigned, unsigned> RegP = *RegI;
1726 assert(LiveRegsConsumers.find(RegP.first) == LiveRegsConsumers.end() ||
1727 LiveRegsConsumers[RegP.first] == 0);
1728 LiveRegsConsumers[RegP.first] += RegP.second;
1730 if (LastPosHighLatencyParentScheduled[Block->
getID()] >
1731 (
unsigned)LastPosWaitedHighLatency)
1732 LastPosWaitedHighLatency =
1733 LastPosHighLatencyParentScheduled[Block->
getID()];
1734 ++NumBlockScheduled;
1738 SIScheduleBlockScheduler::checkRegUsageImpact(std::set<unsigned> &InRegs,
1739 std::set<unsigned> &OutRegs) {
1740 std::vector<int> DiffSetPressure;
1743 for (
unsigned Reg : InRegs) {
1747 if (LiveRegsConsumers[
Reg] > 1)
1750 for (; PSetI.
isValid(); ++PSetI) {
1751 DiffSetPressure[*PSetI] -= PSetI.
getWeight();
1755 for (
unsigned Reg : OutRegs) {
1760 for (; PSetI.
isValid(); ++PSetI) {
1761 DiffSetPressure[*PSetI] += PSetI.
getWeight();
1765 return DiffSetPressure;
1771 SIScheduler::scheduleVariant(SISchedulerBlockCreatorVariant BlockVariant,
1772 SISchedulerBlockSchedulerVariant ScheduleVariant) {
1775 std::vector<SIScheduleBlock*> ScheduledBlocks;
1778 ScheduledBlocks = Scheduler.
getBlocks();
1780 for (
unsigned b = 0; b < ScheduledBlocks.size(); ++b) {
1784 for (
SUnit* SU : SUs)
1809 void SIScheduleDAGMI::topologicalSort() {
1822 void SIScheduleDAGMI::moveLowLatencies() {
1823 unsigned DAGSize =
SUnits.size();
1824 int LastLowLatencyUser = -1;
1825 int LastLowLatencyPos = -1;
1827 for (
unsigned i = 0,
e = ScheduledSUnits.size(); i !=
e; ++i) {
1829 bool IsLowLatencyUser =
false;
1830 unsigned MinPos = 0;
1835 IsLowLatencyUser =
true;
1839 unsigned PredPos = ScheduledSUnitsInv[Pred->
NodeNum];
1840 if (PredPos >= MinPos)
1841 MinPos = PredPos + 1;
1845 unsigned BestPos = LastLowLatencyUser + 1;
1846 if ((
int)BestPos <= LastLowLatencyPos)
1847 BestPos = LastLowLatencyPos + 1;
1848 if (BestPos < MinPos)
1851 for (
unsigned u = i; u > BestPos; --u) {
1852 ++ScheduledSUnitsInv[ScheduledSUnits[u-1]];
1853 ScheduledSUnits[u] = ScheduledSUnits[u-1];
1855 ScheduledSUnits[BestPos] = SU->
NodeNum;
1856 ScheduledSUnitsInv[SU->
NodeNum] = BestPos;
1858 LastLowLatencyPos = BestPos;
1859 if (IsLowLatencyUser)
1860 LastLowLatencyUser = BestPos;
1861 }
else if (IsLowLatencyUser) {
1862 LastLowLatencyUser = i;
1866 bool CopyForLowLat =
false;
1872 CopyForLowLat =
true;
1878 for (
unsigned u = i; u > MinPos; --u) {
1879 ++ScheduledSUnitsInv[ScheduledSUnits[u-1]];
1880 ScheduledSUnits[u] = ScheduledSUnits[u-1];
1882 ScheduledSUnits[MinPos] = SU->
NodeNum;
1883 ScheduledSUnitsInv[SU->
NodeNum] = MinPos;
1890 for (
unsigned i = 0,
e =
SUnits.size(); i !=
e; ++i) {
1891 SUnits[i].isScheduled =
false;
1892 SUnits[i].WeakPredsLeft = SUnitsLinksBackup[i].WeakPredsLeft;
1893 SUnits[i].NumPredsLeft = SUnitsLinksBackup[i].NumPredsLeft;
1894 SUnits[i].WeakSuccsLeft = SUnitsLinksBackup[i].WeakSuccsLeft;
1895 SUnits[i].NumSuccsLeft = SUnitsLinksBackup[i].NumSuccsLeft;
1900 template<
typename _Iterator>
void 1902 unsigned &VgprUsage,
unsigned &SgprUsage) {
1905 for (_Iterator RegI = First; RegI != End; ++RegI) {
1906 unsigned Reg = *RegI;
1911 for (; PSetI.
isValid(); ++PSetI) {
1912 if (*PSetI == VGPRSetID)
1914 else if (*PSetI == SGPRSetID)
1940 SUnitsLinksBackup =
SUnits;
1949 for (
unsigned i = 0,
e = (
unsigned)
SUnits.size(); i !=
e; ++i) {
1981 for (std::pair<SISchedulerBlockCreatorVariant, SISchedulerBlockSchedulerVariant> v : Variants) {
2002 for (std::pair<SISchedulerBlockCreatorVariant, SISchedulerBlockSchedulerVariant> v : Variants) {
2009 ScheduledSUnits = Best.
SUs;
2010 ScheduledSUnitsInv.resize(
SUnits.size());
2012 for (
unsigned i = 0,
e = (
unsigned)
SUnits.size(); i !=
e; ++i) {
2013 ScheduledSUnitsInv[ScheduledSUnits[i]] = i;
2023 for (std::vector<unsigned>::iterator
I = ScheduledSUnits.begin(),
2024 E = ScheduledSUnits.end();
I !=
E; ++
I) {
2038 dbgs() <<
"*** Final schedule for "
ScheduleDAGTopologicalSort Topo
Topo - A topological ordering for SUnits which permits fast IsReachable and similar queries...
Interface definition for SIRegisterInfo.
virtual unsigned getNumRegPressureSets() const =0
Get the number of dimensions of register pressure.
std::vector< SIScheduleBlock * > Blocks
SIScheduleDAGMI(MachineSchedContext *C)
void dumpSchedule() const
dump the scheduled Sequence.
This class represents lattice values for constants.
MachineBasicBlock::iterator CurrentTop
The top of the unscheduled zone.
SIScheduleCandReason Reason
void dump() const override
std::vector< unsigned > IsLowLatencySU
void addUnit(SUnit *SU)
Functions for Block construction.
std::vector< SIScheduleBlock * > getBlocks()
static MachineBasicBlock::iterator nextIfDebug(MachineBasicBlock::iterator I, MachineBasicBlock::const_iterator End)
Non-const version.
std::unique_ptr< MachineSchedStrategy > SchedImpl
SmallVector< SDep, 4 > Preds
All sunit predecessors.
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
bool isScheduled
True once scheduled.
std::vector< unsigned > LowLatencyOffset
static bool hasDataDependencyPred(const SUnit &SU, const SUnit &FromSU)
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Regular data dependence (aka true-dependence).
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
ArrayRef< std::pair< SIScheduleBlock *, SIScheduleBlockLinkKind > > getSuccs() const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void InitDAGTopologicalSorting()
Creates the initial topological ordering from the DAG to be scheduled.
MachineBasicBlock::const_iterator getPos() const
Get the MI position corresponding to this register pressure.
static bool tryLess(int TryVal, int CandVal, SISchedulerCandidate &TryCand, SISchedulerCandidate &Cand, SIScheduleCandReason Reason)
defusechain_iterator - This class provides iterator support for machine operands in the function that...
static const char * getReasonStr(SIScheduleCandReason Reason)
std::set< unsigned > getOutRegs()
void scheduleMI(SUnit *SU, bool IsTopNode)
Move an instruction and update register pressure.
std::set< unsigned > getInRegs()
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
std::vector< int > GetSubGraph(const SUnit &StartSU, const SUnit &TargetSU, bool &Success)
Returns an array of SUs that are both in the successor subtree of StartSU and in the predecessor subt...
MachineBasicBlock::iterator RegionBegin
The beginning of the range to be scheduled.
void buildDAGWithRegPressure()
Call ScheduleDAGInstrs::buildSchedGraph with register pressure tracking enabled.
static def_instr_iterator def_instr_end()
MachineBasicBlock::iterator begin() const
Returns an iterator to the top of the current scheduling region.
void schedule() override
Implement ScheduleDAGInstrs interface for scheduling a sequence of reorderable instructions.
bool isCtrl() const
Shorthand for getKind() != SDep::Data.
unsigned NumPredsLeft
of preds not scheduled.
std::vector< int > TopDownIndex2Block
struct SIScheduleBlockResult scheduleVariant(SISchedulerBlockCreatorVariant BlockVariant, SISchedulerBlockSchedulerVariant ScheduleVariant)
SI Machine Scheduler interface.
void printDebug(bool Full)
void addLiveRegs(ArrayRef< RegisterMaskPair > Regs)
Force liveness of virtual registers or physical register units.
reverse_iterator rbegin()
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
unsigned const MachineRegisterInfo * MRI
~SIScheduleDAGMI() override
RegisterPressure computed within a region of instructions delimited by TopIdx and BottomIdx...
bool isSUInBlock(SUnit *SU, unsigned ID)
std::vector< SUnit * > getScheduledUnits()
std::vector< int > TopDownIndex2SU
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SIScheduleBlockScheduler(SIScheduleDAGMI *DAG, SISchedulerBlockSchedulerVariant Variant, SIScheduleBlocks BlocksStruct)
Printable printVRegOrUnit(unsigned VRegOrUnit, const TargetRegisterInfo *TRI)
Create Printable object to print virtual registers and physical registers on a raw_ostream.
void addSucc(SIScheduleBlock *Succ, SIScheduleBlockLinkKind Kind)
MachineRegisterInfo * getMRI()
MachineBasicBlock * getBB()
Track the current register pressure at some position in the instruction stream, and remember the high...
unsigned getSGPRPressureSet() const
const TargetRegisterInfo * getTRI()
SIScheduleBlocks getBlocks(SISchedulerBlockCreatorVariant BlockVariant)
void initQueues(ArrayRef< SUnit *> TopRoots, ArrayRef< SUnit *> BotRoots)
Release ExitSU predecessors and setup scheduler queues.
bool getMemOperandWithOffset(const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset, const TargetRegisterInfo *TRI) const final
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
MachineBasicBlock::iterator getCurrentBottom()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void schedule(MachineBasicBlock::iterator BeginBlock, MachineBasicBlock::iterator EndBlock)
unsigned getVGPRPressureSet() const
static bool isEXP(const MachineInstr &MI)
unsigned WeakPredsLeft
of weak preds not scheduled.
std::vector< unsigned > MaxSetPressure
Map of max reg pressure indexed by pressure set ID, not class ID.
std::set< unsigned > & getOutRegs()
Color
A "color", which is either even or odd.
Align max(MaybeAlign Lhs, Align Rhs)
GenericScheduler shrinks the unscheduled zone using heuristics to balance the schedule.
bool isDebugValue() const
std::vector< unsigned > IsHighLatencySU
MachineOperand class - Representation of each machine instruction operand.
std::vector< unsigned > SUs
SISchedulerBlockCreatorVariant
SIScheduleBlockCreator(SIScheduleDAGMI *DAG)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineBasicBlock::iterator getCurrentTop()
void fillVgprSgprCost(_Iterator First, _Iterator End, unsigned &VgprUsage, unsigned &SgprUsage)
static bool tryGreater(int TryVal, int CandVal, SISchedulerCandidate &TryCand, SISchedulerCandidate &Cand, SIScheduleCandReason Reason)
bool isHighLatencyBlock()
def_instr_iterator def_instr_begin(unsigned RegNo) const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
ScheduleDAGTopologicalSort * GetTopo()
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
Interface definition for SIInstrInfo.
void addPred(SIScheduleBlock *Pred)
const TargetRegisterInfo * TRI
Target processor register info.
void findRootsAndBiasEdges(SmallVectorImpl< SUnit *> &TopRoots, SmallVectorImpl< SUnit *> &BotRoots)
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
std::vector< int > BottomUpIndex2SU
Iterate over the pressure sets affected by the given physical or virtual register.
PSetIterator getPressureSets(unsigned RegUnit) const
Get an iterator over the pressure sets affected by the given physical or virtual register.
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
static bool isDefBetween(unsigned Reg, SlotIndex First, SlotIndex Last, const MachineRegisterInfo *MRI, const LiveIntervals *LIS)
bool isHighLatencyInstruction(const MachineInstr &MI) const
bool isLowLatencyInstruction(const MachineInstr &MI) const
void placeDebugValues()
Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
const TargetInstrInfo * TII
Target instruction information.
void restoreSULinksLeft()
unsigned NodeNum
Entry # of node in the node vector.
MachineBasicBlock::iterator CurrentBottom
The bottom of the unscheduled zone.
std::set< unsigned > & getInRegs()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::vector< int > TopDownBlock2Index
const std::vector< SIScheduleBlock * > & getPreds() const
void setRepeat(SIScheduleCandReason R)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
SmallVector< SDep, 4 > Succs
All sunit successors.
static const Function * getParent(const Value *V)
bool isWeak() const
Tests if this a weak dependence.
Machine Instruction Scheduler
void setPos(MachineBasicBlock::const_iterator Pos)
unsigned getWeight() const
MachineRegisterInfo & MRI
Virtual/real register map.
std::vector< SUnit > SUnits
The scheduling units.
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
SlotIndex - An opaque wrapper around machine indexes.
unsigned getVGPRSetID() const
RegPressureTracker TopRPTracker
SISchedulerBlockSchedulerVariant
void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block...
Scheduling unit. This is a node in the scheduling DAG.