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
14*57a705d9SJonas Paulsson // usage of processor resources. Scheduler states are saved for the end
15*57a705d9SJonas 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 {
32*57a705d9SJonas Paulsson 
33*57a705d9SJonas Paulsson   const MachineLoopInfo *MLI;
34*57a705d9SJonas Paulsson   const SystemZInstrInfo *TII;
35*57a705d9SJonas Paulsson 
36*57a705d9SJonas Paulsson   // A SchedModel is needed before any DAG is built while advancing past
37*57a705d9SJonas Paulsson   // non-scheduled instructions, so it would not always be possible to call
38*57a705d9SJonas Paulsson   // DAG->getSchedClass(SU).
39*57a705d9SJonas 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     }
618010b631SJonas Paulsson   };
628010b631SJonas Paulsson 
638010b631SJonas Paulsson   // A sorter for the Available set that makes sure that SUs are considered
648010b631SJonas Paulsson   // in the best order.
658010b631SJonas Paulsson   struct SUSorter {
668010b631SJonas Paulsson     bool operator() (SUnit *lhs, SUnit *rhs) const {
678010b631SJonas Paulsson       if (lhs->isScheduleHigh && !rhs->isScheduleHigh)
688010b631SJonas Paulsson         return true;
698010b631SJonas Paulsson       if (!lhs->isScheduleHigh && rhs->isScheduleHigh)
708010b631SJonas Paulsson         return false;
718010b631SJonas Paulsson 
728010b631SJonas Paulsson       if (lhs->getHeight() > rhs->getHeight())
738010b631SJonas Paulsson         return true;
748010b631SJonas Paulsson       else if (lhs->getHeight() < rhs->getHeight())
758010b631SJonas Paulsson         return false;
768010b631SJonas Paulsson 
778010b631SJonas Paulsson       return (lhs->NodeNum < rhs->NodeNum);
788010b631SJonas Paulsson     }
798010b631SJonas Paulsson   };
808010b631SJonas Paulsson   // A set of SUs with a sorter and dump method.
818010b631SJonas Paulsson   struct SUSet : std::set<SUnit*, SUSorter> {
828010b631SJonas Paulsson     #ifndef NDEBUG
83705f798bSSam Clegg     void dump(SystemZHazardRecognizer &HazardRec) const;
848010b631SJonas Paulsson     #endif
858010b631SJonas Paulsson   };
868010b631SJonas Paulsson 
878010b631SJonas Paulsson   /// The set of available SUs to schedule next.
888010b631SJonas Paulsson   SUSet Available;
898010b631SJonas Paulsson 
90*57a705d9SJonas Paulsson   /// Current MBB
91*57a705d9SJonas Paulsson   MachineBasicBlock *MBB;
92*57a705d9SJonas Paulsson 
93*57a705d9SJonas Paulsson   /// Maintain hazard recognizers for all blocks, so that the scheduler state
94*57a705d9SJonas Paulsson   /// can be maintained past BB boundaries when appropariate.
95*57a705d9SJonas Paulsson   typedef std::map<MachineBasicBlock*, SystemZHazardRecognizer*> MBB2HazRec;
96*57a705d9SJonas Paulsson   MBB2HazRec SchedStates;
97*57a705d9SJonas Paulsson 
98*57a705d9SJonas Paulsson   /// Pointer to the HazardRecognizer that tracks the scheduler state for
99*57a705d9SJonas Paulsson   /// the current region.
100*57a705d9SJonas Paulsson   SystemZHazardRecognizer *HazardRec;
101*57a705d9SJonas Paulsson 
102*57a705d9SJonas Paulsson   /// Update the scheduler state by emitting (non-scheduled) instructions
103*57a705d9SJonas Paulsson   /// up to, but not including, NextBegin.
104*57a705d9SJonas Paulsson   void advanceTo(MachineBasicBlock::iterator NextBegin);
1058010b631SJonas Paulsson 
1068010b631SJonas Paulsson public:
1078010b631SJonas Paulsson   SystemZPostRASchedStrategy(const MachineSchedContext *C);
108*57a705d9SJonas Paulsson   virtual ~SystemZPostRASchedStrategy();
109*57a705d9SJonas Paulsson 
110*57a705d9SJonas Paulsson   /// Called for a region before scheduling.
111*57a705d9SJonas Paulsson   void initPolicy(MachineBasicBlock::iterator Begin,
112*57a705d9SJonas Paulsson                   MachineBasicBlock::iterator End,
113*57a705d9SJonas Paulsson                   unsigned NumRegionInstrs) override;
1148010b631SJonas Paulsson 
1158010b631SJonas Paulsson   /// PostRA scheduling does not track pressure.
1168010b631SJonas Paulsson   bool shouldTrackPressure() const override { return false; }
1178010b631SJonas Paulsson 
118*57a705d9SJonas Paulsson   // Process scheduling regions top-down so that scheduler states can be
119*57a705d9SJonas Paulsson   // transferrred over scheduling boundaries.
120*57a705d9SJonas Paulsson   bool doMBBSchedRegionsTopDown() const override { return true; }
121*57a705d9SJonas Paulsson 
122*57a705d9SJonas Paulsson   void initialize(ScheduleDAGMI *dag) override {}
123*57a705d9SJonas Paulsson 
124*57a705d9SJonas Paulsson   /// Tell the strategy that MBB is about to be processed.
125*57a705d9SJonas Paulsson   void enterMBB(MachineBasicBlock *NextMBB) override;
126*57a705d9SJonas Paulsson 
127*57a705d9SJonas Paulsson   /// Tell the strategy that current MBB is done.
128*57a705d9SJonas Paulsson   void leaveMBB() override;
1298010b631SJonas Paulsson 
1308010b631SJonas Paulsson   /// Pick the next node to schedule, or return NULL.
1318010b631SJonas Paulsson   SUnit *pickNode(bool &IsTopNode) override;
1328010b631SJonas Paulsson 
1338010b631SJonas Paulsson   /// ScheduleDAGMI has scheduled an instruction - tell HazardRec
1348010b631SJonas Paulsson   /// about it.
1358010b631SJonas Paulsson   void schedNode(SUnit *SU, bool IsTopNode) override;
1368010b631SJonas Paulsson 
1378010b631SJonas Paulsson   /// SU has had all predecessor dependencies resolved. Put it into
1388010b631SJonas Paulsson   /// Available.
1398010b631SJonas Paulsson   void releaseTopNode(SUnit *SU) override;
1408010b631SJonas Paulsson 
1418010b631SJonas Paulsson   /// Currently only scheduling top-down, so this method is empty.
1428010b631SJonas Paulsson   void releaseBottomNode(SUnit *SU) override {};
1438010b631SJonas Paulsson };
1448010b631SJonas Paulsson 
1453943d2b0SEugene Zelenko } // end namespace llvm
1468010b631SJonas Paulsson 
1473943d2b0SEugene Zelenko #endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
148