Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 1 | //-- SystemZMachineScheduler.cpp - SystemZ Scheduler Interface -*- C++ -*---==// |
| 2 | // |
| 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 Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 14 | // 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 Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "SystemZMachineScheduler.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
Evandro Menezes | 0cd23f56 | 2017-07-11 22:08:28 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "machine-scheduler" |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 23 | |
| 24 | #ifndef NDEBUG |
| 25 | // Print the set of SUs |
| 26 | void SystemZPostRASchedStrategy::SUSet:: |
Rafael Espindola | 6da25f4 | 2017-06-21 23:02:57 +0000 | [diff] [blame] | 27 | dump(SystemZHazardRecognizer &HazardRec) const { |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 28 | dbgs() << "{"; |
| 29 | for (auto &SU : *this) { |
| 30 | HazardRec.dumpSU(SU, dbgs()); |
| 31 | if (SU != *rbegin()) |
| 32 | dbgs() << ", "; |
| 33 | } |
| 34 | dbgs() << "}\n"; |
| 35 | } |
| 36 | #endif |
| 37 | |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 38 | // Try to find a single predecessor that would be interesting for the |
| 39 | // scheduler in the top-most region of MBB. |
| 40 | static MachineBasicBlock *getSingleSchedPred(MachineBasicBlock *MBB, |
| 41 | const MachineLoop *Loop) { |
| 42 | MachineBasicBlock *PredMBB = nullptr; |
| 43 | if (MBB->pred_size() == 1) |
| 44 | PredMBB = *MBB->pred_begin(); |
| 45 | |
| 46 | // The loop header has two predecessors, return the latch, but not for a |
| 47 | // single block loop. |
| 48 | if (MBB->pred_size() == 2 && Loop != nullptr && Loop->getHeader() == MBB) { |
| 49 | for (auto I = MBB->pred_begin(); I != MBB->pred_end(); ++I) |
| 50 | if (Loop->contains(*I)) |
| 51 | PredMBB = (*I == MBB ? nullptr : *I); |
| 52 | } |
| 53 | |
| 54 | assert ((PredMBB == nullptr || !Loop || Loop->contains(PredMBB)) |
| 55 | && "Loop MBB should not consider predecessor outside of loop."); |
| 56 | |
| 57 | return PredMBB; |
| 58 | } |
| 59 | |
| 60 | void SystemZPostRASchedStrategy:: |
| 61 | advanceTo(MachineBasicBlock::iterator NextBegin) { |
| 62 | MachineBasicBlock::iterator LastEmittedMI = HazardRec->getLastEmittedMI(); |
| 63 | MachineBasicBlock::iterator I = |
| 64 | ((LastEmittedMI != nullptr && LastEmittedMI->getParent() == MBB) ? |
| 65 | std::next(LastEmittedMI) : MBB->begin()); |
| 66 | |
| 67 | for (; I != NextBegin; ++I) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 68 | if (I->isPosition() || I->isDebugInstr()) |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 69 | continue; |
| 70 | HazardRec->emitInstruction(&*I); |
| 71 | } |
| 72 | } |
| 73 | |
Jonas Paulsson | 61fbcf5 | 2018-03-07 08:39:00 +0000 | [diff] [blame] | 74 | void SystemZPostRASchedStrategy::initialize(ScheduleDAGMI *dag) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 75 | LLVM_DEBUG(HazardRec->dumpState();); |
Jonas Paulsson | 61fbcf5 | 2018-03-07 08:39:00 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 78 | void SystemZPostRASchedStrategy::enterMBB(MachineBasicBlock *NextMBB) { |
| 79 | assert ((SchedStates.find(NextMBB) == SchedStates.end()) && |
| 80 | "Entering MBB twice?"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 81 | LLVM_DEBUG(dbgs() << "** Entering " << printMBBReference(*NextMBB)); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 82 | |
| 83 | MBB = NextMBB; |
Jonas Paulsson | 61fbcf5 | 2018-03-07 08:39:00 +0000 | [diff] [blame] | 84 | |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 85 | /// Create a HazardRec for MBB, save it in SchedStates and set HazardRec to |
| 86 | /// point to it. |
| 87 | HazardRec = SchedStates[MBB] = new SystemZHazardRecognizer(TII, &SchedModel); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 88 | LLVM_DEBUG(const MachineLoop *Loop = MLI->getLoopFor(MBB); |
| 89 | if (Loop && Loop->getHeader() == MBB) dbgs() << " (Loop header)"; |
| 90 | dbgs() << ":\n";); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 91 | |
| 92 | // Try to take over the state from a single predecessor, if it has been |
| 93 | // scheduled. If this is not possible, we are done. |
| 94 | MachineBasicBlock *SinglePredMBB = |
| 95 | getSingleSchedPred(MBB, MLI->getLoopFor(MBB)); |
| 96 | if (SinglePredMBB == nullptr || |
| 97 | SchedStates.find(SinglePredMBB) == SchedStates.end()) |
| 98 | return; |
| 99 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 100 | LLVM_DEBUG(dbgs() << "** Continued scheduling from " |
| 101 | << printMBBReference(*SinglePredMBB) << "\n";); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 102 | |
| 103 | HazardRec->copyState(SchedStates[SinglePredMBB]); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 104 | LLVM_DEBUG(HazardRec->dumpState();); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 105 | |
| 106 | // Emit incoming terminator(s). Be optimistic and assume that branch |
| 107 | // prediction will generally do "the right thing". |
| 108 | for (MachineBasicBlock::iterator I = SinglePredMBB->getFirstTerminator(); |
| 109 | I != SinglePredMBB->end(); I++) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 110 | LLVM_DEBUG(dbgs() << "** Emitting incoming branch: "; I->dump();); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 111 | bool TakenBranch = (I->isBranch() && |
| 112 | (TII->getBranchInfo(*I).Target->isReg() || // Relative branch |
| 113 | TII->getBranchInfo(*I).Target->getMBB() == MBB)); |
| 114 | HazardRec->emitInstruction(&*I, TakenBranch); |
| 115 | if (TakenBranch) |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void SystemZPostRASchedStrategy::leaveMBB() { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 121 | LLVM_DEBUG(dbgs() << "** Leaving " << printMBBReference(*MBB) << "\n";); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 122 | |
| 123 | // Advance to first terminator. The successor block will handle terminators |
| 124 | // dependent on CFG layout (T/NT branch etc). |
| 125 | advanceTo(MBB->getFirstTerminator()); |
| 126 | } |
| 127 | |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 128 | SystemZPostRASchedStrategy:: |
| 129 | SystemZPostRASchedStrategy(const MachineSchedContext *C) |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 130 | : MLI(C->MLI), |
| 131 | TII(static_cast<const SystemZInstrInfo *> |
| 132 | (C->MF->getSubtarget().getInstrInfo())), |
| 133 | MBB(nullptr), HazardRec(nullptr) { |
| 134 | const TargetSubtargetInfo *ST = &C->MF->getSubtarget(); |
Sanjay Patel | 0d7df36 | 2018-04-08 19:56:04 +0000 | [diff] [blame] | 135 | SchedModel.init(ST); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 136 | } |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 137 | |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 138 | SystemZPostRASchedStrategy::~SystemZPostRASchedStrategy() { |
| 139 | // Delete hazard recognizers kept around for each MBB. |
| 140 | for (auto I : SchedStates) { |
| 141 | SystemZHazardRecognizer *hazrec = I.second; |
| 142 | delete hazrec; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void SystemZPostRASchedStrategy::initPolicy(MachineBasicBlock::iterator Begin, |
| 147 | MachineBasicBlock::iterator End, |
| 148 | unsigned NumRegionInstrs) { |
| 149 | // Don't emit the terminators. |
| 150 | if (Begin->isTerminator()) |
| 151 | return; |
| 152 | |
| 153 | // Emit any instructions before start of region. |
| 154 | advanceTo(Begin); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Pick the next node to schedule. |
| 158 | SUnit *SystemZPostRASchedStrategy::pickNode(bool &IsTopNode) { |
| 159 | // Only scheduling top-down. |
| 160 | IsTopNode = true; |
| 161 | |
| 162 | if (Available.empty()) |
| 163 | return nullptr; |
| 164 | |
| 165 | // If only one choice, return it. |
| 166 | if (Available.size() == 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 167 | LLVM_DEBUG(dbgs() << "** Only one: "; |
| 168 | HazardRec->dumpSU(*Available.begin(), dbgs()); dbgs() << "\n";); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 169 | return *Available.begin(); |
| 170 | } |
| 171 | |
| 172 | // All nodes that are possible to schedule are stored by in the |
| 173 | // Available set. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 174 | LLVM_DEBUG(dbgs() << "** Available: "; Available.dump(*HazardRec);); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 175 | |
| 176 | Candidate Best; |
| 177 | for (auto *SU : Available) { |
| 178 | |
| 179 | // SU is the next candidate to be compared against current Best. |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 180 | Candidate c(SU, *HazardRec); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 181 | |
| 182 | // Remeber which SU is the best candidate. |
| 183 | if (Best.SU == nullptr || c < Best) { |
| 184 | Best = c; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 185 | LLVM_DEBUG(dbgs() << "** Best so far: ";); |
Jonas Paulsson | 61fbcf5 | 2018-03-07 08:39:00 +0000 | [diff] [blame] | 186 | } else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 187 | LLVM_DEBUG(dbgs() << "** Tried : ";); |
| 188 | LLVM_DEBUG(HazardRec->dumpSU(c.SU, dbgs()); c.dumpCosts(); |
| 189 | dbgs() << " Height:" << c.SU->getHeight(); dbgs() << "\n";); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 190 | |
| 191 | // Once we know we have seen all SUs that affect grouping or use unbuffered |
| 192 | // resources, we can stop iterating if Best looks good. |
| 193 | if (!SU->isScheduleHigh && Best.noCost()) |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | assert (Best.SU != nullptr); |
| 198 | return Best.SU; |
| 199 | } |
| 200 | |
| 201 | SystemZPostRASchedStrategy::Candidate:: |
| 202 | Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec) : Candidate() { |
| 203 | SU = SU_; |
| 204 | |
| 205 | // Check the grouping cost. For a node that must begin / end a |
| 206 | // group, it is positive if it would do so prematurely, or negative |
| 207 | // if it would fit naturally into the schedule. |
| 208 | GroupingCost = HazardRec.groupingCost(SU); |
| 209 | |
Jonas Paulsson | 61fbcf5 | 2018-03-07 08:39:00 +0000 | [diff] [blame] | 210 | // Check the resources cost for this SU. |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 211 | ResourcesCost = HazardRec.resourcesCost(SU); |
| 212 | } |
| 213 | |
| 214 | bool SystemZPostRASchedStrategy::Candidate:: |
| 215 | operator<(const Candidate &other) { |
| 216 | |
| 217 | // Check decoder grouping. |
| 218 | if (GroupingCost < other.GroupingCost) |
| 219 | return true; |
| 220 | if (GroupingCost > other.GroupingCost) |
| 221 | return false; |
| 222 | |
| 223 | // Compare the use of resources. |
| 224 | if (ResourcesCost < other.ResourcesCost) |
| 225 | return true; |
| 226 | if (ResourcesCost > other.ResourcesCost) |
| 227 | return false; |
| 228 | |
| 229 | // Higher SU is otherwise generally better. |
| 230 | if (SU->getHeight() > other.SU->getHeight()) |
| 231 | return true; |
| 232 | if (SU->getHeight() < other.SU->getHeight()) |
| 233 | return false; |
| 234 | |
| 235 | // If all same, fall back to original order. |
| 236 | if (SU->NodeNum < other.SU->NodeNum) |
| 237 | return true; |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | void SystemZPostRASchedStrategy::schedNode(SUnit *SU, bool IsTopNode) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 243 | LLVM_DEBUG(dbgs() << "** Scheduling SU(" << SU->NodeNum << ") "; |
| 244 | if (Available.size() == 1) dbgs() << "(only one) "; |
| 245 | Candidate c(SU, *HazardRec); c.dumpCosts(); dbgs() << "\n";); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 246 | |
| 247 | // Remove SU from Available set and update HazardRec. |
| 248 | Available.erase(SU); |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 249 | HazardRec->EmitInstruction(SU); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void SystemZPostRASchedStrategy::releaseTopNode(SUnit *SU) { |
| 253 | // Set isScheduleHigh flag on all SUs that we want to consider first in |
| 254 | // pickNode(). |
Jonas Paulsson | 57a705d | 2017-08-17 08:33:44 +0000 | [diff] [blame] | 255 | const MCSchedClassDesc *SC = HazardRec->getSchedClass(SU); |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 256 | bool AffectsGrouping = (SC->isValid() && (SC->BeginGroup || SC->EndGroup)); |
| 257 | SU->isScheduleHigh = (AffectsGrouping || SU->isUnbuffered); |
| 258 | |
| 259 | // Put all released SUs in the Available set. |
| 260 | Available.insert(SU); |
| 261 | } |