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