blob: ab820e5d3e63d6b70f43034c5d9141382be79e7b [file] [log] [blame]
Eugene Zelenko3943d2b2017-01-24 22:10:43 +00001//==- SystemZMachineScheduler.h - SystemZ Scheduler Interface ----*- C++ -*-==//
Jonas Paulsson8010b632016-10-20 08:27:16 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// -------------------------- Post RA scheduling ---------------------------- //
11// SystemZPostRASchedStrategy is a scheduling strategy which is plugged into
12// the MachineScheduler. It has a sorted Available set of SUs and a pickNode()
13// implementation that looks to optimize decoder grouping and balance the
Jonas Paulsson57a705d2017-08-17 08:33:44 +000014// usage of processor resources. Scheduler states are saved for the end
15// region of each MBB, so that a successor block can learn from it.
Jonas Paulsson8010b632016-10-20 08:27:16 +000016//===----------------------------------------------------------------------===//
17
Jonas Paulsson8010b632016-10-20 08:27:16 +000018#include "SystemZHazardRecognizer.h"
19#include "llvm/CodeGen/MachineScheduler.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000020#include "llvm/CodeGen/ScheduleDAG.h"
21#include <set>
Jonas Paulsson8010b632016-10-20 08:27:16 +000022
23#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
24#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
25
26using namespace llvm;
27
28namespace llvm {
Fangrui Songf78650a2018-07-30 19:41:25 +000029
Jonas Paulsson8010b632016-10-20 08:27:16 +000030/// A MachineSchedStrategy implementation for SystemZ post RA scheduling.
31class SystemZPostRASchedStrategy : public MachineSchedStrategy {
Jonas Paulsson57a705d2017-08-17 08:33:44 +000032
33 const MachineLoopInfo *MLI;
34 const SystemZInstrInfo *TII;
35
36 // A SchedModel is needed before any DAG is built while advancing past
37 // non-scheduled instructions, so it would not always be possible to call
38 // DAG->getSchedClass(SU).
39 TargetSchedModel SchedModel;
Fangrui Songf78650a2018-07-30 19:41:25 +000040
Jonas Paulsson8010b632016-10-20 08:27:16 +000041 /// A candidate during instruction evaluation.
42 struct Candidate {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000043 SUnit *SU = nullptr;
Jonas Paulsson8010b632016-10-20 08:27:16 +000044
45 /// The decoding cost.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000046 int GroupingCost = 0;
Jonas Paulsson8010b632016-10-20 08:27:16 +000047
48 /// The processor resources cost.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000049 int ResourcesCost = 0;
Jonas Paulsson8010b632016-10-20 08:27:16 +000050
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000051 Candidate() = default;
Jonas Paulsson8010b632016-10-20 08:27:16 +000052 Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec);
53
54 // Compare two candidates.
55 bool operator<(const Candidate &other);
56
57 // Check if this node is free of cost ("as good as any").
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000058 bool noCost() const {
Jonas Paulsson8010b632016-10-20 08:27:16 +000059 return (GroupingCost <= 0 && !ResourcesCost);
60 }
Jonas Paulsson61fbcf52018-03-07 08:39:00 +000061
62#ifndef NDEBUG
63 void dumpCosts() {
64 if (GroupingCost != 0)
65 dbgs() << " Grouping cost:" << GroupingCost;
66 if (ResourcesCost != 0)
67 dbgs() << " Resource cost:" << ResourcesCost;
68 }
69#endif
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000070 };
Jonas Paulsson8010b632016-10-20 08:27:16 +000071
72 // A sorter for the Available set that makes sure that SUs are considered
73 // in the best order.
74 struct SUSorter {
75 bool operator() (SUnit *lhs, SUnit *rhs) const {
76 if (lhs->isScheduleHigh && !rhs->isScheduleHigh)
77 return true;
78 if (!lhs->isScheduleHigh && rhs->isScheduleHigh)
79 return false;
80
81 if (lhs->getHeight() > rhs->getHeight())
82 return true;
83 else if (lhs->getHeight() < rhs->getHeight())
84 return false;
85
86 return (lhs->NodeNum < rhs->NodeNum);
87 }
88 };
89 // A set of SUs with a sorter and dump method.
90 struct SUSet : std::set<SUnit*, SUSorter> {
91 #ifndef NDEBUG
Sam Clegg705f7982017-06-21 22:19:17 +000092 void dump(SystemZHazardRecognizer &HazardRec) const;
Jonas Paulsson8010b632016-10-20 08:27:16 +000093 #endif
94 };
95
96 /// The set of available SUs to schedule next.
97 SUSet Available;
98
Jonas Paulsson57a705d2017-08-17 08:33:44 +000099 /// Current MBB
100 MachineBasicBlock *MBB;
101
102 /// Maintain hazard recognizers for all blocks, so that the scheduler state
103 /// can be maintained past BB boundaries when appropariate.
104 typedef std::map<MachineBasicBlock*, SystemZHazardRecognizer*> MBB2HazRec;
105 MBB2HazRec SchedStates;
106
107 /// Pointer to the HazardRecognizer that tracks the scheduler state for
108 /// the current region.
109 SystemZHazardRecognizer *HazardRec;
110
111 /// Update the scheduler state by emitting (non-scheduled) instructions
112 /// up to, but not including, NextBegin.
113 void advanceTo(MachineBasicBlock::iterator NextBegin);
114
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000115public:
Jonas Paulsson8010b632016-10-20 08:27:16 +0000116 SystemZPostRASchedStrategy(const MachineSchedContext *C);
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000117 virtual ~SystemZPostRASchedStrategy();
118
119 /// Called for a region before scheduling.
120 void initPolicy(MachineBasicBlock::iterator Begin,
121 MachineBasicBlock::iterator End,
122 unsigned NumRegionInstrs) override;
Jonas Paulsson8010b632016-10-20 08:27:16 +0000123
124 /// PostRA scheduling does not track pressure.
125 bool shouldTrackPressure() const override { return false; }
126
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000127 // Process scheduling regions top-down so that scheduler states can be
128 // transferrred over scheduling boundaries.
129 bool doMBBSchedRegionsTopDown() const override { return true; }
130
Jonas Paulsson61fbcf52018-03-07 08:39:00 +0000131 void initialize(ScheduleDAGMI *dag) override;
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000132
133 /// Tell the strategy that MBB is about to be processed.
134 void enterMBB(MachineBasicBlock *NextMBB) override;
135
136 /// Tell the strategy that current MBB is done.
137 void leaveMBB() override;
Jonas Paulsson8010b632016-10-20 08:27:16 +0000138
139 /// Pick the next node to schedule, or return NULL.
140 SUnit *pickNode(bool &IsTopNode) override;
141
142 /// ScheduleDAGMI has scheduled an instruction - tell HazardRec
143 /// about it.
144 void schedNode(SUnit *SU, bool IsTopNode) override;
145
146 /// SU has had all predecessor dependencies resolved. Put it into
147 /// Available.
148 void releaseTopNode(SUnit *SU) override;
149
150 /// Currently only scheduling top-down, so this method is empty.
151 void releaseBottomNode(SUnit *SU) override {};
152};
153
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000154} // end namespace llvm
Jonas Paulsson8010b632016-10-20 08:27:16 +0000155
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000156#endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H