18010b631SJonas Paulsson //-- SystemZMachineScheduler.cpp - SystemZ Scheduler Interface -*- C++ -*---==// 28010b631SJonas Paulsson // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68010b631SJonas Paulsson // 78010b631SJonas Paulsson //===----------------------------------------------------------------------===// 88010b631SJonas Paulsson // 98010b631SJonas Paulsson // -------------------------- Post RA scheduling ---------------------------- // 108010b631SJonas Paulsson // SystemZPostRASchedStrategy is a scheduling strategy which is plugged into 118010b631SJonas Paulsson // the MachineScheduler. It has a sorted Available set of SUs and a pickNode() 128010b631SJonas Paulsson // implementation that looks to optimize decoder grouping and balance the 1357a705d9SJonas Paulsson // usage of processor resources. Scheduler states are saved for the end 1457a705d9SJonas Paulsson // region of each MBB, so that a successor block can learn from it. 158010b631SJonas Paulsson //===----------------------------------------------------------------------===// 168010b631SJonas Paulsson 178010b631SJonas Paulsson #include "SystemZMachineScheduler.h" 188010b631SJonas Paulsson 198010b631SJonas Paulsson using namespace llvm; 208010b631SJonas Paulsson 210cd23f56SEvandro Menezes #define DEBUG_TYPE "machine-scheduler" 228010b631SJonas Paulsson 238010b631SJonas Paulsson #ifndef NDEBUG 248010b631SJonas Paulsson // Print the set of SUs 258010b631SJonas Paulsson void SystemZPostRASchedStrategy::SUSet:: 266da25f4fSRafael Espindola dump(SystemZHazardRecognizer &HazardRec) const { 278010b631SJonas Paulsson dbgs() << "{"; 288010b631SJonas Paulsson for (auto &SU : *this) { 298010b631SJonas Paulsson HazardRec.dumpSU(SU, dbgs()); 308010b631SJonas Paulsson if (SU != *rbegin()) 318010b631SJonas Paulsson dbgs() << ", "; 328010b631SJonas Paulsson } 338010b631SJonas Paulsson dbgs() << "}\n"; 348010b631SJonas Paulsson } 358010b631SJonas Paulsson #endif 368010b631SJonas Paulsson 3757a705d9SJonas Paulsson // Try to find a single predecessor that would be interesting for the 3857a705d9SJonas Paulsson // scheduler in the top-most region of MBB. 3957a705d9SJonas Paulsson static MachineBasicBlock *getSingleSchedPred(MachineBasicBlock *MBB, 4057a705d9SJonas Paulsson const MachineLoop *Loop) { 4157a705d9SJonas Paulsson MachineBasicBlock *PredMBB = nullptr; 4257a705d9SJonas Paulsson if (MBB->pred_size() == 1) 4357a705d9SJonas Paulsson PredMBB = *MBB->pred_begin(); 4457a705d9SJonas Paulsson 4557a705d9SJonas Paulsson // The loop header has two predecessors, return the latch, but not for a 4657a705d9SJonas Paulsson // single block loop. 4757a705d9SJonas Paulsson if (MBB->pred_size() == 2 && Loop != nullptr && Loop->getHeader() == MBB) { 4857a705d9SJonas Paulsson for (auto I = MBB->pred_begin(); I != MBB->pred_end(); ++I) 4957a705d9SJonas Paulsson if (Loop->contains(*I)) 5057a705d9SJonas Paulsson PredMBB = (*I == MBB ? nullptr : *I); 5157a705d9SJonas Paulsson } 5257a705d9SJonas Paulsson 5357a705d9SJonas Paulsson assert ((PredMBB == nullptr || !Loop || Loop->contains(PredMBB)) 5457a705d9SJonas Paulsson && "Loop MBB should not consider predecessor outside of loop."); 5557a705d9SJonas Paulsson 5657a705d9SJonas Paulsson return PredMBB; 5757a705d9SJonas Paulsson } 5857a705d9SJonas Paulsson 5957a705d9SJonas Paulsson void SystemZPostRASchedStrategy:: 6057a705d9SJonas Paulsson advanceTo(MachineBasicBlock::iterator NextBegin) { 6157a705d9SJonas Paulsson MachineBasicBlock::iterator LastEmittedMI = HazardRec->getLastEmittedMI(); 6257a705d9SJonas Paulsson MachineBasicBlock::iterator I = 6357a705d9SJonas Paulsson ((LastEmittedMI != nullptr && LastEmittedMI->getParent() == MBB) ? 6457a705d9SJonas Paulsson std::next(LastEmittedMI) : MBB->begin()); 6557a705d9SJonas Paulsson 6657a705d9SJonas Paulsson for (; I != NextBegin; ++I) { 67801bf7ebSShiva Chen if (I->isPosition() || I->isDebugInstr()) 6857a705d9SJonas Paulsson continue; 6957a705d9SJonas Paulsson HazardRec->emitInstruction(&*I); 7057a705d9SJonas Paulsson } 7157a705d9SJonas Paulsson } 7257a705d9SJonas Paulsson 7361fbcf58SJonas Paulsson void SystemZPostRASchedStrategy::initialize(ScheduleDAGMI *dag) { 74d34e60caSNicola Zaghen LLVM_DEBUG(HazardRec->dumpState();); 7561fbcf58SJonas Paulsson } 7661fbcf58SJonas Paulsson 7757a705d9SJonas Paulsson void SystemZPostRASchedStrategy::enterMBB(MachineBasicBlock *NextMBB) { 7857a705d9SJonas Paulsson assert ((SchedStates.find(NextMBB) == SchedStates.end()) && 7957a705d9SJonas Paulsson "Entering MBB twice?"); 80d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Entering " << printMBBReference(*NextMBB)); 8157a705d9SJonas Paulsson 8257a705d9SJonas Paulsson MBB = NextMBB; 8361fbcf58SJonas Paulsson 8457a705d9SJonas Paulsson /// Create a HazardRec for MBB, save it in SchedStates and set HazardRec to 8557a705d9SJonas Paulsson /// point to it. 8657a705d9SJonas Paulsson HazardRec = SchedStates[MBB] = new SystemZHazardRecognizer(TII, &SchedModel); 87d34e60caSNicola Zaghen LLVM_DEBUG(const MachineLoop *Loop = MLI->getLoopFor(MBB); 88d34e60caSNicola Zaghen if (Loop && Loop->getHeader() == MBB) dbgs() << " (Loop header)"; 8957a705d9SJonas Paulsson dbgs() << ":\n";); 9057a705d9SJonas Paulsson 9157a705d9SJonas Paulsson // Try to take over the state from a single predecessor, if it has been 9257a705d9SJonas Paulsson // scheduled. If this is not possible, we are done. 9357a705d9SJonas Paulsson MachineBasicBlock *SinglePredMBB = 9457a705d9SJonas Paulsson getSingleSchedPred(MBB, MLI->getLoopFor(MBB)); 9557a705d9SJonas Paulsson if (SinglePredMBB == nullptr || 9657a705d9SJonas Paulsson SchedStates.find(SinglePredMBB) == SchedStates.end()) 9757a705d9SJonas Paulsson return; 9857a705d9SJonas Paulsson 99d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Continued scheduling from " 10025528d6dSFrancis Visoiu Mistrih << printMBBReference(*SinglePredMBB) << "\n";); 10157a705d9SJonas Paulsson 10257a705d9SJonas Paulsson HazardRec->copyState(SchedStates[SinglePredMBB]); 103d34e60caSNicola Zaghen LLVM_DEBUG(HazardRec->dumpState();); 10457a705d9SJonas Paulsson 10557a705d9SJonas Paulsson // Emit incoming terminator(s). Be optimistic and assume that branch 10657a705d9SJonas Paulsson // prediction will generally do "the right thing". 10757a705d9SJonas Paulsson for (MachineBasicBlock::iterator I = SinglePredMBB->getFirstTerminator(); 10857a705d9SJonas Paulsson I != SinglePredMBB->end(); I++) { 109d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Emitting incoming branch: "; I->dump();); 11057a705d9SJonas Paulsson bool TakenBranch = (I->isBranch() && 11157a705d9SJonas Paulsson (TII->getBranchInfo(*I).Target->isReg() || // Relative branch 11257a705d9SJonas Paulsson TII->getBranchInfo(*I).Target->getMBB() == MBB)); 11357a705d9SJonas Paulsson HazardRec->emitInstruction(&*I, TakenBranch); 11457a705d9SJonas Paulsson if (TakenBranch) 11557a705d9SJonas Paulsson break; 11657a705d9SJonas Paulsson } 11757a705d9SJonas Paulsson } 11857a705d9SJonas Paulsson 11957a705d9SJonas Paulsson void SystemZPostRASchedStrategy::leaveMBB() { 120d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Leaving " << printMBBReference(*MBB) << "\n";); 12157a705d9SJonas Paulsson 12257a705d9SJonas Paulsson // Advance to first terminator. The successor block will handle terminators 12357a705d9SJonas Paulsson // dependent on CFG layout (T/NT branch etc). 12457a705d9SJonas Paulsson advanceTo(MBB->getFirstTerminator()); 12557a705d9SJonas Paulsson } 12657a705d9SJonas Paulsson 1278010b631SJonas Paulsson SystemZPostRASchedStrategy:: 1288010b631SJonas Paulsson SystemZPostRASchedStrategy(const MachineSchedContext *C) 12957a705d9SJonas Paulsson : MLI(C->MLI), 13057a705d9SJonas Paulsson TII(static_cast<const SystemZInstrInfo *> 13157a705d9SJonas Paulsson (C->MF->getSubtarget().getInstrInfo())), 13257a705d9SJonas Paulsson MBB(nullptr), HazardRec(nullptr) { 13357a705d9SJonas Paulsson const TargetSubtargetInfo *ST = &C->MF->getSubtarget(); 1340d7df36cSSanjay Patel SchedModel.init(ST); 13557a705d9SJonas Paulsson } 1368010b631SJonas Paulsson 13757a705d9SJonas Paulsson SystemZPostRASchedStrategy::~SystemZPostRASchedStrategy() { 13857a705d9SJonas Paulsson // Delete hazard recognizers kept around for each MBB. 13957a705d9SJonas Paulsson for (auto I : SchedStates) { 14057a705d9SJonas Paulsson SystemZHazardRecognizer *hazrec = I.second; 14157a705d9SJonas Paulsson delete hazrec; 14257a705d9SJonas Paulsson } 14357a705d9SJonas Paulsson } 14457a705d9SJonas Paulsson 14557a705d9SJonas Paulsson void SystemZPostRASchedStrategy::initPolicy(MachineBasicBlock::iterator Begin, 14657a705d9SJonas Paulsson MachineBasicBlock::iterator End, 14757a705d9SJonas Paulsson unsigned NumRegionInstrs) { 14857a705d9SJonas Paulsson // Don't emit the terminators. 14957a705d9SJonas Paulsson if (Begin->isTerminator()) 15057a705d9SJonas Paulsson return; 15157a705d9SJonas Paulsson 15257a705d9SJonas Paulsson // Emit any instructions before start of region. 15357a705d9SJonas Paulsson advanceTo(Begin); 1548010b631SJonas Paulsson } 1558010b631SJonas Paulsson 1568010b631SJonas Paulsson // Pick the next node to schedule. 1578010b631SJonas Paulsson SUnit *SystemZPostRASchedStrategy::pickNode(bool &IsTopNode) { 1588010b631SJonas Paulsson // Only scheduling top-down. 1598010b631SJonas Paulsson IsTopNode = true; 1608010b631SJonas Paulsson 1618010b631SJonas Paulsson if (Available.empty()) 1628010b631SJonas Paulsson return nullptr; 1638010b631SJonas Paulsson 1648010b631SJonas Paulsson // If only one choice, return it. 1658010b631SJonas Paulsson if (Available.size() == 1) { 166d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Only one: "; 16757a705d9SJonas Paulsson HazardRec->dumpSU(*Available.begin(), dbgs()); dbgs() << "\n";); 1688010b631SJonas Paulsson return *Available.begin(); 1698010b631SJonas Paulsson } 1708010b631SJonas Paulsson 1712f12e45dSJonas Paulsson // All nodes that are possible to schedule are stored in the Available set. 172d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Available: "; Available.dump(*HazardRec);); 1738010b631SJonas Paulsson 1748010b631SJonas Paulsson Candidate Best; 1758010b631SJonas Paulsson for (auto *SU : Available) { 1768010b631SJonas Paulsson 1778010b631SJonas Paulsson // SU is the next candidate to be compared against current Best. 17857a705d9SJonas Paulsson Candidate c(SU, *HazardRec); 1798010b631SJonas Paulsson 1808010b631SJonas Paulsson // Remeber which SU is the best candidate. 1818010b631SJonas Paulsson if (Best.SU == nullptr || c < Best) { 1828010b631SJonas Paulsson Best = c; 183d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Best so far: ";); 18461fbcf58SJonas Paulsson } else 185d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Tried : ";); 186d34e60caSNicola Zaghen LLVM_DEBUG(HazardRec->dumpSU(c.SU, dbgs()); c.dumpCosts(); 187d34e60caSNicola Zaghen dbgs() << " Height:" << c.SU->getHeight(); dbgs() << "\n";); 1888010b631SJonas Paulsson 1898010b631SJonas Paulsson // Once we know we have seen all SUs that affect grouping or use unbuffered 1908010b631SJonas Paulsson // resources, we can stop iterating if Best looks good. 1918010b631SJonas Paulsson if (!SU->isScheduleHigh && Best.noCost()) 1928010b631SJonas Paulsson break; 1938010b631SJonas Paulsson } 1948010b631SJonas Paulsson 1958010b631SJonas Paulsson assert (Best.SU != nullptr); 1968010b631SJonas Paulsson return Best.SU; 1978010b631SJonas Paulsson } 1988010b631SJonas Paulsson 1998010b631SJonas Paulsson SystemZPostRASchedStrategy::Candidate:: 2008010b631SJonas Paulsson Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec) : Candidate() { 2018010b631SJonas Paulsson SU = SU_; 2028010b631SJonas Paulsson 2038010b631SJonas Paulsson // Check the grouping cost. For a node that must begin / end a 2048010b631SJonas Paulsson // group, it is positive if it would do so prematurely, or negative 2058010b631SJonas Paulsson // if it would fit naturally into the schedule. 2068010b631SJonas Paulsson GroupingCost = HazardRec.groupingCost(SU); 2078010b631SJonas Paulsson 2088010b631SJonas Paulsson // Check the resources cost for this SU. 2098010b631SJonas Paulsson ResourcesCost = HazardRec.resourcesCost(SU); 2108010b631SJonas Paulsson } 2118010b631SJonas Paulsson 2128010b631SJonas Paulsson bool SystemZPostRASchedStrategy::Candidate:: 2138010b631SJonas Paulsson operator<(const Candidate &other) { 2148010b631SJonas Paulsson 2158010b631SJonas Paulsson // Check decoder grouping. 2168010b631SJonas Paulsson if (GroupingCost < other.GroupingCost) 2178010b631SJonas Paulsson return true; 2188010b631SJonas Paulsson if (GroupingCost > other.GroupingCost) 2198010b631SJonas Paulsson return false; 2208010b631SJonas Paulsson 2218010b631SJonas Paulsson // Compare the use of resources. 2228010b631SJonas Paulsson if (ResourcesCost < other.ResourcesCost) 2238010b631SJonas Paulsson return true; 2248010b631SJonas Paulsson if (ResourcesCost > other.ResourcesCost) 2258010b631SJonas Paulsson return false; 2268010b631SJonas Paulsson 2278010b631SJonas Paulsson // Higher SU is otherwise generally better. 2288010b631SJonas Paulsson if (SU->getHeight() > other.SU->getHeight()) 2298010b631SJonas Paulsson return true; 2308010b631SJonas Paulsson if (SU->getHeight() < other.SU->getHeight()) 2318010b631SJonas Paulsson return false; 2328010b631SJonas Paulsson 2338010b631SJonas Paulsson // If all same, fall back to original order. 2348010b631SJonas Paulsson if (SU->NodeNum < other.SU->NodeNum) 2358010b631SJonas Paulsson return true; 2368010b631SJonas Paulsson 2378010b631SJonas Paulsson return false; 2388010b631SJonas Paulsson } 2398010b631SJonas Paulsson 2408010b631SJonas Paulsson void SystemZPostRASchedStrategy::schedNode(SUnit *SU, bool IsTopNode) { 241d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "** Scheduling SU(" << SU->NodeNum << ") "; 242d34e60caSNicola Zaghen if (Available.size() == 1) dbgs() << "(only one) "; 243d34e60caSNicola Zaghen Candidate c(SU, *HazardRec); c.dumpCosts(); dbgs() << "\n";); 2448010b631SJonas Paulsson 2458010b631SJonas Paulsson // Remove SU from Available set and update HazardRec. 2468010b631SJonas Paulsson Available.erase(SU); 24757a705d9SJonas Paulsson HazardRec->EmitInstruction(SU); 2488010b631SJonas Paulsson } 2498010b631SJonas Paulsson 2508010b631SJonas Paulsson void SystemZPostRASchedStrategy::releaseTopNode(SUnit *SU) { 2518010b631SJonas Paulsson // Set isScheduleHigh flag on all SUs that we want to consider first in 2528010b631SJonas Paulsson // pickNode(). 25357a705d9SJonas Paulsson const MCSchedClassDesc *SC = HazardRec->getSchedClass(SU); 2548010b631SJonas Paulsson bool AffectsGrouping = (SC->isValid() && (SC->BeginGroup || SC->EndGroup)); 2558010b631SJonas Paulsson SU->isScheduleHigh = (AffectsGrouping || SU->isUnbuffered); 2568010b631SJonas Paulsson 2578010b631SJonas Paulsson // Put all released SUs in the Available set. 2588010b631SJonas Paulsson Available.insert(SU); 2598010b631SJonas Paulsson } 260