| 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 |
#include "jit/SimpleAllocator.h" |
| 8 |
|
| 9 |
#include "mozilla/Maybe.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
|
| 13 |
#include "jit/BitSet.h" |
| 14 |
#include "jit/CompileInfo.h" |
| 15 |
#include "js/Printf.h" |
| 16 |
|
| 17 |
using namespace js; |
| 18 |
using namespace js::jit; |
| 19 |
|
| 20 |
using mozilla::DebugOnly; |
| 21 |
using mozilla::Maybe; |
| 22 |
|
| 23 |
// [SMDOC] Simple Register Allocator |
| 24 |
// |
| 25 |
// This is an alternative register allocator for Ion that's simpler than the |
| 26 |
// backtracking allocator. It attempts to perform register allocation quickly |
| 27 |
// while still producing fairly decent code within basic blocks. |
| 28 |
// |
| 29 |
// The Backtracking allocator is usually the slowest part of the Ion compiler |
| 30 |
// backend, whereas this Simple allocator is often faster than codegen and/or |
| 31 |
// GVN. |
| 32 |
// |
| 33 |
// This allocator lets us experiment with different compilation strategies and |
| 34 |
// it provides a useful baseline for measuring and optimizing the performance of |
| 35 |
// the backtracking allocator. |
| 36 |
// |
| 37 |
// This allocator also helps document our LIR => Register Allocator interface |
| 38 |
// and will make it easier to experiment with alternative register allocators in |
| 39 |
// the future. |
| 40 |
// |
| 41 |
// Phases |
| 42 |
// ====== |
| 43 |
// This allocator has 3 phases with the following main goals: |
| 44 |
// |
| 45 |
// (1) init: initializes a VirtualRegister instance for each virtual register. |
| 46 |
// |
| 47 |
// (2) analyzeLiveness: iterates over all LIR instructions in reverse order to |
| 48 |
// collect the following information: |
| 49 |
// |
| 50 |
// * For each virtual register we record where it's last used (its |
| 51 |
// lastUseInsId_). Register allocation uses this information to free |
| 52 |
// dead registers and stack slots in freeDeadVregsAfterInstruction. |
| 53 |
// |
| 54 |
// * For each basic block it records the set of live GC things (liveGCIn_) |
| 55 |
// at the start of the block. This is used by the register allocation pass |
| 56 |
// to populate safepoints (used for GC tracing). |
| 57 |
// |
| 58 |
// * For virtual registers with fixed register uses, it assigns a register |
| 59 |
// hint to help avoid moves (fixedUseHint_). |
| 60 |
// |
| 61 |
// This function is based on BacktrackingAllocator::buildLivenessInfo. |
| 62 |
// |
| 63 |
// (3) allocateRegisters: this iterates over all LIR instructions (from first to |
| 64 |
// last) to allocate registers and to populate safepoints. |
| 65 |
// |
| 66 |
// Register allocation |
| 67 |
// =================== |
| 68 |
// Producing the fastest possible code is not a goal for this allocator, so it |
| 69 |
// makes the following trade-offs: |
| 70 |
// |
| 71 |
// * It tries to keep values in registers within a basic block, but values that |
| 72 |
// are used across blocks are spilled in the block where they're defined. |
| 73 |
// We try to spill these values as early as possible (instead of all at the |
| 74 |
// end of the block) to help avoid CPU store buffer stalls. |
| 75 |
// |
| 76 |
// * Blocks with a single predecessor can reuse the register state at the end of |
| 77 |
// that predecessor block. Values in these registers must have a stack |
| 78 |
// location too so reusing this state is optional. This is based on a small |
| 79 |
// array with state for just four recent blocks in it, but in practice this is |
| 80 |
// good enough to eliminate a lot of memory loads. |
| 81 |
// |
| 82 |
// * Phis are always assigned a stack slot and phi operands are stored to this |
| 83 |
// location at the end of the predecessor blocks (in allocateForBlockEnd). |
| 84 |
// |
| 85 |
// * Stack slots are freed when virtual registers die and can then be reused. |
| 86 |
// |
| 87 |
// In practice this results in fairly decent code within basic blocks. This |
| 88 |
// allocator generates relatively poor code for tight loops or for code with |
| 89 |
// many short blocks. |
| 90 |
|
| 91 |
static AnyRegister MaybeGetRegisterFromSet(AllocatableRegisterSet regs, |
| 92 |
LDefinition::Type type) { |
| 93 |
// Get an available register from `regs`, or return an invalid register if no |
| 94 |
// register is available. |
| 95 |
switch (type) { |
| 96 |
case LDefinition::Type::FLOAT32: |
| 97 |
if (regs.hasAny<RegTypeName::Float32>()) { |
| 98 |
return AnyRegister(regs.getAnyFloat<RegTypeName::Float32>()); |
| 99 |
} |
| 100 |
break; |
| 101 |
case LDefinition::Type::DOUBLE: |
| 102 |
if (regs.hasAny<RegTypeName::Float64>()) { |
| 103 |
return AnyRegister(regs.getAnyFloat<RegTypeName::Float64>()); |
| 104 |
} |
| 105 |
break; |
| 106 |
case LDefinition::Type::SIMD128: |
| 107 |
if (regs.hasAny<RegTypeName::Vector128>()) { |
| 108 |
return AnyRegister(regs.getAnyFloat<RegTypeName::Vector128>()); |
| 109 |
} |
| 110 |
break; |
| 111 |
default: |
| 112 |
MOZ_ASSERT(!LDefinition::isFloatReg(type)); |
| 113 |
if (regs.hasAny<RegTypeName::GPR>()) { |
| 114 |
return AnyRegister(regs.getAnyGeneral()); |
| 115 |
} |
| 116 |
break; |
| 117 |
} |
| 118 |
return AnyRegister(); |
| 119 |
} |
| 120 |
|
| 121 |
bool SimpleAllocator::init() { |
| 122 |
size_t numBlocks = graph.numBlocks(); |
| 123 |
if (!liveGCIn_.growBy(numBlocks)) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
size_t numVregs = graph.numVirtualRegisters(); |
| 128 |
if (!vregs_.initCapacity(numVregs)) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
for (size_t i = 0; i < numVregs; i++) { |
| 132 |
vregs_.infallibleEmplaceBack(); |
| 133 |
} |
| 134 |
|
| 135 |
// Initialize virtual registers. |
| 136 |
for (size_t blockIndex = 0; blockIndex < numBlocks; blockIndex++) { |
| 137 |
if (mir->shouldCancel("init (block loop)")) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
LBlock* block = graph.getBlock(blockIndex); |
| 142 |
|
| 143 |
for (uint32_t i = 0, numPhis = block->numPhis(); i < numPhis; i++) { |
| 144 |
LPhi* phi = block->getPhi(i); |
| 145 |
LDefinition* def = phi->getDef(0); |
| 146 |
vregs_[def->virtualRegister()].init(phi, def, /* isTemp = */ false); |
| 147 |
} |
| 148 |
|
| 149 |
for (LInstructionIterator iter = block->begin(); iter != block->end(); |
| 150 |
iter++) { |
| 151 |
LInstruction* ins = *iter; |
| 152 |
for (LInstruction::OutputIter output(ins); !output.done(); output++) { |
| 153 |
LDefinition* def = *output; |
| 154 |
vregs_[def->virtualRegister()].init(ins, def, /* isTemp = */ false); |
| 155 |
} |
| 156 |
for (LInstruction::TempIter temp(ins); !temp.done(); temp++) { |
| 157 |
LDefinition* def = *temp; |
| 158 |
vregs_[def->virtualRegister()].init(ins, def, /* isTemp = */ true); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
bool SimpleAllocator::analyzeLiveness() { |
| 167 |
JitSpew(JitSpew_RegAlloc, "Beginning liveness analysis"); |
| 168 |
|
| 169 |
// Stack of active loops. |
| 170 |
struct LoopState { |
| 171 |
// The first instruction of the loop header. |
| 172 |
uint32_t firstId; |
| 173 |
// The last instruction of the backedge. |
| 174 |
uint32_t lastId; |
| 175 |
explicit LoopState(LBlock* header, uint32_t lastId) |
| 176 |
: firstId(header->firstId()), lastId(lastId) {} |
| 177 |
}; |
| 178 |
Vector<LoopState, 4, BackgroundSystemAllocPolicy> loopStack; |
| 179 |
|
| 180 |
#ifdef DEBUG |
| 181 |
// In debug builds, assert that each instruction has a smaller ID than the |
| 182 |
// previous instructions we saw (since we're iterating over instructions in |
| 183 |
// reverse order). Our simple liveness analysis relies on this for the vreg's |
| 184 |
// lastUseInsId_. |
| 185 |
uint32_t lastInsId = UINT32_MAX; |
| 186 |
#endif |
| 187 |
|
| 188 |
for (size_t i = graph.numBlocks(); i > 0; i--) { |
| 189 |
if (mir->shouldCancel("analyzeLiveness (block loop)")) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
LBlock* block = graph.getBlock(i - 1); |
| 194 |
MBasicBlock* mblock = block->mir(); |
| 195 |
VirtualRegBitSet& liveGC = liveGCIn_[mblock->id()]; |
| 196 |
|
| 197 |
if (mblock->isLoopBackedge()) { |
| 198 |
if (!loopStack.emplaceBack(mblock->loopHeaderOfBackedge()->lir(), |
| 199 |
block->lastId())) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// Propagate liveGCIn from our successors to us. Skip backedges, as we fix |
| 205 |
// them up at the loop header. |
| 206 |
for (size_t i = 0; i < mblock->lastIns()->numSuccessors(); i++) { |
| 207 |
MBasicBlock* successor = mblock->lastIns()->getSuccessor(i); |
| 208 |
if (mblock->id() < successor->id()) { |
| 209 |
if (!liveGC.insertAll(liveGCIn_[successor->id()])) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
uint32_t blockFirstId = block->firstId(); |
| 216 |
|
| 217 |
auto handleUseOfVreg = [&](uint32_t insId, uint32_t vregId) { |
| 218 |
uint32_t defId = vregs_[vregId].insId(); |
| 219 |
const LoopState* outerLoop = nullptr; |
| 220 |
if (defId < blockFirstId) { |
| 221 |
// This vreg is defined before the current block. We need to add it to |
| 222 |
// the block's liveGC set if it's a GC type. |
| 223 |
if (vregs_[vregId].isGCType() && !liveGC.insert(vregId)) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
// If we're inside a loop, search for the outermost loop that has this |
| 227 |
// vreg live across the entire loop. We need to extend the vreg's range |
| 228 |
// to cover this loop. |
| 229 |
for (size_t i = loopStack.length(); i > 0; i--) { |
| 230 |
const LoopState& loop = loopStack[i - 1]; |
| 231 |
if (defId >= loop.firstId) { |
| 232 |
break; |
| 233 |
} |
| 234 |
outerLoop = &loop; |
| 235 |
} |
| 236 |
} |
| 237 |
vregs_[vregId].updateLastUseId(outerLoop ? outerLoop->lastId : insId); |
| 238 |
return true; |
| 239 |
}; |
| 240 |
|
| 241 |
// If this block has a successor with phis, the phi operands will be used at |
| 242 |
// the end of the current block. |
| 243 |
if (MBasicBlock* successor = mblock->successorWithPhis()) { |
| 244 |
LBlock* phiSuccessor = successor->lir(); |
| 245 |
uint32_t blockLastInsId = block->lastId(); |
| 246 |
for (size_t j = 0; j < phiSuccessor->numPhis(); j++) { |
| 247 |
LPhi* phi = phiSuccessor->getPhi(j); |
| 248 |
LAllocation* use = phi->getOperand(mblock->positionInPhiSuccessor()); |
| 249 |
if (!handleUseOfVreg(blockLastInsId, use->toUse()->virtualRegister())) { |
| 250 |
return false; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
// Handle instruction uses and outputs. |
| 256 |
for (LInstructionReverseIterator ins = block->rbegin(); |
| 257 |
ins != block->rend(); ins++) { |
| 258 |
if (mir->shouldCancel("analyzeLiveness (instruction loop)")) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
#ifdef DEBUG |
| 263 |
MOZ_ASSERT(ins->id() < lastInsId); |
| 264 |
lastInsId = ins->id(); |
| 265 |
#endif |
| 266 |
|
| 267 |
for (LInstruction::OutputIter output(*ins); !output.done(); output++) { |
| 268 |
uint32_t vregId = output->virtualRegister(); |
| 269 |
if (vregs_[vregId].isGCType()) { |
| 270 |
liveGC.remove(vregId); |
| 271 |
} |
| 272 |
} |
| 273 |
for (LInstruction::InputIter inputAlloc(**ins); inputAlloc.more(); |
| 274 |
inputAlloc.next()) { |
| 275 |
if (!inputAlloc->isUse()) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
LUse* use = inputAlloc->toUse(); |
| 279 |
uint32_t vregId = use->virtualRegister(); |
| 280 |
if (!handleUseOfVreg(ins->id(), vregId)) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
if (use->policy() == LUse::FIXED) { |
| 284 |
// Assign a register hint to the vreg. We'll prefer allocating this |
| 285 |
// register later to avoid unnecessary moves. |
| 286 |
VirtualRegister& vreg = vregs_[vregId]; |
| 287 |
AnyRegister fixedReg = GetFixedRegister(vreg.def(), use); |
| 288 |
if (vreg.fixedUseHint().isNothing()) { |
| 289 |
vreg.setFixedUseHint(fixedReg); |
| 290 |
} else if (*vreg.fixedUseHint() != fixedReg) { |
| 291 |
// Conflicting fixed register uses. Clear the hint. |
| 292 |
vreg.setFixedUseHint(AnyRegister()); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
// Handle phi definitions. |
| 299 |
for (size_t i = 0; i < block->numPhis(); i++) { |
| 300 |
LPhi* phi = block->getPhi(i); |
| 301 |
LDefinition* def = phi->getDef(0); |
| 302 |
uint32_t vregId = def->virtualRegister(); |
| 303 |
if (vregs_[vregId].isGCType()) { |
| 304 |
liveGC.remove(vregId); |
| 305 |
} |
| 306 |
// Loop header phis must not be freed before the end of the backedge |
| 307 |
// because the backedge might store to the phi's stack slot. |
| 308 |
if (mblock->isLoopHeader()) { |
| 309 |
vregs_[vregId].updateLastUseId(loopStack.back().lastId); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (mblock->isLoopHeader()) { |
| 314 |
MBasicBlock* backedge = mblock->backedge(); |
| 315 |
MOZ_ASSERT(loopStack.back().firstId == block->firstId()); |
| 316 |
loopStack.popBack(); |
| 317 |
|
| 318 |
// Propagate the loop header's liveGCIn set to all blocks within the loop. |
| 319 |
if (mblock != backedge && !liveGC.empty()) { |
| 320 |
// Start at the block after |mblock|. |
| 321 |
MOZ_ASSERT(graph.getBlock(i - 1) == mblock->lir()); |
| 322 |
size_t j = i; |
| 323 |
while (true) { |
| 324 |
MBasicBlock* loopBlock = graph.getBlock(j)->mir(); |
| 325 |
if (!liveGCIn_[loopBlock->id()].insertAll(liveGC)) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
if (loopBlock == backedge) { |
| 329 |
break; |
| 330 |
} |
| 331 |
j++; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
MOZ_ASSERT_IF(!mblock->numPredecessors(), liveGC.empty()); |
| 337 |
} |
| 338 |
|
| 339 |
MOZ_ASSERT(loopStack.empty()); |
| 340 |
|
| 341 |
// Initialize vregLastUses_ vector and sort it by instructionId in descending |
| 342 |
// order. This will be used in freeDeadVregsAfterInstruction. |
| 343 |
uint32_t numVregs = vregs_.length(); |
| 344 |
if (!vregLastUses_.reserve(numVregs)) { |
| 345 |
return false; |
| 346 |
} |
| 347 |
for (uint32_t vregId = 1; vregId < numVregs; vregId++) { |
| 348 |
vregLastUses_.infallibleEmplaceBack(vregs_[vregId].lastUseInsId(), vregId); |
| 349 |
} |
| 350 |
auto compareEntries = [](VregLastUse a, VregLastUse b) { |
| 351 |
return a.instructionId > b.instructionId; |
| 352 |
}; |
| 353 |
std::sort(vregLastUses_.begin(), vregLastUses_.end(), compareEntries); |
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
void SimpleAllocator::removeAllocatedRegisterAtIndex(size_t index) { |
| 358 |
// Release the register for this entry. |
| 359 |
AllocatedRegister allocated = allocatedRegs_[index]; |
| 360 |
uint32_t vregId = allocated.vregId(); |
| 361 |
availableRegs_.add(allocated.reg()); |
| 362 |
|
| 363 |
// Use the swap-and-pop idiom to remove the entry so that we don't change the |
| 364 |
// register index of other entries. |
| 365 |
size_t lastIndex = allocatedRegs_.length() - 1; |
| 366 |
if (index != lastIndex) { |
| 367 |
uint32_t lastVregId = allocatedRegs_.back().vregId(); |
| 368 |
allocatedRegs_[index] = allocatedRegs_.back(); |
| 369 |
if (vregs_[lastVregId].registerIndex() == lastIndex) { |
| 370 |
vregs_[lastVregId].setRegisterIndex(index); |
| 371 |
} |
| 372 |
} |
| 373 |
allocatedRegs_.popBack(); |
| 374 |
vregs_[vregId].clearRegisterIndex(); |
| 375 |
|
| 376 |
// In the uncommon case where the vreg might have another allocated register, |
| 377 |
// assign the other register to this vreg if needed. |
| 378 |
if (MOZ_UNLIKELY(hasMultipleRegsForVreg_)) { |
| 379 |
for (size_t j = 0; j < allocatedRegs_.length(); j++) { |
| 380 |
if (allocatedRegs_[j].vregId() == vregId) { |
| 381 |
vregs_[vregId].setRegisterIndex(j); |
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
bool SimpleAllocator::ensureStackLocation(uint32_t vregId, LAllocation* alloc) { |
| 389 |
// Allocate a stack slot for this virtual register if needed. |
| 390 |
VirtualRegister& vreg = vregs_[vregId]; |
| 391 |
if (vreg.hasStackLocation()) { |
| 392 |
*alloc = vreg.stackLocation(); |
| 393 |
return true; |
| 394 |
} |
| 395 |
LStackSlot::Width width = LStackSlot::width(vreg.def()->type()); |
| 396 |
uint32_t slotOffset; |
| 397 |
if (!stackSlotAllocator_.allocateSlot(width, &slotOffset)) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
LStackSlot::SlotAndWidth slot(slotOffset, width); |
| 401 |
vreg.setAllocatedStackSlot(slot); |
| 402 |
*alloc = LStackSlot(slot); |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
LAllocation SimpleAllocator::registerOrStackLocation(LInstruction* ins, |
| 407 |
uint32_t vregId, |
| 408 |
bool trackRegUse) { |
| 409 |
const VirtualRegister& vreg = vregs_[vregId]; |
| 410 |
if (vreg.hasRegister()) { |
| 411 |
AllocatedRegister& allocated = allocatedRegs_[vreg.registerIndex()]; |
| 412 |
if (trackRegUse) { |
| 413 |
allocated.setLastUsedAtInsId(ins->id()); |
| 414 |
} |
| 415 |
return LAllocation(allocated.reg()); |
| 416 |
} |
| 417 |
return vreg.stackLocation(); |
| 418 |
} |
| 419 |
|
| 420 |
bool SimpleAllocator::spillRegister(LInstruction* ins, |
| 421 |
AllocatedRegister allocated) { |
| 422 |
VirtualRegister& vreg = vregs_[allocated.vregId()]; |
| 423 |
MOZ_ASSERT(vreg.insId() < ins->id()); |
| 424 |
if (vreg.hasStackLocation()) { |
| 425 |
return true; |
| 426 |
} |
| 427 |
// Allocate a new stack slot and insert a register => stack move. |
| 428 |
LMoveGroup* input = getInputMoveGroup(ins); |
| 429 |
LAllocation dest; |
| 430 |
if (!ensureStackLocation(allocated.vregId(), &dest)) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
return input->addAfter(LAllocation(allocated.reg()), dest, |
| 434 |
vreg.def()->type()); |
| 435 |
} |
| 436 |
|
| 437 |
bool SimpleAllocator::allocateForBlockEnd(LBlock* block, LInstruction* ins) { |
| 438 |
#ifdef DEBUG |
| 439 |
// All vregs that are live after this block must have a stack location at this |
| 440 |
// point. |
| 441 |
for (const AllocatedRegister& allocated : allocatedRegs_) { |
| 442 |
MOZ_ASSERT_IF(vregs_[allocated.vregId()].lastUseInsId() > ins->id(), |
| 443 |
vregs_[allocated.vregId()].hasStackLocation()); |
| 444 |
} |
| 445 |
#endif |
| 446 |
|
| 447 |
MBasicBlock* successor = block->mir()->successorWithPhis(); |
| 448 |
if (!successor) { |
| 449 |
return true; |
| 450 |
} |
| 451 |
|
| 452 |
// The current block has a successor with phis. For each successor phi, insert |
| 453 |
// a move at the end of the current block to store the phi's operand into the |
| 454 |
// phi's stack slot. |
| 455 |
|
| 456 |
uint32_t position = block->mir()->positionInPhiSuccessor(); |
| 457 |
LBlock* lirSuccessor = successor->lir(); |
| 458 |
LMoveGroup* group = nullptr; |
| 459 |
|
| 460 |
for (size_t i = 0, numPhis = lirSuccessor->numPhis(); i < numPhis; i++) { |
| 461 |
LPhi* phi = lirSuccessor->getPhi(i); |
| 462 |
|
| 463 |
uint32_t sourceVreg = phi->getOperand(position)->toUse()->virtualRegister(); |
| 464 |
uint32_t destVreg = phi->getDef(0)->virtualRegister(); |
| 465 |
if (sourceVreg == destVreg) { |
| 466 |
continue; |
| 467 |
} |
| 468 |
|
| 469 |
if (!group) { |
| 470 |
// The moves we insert here need to happen simultaneously with each other, |
| 471 |
// yet after any existing moves before the instruction. |
| 472 |
LMoveGroup* input = getInputMoveGroup(ins); |
| 473 |
if (input->numMoves() == 0) { |
| 474 |
group = input; |
| 475 |
} else { |
| 476 |
group = LMoveGroup::New(alloc()); |
| 477 |
block->insertAfter(input, group); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
LAllocation source = |
| 482 |
registerOrStackLocation(ins, sourceVreg, /* trackRegUse = */ true); |
| 483 |
LAllocation dest; |
| 484 |
if (!ensureStackLocation(destVreg, &dest)) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
if (!group->add(source, dest, phi->getDef(0)->type())) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
return true; |
| 493 |
} |
| 494 |
|
| 495 |
void SimpleAllocator::scanDefinition(LInstruction* ins, LDefinition* def, |
| 496 |
bool isTemp) { |
| 497 |
// Record fixed registers to make sure we don't assign these registers to |
| 498 |
// uses. |
| 499 |
if (def->policy() == LDefinition::FIXED && def->output()->isAnyRegister()) { |
| 500 |
AnyRegister reg = def->output()->toAnyRegister(); |
| 501 |
if (isTemp) { |
| 502 |
fixedTempRegs_.add(reg); |
| 503 |
} |
| 504 |
// Note: addUnchecked because some instructions use the same fixed register |
| 505 |
// for a Temp and an Output (eg LCallNative). See bug 1962671. |
| 506 |
fixedOutputAndTempRegs_.addUnchecked(reg); |
| 507 |
return; |
| 508 |
} |
| 509 |
if (def->policy() == LDefinition::MUST_REUSE_INPUT) { |
| 510 |
ins->changePolicyOfReusedInputToAny(def); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
bool SimpleAllocator::allocateForNonFixedDefOrUse(LInstruction* ins, |
| 515 |
uint32_t vregId, |
| 516 |
AllocationKind kind, |
| 517 |
AnyRegister* reg) { |
| 518 |
// This function is responsible for finding a new register for a (non-fixed) |
| 519 |
// register def or use. If no register is available, it will evict something. |
| 520 |
|
| 521 |
// This should only be called if the vreg doesn't have a register yet, unless |
| 522 |
// the caller set hasMultipleRegsForVreg_ to true. |
| 523 |
const VirtualRegister& vreg = vregs_[vregId]; |
| 524 |
MOZ_ASSERT_IF(!hasMultipleRegsForVreg_, !vreg.hasRegister()); |
| 525 |
|
| 526 |
// Determine the set of available registers. We can pick any register in |
| 527 |
// availableRegs_ except for fixed output/temp registers that we need to |
| 528 |
// allocate later. Note that UseAtStart inputs may use the same register as |
| 529 |
// outputs (but not temps). |
| 530 |
bool isUseAtStart = (kind == AllocationKind::UseAtStart); |
| 531 |
RegisterSet fixedDefs = |
| 532 |
isUseAtStart ? fixedTempRegs_.set() : fixedOutputAndTempRegs_.set(); |
| 533 |
AllocatableRegisterSet available( |
| 534 |
RegisterSet::Subtract(availableRegs_.set(), fixedDefs)); |
| 535 |
|
| 536 |
// If the virtual register has a register hint, pick that register if it's |
| 537 |
// available. |
| 538 |
if (vreg.fixedUseHint().isSome() && vreg.fixedUseHint()->isValid()) { |
| 539 |
AnyRegister regHint = *vreg.fixedUseHint(); |
| 540 |
if (available.has(regHint)) { |
| 541 |
*reg = regHint; |
| 542 |
return addAllocatedReg(ins, vregId, isUseAtStart, *reg); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
// Try to get an arbitrary register from the set. |
| 547 |
*reg = MaybeGetRegisterFromSet(available, vreg.def()->type()); |
| 548 |
if (reg->isValid()) { |
| 549 |
return addAllocatedReg(ins, vregId, isUseAtStart, *reg); |
| 550 |
} |
| 551 |
|
| 552 |
// No register is available. We need to evict something. |
| 553 |
|
| 554 |
// Determine which registers we must not evict. These are the registers in |
| 555 |
// fixedDefs (same as above) + registers we've already allocated for this |
| 556 |
// instruction. Note again that outputs can be assigned the same register as |
| 557 |
// UseAtStart inputs and we rely on this to not run out of registers on 32-bit |
| 558 |
// x86. |
| 559 |
AllocatableRegisterSet notEvictable; |
| 560 |
if (kind == AllocationKind::Output) { |
| 561 |
notEvictable.set() = |
| 562 |
RegisterSet::Union(fixedDefs, currentInsRegsNotAtStart_.set()); |
| 563 |
} else { |
| 564 |
notEvictable.set() = RegisterSet::Union(fixedDefs, currentInsRegs_.set()); |
| 565 |
} |
| 566 |
|
| 567 |
// Search for the best register to evict. |
| 568 |
LDefinition* def = vreg.def(); |
| 569 |
const AllocatedRegister* bestToEvict = nullptr; |
| 570 |
for (size_t i = 0, len = allocatedRegs_.length(); i < len; i++) { |
| 571 |
AllocatedRegister& allocated = allocatedRegs_[i]; |
| 572 |
if (!def->isCompatibleReg(allocated.reg()) || |
| 573 |
notEvictable.has(allocated.reg())) { |
| 574 |
continue; |
| 575 |
} |
| 576 |
// Heuristics: prefer evicting registers where the vreg is also in another |
| 577 |
// register (uncommon), registers that already have a stack location, or |
| 578 |
// registers that haven't been used recently. |
| 579 |
if (!bestToEvict || vregs_[allocated.vregId()].registerIndex() != i || |
| 580 |
(vregs_[allocated.vregId()].hasStackLocation() && |
| 581 |
!vregs_[bestToEvict->vregId()].hasStackLocation()) || |
| 582 |
allocated.lastUsedAtInsId() < bestToEvict->lastUsedAtInsId()) { |
| 583 |
bestToEvict = &allocated; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
if (bestToEvict) { |
| 588 |
*reg = bestToEvict->reg(); |
| 589 |
} else { |
| 590 |
// We didn't find a compatible register to evict. This can happen for |
| 591 |
// aliased registers, for example if we allocated all Float32 registers and |
| 592 |
// now need a Double register. Pick an arbitrary register that's evictable |
| 593 |
// and evict all of its aliases. |
| 594 |
AllocatableRegisterSet evictable; |
| 595 |
evictable.set() = |
| 596 |
RegisterSet::Subtract(allRegisters_.set(), notEvictable.set()); |
| 597 |
*reg = MaybeGetRegisterFromSet(evictable, def->type()); |
| 598 |
MOZ_ASSERT(reg->numAliased() > 1); |
| 599 |
} |
| 600 |
if (!evictRegister(ins, *reg)) { |
| 601 |
return false; |
| 602 |
} |
| 603 |
return addAllocatedReg(ins, vregId, isUseAtStart, *reg); |
| 604 |
} |
| 605 |
|
| 606 |
bool SimpleAllocator::allocateForFixedUse(LInstruction* ins, const LUse* use, |
| 607 |
AnyRegister* reg) { |
| 608 |
uint32_t vregId = use->virtualRegister(); |
| 609 |
VirtualRegister& vreg = vregs_[vregId]; |
| 610 |
*reg = GetFixedRegister(vreg.def(), use); |
| 611 |
|
| 612 |
// Determine the vreg's current location. |
| 613 |
LAllocation alloc; |
| 614 |
if (vreg.hasRegister()) { |
| 615 |
// Check if the vreg is already using the fixed register. |
| 616 |
AllocatedRegister& allocated = allocatedRegs_[vreg.registerIndex()]; |
| 617 |
if (allocated.reg() == *reg) { |
| 618 |
markUseOfAllocatedReg(ins, allocated, use->usedAtStart()); |
| 619 |
return true; |
| 620 |
} |
| 621 |
|
| 622 |
// Try to avoid having multiple registers for the same vreg by replacing the |
| 623 |
// current register with the new one. If this is not possible (this is |
| 624 |
// uncommon) we set hasMultipleRegsForVreg_ and fix this up at the end of |
| 625 |
// allocateForInstruction. |
| 626 |
alloc = LAllocation(allocated.reg()); |
| 627 |
if (currentInsRegs_.has(allocated.reg())) { |
| 628 |
hasMultipleRegsForVreg_ = true; |
| 629 |
} else { |
| 630 |
removeAllocatedRegisterAtIndex(vreg.registerIndex()); |
| 631 |
} |
| 632 |
} else { |
| 633 |
alloc = vreg.stackLocation(); |
| 634 |
} |
| 635 |
|
| 636 |
// If the fixed register is not available we have to evict it. |
| 637 |
if (!availableRegs_.has(*reg) && !evictRegister(ins, *reg)) { |
| 638 |
return false; |
| 639 |
} |
| 640 |
|
| 641 |
// Mark register as allocated and load the value in it. |
| 642 |
if (!addAllocatedReg(ins, vregId, use->usedAtStart(), *reg)) { |
| 643 |
return false; |
| 644 |
} |
| 645 |
LMoveGroup* input = getInputMoveGroup(ins); |
| 646 |
return input->addAfter(alloc, LAllocation(*reg), vreg.def()->type()); |
| 647 |
} |
| 648 |
|
| 649 |
bool SimpleAllocator::allocateForRegisterUse(LInstruction* ins, const LUse* use, |
| 650 |
AnyRegister* reg) { |
| 651 |
uint32_t vregId = use->virtualRegister(); |
| 652 |
VirtualRegister& vreg = vregs_[vregId]; |
| 653 |
bool useAtStart = use->usedAtStart(); |
| 654 |
|
| 655 |
// Determine the vreg's current location. |
| 656 |
LAllocation alloc; |
| 657 |
if (vreg.hasRegister()) { |
| 658 |
// The vreg already has a register. We can use it if we won't need this |
| 659 |
// register later for a fixed output or temp. |
| 660 |
AllocatedRegister& allocated = allocatedRegs_[vreg.registerIndex()]; |
| 661 |
bool isReserved = useAtStart ? fixedTempRegs_.has(allocated.reg()) |
| 662 |
: fixedOutputAndTempRegs_.has(allocated.reg()); |
| 663 |
if (!isReserved) { |
| 664 |
markUseOfAllocatedReg(ins, allocated, useAtStart); |
| 665 |
*reg = allocated.reg(); |
| 666 |
return true; |
| 667 |
} |
| 668 |
|
| 669 |
// Try to avoid having multiple registers for the same vreg by replacing the |
| 670 |
// current register with the new one. If this is not possible (this is |
| 671 |
// uncommon) we set hasMultipleRegsForVreg_ and fix this up at the end of |
| 672 |
// allocateForInstruction. |
| 673 |
alloc = LAllocation(allocated.reg()); |
| 674 |
if (currentInsRegs_.has(allocated.reg())) { |
| 675 |
hasMultipleRegsForVreg_ = true; |
| 676 |
} else { |
| 677 |
removeAllocatedRegisterAtIndex(vreg.registerIndex()); |
| 678 |
} |
| 679 |
} else { |
| 680 |
alloc = vreg.stackLocation(); |
| 681 |
} |
| 682 |
|
| 683 |
// This vreg is not in a register or it's in a reserved register. Allocate a |
| 684 |
// new register. |
| 685 |
AllocationKind kind = |
| 686 |
useAtStart ? AllocationKind::UseAtStart : AllocationKind::UseOrTemp; |
| 687 |
if (!allocateForNonFixedDefOrUse(ins, vregId, kind, reg)) { |
| 688 |
return false; |
| 689 |
} |
| 690 |
LMoveGroup* input = getInputMoveGroup(ins); |
| 691 |
return input->addAfter(alloc, LAllocation(*reg), vreg.def()->type()); |
| 692 |
} |
| 693 |
|
| 694 |
bool SimpleAllocator::evictRegister(LInstruction* ins, AnyRegister reg) { |
| 695 |
// The caller wants to use `reg` but it's not available. Spill this register |
| 696 |
// (and its aliases) to make it available. |
| 697 |
|
| 698 |
MOZ_ASSERT(reg.isValid()); |
| 699 |
MOZ_ASSERT(!availableRegs_.has(reg)); |
| 700 |
|
| 701 |
for (size_t i = 0; i < allocatedRegs_.length();) { |
| 702 |
AllocatedRegister allocated = allocatedRegs_[i]; |
| 703 |
if (!allocated.reg().aliases(reg)) { |
| 704 |
i++; |
| 705 |
continue; |
| 706 |
} |
| 707 |
|
| 708 |
// Registers are added to the safepoint in `populateSafepoint`, but if we're |
| 709 |
// evicting a register that's being used at-start we lose information about |
| 710 |
// this so we have to add it to the safepoint now. |
| 711 |
if (ins->safepoint() && !ins->isCall() && |
| 712 |
currentInsRegs_.has(allocated.reg())) { |
| 713 |
MOZ_ASSERT(!currentInsRegsNotAtStart_.has(allocated.reg())); |
| 714 |
if (!addLiveRegisterToSafepoint(ins->safepoint(), allocated)) { |
| 715 |
return false; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
// Spill this register unless we know this vreg also lives in a different |
| 720 |
// register (this is uncommon). |
| 721 |
uint32_t vregId = allocated.vregId(); |
| 722 |
if (vregs_[vregId].registerIndex() == i) { |
| 723 |
if (!spillRegister(ins, allocated)) { |
| 724 |
return false; |
| 725 |
} |
| 726 |
} else { |
| 727 |
MOZ_ASSERT(hasMultipleRegsForVreg_); |
| 728 |
} |
| 729 |
removeAllocatedRegisterAtIndex(i); |
| 730 |
|
| 731 |
if (availableRegs_.has(reg)) { |
| 732 |
return true; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
MOZ_CRASH("failed to evict register"); |
| 737 |
} |
| 738 |
|
| 739 |
bool SimpleAllocator::allocateForDefinition(uint32_t blockLastId, |
| 740 |
LInstruction* ins, LDefinition* def, |
| 741 |
bool isTemp) { |
| 742 |
uint32_t vregId = def->virtualRegister(); |
| 743 |
|
| 744 |
// Allocate a register for this definition. Return early for definitions that |
| 745 |
// don't need a register. |
| 746 |
AnyRegister reg; |
| 747 |
switch (def->policy()) { |
| 748 |
case LDefinition::FIXED: { |
| 749 |
if (!def->output()->isAnyRegister()) { |
| 750 |
MOZ_ASSERT(!isTemp); |
| 751 |
vregs_[vregId].setHasStackLocation(); |
| 752 |
return true; |
| 753 |
} |
| 754 |
|
| 755 |
// We need a fixed register. Evict it if it's not available. |
| 756 |
reg = def->output()->toAnyRegister(); |
| 757 |
if (!availableRegs_.has(reg)) { |
| 758 |
// For call instructions we allow Temps and Outputs to use the same |
| 759 |
// fixed register. That should probably be changed at some point, but |
| 760 |
// for now we can handle this edge case by removing the temp's register. |
| 761 |
// See bug 1962671. |
| 762 |
if (!isTemp && fixedTempRegs_.has(reg)) { |
| 763 |
MOZ_ASSERT(ins->isCall()); |
| 764 |
for (size_t i = 0; i < allocatedRegs_.length(); i++) { |
| 765 |
if (allocatedRegs_[i].reg() == reg) { |
| 766 |
removeAllocatedRegisterAtIndex(i); |
| 767 |
break; |
| 768 |
} |
| 769 |
} |
| 770 |
MOZ_ASSERT(availableRegs_.has(reg)); |
| 771 |
} else { |
| 772 |
if (!evictRegister(ins, reg)) { |
| 773 |
return false; |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
if (!addAllocatedReg(ins, vregId, /* usedAtStart = */ false, reg)) { |
| 778 |
return false; |
| 779 |
} |
| 780 |
break; |
| 781 |
} |
| 782 |
case LDefinition::REGISTER: { |
| 783 |
AllocationKind kind = |
| 784 |
isTemp ? AllocationKind::UseOrTemp : AllocationKind::Output; |
| 785 |
if (!allocateForNonFixedDefOrUse(ins, vregId, kind, ®)) { |
| 786 |
return false; |
| 787 |
} |
| 788 |
break; |
| 789 |
} |
| 790 |
case LDefinition::MUST_REUSE_INPUT: { |
| 791 |
// Allocate a new register and add an entry to reusedInputs_ to move the |
| 792 |
// input into this register when we're done allocating registers. |
| 793 |
AllocationKind kind = |
| 794 |
isTemp ? AllocationKind::UseOrTemp : AllocationKind::Output; |
| 795 |
if (!allocateForNonFixedDefOrUse(ins, vregId, kind, ®)) { |
| 796 |
return false; |
| 797 |
} |
| 798 |
LAllocation* useAlloc = ins->getOperand(def->getReusedInput()); |
| 799 |
uint32_t useVregId = useAlloc->toUse()->virtualRegister(); |
| 800 |
LDefinition::Type type = vregs_[useVregId].def()->type(); |
| 801 |
if (!reusedInputs_.emplaceBack(useAlloc, reg, type)) { |
| 802 |
return false; |
| 803 |
} |
| 804 |
break; |
| 805 |
} |
| 806 |
case LDefinition::STACK: { |
| 807 |
// This is a Wasm stack area or stack result. This is used when calling |
| 808 |
// functions with multiple return values. |
| 809 |
MOZ_ASSERT(!isTemp); |
| 810 |
if (def->type() == LDefinition::STACKRESULTS) { |
| 811 |
LStackArea alloc(ins->toInstruction()); |
| 812 |
if (!stackSlotAllocator_.allocateStackArea(&alloc)) { |
| 813 |
return false; |
| 814 |
} |
| 815 |
def->setOutput(alloc); |
| 816 |
} else { |
| 817 |
// Because the definitions are visited in order, the area has been |
| 818 |
// allocated before we reach this result, so we know the operand is an |
| 819 |
// LStackArea. |
| 820 |
const LUse* use = ins->getOperand(0)->toUse(); |
| 821 |
VirtualRegister& area = vregs_[use->virtualRegister()]; |
| 822 |
const LStackArea* areaAlloc = area.def()->output()->toStackArea(); |
| 823 |
def->setOutput(areaAlloc->resultAlloc(ins, def)); |
| 824 |
} |
| 825 |
vregs_[vregId].setHasStackLocation(); |
| 826 |
return true; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
def->setOutput(LAllocation(reg)); |
| 831 |
|
| 832 |
// If this is an output register for a vreg that's used in a different block, |
| 833 |
// we have to spill it to the stack at some point and we want to do that as |
| 834 |
// early as possible. We can't do it here though because we're not allowed to |
| 835 |
// insert moves right before LOsiPoint instructions. Add the outputs to a |
| 836 |
// vector that we check at the start of allocateForInstruction. |
| 837 |
if (!isTemp && vregs_[vregId].lastUseInsId() > blockLastId) { |
| 838 |
if (!eagerSpillOutputs_.append(def)) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
return true; |
| 844 |
} |
| 845 |
|
| 846 |
bool SimpleAllocator::allocateForInstruction(VirtualRegBitSet& liveGC, |
| 847 |
uint32_t blockLastId, |
| 848 |
LInstruction* ins) { |
| 849 |
if (!alloc().ensureBallast()) { |
| 850 |
return false; |
| 851 |
} |
| 852 |
|
| 853 |
assertValidRegisterStateBeforeInstruction(); |
| 854 |
MOZ_ASSERT(reusedInputs_.empty()); |
| 855 |
|
| 856 |
// If we have to spill output registers of a previous instruction, do that now |
| 857 |
// if the instruction is not an LOsiPoint instruction. See the comment at the |
| 858 |
// end of allocateForDefinition. |
| 859 |
if (!eagerSpillOutputs_.empty() && !ins->isOsiPoint()) { |
| 860 |
LMoveGroup* moves = getInputMoveGroup(ins); |
| 861 |
for (LDefinition* def : eagerSpillOutputs_) { |
| 862 |
MOZ_ASSERT(!vregs_[def->virtualRegister()].hasStackLocation()); |
| 863 |
LAllocation dest; |
| 864 |
if (!ensureStackLocation(def->virtualRegister(), &dest)) { |
| 865 |
return false; |
| 866 |
} |
| 867 |
if (!moves->add(*def->output(), dest, def->type())) { |
| 868 |
return false; |
| 869 |
} |
| 870 |
} |
| 871 |
eagerSpillOutputs_.clear(); |
| 872 |
} |
| 873 |
|
| 874 |
// Clear per-instruction register sets. |
| 875 |
currentInsRegs_ = AllocatableRegisterSet(); |
| 876 |
currentInsRegsNotAtStart_ = AllocatableRegisterSet(); |
| 877 |
fixedTempRegs_ = AllocatableRegisterSet(); |
| 878 |
fixedOutputAndTempRegs_ = AllocatableRegisterSet(); |
| 879 |
|
| 880 |
// Allocate all fixed inputs first. |
| 881 |
for (LInstruction::NonSnapshotInputIter alloc(*ins); alloc.more(); |
| 882 |
alloc.next()) { |
| 883 |
if (!alloc->isUse() || alloc->toUse()->policy() != LUse::FIXED) { |
| 884 |
continue; |
| 885 |
} |
| 886 |
AnyRegister reg; |
| 887 |
if (!allocateForFixedUse(ins, alloc->toUse(), ®)) { |
| 888 |
return false; |
| 889 |
} |
| 890 |
alloc.replace(LAllocation(reg)); |
| 891 |
} |
| 892 |
|
| 893 |
// Scan all definitions. If any of these require fixed registers, it will |
| 894 |
// affect which registers are available for the inputs. |
| 895 |
for (LInstruction::TempIter temp(ins); !temp.done(); temp++) { |
| 896 |
scanDefinition(ins, *temp, /* isTemp = */ true); |
| 897 |
} |
| 898 |
for (LInstruction::OutputIter output(ins); !output.done(); output++) { |
| 899 |
scanDefinition(ins, *output, /* isTemp = */ false); |
| 900 |
} |
| 901 |
|
| 902 |
// Allocate inputs which are required to be in non-fixed registers. |
| 903 |
for (LInstruction::NonSnapshotInputIter alloc(*ins); alloc.more(); |
| 904 |
alloc.next()) { |
| 905 |
if (!alloc->isUse() || alloc->toUse()->policy() != LUse::REGISTER) { |
| 906 |
continue; |
| 907 |
} |
| 908 |
AnyRegister reg; |
| 909 |
if (!allocateForRegisterUse(ins, alloc->toUse(), ®)) { |
| 910 |
return false; |
| 911 |
} |
| 912 |
alloc.replace(LAllocation(reg)); |
| 913 |
} |
| 914 |
|
| 915 |
// Allocate all definitions. |
| 916 |
for (LInstruction::TempIter temp(ins); !temp.done(); temp++) { |
| 917 |
if (!allocateForDefinition(blockLastId, ins, *temp, /* isTemp = */ true)) { |
| 918 |
return false; |
| 919 |
} |
| 920 |
} |
| 921 |
for (LInstruction::OutputIter output(ins); !output.done(); output++) { |
| 922 |
LDefinition* def = *output; |
| 923 |
if (!allocateForDefinition(blockLastId, ins, def, /* isTemp = */ false)) { |
| 924 |
return false; |
| 925 |
} |
| 926 |
if (vregs_[def->virtualRegister()].isGCType()) { |
| 927 |
if (!liveGC.insert(def->virtualRegister())) { |
| 928 |
return false; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
// If this instruction is a call, we need to evict all registers except for |
| 934 |
// preserved registers or the instruction's definitions. We have to do this |
| 935 |
// before the next step because call instructions are allowed to clobber |
| 936 |
// their input registers so we shouldn't use the same register for snapshot |
| 937 |
// inputs. |
| 938 |
if (ins->isCall()) { |
| 939 |
for (size_t i = 0; i < allocatedRegs_.length();) { |
| 940 |
AllocatedRegister allocated = allocatedRegs_[i]; |
| 941 |
if (ins->isCallPreserved(allocated.reg()) || |
| 942 |
vregs_[allocated.vregId()].insId() == ins->id()) { |
| 943 |
i++; |
| 944 |
continue; |
| 945 |
} |
| 946 |
if (!spillRegister(ins, allocated)) { |
| 947 |
return false; |
| 948 |
} |
| 949 |
removeAllocatedRegisterAtIndex(i); |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
// Allocate for remaining inputs which do not need to be in registers. |
| 954 |
for (LInstruction::InputIter alloc(*ins); alloc.more(); alloc.next()) { |
| 955 |
if (!alloc->isUse()) { |
| 956 |
continue; |
| 957 |
} |
| 958 |
LUse* use = alloc->toUse(); |
| 959 |
MOZ_ASSERT(use->policy() != LUse::REGISTER && use->policy() != LUse::FIXED); |
| 960 |
uint32_t vreg = use->virtualRegister(); |
| 961 |
// KEEPALIVE uses are very common in JS code for snapshots and these uses |
| 962 |
// don't prefer a register. Don't update the register's last-use (used for |
| 963 |
// eviction heuristics) for them. |
| 964 |
bool trackRegUse = (use->policy() != LUse::KEEPALIVE); |
| 965 |
LAllocation allocated = registerOrStackLocation(ins, vreg, trackRegUse); |
| 966 |
alloc.replace(allocated); |
| 967 |
} |
| 968 |
|
| 969 |
// If this instruction had MUST_REUSE_INPUT definitions, we've now assigned a |
| 970 |
// register or stack slot for the input and we allocated a register for the |
| 971 |
// output. We can now insert a move and then rewrite the input LAllocation to |
| 972 |
// match the LDefinition (code generation relies on this). |
| 973 |
while (!reusedInputs_.empty()) { |
| 974 |
auto entry = reusedInputs_.popCopy(); |
| 975 |
LMoveGroup* input = getInputMoveGroup(ins); |
| 976 |
if (!input->addAfter(*entry.source, LAllocation(entry.dest), entry.type)) { |
| 977 |
return false; |
| 978 |
} |
| 979 |
*entry.source = LAllocation(entry.dest); |
| 980 |
} |
| 981 |
|
| 982 |
// Add registers and stack locations to the instruction's safepoint if needed. |
| 983 |
if (LSafepoint* safepoint = ins->safepoint()) { |
| 984 |
if (!populateSafepoint(liveGC, ins, safepoint)) { |
| 985 |
return false; |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
// In the uncommon case where a vreg might have multiple allocated registers, |
| 990 |
// discard the registers not linked from the vreg. This simplifies other parts |
| 991 |
// of the allocator. |
| 992 |
if (hasMultipleRegsForVreg_) { |
| 993 |
for (size_t i = 0; i < allocatedRegs_.length();) { |
| 994 |
VirtualRegister& vreg = vregs_[allocatedRegs_[i].vregId()]; |
| 995 |
if (vreg.registerIndex() != i) { |
| 996 |
removeAllocatedRegisterAtIndex(i); |
| 997 |
continue; |
| 998 |
} |
| 999 |
i++; |
| 1000 |
} |
| 1001 |
hasMultipleRegsForVreg_ = false; |
| 1002 |
} |
| 1003 |
|
| 1004 |
return true; |
| 1005 |
} |
| 1006 |
|
| 1007 |
bool SimpleAllocator::addLiveRegisterToSafepoint(LSafepoint* safepoint, |
| 1008 |
AllocatedRegister allocated) { |
| 1009 |
safepoint->addLiveRegister(allocated.reg()); |
| 1010 |
const VirtualRegister& vreg = vregs_[allocated.vregId()]; |
| 1011 |
if (vreg.isGCType()) { |
| 1012 |
if (!safepoint->addGCAllocation(allocated.vregId(), vreg.def(), |
| 1013 |
LAllocation(allocated.reg()))) { |
| 1014 |
return false; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
return true; |
| 1018 |
} |
| 1019 |
|
| 1020 |
bool SimpleAllocator::populateSafepoint(VirtualRegBitSet& liveGC, |
| 1021 |
LInstruction* ins, |
| 1022 |
LSafepoint* safepoint) { |
| 1023 |
// Add allocated registers to the safepoint. Safepoints for call instructions |
| 1024 |
// never include any registers. |
| 1025 |
if (!ins->isCall()) { |
| 1026 |
for (AllocatedRegister allocated : allocatedRegs_) { |
| 1027 |
// Outputs (but not temps) of the current instruction are ignored, except |
| 1028 |
// for the clobberedRegs set that's used for debug assertions. |
| 1029 |
const VirtualRegister& vreg = vregs_[allocated.vregId()]; |
| 1030 |
if (vreg.insId() == ins->id()) { |
| 1031 |
#ifdef CHECK_OSIPOINT_REGISTERS |
| 1032 |
safepoint->addClobberedRegister(allocated.reg()); |
| 1033 |
#endif |
| 1034 |
if (!vreg.isTemp()) { |
| 1035 |
continue; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
if (!addLiveRegisterToSafepoint(safepoint, allocated)) { |
| 1039 |
return false; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
// Also add stack locations that store GC things. |
| 1045 |
for (VirtualRegBitSet::Iterator liveRegId(liveGC); liveRegId; ++liveRegId) { |
| 1046 |
uint32_t vregId = *liveRegId; |
| 1047 |
const VirtualRegister& vreg = vregs_[vregId]; |
| 1048 |
MOZ_ASSERT(vreg.isGCType()); |
| 1049 |
if (!vreg.hasStackLocation() || vreg.insId() == ins->id()) { |
| 1050 |
continue; |
| 1051 |
} |
| 1052 |
if (!safepoint->addGCAllocation(vregId, vreg.def(), vreg.stackLocation())) { |
| 1053 |
return false; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
return true; |
| 1058 |
} |
| 1059 |
|
| 1060 |
void SimpleAllocator::freeDeadVregsAfterInstruction(VirtualRegBitSet& liveGC, |
| 1061 |
LNode* ins) { |
| 1062 |
// Mark virtual registers that won't be used after `ins` as dead. This also |
| 1063 |
// frees their stack slots and registers. |
| 1064 |
while (!vregLastUses_.empty() && |
| 1065 |
vregLastUses_.back().instructionId <= ins->id()) { |
| 1066 |
VregLastUse entry = vregLastUses_.popCopy(); |
| 1067 |
VirtualRegister& vreg = vregs_[entry.vregId]; |
| 1068 |
if (vreg.hasRegister()) { |
| 1069 |
removeAllocatedRegisterAtIndex(vreg.registerIndex()); |
| 1070 |
} |
| 1071 |
if (vreg.hasAllocatedStackSlot()) { |
| 1072 |
LStackSlot::SlotAndWidth stackSlot = vreg.stackSlot(); |
| 1073 |
stackSlotAllocator_.freeSlot(stackSlot.width(), stackSlot.slot()); |
| 1074 |
} |
| 1075 |
if (vreg.isGCType()) { |
| 1076 |
liveGC.remove(entry.vregId); |
| 1077 |
} |
| 1078 |
vreg.markDead(); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
bool SimpleAllocator::tryReuseRegistersFromPredecessor(MBasicBlock* block) { |
| 1083 |
// Try to reuse the register state from our predecessor block if we have a |
| 1084 |
// single predecessor. Note that this is completely optional because all live |
| 1085 |
// vregs must have a stack location at this point. |
| 1086 |
|
| 1087 |
if (block->numPredecessors() != 1) { |
| 1088 |
return true; |
| 1089 |
} |
| 1090 |
|
| 1091 |
auto findBlockState = [this](uint32_t predId) -> const BlockState* { |
| 1092 |
for (const BlockState& state : blockStates_) { |
| 1093 |
if (state.blockIndex == predId) { |
| 1094 |
return &state; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
return nullptr; |
| 1098 |
}; |
| 1099 |
const BlockState* state = findBlockState(block->getPredecessor(0)->id()); |
| 1100 |
if (!state) { |
| 1101 |
return true; |
| 1102 |
} |
| 1103 |
|
| 1104 |
MOZ_ASSERT(allocatedRegs_.empty()); |
| 1105 |
availableRegs_ = state->availableRegs; |
| 1106 |
for (AllocatedRegister allocated : state->allocatedRegs) { |
| 1107 |
// Ignore registers for vregs that were marked dead between the |
| 1108 |
// predecessor block and the current block. |
| 1109 |
if (vregs_[allocated.vregId()].isDead()) { |
| 1110 |
availableRegs_.add(allocated.reg()); |
| 1111 |
} else { |
| 1112 |
VirtualRegister& vreg = vregs_[allocated.vregId()]; |
| 1113 |
MOZ_ASSERT(!vreg.hasRegister()); |
| 1114 |
vreg.setRegisterIndex(allocatedRegs_.length()); |
| 1115 |
if (!allocatedRegs_.append(allocated)) { |
| 1116 |
return false; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
} |
| 1120 |
return true; |
| 1121 |
} |
| 1122 |
|
| 1123 |
void SimpleAllocator::saveAndClearAllocatedRegisters(MBasicBlock* block) { |
| 1124 |
if (allocatedRegs_.empty()) { |
| 1125 |
return; |
| 1126 |
} |
| 1127 |
|
| 1128 |
for (AllocatedRegister allocated : allocatedRegs_) { |
| 1129 |
vregs_[allocated.vregId()].clearRegisterIndex(); |
| 1130 |
} |
| 1131 |
|
| 1132 |
// Ensure at least one successor has a single predecessor block that could use |
| 1133 |
// our register state later in tryReuseRegistersFromPredecessor. |
| 1134 |
bool shouldSave = false; |
| 1135 |
for (size_t i = 0; i < block->numSuccessors(); i++) { |
| 1136 |
if (block->getSuccessor(i)->numPredecessors() == 1 && |
| 1137 |
block->getSuccessor(i)->id() > block->id()) { |
| 1138 |
shouldSave = true; |
| 1139 |
break; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
if (shouldSave) { |
| 1143 |
// Save current register state in the circular buffer. We use std::swap for |
| 1144 |
// the vectors to try to reuse malloc buffers. |
| 1145 |
BlockState& state = blockStates_[nextBlockStateIndex_]; |
| 1146 |
std::swap(allocatedRegs_, state.allocatedRegs); |
| 1147 |
state.availableRegs = availableRegs_; |
| 1148 |
state.blockIndex = block->id(); |
| 1149 |
nextBlockStateIndex_ = (nextBlockStateIndex_ + 1) % BlockStateLength; |
| 1150 |
} |
| 1151 |
allocatedRegs_.clear(); |
| 1152 |
availableRegs_ = allRegisters_; |
| 1153 |
} |
| 1154 |
|
| 1155 |
bool SimpleAllocator::allocateRegisters() { |
| 1156 |
// Initialize register state. |
| 1157 |
MOZ_ASSERT(allocatedRegs_.empty()); |
| 1158 |
availableRegs_ = allRegisters_; |
| 1159 |
|
| 1160 |
size_t numBlocks = graph.numBlocks(); |
| 1161 |
|
| 1162 |
// It's very common for JS code to have a basic block that jumps to the next |
| 1163 |
// block and the next block doesn't have any other predecessors. We treat |
| 1164 |
// such blocks as a single block. |
| 1165 |
bool fuseWithNextBlock = false; |
| 1166 |
|
| 1167 |
for (size_t blockIndex = 0; blockIndex < numBlocks; blockIndex++) { |
| 1168 |
if (mir->shouldCancel("allocateRegisters (block loop)")) { |
| 1169 |
return false; |
| 1170 |
} |
| 1171 |
|
| 1172 |
LBlock* block = graph.getBlock(blockIndex); |
| 1173 |
MBasicBlock* mblock = block->mir(); |
| 1174 |
MOZ_ASSERT(mblock->id() == blockIndex); |
| 1175 |
|
| 1176 |
VirtualRegBitSet& liveGC = liveGCIn_[blockIndex]; |
| 1177 |
|
| 1178 |
// Try to reuse the register state from our predecessor. If we fused this |
| 1179 |
// block with the previous block we're already using its register state. |
| 1180 |
if (!fuseWithNextBlock) { |
| 1181 |
MOZ_ASSERT(allocatedRegs_.empty()); |
| 1182 |
if (!tryReuseRegistersFromPredecessor(mblock)) { |
| 1183 |
return false; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
// Note: sometimes two blocks appear fusable but the successor block still |
| 1188 |
// has an (unnecessary) phi. Don't fuse in this edge case. |
| 1189 |
fuseWithNextBlock = mblock->numSuccessors() == 1 && |
| 1190 |
mblock->getSuccessor(0)->id() == blockIndex + 1 && |
| 1191 |
mblock->getSuccessor(0)->numPredecessors() == 1 && |
| 1192 |
graph.getBlock(blockIndex + 1)->numPhis() == 0; |
| 1193 |
|
| 1194 |
uint32_t blockLastId = fuseWithNextBlock |
| 1195 |
? graph.getBlock(blockIndex + 1)->lastId() |
| 1196 |
: block->lastId(); |
| 1197 |
|
| 1198 |
// Allocate stack slots for phis. |
| 1199 |
for (uint32_t i = 0, numPhis = block->numPhis(); i < numPhis; i++) { |
| 1200 |
LPhi* phi = block->getPhi(i); |
| 1201 |
LDefinition* def = phi->getDef(0); |
| 1202 |
uint32_t vregId = def->virtualRegister(); |
| 1203 |
bool isGCType = vregs_[vregId].isGCType(); |
| 1204 |
LAllocation defAlloc; |
| 1205 |
if (!ensureStackLocation(vregId, &defAlloc)) { |
| 1206 |
return false; |
| 1207 |
} |
| 1208 |
def->setOutput(defAlloc); |
| 1209 |
if (isGCType && !liveGC.insert(vregId)) { |
| 1210 |
return false; |
| 1211 |
} |
| 1212 |
if (i == numPhis - 1) { |
| 1213 |
freeDeadVregsAfterInstruction(liveGC, phi); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
// Allocate registers for each instruction. |
| 1218 |
for (LInstructionIterator iter = block->begin(); iter != block->end(); |
| 1219 |
iter++) { |
| 1220 |
if (mir->shouldCancel("allocateRegisters (instruction loop)")) { |
| 1221 |
return false; |
| 1222 |
} |
| 1223 |
|
| 1224 |
LInstruction* ins = *iter; |
| 1225 |
if (!allocateForInstruction(liveGC, blockLastId, ins)) { |
| 1226 |
return false; |
| 1227 |
} |
| 1228 |
if (ins == *block->rbegin() && !fuseWithNextBlock) { |
| 1229 |
if (!allocateForBlockEnd(block, ins)) { |
| 1230 |
return false; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
freeDeadVregsAfterInstruction(liveGC, ins); |
| 1234 |
} |
| 1235 |
|
| 1236 |
if (!fuseWithNextBlock) { |
| 1237 |
saveAndClearAllocatedRegisters(mblock); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
// All vregs must now be dead. |
| 1242 |
MOZ_ASSERT(vregLastUses_.empty()); |
| 1243 |
#ifdef DEBUG |
| 1244 |
for (size_t i = 1; i < vregs_.length(); i++) { |
| 1245 |
MOZ_ASSERT(vregs_[i].isDead()); |
| 1246 |
} |
| 1247 |
#endif |
| 1248 |
|
| 1249 |
graph.setLocalSlotsSize(stackSlotAllocator_.stackHeight()); |
| 1250 |
return true; |
| 1251 |
} |
| 1252 |
|
| 1253 |
void SimpleAllocator::assertValidRegisterStateBeforeInstruction() const { |
| 1254 |
#ifdef DEBUG |
| 1255 |
MOZ_ASSERT(!hasMultipleRegsForVreg_); |
| 1256 |
|
| 1257 |
// Assert allocatedRegs_ does not contain duplicate registers and matches the |
| 1258 |
// availableRegs_ set. |
| 1259 |
AllocatableRegisterSet available = allRegisters_; |
| 1260 |
for (size_t i = 0; i < allocatedRegs_.length(); i++) { |
| 1261 |
AllocatedRegister allocated = allocatedRegs_[i]; |
| 1262 |
available.take(allocated.reg()); |
| 1263 |
const VirtualRegister& vreg = vregs_[allocated.vregId()]; |
| 1264 |
MOZ_ASSERT(vreg.registerIndex() == i); |
| 1265 |
MOZ_ASSERT(!vreg.isTemp()); |
| 1266 |
} |
| 1267 |
MOZ_ASSERT(availableRegs_.set() == available.set()); |
| 1268 |
|
| 1269 |
// Assert vregs have a valid register index. Limit this to the first 20 vregs |
| 1270 |
// to not slow down debug builds too much. |
| 1271 |
size_t numVregsToCheck = std::min<size_t>(20, vregs_.length()); |
| 1272 |
for (size_t i = 1; i < numVregsToCheck; i++) { |
| 1273 |
if (!vregs_[i].isDead() && vregs_[i].hasRegister()) { |
| 1274 |
size_t index = vregs_[i].registerIndex(); |
| 1275 |
MOZ_ASSERT(allocatedRegs_[index].vregId() == i); |
| 1276 |
} |
| 1277 |
} |
| 1278 |
#endif |
| 1279 |
} |
| 1280 |
|
| 1281 |
bool SimpleAllocator::go() { |
| 1282 |
JitSpewCont(JitSpew_RegAlloc, "\n"); |
| 1283 |
JitSpew(JitSpew_RegAlloc, "Beginning register allocation"); |
| 1284 |
|
| 1285 |
JitSpewCont(JitSpew_RegAlloc, "\n"); |
| 1286 |
if (JitSpewEnabled(JitSpew_RegAlloc)) { |
| 1287 |
dumpInstructions("(Pre-allocation LIR)"); |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (!init()) { |
| 1291 |
return false; |
| 1292 |
} |
| 1293 |
if (!analyzeLiveness()) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
if (!allocateRegisters()) { |
| 1297 |
return false; |
| 1298 |
} |
| 1299 |
return true; |
| 1300 |
} |