| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef jit_BacktrackingAllocator_h |
| 8 |
#define jit_BacktrackingAllocator_h |
| 9 |
|
| 10 |
#include "mozilla/Array.h" |
| 11 |
#include "mozilla/Atomics.h" |
| 12 |
#include "mozilla/Attributes.h" |
| 13 |
#include "mozilla/Maybe.h" |
| 14 |
|
| 15 |
#include "ds/AvlTree.h" |
| 16 |
#include "ds/PriorityQueue.h" |
| 17 |
#include "jit/RegisterAllocator.h" |
| 18 |
#include "jit/SparseBitSet.h" |
| 19 |
#include "jit/StackSlotAllocator.h" |
| 20 |
|
| 21 |
// Gives better traces in Nightly/debug builds (could be EARLY_BETA_OR_EARLIER) |
| 22 |
#if defined(NIGHTLY_BUILD) || defined(DEBUG) |
| 23 |
# define AVOID_INLINE_FOR_DEBUGGING MOZ_NEVER_INLINE |
| 24 |
#else |
| 25 |
# define AVOID_INLINE_FOR_DEBUGGING |
| 26 |
#endif |
| 27 |
|
| 28 |
// Backtracking priority queue based register allocator based on that described |
| 29 |
// in the following blog post: |
| 30 |
// |
| 31 |
// http://blog.llvm.org/2011/09/greedy-register-allocation-in-llvm-30.html |
| 32 |
|
| 33 |
namespace js { |
| 34 |
namespace jit { |
| 35 |
|
| 36 |
class Requirement { |
| 37 |
public: |
| 38 |
enum Kind { NONE, REGISTER, FIXED }; |
| 39 |
|
| 40 |
Requirement() : kind_(NONE) {} |
| 41 |
|
| 42 |
explicit Requirement(Kind kind) : kind_(kind) { |
| 43 |
// FIXED has a dedicated constructor. |
| 44 |
MOZ_ASSERT(kind != FIXED); |
| 45 |
} |
| 46 |
|
| 47 |
explicit Requirement(LAllocation fixed) : kind_(FIXED), allocation_(fixed) { |
| 48 |
MOZ_ASSERT(!fixed.isBogus() && !fixed.isUse()); |
| 49 |
} |
| 50 |
|
| 51 |
Kind kind() const { return kind_; } |
| 52 |
|
| 53 |
LAllocation allocation() const { |
| 54 |
MOZ_ASSERT(!allocation_.isBogus() && !allocation_.isUse()); |
| 55 |
return allocation_; |
| 56 |
} |
| 57 |
|
| 58 |
[[nodiscard]] bool merge(const Requirement& newRequirement) { |
| 59 |
// Merge newRequirement with any existing requirement, returning false |
| 60 |
// if the new and old requirements conflict. |
| 61 |
|
| 62 |
if (newRequirement.kind() == Requirement::FIXED) { |
| 63 |
if (kind() == Requirement::FIXED) { |
| 64 |
return newRequirement.allocation() == allocation(); |
| 65 |
} |
| 66 |
*this = newRequirement; |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
MOZ_ASSERT(newRequirement.kind() == Requirement::REGISTER); |
| 71 |
if (kind() == Requirement::FIXED) { |
| 72 |
return allocation().isAnyRegister(); |
| 73 |
} |
| 74 |
|
| 75 |
*this = newRequirement; |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
Kind kind_; |
| 81 |
LAllocation allocation_; |
| 82 |
}; |
| 83 |
|
| 84 |
struct UsePosition : public TempObject, |
| 85 |
public InlineForwardListNode<UsePosition> { |
| 86 |
private: |
| 87 |
// A UsePosition is an LUse* with a CodePosition. UsePosition also has an |
| 88 |
// optimization that allows access to the associated LUse::Policy without |
| 89 |
// dereferencing memory: the policy is encoded in the low bits of the LUse*. |
| 90 |
// |
| 91 |
// Note however that because LUse* is uintptr_t-aligned, on 32-bit systems |
| 92 |
// there are only 4 encodable values, for more than 4 use policies; in that |
| 93 |
// case we allocate the common LUse::ANY, LUse::REGISTER, and LUse::FIXED use |
| 94 |
// policies to tags, and use tag 0x3 to indicate that dereferencing the LUse |
| 95 |
// is necessary to get the policy (KEEPALIVE or STACK, in that case). |
| 96 |
uintptr_t use_; |
| 97 |
static_assert(LUse::ANY < 0x3, |
| 98 |
"LUse::ANY can be represented in low tag on 32-bit systems"); |
| 99 |
static_assert(LUse::REGISTER < 0x3, |
| 100 |
"LUse::REGISTER can be represented in tag on 32-bit systems"); |
| 101 |
static_assert(LUse::FIXED < 0x3, |
| 102 |
"LUse::FIXED can be represented in tag on 32-bit systems"); |
| 103 |
|
| 104 |
static constexpr uintptr_t PolicyMask = sizeof(uintptr_t) - 1; |
| 105 |
static constexpr uintptr_t UseMask = ~PolicyMask; |
| 106 |
|
| 107 |
void setUse(LUse* use) { |
| 108 |
// RECOVERED_INPUT is used by snapshots and ignored when building the |
| 109 |
// liveness information. Thus we can safely assume that no such value |
| 110 |
// would be seen. |
| 111 |
MOZ_ASSERT(use->policy() != LUse::RECOVERED_INPUT); |
| 112 |
|
| 113 |
uintptr_t policyBits = use->policy(); |
| 114 |
#ifndef JS_64BIT |
| 115 |
// On a 32-bit machine, LUse::KEEPALIVE and LUse::STACK are accessed by |
| 116 |
// dereferencing the use pointer. |
| 117 |
if (policyBits >= PolicyMask) { |
| 118 |
policyBits = PolicyMask; |
| 119 |
} |
| 120 |
#endif |
| 121 |
use_ = uintptr_t(use) | policyBits; |
| 122 |
MOZ_ASSERT(use->policy() == usePolicy()); |
| 123 |
} |
| 124 |
|
| 125 |
public: |
| 126 |
CodePosition pos; |
| 127 |
|
| 128 |
LUse* use() const { return reinterpret_cast<LUse*>(use_ & UseMask); } |
| 129 |
|
| 130 |
LUse::Policy usePolicy() const { |
| 131 |
uintptr_t bits = use_ & PolicyMask; |
| 132 |
#ifndef JS_64BIT |
| 133 |
// On 32-bit machines, reach out to memory if it's LUse::KEEPALIVE or |
| 134 |
// LUse::STACK. |
| 135 |
if (bits == PolicyMask) { |
| 136 |
return use()->policy(); |
| 137 |
} |
| 138 |
#endif |
| 139 |
LUse::Policy policy = LUse::Policy(bits); |
| 140 |
MOZ_ASSERT(use()->policy() == policy); |
| 141 |
return policy; |
| 142 |
} |
| 143 |
|
| 144 |
UsePosition(LUse* use, CodePosition pos) : pos(pos) { |
| 145 |
// Verify that the usedAtStart() flag is consistent with the |
| 146 |
// subposition. For now ignore fixed registers, because they |
| 147 |
// are handled specially around calls. |
| 148 |
MOZ_ASSERT_IF(!use->isFixedRegister(), |
| 149 |
pos.subpos() == (use->usedAtStart() ? CodePosition::INPUT |
| 150 |
: CodePosition::OUTPUT)); |
| 151 |
setUse(use); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
using UsePositionIterator = InlineForwardListIterator<UsePosition>; |
| 156 |
|
| 157 |
// Backtracking allocator data structures overview. |
| 158 |
// |
| 159 |
// LiveRange: A continuous range of positions where a virtual register is live. |
| 160 |
// LiveBundle: A set of LiveRanges which do not overlap. |
| 161 |
// VirtualRegister: A set of all LiveRanges used for some LDefinition. |
| 162 |
// |
| 163 |
// The allocator first performs a liveness ananlysis on the LIR graph which |
| 164 |
// constructs LiveRanges for each VirtualRegister, determining where the |
| 165 |
// registers are live. |
| 166 |
// |
| 167 |
// The ranges are then bundled together according to heuristics, and placed on |
| 168 |
// the allocation queue. |
| 169 |
// |
| 170 |
// As bundles are removed from the allocation queue, we attempt to find a |
| 171 |
// physical register or stack slot allocation for all ranges in the removed |
| 172 |
// bundle, possibly evicting already-allocated bundles. See processBundle() |
| 173 |
// for details. |
| 174 |
// |
| 175 |
// If we are not able to allocate a bundle, it is split according to heuristics |
| 176 |
// into two or more smaller bundles which cover all the ranges of the original. |
| 177 |
// These smaller bundles are then allocated independently. |
| 178 |
|
| 179 |
class LiveBundle; |
| 180 |
class VirtualRegister; |
| 181 |
|
| 182 |
class LiveRange : public TempObject, public InlineForwardListNode<LiveRange> { |
| 183 |
public: |
| 184 |
struct Range { |
| 185 |
// The beginning of this range, inclusive. |
| 186 |
CodePosition from; |
| 187 |
|
| 188 |
// The end of this range, exclusive. |
| 189 |
CodePosition to; |
| 190 |
|
| 191 |
Range() = default; |
| 192 |
|
| 193 |
Range(CodePosition from, CodePosition to) : from(from), to(to) { |
| 194 |
MOZ_ASSERT(!empty()); |
| 195 |
} |
| 196 |
|
| 197 |
bool empty() { |
| 198 |
MOZ_ASSERT(from <= to); |
| 199 |
return from == to; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
private: |
| 204 |
// The virtual register this range is for, or nullptr if this does not have a |
| 205 |
// virtual register (for example, it is in the callRanges bundle). |
| 206 |
VirtualRegister* vreg_; |
| 207 |
|
| 208 |
// The bundle containing this range, null if liveness information is being |
| 209 |
// constructed and we haven't started allocating bundles yet. |
| 210 |
LiveBundle* bundle_; |
| 211 |
|
| 212 |
// The code positions in this range. |
| 213 |
Range range_; |
| 214 |
|
| 215 |
// All uses of the virtual register in this range, ordered by location. |
| 216 |
InlineForwardList<UsePosition> uses_; |
| 217 |
|
| 218 |
// Total spill weight that calculate from all the uses' policy. Because the |
| 219 |
// use's policy can't be changed after initialization, we can update the |
| 220 |
// weight whenever a use is added to or remove from this range. This way, we |
| 221 |
// don't need to iterate all the uses every time computeSpillWeight() is |
| 222 |
// called. |
| 223 |
size_t usesSpillWeight_; |
| 224 |
|
| 225 |
// Number of uses that have policy LUse::FIXED. |
| 226 |
uint32_t numFixedUses_; |
| 227 |
|
| 228 |
// Whether this range contains the virtual register's definition. |
| 229 |
bool hasDefinition_; |
| 230 |
|
| 231 |
LiveRange(VirtualRegister* vreg, Range range) |
| 232 |
: vreg_(vreg), |
| 233 |
bundle_(nullptr), |
| 234 |
range_(range), |
| 235 |
usesSpillWeight_(0), |
| 236 |
numFixedUses_(0), |
| 237 |
hasDefinition_(false) |
| 238 |
|
| 239 |
{ |
| 240 |
MOZ_ASSERT(!range.empty()); |
| 241 |
} |
| 242 |
|
| 243 |
void noteAddedUse(UsePosition* use); |
| 244 |
void noteRemovedUse(UsePosition* use); |
| 245 |
|
| 246 |
public: |
| 247 |
static LiveRange* FallibleNew(TempAllocator& alloc, VirtualRegister* vreg, |
| 248 |
CodePosition from, CodePosition to) { |
| 249 |
return new (alloc.fallible()) LiveRange(vreg, Range(from, to)); |
| 250 |
} |
| 251 |
|
| 252 |
VirtualRegister& vreg() const { |
| 253 |
MOZ_ASSERT(hasVreg()); |
| 254 |
return *vreg_; |
| 255 |
} |
| 256 |
bool hasVreg() const { return vreg_ != nullptr; } |
| 257 |
|
| 258 |
LiveBundle* bundle() const { return bundle_; } |
| 259 |
|
| 260 |
CodePosition from() const { return range_.from; } |
| 261 |
CodePosition to() const { return range_.to; } |
| 262 |
bool covers(CodePosition pos) const { return pos >= from() && pos < to(); } |
| 263 |
|
| 264 |
// Whether this range wholly contains other. |
| 265 |
bool contains(LiveRange* other) const; |
| 266 |
|
| 267 |
// Intersect this range with other, returning the subranges of this |
| 268 |
// that are before, inside, or after other. |
| 269 |
void intersect(LiveRange* other, Range* pre, Range* inside, |
| 270 |
Range* post) const; |
| 271 |
|
| 272 |
// Whether this range has any intersection with other. |
| 273 |
bool intersects(LiveRange* other) const; |
| 274 |
|
| 275 |
UsePositionIterator usesBegin() const { return uses_.begin(); } |
| 276 |
UsePosition* lastUse() const { return uses_.back(); } |
| 277 |
bool hasUses() const { return !!usesBegin(); } |
| 278 |
UsePosition* popUse(); |
| 279 |
|
| 280 |
bool hasDefinition() const { return hasDefinition_; } |
| 281 |
|
| 282 |
void setFrom(CodePosition from) { |
| 283 |
range_.from = from; |
| 284 |
MOZ_ASSERT(!range_.empty()); |
| 285 |
} |
| 286 |
void setTo(CodePosition to) { |
| 287 |
range_.to = to; |
| 288 |
MOZ_ASSERT(!range_.empty()); |
| 289 |
} |
| 290 |
|
| 291 |
void setBundle(LiveBundle* bundle) { bundle_ = bundle; } |
| 292 |
|
| 293 |
void addUse(UsePosition* use); |
| 294 |
|
| 295 |
void tryToMoveDefAndUsesInto(LiveRange* other); |
| 296 |
void moveAllUsesToTheEndOf(LiveRange* other); |
| 297 |
|
| 298 |
void setHasDefinition() { |
| 299 |
MOZ_ASSERT(!hasDefinition_); |
| 300 |
hasDefinition_ = true; |
| 301 |
} |
| 302 |
|
| 303 |
size_t usesSpillWeight() { return usesSpillWeight_; } |
| 304 |
uint32_t numFixedUses() { return numFixedUses_; } |
| 305 |
|
| 306 |
#ifdef JS_JITSPEW |
| 307 |
// Return a string describing this range. |
| 308 |
UniqueChars toString() const; |
| 309 |
#endif |
| 310 |
|
| 311 |
// Comparator for use in AVL trees. |
| 312 |
static int compare(LiveRange* v0, LiveRange* v1) { |
| 313 |
// The denoted range includes 'from' but excludes 'to'. |
| 314 |
if (v0->to() <= v1->from()) { |
| 315 |
return -1; |
| 316 |
} |
| 317 |
if (v0->from() >= v1->to()) { |
| 318 |
return 1; |
| 319 |
} |
| 320 |
return 0; |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
// LiveRangePlus is a simple wrapper around a LiveRange*. It caches the |
| 325 |
// LiveRange*'s `.range_.from` and `.range_.to` CodePositions. The only |
| 326 |
// purpose of this is to avoid some cache misses that would otherwise occur |
| 327 |
// when comparing those fields in an AvlTree<LiveRange*, ..>. This measurably |
| 328 |
// speeds up the allocator in some cases. See bug 1814204. |
| 329 |
|
| 330 |
class LiveRangePlus { |
| 331 |
// The LiveRange we're wrapping. |
| 332 |
LiveRange* liveRange_; |
| 333 |
// Cached versions of liveRange_->range_.from and lr->range_.to |
| 334 |
CodePosition from_; |
| 335 |
CodePosition to_; |
| 336 |
|
| 337 |
public: |
| 338 |
explicit LiveRangePlus(LiveRange* lr) |
| 339 |
: liveRange_(lr), from_(lr->from()), to_(lr->to()) {} |
| 340 |
LiveRangePlus() : liveRange_(nullptr) {} |
| 341 |
~LiveRangePlus() { |
| 342 |
MOZ_ASSERT(liveRange_ ? from_ == liveRange_->from() |
| 343 |
: from_ == CodePosition()); |
| 344 |
MOZ_ASSERT(liveRange_ ? to_ == liveRange_->to() : to_ == CodePosition()); |
| 345 |
} |
| 346 |
|
| 347 |
LiveRange* liveRange() const { return liveRange_; } |
| 348 |
|
| 349 |
// Comparator for use in AVL trees. |
| 350 |
static int compare(const LiveRangePlus& lrp0, const LiveRangePlus& lrp1) { |
| 351 |
// The denoted range includes 'from' but excludes 'to'. |
| 352 |
if (lrp0.to_ <= lrp1.from_) { |
| 353 |
return -1; |
| 354 |
} |
| 355 |
if (lrp0.from_ >= lrp1.to_) { |
| 356 |
return 1; |
| 357 |
} |
| 358 |
return 0; |
| 359 |
} |
| 360 |
}; |
| 361 |
|
| 362 |
// Make sure there's no alignment holes or vtable present. Per bug 1814204, |
| 363 |
// it's important that this structure is as small as possible. |
| 364 |
static_assert(sizeof(LiveRangePlus) == |
| 365 |
sizeof(LiveRange*) + 2 * sizeof(CodePosition)); |
| 366 |
|
| 367 |
// Tracks information about bundles that should all be spilled to the same |
| 368 |
// physical location. At the beginning of allocation, each bundle has its own |
| 369 |
// spill set. As bundles are split, the new smaller bundles continue to use the |
| 370 |
// same spill set. |
| 371 |
class SpillSet : public TempObject { |
| 372 |
// All bundles with this spill set which have been spilled. All bundles in |
| 373 |
// this list will be given the same physical slot. |
| 374 |
Vector<LiveBundle*, 1, JitAllocPolicy> list_; |
| 375 |
|
| 376 |
explicit SpillSet(TempAllocator& alloc) : list_(alloc) {} |
| 377 |
|
| 378 |
public: |
| 379 |
static SpillSet* New(TempAllocator& alloc) { |
| 380 |
return new (alloc) SpillSet(alloc); |
| 381 |
} |
| 382 |
|
| 383 |
[[nodiscard]] bool addSpilledBundle(LiveBundle* bundle) { |
| 384 |
return list_.append(bundle); |
| 385 |
} |
| 386 |
size_t numSpilledBundles() const { return list_.length(); } |
| 387 |
LiveBundle* spilledBundle(size_t i) const { return list_[i]; } |
| 388 |
|
| 389 |
void setAllocation(LAllocation alloc); |
| 390 |
}; |
| 391 |
|
| 392 |
// A set of live ranges which are all pairwise disjoint. The register allocator |
| 393 |
// attempts to find allocations for an entire bundle, and if it fails the |
| 394 |
// bundle will be broken into smaller ones which are allocated independently. |
| 395 |
class LiveBundle : public TempObject { |
| 396 |
// Set to use if this bundle or one it is split into is spilled. |
| 397 |
SpillSet* spill_; |
| 398 |
|
| 399 |
// All the ranges in this set, ordered by location. |
| 400 |
InlineForwardList<LiveRange> ranges_; |
| 401 |
|
| 402 |
// Allocation to use for ranges in this set, bogus if unallocated or spilled |
| 403 |
// and not yet given a physical stack slot. |
| 404 |
LAllocation alloc_; |
| 405 |
|
| 406 |
// Bundle which entirely contains this one and has no register uses. This |
| 407 |
// may or may not be spilled by the allocator, but it can be spilled and |
| 408 |
// will not be split. |
| 409 |
LiveBundle* spillParent_; |
| 410 |
|
| 411 |
// This is used for debug-printing bundles. It gives them an |
| 412 |
// identifiable identity in the debug output, which they otherwise wouldn't |
| 413 |
// have. It's also used for sorting VirtualRegister's live ranges; see the |
| 414 |
// comment in VirtualRegister::sortRanges. |
| 415 |
const uint32_t id_; |
| 416 |
|
| 417 |
LiveBundle(SpillSet* spill, LiveBundle* spillParent, uint32_t id) |
| 418 |
: spill_(spill), spillParent_(spillParent), id_(id) {} |
| 419 |
|
| 420 |
public: |
| 421 |
static LiveBundle* FallibleNew(TempAllocator& alloc, SpillSet* spill, |
| 422 |
LiveBundle* spillParent, uint32_t id) { |
| 423 |
return new (alloc.fallible()) LiveBundle(spill, spillParent, id); |
| 424 |
} |
| 425 |
|
| 426 |
using RangeIterator = InlineForwardListIterator<LiveRange>; |
| 427 |
|
| 428 |
SpillSet* spillSet() const { return spill_; } |
| 429 |
void setSpillSet(SpillSet* spill) { spill_ = spill; } |
| 430 |
|
| 431 |
RangeIterator rangesBegin() const { return ranges_.begin(); } |
| 432 |
RangeIterator rangesBegin(LiveRange* range) const { |
| 433 |
return ranges_.begin(range); |
| 434 |
} |
| 435 |
bool hasRanges() const { return !!rangesBegin(); } |
| 436 |
LiveRange* firstRange() const { return *rangesBegin(); } |
| 437 |
LiveRange* lastRange() const { return ranges_.back(); } |
| 438 |
LiveRange* rangeFor(CodePosition pos) const; |
| 439 |
void removeRange(LiveRange* range); |
| 440 |
void removeRangeAndIncrementIterator(RangeIterator& iter) { |
| 441 |
ranges_.removeAndIncrement(iter); |
| 442 |
} |
| 443 |
void removeAllRangesFromVirtualRegisters(); |
| 444 |
void addRange(LiveRange* range, LiveRange* startAt = nullptr); |
| 445 |
void addRangeAtEnd(LiveRange* range); |
| 446 |
[[nodiscard]] bool addRangeAtEnd(TempAllocator& alloc, VirtualRegister* vreg, |
| 447 |
CodePosition from, CodePosition to); |
| 448 |
[[nodiscard]] bool addRangeAndDistributeUses(TempAllocator& alloc, |
| 449 |
LiveRange* oldRange, |
| 450 |
CodePosition from, |
| 451 |
CodePosition to); |
| 452 |
LiveRange* popFirstRange(); |
| 453 |
#ifdef DEBUG |
| 454 |
size_t numRanges() const; |
| 455 |
#endif |
| 456 |
|
| 457 |
LAllocation allocation() const { return alloc_; } |
| 458 |
void setAllocation(LAllocation alloc) { alloc_ = alloc; } |
| 459 |
|
| 460 |
LiveBundle* spillParent() const { return spillParent_; } |
| 461 |
|
| 462 |
uint32_t id() const { return id_; } |
| 463 |
|
| 464 |
#ifdef JS_JITSPEW |
| 465 |
// Return a string describing this bundle. |
| 466 |
UniqueChars toString() const; |
| 467 |
#endif |
| 468 |
}; |
| 469 |
|
| 470 |
// Information about a control flow edge to resolve (by inserting a move from |
| 471 |
// predecessor range to successor range) in createMoveGroupsForControlFlowEdges. |
| 472 |
struct ControlFlowEdge { |
| 473 |
// The predecessor and successor sides of this edge. |
| 474 |
LBlock* predecessor; |
| 475 |
LBlock* successor; |
| 476 |
|
| 477 |
// Live range that covers the successor block. |
| 478 |
LiveRange* successorRange; |
| 479 |
|
| 480 |
// Exit position of the predecessor block. This is |exitOf(predecessor)| but |
| 481 |
// cached here. |
| 482 |
CodePosition predecessorExit; |
| 483 |
|
| 484 |
ControlFlowEdge(LBlock* predecessor, LBlock* successor, |
| 485 |
LiveRange* successorRange, CodePosition predecessorExit) |
| 486 |
: predecessor(predecessor), |
| 487 |
successor(successor), |
| 488 |
successorRange(successorRange), |
| 489 |
predecessorExit(predecessorExit) { |
| 490 |
MOZ_ASSERT(predecessor != successor); |
| 491 |
} |
| 492 |
}; |
| 493 |
using ControlFlowEdgeVector = |
| 494 |
Vector<ControlFlowEdge, 8, BackgroundSystemAllocPolicy>; |
| 495 |
|
| 496 |
// Information about the allocation for a virtual register. |
| 497 |
class VirtualRegister { |
| 498 |
public: |
| 499 |
// Note: most virtual registers have <= 4 live ranges (at least 95% on x64 for |
| 500 |
// a few very large Wasm modules). |
| 501 |
using RangeVector = Vector<LiveRange*, 4, BackgroundSystemAllocPolicy>; |
| 502 |
class RangeIterator; |
| 503 |
|
| 504 |
private: |
| 505 |
// Instruction which defines this register. |
| 506 |
LNode* ins_ = nullptr; |
| 507 |
|
| 508 |
// Definition in the instruction for this register. |
| 509 |
LDefinition* def_ = nullptr; |
| 510 |
|
| 511 |
// All live ranges for this register. These may overlap each other. |
| 512 |
// If |rangesSorted_| is true, then these are ordered by their start position |
| 513 |
// in descending order. |
| 514 |
RangeVector ranges_; |
| 515 |
|
| 516 |
// Whether def_ is a temp or an output. |
| 517 |
bool isTemp_ = false; |
| 518 |
|
| 519 |
// Whether this vreg is an input for some phi. This use is not reflected in |
| 520 |
// any range on the vreg. |
| 521 |
bool usedByPhi_ = false; |
| 522 |
|
| 523 |
// If this register's definition is MUST_REUSE_INPUT, whether a copy must |
| 524 |
// be introduced before the definition that relaxes the policy. |
| 525 |
bool mustCopyInput_ = false; |
| 526 |
|
| 527 |
// If true, the |ranges_| vector is guaranteed to be sorted. |
| 528 |
bool rangesSorted_ = true; |
| 529 |
|
| 530 |
void operator=(const VirtualRegister&) = delete; |
| 531 |
VirtualRegister(const VirtualRegister&) = delete; |
| 532 |
|
| 533 |
#ifdef DEBUG |
| 534 |
void assertRangesSorted() const; |
| 535 |
#else |
| 536 |
void assertRangesSorted() const {} |
| 537 |
#endif |
| 538 |
|
| 539 |
const RangeVector& sortedRanges() const { |
| 540 |
assertRangesSorted(); |
| 541 |
return ranges_; |
| 542 |
} |
| 543 |
|
| 544 |
public: |
| 545 |
VirtualRegister() = default; |
| 546 |
|
| 547 |
void init(LNode* ins, LDefinition* def, bool isTemp) { |
| 548 |
MOZ_ASSERT(!ins_); |
| 549 |
ins_ = ins; |
| 550 |
def_ = def; |
| 551 |
isTemp_ = isTemp; |
| 552 |
} |
| 553 |
|
| 554 |
LNode* ins() const { return ins_; } |
| 555 |
LDefinition* def() const { return def_; } |
| 556 |
LDefinition::Type type() const { return def()->type(); } |
| 557 |
uint32_t vreg() const { return def()->virtualRegister(); } |
| 558 |
bool isCompatible(const AnyRegister& r) const { |
| 559 |
return def_->isCompatibleReg(r); |
| 560 |
} |
| 561 |
bool isCompatible(const VirtualRegister& vr) const { |
| 562 |
return def_->isCompatibleDef(*vr.def_); |
| 563 |
} |
| 564 |
bool isTemp() const { return isTemp_; } |
| 565 |
|
| 566 |
void setUsedByPhi() { usedByPhi_ = true; } |
| 567 |
bool usedByPhi() { return usedByPhi_; } |
| 568 |
|
| 569 |
void setMustCopyInput() { mustCopyInput_ = true; } |
| 570 |
bool mustCopyInput() { return mustCopyInput_; } |
| 571 |
|
| 572 |
bool hasRanges() const { return !ranges_.empty(); } |
| 573 |
LiveRange* firstRange() const { |
| 574 |
assertRangesSorted(); |
| 575 |
return ranges_.back(); |
| 576 |
} |
| 577 |
LiveRange* lastRange() const { |
| 578 |
assertRangesSorted(); |
| 579 |
return ranges_[0]; |
| 580 |
} |
| 581 |
LiveRange* rangeFor(CodePosition pos, bool preferRegister = false) const; |
| 582 |
void sortRanges(); |
| 583 |
|
| 584 |
void removeFirstRange(RangeIterator& iter); |
| 585 |
void removeRangesForBundle(LiveBundle* bundle); |
| 586 |
template <typename Pred> |
| 587 |
void removeRangesIf(Pred&& pred); |
| 588 |
|
| 589 |
[[nodiscard]] bool replaceLastRangeLinear(LiveRange* old, LiveRange* newPre, |
| 590 |
LiveRange* newPost); |
| 591 |
|
| 592 |
[[nodiscard]] bool addRange(LiveRange* range); |
| 593 |
|
| 594 |
LiveBundle* firstBundle() const { return firstRange()->bundle(); } |
| 595 |
|
| 596 |
[[nodiscard]] bool addInitialRange(TempAllocator& alloc, CodePosition from, |
| 597 |
CodePosition to); |
| 598 |
void addInitialUse(UsePosition* use); |
| 599 |
void setInitialDefinition(CodePosition from); |
| 600 |
|
| 601 |
// Iterator visiting a VirtualRegister's live ranges in order of increasing |
| 602 |
// start position. Because the ranges are sorted in descending order, this |
| 603 |
// iterates over the vector from index |length - 1| to 0. |
| 604 |
class MOZ_RAII RangeIterator { |
| 605 |
const RangeVector& ranges_; |
| 606 |
#ifdef DEBUG |
| 607 |
const VirtualRegister& reg_; |
| 608 |
#endif |
| 609 |
// if |pos_| is 0, the iterator is done. Else, |pos_ - 1| is the index of |
| 610 |
// the range that will be returned by |*iter|. |
| 611 |
size_t pos_; |
| 612 |
|
| 613 |
public: |
| 614 |
explicit RangeIterator(const VirtualRegister& reg) |
| 615 |
: ranges_(reg.sortedRanges()), |
| 616 |
#ifdef DEBUG |
| 617 |
reg_(reg), |
| 618 |
#endif |
| 619 |
pos_(ranges_.length()) { |
| 620 |
} |
| 621 |
RangeIterator(const VirtualRegister& reg, size_t index) |
| 622 |
: ranges_(reg.sortedRanges()), |
| 623 |
#ifdef DEBUG |
| 624 |
reg_(reg), |
| 625 |
#endif |
| 626 |
pos_(index + 1) { |
| 627 |
MOZ_ASSERT(index < ranges_.length()); |
| 628 |
} |
| 629 |
|
| 630 |
#ifdef DEBUG |
| 631 |
~RangeIterator() { |
| 632 |
// Ranges should stay sorted during iteration. |
| 633 |
reg_.assertRangesSorted(); |
| 634 |
} |
| 635 |
#endif |
| 636 |
|
| 637 |
RangeIterator(RangeIterator&) = delete; |
| 638 |
void operator=(RangeIterator&) = delete; |
| 639 |
|
| 640 |
bool done() const { return pos_ == 0; } |
| 641 |
|
| 642 |
explicit operator bool() const { return !done(); } |
| 643 |
|
| 644 |
LiveRange* operator*() const { |
| 645 |
MOZ_ASSERT(!done()); |
| 646 |
return ranges_[pos_ - 1]; |
| 647 |
} |
| 648 |
LiveRange* operator->() { return operator*(); } |
| 649 |
|
| 650 |
size_t index() const { |
| 651 |
MOZ_ASSERT(!done()); |
| 652 |
return pos_ - 1; |
| 653 |
} |
| 654 |
|
| 655 |
void operator++(int) { |
| 656 |
MOZ_ASSERT(!done()); |
| 657 |
pos_--; |
| 658 |
} |
| 659 |
}; |
| 660 |
}; |
| 661 |
|
| 662 |
// A sequence of code positions, for tellings BacktrackingAllocator::splitAt |
| 663 |
// where to split. |
| 664 |
using SplitPositionVector = |
| 665 |
js::Vector<CodePosition, 4, BackgroundSystemAllocPolicy>; |
| 666 |
|
| 667 |
class BacktrackingAllocator : protected RegisterAllocator { |
| 668 |
friend class JSONSpewer; |
| 669 |
|
| 670 |
// Computed data |
| 671 |
InstructionDataMap insData; |
| 672 |
Vector<CodePosition, 12, SystemAllocPolicy> entryPositions; |
| 673 |
Vector<CodePosition, 12, SystemAllocPolicy> exitPositions; |
| 674 |
|
| 675 |
using VirtualRegBitSet = SparseBitSet<BackgroundSystemAllocPolicy>; |
| 676 |
Vector<VirtualRegBitSet, 0, JitAllocPolicy> liveIn; |
| 677 |
Vector<VirtualRegister, 0, JitAllocPolicy> vregs; |
| 678 |
|
| 679 |
// Allocation state. |
| 680 |
StackSlotAllocator stackSlotAllocator; |
| 681 |
|
| 682 |
// List of all instructions with a safepoint. The order is the same as the |
| 683 |
// order of the instructions in the LIR graph. |
| 684 |
Vector<LInstruction*, 0, JitAllocPolicy> safepoints_; |
| 685 |
|
| 686 |
// List of all non-call instructions with a safepoint. The order is the same |
| 687 |
// as the order of the instructions in the LIR graph. |
| 688 |
Vector<LInstruction*, 0, JitAllocPolicy> nonCallSafepoints_; |
| 689 |
|
| 690 |
// Priority queue element: a bundle and the associated priority. |
| 691 |
struct QueueItem { |
| 692 |
LiveBundle* bundle; |
| 693 |
|
| 694 |
QueueItem(LiveBundle* bundle, size_t priority) |
| 695 |
: bundle(bundle), priority_(priority) {} |
| 696 |
|
| 697 |
static bool higherPriority(const QueueItem& a, const QueueItem& b) { |
| 698 |
return a.priority_ > b.priority_; |
| 699 |
} |
| 700 |
|
| 701 |
private: |
| 702 |
size_t priority_; |
| 703 |
}; |
| 704 |
|
| 705 |
PriorityQueue<QueueItem, QueueItem, 0, BackgroundSystemAllocPolicy> |
| 706 |
allocationQueue; |
| 707 |
|
| 708 |
// This is a set of LiveRange. They must be non-overlapping. Attempts |
| 709 |
// to add an overlapping range will cause AvlTree::insert to MOZ_CRASH(). |
| 710 |
using LiveRangeSet = AvlTree<LiveRange*, LiveRange>; |
| 711 |
|
| 712 |
// The same, but for LiveRangePlus. See comments on LiveRangePlus. |
| 713 |
using LiveRangePlusSet = AvlTree<LiveRangePlus, LiveRangePlus>; |
| 714 |
|
| 715 |
// Each physical register is associated with the set of ranges over which |
| 716 |
// that register is currently allocated. |
| 717 |
struct PhysicalRegister { |
| 718 |
bool allocatable; |
| 719 |
AnyRegister reg; |
| 720 |
LiveRangePlusSet allocations; |
| 721 |
|
| 722 |
PhysicalRegister() : allocatable(false) {} |
| 723 |
}; |
| 724 |
mozilla::Array<PhysicalRegister, AnyRegister::Total> registers; |
| 725 |
|
| 726 |
// Ranges of code which are considered to be hot, for which good allocation |
| 727 |
// should be prioritized. |
| 728 |
LiveRangeSet hotcode; |
| 729 |
|
| 730 |
// Output positions of all call instructions (where all registers must be |
| 731 |
// spilled). This vector is sorted in ascending order and doesn't contain |
| 732 |
// duplicate values. |
| 733 |
Vector<CodePosition, 16, BackgroundSystemAllocPolicy> callPositions; |
| 734 |
|
| 735 |
// Information about an allocated stack slot. |
| 736 |
struct SpillSlot : public TempObject, |
| 737 |
public InlineForwardListNode<SpillSlot> { |
| 738 |
LStackSlot alloc; |
| 739 |
LiveRangePlusSet allocated; |
| 740 |
|
| 741 |
SpillSlot(uint32_t slot, LStackSlot::Width width, LifoAlloc* alloc) |
| 742 |
: alloc(slot, width), allocated(alloc) {} |
| 743 |
}; |
| 744 |
using SpillSlotList = InlineForwardList<SpillSlot>; |
| 745 |
|
| 746 |
// All allocated slots of each width. |
| 747 |
SpillSlotList normalSlots, doubleSlots, quadSlots; |
| 748 |
|
| 749 |
Vector<LiveBundle*, 4, BackgroundSystemAllocPolicy> spilledBundles; |
| 750 |
|
| 751 |
// The bundle id that will be used for the next LiveBundle that's allocated. |
| 752 |
uint32_t nextBundleId_ = 0; |
| 753 |
|
| 754 |
using LiveBundleVector = Vector<LiveBundle*, 4, BackgroundSystemAllocPolicy>; |
| 755 |
|
| 756 |
// Misc accessors |
| 757 |
bool compilingWasm() { return mir->outerInfo().compilingWasm(); } |
| 758 |
VirtualRegister& vreg(const LDefinition* def) { |
| 759 |
return vregs[def->virtualRegister()]; |
| 760 |
} |
| 761 |
VirtualRegister& vreg(const LAllocation* alloc) { |
| 762 |
MOZ_ASSERT(alloc->isUse()); |
| 763 |
return vregs[alloc->toUse()->virtualRegister()]; |
| 764 |
} |
| 765 |
|
| 766 |
uint32_t getNextBundleId() { return nextBundleId_++; } |
| 767 |
|
| 768 |
CodePosition entryOf(const LBlock* block) { |
| 769 |
return entryPositions[block->mir()->id()]; |
| 770 |
} |
| 771 |
CodePosition exitOf(const LBlock* block) { |
| 772 |
return exitPositions[block->mir()->id()]; |
| 773 |
} |
| 774 |
|
| 775 |
// Atomic group helper. See comments in BacktrackingAllocator.cpp. |
| 776 |
CodePosition minimalDefEnd(LNode* ins) const; |
| 777 |
|
| 778 |
// Helpers for creating and adding MoveGroups |
| 779 |
[[nodiscard]] bool addMove(LMoveGroup* moves, LiveRange* from, LiveRange* to, |
| 780 |
LDefinition::Type type) { |
| 781 |
LAllocation fromAlloc = from->bundle()->allocation(); |
| 782 |
LAllocation toAlloc = to->bundle()->allocation(); |
| 783 |
MOZ_ASSERT(fromAlloc != toAlloc); |
| 784 |
return moves->add(fromAlloc, toAlloc, type); |
| 785 |
} |
| 786 |
|
| 787 |
[[nodiscard]] bool moveInput(LInstruction* ins, LiveRange* from, |
| 788 |
LiveRange* to, LDefinition::Type type) { |
| 789 |
if (from->bundle()->allocation() == to->bundle()->allocation()) { |
| 790 |
return true; |
| 791 |
} |
| 792 |
LMoveGroup* moves = getInputMoveGroup(ins); |
| 793 |
return addMove(moves, from, to, type); |
| 794 |
} |
| 795 |
|
| 796 |
[[nodiscard]] bool moveAfter(LInstruction* ins, LiveRange* from, |
| 797 |
LiveRange* to, LDefinition::Type type) { |
| 798 |
if (from->bundle()->allocation() == to->bundle()->allocation()) { |
| 799 |
return true; |
| 800 |
} |
| 801 |
LMoveGroup* moves = getMoveGroupAfter(ins); |
| 802 |
return addMove(moves, from, to, type); |
| 803 |
} |
| 804 |
|
| 805 |
[[nodiscard]] bool moveAtExit(LBlock* block, LiveRange* from, LiveRange* to, |
| 806 |
LDefinition::Type type) { |
| 807 |
if (from->bundle()->allocation() == to->bundle()->allocation()) { |
| 808 |
return true; |
| 809 |
} |
| 810 |
LMoveGroup* moves = block->getExitMoveGroup(alloc()); |
| 811 |
return addMove(moves, from, to, type); |
| 812 |
} |
| 813 |
|
| 814 |
[[nodiscard]] bool moveAtEntry(LBlock* block, LiveRange* from, LiveRange* to, |
| 815 |
LDefinition::Type type) { |
| 816 |
if (from->bundle()->allocation() == to->bundle()->allocation()) { |
| 817 |
return true; |
| 818 |
} |
| 819 |
LMoveGroup* moves = block->getEntryMoveGroup(alloc()); |
| 820 |
return addMove(moves, from, to, type); |
| 821 |
} |
| 822 |
|
| 823 |
// Out-of-line methods, in the same sequence as in BacktrackingAllocator.cpp. |
| 824 |
|
| 825 |
// Misc helpers: queries about uses |
| 826 |
bool isReusedInput(LUse* use, LNode* ins, bool considerCopy); |
| 827 |
bool isRegisterUse(UsePosition* use, LNode* ins, bool considerCopy = false); |
| 828 |
bool isRegisterDefinition(LiveRange* range); |
| 829 |
|
| 830 |
// Misc helpers: atomic LIR groups |
| 831 |
// (these are all in the parent class, RegisterAllocator) |
| 832 |
|
| 833 |
// Misc helpers: computation of bundle priorities and spill weights |
| 834 |
size_t computePriority(LiveBundle* bundle); |
| 835 |
bool minimalDef(LiveRange* range, LNode* ins); |
| 836 |
bool minimalUse(LiveRange* range, UsePosition* use); |
| 837 |
bool minimalBundle(LiveBundle* bundle, bool* pfixed = nullptr); |
| 838 |
size_t computeSpillWeight(LiveBundle* bundle); |
| 839 |
size_t maximumSpillWeight(const LiveBundleVector& bundles); |
| 840 |
|
| 841 |
// Initialization of the allocator |
| 842 |
[[nodiscard]] bool init(); |
| 843 |
|
| 844 |
// Liveness analysis |
| 845 |
[[nodiscard]] bool addInitialFixedRange(AnyRegister reg, CodePosition from, |
| 846 |
CodePosition to); |
| 847 |
[[nodiscard]] bool buildLivenessInfo(); |
| 848 |
|
| 849 |
// Call positions. |
| 850 |
mozilla::Maybe<size_t> lookupFirstCallPositionInRange(CodePosition from, |
| 851 |
CodePosition to); |
| 852 |
|
| 853 |
// Merging and queueing of LiveRange groups |
| 854 |
void tryMergeBundles(LiveBundle* bundle0, LiveBundle* bundle1); |
| 855 |
[[nodiscard]] bool allocateStackDefinition(VirtualRegister& reg); |
| 856 |
[[nodiscard]] bool tryMergeReusedRegister(VirtualRegister& def, |
| 857 |
VirtualRegister& input); |
| 858 |
[[nodiscard]] bool mergeAndQueueRegisters(); |
| 859 |
|
| 860 |
// Implementation of splitting decisions, but not the making of those |
| 861 |
// decisions |
| 862 |
[[nodiscard]] bool updateVirtualRegisterListsThenRequeueBundles( |
| 863 |
LiveBundle* bundle, const LiveBundleVector& newBundles); |
| 864 |
|
| 865 |
// Implementation of splitting decisions, but not the making of those |
| 866 |
// decisions |
| 867 |
[[nodiscard]] bool splitAt(LiveBundle* bundle, |
| 868 |
const SplitPositionVector& splitPositions); |
| 869 |
|
| 870 |
// Creation of splitting decisions, but not their implementation |
| 871 |
[[nodiscard]] bool splitAcrossCalls(LiveBundle* bundle); |
| 872 |
[[nodiscard]] bool trySplitAcrossHotcode(LiveBundle* bundle, bool* success); |
| 873 |
[[nodiscard]] bool trySplitAfterLastRegisterUse(LiveBundle* bundle, |
| 874 |
LiveBundle* conflict, |
| 875 |
bool* success); |
| 876 |
[[nodiscard]] bool trySplitBeforeFirstRegisterUse(LiveBundle* bundle, |
| 877 |
LiveBundle* conflict, |
| 878 |
bool* success); |
| 879 |
|
| 880 |
// The top level driver for the splitting machinery |
| 881 |
[[nodiscard]] bool chooseBundleSplit(LiveBundle* bundle, bool hasCall, |
| 882 |
LiveBundle* conflict); |
| 883 |
|
| 884 |
// Bundle allocation |
| 885 |
[[nodiscard]] bool computeRequirement(LiveBundle* bundle, |
| 886 |
Requirement* prequirement, |
| 887 |
Requirement* phint); |
| 888 |
[[nodiscard]] bool tryAllocateRegister(PhysicalRegister& r, |
| 889 |
LiveBundle* bundle, bool* success, |
| 890 |
bool* hasCall, |
| 891 |
LiveBundleVector& conflicting); |
| 892 |
[[nodiscard]] bool tryAllocateAnyRegister(LiveBundle* bundle, bool* success, |
| 893 |
bool* hasCall, |
| 894 |
LiveBundleVector& conflicting); |
| 895 |
[[nodiscard]] bool evictBundle(LiveBundle* bundle); |
| 896 |
[[nodiscard]] bool tryAllocateFixed(LiveBundle* bundle, |
| 897 |
Requirement requirement, bool* success, |
| 898 |
bool* hasCall, |
| 899 |
LiveBundleVector& conflicting); |
| 900 |
[[nodiscard]] bool tryAllocateNonFixed(LiveBundle* bundle, |
| 901 |
Requirement requirement, |
| 902 |
Requirement hint, bool* success, |
| 903 |
bool* hasCall, |
| 904 |
LiveBundleVector& conflicting); |
| 905 |
[[nodiscard]] bool processBundle(const MIRGenerator* mir, LiveBundle* bundle); |
| 906 |
[[nodiscard]] bool spill(LiveBundle* bundle); |
| 907 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool |
| 908 |
tryAllocatingRegistersForSpillBundles(); |
| 909 |
|
| 910 |
// Rewriting of the LIR after bundle processing is done |
| 911 |
[[nodiscard]] bool insertAllRanges(LiveRangePlusSet& set, LiveBundle* bundle); |
| 912 |
void sortVirtualRegisterRanges(); |
| 913 |
[[nodiscard]] bool pickStackSlot(SpillSet* spill); |
| 914 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool pickStackSlots(); |
| 915 |
[[nodiscard]] bool moveAtEdge(LBlock* predecessor, LBlock* successor, |
| 916 |
LiveRange* from, LiveRange* to, |
| 917 |
LDefinition::Type type); |
| 918 |
void removeDeadRanges(VirtualRegister& reg); |
| 919 |
[[nodiscard]] bool createMoveGroupsForControlFlowEdges( |
| 920 |
const VirtualRegister& reg, const ControlFlowEdgeVector& edges); |
| 921 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool |
| 922 |
createMoveGroupsFromLiveRangeTransitions(); |
| 923 |
size_t findFirstNonCallSafepoint(CodePosition pos, size_t startFrom); |
| 924 |
void addLiveRegistersForRange(VirtualRegister& reg, LiveRange* range, |
| 925 |
size_t* firstNonCallSafepoint); |
| 926 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool installAllocationsInLIR(); |
| 927 |
size_t findFirstSafepoint(CodePosition pos, size_t startFrom); |
| 928 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool populateSafepoints(); |
| 929 |
[[nodiscard]] AVOID_INLINE_FOR_DEBUGGING bool annotateMoveGroups(); |
| 930 |
|
| 931 |
// Debug-printing support |
| 932 |
#ifdef JS_JITSPEW |
| 933 |
void dumpLiveRangesByVReg(const char* who); |
| 934 |
void dumpLiveRangesByBundle(const char* who); |
| 935 |
void dumpAllocations(); |
| 936 |
#endif |
| 937 |
|
| 938 |
// Top level of the register allocation machinery, and the only externally |
| 939 |
// visible bit. |
| 940 |
public: |
| 941 |
BacktrackingAllocator(MIRGenerator* mir, LIRGenerator* lir, LIRGraph& graph) |
| 942 |
: RegisterAllocator(mir, lir, graph), |
| 943 |
liveIn(mir->alloc()), |
| 944 |
vregs(mir->alloc()), |
| 945 |
safepoints_(mir->alloc()), |
| 946 |
nonCallSafepoints_(mir->alloc()) {} |
| 947 |
|
| 948 |
[[nodiscard]] bool go(); |
| 949 |
}; |
| 950 |
|
| 951 |
} // namespace jit |
| 952 |
} // namespace js |
| 953 |
|
| 954 |
#endif /* jit_BacktrackingAllocator_h */ |