13943d2b0SEugene Zelenko //==- SystemZMachineScheduler.h - SystemZ Scheduler Interface ----*- C++ -*-==// 28010b631SJonas Paulsson // 38010b631SJonas Paulsson // The LLVM Compiler Infrastructure 48010b631SJonas Paulsson // 58010b631SJonas Paulsson // This file is distributed under the University of Illinois Open Source 68010b631SJonas Paulsson // License. See LICENSE.TXT for details. 78010b631SJonas Paulsson // 88010b631SJonas Paulsson //===----------------------------------------------------------------------===// 98010b631SJonas Paulsson // 108010b631SJonas Paulsson // -------------------------- Post RA scheduling ---------------------------- // 118010b631SJonas Paulsson // SystemZPostRASchedStrategy is a scheduling strategy which is plugged into 128010b631SJonas Paulsson // the MachineScheduler. It has a sorted Available set of SUs and a pickNode() 138010b631SJonas Paulsson // implementation that looks to optimize decoder grouping and balance the 1457a705d9SJonas Paulsson // usage of processor resources. Scheduler states are saved for the end 1557a705d9SJonas Paulsson // region of each MBB, so that a successor block can learn from it. 168010b631SJonas Paulsson //===----------------------------------------------------------------------===// 178010b631SJonas Paulsson 188010b631SJonas Paulsson #include "SystemZHazardRecognizer.h" 198010b631SJonas Paulsson #include "llvm/CodeGen/MachineScheduler.h" 203943d2b0SEugene Zelenko #include "llvm/CodeGen/ScheduleDAG.h" 213943d2b0SEugene Zelenko #include <set> 228010b631SJonas Paulsson 238010b631SJonas Paulsson #ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H 248010b631SJonas Paulsson #define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H 258010b631SJonas Paulsson 268010b631SJonas Paulsson using namespace llvm; 278010b631SJonas Paulsson 288010b631SJonas Paulsson namespace llvm { 298010b631SJonas Paulsson 308010b631SJonas Paulsson /// A MachineSchedStrategy implementation for SystemZ post RA scheduling. 318010b631SJonas Paulsson class SystemZPostRASchedStrategy : public MachineSchedStrategy { 3257a705d9SJonas Paulsson 3357a705d9SJonas Paulsson const MachineLoopInfo *MLI; 3457a705d9SJonas Paulsson const SystemZInstrInfo *TII; 3557a705d9SJonas Paulsson 3657a705d9SJonas Paulsson // A SchedModel is needed before any DAG is built while advancing past 3757a705d9SJonas Paulsson // non-scheduled instructions, so it would not always be possible to call 3857a705d9SJonas Paulsson // DAG->getSchedClass(SU). 3957a705d9SJonas Paulsson TargetSchedModel SchedModel; 408010b631SJonas Paulsson 418010b631SJonas Paulsson /// A candidate during instruction evaluation. 428010b631SJonas Paulsson struct Candidate { 433943d2b0SEugene Zelenko SUnit *SU = nullptr; 448010b631SJonas Paulsson 458010b631SJonas Paulsson /// The decoding cost. 463943d2b0SEugene Zelenko int GroupingCost = 0; 478010b631SJonas Paulsson 488010b631SJonas Paulsson /// The processor resources cost. 493943d2b0SEugene Zelenko int ResourcesCost = 0; 508010b631SJonas Paulsson 513943d2b0SEugene Zelenko Candidate() = default; 528010b631SJonas Paulsson Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec); 538010b631SJonas Paulsson 548010b631SJonas Paulsson // Compare two candidates. 558010b631SJonas Paulsson bool operator<(const Candidate &other); 568010b631SJonas Paulsson 578010b631SJonas Paulsson // Check if this node is free of cost ("as good as any"). 583943d2b0SEugene Zelenko bool noCost() const { 598010b631SJonas Paulsson return (GroupingCost <= 0 && !ResourcesCost); 608010b631SJonas Paulsson } 61*61fbcf58SJonas Paulsson 62*61fbcf58SJonas Paulsson #ifndef NDEBUG 63*61fbcf58SJonas Paulsson void dumpCosts() { 64*61fbcf58SJonas Paulsson if (GroupingCost != 0) 65*61fbcf58SJonas Paulsson dbgs() << " Grouping cost:" << GroupingCost; 66*61fbcf58SJonas Paulsson if (ResourcesCost != 0) 67*61fbcf58SJonas Paulsson dbgs() << " Resource cost:" << ResourcesCost; 68*61fbcf58SJonas Paulsson } 69*61fbcf58SJonas Paulsson #endif 708010b631SJonas Paulsson }; 718010b631SJonas Paulsson 728010b631SJonas Paulsson // A sorter for the Available set that makes sure that SUs are considered 738010b631SJonas Paulsson // in the best order. 748010b631SJonas Paulsson struct SUSorter { 758010b631SJonas Paulsson bool operator() (SUnit *lhs, SUnit *rhs) const { 768010b631SJonas Paulsson if (lhs->isScheduleHigh && !rhs->isScheduleHigh) 778010b631SJonas Paulsson return true; 788010b631SJonas Paulsson if (!lhs->isScheduleHigh && rhs->isScheduleHigh) 798010b631SJonas Paulsson return false; 808010b631SJonas Paulsson 818010b631SJonas Paulsson if (lhs->getHeight() > rhs->getHeight()) 828010b631SJonas Paulsson return true; 838010b631SJonas Paulsson else if (lhs->getHeight() < rhs->getHeight()) 848010b631SJonas Paulsson return false; 858010b631SJonas Paulsson 868010b631SJonas Paulsson return (lhs->NodeNum < rhs->NodeNum); 878010b631SJonas Paulsson } 888010b631SJonas Paulsson }; 898010b631SJonas Paulsson // A set of SUs with a sorter and dump method. 908010b631SJonas Paulsson struct SUSet : std::set<SUnit*, SUSorter> { 918010b631SJonas Paulsson #ifndef NDEBUG 92705f798bSSam Clegg void dump(SystemZHazardRecognizer &HazardRec) const; 938010b631SJonas Paulsson #endif 948010b631SJonas Paulsson }; 958010b631SJonas Paulsson 968010b631SJonas Paulsson /// The set of available SUs to schedule next. 978010b631SJonas Paulsson SUSet Available; 988010b631SJonas Paulsson 9957a705d9SJonas Paulsson /// Current MBB 10057a705d9SJonas Paulsson MachineBasicBlock *MBB; 10157a705d9SJonas Paulsson 10257a705d9SJonas Paulsson /// Maintain hazard recognizers for all blocks, so that the scheduler state 10357a705d9SJonas Paulsson /// can be maintained past BB boundaries when appropariate. 10457a705d9SJonas Paulsson typedef std::map<MachineBasicBlock*, SystemZHazardRecognizer*> MBB2HazRec; 10557a705d9SJonas Paulsson MBB2HazRec SchedStates; 10657a705d9SJonas Paulsson 10757a705d9SJonas Paulsson /// Pointer to the HazardRecognizer that tracks the scheduler state for 10857a705d9SJonas Paulsson /// the current region. 10957a705d9SJonas Paulsson SystemZHazardRecognizer *HazardRec; 11057a705d9SJonas Paulsson 11157a705d9SJonas Paulsson /// Update the scheduler state by emitting (non-scheduled) instructions 11257a705d9SJonas Paulsson /// up to, but not including, NextBegin. 11357a705d9SJonas Paulsson void advanceTo(MachineBasicBlock::iterator NextBegin); 1148010b631SJonas Paulsson 1158010b631SJonas Paulsson public: 1168010b631SJonas Paulsson SystemZPostRASchedStrategy(const MachineSchedContext *C); 11757a705d9SJonas Paulsson virtual ~SystemZPostRASchedStrategy(); 11857a705d9SJonas Paulsson 11957a705d9SJonas Paulsson /// Called for a region before scheduling. 12057a705d9SJonas Paulsson void initPolicy(MachineBasicBlock::iterator Begin, 12157a705d9SJonas Paulsson MachineBasicBlock::iterator End, 12257a705d9SJonas Paulsson unsigned NumRegionInstrs) override; 1238010b631SJonas Paulsson 1248010b631SJonas Paulsson /// PostRA scheduling does not track pressure. 1258010b631SJonas Paulsson bool shouldTrackPressure() const override { return false; } 1268010b631SJonas Paulsson 12757a705d9SJonas Paulsson // Process scheduling regions top-down so that scheduler states can be 12857a705d9SJonas Paulsson // transferrred over scheduling boundaries. 12957a705d9SJonas Paulsson bool doMBBSchedRegionsTopDown() const override { return true; } 13057a705d9SJonas Paulsson 131*61fbcf58SJonas Paulsson void initialize(ScheduleDAGMI *dag) override; 13257a705d9SJonas Paulsson 13357a705d9SJonas Paulsson /// Tell the strategy that MBB is about to be processed. 13457a705d9SJonas Paulsson void enterMBB(MachineBasicBlock *NextMBB) override; 13557a705d9SJonas Paulsson 13657a705d9SJonas Paulsson /// Tell the strategy that current MBB is done. 13757a705d9SJonas Paulsson void leaveMBB() override; 1388010b631SJonas Paulsson 1398010b631SJonas Paulsson /// Pick the next node to schedule, or return NULL. 1408010b631SJonas Paulsson SUnit *pickNode(bool &IsTopNode) override; 1418010b631SJonas Paulsson 1428010b631SJonas Paulsson /// ScheduleDAGMI has scheduled an instruction - tell HazardRec 1438010b631SJonas Paulsson /// about it. 1448010b631SJonas Paulsson void schedNode(SUnit *SU, bool IsTopNode) override; 1458010b631SJonas Paulsson 1468010b631SJonas Paulsson /// SU has had all predecessor dependencies resolved. Put it into 1478010b631SJonas Paulsson /// Available. 1488010b631SJonas Paulsson void releaseTopNode(SUnit *SU) override; 1498010b631SJonas Paulsson 1508010b631SJonas Paulsson /// Currently only scheduling top-down, so this method is empty. 1518010b631SJonas Paulsson void releaseBottomNode(SUnit *SU) override {}; 1528010b631SJonas Paulsson }; 1538010b631SJonas Paulsson 1543943d2b0SEugene Zelenko } // end namespace llvm 1558010b631SJonas Paulsson 1563943d2b0SEugene Zelenko #endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H 157