| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#ifndef LLVM_CODEGEN_MACHINESCHEDULER_H |
| 75 |
#define LLVM_CODEGEN_MACHINESCHEDULER_H |
| 76 |
|
| 77 |
#include "llvm/ADT/APInt.h" |
| 78 |
#include "llvm/ADT/ArrayRef.h" |
| 79 |
#include "llvm/ADT/BitVector.h" |
| 80 |
#include "llvm/ADT/STLExtras.h" |
| 81 |
#include "llvm/ADT/SmallVector.h" |
| 82 |
#include "llvm/ADT/StringRef.h" |
| 83 |
#include "llvm/ADT/Twine.h" |
| 84 |
#include "llvm/CodeGen/MachineBasicBlock.h" |
| 85 |
#include "llvm/CodeGen/MachinePassRegistry.h" |
| 86 |
#include "llvm/CodeGen/RegisterPressure.h" |
| 87 |
#include "llvm/CodeGen/ScheduleDAG.h" |
| 88 |
#include "llvm/CodeGen/ScheduleDAGInstrs.h" |
| 89 |
#include "llvm/CodeGen/ScheduleDAGMutation.h" |
| 90 |
#include "llvm/CodeGen/TargetSchedule.h" |
| 91 |
#include "llvm/Support/CommandLine.h" |
| 92 |
#include "llvm/Support/ErrorHandling.h" |
| 93 |
#include <algorithm> |
| 94 |
#include <cassert> |
| 95 |
#include <memory> |
| 96 |
#include <string> |
| 97 |
#include <vector> |
| 98 |
|
| 99 |
namespace llvm { |
| 100 |
|
| 101 |
extern cl::opt<bool> ForceTopDown; |
| 102 |
extern cl::opt<bool> ForceBottomUp; |
| 103 |
extern cl::opt<bool> VerifyScheduling; |
| 104 |
#ifndef NDEBUG |
| 105 |
extern cl::opt<bool> ViewMISchedDAGs; |
| 106 |
#else |
| 107 |
extern const bool ViewMISchedDAGs; |
| 108 |
#endif |
| 109 |
|
| 110 |
class AAResults; |
| 111 |
class LiveIntervals; |
| 112 |
class MachineDominatorTree; |
| 113 |
class MachineFunction; |
| 114 |
class MachineInstr; |
| 115 |
class MachineLoopInfo; |
| 116 |
class RegisterClassInfo; |
| 117 |
class SchedDFSResult; |
| 118 |
class ScheduleHazardRecognizer; |
| 119 |
class TargetInstrInfo; |
| 120 |
class TargetPassConfig; |
| 121 |
class TargetRegisterInfo; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
struct MachineSchedContext { |
| 126 |
MachineFunction *MF = nullptr; |
| 127 |
const MachineLoopInfo *MLI = nullptr; |
| 128 |
const MachineDominatorTree *MDT = nullptr; |
| 129 |
const TargetPassConfig *PassConfig = nullptr; |
| 130 |
AAResults *AA = nullptr; |
| 131 |
LiveIntervals *LIS = nullptr; |
| 132 |
|
| 133 |
RegisterClassInfo *RegClassInfo; |
| 134 |
|
| 135 |
MachineSchedContext(); |
| 136 |
virtual ~MachineSchedContext(); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class MachineSchedRegistry |
| 142 |
: public MachinePassRegistryNode< |
| 143 |
ScheduleDAGInstrs *(*)(MachineSchedContext *)> { |
| 144 |
public: |
| 145 |
using ScheduleDAGCtor = ScheduleDAGInstrs *(*)(MachineSchedContext *); |
| 146 |
|
| 147 |
|
| 148 |
using FunctionPassCtor = ScheduleDAGCtor; |
| 149 |
|
| 150 |
static MachinePassRegistry<ScheduleDAGCtor> Registry; |
| 151 |
|
| 152 |
MachineSchedRegistry(const char *N, const char *D, ScheduleDAGCtor C) |
| 153 |
: MachinePassRegistryNode(N, D, C) { |
| 154 |
Registry.Add(this); |
| 155 |
} |
| 156 |
|
| 157 |
~MachineSchedRegistry() { Registry.Remove(this); } |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
MachineSchedRegistry *getNext() const { |
| 162 |
return (MachineSchedRegistry *)MachinePassRegistryNode::getNext(); |
| 163 |
} |
| 164 |
|
| 165 |
static MachineSchedRegistry *getList() { |
| 166 |
return (MachineSchedRegistry *)Registry.getList(); |
| 167 |
} |
| 168 |
|
| 169 |
static void setListener(MachinePassRegistryListener<FunctionPassCtor> *L) { |
| 170 |
Registry.setListener(L); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
class ScheduleDAGMI; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
struct MachineSchedPolicy { |
| 180 |
|
| 181 |
bool ShouldTrackPressure = false; |
| 182 |
|
| 183 |
|
| 184 |
bool ShouldTrackLaneMasks = false; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool OnlyTopDown = false; |
| 189 |
bool OnlyBottomUp = false; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
bool DisableLatencyHeuristic = false; |
| 194 |
|
| 195 |
|
| 196 |
bool ComputeDFSResult = false; |
| 197 |
|
| 198 |
MachineSchedPolicy() = default; |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
class MachineSchedStrategy { |
| 207 |
virtual void anchor(); |
| 208 |
|
| 209 |
public: |
| 210 |
virtual ~MachineSchedStrategy() = default; |
| 211 |
|
| 212 |
|
| 213 |
virtual void initPolicy(MachineBasicBlock::iterator Begin, |
| 214 |
MachineBasicBlock::iterator End, |
| 215 |
unsigned NumRegionInstrs) {} |
| 216 |
|
| 217 |
virtual void dumpPolicy() const {} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
virtual bool shouldTrackPressure() const { return true; } |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
virtual bool shouldTrackLaneMasks() const { return false; } |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
virtual bool doMBBSchedRegionsTopDown() const { return false; } |
| 232 |
|
| 233 |
|
| 234 |
virtual void initialize(ScheduleDAGMI *DAG) = 0; |
| 235 |
|
| 236 |
|
| 237 |
virtual void enterMBB(MachineBasicBlock *MBB) {}; |
| 238 |
|
| 239 |
|
| 240 |
virtual void leaveMBB() {}; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
virtual void registerRoots() {} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
virtual SUnit *pickNode(bool &IsTopNode) = 0; |
| 250 |
|
| 251 |
|
| 252 |
virtual void scheduleTree(unsigned SubtreeID) {} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
virtual void schedNode(SUnit *SU, bool IsTopNode) = 0; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
virtual void releaseTopNode(SUnit *SU) = 0; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
virtual void releaseBottomNode(SUnit *SU) = 0; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
class ScheduleDAGMI : public ScheduleDAGInstrs { |
| 272 |
protected: |
| 273 |
AAResults *AA; |
| 274 |
LiveIntervals *LIS; |
| 275 |
std::unique_ptr<MachineSchedStrategy> SchedImpl; |
| 276 |
|
| 277 |
|
| 278 |
std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations; |
| 279 |
|
| 280 |
|
| 281 |
MachineBasicBlock::iterator CurrentTop; |
| 282 |
|
| 283 |
|
| 284 |
MachineBasicBlock::iterator CurrentBottom; |
| 285 |
|
| 286 |
|
| 287 |
const SUnit *NextClusterPred = nullptr; |
| 288 |
const SUnit *NextClusterSucc = nullptr; |
| 289 |
|
| 290 |
#ifndef NDEBUG |
| 291 |
|
| 292 |
|
| 293 |
unsigned NumInstrsScheduled = 0; |
| 294 |
#endif |
| 295 |
|
| 296 |
public: |
| 297 |
ScheduleDAGMI(MachineSchedContext *C, std::unique_ptr<MachineSchedStrategy> S, |
| 298 |
bool RemoveKillFlags) |
| 299 |
: ScheduleDAGInstrs(*C->MF, C->MLI, RemoveKillFlags), AA(C->AA), |
| 300 |
LIS(C->LIS), SchedImpl(std::move(S)) {} |
| 301 |
|
| 302 |
|
| 303 |
~ScheduleDAGMI() override; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
bool doMBBSchedRegionsTopDown() const override { |
| 309 |
return SchedImpl->doMBBSchedRegionsTopDown(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
LiveIntervals *getLIS() const { return LIS; } |
| 314 |
|
| 315 |
|
| 316 |
virtual bool hasVRegLiveness() const { return false; } |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) { |
| 324 |
if (Mutation) |
| 325 |
Mutations.push_back(std::move(Mutation)); |
| 326 |
} |
| 327 |
|
| 328 |
MachineBasicBlock::iterator top() const { return CurrentTop; } |
| 329 |
MachineBasicBlock::iterator bottom() const { return CurrentBottom; } |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
void enterRegion(MachineBasicBlock *bb, |
| 335 |
MachineBasicBlock::iterator begin, |
| 336 |
MachineBasicBlock::iterator end, |
| 337 |
unsigned regioninstrs) override; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void schedule() override; |
| 342 |
|
| 343 |
void startBlock(MachineBasicBlock *bb) override; |
| 344 |
void finishBlock() override; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
void moveInstruction(MachineInstr *MI, MachineBasicBlock::iterator InsertPos); |
| 349 |
|
| 350 |
const SUnit *getNextClusterPred() const { return NextClusterPred; } |
| 351 |
|
| 352 |
const SUnit *getNextClusterSucc() const { return NextClusterSucc; } |
| 353 |
|
| 354 |
void viewGraph(const Twine &Name, const Twine &Title) override; |
| 355 |
void viewGraph() override; |
| 356 |
|
| 357 |
protected: |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
void postprocessDAG(); |
| 363 |
|
| 364 |
|
| 365 |
void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots); |
| 366 |
|
| 367 |
|
| 368 |
void updateQueues(SUnit *SU, bool IsTopNode); |
| 369 |
|
| 370 |
|
| 371 |
void placeDebugValues(); |
| 372 |
|
| 373 |
|
| 374 |
void dumpSchedule() const; |
| 375 |
|
| 376 |
|
| 377 |
bool checkSchedLimit(); |
| 378 |
|
| 379 |
void findRootsAndBiasEdges(SmallVectorImpl<SUnit*> &TopRoots, |
| 380 |
SmallVectorImpl<SUnit*> &BotRoots); |
| 381 |
|
| 382 |
void releaseSucc(SUnit *SU, SDep *SuccEdge); |
| 383 |
void releaseSuccessors(SUnit *SU); |
| 384 |
void releasePred(SUnit *SU, SDep *PredEdge); |
| 385 |
void releasePredecessors(SUnit *SU); |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
class ScheduleDAGMILive : public ScheduleDAGMI { |
| 391 |
protected: |
| 392 |
RegisterClassInfo *RegClassInfo; |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
SchedDFSResult *DFSResult = nullptr; |
| 397 |
BitVector ScheduledTrees; |
| 398 |
|
| 399 |
MachineBasicBlock::iterator LiveRegionEnd; |
| 400 |
|
| 401 |
|
| 402 |
VReg2SUnitMultiMap VRegUses; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
PressureDiffs SUPressureDiffs; |
| 408 |
|
| 409 |
|
| 410 |
bool ShouldTrackPressure = false; |
| 411 |
bool ShouldTrackLaneMasks = false; |
| 412 |
IntervalPressure RegPressure; |
| 413 |
RegPressureTracker RPTracker; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
std::vector<PressureChange> RegionCriticalPSets; |
| 419 |
|
| 420 |
|
| 421 |
IntervalPressure TopPressure; |
| 422 |
RegPressureTracker TopRPTracker; |
| 423 |
|
| 424 |
|
| 425 |
IntervalPressure BotPressure; |
| 426 |
RegPressureTracker BotRPTracker; |
| 427 |
|
| 428 |
public: |
| 429 |
ScheduleDAGMILive(MachineSchedContext *C, |
| 430 |
std::unique_ptr<MachineSchedStrategy> S) |
| 431 |
: ScheduleDAGMI(C, std::move(S), false), |
| 432 |
RegClassInfo(C->RegClassInfo), RPTracker(RegPressure), |
| 433 |
TopRPTracker(TopPressure), BotRPTracker(BotPressure) {} |
| 434 |
|
| 435 |
~ScheduleDAGMILive() override; |
| 436 |
|
| 437 |
|
| 438 |
bool hasVRegLiveness() const override { return true; } |
| 439 |
|
| 440 |
|
| 441 |
bool isTrackingPressure() const { return ShouldTrackPressure; } |
| 442 |
|
| 443 |
|
| 444 |
const IntervalPressure &getTopPressure() const { return TopPressure; } |
| 445 |
const RegPressureTracker &getTopRPTracker() const { return TopRPTracker; } |
| 446 |
|
| 447 |
|
| 448 |
const IntervalPressure &getBotPressure() const { return BotPressure; } |
| 449 |
const RegPressureTracker &getBotRPTracker() const { return BotRPTracker; } |
| 450 |
|
| 451 |
|
| 452 |
const IntervalPressure &getRegPressure() const { return RegPressure; } |
| 453 |
|
| 454 |
const std::vector<PressureChange> &getRegionCriticalPSets() const { |
| 455 |
return RegionCriticalPSets; |
| 456 |
} |
| 457 |
|
| 458 |
PressureDiff &getPressureDiff(const SUnit *SU) { |
| 459 |
return SUPressureDiffs[SU->NodeNum]; |
| 460 |
} |
| 461 |
const PressureDiff &getPressureDiff(const SUnit *SU) const { |
| 462 |
return SUPressureDiffs[SU->NodeNum]; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
void computeDFSResult(); |
| 468 |
|
| 469 |
|
| 470 |
const SchedDFSResult *getDFSResult() const { return DFSResult; } |
| 471 |
|
| 472 |
BitVector &getScheduledTrees() { return ScheduledTrees; } |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
void enterRegion(MachineBasicBlock *bb, |
| 478 |
MachineBasicBlock::iterator begin, |
| 479 |
MachineBasicBlock::iterator end, |
| 480 |
unsigned regioninstrs) override; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
void schedule() override; |
| 485 |
|
| 486 |
|
| 487 |
unsigned computeCyclicCriticalPath(); |
| 488 |
|
| 489 |
void dump() const override; |
| 490 |
|
| 491 |
protected: |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
void buildDAGWithRegPressure(); |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
void initQueues(ArrayRef<SUnit*> TopRoots, ArrayRef<SUnit*> BotRoots); |
| 503 |
|
| 504 |
|
| 505 |
void scheduleMI(SUnit *SU, bool IsTopNode); |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
void initRegPressure(); |
| 510 |
|
| 511 |
void updatePressureDiffs(ArrayRef<RegisterMaskPair> LiveUses); |
| 512 |
|
| 513 |
void updateScheduledPressure(const SUnit *SU, |
| 514 |
const std::vector<unsigned> &NewMaxPressure); |
| 515 |
|
| 516 |
void collectVRegUses(SUnit &SU); |
| 517 |
}; |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
class ReadyQueue { |
| 533 |
unsigned ID; |
| 534 |
std::string Name; |
| 535 |
std::vector<SUnit*> Queue; |
| 536 |
|
| 537 |
public: |
| 538 |
ReadyQueue(unsigned id, const Twine &name): ID(id), Name(name.str()) {} |
| 539 |
|
| 540 |
unsigned getID() const { return ID; } |
| 541 |
|
| 542 |
StringRef getName() const { return Name; } |
| 543 |
|
| 544 |
|
| 545 |
bool isInQueue(SUnit *SU) const { return (SU->NodeQueueId & ID); } |
| 546 |
|
| 547 |
bool empty() const { return Queue.empty(); } |
| 548 |
|
| 549 |
void clear() { Queue.clear(); } |
| 550 |
|
| 551 |
unsigned size() const { return Queue.size(); } |
| 552 |
|
| 553 |
using iterator = std::vector<SUnit*>::iterator; |
| 554 |
|
| 555 |
iterator begin() { return Queue.begin(); } |
| 556 |
|
| 557 |
iterator end() { return Queue.end(); } |
| 558 |
|
| 559 |
ArrayRef<SUnit*> elements() { return Queue; } |
| 560 |
|
| 561 |
iterator find(SUnit *SU) { return llvm::find(Queue, SU); } |
| 562 |
|
| 563 |
void push(SUnit *SU) { |
| 564 |
Queue.push_back(SU); |
| 565 |
SU->NodeQueueId |= ID; |
| 566 |
} |
| 567 |
|
| 568 |
iterator remove(iterator I) { |
| 569 |
(*I)->NodeQueueId &= ~ID; |
| 570 |
*I = Queue.back(); |
| 571 |
unsigned idx = I - Queue.begin(); |
| 572 |
Queue.pop_back(); |
| 573 |
return Queue.begin() + idx; |
| 574 |
} |
| 575 |
|
| 576 |
void dump() const; |
| 577 |
}; |
| 578 |
|
| 579 |
|
| 580 |
struct SchedRemainder { |
| 581 |
|
| 582 |
unsigned CriticalPath; |
| 583 |
unsigned CyclicCritPath; |
| 584 |
|
| 585 |
|
| 586 |
unsigned RemIssueCount; |
| 587 |
|
| 588 |
bool IsAcyclicLatencyLimited; |
| 589 |
|
| 590 |
|
| 591 |
SmallVector<unsigned, 16> RemainingCounts; |
| 592 |
|
| 593 |
SchedRemainder() { reset(); } |
| 594 |
|
| 595 |
void reset() { |
| 596 |
CriticalPath = 0; |
| 597 |
CyclicCritPath = 0; |
| 598 |
RemIssueCount = 0; |
| 599 |
IsAcyclicLatencyLimited = false; |
| 600 |
RemainingCounts.clear(); |
| 601 |
} |
| 602 |
|
| 603 |
void init(ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel); |
| 604 |
}; |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
class SchedBoundary { |
| 610 |
public: |
| 611 |
|
| 612 |
enum { |
| 613 |
TopQID = 1, |
| 614 |
BotQID = 2, |
| 615 |
LogMaxQID = 2 |
| 616 |
}; |
| 617 |
|
| 618 |
ScheduleDAGMI *DAG = nullptr; |
| 619 |
const TargetSchedModel *SchedModel = nullptr; |
| 620 |
SchedRemainder *Rem = nullptr; |
| 621 |
|
| 622 |
ReadyQueue Available; |
| 623 |
ReadyQueue Pending; |
| 624 |
|
| 625 |
ScheduleHazardRecognizer *HazardRec = nullptr; |
| 626 |
|
| 627 |
private: |
| 628 |
|
| 629 |
|
| 630 |
bool CheckPending; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
unsigned CurrCycle; |
| 636 |
|
| 637 |
|
| 638 |
unsigned CurrMOps; |
| 639 |
|
| 640 |
|
| 641 |
unsigned MinReadyCycle; |
| 642 |
|
| 643 |
|
| 644 |
unsigned ExpectedLatency; |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
unsigned DependentLatency; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
unsigned RetiredMOps; |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
SmallVector<unsigned, 16> ExecutedResCounts; |
| 661 |
|
| 662 |
|
| 663 |
unsigned MaxExecutedResCount; |
| 664 |
|
| 665 |
|
| 666 |
unsigned ZoneCritResIdx; |
| 667 |
|
| 668 |
|
| 669 |
bool IsResourceLimited; |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
SmallVector<unsigned, 16> ReservedCycles; |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
SmallVector<unsigned, 16> ReservedCyclesIndex; |
| 678 |
|
| 679 |
|
| 680 |
SmallVector<APInt, 16> ResourceGroupSubUnitMasks; |
| 681 |
|
| 682 |
#ifndef NDEBUG |
| 683 |
|
| 684 |
|
| 685 |
unsigned MaxObservedStall; |
| 686 |
#endif |
| 687 |
|
| 688 |
public: |
| 689 |
|
| 690 |
|
| 691 |
SchedBoundary(unsigned ID, const Twine &Name): |
| 692 |
Available(ID, Name+".A"), Pending(ID << LogMaxQID, Name+".P") { |
| 693 |
reset(); |
| 694 |
} |
| 695 |
|
| 696 |
~SchedBoundary(); |
| 697 |
|
| 698 |
void reset(); |
| 699 |
|
| 700 |
void init(ScheduleDAGMI *dag, const TargetSchedModel *smodel, |
| 701 |
SchedRemainder *rem); |
| 702 |
|
| 703 |
bool isTop() const { |
| 704 |
return Available.getID() == TopQID; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
unsigned getCurrCycle() const { return CurrCycle; } |
| 709 |
|
| 710 |
|
| 711 |
unsigned getCurrMOps() const { return CurrMOps; } |
| 712 |
|
| 713 |
|
| 714 |
unsigned getDependentLatency() const { return DependentLatency; } |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
unsigned getScheduledLatency() const { |
| 720 |
return std::max(ExpectedLatency, CurrCycle); |
| 721 |
} |
| 722 |
|
| 723 |
unsigned getUnscheduledLatency(SUnit *SU) const { |
| 724 |
return isTop() ? SU->getHeight() : SU->getDepth(); |
| 725 |
} |
| 726 |
|
| 727 |
unsigned getResourceCount(unsigned ResIdx) const { |
| 728 |
return ExecutedResCounts[ResIdx]; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
unsigned getCriticalCount() const { |
| 734 |
if (!ZoneCritResIdx) |
| 735 |
return RetiredMOps * SchedModel->getMicroOpFactor(); |
| 736 |
return getResourceCount(ZoneCritResIdx); |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
unsigned getExecutedCount() const { |
| 743 |
return std::max(CurrCycle * SchedModel->getLatencyFactor(), |
| 744 |
MaxExecutedResCount); |
| 745 |
} |
| 746 |
|
| 747 |
unsigned getZoneCritResIdx() const { return ZoneCritResIdx; } |
| 748 |
|
| 749 |
|
| 750 |
bool isResourceLimited() const { return IsResourceLimited; } |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
unsigned getLatencyStallCycles(SUnit *SU); |
| 755 |
|
| 756 |
unsigned getNextResourceCycleByInstance(unsigned InstanceIndex, |
| 757 |
unsigned Cycles); |
| 758 |
|
| 759 |
std::pair<unsigned, unsigned> getNextResourceCycle(const MCSchedClassDesc *SC, |
| 760 |
unsigned PIdx, |
| 761 |
unsigned Cycles); |
| 762 |
|
| 763 |
bool isUnbufferedGroup(unsigned PIdx) const { |
| 764 |
return SchedModel->getProcResource(PIdx)->SubUnitsIdxBegin && |
| 765 |
!SchedModel->getProcResource(PIdx)->BufferSize; |
| 766 |
} |
| 767 |
|
| 768 |
bool checkHazard(SUnit *SU); |
| 769 |
|
| 770 |
unsigned findMaxLatency(ArrayRef<SUnit*> ReadySUs); |
| 771 |
|
| 772 |
unsigned getOtherResourceCount(unsigned &OtherCritIdx); |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
void releaseNode(SUnit *SU, unsigned ReadyCycle, bool InPQueue, |
| 783 |
unsigned Idx = 0); |
| 784 |
|
| 785 |
void bumpCycle(unsigned NextCycle); |
| 786 |
|
| 787 |
void incExecutedResources(unsigned PIdx, unsigned Count); |
| 788 |
|
| 789 |
unsigned countResource(const MCSchedClassDesc *SC, unsigned PIdx, |
| 790 |
unsigned Cycles, unsigned ReadyCycle); |
| 791 |
|
| 792 |
void bumpNode(SUnit *SU); |
| 793 |
|
| 794 |
void releasePending(); |
| 795 |
|
| 796 |
void removeReady(SUnit *SU); |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
SUnit *pickOnlyChoice(); |
| 802 |
|
| 803 |
void dumpScheduledState() const; |
| 804 |
}; |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
class GenericSchedulerBase : public MachineSchedStrategy { |
| 810 |
public: |
| 811 |
|
| 812 |
|
| 813 |
enum CandReason : uint8_t { |
| 814 |
NoCand, Only1, PhysReg, RegExcess, RegCritical, Stall, Cluster, Weak, |
| 815 |
RegMax, ResourceReduce, ResourceDemand, BotHeightReduce, BotPathReduce, |
| 816 |
TopDepthReduce, TopPathReduce, NextDefUse, NodeOrder}; |
| 817 |
|
| 818 |
#ifndef NDEBUG |
| 819 |
static const char *getReasonStr(GenericSchedulerBase::CandReason Reason); |
| 820 |
#endif |
| 821 |
|
| 822 |
|
| 823 |
struct CandPolicy { |
| 824 |
bool ReduceLatency = false; |
| 825 |
unsigned ReduceResIdx = 0; |
| 826 |
unsigned DemandResIdx = 0; |
| 827 |
|
| 828 |
CandPolicy() = default; |
| 829 |
|
| 830 |
bool operator==(const CandPolicy &RHS) const { |
| 831 |
return ReduceLatency == RHS.ReduceLatency && |
| 832 |
ReduceResIdx == RHS.ReduceResIdx && |
| 833 |
DemandResIdx == RHS.DemandResIdx; |
| 834 |
} |
| 835 |
bool operator!=(const CandPolicy &RHS) const { |
| 836 |
return !(*this == RHS); |
| 837 |
} |
| 838 |
}; |
| 839 |
|
| 840 |
|
| 841 |
struct SchedResourceDelta { |
| 842 |
|
| 843 |
unsigned CritResources = 0; |
| 844 |
|
| 845 |
|
| 846 |
unsigned DemandedResources = 0; |
| 847 |
|
| 848 |
SchedResourceDelta() = default; |
| 849 |
|
| 850 |
bool operator==(const SchedResourceDelta &RHS) const { |
| 851 |
return CritResources == RHS.CritResources |
| 852 |
&& DemandedResources == RHS.DemandedResources; |
| 853 |
} |
| 854 |
bool operator!=(const SchedResourceDelta &RHS) const { |
| 855 |
return !operator==(RHS); |
| 856 |
} |
| 857 |
}; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
struct SchedCandidate { |
| 862 |
CandPolicy Policy; |
| 863 |
|
| 864 |
|
| 865 |
SUnit *SU; |
| 866 |
|
| 867 |
|
| 868 |
CandReason Reason; |
| 869 |
|
| 870 |
|
| 871 |
bool AtTop; |
| 872 |
|
| 873 |
|
| 874 |
RegPressureDelta RPDelta; |
| 875 |
|
| 876 |
|
| 877 |
SchedResourceDelta ResDelta; |
| 878 |
|
| 879 |
SchedCandidate() { reset(CandPolicy()); } |
| 880 |
SchedCandidate(const CandPolicy &Policy) { reset(Policy); } |
| 881 |
|
| 882 |
void reset(const CandPolicy &NewPolicy) { |
| 883 |
Policy = NewPolicy; |
| 884 |
SU = nullptr; |
| 885 |
Reason = NoCand; |
| 886 |
AtTop = false; |
| 887 |
RPDelta = RegPressureDelta(); |
| 888 |
ResDelta = SchedResourceDelta(); |
| 889 |
} |
| 890 |
|
| 891 |
bool isValid() const { return SU; } |
| 892 |
|
| 893 |
|
| 894 |
void setBest(SchedCandidate &Best) { |
| 895 |
assert(Best.Reason != NoCand && "uninitialized Sched candidate"); |
| 896 |
SU = Best.SU; |
| 897 |
Reason = Best.Reason; |
| 898 |
AtTop = Best.AtTop; |
| 899 |
RPDelta = Best.RPDelta; |
| 900 |
ResDelta = Best.ResDelta; |
| 901 |
} |
| 902 |
|
| 903 |
void initResourceDelta(const ScheduleDAGMI *DAG, |
| 904 |
const TargetSchedModel *SchedModel); |
| 905 |
}; |
| 906 |
|
| 907 |
protected: |
| 908 |
const MachineSchedContext *Context; |
| 909 |
const TargetSchedModel *SchedModel = nullptr; |
| 910 |
const TargetRegisterInfo *TRI = nullptr; |
| 911 |
|
| 912 |
SchedRemainder Rem; |
| 913 |
|
| 914 |
GenericSchedulerBase(const MachineSchedContext *C) : Context(C) {} |
| 915 |
|
| 916 |
void setPolicy(CandPolicy &Policy, bool IsPostRA, SchedBoundary &CurrZone, |
| 917 |
SchedBoundary *OtherZone); |
| 918 |
|
| 919 |
#ifndef NDEBUG |
| 920 |
void traceCandidate(const SchedCandidate &Cand); |
| 921 |
#endif |
| 922 |
|
| 923 |
private: |
| 924 |
bool shouldReduceLatency(const CandPolicy &Policy, SchedBoundary &CurrZone, |
| 925 |
bool ComputeRemLatency, unsigned &RemLatency) const; |
| 926 |
}; |
| 927 |
|
| 928 |
|
| 929 |
bool tryLess(int TryVal, int CandVal, |
| 930 |
GenericSchedulerBase::SchedCandidate &TryCand, |
| 931 |
GenericSchedulerBase::SchedCandidate &Cand, |
| 932 |
GenericSchedulerBase::CandReason Reason); |
| 933 |
bool tryGreater(int TryVal, int CandVal, |
| 934 |
GenericSchedulerBase::SchedCandidate &TryCand, |
| 935 |
GenericSchedulerBase::SchedCandidate &Cand, |
| 936 |
GenericSchedulerBase::CandReason Reason); |
| 937 |
bool tryLatency(GenericSchedulerBase::SchedCandidate &TryCand, |
| 938 |
GenericSchedulerBase::SchedCandidate &Cand, |
| 939 |
SchedBoundary &Zone); |
| 940 |
bool tryPressure(const PressureChange &TryP, |
| 941 |
const PressureChange &CandP, |
| 942 |
GenericSchedulerBase::SchedCandidate &TryCand, |
| 943 |
GenericSchedulerBase::SchedCandidate &Cand, |
| 944 |
GenericSchedulerBase::CandReason Reason, |
| 945 |
const TargetRegisterInfo *TRI, |
| 946 |
const MachineFunction &MF); |
| 947 |
unsigned getWeakLeft(const SUnit *SU, bool isTop); |
| 948 |
int biasPhysReg(const SUnit *SU, bool isTop); |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
class GenericScheduler : public GenericSchedulerBase { |
| 953 |
public: |
| 954 |
GenericScheduler(const MachineSchedContext *C): |
| 955 |
GenericSchedulerBase(C), Top(SchedBoundary::TopQID, "TopQ"), |
| 956 |
Bot(SchedBoundary::BotQID, "BotQ") {} |
| 957 |
|
| 958 |
void initPolicy(MachineBasicBlock::iterator Begin, |
| 959 |
MachineBasicBlock::iterator End, |
| 960 |
unsigned NumRegionInstrs) override; |
| 961 |
|
| 962 |
void dumpPolicy() const override; |
| 963 |
|
| 964 |
bool shouldTrackPressure() const override { |
| 965 |
return RegionPolicy.ShouldTrackPressure; |
| 966 |
} |
| 967 |
|
| 968 |
bool shouldTrackLaneMasks() const override { |
| 969 |
return RegionPolicy.ShouldTrackLaneMasks; |
| 970 |
} |
| 971 |
|
| 972 |
void initialize(ScheduleDAGMI *dag) override; |
| 973 |
|
| 974 |
SUnit *pickNode(bool &IsTopNode) override; |
| 975 |
|
| 976 |
void schedNode(SUnit *SU, bool IsTopNode) override; |
| 977 |
|
| 978 |
void releaseTopNode(SUnit *SU) override { |
| 979 |
if (SU->isScheduled) |
| 980 |
return; |
| 981 |
|
| 982 |
Top.releaseNode(SU, SU->TopReadyCycle, false); |
| 983 |
TopCand.SU = nullptr; |
| 984 |
} |
| 985 |
|
| 986 |
void releaseBottomNode(SUnit *SU) override { |
| 987 |
if (SU->isScheduled) |
| 988 |
return; |
| 989 |
|
| 990 |
Bot.releaseNode(SU, SU->BotReadyCycle, false); |
| 991 |
BotCand.SU = nullptr; |
| 992 |
} |
| 993 |
|
| 994 |
void registerRoots() override; |
| 995 |
|
| 996 |
protected: |
| 997 |
ScheduleDAGMILive *DAG = nullptr; |
| 998 |
|
| 999 |
MachineSchedPolicy RegionPolicy; |
| 1000 |
|
| 1001 |
|
| 1002 |
SchedBoundary Top; |
| 1003 |
SchedBoundary Bot; |
| 1004 |
|
| 1005 |
|
| 1006 |
SchedCandidate TopCand; |
| 1007 |
|
| 1008 |
SchedCandidate BotCand; |
| 1009 |
|
| 1010 |
void checkAcyclicLatency(); |
| 1011 |
|
| 1012 |
void initCandidate(SchedCandidate &Cand, SUnit *SU, bool AtTop, |
| 1013 |
const RegPressureTracker &RPTracker, |
| 1014 |
RegPressureTracker &TempTracker); |
| 1015 |
|
| 1016 |
virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand, |
| 1017 |
SchedBoundary *Zone) const; |
| 1018 |
|
| 1019 |
SUnit *pickNodeBidirectional(bool &IsTopNode); |
| 1020 |
|
| 1021 |
void pickNodeFromQueue(SchedBoundary &Zone, |
| 1022 |
const CandPolicy &ZonePolicy, |
| 1023 |
const RegPressureTracker &RPTracker, |
| 1024 |
SchedCandidate &Candidate); |
| 1025 |
|
| 1026 |
void reschedulePhysReg(SUnit *SU, bool isTop); |
| 1027 |
}; |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
class PostGenericScheduler : public GenericSchedulerBase { |
| 1035 |
protected: |
| 1036 |
ScheduleDAGMI *DAG = nullptr; |
| 1037 |
SchedBoundary Top; |
| 1038 |
SmallVector<SUnit*, 8> BotRoots; |
| 1039 |
|
| 1040 |
public: |
| 1041 |
PostGenericScheduler(const MachineSchedContext *C): |
| 1042 |
GenericSchedulerBase(C), Top(SchedBoundary::TopQID, "TopQ") {} |
| 1043 |
|
| 1044 |
~PostGenericScheduler() override = default; |
| 1045 |
|
| 1046 |
void initPolicy(MachineBasicBlock::iterator Begin, |
| 1047 |
MachineBasicBlock::iterator End, |
| 1048 |
unsigned NumRegionInstrs) override { |
| 1049 |
|
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
bool shouldTrackPressure() const override { return false; } |
| 1054 |
|
| 1055 |
void initialize(ScheduleDAGMI *Dag) override; |
| 1056 |
|
| 1057 |
void registerRoots() override; |
| 1058 |
|
| 1059 |
SUnit *pickNode(bool &IsTopNode) override; |
| 1060 |
|
| 1061 |
void scheduleTree(unsigned SubtreeID) override { |
| 1062 |
llvm_unreachable("PostRA scheduler does not support subtree analysis."); |
| 1063 |
} |
| 1064 |
|
| 1065 |
void schedNode(SUnit *SU, bool IsTopNode) override; |
| 1066 |
|
| 1067 |
void releaseTopNode(SUnit *SU) override { |
| 1068 |
if (SU->isScheduled) |
| 1069 |
return; |
| 1070 |
Top.releaseNode(SU, SU->TopReadyCycle, false); |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
void releaseBottomNode(SUnit *SU) override { |
| 1075 |
BotRoots.push_back(SU); |
| 1076 |
} |
| 1077 |
|
| 1078 |
protected: |
| 1079 |
virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand); |
| 1080 |
|
| 1081 |
void pickNodeFromQueue(SchedCandidate &Cand); |
| 1082 |
}; |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
ScheduleDAGMILive *createGenericSchedLive(MachineSchedContext *C); |
| 1088 |
|
| 1089 |
|
| 1090 |
ScheduleDAGMI *createGenericSchedPostRA(MachineSchedContext *C); |
| 1091 |
|
| 1092 |
std::unique_ptr<ScheduleDAGMutation> |
| 1093 |
createLoadClusterDAGMutation(const TargetInstrInfo *TII, |
| 1094 |
const TargetRegisterInfo *TRI); |
| 1095 |
|
| 1096 |
std::unique_ptr<ScheduleDAGMutation> |
| 1097 |
createStoreClusterDAGMutation(const TargetInstrInfo *TII, |
| 1098 |
const TargetRegisterInfo *TRI); |
| 1099 |
|
| 1100 |
std::unique_ptr<ScheduleDAGMutation> |
| 1101 |
createCopyConstrainDAGMutation(const TargetInstrInfo *TII, |
| 1102 |
const TargetRegisterInfo *TRI); |
| 1103 |
|
| 1104 |
} |
| 1105 |
|
| 1106 |
#endif |