| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef LLVM_CODEGEN_SCHEDULEDAG_H |
| 16 |
#define LLVM_CODEGEN_SCHEDULEDAG_H |
| 17 |
|
| 18 |
#include "llvm/ADT/BitVector.h" |
| 19 |
#include "llvm/ADT/GraphTraits.h" |
| 20 |
#include "llvm/ADT/PointerIntPair.h" |
| 21 |
#include "llvm/ADT/SmallVector.h" |
| 22 |
#include "llvm/ADT/iterator.h" |
| 23 |
#include "llvm/CodeGen/MachineInstr.h" |
| 24 |
#include "llvm/CodeGen/TargetLowering.h" |
| 25 |
#include "llvm/Support/ErrorHandling.h" |
| 26 |
#include <cassert> |
| 27 |
#include <cstddef> |
| 28 |
#include <iterator> |
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
namespace llvm { |
| 33 |
|
| 34 |
template<class Graph> class GraphWriter; |
| 35 |
class LLVMTargetMachine; |
| 36 |
class MachineFunction; |
| 37 |
class MachineRegisterInfo; |
| 38 |
class MCInstrDesc; |
| 39 |
struct MCSchedClassDesc; |
| 40 |
class SDNode; |
| 41 |
class SUnit; |
| 42 |
class ScheduleDAG; |
| 43 |
class TargetInstrInfo; |
| 44 |
class TargetRegisterClass; |
| 45 |
class TargetRegisterInfo; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class SDep { |
| 50 |
public: |
| 51 |
|
| 52 |
enum Kind { |
| 53 |
Data, |
| 54 |
Anti, |
| 55 |
Output, |
| 56 |
Order |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
enum OrderKind { |
| 69 |
Barrier, |
| 70 |
MayAliasMem, |
| 71 |
MustAliasMem, |
| 72 |
Artificial, |
| 73 |
Weak, |
| 74 |
Cluster |
| 75 |
}; |
| 76 |
|
| 77 |
private: |
| 78 |
|
| 79 |
|
| 80 |
PointerIntPair<SUnit *, 2, Kind> Dep; |
| 81 |
|
| 82 |
|
| 83 |
union { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
unsigned Reg; |
| 88 |
|
| 89 |
|
| 90 |
unsigned OrdKind; |
| 91 |
} Contents; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
unsigned Latency; |
| 97 |
|
| 98 |
public: |
| 99 |
|
| 100 |
|
| 101 |
SDep() : Dep(nullptr, Data) {} |
| 102 |
|
| 103 |
|
| 104 |
SDep(SUnit *S, Kind kind, unsigned Reg) |
| 105 |
: Dep(S, kind), Contents() { |
| 106 |
switch (kind) { |
| 107 |
default: |
| 108 |
llvm_unreachable("Reg given for non-register dependence!"); |
| 109 |
case Anti: |
| 110 |
case Output: |
| 111 |
assert(Reg != 0 && |
| 112 |
"SDep::Anti and SDep::Output must use a non-zero Reg!"); |
| 113 |
Contents.Reg = Reg; |
| 114 |
Latency = 0; |
| 115 |
break; |
| 116 |
case Data: |
| 117 |
Contents.Reg = Reg; |
| 118 |
Latency = 1; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
SDep(SUnit *S, OrderKind kind) |
| 124 |
: Dep(S, Order), Contents(), Latency(0) { |
| 125 |
Contents.OrdKind = kind; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
bool overlaps(const SDep &Other) const; |
| 130 |
|
| 131 |
bool operator==(const SDep &Other) const { |
| 132 |
return overlaps(Other) && Latency == Other.Latency; |
| 133 |
} |
| 134 |
|
| 135 |
bool operator!=(const SDep &Other) const { |
| 136 |
return !operator==(Other); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
unsigned getLatency() const { |
| 143 |
return Latency; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
void setLatency(unsigned Lat) { |
| 148 |
Latency = Lat; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
SUnit *getSUnit() const; |
| 153 |
|
| 154 |
|
| 155 |
void setSUnit(SUnit *SU); |
| 156 |
|
| 157 |
|
| 158 |
Kind getKind() const; |
| 159 |
|
| 160 |
|
| 161 |
bool isCtrl() const { |
| 162 |
return getKind() != Data; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
bool isNormalMemory() const { |
| 169 |
return getKind() == Order && (Contents.OrdKind == MayAliasMem |
| 170 |
|| Contents.OrdKind == MustAliasMem); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
bool isBarrier() const { |
| 175 |
return getKind() == Order && Contents.OrdKind == Barrier; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
bool isNormalMemoryOrBarrier() const { |
| 180 |
return (isNormalMemory() || isBarrier()); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
bool isMustAlias() const { |
| 187 |
return getKind() == Order && Contents.OrdKind == MustAliasMem; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
bool isWeak() const { |
| 195 |
return getKind() == Order && Contents.OrdKind >= Weak; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
bool isArtificial() const { |
| 201 |
return getKind() == Order && Contents.OrdKind == Artificial; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
bool isCluster() const { |
| 207 |
return getKind() == Order && Contents.OrdKind == Cluster; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
bool isAssignedRegDep() const { |
| 212 |
return getKind() == Data && Contents.Reg != 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
unsigned getReg() const { |
| 219 |
assert((getKind() == Data || getKind() == Anti || getKind() == Output) && |
| 220 |
"getReg called on non-register dependence edge!"); |
| 221 |
return Contents.Reg; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
void setReg(unsigned Reg) { |
| 229 |
assert((getKind() == Data || getKind() == Anti || getKind() == Output) && |
| 230 |
"setReg called on non-register dependence edge!"); |
| 231 |
assert((getKind() != Anti || Reg != 0) && |
| 232 |
"SDep::Anti edge cannot use the zero register!"); |
| 233 |
assert((getKind() != Output || Reg != 0) && |
| 234 |
"SDep::Output edge cannot use the zero register!"); |
| 235 |
Contents.Reg = Reg; |
| 236 |
} |
| 237 |
|
| 238 |
void dump(const TargetRegisterInfo *TRI = nullptr) const; |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
class SUnit { |
| 243 |
private: |
| 244 |
enum : unsigned { BoundaryID = ~0u }; |
| 245 |
|
| 246 |
SDNode *Node = nullptr; |
| 247 |
MachineInstr *Instr = nullptr; |
| 248 |
|
| 249 |
public: |
| 250 |
SUnit *OrigNode = nullptr; |
| 251 |
|
| 252 |
|
| 253 |
const MCSchedClassDesc *SchedClass = |
| 254 |
nullptr; |
| 255 |
|
| 256 |
SmallVector<SDep, 4> Preds; |
| 257 |
SmallVector<SDep, 4> Succs; |
| 258 |
|
| 259 |
typedef SmallVectorImpl<SDep>::iterator pred_iterator; |
| 260 |
typedef SmallVectorImpl<SDep>::iterator succ_iterator; |
| 261 |
typedef SmallVectorImpl<SDep>::const_iterator const_pred_iterator; |
| 262 |
typedef SmallVectorImpl<SDep>::const_iterator const_succ_iterator; |
| 263 |
|
| 264 |
unsigned NodeNum = BoundaryID; |
| 265 |
unsigned NodeQueueId = 0; |
| 266 |
unsigned NumPreds = 0; |
| 267 |
unsigned NumSuccs = 0; |
| 268 |
unsigned NumPredsLeft = 0; |
| 269 |
unsigned NumSuccsLeft = 0; |
| 270 |
unsigned WeakPredsLeft = 0; |
| 271 |
unsigned WeakSuccsLeft = 0; |
| 272 |
unsigned short NumRegDefsLeft = 0; |
| 273 |
unsigned short Latency = 0; |
| 274 |
bool isVRegCycle : 1; |
| 275 |
bool isCall : 1; |
| 276 |
bool isCallOp : 1; |
| 277 |
bool isTwoAddress : 1; |
| 278 |
bool isCommutable : 1; |
| 279 |
bool hasPhysRegUses : 1; |
| 280 |
bool hasPhysRegDefs : 1; |
| 281 |
bool hasPhysRegClobbers : 1; |
| 282 |
bool isPending : 1; |
| 283 |
bool isAvailable : 1; |
| 284 |
bool isScheduled : 1; |
| 285 |
bool isScheduleHigh : 1; |
| 286 |
bool isScheduleLow : 1; |
| 287 |
bool isCloned : 1; |
| 288 |
bool isUnbuffered : 1; |
| 289 |
bool hasReservedResource : 1; |
| 290 |
Sched::Preference SchedulingPref = Sched::None; |
| 291 |
|
| 292 |
private: |
| 293 |
bool isDepthCurrent : 1; |
| 294 |
bool isHeightCurrent : 1; |
| 295 |
unsigned Depth = 0; |
| 296 |
unsigned Height = 0; |
| 297 |
|
| 298 |
public: |
| 299 |
unsigned TopReadyCycle = 0; |
| 300 |
unsigned BotReadyCycle = 0; |
| 301 |
|
| 302 |
const TargetRegisterClass *CopyDstRC = |
| 303 |
nullptr; |
| 304 |
const TargetRegisterClass *CopySrcRC = nullptr; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
SUnit(SDNode *node, unsigned nodenum) |
| 309 |
: Node(node), NodeNum(nodenum), isVRegCycle(false), isCall(false), |
| 310 |
isCallOp(false), isTwoAddress(false), isCommutable(false), |
| 311 |
hasPhysRegUses(false), hasPhysRegDefs(false), hasPhysRegClobbers(false), |
| 312 |
isPending(false), isAvailable(false), isScheduled(false), |
| 313 |
isScheduleHigh(false), isScheduleLow(false), isCloned(false), |
| 314 |
isUnbuffered(false), hasReservedResource(false), isDepthCurrent(false), |
| 315 |
isHeightCurrent(false) {} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
SUnit(MachineInstr *instr, unsigned nodenum) |
| 320 |
: Instr(instr), NodeNum(nodenum), isVRegCycle(false), isCall(false), |
| 321 |
isCallOp(false), isTwoAddress(false), isCommutable(false), |
| 322 |
hasPhysRegUses(false), hasPhysRegDefs(false), hasPhysRegClobbers(false), |
| 323 |
isPending(false), isAvailable(false), isScheduled(false), |
| 324 |
isScheduleHigh(false), isScheduleLow(false), isCloned(false), |
| 325 |
isUnbuffered(false), hasReservedResource(false), isDepthCurrent(false), |
| 326 |
isHeightCurrent(false) {} |
| 327 |
|
| 328 |
|
| 329 |
SUnit() |
| 330 |
: isVRegCycle(false), isCall(false), isCallOp(false), isTwoAddress(false), |
| 331 |
isCommutable(false), hasPhysRegUses(false), hasPhysRegDefs(false), |
| 332 |
hasPhysRegClobbers(false), isPending(false), isAvailable(false), |
| 333 |
isScheduled(false), isScheduleHigh(false), isScheduleLow(false), |
| 334 |
isCloned(false), isUnbuffered(false), hasReservedResource(false), |
| 335 |
isDepthCurrent(false), isHeightCurrent(false) {} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
bool isBoundaryNode() const { return NodeNum == BoundaryID; } |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
void setNode(SDNode *N) { |
| 349 |
assert(!Instr && "Setting SDNode of SUnit with MachineInstr!"); |
| 350 |
Node = N; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
SDNode *getNode() const { |
| 356 |
assert(!Instr && "Reading SDNode of SUnit with MachineInstr!"); |
| 357 |
return Node; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
bool isInstr() const { return Instr; } |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
void setInstr(MachineInstr *MI) { |
| 367 |
assert(!Node && "Setting MachineInstr of SUnit with SDNode!"); |
| 368 |
Instr = MI; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
MachineInstr *getInstr() const { |
| 374 |
assert(!Node && "Reading MachineInstr of SUnit with SDNode!"); |
| 375 |
return Instr; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
bool addPred(const SDep &D, bool Required = true); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
bool addPredBarrier(SUnit *SU) { |
| 385 |
SDep Dep(SU, SDep::Barrier); |
| 386 |
unsigned TrueMemOrderLatency = |
| 387 |
((SU->getInstr()->mayStore() && this->getInstr()->mayLoad()) ? 1 : 0); |
| 388 |
Dep.setLatency(TrueMemOrderLatency); |
| 389 |
return addPred(Dep); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
void removePred(const SDep &D); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
unsigned getDepth() const { |
| 399 |
if (!isDepthCurrent) |
| 400 |
const_cast<SUnit *>(this)->ComputeDepth(); |
| 401 |
return Depth; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
unsigned getHeight() const { |
| 407 |
if (!isHeightCurrent) |
| 408 |
const_cast<SUnit *>(this)->ComputeHeight(); |
| 409 |
return Height; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
void setDepthToAtLeast(unsigned NewDepth); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
void setHeightToAtLeast(unsigned NewHeight); |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
void setDepthDirty(); |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
void setHeightDirty(); |
| 429 |
|
| 430 |
|
| 431 |
bool isPred(const SUnit *N) const { |
| 432 |
for (const SDep &Pred : Preds) |
| 433 |
if (Pred.getSUnit() == N) |
| 434 |
return true; |
| 435 |
return false; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
bool isSucc(const SUnit *N) const { |
| 440 |
for (const SDep &Succ : Succs) |
| 441 |
if (Succ.getSUnit() == N) |
| 442 |
return true; |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
bool isTopReady() const { |
| 447 |
return NumPredsLeft == 0; |
| 448 |
} |
| 449 |
bool isBottomReady() const { |
| 450 |
return NumSuccsLeft == 0; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
void biasCriticalPath(); |
| 456 |
|
| 457 |
void dumpAttributes() const; |
| 458 |
|
| 459 |
private: |
| 460 |
void ComputeDepth(); |
| 461 |
void ComputeHeight(); |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
inline bool SDep::overlaps(const SDep &Other) const { |
| 466 |
if (Dep != Other.Dep) |
| 467 |
return false; |
| 468 |
switch (Dep.getInt()) { |
| 469 |
case Data: |
| 470 |
case Anti: |
| 471 |
case Output: |
| 472 |
return Contents.Reg == Other.Contents.Reg; |
| 473 |
case Order: |
| 474 |
return Contents.OrdKind == Other.Contents.OrdKind; |
| 475 |
} |
| 476 |
llvm_unreachable("Invalid dependency kind!"); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
inline SUnit *SDep::getSUnit() const { return Dep.getPointer(); } |
| 481 |
|
| 482 |
|
| 483 |
inline void SDep::setSUnit(SUnit *SU) { Dep.setPointer(SU); } |
| 484 |
|
| 485 |
|
| 486 |
inline SDep::Kind SDep::getKind() const { return Dep.getInt(); } |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
class SchedulingPriorityQueue { |
| 497 |
virtual void anchor(); |
| 498 |
|
| 499 |
unsigned CurCycle = 0; |
| 500 |
bool HasReadyFilter; |
| 501 |
|
| 502 |
public: |
| 503 |
SchedulingPriorityQueue(bool rf = false) : HasReadyFilter(rf) {} |
| 504 |
|
| 505 |
virtual ~SchedulingPriorityQueue() = default; |
| 506 |
|
| 507 |
virtual bool isBottomUp() const = 0; |
| 508 |
|
| 509 |
virtual void initNodes(std::vector<SUnit> &SUnits) = 0; |
| 510 |
virtual void addNode(const SUnit *SU) = 0; |
| 511 |
virtual void updateNode(const SUnit *SU) = 0; |
| 512 |
virtual void releaseState() = 0; |
| 513 |
|
| 514 |
virtual bool empty() const = 0; |
| 515 |
|
| 516 |
bool hasReadyFilter() const { return HasReadyFilter; } |
| 517 |
|
| 518 |
virtual bool tracksRegPressure() const { return false; } |
| 519 |
|
| 520 |
virtual bool isReady(SUnit *) const { |
| 521 |
assert(!HasReadyFilter && "The ready filter must override isReady()"); |
| 522 |
return true; |
| 523 |
} |
| 524 |
|
| 525 |
virtual void push(SUnit *U) = 0; |
| 526 |
|
| 527 |
void push_all(const std::vector<SUnit *> &Nodes) { |
| 528 |
for (std::vector<SUnit *>::const_iterator I = Nodes.begin(), |
| 529 |
E = Nodes.end(); I != E; ++I) |
| 530 |
push(*I); |
| 531 |
} |
| 532 |
|
| 533 |
virtual SUnit *pop() = 0; |
| 534 |
|
| 535 |
virtual void remove(SUnit *SU) = 0; |
| 536 |
|
| 537 |
virtual void dump(ScheduleDAG *) const {} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
virtual void scheduledNode(SUnit *) {} |
| 543 |
|
| 544 |
virtual void unscheduledNode(SUnit *) {} |
| 545 |
|
| 546 |
void setCurCycle(unsigned Cycle) { |
| 547 |
CurCycle = Cycle; |
| 548 |
} |
| 549 |
|
| 550 |
unsigned getCurCycle() const { |
| 551 |
return CurCycle; |
| 552 |
} |
| 553 |
}; |
| 554 |
|
| 555 |
class ScheduleDAG { |
| 556 |
public: |
| 557 |
const LLVMTargetMachine &TM; |
| 558 |
const TargetInstrInfo *TII; |
| 559 |
const TargetRegisterInfo *TRI; |
| 560 |
MachineFunction &MF; |
| 561 |
MachineRegisterInfo &MRI; |
| 562 |
std::vector<SUnit> SUnits; |
| 563 |
SUnit EntrySU; |
| 564 |
SUnit ExitSU; |
| 565 |
|
| 566 |
#ifdef NDEBUG |
| 567 |
static const bool StressSched = false; |
| 568 |
#else |
| 569 |
bool StressSched; |
| 570 |
#endif |
| 571 |
|
| 572 |
explicit ScheduleDAG(MachineFunction &mf); |
| 573 |
|
| 574 |
virtual ~ScheduleDAG(); |
| 575 |
|
| 576 |
|
| 577 |
void clearDAG(); |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
const MCInstrDesc *getInstrDesc(const SUnit *SU) const { |
| 582 |
if (SU->isInstr()) return &SU->getInstr()->getDesc(); |
| 583 |
return getNodeDesc(SU->getNode()); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
virtual void viewGraph(const Twine &Name, const Twine &Title); |
| 588 |
virtual void viewGraph(); |
| 589 |
|
| 590 |
virtual void dumpNode(const SUnit &SU) const = 0; |
| 591 |
virtual void dump() const = 0; |
| 592 |
void dumpNodeName(const SUnit &SU) const; |
| 593 |
|
| 594 |
|
| 595 |
virtual std::string getGraphNodeLabel(const SUnit *SU) const = 0; |
| 596 |
|
| 597 |
|
| 598 |
virtual std::string getDAGName() const = 0; |
| 599 |
|
| 600 |
|
| 601 |
virtual void addCustomGraphFeatures(GraphWriter<ScheduleDAG*> &) const {} |
| 602 |
|
| 603 |
#ifndef NDEBUG |
| 604 |
|
| 605 |
|
| 606 |
unsigned VerifyScheduledDAG(bool isBottomUp); |
| 607 |
#endif |
| 608 |
|
| 609 |
protected: |
| 610 |
void dumpNodeAll(const SUnit &SU) const; |
| 611 |
|
| 612 |
private: |
| 613 |
|
| 614 |
const MCInstrDesc *getNodeDesc(const SDNode *Node) const; |
| 615 |
}; |
| 616 |
|
| 617 |
class SUnitIterator { |
| 618 |
SUnit *Node; |
| 619 |
unsigned Operand; |
| 620 |
|
| 621 |
SUnitIterator(SUnit *N, unsigned Op) : Node(N), Operand(Op) {} |
| 622 |
|
| 623 |
public: |
| 624 |
using iterator_category = std::forward_iterator_tag; |
| 625 |
using value_type = SUnit; |
| 626 |
using difference_type = std::ptrdiff_t; |
| 627 |
using pointer = value_type *; |
| 628 |
using reference = value_type &; |
| 629 |
|
| 630 |
bool operator==(const SUnitIterator& x) const { |
| 631 |
return Operand == x.Operand; |
| 632 |
} |
| 633 |
bool operator!=(const SUnitIterator& x) const { return !operator==(x); } |
| 634 |
|
| 635 |
pointer operator*() const { |
| 636 |
return Node->Preds[Operand].getSUnit(); |
| 637 |
} |
| 638 |
pointer operator->() const { return operator*(); } |
| 639 |
|
| 640 |
SUnitIterator& operator++() { |
| 641 |
++Operand; |
| 642 |
return *this; |
| 643 |
} |
| 644 |
SUnitIterator operator++(int) { |
| 645 |
SUnitIterator tmp = *this; ++*this; return tmp; |
| 646 |
} |
| 647 |
|
| 648 |
static SUnitIterator begin(SUnit *N) { return SUnitIterator(N, 0); } |
| 649 |
static SUnitIterator end (SUnit *N) { |
| 650 |
return SUnitIterator(N, (unsigned)N->Preds.size()); |
| 651 |
} |
| 652 |
|
| 653 |
unsigned getOperand() const { return Operand; } |
| 654 |
const SUnit *getNode() const { return Node; } |
| 655 |
|
| 656 |
|
| 657 |
bool isCtrlDep() const { |
| 658 |
return getSDep().isCtrl(); |
| 659 |
} |
| 660 |
bool isArtificialDep() const { |
| 661 |
return getSDep().isArtificial(); |
| 662 |
} |
| 663 |
const SDep &getSDep() const { |
| 664 |
return Node->Preds[Operand]; |
| 665 |
} |
| 666 |
}; |
| 667 |
|
| 668 |
template <> struct GraphTraits<SUnit*> { |
| 669 |
typedef SUnit *NodeRef; |
| 670 |
typedef SUnitIterator ChildIteratorType; |
| 671 |
static NodeRef getEntryNode(SUnit *N) { return N; } |
| 672 |
static ChildIteratorType child_begin(NodeRef N) { |
| 673 |
return SUnitIterator::begin(N); |
| 674 |
} |
| 675 |
static ChildIteratorType child_end(NodeRef N) { |
| 676 |
return SUnitIterator::end(N); |
| 677 |
} |
| 678 |
}; |
| 679 |
|
| 680 |
template <> struct GraphTraits<ScheduleDAG*> : public GraphTraits<SUnit*> { |
| 681 |
typedef pointer_iterator<std::vector<SUnit>::iterator> nodes_iterator; |
| 682 |
static nodes_iterator nodes_begin(ScheduleDAG *G) { |
| 683 |
return nodes_iterator(G->SUnits.begin()); |
| 684 |
} |
| 685 |
static nodes_iterator nodes_end(ScheduleDAG *G) { |
| 686 |
return nodes_iterator(G->SUnits.end()); |
| 687 |
} |
| 688 |
}; |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
class ScheduleDAGTopologicalSort { |
| 695 |
|
| 696 |
std::vector<SUnit> &SUnits; |
| 697 |
SUnit *ExitSU; |
| 698 |
|
| 699 |
|
| 700 |
bool Dirty = false; |
| 701 |
|
| 702 |
|
| 703 |
SmallVector<std::pair<SUnit *, SUnit *>, 16> Updates; |
| 704 |
|
| 705 |
|
| 706 |
std::vector<int> Index2Node; |
| 707 |
|
| 708 |
std::vector<int> Node2Index; |
| 709 |
|
| 710 |
BitVector Visited; |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
void DFS(const SUnit *SU, int UpperBound, bool& HasLoop); |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
void Shift(BitVector& Visited, int LowerBound, int UpperBound); |
| 720 |
|
| 721 |
|
| 722 |
void Allocate(int n, int index); |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
void FixOrder(); |
| 728 |
|
| 729 |
public: |
| 730 |
ScheduleDAGTopologicalSort(std::vector<SUnit> &SUnits, SUnit *ExitSU); |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
void AddSUnitWithoutPredecessors(const SUnit *SU); |
| 735 |
|
| 736 |
|
| 737 |
void InitDAGTopologicalSorting(); |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
std::vector<int> GetSubGraph(const SUnit &StartSU, const SUnit &TargetSU, |
| 745 |
bool &Success); |
| 746 |
|
| 747 |
|
| 748 |
bool IsReachable(const SUnit *SU, const SUnit *TargetSU); |
| 749 |
|
| 750 |
|
| 751 |
bool WillCreateCycle(SUnit *TargetSU, SUnit *SU); |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
void AddPred(SUnit *Y, SUnit *X); |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
void AddPredQueued(SUnit *Y, SUnit *X); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
void RemovePred(SUnit *M, SUnit *N); |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
void MarkDirty() { Dirty = true; } |
| 769 |
|
| 770 |
typedef std::vector<int>::iterator iterator; |
| 771 |
typedef std::vector<int>::const_iterator const_iterator; |
| 772 |
iterator begin() { return Index2Node.begin(); } |
| 773 |
const_iterator begin() const { return Index2Node.begin(); } |
| 774 |
iterator end() { return Index2Node.end(); } |
| 775 |
const_iterator end() const { return Index2Node.end(); } |
| 776 |
|
| 777 |
typedef std::vector<int>::reverse_iterator reverse_iterator; |
| 778 |
typedef std::vector<int>::const_reverse_iterator const_reverse_iterator; |
| 779 |
reverse_iterator rbegin() { return Index2Node.rbegin(); } |
| 780 |
const_reverse_iterator rbegin() const { return Index2Node.rbegin(); } |
| 781 |
reverse_iterator rend() { return Index2Node.rend(); } |
| 782 |
const_reverse_iterator rend() const { return Index2Node.rend(); } |
| 783 |
}; |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
#endif |