| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef LLVM_CODEGEN_LIVEINTERVAL_H |
| 21 |
#define LLVM_CODEGEN_LIVEINTERVAL_H |
| 22 |
|
| 23 |
#include "llvm/ADT/ArrayRef.h" |
| 24 |
#include "llvm/ADT/IntEqClasses.h" |
| 25 |
#include "llvm/ADT/STLExtras.h" |
| 26 |
#include "llvm/ADT/SmallVector.h" |
| 27 |
#include "llvm/ADT/iterator_range.h" |
| 28 |
#include "llvm/CodeGen/Register.h" |
| 29 |
#include "llvm/CodeGen/SlotIndexes.h" |
| 30 |
#include "llvm/MC/LaneBitmask.h" |
| 31 |
#include "llvm/Support/Allocator.h" |
| 32 |
#include "llvm/Support/MathExtras.h" |
| 33 |
#include <algorithm> |
| 34 |
#include <cassert> |
| 35 |
#include <cstddef> |
| 36 |
#include <functional> |
| 37 |
#include <memory> |
| 38 |
#include <set> |
| 39 |
#include <tuple> |
| 40 |
#include <utility> |
| 41 |
|
| 42 |
namespace llvm { |
| 43 |
|
| 44 |
class CoalescerPair; |
| 45 |
class LiveIntervals; |
| 46 |
class MachineRegisterInfo; |
| 47 |
class raw_ostream; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class VNInfo { |
| 54 |
public: |
| 55 |
using Allocator = BumpPtrAllocator; |
| 56 |
|
| 57 |
|
| 58 |
unsigned id; |
| 59 |
|
| 60 |
|
| 61 |
SlotIndex def; |
| 62 |
|
| 63 |
|
| 64 |
VNInfo(unsigned i, SlotIndex d) : id(i), def(d) {} |
| 65 |
|
| 66 |
|
| 67 |
VNInfo(unsigned i, const VNInfo &orig) : id(i), def(orig.def) {} |
| 68 |
|
| 69 |
|
| 70 |
void copyFrom(VNInfo &src) { |
| 71 |
def = src.def; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
bool isPHIDef() const { return def.isBlock(); } |
| 79 |
|
| 80 |
|
| 81 |
bool isUnused() const { return !def.isValid(); } |
| 82 |
|
| 83 |
|
| 84 |
void markUnused() { def = SlotIndex(); } |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
class LiveQueryResult { |
| 91 |
VNInfo *const EarlyVal; |
| 92 |
VNInfo *const LateVal; |
| 93 |
const SlotIndex EndPoint; |
| 94 |
const bool Kill; |
| 95 |
|
| 96 |
public: |
| 97 |
LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint, |
| 98 |
bool Kill) |
| 99 |
: EarlyVal(EarlyVal), LateVal(LateVal), EndPoint(EndPoint), Kill(Kill) |
| 100 |
{} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
VNInfo *valueIn() const { |
| 106 |
return EarlyVal; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
bool isKill() const { |
| 113 |
return Kill; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
bool isDeadDef() const { |
| 118 |
return EndPoint.isDead(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
VNInfo *valueOut() const { |
| 124 |
return isDeadDef() ? nullptr : LateVal; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
VNInfo *valueOutOrDead() const { |
| 130 |
return LateVal; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
VNInfo *valueDefined() const { |
| 136 |
return EarlyVal == LateVal ? nullptr : LateVal; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
SlotIndex endPoint() const { |
| 148 |
return EndPoint; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
class LiveRange { |
| 158 |
public: |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
struct Segment { |
| 163 |
SlotIndex start; |
| 164 |
SlotIndex end; |
| 165 |
VNInfo *valno = nullptr; |
| 166 |
|
| 167 |
|
| 168 |
Segment() = default; |
| 169 |
|
| 170 |
Segment(SlotIndex S, SlotIndex E, VNInfo *V) |
| 171 |
: start(S), end(E), valno(V) { |
| 172 |
assert(S < E && "Cannot create empty or backwards segment"); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
bool contains(SlotIndex I) const { |
| 177 |
return start <= I && I < end; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
bool containsInterval(SlotIndex S, SlotIndex E) const { |
| 182 |
assert((S < E) && "Backwards interval?"); |
| 183 |
return (start <= S && S < end) && (start < E && E <= end); |
| 184 |
} |
| 185 |
|
| 186 |
bool operator<(const Segment &Other) const { |
| 187 |
return std::tie(start, end) < std::tie(Other.start, Other.end); |
| 188 |
} |
| 189 |
bool operator==(const Segment &Other) const { |
| 190 |
return start == Other.start && end == Other.end; |
| 191 |
} |
| 192 |
|
| 193 |
bool operator!=(const Segment &Other) const { |
| 194 |
return !(*this == Other); |
| 195 |
} |
| 196 |
|
| 197 |
void dump() const; |
| 198 |
}; |
| 199 |
|
| 200 |
using Segments = SmallVector<Segment, 2>; |
| 201 |
using VNInfoList = SmallVector<VNInfo *, 2>; |
| 202 |
|
| 203 |
Segments segments; |
| 204 |
VNInfoList valnos; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
using SegmentSet = std::set<Segment>; |
| 210 |
std::unique_ptr<SegmentSet> segmentSet; |
| 211 |
|
| 212 |
using iterator = Segments::iterator; |
| 213 |
using const_iterator = Segments::const_iterator; |
| 214 |
|
| 215 |
iterator begin() { return segments.begin(); } |
| 216 |
iterator end() { return segments.end(); } |
| 217 |
|
| 218 |
const_iterator begin() const { return segments.begin(); } |
| 219 |
const_iterator end() const { return segments.end(); } |
| 220 |
|
| 221 |
using vni_iterator = VNInfoList::iterator; |
| 222 |
using const_vni_iterator = VNInfoList::const_iterator; |
| 223 |
|
| 224 |
vni_iterator vni_begin() { return valnos.begin(); } |
| 225 |
vni_iterator vni_end() { return valnos.end(); } |
| 226 |
|
| 227 |
const_vni_iterator vni_begin() const { return valnos.begin(); } |
| 228 |
const_vni_iterator vni_end() const { return valnos.end(); } |
| 229 |
|
| 230 |
|
| 231 |
LiveRange(bool UseSegmentSet = false) |
| 232 |
: segmentSet(UseSegmentSet ? std::make_unique<SegmentSet>() |
| 233 |
: nullptr) {} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
LiveRange(const LiveRange &Other, BumpPtrAllocator &Allocator) { |
| 238 |
assert(Other.segmentSet == nullptr && |
| 239 |
"Copying of LiveRanges with active SegmentSets is not supported"); |
| 240 |
assign(Other, Allocator); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
void assign(const LiveRange &Other, BumpPtrAllocator &Allocator) { |
| 245 |
if (this == &Other) |
| 246 |
return; |
| 247 |
|
| 248 |
assert(Other.segmentSet == nullptr && |
| 249 |
"Copying of LiveRanges with active SegmentSets is not supported"); |
| 250 |
|
| 251 |
for (const VNInfo *VNI : Other.valnos) |
| 252 |
createValueCopy(VNI, Allocator); |
| 253 |
|
| 254 |
for (const Segment &S : Other.segments) |
| 255 |
segments.push_back(Segment(S.start, S.end, valnos[S.valno->id])); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
iterator advanceTo(iterator I, SlotIndex Pos) { |
| 264 |
assert(I != end()); |
| 265 |
if (Pos >= endIndex()) |
| 266 |
return end(); |
| 267 |
while (I->end <= Pos) ++I; |
| 268 |
return I; |
| 269 |
} |
| 270 |
|
| 271 |
const_iterator advanceTo(const_iterator I, SlotIndex Pos) const { |
| 272 |
assert(I != end()); |
| 273 |
if (Pos >= endIndex()) |
| 274 |
return end(); |
| 275 |
while (I->end <= Pos) ++I; |
| 276 |
return I; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
iterator find(SlotIndex Pos); |
| 287 |
|
| 288 |
const_iterator find(SlotIndex Pos) const { |
| 289 |
return const_cast<LiveRange*>(this)->find(Pos); |
| 290 |
} |
| 291 |
|
| 292 |
void clear() { |
| 293 |
valnos.clear(); |
| 294 |
segments.clear(); |
| 295 |
} |
| 296 |
|
| 297 |
size_t size() const { |
| 298 |
return segments.size(); |
| 299 |
} |
| 300 |
|
| 301 |
bool hasAtLeastOneValue() const { return !valnos.empty(); } |
| 302 |
|
| 303 |
bool containsOneValue() const { return valnos.size() == 1; } |
| 304 |
|
| 305 |
unsigned getNumValNums() const { return (unsigned)valnos.size(); } |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
inline VNInfo *getValNumInfo(unsigned ValNo) { |
| 310 |
return valnos[ValNo]; |
| 311 |
} |
| 312 |
inline const VNInfo *getValNumInfo(unsigned ValNo) const { |
| 313 |
return valnos[ValNo]; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
bool containsValue(const VNInfo *VNI) const { |
| 318 |
return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) { |
| 324 |
VNInfo *VNI = |
| 325 |
new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def); |
| 326 |
valnos.push_back(VNI); |
| 327 |
return VNI; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
VNInfo *createDeadDef(VNInfo *VNI); |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
VNInfo *createValueCopy(const VNInfo *orig, |
| 342 |
VNInfo::Allocator &VNInfoAllocator) { |
| 343 |
VNInfo *VNI = |
| 344 |
new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig); |
| 345 |
valnos.push_back(VNI); |
| 346 |
return VNI; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
void RenumberValues(); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
void MergeValueInAsValue(const LiveRange &RHS, |
| 372 |
const VNInfo *RHSValNo, VNInfo *LHSValNo); |
| 373 |
|
| 374 |
bool empty() const { return segments.empty(); } |
| 375 |
|
| 376 |
|
| 377 |
SlotIndex beginIndex() const { |
| 378 |
assert(!empty() && "Call to beginIndex() on empty range."); |
| 379 |
return segments.front().start; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
SlotIndex endIndex() const { |
| 385 |
assert(!empty() && "Call to endIndex() on empty range."); |
| 386 |
return segments.back().end; |
| 387 |
} |
| 388 |
|
| 389 |
bool expiredAt(SlotIndex index) const { |
| 390 |
return index >= endIndex(); |
| 391 |
} |
| 392 |
|
| 393 |
bool liveAt(SlotIndex index) const { |
| 394 |
const_iterator r = find(index); |
| 395 |
return r != end() && r->start <= index; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
const Segment *getSegmentContaining(SlotIndex Idx) const { |
| 401 |
const_iterator I = FindSegmentContaining(Idx); |
| 402 |
return I == end() ? nullptr : &*I; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
Segment *getSegmentContaining(SlotIndex Idx) { |
| 408 |
iterator I = FindSegmentContaining(Idx); |
| 409 |
return I == end() ? nullptr : &*I; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
VNInfo *getVNInfoAt(SlotIndex Idx) const { |
| 414 |
const_iterator I = FindSegmentContaining(Idx); |
| 415 |
return I == end() ? nullptr : I->valno; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
VNInfo *getVNInfoBefore(SlotIndex Idx) const { |
| 422 |
const_iterator I = FindSegmentContaining(Idx.getPrevSlot()); |
| 423 |
return I == end() ? nullptr : I->valno; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
iterator FindSegmentContaining(SlotIndex Idx) { |
| 429 |
iterator I = find(Idx); |
| 430 |
return I != end() && I->start <= Idx ? I : end(); |
| 431 |
} |
| 432 |
|
| 433 |
const_iterator FindSegmentContaining(SlotIndex Idx) const { |
| 434 |
const_iterator I = find(Idx); |
| 435 |
return I != end() && I->start <= Idx ? I : end(); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
bool overlaps(const LiveRange &other) const { |
| 441 |
if (other.empty()) |
| 442 |
return false; |
| 443 |
return overlapsFrom(other, other.begin()); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
bool overlaps(const LiveRange &Other, const CoalescerPair &CP, |
| 452 |
const SlotIndexes&) const; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
bool overlaps(SlotIndex Start, SlotIndex End) const; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
bool overlapsFrom(const LiveRange &Other, const_iterator StartPos) const; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
bool covers(const LiveRange &Other) const; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
iterator addSegment(Segment S); |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs, |
| 489 |
SlotIndex StartIdx, SlotIndex Kill); |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill); |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
void join(LiveRange &Other, |
| 502 |
const int *ValNoAssignments, |
| 503 |
const int *RHSValNoAssignments, |
| 504 |
SmallVectorImpl<VNInfo *> &NewVNInfo); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
bool isLocal(SlotIndex Start, SlotIndex End) const { |
| 511 |
return beginIndex() > Start.getBaseIndex() && |
| 512 |
endIndex() < End.getBoundaryIndex(); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
void removeSegment(SlotIndex Start, SlotIndex End, |
| 518 |
bool RemoveDeadValNo = false); |
| 519 |
|
| 520 |
void removeSegment(Segment S, bool RemoveDeadValNo = false) { |
| 521 |
removeSegment(S.start, S.end, RemoveDeadValNo); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
iterator removeSegment(iterator I, bool RemoveDeadValNo = false); |
| 526 |
|
| 527 |
|
| 528 |
void removeValNoIfDead(VNInfo *ValNo); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
LiveQueryResult Query(SlotIndex Idx) const { |
| 534 |
|
| 535 |
const_iterator I = find(Idx.getBaseIndex()); |
| 536 |
const_iterator E = end(); |
| 537 |
if (I == E) |
| 538 |
return LiveQueryResult(nullptr, nullptr, SlotIndex(), false); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
VNInfo *EarlyVal = nullptr; |
| 544 |
VNInfo *LateVal = nullptr; |
| 545 |
SlotIndex EndPoint; |
| 546 |
bool Kill = false; |
| 547 |
if (I->start <= Idx.getBaseIndex()) { |
| 548 |
EarlyVal = I->valno; |
| 549 |
EndPoint = I->end; |
| 550 |
|
| 551 |
if (SlotIndex::isSameInstr(Idx, I->end)) { |
| 552 |
Kill = true; |
| 553 |
if (++I == E) |
| 554 |
return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if (EarlyVal->def == Idx.getBaseIndex()) |
| 561 |
EarlyVal = nullptr; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
if (!SlotIndex::isEarlierInstr(Idx, I->start)) { |
| 566 |
LateVal = I->valno; |
| 567 |
EndPoint = I->end; |
| 568 |
} |
| 569 |
return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
void removeValNo(VNInfo *ValNo); |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
bool isZeroLength(SlotIndexes *Indexes) const { |
| 579 |
for (const Segment &S : segments) |
| 580 |
if (Indexes->getNextNonNullIndex(S.start).getBaseIndex() < |
| 581 |
S.end.getBaseIndex()) |
| 582 |
return false; |
| 583 |
return true; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
bool isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const; |
| 590 |
|
| 591 |
bool operator<(const LiveRange& other) const { |
| 592 |
const SlotIndex &thisIndex = beginIndex(); |
| 593 |
const SlotIndex &otherIndex = other.beginIndex(); |
| 594 |
return thisIndex < otherIndex; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
bool isUndefIn(ArrayRef<SlotIndex> Undefs, SlotIndex Begin, |
| 600 |
SlotIndex End) const { |
| 601 |
return llvm::any_of(Undefs, [Begin, End](SlotIndex Idx) -> bool { |
| 602 |
return Begin <= Idx && Idx < End; |
| 603 |
}); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
void flushSegmentSet(); |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
template <typename Range, typename OutputIt> |
| 619 |
bool findIndexesLiveAt(Range &&R, OutputIt O) const { |
| 620 |
assert(llvm::is_sorted(R)); |
| 621 |
auto Idx = R.begin(), EndIdx = R.end(); |
| 622 |
auto Seg = segments.begin(), EndSeg = segments.end(); |
| 623 |
bool Found = false; |
| 624 |
while (Idx != EndIdx && Seg != EndSeg) { |
| 625 |
|
| 626 |
|
| 627 |
if (Seg->end <= *Idx) { |
| 628 |
Seg = std::upper_bound( |
| 629 |
++Seg, EndSeg, *Idx, |
| 630 |
[=](std::remove_reference_t<decltype(*Idx)> V, |
| 631 |
const std::remove_reference_t<decltype(*Seg)> &S) { |
| 632 |
return V < S.end; |
| 633 |
}); |
| 634 |
if (Seg == EndSeg) |
| 635 |
break; |
| 636 |
} |
| 637 |
auto NotLessStart = std::lower_bound(Idx, EndIdx, Seg->start); |
| 638 |
if (NotLessStart == EndIdx) |
| 639 |
break; |
| 640 |
auto NotLessEnd = std::lower_bound(NotLessStart, EndIdx, Seg->end); |
| 641 |
if (NotLessEnd != NotLessStart) { |
| 642 |
Found = true; |
| 643 |
O = std::copy(NotLessStart, NotLessEnd, O); |
| 644 |
} |
| 645 |
Idx = NotLessEnd; |
| 646 |
++Seg; |
| 647 |
} |
| 648 |
return Found; |
| 649 |
} |
| 650 |
|
| 651 |
void print(raw_ostream &OS) const; |
| 652 |
void dump() const; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
#ifdef NDEBUG |
| 658 |
void verify() const {} |
| 659 |
#else |
| 660 |
void verify() const; |
| 661 |
#endif |
| 662 |
|
| 663 |
protected: |
| 664 |
|
| 665 |
void append(const LiveRange::Segment S); |
| 666 |
|
| 667 |
private: |
| 668 |
friend class LiveRangeUpdater; |
| 669 |
void addSegmentToSet(Segment S); |
| 670 |
void markValNoForDeletion(VNInfo *V); |
| 671 |
}; |
| 672 |
|
| 673 |
inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) { |
| 674 |
LR.print(OS); |
| 675 |
return OS; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
class LiveInterval : public LiveRange { |
| 681 |
public: |
| 682 |
using super = LiveRange; |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
class SubRange : public LiveRange { |
| 688 |
public: |
| 689 |
SubRange *Next = nullptr; |
| 690 |
LaneBitmask LaneMask; |
| 691 |
|
| 692 |
|
| 693 |
SubRange(LaneBitmask LaneMask) : LaneMask(LaneMask) {} |
| 694 |
|
| 695 |
|
| 696 |
SubRange(LaneBitmask LaneMask, const LiveRange &Other, |
| 697 |
BumpPtrAllocator &Allocator) |
| 698 |
: LiveRange(Other, Allocator), LaneMask(LaneMask) {} |
| 699 |
|
| 700 |
void print(raw_ostream &OS) const; |
| 701 |
void dump() const; |
| 702 |
}; |
| 703 |
|
| 704 |
private: |
| 705 |
SubRange *SubRanges = nullptr; |
| 706 |
|
| 707 |
const Register Reg; |
| 708 |
float Weight = 0.0; |
| 709 |
|
| 710 |
public: |
| 711 |
Register reg() const { return Reg; } |
| 712 |
float weight() const { return Weight; } |
| 713 |
void incrementWeight(float Inc) { Weight += Inc; } |
| 714 |
void setWeight(float Value) { Weight = Value; } |
| 715 |
|
| 716 |
LiveInterval(unsigned Reg, float Weight) : Reg(Reg), Weight(Weight) {} |
| 717 |
|
| 718 |
~LiveInterval() { |
| 719 |
clearSubRanges(); |
| 720 |
} |
| 721 |
|
| 722 |
template<typename T> |
| 723 |
class SingleLinkedListIterator { |
| 724 |
T *P; |
| 725 |
|
| 726 |
public: |
| 727 |
SingleLinkedListIterator(T *P) : P(P) {} |
| 728 |
|
| 729 |
SingleLinkedListIterator<T> &operator++() { |
| 730 |
P = P->Next; |
| 731 |
return *this; |
| 732 |
} |
| 733 |
SingleLinkedListIterator<T> operator++(int) { |
| 734 |
SingleLinkedListIterator res = *this; |
| 735 |
++*this; |
| 736 |
return res; |
| 737 |
} |
| 738 |
bool operator!=(const SingleLinkedListIterator<T> &Other) const { |
| 739 |
return P != Other.operator->(); |
| 740 |
} |
| 741 |
bool operator==(const SingleLinkedListIterator<T> &Other) const { |
| 742 |
return P == Other.operator->(); |
| 743 |
} |
| 744 |
T &operator*() const { |
| 745 |
return *P; |
| 746 |
} |
| 747 |
T *operator->() const { |
| 748 |
return P; |
| 749 |
} |
| 750 |
}; |
| 751 |
|
| 752 |
using subrange_iterator = SingleLinkedListIterator<SubRange>; |
| 753 |
using const_subrange_iterator = SingleLinkedListIterator<const SubRange>; |
| 754 |
|
| 755 |
subrange_iterator subrange_begin() { |
| 756 |
return subrange_iterator(SubRanges); |
| 757 |
} |
| 758 |
subrange_iterator subrange_end() { |
| 759 |
return subrange_iterator(nullptr); |
| 760 |
} |
| 761 |
|
| 762 |
const_subrange_iterator subrange_begin() const { |
| 763 |
return const_subrange_iterator(SubRanges); |
| 764 |
} |
| 765 |
const_subrange_iterator subrange_end() const { |
| 766 |
return const_subrange_iterator(nullptr); |
| 767 |
} |
| 768 |
|
| 769 |
iterator_range<subrange_iterator> subranges() { |
| 770 |
return make_range(subrange_begin(), subrange_end()); |
| 771 |
} |
| 772 |
|
| 773 |
iterator_range<const_subrange_iterator> subranges() const { |
| 774 |
return make_range(subrange_begin(), subrange_end()); |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
SubRange *createSubRange(BumpPtrAllocator &Allocator, |
| 780 |
LaneBitmask LaneMask) { |
| 781 |
SubRange *Range = new (Allocator) SubRange(LaneMask); |
| 782 |
appendSubRange(Range); |
| 783 |
return Range; |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
SubRange *createSubRangeFrom(BumpPtrAllocator &Allocator, |
| 789 |
LaneBitmask LaneMask, |
| 790 |
const LiveRange &CopyFrom) { |
| 791 |
SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator); |
| 792 |
appendSubRange(Range); |
| 793 |
return Range; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
bool hasSubRanges() const { |
| 798 |
return SubRanges != nullptr; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
void clearSubRanges(); |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
void removeEmptySubRanges(); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
unsigned getSize() const; |
| 811 |
|
| 812 |
|
| 813 |
bool isSpillable() const { return Weight != huge_valf; } |
| 814 |
|
| 815 |
|
| 816 |
void markNotSpillable() { Weight = huge_valf; } |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
void computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs, |
| 821 |
LaneBitmask LaneMask, |
| 822 |
const MachineRegisterInfo &MRI, |
| 823 |
const SlotIndexes &Indexes) const; |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, |
| 865 |
std::function<void(LiveInterval::SubRange &)> Apply, |
| 866 |
const SlotIndexes &Indexes, |
| 867 |
const TargetRegisterInfo &TRI, |
| 868 |
unsigned ComposeSubRegIdx = 0); |
| 869 |
|
| 870 |
bool operator<(const LiveInterval& other) const { |
| 871 |
const SlotIndex &thisIndex = beginIndex(); |
| 872 |
const SlotIndex &otherIndex = other.beginIndex(); |
| 873 |
return std::tie(thisIndex, Reg) < std::tie(otherIndex, other.Reg); |
| 874 |
} |
| 875 |
|
| 876 |
void print(raw_ostream &OS) const; |
| 877 |
void dump() const; |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
#ifdef NDEBUG |
| 883 |
void verify(const MachineRegisterInfo *MRI = nullptr) const {} |
| 884 |
#else |
| 885 |
void verify(const MachineRegisterInfo *MRI = nullptr) const; |
| 886 |
#endif |
| 887 |
|
| 888 |
private: |
| 889 |
|
| 890 |
void appendSubRange(SubRange *Range) { |
| 891 |
Range->Next = SubRanges; |
| 892 |
SubRanges = Range; |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
void freeSubRange(SubRange *S); |
| 897 |
}; |
| 898 |
|
| 899 |
inline raw_ostream &operator<<(raw_ostream &OS, |
| 900 |
const LiveInterval::SubRange &SR) { |
| 901 |
SR.print(OS); |
| 902 |
return OS; |
| 903 |
} |
| 904 |
|
| 905 |
inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) { |
| 906 |
LI.print(OS); |
| 907 |
return OS; |
| 908 |
} |
| 909 |
|
| 910 |
raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S); |
| 911 |
|
| 912 |
inline bool operator<(SlotIndex V, const LiveRange::Segment &S) { |
| 913 |
return V < S.start; |
| 914 |
} |
| 915 |
|
| 916 |
inline bool operator<(const LiveRange::Segment &S, SlotIndex V) { |
| 917 |
return S.start < V; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
class LiveRangeUpdater { |
| 929 |
LiveRange *LR; |
| 930 |
SlotIndex LastStart; |
| 931 |
LiveRange::iterator WriteI; |
| 932 |
LiveRange::iterator ReadI; |
| 933 |
SmallVector<LiveRange::Segment, 16> Spills; |
| 934 |
void mergeSpills(); |
| 935 |
|
| 936 |
public: |
| 937 |
|
| 938 |
|
| 939 |
LiveRangeUpdater(LiveRange *lr = nullptr) : LR(lr) {} |
| 940 |
|
| 941 |
~LiveRangeUpdater() { flush(); } |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
void add(LiveRange::Segment); |
| 947 |
|
| 948 |
void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) { |
| 949 |
add(LiveRange::Segment(Start, End, VNI)); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
bool isDirty() const { return LastStart.isValid(); } |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
void flush(); |
| 959 |
|
| 960 |
|
| 961 |
void setDest(LiveRange *lr) { |
| 962 |
if (LR != lr && isDirty()) |
| 963 |
flush(); |
| 964 |
LR = lr; |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
LiveRange *getDest() const { return LR; } |
| 969 |
|
| 970 |
void dump() const; |
| 971 |
void print(raw_ostream&) const; |
| 972 |
}; |
| 973 |
|
| 974 |
inline raw_ostream &operator<<(raw_ostream &OS, const LiveRangeUpdater &X) { |
| 975 |
X.print(OS); |
| 976 |
return OS; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
class ConnectedVNInfoEqClasses { |
| 993 |
LiveIntervals &LIS; |
| 994 |
IntEqClasses EqClass; |
| 995 |
|
| 996 |
public: |
| 997 |
explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
unsigned Classify(const LiveRange &LR); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; } |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
void Distribute(LiveInterval &LI, LiveInterval *LIV[], |
| 1012 |
MachineRegisterInfo &MRI); |
| 1013 |
}; |
| 1014 |
|
| 1015 |
} |
| 1016 |
|
| 1017 |
#endif |