42 #define DEBUG_TYPE "machine-scheduler" 146 case NoCand:
return "NOCAND";
148 case Latency:
return "LATENCY";
150 case Depth:
return "DEPTH";
164 if (TryVal < CandVal) {
168 if (TryVal > CandVal) {
181 if (TryVal > CandVal) {
185 if (TryVal < CandVal) {
199 NodeNum2Index[SU->
NodeNum] = SUnits.size();
200 SUnits.push_back(SU);
204 void SIScheduleBlock::traceCandidate(
const SISchedCandidate &Cand) {
211 void SIScheduleBlock::tryCandidateTopDown(SISchedCandidate &Cand,
212 SISchedCandidate &TryCand) {
214 if (!Cand.isValid()) {
219 if (Cand.SGPRUsage > 60 &&
240 Cand.HasLowLatencyNonWaitedParent,
248 if (TryCand.IsLowLatency &&
258 if (TryCand.SU->NodeNum < Cand.SU->NodeNum) {
263 SUnit* SIScheduleBlock::pickNode() {
264 SISchedCandidate TopCand;
266 for (
SUnit* SU : TopReadySUs) {
267 SISchedCandidate TryCand;
268 std::vector<unsigned> pressure;
269 std::vector<unsigned> MaxPressure;
272 TopRPTracker.getDownwardPressure(SU->getInstr(), pressure, MaxPressure);
273 TryCand.SGPRUsage = pressure[DAG->getSGPRSetID()];
274 TryCand.VGPRUsage = pressure[DAG->getVGPRSetID()];
275 TryCand.IsLowLatency = DAG->IsLowLatencySU[SU->NodeNum];
276 TryCand.LowLatencyOffset = DAG->LowLatencyOffset[SU->NodeNum];
277 TryCand.HasLowLatencyNonWaitedParent =
278 HasLowLatencyNonWaitedParent[NodeNum2Index[SU->NodeNum]];
279 tryCandidateTopDown(TopCand, TryCand);
280 if (TryCand.Reason !=
NoCand)
281 TopCand.setBest(TryCand);
294 for (
SUnit* SU : SUnits) {
295 if (!SU->NumPredsLeft)
296 TopReadySUs.push_back(SU);
299 while (!TopReadySUs.empty()) {
300 SUnit *SU = TopReadySUs[0];
301 ScheduledSUnits.push_back(SU);
320 if (InstSlot >= First && InstSlot <= Last)
332 DAG->initRPTracker(TopRPTracker);
333 DAG->initRPTracker(BotRPTracker);
334 DAG->initRPTracker(RPTracker);
338 for (
SUnit* SU : ScheduledSUnits) {
339 RPTracker.setPos(SU->getInstr());
344 RPTracker.closeRegion();
347 TopRPTracker.addLiveRegs(RPTracker.getPressure().LiveInRegs);
348 BotRPTracker.
addLiveRegs(RPTracker.getPressure().LiveOutRegs);
351 for (
const auto &RegMaskPair : RPTracker.getPressure().LiveInRegs) {
353 LiveInRegs.insert(RegMaskPair.RegUnit);
378 for (
const auto &RegMaskPair : RPTracker.getPressure().LiveOutRegs) {
379 unsigned Reg = RegMaskPair.RegUnit;
384 LiveOutRegs.insert(Reg);
392 LiveInPressure = TopPressure.MaxSetPressure;
396 TopRPTracker.closeTop();
405 initRegPressure(BeginBlock, EndBlock);
412 for (
SUnit* SU : SUnits) {
413 if (!SU->NumPredsLeft)
414 TopReadySUs.push_back(SU);
417 while (!TopReadySUs.empty()) {
418 SUnit *SU = pickNode();
419 ScheduledSUnits.push_back(SU);
420 TopRPTracker.setPos(SU->
getInstr());
421 TopRPTracker.advance();
426 InternalAdditionnalPressure.resize(TopPressure.MaxSetPressure.size());
430 assert(SUnits.size() == ScheduledSUnits.size() &&
431 TopReadySUs.empty());
432 for (
SUnit* SU : SUnits) {
434 SU->NumPredsLeft == 0);
441 void SIScheduleBlock::undoSchedule() {
442 for (
SUnit* SU : SUnits) {
443 SU->isScheduled =
false;
444 for (
SDep& Succ : SU->Succs) {
446 undoReleaseSucc(SU, &Succ);
449 HasLowLatencyNonWaitedParent.assign(SUnits.size(), 0);
450 ScheduledSUnits.clear();
454 void SIScheduleBlock::undoReleaseSucc(
SUnit *SU,
SDep *SuccEdge) {
464 void SIScheduleBlock::releaseSucc(
SUnit *SU,
SDep *SuccEdge) {
473 dbgs() <<
"*** Scheduling failed! ***\n";
475 dbgs() <<
" has been released too many times!\n";
484 void SIScheduleBlock::releaseSuccessors(
SUnit *SU,
bool InOrOutBlock) {
488 if (SuccSU->
NodeNum >= DAG->SUnits.size())
491 if (BC->isSUInBlock(SuccSU,
ID) != InOrOutBlock)
494 releaseSucc(SU, &Succ);
496 TopReadySUs.push_back(SuccSU);
500 void SIScheduleBlock::nodeScheduled(
SUnit *SU) {
503 std::vector<SUnit *>::iterator
I =
llvm::find(TopReadySUs, SU);
504 if (I == TopReadySUs.end()) {
505 dbgs() <<
"Data Structure Bug in SI Scheduler\n";
508 TopReadySUs.erase(I);
510 releaseSuccessors(SU,
true);
513 if (HasLowLatencyNonWaitedParent[NodeNum2Index[SU->
NodeNum]])
514 HasLowLatencyNonWaitedParent.assign(SUnits.size(), 0);
516 if (DAG->IsLowLatencySU[SU->
NodeNum]) {
518 std::map<unsigned, unsigned>::iterator I =
520 if (I != NodeNum2Index.end())
521 HasLowLatencyNonWaitedParent[I->second] = 1;
529 for (
SUnit* SU : SUnits) {
530 releaseSuccessors(SU,
false);
531 if (DAG->IsHighLatencySU[SU->
NodeNum])
532 HighLatencyBlock =
true;
534 HasLowLatencyNonWaitedParent.resize(SUnits.size(), 0);
539 unsigned PredID = Pred->
getID();
543 if (PredID ==
P->getID())
546 Preds.push_back(Pred);
551 return PredID == S.first->getID();
553 "Loop in the Block Graph!");
558 unsigned SuccID = Succ->
getID();
561 for (std::pair<SIScheduleBlock*, SIScheduleBlockLinkKind> &S : Succs) {
562 if (SuccID == S.first->getID()) {
570 ++NumHighLatencySuccessors;
571 Succs.push_back(std::make_pair(Succ, Kind));
575 "Loop in the Block Graph!");
580 dbgs() <<
"Block (" <<
ID <<
")\n";
584 dbgs() <<
"\nContains High Latency Instruction: " 585 << HighLatencyBlock <<
'\n';
586 dbgs() <<
"\nDepends On:\n";
588 P->printDebug(
false);
591 dbgs() <<
"\nSuccessors:\n";
592 for (std::pair<SIScheduleBlock*, SIScheduleBlockLinkKind> S : Succs) {
594 dbgs() <<
"(Data Dep) ";
595 S.first->printDebug(
false);
599 dbgs() <<
"LiveInPressure " << LiveInPressure[DAG->getSGPRSetID()] <<
' ' 600 << LiveInPressure[DAG->getVGPRSetID()] <<
'\n';
601 dbgs() <<
"LiveOutPressure " << LiveOutPressure[DAG->getSGPRSetID()] <<
' ' 602 << LiveOutPressure[DAG->getVGPRSetID()] <<
"\n\n";
603 dbgs() <<
"LiveIns:\n";
604 for (
unsigned Reg : LiveInRegs)
607 dbgs() <<
"\nLiveOuts:\n";
608 for (
unsigned Reg : LiveOutRegs)
612 dbgs() <<
"\nInstructions:\n";
614 for (
SUnit* SU : SUnits) {
618 for (
SUnit* SU : SUnits) {
623 dbgs() <<
"///////////////////////\n";
637 std::map<SISchedulerBlockCreatorVariant, SIScheduleBlocks>::iterator
B =
638 Blocks.find(BlockVariant);
639 if (B == Blocks.end()) {
641 createBlocksForVariant(BlockVariant);
643 scheduleInsideBlocks();
645 Res.
Blocks = CurrentBlocks;
648 Blocks[BlockVariant] = Res;
658 return CurrentBlocks[Node2CurrentBlock[SU->
NodeNum]]->getID() ==
ID;
661 void SIScheduleBlockCreator::colorHighLatenciesAlone() {
662 unsigned DAGSize = DAG->
SUnits.size();
664 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
667 CurrentColoring[SU->
NodeNum] = NextReservedID++;
674 for (
const auto &PredDep : SU.
Preds) {
675 if (PredDep.getSUnit() == &FromSU &&
682 void SIScheduleBlockCreator::colorHighLatenciesGroups() {
683 unsigned DAGSize = DAG->
SUnits.size();
684 unsigned NumHighLatencies = 0;
686 int Color = NextReservedID;
688 std::set<unsigned> FormingGroup;
690 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
696 if (NumHighLatencies == 0)
699 if (NumHighLatencies <= 6)
701 else if (NumHighLatencies <= 12)
709 unsigned CompatibleGroup =
true;
710 int ProposedColor =
Color;
711 std::vector<int> AdditionalElements;
723 for (
unsigned j : FormingGroup) {
725 std::vector<int> SubGraph;
738 else if (SubGraph.size() > 5) {
740 CompatibleGroup =
false;
745 for (
unsigned k : SubGraph) {
751 (CurrentColoring[k] != ProposedColor &&
752 CurrentColoring[k] != 0)) {
753 CompatibleGroup =
false;
759 CompatibleGroup =
false;
763 if (!CompatibleGroup)
767 CompatibleGroup =
false;
775 AdditionalElements.insert(AdditionalElements.end(),
776 SubGraph.begin(), SubGraph.end());
779 if (CompatibleGroup) {
780 FormingGroup.insert(SU.
NodeNum);
781 for (
unsigned j : AdditionalElements)
782 CurrentColoring[j] = ProposedColor;
783 CurrentColoring[SU.
NodeNum] = ProposedColor;
789 if (!CompatibleGroup) {
790 FormingGroup.clear();
791 Color = ++NextReservedID;
792 ProposedColor =
Color;
793 FormingGroup.insert(SU.
NodeNum);
794 CurrentColoring[SU.
NodeNum] = ProposedColor;
796 }
else if (Count == GroupSize) {
797 FormingGroup.clear();
798 Color = ++NextReservedID;
799 ProposedColor =
Color;
806 void SIScheduleBlockCreator::colorComputeReservedDependencies() {
807 unsigned DAGSize = DAG->
SUnits.size();
808 std::map<std::set<unsigned>,
unsigned> ColorCombinations;
810 CurrentTopDownReservedDependencyColoring.clear();
811 CurrentBottomUpReservedDependencyColoring.clear();
813 CurrentTopDownReservedDependencyColoring.resize(DAGSize, 0);
814 CurrentBottomUpReservedDependencyColoring.resize(DAGSize, 0);
821 std::set<unsigned> SUColors;
824 if (CurrentColoring[SU->
NodeNum]) {
825 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
834 if (CurrentTopDownReservedDependencyColoring[Pred->
NodeNum] > 0)
835 SUColors.insert(CurrentTopDownReservedDependencyColoring[Pred->
NodeNum]);
838 if (SUColors.empty())
841 if (SUColors.size() == 1 && *SUColors.begin() > DAGSize)
842 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
845 std::map<std::set<unsigned>,
unsigned>::iterator Pos =
846 ColorCombinations.find(SUColors);
847 if (Pos != ColorCombinations.end()) {
848 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] = Pos->second;
850 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] =
852 ColorCombinations[SUColors] = NextNonReservedID++;
857 ColorCombinations.clear();
863 std::set<unsigned> SUColors;
866 if (CurrentColoring[SU->
NodeNum]) {
867 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
876 if (CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum] > 0)
877 SUColors.insert(CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum]);
880 if (SUColors.empty())
883 if (SUColors.size() == 1 && *SUColors.begin() > DAGSize)
884 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
887 std::map<std::set<unsigned>,
unsigned>::iterator Pos =
888 ColorCombinations.find(SUColors);
889 if (Pos != ColorCombinations.end()) {
890 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] = Pos->second;
892 CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] =
894 ColorCombinations[SUColors] = NextNonReservedID++;
900 void SIScheduleBlockCreator::colorAccordingToReservedDependencies() {
901 unsigned DAGSize = DAG->
SUnits.size();
902 std::map<std::pair<unsigned, unsigned>,
unsigned> ColorCombinations;
907 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
909 std::pair<unsigned, unsigned> SUColors;
912 if (CurrentColoring[SU->
NodeNum])
915 SUColors.first = CurrentTopDownReservedDependencyColoring[SU->
NodeNum];
916 SUColors.second = CurrentBottomUpReservedDependencyColoring[SU->
NodeNum];
918 std::map<std::pair<unsigned, unsigned>,
unsigned>::iterator Pos =
919 ColorCombinations.find(SUColors);
920 if (Pos != ColorCombinations.end()) {
921 CurrentColoring[SU->
NodeNum] = Pos->second;
923 CurrentColoring[SU->
NodeNum] = NextNonReservedID;
924 ColorCombinations[SUColors] = NextNonReservedID++;
929 void SIScheduleBlockCreator::colorEndsAccordingToDependencies() {
930 unsigned DAGSize = DAG->
SUnits.size();
931 std::vector<int> PendingColoring = CurrentColoring;
934 CurrentBottomUpReservedDependencyColoring.size() == DAGSize &&
935 CurrentTopDownReservedDependencyColoring.size() == DAGSize);
938 if (*std::max_element(CurrentBottomUpReservedDependencyColoring.begin(),
939 CurrentBottomUpReservedDependencyColoring.end()) == 0 &&
940 *std::max_element(CurrentTopDownReservedDependencyColoring.begin(),
941 CurrentTopDownReservedDependencyColoring.end()) == 0)
946 std::set<unsigned> SUColors;
947 std::set<unsigned> SUColorsPending;
949 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
952 if (CurrentBottomUpReservedDependencyColoring[SU->
NodeNum] > 0 ||
953 CurrentTopDownReservedDependencyColoring[SU->
NodeNum] > 0)
960 if (CurrentBottomUpReservedDependencyColoring[Succ->
NodeNum] > 0 ||
961 CurrentTopDownReservedDependencyColoring[Succ->
NodeNum] > 0)
962 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
963 SUColorsPending.insert(PendingColoring[Succ->
NodeNum]);
968 if (SUColors.size() == 1 && SUColorsPending.size() == 1)
969 PendingColoring[SU->
NodeNum] = *SUColors.begin();
972 PendingColoring[SU->
NodeNum] = NextNonReservedID++;
974 CurrentColoring = PendingColoring;
978 void SIScheduleBlockCreator::colorForceConsecutiveOrderInGroup() {
979 unsigned DAGSize = DAG->
SUnits.size();
980 unsigned PreviousColor;
981 std::set<unsigned> SeenColors;
986 PreviousColor = CurrentColoring[0];
988 for (
unsigned i = 1, e = DAGSize; i != e; ++i) {
990 unsigned CurrentColor = CurrentColoring[i];
991 unsigned PreviousColorSave = PreviousColor;
994 if (CurrentColor != PreviousColor)
995 SeenColors.insert(PreviousColor);
996 PreviousColor = CurrentColor;
998 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1001 if (SeenColors.find(CurrentColor) == SeenColors.end())
1004 if (PreviousColorSave != CurrentColor)
1005 CurrentColoring[i] = NextNonReservedID++;
1007 CurrentColoring[i] = CurrentColoring[i-1];
1011 void SIScheduleBlockCreator::colorMergeConstantLoadsNextGroup() {
1012 unsigned DAGSize = DAG->
SUnits.size();
1016 std::set<unsigned> SUColors;
1018 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1030 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1032 if (SUColors.size() == 1)
1033 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1037 void SIScheduleBlockCreator::colorMergeIfPossibleNextGroup() {
1038 unsigned DAGSize = DAG->
SUnits.size();
1042 std::set<unsigned> SUColors;
1044 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1051 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1053 if (SUColors.size() == 1)
1054 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1058 void SIScheduleBlockCreator::colorMergeIfPossibleNextGroupOnlyForReserved() {
1059 unsigned DAGSize = DAG->
SUnits.size();
1063 std::set<unsigned> SUColors;
1065 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1072 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1074 if (SUColors.size() == 1 && *SUColors.begin() <= DAGSize)
1075 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1079 void SIScheduleBlockCreator::colorMergeIfPossibleSmallGroupsToNextGroup() {
1080 unsigned DAGSize = DAG->
SUnits.size();
1081 std::map<unsigned, unsigned> ColorCount;
1085 unsigned color = CurrentColoring[SU->
NodeNum];
1086 ++ColorCount[color];
1091 unsigned color = CurrentColoring[SU->
NodeNum];
1092 std::set<unsigned> SUColors;
1094 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1097 if (ColorCount[color] > 1)
1104 SUColors.insert(CurrentColoring[Succ->
NodeNum]);
1106 if (SUColors.size() == 1 && *SUColors.begin() != color) {
1107 --ColorCount[color];
1108 CurrentColoring[SU->
NodeNum] = *SUColors.begin();
1109 ++ColorCount[*SUColors.begin()];
1114 void SIScheduleBlockCreator::cutHugeBlocks() {
1118 void SIScheduleBlockCreator::regroupNoUserInstructions() {
1119 unsigned DAGSize = DAG->
SUnits.size();
1120 int GroupID = NextNonReservedID++;
1124 bool hasSuccessor =
false;
1126 if (CurrentColoring[SU->
NodeNum] <= (
int)DAGSize)
1133 hasSuccessor =
true;
1136 CurrentColoring[SU->
NodeNum] = GroupID;
1140 void SIScheduleBlockCreator::colorExports() {
1141 unsigned ExportColor = NextNonReservedID++;
1161 for (
unsigned j : ExpGroup) {
1163 std::vector<int> SubGraph;
1179 for (
unsigned k : SubGraph) {
1187 ExpGroup.push_back(SUNum);
1192 for (
unsigned j : ExpGroup)
1193 CurrentColoring[j] = ExportColor;
1197 unsigned DAGSize = DAG->
SUnits.size();
1198 std::map<unsigned,unsigned> RealID;
1200 CurrentBlocks.clear();
1201 CurrentColoring.clear();
1202 CurrentColoring.resize(DAGSize, 0);
1203 Node2CurrentBlock.clear();
1209 NextNonReservedID = DAGSize + 1;
1214 colorHighLatenciesGroups();
1216 colorHighLatenciesAlone();
1217 colorComputeReservedDependencies();
1218 colorAccordingToReservedDependencies();
1219 colorEndsAccordingToDependencies();
1221 colorForceConsecutiveOrderInGroup();
1222 regroupNoUserInstructions();
1223 colorMergeConstantLoadsNextGroup();
1224 colorMergeIfPossibleNextGroupOnlyForReserved();
1228 Node2CurrentBlock.resize(DAGSize, -1);
1229 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1232 if (RealID.find(Color) == RealID.end()) {
1233 int ID = CurrentBlocks.size();
1234 BlockPtrs.push_back(llvm::make_unique<SIScheduleBlock>(DAG,
this, ID));
1235 CurrentBlocks.push_back(BlockPtrs.rbegin()->get());
1238 CurrentBlocks[RealID[
Color]]->addUnit(SU);
1243 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1245 int SUID = Node2CurrentBlock[i];
1250 if (Node2CurrentBlock[Succ->
NodeNum] != SUID)
1251 CurrentBlocks[SUID]->addSucc(CurrentBlocks[Node2CurrentBlock[Succ->
NodeNum]],
1258 if (Node2CurrentBlock[Pred->
NodeNum] != SUID)
1259 CurrentBlocks[SUID]->addPred(CurrentBlocks[Node2CurrentBlock[Pred->
NodeNum]]);
1264 for (
unsigned i = 0, e = CurrentBlocks.size(); i != e; ++i) {
1269 for (
unsigned i = 0, e = CurrentBlocks.size(); i != e; ++i) {
1281 for (; I != End; ++
I) {
1282 if (!I->isDebugInstr())
1288 void SIScheduleBlockCreator::topologicalSort() {
1289 unsigned DAGSize = CurrentBlocks.size();
1290 std::vector<int> WorkList;
1294 WorkList.reserve(DAGSize);
1295 TopDownIndex2Block.resize(DAGSize);
1296 TopDownBlock2Index.resize(DAGSize);
1297 BottomUpIndex2Block.resize(DAGSize);
1299 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1301 unsigned Degree = Block->
getSuccs().size();
1302 TopDownBlock2Index[i] = Degree;
1304 WorkList.push_back(i);
1309 while (!WorkList.empty()) {
1310 int i = WorkList.back();
1312 WorkList.pop_back();
1313 TopDownBlock2Index[i] = --
Id;
1314 TopDownIndex2Block[
Id] = i;
1316 if (!--TopDownBlock2Index[Pred->getID()])
1317 WorkList.push_back(Pred->getID());
1323 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1326 assert(TopDownBlock2Index[i] > TopDownBlock2Index[Pred->getID()] &&
1327 "Wrong Top Down topological sorting");
1332 BottomUpIndex2Block = std::vector<int>(TopDownIndex2Block.rbegin(),
1333 TopDownIndex2Block.rend());
1336 void SIScheduleBlockCreator::scheduleInsideBlocks() {
1337 unsigned DAGSize = CurrentBlocks.size();
1343 LLVM_DEBUG(
dbgs() <<
"First phase: Fast scheduling for Reg Liveness\n");
1344 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1354 std::vector<MachineBasicBlock::iterator> PosOld;
1355 std::vector<MachineBasicBlock::iterator> PosNew;
1356 PosOld.reserve(DAG->
SUnits.size());
1357 PosNew.reserve(DAG->
SUnits.size());
1359 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1360 int BlockIndice = TopDownIndex2Block[i];
1364 for (
SUnit* SU : SUs) {
1367 PosOld.push_back(Pos);
1368 if (&*CurrentTopFastSched == MI) {
1369 PosNew.push_back(Pos);
1370 CurrentTopFastSched =
nextIfDebug(++CurrentTopFastSched,
1382 PosNew.push_back(CurrentTopFastSched);
1391 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1394 Block->
schedule((*SUs.begin())->getInstr(), (*SUs.rbegin())->getInstr());
1399 for (
unsigned i = PosOld.size(), e = 0; i != e; --i) {
1411 LLVM_DEBUG(
for (
unsigned i = 0, e = CurrentBlocks.size(); i != e; ++i) {
1417 void SIScheduleBlockCreator::fillStats() {
1418 unsigned DAGSize = CurrentBlocks.size();
1420 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1421 int BlockIndice = TopDownIndex2Block[i];
1428 if (Depth < Pred->
Depth + Pred->getCost())
1429 Depth = Pred->Depth + Pred->getCost();
1435 for (
unsigned i = 0, e = DAGSize; i != e; ++i) {
1436 int BlockIndice = BottomUpIndex2Block[i];
1441 unsigned Height = 0;
1443 Height =
std::max(Height, Succ.first->Height + Succ.first->getCost());
1454 DAG(DAG), Variant(Variant), Blocks(BlocksStruct.Blocks),
1455 LastPosWaitedHighLatency(0), NumBlockScheduled(0), VregCurrentUsage(0),
1456 SregCurrentUsage(0), maxVregUsage(0), maxSregUsage(0) {
1468 LiveOutRegsNumUsages.resize(Blocks.size());
1469 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1475 std::set<unsigned> PredOutRegs = Pred->getOutRegs();
1476 std::set<unsigned>::iterator RegPos = PredOutRegs.find(
Reg);
1478 if (RegPos != PredOutRegs.end()) {
1490 ++LiveOutRegsNumUsages[PredID][
Reg];
1494 LastPosHighLatencyParentScheduled.resize(Blocks.size(), 0);
1495 BlockNumPredsLeft.resize(Blocks.size());
1496 BlockNumSuccsLeft.resize(Blocks.size());
1498 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1500 BlockNumPredsLeft[i] = Block->
getPreds().size();
1501 BlockNumSuccsLeft[i] = Block->
getSuccs().size();
1505 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1511 std::set<unsigned> InRegs = DAG->
getInRegs();
1512 addLiveRegs(InRegs);
1518 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1522 const std::set<unsigned> &OutRegs = Block->
getOutRegs();
1524 if (OutRegs.find(
Reg) == OutRegs.end())
1527 ++LiveOutRegsNumUsages[
ID][
Reg];
1534 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1539 std::set<unsigned> PredOutRegs = Pred->getOutRegs();
1540 std::set<unsigned>::iterator RegPos = PredOutRegs.find(
Reg);
1542 if (RegPos != PredOutRegs.end()) {
1549 ++LiveRegsConsumers[
Reg];
1553 for (
unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1555 if (BlockNumPredsLeft[i] == 0) {
1556 ReadyBlocks.push_back(Block);
1561 BlocksScheduled.push_back(Block);
1562 blockScheduled(Block);
1566 : BlocksScheduled) {
1567 dbgs() <<
' ' << Block->getID();
1571 bool SIScheduleBlockScheduler::tryCandidateLatency(SIBlockSchedCandidate &Cand,
1572 SIBlockSchedCandidate &TryCand) {
1573 if (!Cand.isValid()) {
1580 Cand.LastPosHighLatParentScheduled, TryCand, Cand,
Latency))
1587 TryCand, Cand,
Depth))
1590 Cand.NumHighLatencySuccessors,
1596 bool SIScheduleBlockScheduler::tryCandidateRegUsage(SIBlockSchedCandidate &Cand,
1597 SIBlockSchedCandidate &TryCand) {
1598 if (!Cand.isValid()) {
1607 Cand.NumSuccessors > 0,
1619 SIBlockSchedCandidate Cand;
1620 std::vector<SIScheduleBlock*>::iterator Best;
1622 if (ReadyBlocks.empty())
1626 VregCurrentUsage, SregCurrentUsage);
1627 if (VregCurrentUsage > maxVregUsage)
1628 maxVregUsage = VregCurrentUsage;
1629 if (SregCurrentUsage > maxSregUsage)
1630 maxSregUsage = SregCurrentUsage;
1633 : ReadyBlocks)
dbgs()
1634 << Block->
getID() <<
' ';
1635 dbgs() <<
"\nCurrent Live:\n";
1640 dbgs() <<
"Current VGPRs: " << VregCurrentUsage <<
'\n';
1641 dbgs() <<
"Current SGPRs: " << SregCurrentUsage <<
'\n';);
1643 Cand.Block =
nullptr;
1644 for (std::vector<SIScheduleBlock*>::iterator
I = ReadyBlocks.begin(),
1645 E = ReadyBlocks.end();
I !=
E; ++
I) {
1646 SIBlockSchedCandidate TryCand;
1648 TryCand.IsHighLatency = TryCand.Block->isHighLatencyBlock();
1649 TryCand.VGPRUsageDiff =
1650 checkRegUsageImpact(TryCand.Block->getInRegs(),
1652 TryCand.NumSuccessors = TryCand.Block->getSuccs().size();
1653 TryCand.NumHighLatencySuccessors =
1654 TryCand.Block->getNumHighLatencySuccessors();
1655 TryCand.LastPosHighLatParentScheduled =
1656 (
unsigned int) std::max<int> (0,
1657 LastPosHighLatencyParentScheduled[TryCand.Block->getID()] -
1658 LastPosWaitedHighLatency);
1659 TryCand.Height = TryCand.Block->Height;
1661 if (VregCurrentUsage > 120 ||
1663 if (!tryCandidateRegUsage(Cand, TryCand) &&
1665 tryCandidateLatency(Cand, TryCand);
1667 if (!tryCandidateLatency(Cand, TryCand))
1668 tryCandidateRegUsage(Cand, TryCand);
1670 if (TryCand.Reason !=
NoCand) {
1671 Cand.setBest(TryCand);
1673 LLVM_DEBUG(
dbgs() <<
"Best Current Choice: " << Cand.Block->getID() <<
' ' 1679 dbgs() <<
"Is a block with high latency instruction: " 1680 << (Cand.IsHighLatency ?
"yes\n" :
"no\n");
1681 dbgs() <<
"Position of last high latency dependency: " 1682 << Cand.LastPosHighLatParentScheduled <<
'\n';
1683 dbgs() <<
"VGPRUsageDiff: " << Cand.VGPRUsageDiff <<
'\n';
1687 ReadyBlocks.erase(Best);
1693 void SIScheduleBlockScheduler::addLiveRegs(std::set<unsigned> &
Regs) {
1694 for (
unsigned Reg : Regs) {
1699 (void) LiveRegs.insert(
Reg);
1703 void SIScheduleBlockScheduler::decreaseLiveRegs(
SIScheduleBlock *Block,
1704 std::set<unsigned> &
Regs) {
1705 for (
unsigned Reg : Regs) {
1707 std::set<unsigned>::iterator Pos = LiveRegs.find(
Reg);
1708 assert (Pos != LiveRegs.end() &&
1709 LiveRegsConsumers.find(
Reg) != LiveRegsConsumers.end() &&
1710 LiveRegsConsumers[
Reg] >= 1);
1711 --LiveRegsConsumers[
Reg];
1712 if (LiveRegsConsumers[
Reg] == 0)
1713 LiveRegs.erase(Pos);
1717 void SIScheduleBlockScheduler::releaseBlockSuccs(
SIScheduleBlock *Parent) {
1718 for (
const auto &Block : Parent->
getSuccs()) {
1719 if (--BlockNumPredsLeft[Block.first->
getID()] == 0)
1720 ReadyBlocks.push_back(Block.first);
1724 LastPosHighLatencyParentScheduled[Block.first->
getID()] = NumBlockScheduled;
1728 void SIScheduleBlockScheduler::blockScheduled(
SIScheduleBlock *Block) {
1729 decreaseLiveRegs(Block, Block->
getInRegs());
1731 releaseBlockSuccs(Block);
1732 for (std::map<unsigned, unsigned>::iterator RegI =
1733 LiveOutRegsNumUsages[Block->
getID()].begin(),
1734 E = LiveOutRegsNumUsages[Block->
getID()].end(); RegI !=
E; ++RegI) {
1735 std::pair<unsigned, unsigned> RegP = *RegI;
1737 assert(LiveRegsConsumers.find(RegP.first) == LiveRegsConsumers.end() ||
1738 LiveRegsConsumers[RegP.first] == 0);
1739 LiveRegsConsumers[RegP.first] += RegP.second;
1741 if (LastPosHighLatencyParentScheduled[Block->
getID()] >
1742 (
unsigned)LastPosWaitedHighLatency)
1743 LastPosWaitedHighLatency =
1744 LastPosHighLatencyParentScheduled[Block->
getID()];
1745 ++NumBlockScheduled;
1749 SIScheduleBlockScheduler::checkRegUsageImpact(std::set<unsigned> &InRegs,
1750 std::set<unsigned> &OutRegs) {
1751 std::vector<int> DiffSetPressure;
1754 for (
unsigned Reg : InRegs) {
1758 if (LiveRegsConsumers[
Reg] > 1)
1761 for (; PSetI.
isValid(); ++PSetI) {
1762 DiffSetPressure[*PSetI] -= PSetI.
getWeight();
1766 for (
unsigned Reg : OutRegs) {
1771 for (; PSetI.
isValid(); ++PSetI) {
1772 DiffSetPressure[*PSetI] += PSetI.
getWeight();
1776 return DiffSetPressure;
1782 SIScheduler::scheduleVariant(SISchedulerBlockCreatorVariant BlockVariant,
1783 SISchedulerBlockSchedulerVariant ScheduleVariant) {
1786 std::vector<SIScheduleBlock*> ScheduledBlocks;
1789 ScheduledBlocks = Scheduler.
getBlocks();
1791 for (
unsigned b = 0; b < ScheduledBlocks.size(); ++b) {
1795 for (
SUnit* SU : SUs)
1820 void SIScheduleDAGMI::topologicalSort() {
1833 void SIScheduleDAGMI::moveLowLatencies() {
1834 unsigned DAGSize =
SUnits.size();
1835 int LastLowLatencyUser = -1;
1836 int LastLowLatencyPos = -1;
1838 for (
unsigned i = 0, e = ScheduledSUnits.size(); i != e; ++i) {
1840 bool IsLowLatencyUser =
false;
1841 unsigned MinPos = 0;
1846 IsLowLatencyUser =
true;
1850 unsigned PredPos = ScheduledSUnitsInv[Pred->
NodeNum];
1851 if (PredPos >= MinPos)
1852 MinPos = PredPos + 1;
1856 unsigned BestPos = LastLowLatencyUser + 1;
1857 if ((
int)BestPos <= LastLowLatencyPos)
1858 BestPos = LastLowLatencyPos + 1;
1859 if (BestPos < MinPos)
1862 for (
unsigned u = i; u > BestPos; --u) {
1863 ++ScheduledSUnitsInv[ScheduledSUnits[u-1]];
1864 ScheduledSUnits[u] = ScheduledSUnits[u-1];
1866 ScheduledSUnits[BestPos] = SU->
NodeNum;
1867 ScheduledSUnitsInv[SU->
NodeNum] = BestPos;
1869 LastLowLatencyPos = BestPos;
1870 if (IsLowLatencyUser)
1871 LastLowLatencyUser = BestPos;
1872 }
else if (IsLowLatencyUser) {
1873 LastLowLatencyUser = i;
1877 bool CopyForLowLat =
false;
1881 CopyForLowLat =
true;
1887 for (
unsigned u = i; u > MinPos; --u) {
1888 ++ScheduledSUnitsInv[ScheduledSUnits[u-1]];
1889 ScheduledSUnits[u] = ScheduledSUnits[u-1];
1891 ScheduledSUnits[MinPos] = SU->
NodeNum;
1892 ScheduledSUnitsInv[SU->
NodeNum] = MinPos;
1899 for (
unsigned i = 0, e =
SUnits.size(); i != e; ++i) {
1900 SUnits[i].isScheduled =
false;
1901 SUnits[i].WeakPredsLeft = SUnitsLinksBackup[i].WeakPredsLeft;
1902 SUnits[i].NumPredsLeft = SUnitsLinksBackup[i].NumPredsLeft;
1903 SUnits[i].WeakSuccsLeft = SUnitsLinksBackup[i].WeakSuccsLeft;
1904 SUnits[i].NumSuccsLeft = SUnitsLinksBackup[i].NumSuccsLeft;
1909 template<
typename _Iterator>
void 1911 unsigned &VgprUsage,
unsigned &SgprUsage) {
1914 for (_Iterator RegI = First; RegI != End; ++RegI) {
1915 unsigned Reg = *RegI;
1920 for (; PSetI.
isValid(); ++PSetI) {
1921 if (*PSetI == VGPRSetID)
1923 else if (*PSetI == SGPRSetID)
1949 SUnitsLinksBackup =
SUnits;
1958 for (
unsigned i = 0, e = (
unsigned)SUnits.size(); i != e; ++i) {
1959 SUnit *SU = &SUnits[i];
1960 unsigned BaseLatReg;
1990 for (std::pair<SISchedulerBlockCreatorVariant, SISchedulerBlockSchedulerVariant> v : Variants) {
2011 for (std::pair<SISchedulerBlockCreatorVariant, SISchedulerBlockSchedulerVariant> v : Variants) {
2018 ScheduledSUnits = Best.
SUs;
2019 ScheduledSUnitsInv.resize(SUnits.size());
2021 for (
unsigned i = 0, e = (
unsigned)SUnits.size(); i != e; ++i) {
2022 ScheduledSUnitsInv[ScheduledSUnits[i]] = i;
2032 for (std::vector<unsigned>::iterator
I = ScheduledSUnits.begin(),
2033 E = ScheduledSUnits.end();
I !=
E; ++
I) {
2047 dbgs() <<
"*** Final schedule for "
Interface definition for SIRegisterInfo.
virtual unsigned getNumRegPressureSets() const =0
Get the number of dimensions of register pressure.
std::vector< SIScheduleBlock * > Blocks
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
SIScheduleDAGMI(MachineSchedContext *C)
void dumpSchedule() const
dump the scheduled Sequence.
Compute iterated dominance frontiers using a linear time algorithm.
ScheduleDAGTopologicalSort Topo
Topo - A topological ordering for SUnits which permits fast IsReachable and similar queries...
MachineBasicBlock::iterator CurrentTop
The top of the unscheduled zone.
SIScheduleCandReason Reason
void dump(const ScheduleDAG *G) const
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
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::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
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.
bool getMemOpBaseRegImmOfs(MachineInstr &LdSt, unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI) const final
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.
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.
GenericScheduler shrinks the unscheduled zone using heuristics to balance the schedule.
bool isDebugValue() const
std::vector< unsigned > IsHighLatencySU
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)
SmallVector< SDep, 4 > Succs
All sunit successors.
~SIScheduleBlockCreator()
static const Function * getParent(const Value *V)
bool isWeak() const
Tests if this a weak dependence.
Machine Instruction Scheduler
void dumpAll(const ScheduleDAG *G) const
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.