| 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/ScalarReplacement.h" |
| 8 |
|
| 9 |
#include "jit/IonAnalysis.h" |
| 10 |
#include "jit/JitSpewer.h" |
| 11 |
#include "jit/MIR-wasm.h" |
| 12 |
#include "jit/MIR.h" |
| 13 |
#include "jit/MIRGenerator.h" |
| 14 |
#include "jit/MIRGraph.h" |
| 15 |
#include "jit/WarpBuilderShared.h" |
| 16 |
#include "js/Vector.h" |
| 17 |
#include "vm/ArgumentsObject.h" |
| 18 |
|
| 19 |
#include "gc/ObjectKind-inl.h" |
| 20 |
|
| 21 |
namespace js { |
| 22 |
namespace jit { |
| 23 |
|
| 24 |
template <typename MemoryView> |
| 25 |
class EmulateStateOf { |
| 26 |
private: |
| 27 |
using BlockState = typename MemoryView::BlockState; |
| 28 |
|
| 29 |
const MIRGenerator* mir_; |
| 30 |
MIRGraph& graph_; |
| 31 |
|
| 32 |
// Block state at the entrance of all basic blocks. |
| 33 |
Vector<BlockState*, 8, SystemAllocPolicy> states_; |
| 34 |
|
| 35 |
public: |
| 36 |
EmulateStateOf(const MIRGenerator* mir, MIRGraph& graph) |
| 37 |
: mir_(mir), graph_(graph) {} |
| 38 |
|
| 39 |
bool run(MemoryView& view); |
| 40 |
}; |
| 41 |
|
| 42 |
template <typename MemoryView> |
| 43 |
bool EmulateStateOf<MemoryView>::run(MemoryView& view) { |
| 44 |
// Initialize the current block state of each block to an unknown state. |
| 45 |
if (!states_.appendN(nullptr, graph_.numBlocks())) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// Initialize the first block which needs to be traversed in RPO. |
| 50 |
MBasicBlock* startBlock = view.startingBlock(); |
| 51 |
if (!view.initStartingState(&states_[startBlock->id()])) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
// Iterate over each basic block which has a valid entry state, and merge |
| 56 |
// the state in the successor blocks. |
| 57 |
for (ReversePostorderIterator block = graph_.rpoBegin(startBlock); |
| 58 |
block != graph_.rpoEnd(); block++) { |
| 59 |
if (mir_->shouldCancel(MemoryView::phaseName)) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
// Get the block state as the result of the merge of all predecessors |
| 64 |
// which have already been visited in RPO. This means that backedges |
| 65 |
// are not yet merged into the loop. |
| 66 |
BlockState* state = states_[block->id()]; |
| 67 |
if (!state) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
view.setEntryBlockState(state); |
| 71 |
|
| 72 |
// Iterates over resume points, phi and instructions. |
| 73 |
for (MNodeIterator iter(*block); iter;) { |
| 74 |
// Increment the iterator before visiting the instruction, as the |
| 75 |
// visit function might discard itself from the basic block. |
| 76 |
MNode* ins = *iter++; |
| 77 |
if (ins->isDefinition()) { |
| 78 |
MDefinition* def = ins->toDefinition(); |
| 79 |
switch (def->op()) { |
| 80 |
#define MIR_OP(op) \ |
| 81 |
case MDefinition::Opcode::op: \ |
| 82 |
view.visit##op(def->to##op()); \ |
| 83 |
break; |
| 84 |
MIR_OPCODE_LIST(MIR_OP) |
| 85 |
#undef MIR_OP |
| 86 |
} |
| 87 |
} else { |
| 88 |
view.visitResumePoint(ins->toResumePoint()); |
| 89 |
} |
| 90 |
if (!graph_.alloc().ensureBallast()) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
if (view.oom()) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// For each successor, merge the current state into the state of the |
| 99 |
// successors. |
| 100 |
for (size_t s = 0; s < block->numSuccessors(); s++) { |
| 101 |
MBasicBlock* succ = block->getSuccessor(s); |
| 102 |
if (!view.mergeIntoSuccessorState(*block, succ, &states_[succ->id()])) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
states_.clear(); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
static inline bool IsOptimizableObjectInstruction(MInstruction* ins) { |
| 113 |
return ins->isNewObject() || ins->isNewPlainObject() || |
| 114 |
ins->isNewCallObject() || ins->isNewIterator(); |
| 115 |
} |
| 116 |
|
| 117 |
static bool PhiOperandEqualTo(MDefinition* operand, MInstruction* newObject) { |
| 118 |
if (operand == newObject) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
switch (operand->op()) { |
| 123 |
case MDefinition::Opcode::GuardShape: |
| 124 |
return PhiOperandEqualTo(operand->toGuardShape()->input(), newObject); |
| 125 |
|
| 126 |
case MDefinition::Opcode::GuardToClass: |
| 127 |
return PhiOperandEqualTo(operand->toGuardToClass()->input(), newObject); |
| 128 |
|
| 129 |
case MDefinition::Opcode::CheckIsObj: |
| 130 |
return PhiOperandEqualTo(operand->toCheckIsObj()->input(), newObject); |
| 131 |
|
| 132 |
case MDefinition::Opcode::Unbox: |
| 133 |
return PhiOperandEqualTo(operand->toUnbox()->input(), newObject); |
| 134 |
|
| 135 |
default: |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Return true if all phi operands are equal to |newObject|. |
| 141 |
static bool PhiOperandsEqualTo(MPhi* phi, MInstruction* newObject) { |
| 142 |
MOZ_ASSERT(IsOptimizableObjectInstruction(newObject)); |
| 143 |
|
| 144 |
for (size_t i = 0, e = phi->numOperands(); i < e; i++) { |
| 145 |
if (!PhiOperandEqualTo(phi->getOperand(i), newObject)) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
static bool IsObjectEscaped(MDefinition* ins, MInstruction* newObject, |
| 153 |
const Shape* shapeDefault = nullptr); |
| 154 |
|
| 155 |
// Returns False if the lambda is not escaped and if it is optimizable by |
| 156 |
// ScalarReplacementOfObject. |
| 157 |
static bool IsLambdaEscaped(MInstruction* ins, MInstruction* lambda, |
| 158 |
MInstruction* newObject, const Shape* shape) { |
| 159 |
MOZ_ASSERT(lambda->isLambda() || lambda->isFunctionWithProto()); |
| 160 |
MOZ_ASSERT(IsOptimizableObjectInstruction(newObject)); |
| 161 |
JitSpewDef(JitSpew_Escape, "Check lambda\n", ins); |
| 162 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 163 |
|
| 164 |
// The scope chain is not escaped if none of the Lambdas which are |
| 165 |
// capturing it are escaped. |
| 166 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 167 |
MNode* consumer = (*i)->consumer(); |
| 168 |
if (!consumer->isDefinition()) { |
| 169 |
// Cannot optimize if it is observable from fun.arguments or others. |
| 170 |
if (!consumer->toResumePoint()->isRecoverableOperand(*i)) { |
| 171 |
JitSpew(JitSpew_Escape, "Observable lambda cannot be recovered"); |
| 172 |
return true; |
| 173 |
} |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
MDefinition* def = consumer->toDefinition(); |
| 178 |
switch (def->op()) { |
| 179 |
case MDefinition::Opcode::GuardToFunction: { |
| 180 |
auto* guard = def->toGuardToFunction(); |
| 181 |
if (IsLambdaEscaped(guard, lambda, newObject, shape)) { |
| 182 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 183 |
return true; |
| 184 |
} |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
case MDefinition::Opcode::GuardFunctionScript: { |
| 189 |
auto* guard = def->toGuardFunctionScript(); |
| 190 |
BaseScript* actual; |
| 191 |
if (lambda->isLambda()) { |
| 192 |
actual = lambda->toLambda()->templateFunction()->baseScript(); |
| 193 |
} else { |
| 194 |
actual = lambda->toFunctionWithProto()->function()->baseScript(); |
| 195 |
} |
| 196 |
if (actual != guard->expected()) { |
| 197 |
JitSpewDef(JitSpew_Escape, "has a non-matching script guard\n", |
| 198 |
guard); |
| 199 |
return true; |
| 200 |
} |
| 201 |
if (IsLambdaEscaped(guard, lambda, newObject, shape)) { |
| 202 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 203 |
return true; |
| 204 |
} |
| 205 |
break; |
| 206 |
} |
| 207 |
|
| 208 |
case MDefinition::Opcode::FunctionEnvironment: { |
| 209 |
if (IsObjectEscaped(def->toFunctionEnvironment(), newObject, shape)) { |
| 210 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 211 |
return true; |
| 212 |
} |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
default: |
| 217 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 218 |
return true; |
| 219 |
} |
| 220 |
} |
| 221 |
JitSpew(JitSpew_Escape, "Lambda is not escaped"); |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
static bool IsLambdaEscaped(MInstruction* lambda, MInstruction* newObject, |
| 226 |
const Shape* shape) { |
| 227 |
return IsLambdaEscaped(lambda, lambda, newObject, shape); |
| 228 |
} |
| 229 |
|
| 230 |
// Returns False if the object is not escaped and if it is optimizable by |
| 231 |
// ScalarReplacementOfObject. |
| 232 |
// |
| 233 |
// For the moment, this code is dumb as it only supports objects which are not |
| 234 |
// changing shape. |
| 235 |
static bool IsObjectEscaped(MDefinition* ins, MInstruction* newObject, |
| 236 |
const Shape* shapeDefault) { |
| 237 |
MOZ_ASSERT(ins->type() == MIRType::Object || ins->isPhi()); |
| 238 |
MOZ_ASSERT(IsOptimizableObjectInstruction(newObject)); |
| 239 |
|
| 240 |
JitSpewDef(JitSpew_Escape, "Check object\n", ins); |
| 241 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 242 |
|
| 243 |
const Shape* shape = shapeDefault; |
| 244 |
if (!shape) { |
| 245 |
if (ins->isNewPlainObject()) { |
| 246 |
shape = ins->toNewPlainObject()->shape(); |
| 247 |
} else if (JSObject* templateObj = MObjectState::templateObjectOf(ins)) { |
| 248 |
shape = templateObj->shape(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (!shape) { |
| 253 |
JitSpew(JitSpew_Escape, "No shape defined."); |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
// Check if the object is escaped. If the object is not the first argument |
| 258 |
// of either a known Store / Load, then we consider it as escaped. This is a |
| 259 |
// cheap and conservative escape analysis. |
| 260 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 261 |
MNode* consumer = (*i)->consumer(); |
| 262 |
if (!consumer->isDefinition()) { |
| 263 |
// Cannot optimize if it is observable from fun.arguments or others. |
| 264 |
if (!consumer->toResumePoint()->isRecoverableOperand(*i)) { |
| 265 |
JitSpew(JitSpew_Escape, "Observable object cannot be recovered"); |
| 266 |
return true; |
| 267 |
} |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
MDefinition* def = consumer->toDefinition(); |
| 272 |
switch (def->op()) { |
| 273 |
case MDefinition::Opcode::StoreFixedSlot: |
| 274 |
case MDefinition::Opcode::LoadFixedSlot: |
| 275 |
// Not escaped if it is the first argument. |
| 276 |
if (def->indexOf(*i) == 0) { |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 281 |
return true; |
| 282 |
|
| 283 |
case MDefinition::Opcode::PostWriteBarrier: |
| 284 |
break; |
| 285 |
|
| 286 |
case MDefinition::Opcode::Slots: { |
| 287 |
#ifdef DEBUG |
| 288 |
// Assert that MSlots are only used by MStoreDynamicSlot and |
| 289 |
// MLoadDynamicSlot. |
| 290 |
MSlots* ins = def->toSlots(); |
| 291 |
MOZ_ASSERT(ins->object() != 0); |
| 292 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 293 |
// toDefinition should normally never fail, since they don't get |
| 294 |
// captured by resume points. |
| 295 |
MDefinition* def = (*i)->consumer()->toDefinition(); |
| 296 |
MOZ_ASSERT(def->op() == MDefinition::Opcode::StoreDynamicSlot || |
| 297 |
def->op() == MDefinition::Opcode::LoadDynamicSlot); |
| 298 |
} |
| 299 |
#endif |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
case MDefinition::Opcode::GuardShape: { |
| 304 |
MGuardShape* guard = def->toGuardShape(); |
| 305 |
if (shape != guard->shape()) { |
| 306 |
JitSpewDef(JitSpew_Escape, "has a non-matching guard shape\n", guard); |
| 307 |
return true; |
| 308 |
} |
| 309 |
if (IsObjectEscaped(def->toInstruction(), newObject, shape)) { |
| 310 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 311 |
return true; |
| 312 |
} |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
case MDefinition::Opcode::GuardToClass: { |
| 317 |
MGuardToClass* guard = def->toGuardToClass(); |
| 318 |
if (!shape || shape->getObjectClass() != guard->getClass()) { |
| 319 |
JitSpewDef(JitSpew_Escape, "has a non-matching class guard\n", guard); |
| 320 |
return true; |
| 321 |
} |
| 322 |
if (IsObjectEscaped(def->toInstruction(), newObject, shape)) { |
| 323 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 324 |
return true; |
| 325 |
} |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
case MDefinition::Opcode::CheckIsObj: { |
| 330 |
if (IsObjectEscaped(def->toInstruction(), newObject, shape)) { |
| 331 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 332 |
return true; |
| 333 |
} |
| 334 |
break; |
| 335 |
} |
| 336 |
|
| 337 |
case MDefinition::Opcode::Unbox: { |
| 338 |
if (def->type() != MIRType::Object) { |
| 339 |
JitSpewDef(JitSpew_Escape, "has an invalid unbox\n", def); |
| 340 |
return true; |
| 341 |
} |
| 342 |
if (IsObjectEscaped(def->toInstruction(), newObject, shape)) { |
| 343 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 344 |
return true; |
| 345 |
} |
| 346 |
break; |
| 347 |
} |
| 348 |
|
| 349 |
case MDefinition::Opcode::Lambda: |
| 350 |
case MDefinition::Opcode::FunctionWithProto: { |
| 351 |
if (IsLambdaEscaped(def->toInstruction(), newObject, shape)) { |
| 352 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 353 |
return true; |
| 354 |
} |
| 355 |
break; |
| 356 |
} |
| 357 |
|
| 358 |
case MDefinition::Opcode::Phi: { |
| 359 |
auto* phi = def->toPhi(); |
| 360 |
if (!PhiOperandsEqualTo(phi, newObject)) { |
| 361 |
JitSpewDef(JitSpew_Escape, "has different phi operands\n", def); |
| 362 |
return true; |
| 363 |
} |
| 364 |
if (IsObjectEscaped(phi, newObject, shape)) { |
| 365 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 366 |
return true; |
| 367 |
} |
| 368 |
break; |
| 369 |
} |
| 370 |
|
| 371 |
case MDefinition::Opcode::Compare: { |
| 372 |
bool canFold; |
| 373 |
if (!def->toCompare()->tryFold(&canFold)) { |
| 374 |
JitSpewDef(JitSpew_Escape, "has an unsupported compare\n", def); |
| 375 |
return true; |
| 376 |
} |
| 377 |
break; |
| 378 |
} |
| 379 |
|
| 380 |
// Doesn't escape the object. |
| 381 |
case MDefinition::Opcode::IsObject: |
| 382 |
break; |
| 383 |
|
| 384 |
// This instruction is a no-op used to verify that scalar replacement |
| 385 |
// is working as expected in jit-test. |
| 386 |
case MDefinition::Opcode::AssertRecoveredOnBailout: |
| 387 |
break; |
| 388 |
|
| 389 |
// This is just a special flavor of constant which lets us optimize |
| 390 |
// out some guards in certain circumstances. We'll turn this into a |
| 391 |
// regular constant later. |
| 392 |
case MDefinition::Opcode::ConstantProto: |
| 393 |
break; |
| 394 |
|
| 395 |
// We definitely don't need barriers for objects that don't exist. |
| 396 |
case MDefinition::Opcode::AssertCanElidePostWriteBarrier: |
| 397 |
break; |
| 398 |
|
| 399 |
default: |
| 400 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 401 |
return true; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
JitSpew(JitSpew_Escape, "Object is not escaped"); |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
class ObjectMemoryView : public MDefinitionVisitorDefaultNoop { |
| 410 |
public: |
| 411 |
using BlockState = MObjectState; |
| 412 |
static const char phaseName[]; |
| 413 |
|
| 414 |
private: |
| 415 |
TempAllocator& alloc_; |
| 416 |
MConstant* undefinedVal_; |
| 417 |
MInstruction* obj_; |
| 418 |
MBasicBlock* startBlock_; |
| 419 |
BlockState* state_; |
| 420 |
|
| 421 |
// Used to improve the memory usage by sharing common modification. |
| 422 |
const MResumePoint* lastResumePoint_; |
| 423 |
|
| 424 |
bool oom_; |
| 425 |
|
| 426 |
public: |
| 427 |
ObjectMemoryView(TempAllocator& alloc, MInstruction* obj); |
| 428 |
|
| 429 |
MBasicBlock* startingBlock(); |
| 430 |
bool initStartingState(BlockState** outState); |
| 431 |
|
| 432 |
void setEntryBlockState(BlockState* state); |
| 433 |
bool mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ, |
| 434 |
BlockState** pSuccState); |
| 435 |
|
| 436 |
#ifdef DEBUG |
| 437 |
void assertSuccess(); |
| 438 |
#else |
| 439 |
void assertSuccess() {} |
| 440 |
#endif |
| 441 |
|
| 442 |
bool oom() const { return oom_; } |
| 443 |
|
| 444 |
private: |
| 445 |
MDefinition* functionForCallObject(MDefinition* ins); |
| 446 |
|
| 447 |
public: |
| 448 |
void visitResumePoint(MResumePoint* rp); |
| 449 |
void visitObjectState(MObjectState* ins); |
| 450 |
void visitStoreFixedSlot(MStoreFixedSlot* ins); |
| 451 |
void visitLoadFixedSlot(MLoadFixedSlot* ins); |
| 452 |
void visitPostWriteBarrier(MPostWriteBarrier* ins); |
| 453 |
void visitStoreDynamicSlot(MStoreDynamicSlot* ins); |
| 454 |
void visitLoadDynamicSlot(MLoadDynamicSlot* ins); |
| 455 |
void visitGuardShape(MGuardShape* ins); |
| 456 |
void visitGuardToClass(MGuardToClass* ins); |
| 457 |
void visitCheckIsObj(MCheckIsObj* ins); |
| 458 |
void visitUnbox(MUnbox* ins); |
| 459 |
void visitFunctionEnvironment(MFunctionEnvironment* ins); |
| 460 |
void visitGuardToFunction(MGuardToFunction* ins); |
| 461 |
void visitGuardFunctionScript(MGuardFunctionScript* ins); |
| 462 |
void visitLambda(MLambda* ins); |
| 463 |
void visitFunctionWithProto(MFunctionWithProto* ins); |
| 464 |
void visitPhi(MPhi* ins); |
| 465 |
void visitCompare(MCompare* ins); |
| 466 |
void visitConstantProto(MConstantProto* ins); |
| 467 |
void visitIsObject(MIsObject* ins); |
| 468 |
void visitAssertCanElidePostWriteBarrier( |
| 469 |
MAssertCanElidePostWriteBarrier* ins); |
| 470 |
}; |
| 471 |
|
| 472 |
/* static */ const char ObjectMemoryView::phaseName[] = |
| 473 |
"Scalar Replacement of Object"; |
| 474 |
|
| 475 |
ObjectMemoryView::ObjectMemoryView(TempAllocator& alloc, MInstruction* obj) |
| 476 |
: alloc_(alloc), |
| 477 |
undefinedVal_(nullptr), |
| 478 |
obj_(obj), |
| 479 |
startBlock_(obj->block()), |
| 480 |
state_(nullptr), |
| 481 |
lastResumePoint_(nullptr), |
| 482 |
oom_(false) { |
| 483 |
// Annotate snapshots RValue such that we recover the store first. |
| 484 |
obj_->setIncompleteObject(); |
| 485 |
|
| 486 |
// Annotate the instruction such that we do not replace it by a |
| 487 |
// Magic(JS_OPTIMIZED_OUT) in case of removed uses. |
| 488 |
obj_->setImplicitlyUsedUnchecked(); |
| 489 |
} |
| 490 |
|
| 491 |
MBasicBlock* ObjectMemoryView::startingBlock() { return startBlock_; } |
| 492 |
|
| 493 |
bool ObjectMemoryView::initStartingState(BlockState** outState) { |
| 494 |
// Uninitialized slots have an "undefined" value. |
| 495 |
undefinedVal_ = MConstant::New(alloc_, UndefinedValue()); |
| 496 |
startBlock_->insertBefore(obj_, undefinedVal_); |
| 497 |
|
| 498 |
// Create a new block state and insert at it at the location of the new |
| 499 |
// object. |
| 500 |
BlockState* state = BlockState::New(alloc_, obj_); |
| 501 |
if (!state) { |
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
startBlock_->insertAfter(obj_, state); |
| 506 |
|
| 507 |
// Initialize the properties of the object state. |
| 508 |
state->initFromTemplateObject(alloc_, undefinedVal_); |
| 509 |
|
| 510 |
// Hold out of resume point until it is visited. |
| 511 |
state->setInWorklist(); |
| 512 |
|
| 513 |
*outState = state; |
| 514 |
return true; |
| 515 |
} |
| 516 |
|
| 517 |
void ObjectMemoryView::setEntryBlockState(BlockState* state) { state_ = state; } |
| 518 |
|
| 519 |
bool ObjectMemoryView::mergeIntoSuccessorState(MBasicBlock* curr, |
| 520 |
MBasicBlock* succ, |
| 521 |
BlockState** pSuccState) { |
| 522 |
BlockState* succState = *pSuccState; |
| 523 |
|
| 524 |
// When a block has no state yet, create an empty one for the |
| 525 |
// successor. |
| 526 |
if (!succState) { |
| 527 |
// If the successor is not dominated then the object cannot flow |
| 528 |
// in this basic block without a Phi. We know that no Phi exist |
| 529 |
// in non-dominated successors as the conservative escaped |
| 530 |
// analysis fails otherwise. Such condition can succeed if the |
| 531 |
// successor is a join at the end of a if-block and the object |
| 532 |
// only exists within the branch. |
| 533 |
if (!startBlock_->dominates(succ)) { |
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
// If there is only one predecessor, carry over the last state of the |
| 538 |
// block to the successor. As the block state is immutable, if the |
| 539 |
// current block has multiple successors, they will share the same entry |
| 540 |
// state. |
| 541 |
if (succ->numPredecessors() <= 1 || !state_->numSlots()) { |
| 542 |
*pSuccState = state_; |
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
// If we have multiple predecessors, then we allocate one Phi node for |
| 547 |
// each predecessor, and create a new block state which only has phi |
| 548 |
// nodes. These would later be removed by the removal of redundant phi |
| 549 |
// nodes. |
| 550 |
succState = BlockState::Copy(alloc_, state_); |
| 551 |
if (!succState) { |
| 552 |
return false; |
| 553 |
} |
| 554 |
|
| 555 |
size_t numPreds = succ->numPredecessors(); |
| 556 |
for (size_t slot = 0; slot < state_->numSlots(); slot++) { |
| 557 |
MPhi* phi = MPhi::New(alloc_.fallible()); |
| 558 |
if (!phi || !phi->reserveLength(numPreds)) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
// Fill the input of the successors Phi with undefined |
| 563 |
// values, and each block later fills the Phi inputs. |
| 564 |
for (size_t p = 0; p < numPreds; p++) { |
| 565 |
phi->addInput(undefinedVal_); |
| 566 |
} |
| 567 |
|
| 568 |
// Add Phi in the list of Phis of the basic block. |
| 569 |
succ->addPhi(phi); |
| 570 |
succState->setSlot(slot, phi); |
| 571 |
} |
| 572 |
|
| 573 |
// Insert the newly created block state instruction at the beginning |
| 574 |
// of the successor block, after all the phi nodes. Note that it |
| 575 |
// would be captured by the entry resume point of the successor |
| 576 |
// block. |
| 577 |
succ->insertBefore(succ->safeInsertTop(), succState); |
| 578 |
*pSuccState = succState; |
| 579 |
} |
| 580 |
|
| 581 |
MOZ_ASSERT_IF(succ == startBlock_, startBlock_->isLoopHeader()); |
| 582 |
if (succ->numPredecessors() > 1 && succState->numSlots() && |
| 583 |
succ != startBlock_) { |
| 584 |
// We need to re-compute successorWithPhis as the previous EliminatePhis |
| 585 |
// phase might have removed all the Phis from the successor block. |
| 586 |
size_t currIndex; |
| 587 |
MOZ_ASSERT(!succ->phisEmpty()); |
| 588 |
if (curr->successorWithPhis()) { |
| 589 |
MOZ_ASSERT(curr->successorWithPhis() == succ); |
| 590 |
currIndex = curr->positionInPhiSuccessor(); |
| 591 |
} else { |
| 592 |
currIndex = succ->indexForPredecessor(curr); |
| 593 |
curr->setSuccessorWithPhis(succ, currIndex); |
| 594 |
} |
| 595 |
MOZ_ASSERT(succ->getPredecessor(currIndex) == curr); |
| 596 |
|
| 597 |
// Copy the current slot states to the index of current block in all the |
| 598 |
// Phi created during the first visit of the successor. |
| 599 |
for (size_t slot = 0; slot < state_->numSlots(); slot++) { |
| 600 |
MPhi* phi = succState->getSlot(slot)->toPhi(); |
| 601 |
phi->replaceOperand(currIndex, state_->getSlot(slot)); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
return true; |
| 606 |
} |
| 607 |
|
| 608 |
#ifdef DEBUG |
| 609 |
void ObjectMemoryView::assertSuccess() { |
| 610 |
for (MUseIterator i(obj_->usesBegin()); i != obj_->usesEnd(); i++) { |
| 611 |
MNode* ins = (*i)->consumer(); |
| 612 |
MDefinition* def = nullptr; |
| 613 |
|
| 614 |
// Resume points have been replaced by the object state. |
| 615 |
if (ins->isResumePoint() || |
| 616 |
(def = ins->toDefinition())->isRecoveredOnBailout()) { |
| 617 |
MOZ_ASSERT(obj_->isIncompleteObject()); |
| 618 |
continue; |
| 619 |
} |
| 620 |
|
| 621 |
// The only remaining uses would be removed by DCE, which will also |
| 622 |
// recover the object on bailouts. |
| 623 |
MOZ_ASSERT(def->isSlots() || def->isLambda() || def->isFunctionWithProto()); |
| 624 |
MOZ_ASSERT(!def->hasDefUses()); |
| 625 |
} |
| 626 |
} |
| 627 |
#endif |
| 628 |
|
| 629 |
void ObjectMemoryView::visitResumePoint(MResumePoint* rp) { |
| 630 |
// As long as the MObjectState is not yet seen next to the allocation, we do |
| 631 |
// not patch the resume point to recover the side effects. |
| 632 |
if (!state_->isInWorklist()) { |
| 633 |
rp->addStore(alloc_, state_, lastResumePoint_); |
| 634 |
lastResumePoint_ = rp; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
void ObjectMemoryView::visitObjectState(MObjectState* ins) { |
| 639 |
if (ins->isInWorklist()) { |
| 640 |
ins->setNotInWorklist(); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
void ObjectMemoryView::visitStoreFixedSlot(MStoreFixedSlot* ins) { |
| 645 |
// Skip stores made on other objects. |
| 646 |
if (ins->object() != obj_) { |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
// Clone the state and update the slot value. |
| 651 |
if (state_->hasFixedSlot(ins->slot())) { |
| 652 |
state_ = BlockState::Copy(alloc_, state_); |
| 653 |
if (!state_) { |
| 654 |
oom_ = true; |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
state_->setFixedSlot(ins->slot(), ins->value()); |
| 659 |
ins->block()->insertBefore(ins->toInstruction(), state_); |
| 660 |
} else { |
| 661 |
// UnsafeSetReserveSlot can access baked-in slots which are guarded by |
| 662 |
// conditions, which are not seen by the escape analysis. |
| 663 |
MBail* bailout = MBail::New(alloc_, BailoutKind::Inevitable); |
| 664 |
ins->block()->insertBefore(ins, bailout); |
| 665 |
} |
| 666 |
|
| 667 |
// Remove original instruction. |
| 668 |
ins->block()->discard(ins); |
| 669 |
} |
| 670 |
|
| 671 |
void ObjectMemoryView::visitLoadFixedSlot(MLoadFixedSlot* ins) { |
| 672 |
// Skip loads made on other objects. |
| 673 |
if (ins->object() != obj_) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
// Replace load by the slot value. |
| 678 |
if (state_->hasFixedSlot(ins->slot())) { |
| 679 |
ins->replaceAllUsesWith(state_->getFixedSlot(ins->slot())); |
| 680 |
} else { |
| 681 |
// UnsafeGetReserveSlot can access baked-in slots which are guarded by |
| 682 |
// conditions, which are not seen by the escape analysis. |
| 683 |
MBail* bailout = MBail::New(alloc_, BailoutKind::Inevitable); |
| 684 |
ins->block()->insertBefore(ins, bailout); |
| 685 |
ins->replaceAllUsesWith(undefinedVal_); |
| 686 |
} |
| 687 |
|
| 688 |
// Remove original instruction. |
| 689 |
ins->block()->discard(ins); |
| 690 |
} |
| 691 |
|
| 692 |
void ObjectMemoryView::visitPostWriteBarrier(MPostWriteBarrier* ins) { |
| 693 |
// Skip loads made on other objects. |
| 694 |
if (ins->object() != obj_) { |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
// Remove original instruction. |
| 699 |
ins->block()->discard(ins); |
| 700 |
} |
| 701 |
|
| 702 |
void ObjectMemoryView::visitStoreDynamicSlot(MStoreDynamicSlot* ins) { |
| 703 |
// Skip stores made on other objects. |
| 704 |
MSlots* slots = ins->slots()->toSlots(); |
| 705 |
if (slots->object() != obj_) { |
| 706 |
// Guard objects are replaced when they are visited. |
| 707 |
MOZ_ASSERT(!slots->object()->isGuardShape() || |
| 708 |
slots->object()->toGuardShape()->object() != obj_); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
// Clone the state and update the slot value. |
| 713 |
if (state_->hasDynamicSlot(ins->slot())) { |
| 714 |
state_ = BlockState::Copy(alloc_, state_); |
| 715 |
if (!state_) { |
| 716 |
oom_ = true; |
| 717 |
return; |
| 718 |
} |
| 719 |
|
| 720 |
state_->setDynamicSlot(ins->slot(), ins->value()); |
| 721 |
ins->block()->insertBefore(ins->toInstruction(), state_); |
| 722 |
} else { |
| 723 |
// UnsafeSetReserveSlot can access baked-in slots which are guarded by |
| 724 |
// conditions, which are not seen by the escape analysis. |
| 725 |
MBail* bailout = MBail::New(alloc_, BailoutKind::Inevitable); |
| 726 |
ins->block()->insertBefore(ins, bailout); |
| 727 |
} |
| 728 |
|
| 729 |
// Remove original instruction. |
| 730 |
ins->block()->discard(ins); |
| 731 |
} |
| 732 |
|
| 733 |
void ObjectMemoryView::visitLoadDynamicSlot(MLoadDynamicSlot* ins) { |
| 734 |
// Skip loads made on other objects. |
| 735 |
MSlots* slots = ins->slots()->toSlots(); |
| 736 |
if (slots->object() != obj_) { |
| 737 |
// Guard objects are replaced when they are visited. |
| 738 |
MOZ_ASSERT(!slots->object()->isGuardShape() || |
| 739 |
slots->object()->toGuardShape()->object() != obj_); |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
// Replace load by the slot value. |
| 744 |
if (state_->hasDynamicSlot(ins->slot())) { |
| 745 |
ins->replaceAllUsesWith(state_->getDynamicSlot(ins->slot())); |
| 746 |
} else { |
| 747 |
// UnsafeGetReserveSlot can access baked-in slots which are guarded by |
| 748 |
// conditions, which are not seen by the escape analysis. |
| 749 |
MBail* bailout = MBail::New(alloc_, BailoutKind::Inevitable); |
| 750 |
ins->block()->insertBefore(ins, bailout); |
| 751 |
ins->replaceAllUsesWith(undefinedVal_); |
| 752 |
} |
| 753 |
|
| 754 |
// Remove original instruction. |
| 755 |
ins->block()->discard(ins); |
| 756 |
} |
| 757 |
|
| 758 |
void ObjectMemoryView::visitGuardShape(MGuardShape* ins) { |
| 759 |
// Skip guards on other objects. |
| 760 |
if (ins->object() != obj_) { |
| 761 |
return; |
| 762 |
} |
| 763 |
|
| 764 |
// Replace the guard by its object. |
| 765 |
ins->replaceAllUsesWith(obj_); |
| 766 |
|
| 767 |
// Remove original instruction. |
| 768 |
ins->block()->discard(ins); |
| 769 |
} |
| 770 |
|
| 771 |
void ObjectMemoryView::visitGuardToClass(MGuardToClass* ins) { |
| 772 |
// Skip guards on other objects. |
| 773 |
if (ins->object() != obj_) { |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
// Replace the guard by its object. |
| 778 |
ins->replaceAllUsesWith(obj_); |
| 779 |
|
| 780 |
// Remove original instruction. |
| 781 |
ins->block()->discard(ins); |
| 782 |
} |
| 783 |
|
| 784 |
void ObjectMemoryView::visitCheckIsObj(MCheckIsObj* ins) { |
| 785 |
// Skip checks on other objects. |
| 786 |
if (ins->input() != obj_) { |
| 787 |
return; |
| 788 |
} |
| 789 |
|
| 790 |
// Replace the check by its object. |
| 791 |
ins->replaceAllUsesWith(obj_); |
| 792 |
|
| 793 |
// Remove original instruction. |
| 794 |
ins->block()->discard(ins); |
| 795 |
} |
| 796 |
|
| 797 |
void ObjectMemoryView::visitUnbox(MUnbox* ins) { |
| 798 |
// Skip unrelated unboxes. |
| 799 |
if (ins->input() != obj_) { |
| 800 |
return; |
| 801 |
} |
| 802 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 803 |
|
| 804 |
// Replace the unbox with the object. |
| 805 |
ins->replaceAllUsesWith(obj_); |
| 806 |
|
| 807 |
// Remove the unbox. |
| 808 |
ins->block()->discard(ins); |
| 809 |
} |
| 810 |
|
| 811 |
MDefinition* ObjectMemoryView::functionForCallObject(MDefinition* ins) { |
| 812 |
// Return early when we don't replace MNewCallObject. |
| 813 |
if (!obj_->isNewCallObject()) { |
| 814 |
return nullptr; |
| 815 |
} |
| 816 |
|
| 817 |
// Unwrap instructions until we found either MLambda or MFunctionWithProto. |
| 818 |
// Return the function instruction if their environment chain matches the |
| 819 |
// MNewCallObject we're about to replace. |
| 820 |
while (true) { |
| 821 |
switch (ins->op()) { |
| 822 |
case MDefinition::Opcode::Lambda: { |
| 823 |
if (ins->toLambda()->environmentChain() == obj_) { |
| 824 |
return ins; |
| 825 |
} |
| 826 |
return nullptr; |
| 827 |
} |
| 828 |
case MDefinition::Opcode::FunctionWithProto: { |
| 829 |
if (ins->toFunctionWithProto()->environmentChain() == obj_) { |
| 830 |
return ins; |
| 831 |
} |
| 832 |
return nullptr; |
| 833 |
} |
| 834 |
case MDefinition::Opcode::FunctionEnvironment: |
| 835 |
ins = ins->toFunctionEnvironment()->function(); |
| 836 |
break; |
| 837 |
case MDefinition::Opcode::GuardToFunction: |
| 838 |
ins = ins->toGuardToFunction()->object(); |
| 839 |
break; |
| 840 |
case MDefinition::Opcode::GuardFunctionScript: |
| 841 |
ins = ins->toGuardFunctionScript()->function(); |
| 842 |
break; |
| 843 |
default: |
| 844 |
return nullptr; |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
void ObjectMemoryView::visitFunctionEnvironment(MFunctionEnvironment* ins) { |
| 850 |
// Skip function environment which are not aliases of the NewCallObject. |
| 851 |
if (!functionForCallObject(ins)) { |
| 852 |
return; |
| 853 |
} |
| 854 |
|
| 855 |
// Replace the function environment by the scope chain of the lambda. |
| 856 |
ins->replaceAllUsesWith(obj_); |
| 857 |
|
| 858 |
// Remove original instruction. |
| 859 |
ins->block()->discard(ins); |
| 860 |
} |
| 861 |
|
| 862 |
void ObjectMemoryView::visitGuardToFunction(MGuardToFunction* ins) { |
| 863 |
// Skip guards on other objects. |
| 864 |
auto* function = functionForCallObject(ins); |
| 865 |
if (!function) { |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
// Replace the guard by its object. |
| 870 |
ins->replaceAllUsesWith(function); |
| 871 |
|
| 872 |
// Remove original instruction. |
| 873 |
ins->block()->discard(ins); |
| 874 |
} |
| 875 |
|
| 876 |
void ObjectMemoryView::visitGuardFunctionScript(MGuardFunctionScript* ins) { |
| 877 |
// Skip guards on other objects. |
| 878 |
auto* function = functionForCallObject(ins); |
| 879 |
if (!function) { |
| 880 |
return; |
| 881 |
} |
| 882 |
|
| 883 |
// Replace the guard by its object. |
| 884 |
ins->replaceAllUsesWith(function); |
| 885 |
|
| 886 |
// Remove original instruction. |
| 887 |
ins->block()->discard(ins); |
| 888 |
} |
| 889 |
|
| 890 |
void ObjectMemoryView::visitLambda(MLambda* ins) { |
| 891 |
if (ins->environmentChain() != obj_) { |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
// In order to recover the lambda we need to recover the scope chain, as the |
| 896 |
// lambda is holding it. |
| 897 |
ins->setIncompleteObject(); |
| 898 |
} |
| 899 |
|
| 900 |
void ObjectMemoryView::visitFunctionWithProto(MFunctionWithProto* ins) { |
| 901 |
if (ins->environmentChain() != obj_) { |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
ins->setIncompleteObject(); |
| 906 |
} |
| 907 |
|
| 908 |
void ObjectMemoryView::visitPhi(MPhi* ins) { |
| 909 |
// Skip phis on other objects. |
| 910 |
if (!PhiOperandsEqualTo(ins, obj_)) { |
| 911 |
return; |
| 912 |
} |
| 913 |
|
| 914 |
// Replace the phi by its object. |
| 915 |
ins->replaceAllUsesWith(obj_); |
| 916 |
|
| 917 |
// Remove original instruction. |
| 918 |
ins->block()->discardPhi(ins); |
| 919 |
} |
| 920 |
|
| 921 |
void ObjectMemoryView::visitCompare(MCompare* ins) { |
| 922 |
// Skip unrelated comparisons. |
| 923 |
if (ins->lhs() != obj_ && ins->rhs() != obj_) { |
| 924 |
return; |
| 925 |
} |
| 926 |
|
| 927 |
bool folded; |
| 928 |
MOZ_ALWAYS_TRUE(ins->tryFold(&folded)); |
| 929 |
|
| 930 |
auto* cst = MConstant::New(alloc_, BooleanValue(folded)); |
| 931 |
ins->block()->insertBefore(ins, cst); |
| 932 |
|
| 933 |
// Replace the comparison with a constant. |
| 934 |
ins->replaceAllUsesWith(cst); |
| 935 |
|
| 936 |
// Remove original instruction. |
| 937 |
ins->block()->discard(ins); |
| 938 |
} |
| 939 |
|
| 940 |
void ObjectMemoryView::visitConstantProto(MConstantProto* ins) { |
| 941 |
if (ins->getReceiverObject() != obj_) { |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
auto* cst = ins->protoObject(); |
| 946 |
ins->replaceAllUsesWith(cst); |
| 947 |
ins->block()->discard(ins); |
| 948 |
} |
| 949 |
|
| 950 |
void ObjectMemoryView::visitIsObject(MIsObject* ins) { |
| 951 |
// Skip unrelated tests. |
| 952 |
if (ins->input() != obj_) { |
| 953 |
return; |
| 954 |
} |
| 955 |
|
| 956 |
auto* cst = MConstant::New(alloc_, BooleanValue(true)); |
| 957 |
ins->block()->insertBefore(ins, cst); |
| 958 |
|
| 959 |
// Replace the test with a constant. |
| 960 |
ins->replaceAllUsesWith(cst); |
| 961 |
|
| 962 |
// Remove original instruction. |
| 963 |
ins->block()->discard(ins); |
| 964 |
} |
| 965 |
|
| 966 |
void ObjectMemoryView::visitAssertCanElidePostWriteBarrier( |
| 967 |
MAssertCanElidePostWriteBarrier* ins) { |
| 968 |
if (ins->object() != obj_) { |
| 969 |
return; |
| 970 |
} |
| 971 |
|
| 972 |
ins->block()->discard(ins); |
| 973 |
} |
| 974 |
|
| 975 |
static bool IndexOf(MDefinition* ins, int32_t* res) { |
| 976 |
MOZ_ASSERT(ins->isLoadElement() || ins->isStoreElement()); |
| 977 |
MDefinition* indexDef = ins->getOperand(1); // ins->index(); |
| 978 |
if (indexDef->isSpectreMaskIndex()) { |
| 979 |
indexDef = indexDef->toSpectreMaskIndex()->index(); |
| 980 |
} |
| 981 |
if (indexDef->isBoundsCheck()) { |
| 982 |
indexDef = indexDef->toBoundsCheck()->index(); |
| 983 |
} |
| 984 |
if (indexDef->isToNumberInt32()) { |
| 985 |
indexDef = indexDef->toToNumberInt32()->getOperand(0); |
| 986 |
} |
| 987 |
MConstant* indexDefConst = indexDef->maybeConstantValue(); |
| 988 |
if (!indexDefConst || indexDefConst->type() != MIRType::Int32) { |
| 989 |
return false; |
| 990 |
} |
| 991 |
*res = indexDefConst->toInt32(); |
| 992 |
return true; |
| 993 |
} |
| 994 |
|
| 995 |
static inline bool IsOptimizableArrayInstruction(MInstruction* ins) { |
| 996 |
return ins->isNewArray() || ins->isNewArrayObject(); |
| 997 |
} |
| 998 |
|
| 999 |
// We don't support storing holes when doing scalar replacement, so any |
| 1000 |
// optimizable MNewArrayObject instruction is guaranteed to be packed. |
| 1001 |
static inline bool IsPackedArray(MInstruction* ins) { |
| 1002 |
return ins->isNewArrayObject(); |
| 1003 |
} |
| 1004 |
|
| 1005 |
// Returns False if the elements is not escaped and if it is optimizable by |
| 1006 |
// ScalarReplacementOfArray. |
| 1007 |
static bool IsElementEscaped(MDefinition* def, MInstruction* newArray, |
| 1008 |
uint32_t arraySize) { |
| 1009 |
MOZ_ASSERT(def->isElements()); |
| 1010 |
MOZ_ASSERT(IsOptimizableArrayInstruction(newArray)); |
| 1011 |
|
| 1012 |
JitSpewDef(JitSpew_Escape, "Check elements\n", def); |
| 1013 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 1014 |
|
| 1015 |
for (MUseIterator i(def->usesBegin()); i != def->usesEnd(); i++) { |
| 1016 |
// The MIRType::Elements cannot be captured in a resume point as |
| 1017 |
// it does not represent a value allocation. |
| 1018 |
MDefinition* access = (*i)->consumer()->toDefinition(); |
| 1019 |
|
| 1020 |
switch (access->op()) { |
| 1021 |
case MDefinition::Opcode::LoadElement: { |
| 1022 |
MOZ_ASSERT(access->toLoadElement()->elements() == def); |
| 1023 |
|
| 1024 |
// If the index is not a constant then this index can alias |
| 1025 |
// all others. We do not handle this case. |
| 1026 |
int32_t index; |
| 1027 |
if (!IndexOf(access, &index)) { |
| 1028 |
JitSpewDef(JitSpew_Escape, |
| 1029 |
"has a load element with a non-trivial index\n", access); |
| 1030 |
return true; |
| 1031 |
} |
| 1032 |
if (index < 0 || arraySize <= uint32_t(index)) { |
| 1033 |
JitSpewDef(JitSpew_Escape, |
| 1034 |
"has a load element with an out-of-bound index\n", access); |
| 1035 |
return true; |
| 1036 |
} |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
|
| 1040 |
case MDefinition::Opcode::StoreElement: { |
| 1041 |
MStoreElement* storeElem = access->toStoreElement(); |
| 1042 |
MOZ_ASSERT(storeElem->elements() == def); |
| 1043 |
|
| 1044 |
// StoreElement must bail out if it stores to a hole, in case |
| 1045 |
// there is a setter on the prototype chain. If this StoreElement |
| 1046 |
// might store to a hole, we can't scalar-replace it. |
| 1047 |
if (storeElem->needsHoleCheck()) { |
| 1048 |
JitSpewDef(JitSpew_Escape, "has a store element with a hole check\n", |
| 1049 |
storeElem); |
| 1050 |
return true; |
| 1051 |
} |
| 1052 |
|
| 1053 |
// If the index is not a constant then this index can alias |
| 1054 |
// all others. We do not handle this case. |
| 1055 |
int32_t index; |
| 1056 |
if (!IndexOf(storeElem, &index)) { |
| 1057 |
JitSpewDef(JitSpew_Escape, |
| 1058 |
"has a store element with a non-trivial index\n", |
| 1059 |
storeElem); |
| 1060 |
return true; |
| 1061 |
} |
| 1062 |
if (index < 0 || arraySize <= uint32_t(index)) { |
| 1063 |
JitSpewDef(JitSpew_Escape, |
| 1064 |
"has a store element with an out-of-bound index\n", |
| 1065 |
storeElem); |
| 1066 |
return true; |
| 1067 |
} |
| 1068 |
|
| 1069 |
// Dense element holes are written using MStoreHoleValueElement instead |
| 1070 |
// of MStoreElement. |
| 1071 |
MOZ_ASSERT(storeElem->value()->type() != MIRType::MagicHole); |
| 1072 |
break; |
| 1073 |
} |
| 1074 |
|
| 1075 |
case MDefinition::Opcode::SetInitializedLength: |
| 1076 |
MOZ_ASSERT(access->toSetInitializedLength()->elements() == def); |
| 1077 |
break; |
| 1078 |
|
| 1079 |
case MDefinition::Opcode::InitializedLength: |
| 1080 |
MOZ_ASSERT(access->toInitializedLength()->elements() == def); |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case MDefinition::Opcode::ArrayLength: |
| 1084 |
MOZ_ASSERT(access->toArrayLength()->elements() == def); |
| 1085 |
break; |
| 1086 |
|
| 1087 |
case MDefinition::Opcode::ApplyArray: |
| 1088 |
MOZ_ASSERT(access->toApplyArray()->getElements() == def); |
| 1089 |
if (!IsPackedArray(newArray)) { |
| 1090 |
JitSpewDef(JitSpew_Escape, "is not guaranteed to be packed\n", |
| 1091 |
access); |
| 1092 |
return true; |
| 1093 |
} |
| 1094 |
break; |
| 1095 |
|
| 1096 |
case MDefinition::Opcode::ConstructArray: |
| 1097 |
MOZ_ASSERT(access->toConstructArray()->getElements() == def); |
| 1098 |
if (!IsPackedArray(newArray)) { |
| 1099 |
JitSpewDef(JitSpew_Escape, "is not guaranteed to be packed\n", |
| 1100 |
access); |
| 1101 |
return true; |
| 1102 |
} |
| 1103 |
break; |
| 1104 |
|
| 1105 |
default: |
| 1106 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", access); |
| 1107 |
return true; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
JitSpew(JitSpew_Escape, "Elements is not escaped"); |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
|
| 1114 |
// Returns False if the array is not escaped and if it is optimizable by |
| 1115 |
// ScalarReplacementOfArray. |
| 1116 |
// |
| 1117 |
// For the moment, this code is dumb as it only supports arrays which are not |
| 1118 |
// changing length, with only access with known constants. |
| 1119 |
static bool IsArrayEscaped(MInstruction* ins, MInstruction* newArray) { |
| 1120 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 1121 |
MOZ_ASSERT(IsOptimizableArrayInstruction(newArray)); |
| 1122 |
|
| 1123 |
JitSpewDef(JitSpew_Escape, "Check array\n", ins); |
| 1124 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 1125 |
|
| 1126 |
const Shape* shape; |
| 1127 |
uint32_t length; |
| 1128 |
if (newArray->isNewArrayObject()) { |
| 1129 |
length = newArray->toNewArrayObject()->length(); |
| 1130 |
shape = newArray->toNewArrayObject()->shape(); |
| 1131 |
} else { |
| 1132 |
length = newArray->toNewArray()->length(); |
| 1133 |
JSObject* templateObject = newArray->toNewArray()->templateObject(); |
| 1134 |
if (!templateObject) { |
| 1135 |
JitSpew(JitSpew_Escape, "No template object defined."); |
| 1136 |
return true; |
| 1137 |
} |
| 1138 |
shape = templateObject->shape(); |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (length >= 16) { |
| 1142 |
JitSpew(JitSpew_Escape, "Array has too many elements"); |
| 1143 |
return true; |
| 1144 |
} |
| 1145 |
|
| 1146 |
// Check if the object is escaped. If the object is not the first argument |
| 1147 |
// of either a known Store / Load, then we consider it as escaped. This is a |
| 1148 |
// cheap and conservative escape analysis. |
| 1149 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 1150 |
MNode* consumer = (*i)->consumer(); |
| 1151 |
if (!consumer->isDefinition()) { |
| 1152 |
// Cannot optimize if it is observable from fun.arguments or others. |
| 1153 |
if (!consumer->toResumePoint()->isRecoverableOperand(*i)) { |
| 1154 |
JitSpew(JitSpew_Escape, "Observable array cannot be recovered"); |
| 1155 |
return true; |
| 1156 |
} |
| 1157 |
continue; |
| 1158 |
} |
| 1159 |
|
| 1160 |
MDefinition* def = consumer->toDefinition(); |
| 1161 |
switch (def->op()) { |
| 1162 |
case MDefinition::Opcode::Elements: { |
| 1163 |
MElements* elem = def->toElements(); |
| 1164 |
MOZ_ASSERT(elem->object() == ins); |
| 1165 |
if (IsElementEscaped(elem, newArray, length)) { |
| 1166 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", elem); |
| 1167 |
return true; |
| 1168 |
} |
| 1169 |
|
| 1170 |
break; |
| 1171 |
} |
| 1172 |
|
| 1173 |
case MDefinition::Opcode::GuardShape: { |
| 1174 |
MGuardShape* guard = def->toGuardShape(); |
| 1175 |
if (shape != guard->shape()) { |
| 1176 |
JitSpewDef(JitSpew_Escape, "has a non-matching guard shape\n", guard); |
| 1177 |
return true; |
| 1178 |
} |
| 1179 |
if (IsArrayEscaped(guard, newArray)) { |
| 1180 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1181 |
return true; |
| 1182 |
} |
| 1183 |
|
| 1184 |
break; |
| 1185 |
} |
| 1186 |
|
| 1187 |
case MDefinition::Opcode::GuardToClass: { |
| 1188 |
MGuardToClass* guard = def->toGuardToClass(); |
| 1189 |
if (shape->getObjectClass() != guard->getClass()) { |
| 1190 |
JitSpewDef(JitSpew_Escape, "has a non-matching class guard\n", guard); |
| 1191 |
return true; |
| 1192 |
} |
| 1193 |
if (IsArrayEscaped(guard, newArray)) { |
| 1194 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1195 |
return true; |
| 1196 |
} |
| 1197 |
|
| 1198 |
break; |
| 1199 |
} |
| 1200 |
|
| 1201 |
case MDefinition::Opcode::GuardArrayIsPacked: { |
| 1202 |
auto* guard = def->toGuardArrayIsPacked(); |
| 1203 |
if (!IsPackedArray(newArray)) { |
| 1204 |
JitSpewDef(JitSpew_Escape, "is not guaranteed to be packed\n", def); |
| 1205 |
return true; |
| 1206 |
} |
| 1207 |
if (IsArrayEscaped(guard, newArray)) { |
| 1208 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1209 |
return true; |
| 1210 |
} |
| 1211 |
break; |
| 1212 |
} |
| 1213 |
|
| 1214 |
case MDefinition::Opcode::Unbox: { |
| 1215 |
if (def->type() != MIRType::Object) { |
| 1216 |
JitSpewDef(JitSpew_Escape, "has an invalid unbox\n", def); |
| 1217 |
return true; |
| 1218 |
} |
| 1219 |
if (IsArrayEscaped(def->toInstruction(), newArray)) { |
| 1220 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1221 |
return true; |
| 1222 |
} |
| 1223 |
break; |
| 1224 |
} |
| 1225 |
|
| 1226 |
// This instruction is supported for |JSOp::OptimizeSpreadCall|. |
| 1227 |
case MDefinition::Opcode::Compare: { |
| 1228 |
bool canFold; |
| 1229 |
if (!def->toCompare()->tryFold(&canFold)) { |
| 1230 |
JitSpewDef(JitSpew_Escape, "has an unsupported compare\n", def); |
| 1231 |
return true; |
| 1232 |
} |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
|
| 1236 |
case MDefinition::Opcode::PostWriteBarrier: |
| 1237 |
case MDefinition::Opcode::PostWriteElementBarrier: |
| 1238 |
break; |
| 1239 |
|
| 1240 |
// This instruction is a no-op used to verify that scalar replacement |
| 1241 |
// is working as expected in jit-test. |
| 1242 |
case MDefinition::Opcode::AssertRecoveredOnBailout: |
| 1243 |
break; |
| 1244 |
|
| 1245 |
default: |
| 1246 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 1247 |
return true; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
JitSpew(JitSpew_Escape, "Array is not escaped"); |
| 1252 |
return false; |
| 1253 |
} |
| 1254 |
|
| 1255 |
// This class replaces every MStoreElement and MSetInitializedLength by an |
| 1256 |
// MArrayState which emulates the content of the array. All MLoadElement, |
| 1257 |
// MInitializedLength and MArrayLength are replaced by the corresponding value. |
| 1258 |
// |
| 1259 |
// In order to restore the value of the array correctly in case of bailouts, we |
| 1260 |
// replace all reference of the allocation by the MArrayState definition. |
| 1261 |
class ArrayMemoryView : public MDefinitionVisitorDefaultNoop { |
| 1262 |
public: |
| 1263 |
using BlockState = MArrayState; |
| 1264 |
static const char* phaseName; |
| 1265 |
|
| 1266 |
private: |
| 1267 |
TempAllocator& alloc_; |
| 1268 |
MConstant* undefinedVal_; |
| 1269 |
MConstant* length_; |
| 1270 |
MInstruction* arr_; |
| 1271 |
MBasicBlock* startBlock_; |
| 1272 |
BlockState* state_; |
| 1273 |
|
| 1274 |
// Used to improve the memory usage by sharing common modification. |
| 1275 |
const MResumePoint* lastResumePoint_; |
| 1276 |
|
| 1277 |
bool oom_; |
| 1278 |
|
| 1279 |
public: |
| 1280 |
ArrayMemoryView(TempAllocator& alloc, MInstruction* arr); |
| 1281 |
|
| 1282 |
MBasicBlock* startingBlock(); |
| 1283 |
bool initStartingState(BlockState** pState); |
| 1284 |
|
| 1285 |
void setEntryBlockState(BlockState* state); |
| 1286 |
bool mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ, |
| 1287 |
BlockState** pSuccState); |
| 1288 |
|
| 1289 |
#ifdef DEBUG |
| 1290 |
void assertSuccess(); |
| 1291 |
#else |
| 1292 |
void assertSuccess() {} |
| 1293 |
#endif |
| 1294 |
|
| 1295 |
bool oom() const { return oom_; } |
| 1296 |
|
| 1297 |
private: |
| 1298 |
bool isArrayStateElements(MDefinition* elements); |
| 1299 |
void discardInstruction(MInstruction* ins, MDefinition* elements); |
| 1300 |
|
| 1301 |
public: |
| 1302 |
void visitResumePoint(MResumePoint* rp); |
| 1303 |
void visitArrayState(MArrayState* ins); |
| 1304 |
void visitStoreElement(MStoreElement* ins); |
| 1305 |
void visitLoadElement(MLoadElement* ins); |
| 1306 |
void visitSetInitializedLength(MSetInitializedLength* ins); |
| 1307 |
void visitInitializedLength(MInitializedLength* ins); |
| 1308 |
void visitArrayLength(MArrayLength* ins); |
| 1309 |
void visitPostWriteBarrier(MPostWriteBarrier* ins); |
| 1310 |
void visitPostWriteElementBarrier(MPostWriteElementBarrier* ins); |
| 1311 |
void visitGuardShape(MGuardShape* ins); |
| 1312 |
void visitGuardToClass(MGuardToClass* ins); |
| 1313 |
void visitGuardArrayIsPacked(MGuardArrayIsPacked* ins); |
| 1314 |
void visitUnbox(MUnbox* ins); |
| 1315 |
void visitCompare(MCompare* ins); |
| 1316 |
void visitApplyArray(MApplyArray* ins); |
| 1317 |
void visitConstructArray(MConstructArray* ins); |
| 1318 |
}; |
| 1319 |
|
| 1320 |
const char* ArrayMemoryView::phaseName = "Scalar Replacement of Array"; |
| 1321 |
|
| 1322 |
ArrayMemoryView::ArrayMemoryView(TempAllocator& alloc, MInstruction* arr) |
| 1323 |
: alloc_(alloc), |
| 1324 |
undefinedVal_(nullptr), |
| 1325 |
length_(nullptr), |
| 1326 |
arr_(arr), |
| 1327 |
startBlock_(arr->block()), |
| 1328 |
state_(nullptr), |
| 1329 |
lastResumePoint_(nullptr), |
| 1330 |
oom_(false) { |
| 1331 |
// Annotate snapshots RValue such that we recover the store first. |
| 1332 |
arr_->setIncompleteObject(); |
| 1333 |
|
| 1334 |
// Annotate the instruction such that we do not replace it by a |
| 1335 |
// Magic(JS_OPTIMIZED_OUT) in case of removed uses. |
| 1336 |
arr_->setImplicitlyUsedUnchecked(); |
| 1337 |
} |
| 1338 |
|
| 1339 |
MBasicBlock* ArrayMemoryView::startingBlock() { return startBlock_; } |
| 1340 |
|
| 1341 |
bool ArrayMemoryView::initStartingState(BlockState** pState) { |
| 1342 |
// Uninitialized elements have an "undefined" value. |
| 1343 |
undefinedVal_ = MConstant::New(alloc_, UndefinedValue()); |
| 1344 |
MConstant* initLength = MConstant::New(alloc_, Int32Value(0)); |
| 1345 |
arr_->block()->insertBefore(arr_, undefinedVal_); |
| 1346 |
arr_->block()->insertBefore(arr_, initLength); |
| 1347 |
|
| 1348 |
// Create a new block state and insert at it at the location of the new array. |
| 1349 |
BlockState* state = BlockState::New(alloc_, arr_, initLength); |
| 1350 |
if (!state) { |
| 1351 |
return false; |
| 1352 |
} |
| 1353 |
|
| 1354 |
startBlock_->insertAfter(arr_, state); |
| 1355 |
|
| 1356 |
// Initialize the elements of the array state. |
| 1357 |
state->initFromTemplateObject(alloc_, undefinedVal_); |
| 1358 |
|
| 1359 |
// Hold out of resume point until it is visited. |
| 1360 |
state->setInWorklist(); |
| 1361 |
|
| 1362 |
*pState = state; |
| 1363 |
return true; |
| 1364 |
} |
| 1365 |
|
| 1366 |
void ArrayMemoryView::setEntryBlockState(BlockState* state) { state_ = state; } |
| 1367 |
|
| 1368 |
bool ArrayMemoryView::mergeIntoSuccessorState(MBasicBlock* curr, |
| 1369 |
MBasicBlock* succ, |
| 1370 |
BlockState** pSuccState) { |
| 1371 |
BlockState* succState = *pSuccState; |
| 1372 |
|
| 1373 |
// When a block has no state yet, create an empty one for the |
| 1374 |
// successor. |
| 1375 |
if (!succState) { |
| 1376 |
// If the successor is not dominated then the array cannot flow |
| 1377 |
// in this basic block without a Phi. We know that no Phi exist |
| 1378 |
// in non-dominated successors as the conservative escaped |
| 1379 |
// analysis fails otherwise. Such condition can succeed if the |
| 1380 |
// successor is a join at the end of a if-block and the array |
| 1381 |
// only exists within the branch. |
| 1382 |
if (!startBlock_->dominates(succ)) { |
| 1383 |
return true; |
| 1384 |
} |
| 1385 |
|
| 1386 |
// If there is only one predecessor, carry over the last state of the |
| 1387 |
// block to the successor. As the block state is immutable, if the |
| 1388 |
// current block has multiple successors, they will share the same entry |
| 1389 |
// state. |
| 1390 |
if (succ->numPredecessors() <= 1 || !state_->numElements()) { |
| 1391 |
*pSuccState = state_; |
| 1392 |
return true; |
| 1393 |
} |
| 1394 |
|
| 1395 |
// If we have multiple predecessors, then we allocate one Phi node for |
| 1396 |
// each predecessor, and create a new block state which only has phi |
| 1397 |
// nodes. These would later be removed by the removal of redundant phi |
| 1398 |
// nodes. |
| 1399 |
succState = BlockState::Copy(alloc_, state_); |
| 1400 |
if (!succState) { |
| 1401 |
return false; |
| 1402 |
} |
| 1403 |
|
| 1404 |
size_t numPreds = succ->numPredecessors(); |
| 1405 |
for (size_t index = 0; index < state_->numElements(); index++) { |
| 1406 |
MPhi* phi = MPhi::New(alloc_.fallible()); |
| 1407 |
if (!phi || !phi->reserveLength(numPreds)) { |
| 1408 |
return false; |
| 1409 |
} |
| 1410 |
|
| 1411 |
// Fill the input of the successors Phi with undefined |
| 1412 |
// values, and each block later fills the Phi inputs. |
| 1413 |
for (size_t p = 0; p < numPreds; p++) { |
| 1414 |
phi->addInput(undefinedVal_); |
| 1415 |
} |
| 1416 |
|
| 1417 |
// Add Phi in the list of Phis of the basic block. |
| 1418 |
succ->addPhi(phi); |
| 1419 |
succState->setElement(index, phi); |
| 1420 |
} |
| 1421 |
|
| 1422 |
// Insert the newly created block state instruction at the beginning |
| 1423 |
// of the successor block, after all the phi nodes. Note that it |
| 1424 |
// would be captured by the entry resume point of the successor |
| 1425 |
// block. |
| 1426 |
succ->insertBefore(succ->safeInsertTop(), succState); |
| 1427 |
*pSuccState = succState; |
| 1428 |
} |
| 1429 |
|
| 1430 |
MOZ_ASSERT_IF(succ == startBlock_, startBlock_->isLoopHeader()); |
| 1431 |
if (succ->numPredecessors() > 1 && succState->numElements() && |
| 1432 |
succ != startBlock_) { |
| 1433 |
// We need to re-compute successorWithPhis as the previous EliminatePhis |
| 1434 |
// phase might have removed all the Phis from the successor block. |
| 1435 |
size_t currIndex; |
| 1436 |
MOZ_ASSERT(!succ->phisEmpty()); |
| 1437 |
if (curr->successorWithPhis()) { |
| 1438 |
MOZ_ASSERT(curr->successorWithPhis() == succ); |
| 1439 |
currIndex = curr->positionInPhiSuccessor(); |
| 1440 |
} else { |
| 1441 |
currIndex = succ->indexForPredecessor(curr); |
| 1442 |
curr->setSuccessorWithPhis(succ, currIndex); |
| 1443 |
} |
| 1444 |
MOZ_ASSERT(succ->getPredecessor(currIndex) == curr); |
| 1445 |
|
| 1446 |
// Copy the current element states to the index of current block in all |
| 1447 |
// the Phi created during the first visit of the successor. |
| 1448 |
for (size_t index = 0; index < state_->numElements(); index++) { |
| 1449 |
MPhi* phi = succState->getElement(index)->toPhi(); |
| 1450 |
phi->replaceOperand(currIndex, state_->getElement(index)); |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
return true; |
| 1455 |
} |
| 1456 |
|
| 1457 |
#ifdef DEBUG |
| 1458 |
void ArrayMemoryView::assertSuccess() { MOZ_ASSERT(!arr_->hasLiveDefUses()); } |
| 1459 |
#endif |
| 1460 |
|
| 1461 |
void ArrayMemoryView::visitResumePoint(MResumePoint* rp) { |
| 1462 |
// As long as the MArrayState is not yet seen next to the allocation, we do |
| 1463 |
// not patch the resume point to recover the side effects. |
| 1464 |
if (!state_->isInWorklist()) { |
| 1465 |
rp->addStore(alloc_, state_, lastResumePoint_); |
| 1466 |
lastResumePoint_ = rp; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
void ArrayMemoryView::visitArrayState(MArrayState* ins) { |
| 1471 |
if (ins->isInWorklist()) { |
| 1472 |
ins->setNotInWorklist(); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
bool ArrayMemoryView::isArrayStateElements(MDefinition* elements) { |
| 1477 |
return elements->isElements() && elements->toElements()->object() == arr_; |
| 1478 |
} |
| 1479 |
|
| 1480 |
void ArrayMemoryView::discardInstruction(MInstruction* ins, |
| 1481 |
MDefinition* elements) { |
| 1482 |
MOZ_ASSERT(elements->isElements()); |
| 1483 |
ins->block()->discard(ins); |
| 1484 |
if (!elements->hasLiveDefUses()) { |
| 1485 |
elements->block()->discard(elements->toInstruction()); |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
void ArrayMemoryView::visitStoreElement(MStoreElement* ins) { |
| 1490 |
// Skip other array objects. |
| 1491 |
MDefinition* elements = ins->elements(); |
| 1492 |
if (!isArrayStateElements(elements)) { |
| 1493 |
return; |
| 1494 |
} |
| 1495 |
|
| 1496 |
// Register value of the setter in the state. |
| 1497 |
int32_t index; |
| 1498 |
MOZ_ALWAYS_TRUE(IndexOf(ins, &index)); |
| 1499 |
state_ = BlockState::Copy(alloc_, state_); |
| 1500 |
if (!state_) { |
| 1501 |
oom_ = true; |
| 1502 |
return; |
| 1503 |
} |
| 1504 |
|
| 1505 |
state_->setElement(index, ins->value()); |
| 1506 |
ins->block()->insertBefore(ins, state_); |
| 1507 |
|
| 1508 |
// Remove original instruction. |
| 1509 |
discardInstruction(ins, elements); |
| 1510 |
} |
| 1511 |
|
| 1512 |
void ArrayMemoryView::visitLoadElement(MLoadElement* ins) { |
| 1513 |
// Skip other array objects. |
| 1514 |
MDefinition* elements = ins->elements(); |
| 1515 |
if (!isArrayStateElements(elements)) { |
| 1516 |
return; |
| 1517 |
} |
| 1518 |
|
| 1519 |
// Replace by the value contained at the index. |
| 1520 |
int32_t index; |
| 1521 |
MOZ_ALWAYS_TRUE(IndexOf(ins, &index)); |
| 1522 |
|
| 1523 |
// The only way to store a hole value in a new array is with |
| 1524 |
// StoreHoleValueElement, which IsElementEscaped does not allow. |
| 1525 |
// Therefore, we do not have to do a hole check. |
| 1526 |
MDefinition* element = state_->getElement(index); |
| 1527 |
MOZ_ASSERT(element->type() != MIRType::MagicHole); |
| 1528 |
|
| 1529 |
ins->replaceAllUsesWith(element); |
| 1530 |
|
| 1531 |
// Remove original instruction. |
| 1532 |
discardInstruction(ins, elements); |
| 1533 |
} |
| 1534 |
|
| 1535 |
void ArrayMemoryView::visitSetInitializedLength(MSetInitializedLength* ins) { |
| 1536 |
// Skip other array objects. |
| 1537 |
MDefinition* elements = ins->elements(); |
| 1538 |
if (!isArrayStateElements(elements)) { |
| 1539 |
return; |
| 1540 |
} |
| 1541 |
|
| 1542 |
// Replace by the new initialized length. Note that the argument of |
| 1543 |
// MSetInitializedLength is the last index and not the initialized length. |
| 1544 |
// To obtain the length, we need to add 1 to it, and thus we need to create |
| 1545 |
// a new constant that we register in the ArrayState. |
| 1546 |
state_ = BlockState::Copy(alloc_, state_); |
| 1547 |
if (!state_) { |
| 1548 |
oom_ = true; |
| 1549 |
return; |
| 1550 |
} |
| 1551 |
|
| 1552 |
int32_t initLengthValue = ins->index()->maybeConstantValue()->toInt32() + 1; |
| 1553 |
MConstant* initLength = MConstant::New(alloc_, Int32Value(initLengthValue)); |
| 1554 |
ins->block()->insertBefore(ins, initLength); |
| 1555 |
ins->block()->insertBefore(ins, state_); |
| 1556 |
state_->setInitializedLength(initLength); |
| 1557 |
|
| 1558 |
// Remove original instruction. |
| 1559 |
discardInstruction(ins, elements); |
| 1560 |
} |
| 1561 |
|
| 1562 |
void ArrayMemoryView::visitInitializedLength(MInitializedLength* ins) { |
| 1563 |
// Skip other array objects. |
| 1564 |
MDefinition* elements = ins->elements(); |
| 1565 |
if (!isArrayStateElements(elements)) { |
| 1566 |
return; |
| 1567 |
} |
| 1568 |
|
| 1569 |
// Replace by the value of the length. |
| 1570 |
ins->replaceAllUsesWith(state_->initializedLength()); |
| 1571 |
|
| 1572 |
// Remove original instruction. |
| 1573 |
discardInstruction(ins, elements); |
| 1574 |
} |
| 1575 |
|
| 1576 |
void ArrayMemoryView::visitArrayLength(MArrayLength* ins) { |
| 1577 |
// Skip other array objects. |
| 1578 |
MDefinition* elements = ins->elements(); |
| 1579 |
if (!isArrayStateElements(elements)) { |
| 1580 |
return; |
| 1581 |
} |
| 1582 |
|
| 1583 |
// Replace by the value of the length. |
| 1584 |
if (!length_) { |
| 1585 |
length_ = MConstant::New(alloc_, Int32Value(state_->numElements())); |
| 1586 |
arr_->block()->insertBefore(arr_, length_); |
| 1587 |
} |
| 1588 |
ins->replaceAllUsesWith(length_); |
| 1589 |
|
| 1590 |
// Remove original instruction. |
| 1591 |
discardInstruction(ins, elements); |
| 1592 |
} |
| 1593 |
|
| 1594 |
void ArrayMemoryView::visitPostWriteBarrier(MPostWriteBarrier* ins) { |
| 1595 |
// Skip barriers on other objects. |
| 1596 |
if (ins->object() != arr_) { |
| 1597 |
return; |
| 1598 |
} |
| 1599 |
|
| 1600 |
// Remove original instruction. |
| 1601 |
ins->block()->discard(ins); |
| 1602 |
} |
| 1603 |
|
| 1604 |
void ArrayMemoryView::visitPostWriteElementBarrier( |
| 1605 |
MPostWriteElementBarrier* ins) { |
| 1606 |
// Skip barriers on other objects. |
| 1607 |
if (ins->object() != arr_) { |
| 1608 |
return; |
| 1609 |
} |
| 1610 |
|
| 1611 |
// Remove original instruction. |
| 1612 |
ins->block()->discard(ins); |
| 1613 |
} |
| 1614 |
|
| 1615 |
void ArrayMemoryView::visitGuardShape(MGuardShape* ins) { |
| 1616 |
// Skip guards on other objects. |
| 1617 |
if (ins->object() != arr_) { |
| 1618 |
return; |
| 1619 |
} |
| 1620 |
|
| 1621 |
// Replace the guard by its object. |
| 1622 |
ins->replaceAllUsesWith(arr_); |
| 1623 |
|
| 1624 |
// Remove original instruction. |
| 1625 |
ins->block()->discard(ins); |
| 1626 |
} |
| 1627 |
|
| 1628 |
void ArrayMemoryView::visitGuardToClass(MGuardToClass* ins) { |
| 1629 |
// Skip guards on other objects. |
| 1630 |
if (ins->object() != arr_) { |
| 1631 |
return; |
| 1632 |
} |
| 1633 |
|
| 1634 |
// Replace the guard by its object. |
| 1635 |
ins->replaceAllUsesWith(arr_); |
| 1636 |
|
| 1637 |
// Remove original instruction. |
| 1638 |
ins->block()->discard(ins); |
| 1639 |
} |
| 1640 |
|
| 1641 |
void ArrayMemoryView::visitGuardArrayIsPacked(MGuardArrayIsPacked* ins) { |
| 1642 |
// Skip guards on other objects. |
| 1643 |
if (ins->array() != arr_) { |
| 1644 |
return; |
| 1645 |
} |
| 1646 |
|
| 1647 |
// Replace the guard by its object. |
| 1648 |
ins->replaceAllUsesWith(arr_); |
| 1649 |
|
| 1650 |
// Remove original instruction. |
| 1651 |
ins->block()->discard(ins); |
| 1652 |
} |
| 1653 |
|
| 1654 |
void ArrayMemoryView::visitUnbox(MUnbox* ins) { |
| 1655 |
// Skip unrelated unboxes. |
| 1656 |
if (ins->getOperand(0) != arr_) { |
| 1657 |
return; |
| 1658 |
} |
| 1659 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 1660 |
|
| 1661 |
// Replace the unbox with the array object. |
| 1662 |
ins->replaceAllUsesWith(arr_); |
| 1663 |
|
| 1664 |
// Remove the unbox. |
| 1665 |
ins->block()->discard(ins); |
| 1666 |
} |
| 1667 |
|
| 1668 |
void ArrayMemoryView::visitCompare(MCompare* ins) { |
| 1669 |
// Skip unrelated comparisons. |
| 1670 |
if (ins->lhs() != arr_ && ins->rhs() != arr_) { |
| 1671 |
return; |
| 1672 |
} |
| 1673 |
|
| 1674 |
bool folded; |
| 1675 |
MOZ_ALWAYS_TRUE(ins->tryFold(&folded)); |
| 1676 |
|
| 1677 |
auto* cst = MConstant::New(alloc_, BooleanValue(folded)); |
| 1678 |
ins->block()->insertBefore(ins, cst); |
| 1679 |
|
| 1680 |
// Replace the comparison with a constant. |
| 1681 |
ins->replaceAllUsesWith(cst); |
| 1682 |
|
| 1683 |
// Remove original instruction. |
| 1684 |
ins->block()->discard(ins); |
| 1685 |
} |
| 1686 |
|
| 1687 |
void ArrayMemoryView::visitApplyArray(MApplyArray* ins) { |
| 1688 |
// Skip other array objects. |
| 1689 |
MDefinition* elements = ins->getElements(); |
| 1690 |
if (!isArrayStateElements(elements)) { |
| 1691 |
return; |
| 1692 |
} |
| 1693 |
|
| 1694 |
uint32_t numElements = state_->numElements(); |
| 1695 |
|
| 1696 |
CallInfo callInfo(alloc_, /*constructing=*/false, ins->ignoresReturnValue()); |
| 1697 |
if (!callInfo.initForApplyArray(ins->getFunction(), ins->getThis(), |
| 1698 |
numElements)) { |
| 1699 |
oom_ = true; |
| 1700 |
return; |
| 1701 |
} |
| 1702 |
|
| 1703 |
for (uint32_t i = 0; i < numElements; i++) { |
| 1704 |
auto* element = state_->getElement(i); |
| 1705 |
MOZ_ASSERT(element->type() != MIRType::MagicHole); |
| 1706 |
|
| 1707 |
callInfo.initArg(i, element); |
| 1708 |
} |
| 1709 |
|
| 1710 |
auto addUndefined = [this]() { return undefinedVal_; }; |
| 1711 |
|
| 1712 |
bool needsThisCheck = false; |
| 1713 |
bool isDOMCall = false; |
| 1714 |
auto* call = MakeCall(alloc_, addUndefined, callInfo, needsThisCheck, |
| 1715 |
ins->getSingleTarget(), isDOMCall); |
| 1716 |
if (!call) { |
| 1717 |
oom_ = true; |
| 1718 |
return; |
| 1719 |
} |
| 1720 |
if (!ins->maybeCrossRealm()) { |
| 1721 |
call->setNotCrossRealm(); |
| 1722 |
} |
| 1723 |
|
| 1724 |
ins->block()->insertBefore(ins, call); |
| 1725 |
ins->replaceAllUsesWith(call); |
| 1726 |
|
| 1727 |
call->stealResumePoint(ins); |
| 1728 |
|
| 1729 |
// Remove original instruction. |
| 1730 |
discardInstruction(ins, elements); |
| 1731 |
} |
| 1732 |
|
| 1733 |
void ArrayMemoryView::visitConstructArray(MConstructArray* ins) { |
| 1734 |
// Skip other array objects. |
| 1735 |
MDefinition* elements = ins->getElements(); |
| 1736 |
if (!isArrayStateElements(elements)) { |
| 1737 |
return; |
| 1738 |
} |
| 1739 |
|
| 1740 |
uint32_t numElements = state_->numElements(); |
| 1741 |
|
| 1742 |
CallInfo callInfo(alloc_, /*constructing=*/true, ins->ignoresReturnValue()); |
| 1743 |
if (!callInfo.initForConstructArray(ins->getFunction(), ins->getThis(), |
| 1744 |
ins->getNewTarget(), numElements)) { |
| 1745 |
oom_ = true; |
| 1746 |
return; |
| 1747 |
} |
| 1748 |
|
| 1749 |
for (uint32_t i = 0; i < numElements; i++) { |
| 1750 |
auto* element = state_->getElement(i); |
| 1751 |
MOZ_ASSERT(element->type() != MIRType::MagicHole); |
| 1752 |
|
| 1753 |
callInfo.initArg(i, element); |
| 1754 |
} |
| 1755 |
|
| 1756 |
auto addUndefined = [this]() { return undefinedVal_; }; |
| 1757 |
|
| 1758 |
bool needsThisCheck = ins->needsThisCheck(); |
| 1759 |
bool isDOMCall = false; |
| 1760 |
auto* call = MakeCall(alloc_, addUndefined, callInfo, needsThisCheck, |
| 1761 |
ins->getSingleTarget(), isDOMCall); |
| 1762 |
if (!call) { |
| 1763 |
oom_ = true; |
| 1764 |
return; |
| 1765 |
} |
| 1766 |
if (!ins->maybeCrossRealm()) { |
| 1767 |
call->setNotCrossRealm(); |
| 1768 |
} |
| 1769 |
|
| 1770 |
ins->block()->insertBefore(ins, call); |
| 1771 |
ins->replaceAllUsesWith(call); |
| 1772 |
|
| 1773 |
call->stealResumePoint(ins); |
| 1774 |
|
| 1775 |
// Remove original instruction. |
| 1776 |
discardInstruction(ins, elements); |
| 1777 |
} |
| 1778 |
|
| 1779 |
static inline bool IsOptimizableArgumentsInstruction(MInstruction* ins) { |
| 1780 |
return ins->isCreateArgumentsObject() || |
| 1781 |
ins->isCreateInlinedArgumentsObject(); |
| 1782 |
} |
| 1783 |
|
| 1784 |
class ArgumentsReplacer : public MDefinitionVisitorDefaultNoop { |
| 1785 |
private: |
| 1786 |
const MIRGenerator* mir_; |
| 1787 |
MIRGraph& graph_; |
| 1788 |
MInstruction* args_; |
| 1789 |
|
| 1790 |
bool oom_ = false; |
| 1791 |
|
| 1792 |
TempAllocator& alloc() { return graph_.alloc(); } |
| 1793 |
|
| 1794 |
bool isInlinedArguments() const { |
| 1795 |
return args_->isCreateInlinedArgumentsObject(); |
| 1796 |
} |
| 1797 |
|
| 1798 |
MNewArrayObject* inlineArgsArray(MInstruction* ins, Shape* shape, |
| 1799 |
uint32_t begin, uint32_t count); |
| 1800 |
|
| 1801 |
void visitGuardToClass(MGuardToClass* ins); |
| 1802 |
void visitGuardProto(MGuardProto* ins); |
| 1803 |
void visitGuardArgumentsObjectFlags(MGuardArgumentsObjectFlags* ins); |
| 1804 |
void visitUnbox(MUnbox* ins); |
| 1805 |
void visitGetArgumentsObjectArg(MGetArgumentsObjectArg* ins); |
| 1806 |
void visitLoadArgumentsObjectArg(MLoadArgumentsObjectArg* ins); |
| 1807 |
void visitLoadArgumentsObjectArgHole(MLoadArgumentsObjectArgHole* ins); |
| 1808 |
void visitInArgumentsObjectArg(MInArgumentsObjectArg* ins); |
| 1809 |
void visitArgumentsObjectLength(MArgumentsObjectLength* ins); |
| 1810 |
void visitApplyArgsObj(MApplyArgsObj* ins); |
| 1811 |
void visitArrayFromArgumentsObject(MArrayFromArgumentsObject* ins); |
| 1812 |
void visitArgumentsSlice(MArgumentsSlice* ins); |
| 1813 |
void visitLoadFixedSlot(MLoadFixedSlot* ins); |
| 1814 |
|
| 1815 |
bool oom() const { return oom_; } |
| 1816 |
|
| 1817 |
public: |
| 1818 |
ArgumentsReplacer(const MIRGenerator* mir, MIRGraph& graph, |
| 1819 |
MInstruction* args) |
| 1820 |
: mir_(mir), graph_(graph), args_(args) { |
| 1821 |
MOZ_ASSERT(IsOptimizableArgumentsInstruction(args_)); |
| 1822 |
} |
| 1823 |
|
| 1824 |
bool escapes(MInstruction* ins, bool guardedForMapped = false); |
| 1825 |
bool run(); |
| 1826 |
void assertSuccess(); |
| 1827 |
}; |
| 1828 |
|
| 1829 |
// Returns false if the arguments object does not escape. |
| 1830 |
bool ArgumentsReplacer::escapes(MInstruction* ins, bool guardedForMapped) { |
| 1831 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 1832 |
|
| 1833 |
JitSpewDef(JitSpew_Escape, "Check arguments object\n", ins); |
| 1834 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 1835 |
|
| 1836 |
// We can replace inlined arguments in scripts with OSR entries, but |
| 1837 |
// the outermost arguments object has already been allocated before |
| 1838 |
// we enter via OSR and can't be replaced. |
| 1839 |
if (ins->isCreateArgumentsObject() && graph_.osrBlock()) { |
| 1840 |
JitSpew(JitSpew_Escape, "Can't replace outermost OSR arguments"); |
| 1841 |
return true; |
| 1842 |
} |
| 1843 |
|
| 1844 |
// Check all uses to see whether they can be supported without |
| 1845 |
// allocating an ArgumentsObject. |
| 1846 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 1847 |
MNode* consumer = (*i)->consumer(); |
| 1848 |
|
| 1849 |
// If a resume point can observe this instruction, we can only optimize |
| 1850 |
// if it is recoverable. |
| 1851 |
if (consumer->isResumePoint()) { |
| 1852 |
if (!consumer->toResumePoint()->isRecoverableOperand(*i)) { |
| 1853 |
JitSpew(JitSpew_Escape, "Observable args object cannot be recovered"); |
| 1854 |
return true; |
| 1855 |
} |
| 1856 |
continue; |
| 1857 |
} |
| 1858 |
|
| 1859 |
MDefinition* def = consumer->toDefinition(); |
| 1860 |
switch (def->op()) { |
| 1861 |
case MDefinition::Opcode::GuardToClass: { |
| 1862 |
MGuardToClass* guard = def->toGuardToClass(); |
| 1863 |
if (!guard->isArgumentsObjectClass()) { |
| 1864 |
JitSpewDef(JitSpew_Escape, "has a non-matching class guard\n", guard); |
| 1865 |
return true; |
| 1866 |
} |
| 1867 |
bool isMapped = guard->getClass() == &MappedArgumentsObject::class_; |
| 1868 |
if (escapes(guard, isMapped)) { |
| 1869 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1870 |
return true; |
| 1871 |
} |
| 1872 |
break; |
| 1873 |
} |
| 1874 |
|
| 1875 |
case MDefinition::Opcode::GuardProto: { |
| 1876 |
if (escapes(def->toInstruction(), guardedForMapped)) { |
| 1877 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1878 |
return true; |
| 1879 |
} |
| 1880 |
break; |
| 1881 |
} |
| 1882 |
|
| 1883 |
case MDefinition::Opcode::GuardArgumentsObjectFlags: { |
| 1884 |
if (escapes(def->toInstruction(), guardedForMapped)) { |
| 1885 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1886 |
return true; |
| 1887 |
} |
| 1888 |
break; |
| 1889 |
} |
| 1890 |
|
| 1891 |
case MDefinition::Opcode::Unbox: { |
| 1892 |
if (def->type() != MIRType::Object) { |
| 1893 |
JitSpewDef(JitSpew_Escape, "has an invalid unbox\n", def); |
| 1894 |
return true; |
| 1895 |
} |
| 1896 |
if (escapes(def->toInstruction())) { |
| 1897 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 1898 |
return true; |
| 1899 |
} |
| 1900 |
break; |
| 1901 |
} |
| 1902 |
|
| 1903 |
case MDefinition::Opcode::LoadFixedSlot: { |
| 1904 |
MLoadFixedSlot* load = def->toLoadFixedSlot(); |
| 1905 |
|
| 1906 |
// We can replace arguments.callee. |
| 1907 |
if (load->slot() == ArgumentsObject::CALLEE_SLOT) { |
| 1908 |
MOZ_ASSERT(guardedForMapped); |
| 1909 |
continue; |
| 1910 |
} |
| 1911 |
JitSpew(JitSpew_Escape, "is escaped by unsupported LoadFixedSlot\n"); |
| 1912 |
return true; |
| 1913 |
} |
| 1914 |
|
| 1915 |
case MDefinition::Opcode::ApplyArgsObj: { |
| 1916 |
if (ins == def->toApplyArgsObj()->getThis()) { |
| 1917 |
JitSpew(JitSpew_Escape, "is escaped as |this| arg of ApplyArgsObj\n"); |
| 1918 |
return true; |
| 1919 |
} |
| 1920 |
MOZ_ASSERT(ins == def->toApplyArgsObj()->getArgsObj()); |
| 1921 |
break; |
| 1922 |
} |
| 1923 |
|
| 1924 |
// This is a replaceable consumer. |
| 1925 |
case MDefinition::Opcode::ArgumentsObjectLength: |
| 1926 |
case MDefinition::Opcode::GetArgumentsObjectArg: |
| 1927 |
case MDefinition::Opcode::LoadArgumentsObjectArg: |
| 1928 |
case MDefinition::Opcode::LoadArgumentsObjectArgHole: |
| 1929 |
case MDefinition::Opcode::InArgumentsObjectArg: |
| 1930 |
case MDefinition::Opcode::ArrayFromArgumentsObject: |
| 1931 |
case MDefinition::Opcode::ArgumentsSlice: |
| 1932 |
break; |
| 1933 |
|
| 1934 |
// This instruction is a no-op used to test that scalar replacement |
| 1935 |
// is working as expected. |
| 1936 |
case MDefinition::Opcode::AssertRecoveredOnBailout: |
| 1937 |
break; |
| 1938 |
|
| 1939 |
default: |
| 1940 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 1941 |
return true; |
| 1942 |
} |
| 1943 |
} |
| 1944 |
|
| 1945 |
JitSpew(JitSpew_Escape, "ArgumentsObject is not escaped"); |
| 1946 |
return false; |
| 1947 |
} |
| 1948 |
|
| 1949 |
// Replacing the arguments object is simpler than replacing an object |
| 1950 |
// or array, because the arguments object does not change state. |
| 1951 |
bool ArgumentsReplacer::run() { |
| 1952 |
MBasicBlock* startBlock = args_->block(); |
| 1953 |
|
| 1954 |
// Iterate over each basic block. |
| 1955 |
for (ReversePostorderIterator block = graph_.rpoBegin(startBlock); |
| 1956 |
block != graph_.rpoEnd(); block++) { |
| 1957 |
if (mir_->shouldCancel("Scalar replacement of Arguments Object")) { |
| 1958 |
return false; |
| 1959 |
} |
| 1960 |
|
| 1961 |
// Iterates over phis and instructions. |
| 1962 |
// We do not have to visit resume points. Any resume points that capture |
| 1963 |
// the argument object will be handled by the Sink pass. |
| 1964 |
for (MDefinitionIterator iter(*block); iter;) { |
| 1965 |
// Increment the iterator before visiting the instruction, as the |
| 1966 |
// visit function might discard itself from the basic block. |
| 1967 |
MDefinition* def = *iter++; |
| 1968 |
switch (def->op()) { |
| 1969 |
#define MIR_OP(op) \ |
| 1970 |
case MDefinition::Opcode::op: \ |
| 1971 |
visit##op(def->to##op()); \ |
| 1972 |
break; |
| 1973 |
MIR_OPCODE_LIST(MIR_OP) |
| 1974 |
#undef MIR_OP |
| 1975 |
} |
| 1976 |
if (!graph_.alloc().ensureBallast()) { |
| 1977 |
return false; |
| 1978 |
} |
| 1979 |
if (oom()) { |
| 1980 |
return false; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
} |
| 1984 |
|
| 1985 |
assertSuccess(); |
| 1986 |
return true; |
| 1987 |
} |
| 1988 |
|
| 1989 |
void ArgumentsReplacer::assertSuccess() { |
| 1990 |
MOZ_ASSERT(args_->canRecoverOnBailout()); |
| 1991 |
MOZ_ASSERT(!args_->hasLiveDefUses()); |
| 1992 |
} |
| 1993 |
|
| 1994 |
void ArgumentsReplacer::visitGuardToClass(MGuardToClass* ins) { |
| 1995 |
// Skip guards on other objects. |
| 1996 |
if (ins->object() != args_) { |
| 1997 |
return; |
| 1998 |
} |
| 1999 |
MOZ_ASSERT(ins->isArgumentsObjectClass()); |
| 2000 |
|
| 2001 |
// Replace the guard with the args object. |
| 2002 |
ins->replaceAllUsesWith(args_); |
| 2003 |
|
| 2004 |
// Remove the guard. |
| 2005 |
ins->block()->discard(ins); |
| 2006 |
} |
| 2007 |
|
| 2008 |
void ArgumentsReplacer::visitGuardProto(MGuardProto* ins) { |
| 2009 |
// Skip guards on other objects. |
| 2010 |
if (ins->object() != args_) { |
| 2011 |
return; |
| 2012 |
} |
| 2013 |
|
| 2014 |
// The prototype can only be changed through explicit operations, for example |
| 2015 |
// by calling |Reflect.setPrototype|. We have already determined that the args |
| 2016 |
// object doesn't escape, so its prototype can't be mutated. |
| 2017 |
|
| 2018 |
// Replace the guard with the args object. |
| 2019 |
ins->replaceAllUsesWith(args_); |
| 2020 |
|
| 2021 |
// Remove the guard. |
| 2022 |
ins->block()->discard(ins); |
| 2023 |
} |
| 2024 |
|
| 2025 |
void ArgumentsReplacer::visitGuardArgumentsObjectFlags( |
| 2026 |
MGuardArgumentsObjectFlags* ins) { |
| 2027 |
// Skip other arguments objects. |
| 2028 |
if (ins->argsObject() != args_) { |
| 2029 |
return; |
| 2030 |
} |
| 2031 |
|
| 2032 |
#ifdef DEBUG |
| 2033 |
// Each *_OVERRIDDEN_BIT can only be set by setting or deleting a |
| 2034 |
// property of the args object. We have already determined that the |
| 2035 |
// args object doesn't escape, so its properties can't be mutated. |
| 2036 |
// |
| 2037 |
// FORWARDED_ARGUMENTS_BIT is set if any mapped argument is closed |
| 2038 |
// over, which is an immutable property of the script. Because we |
| 2039 |
// are replacing the args object for a known script, we can check |
| 2040 |
// the flag once, which is done when we first attach the CacheIR, |
| 2041 |
// and rely on it. (Note that this wouldn't be true if we didn't |
| 2042 |
// know the origin of args_, because it could be passed in from |
| 2043 |
// another function.) |
| 2044 |
uint32_t supportedBits = ArgumentsObject::LENGTH_OVERRIDDEN_BIT | |
| 2045 |
ArgumentsObject::ITERATOR_OVERRIDDEN_BIT | |
| 2046 |
ArgumentsObject::ELEMENT_OVERRIDDEN_BIT | |
| 2047 |
ArgumentsObject::CALLEE_OVERRIDDEN_BIT | |
| 2048 |
ArgumentsObject::FORWARDED_ARGUMENTS_BIT; |
| 2049 |
|
| 2050 |
MOZ_ASSERT((ins->flags() & ~supportedBits) == 0); |
| 2051 |
MOZ_ASSERT_IF(ins->flags() & ArgumentsObject::FORWARDED_ARGUMENTS_BIT, |
| 2052 |
!args_->block()->info().anyFormalIsForwarded()); |
| 2053 |
#endif |
| 2054 |
|
| 2055 |
// Replace the guard with the args object. |
| 2056 |
ins->replaceAllUsesWith(args_); |
| 2057 |
|
| 2058 |
// Remove the guard. |
| 2059 |
ins->block()->discard(ins); |
| 2060 |
} |
| 2061 |
|
| 2062 |
void ArgumentsReplacer::visitUnbox(MUnbox* ins) { |
| 2063 |
// Skip unrelated unboxes. |
| 2064 |
if (ins->getOperand(0) != args_) { |
| 2065 |
return; |
| 2066 |
} |
| 2067 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 2068 |
|
| 2069 |
// Replace the unbox with the args object. |
| 2070 |
ins->replaceAllUsesWith(args_); |
| 2071 |
|
| 2072 |
// Remove the unbox. |
| 2073 |
ins->block()->discard(ins); |
| 2074 |
} |
| 2075 |
|
| 2076 |
void ArgumentsReplacer::visitGetArgumentsObjectArg( |
| 2077 |
MGetArgumentsObjectArg* ins) { |
| 2078 |
// Skip other arguments objects. |
| 2079 |
if (ins->argsObject() != args_) { |
| 2080 |
return; |
| 2081 |
} |
| 2082 |
|
| 2083 |
// We don't support setting arguments in ArgumentsReplacer::escapes, |
| 2084 |
// so we can load the initial value of the argument without worrying |
| 2085 |
// about it being stale. |
| 2086 |
MDefinition* getArg; |
| 2087 |
if (isInlinedArguments()) { |
| 2088 |
// Inlined frames have direct access to the actual arguments. |
| 2089 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2090 |
if (ins->argno() < actualArgs->numActuals()) { |
| 2091 |
getArg = actualArgs->getArg(ins->argno()); |
| 2092 |
} else { |
| 2093 |
// Omitted arguments are not mapped to the arguments object, and |
| 2094 |
// will always be undefined. |
| 2095 |
auto* undef = MConstant::New(alloc(), UndefinedValue()); |
| 2096 |
ins->block()->insertBefore(ins, undef); |
| 2097 |
getArg = undef; |
| 2098 |
} |
| 2099 |
} else { |
| 2100 |
// Load the argument from the frame. |
| 2101 |
auto* index = MConstant::New(alloc(), Int32Value(ins->argno())); |
| 2102 |
ins->block()->insertBefore(ins, index); |
| 2103 |
|
| 2104 |
auto* loadArg = MGetFrameArgument::New(alloc(), index); |
| 2105 |
ins->block()->insertBefore(ins, loadArg); |
| 2106 |
getArg = loadArg; |
| 2107 |
} |
| 2108 |
ins->replaceAllUsesWith(getArg); |
| 2109 |
|
| 2110 |
// Remove original instruction. |
| 2111 |
ins->block()->discard(ins); |
| 2112 |
} |
| 2113 |
|
| 2114 |
void ArgumentsReplacer::visitLoadArgumentsObjectArg( |
| 2115 |
MLoadArgumentsObjectArg* ins) { |
| 2116 |
// Skip other arguments objects. |
| 2117 |
if (ins->argsObject() != args_) { |
| 2118 |
return; |
| 2119 |
} |
| 2120 |
|
| 2121 |
MDefinition* index = ins->index(); |
| 2122 |
|
| 2123 |
MInstruction* loadArg; |
| 2124 |
if (isInlinedArguments()) { |
| 2125 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2126 |
|
| 2127 |
// Insert bounds check. |
| 2128 |
auto* length = |
| 2129 |
MConstant::New(alloc(), Int32Value(actualArgs->numActuals())); |
| 2130 |
ins->block()->insertBefore(ins, length); |
| 2131 |
|
| 2132 |
MInstruction* check = MBoundsCheck::New(alloc(), index, length); |
| 2133 |
check->setBailoutKind(ins->bailoutKind()); |
| 2134 |
ins->block()->insertBefore(ins, check); |
| 2135 |
|
| 2136 |
if (mir_->outerInfo().hadBoundsCheckBailout()) { |
| 2137 |
check->setNotMovable(); |
| 2138 |
} |
| 2139 |
|
| 2140 |
loadArg = MGetInlinedArgument::New(alloc(), check, actualArgs); |
| 2141 |
if (!loadArg) { |
| 2142 |
oom_ = true; |
| 2143 |
return; |
| 2144 |
} |
| 2145 |
} else { |
| 2146 |
// Insert bounds check. |
| 2147 |
auto* length = MArgumentsLength::New(alloc()); |
| 2148 |
ins->block()->insertBefore(ins, length); |
| 2149 |
|
| 2150 |
MInstruction* check = MBoundsCheck::New(alloc(), index, length); |
| 2151 |
check->setBailoutKind(ins->bailoutKind()); |
| 2152 |
ins->block()->insertBefore(ins, check); |
| 2153 |
|
| 2154 |
if (mir_->outerInfo().hadBoundsCheckBailout()) { |
| 2155 |
check->setNotMovable(); |
| 2156 |
} |
| 2157 |
|
| 2158 |
if (JitOptions.spectreIndexMasking) { |
| 2159 |
check = MSpectreMaskIndex::New(alloc(), check, length); |
| 2160 |
ins->block()->insertBefore(ins, check); |
| 2161 |
} |
| 2162 |
|
| 2163 |
loadArg = MGetFrameArgument::New(alloc(), check); |
| 2164 |
} |
| 2165 |
ins->block()->insertBefore(ins, loadArg); |
| 2166 |
ins->replaceAllUsesWith(loadArg); |
| 2167 |
|
| 2168 |
// Remove original instruction. |
| 2169 |
ins->block()->discard(ins); |
| 2170 |
} |
| 2171 |
|
| 2172 |
void ArgumentsReplacer::visitLoadArgumentsObjectArgHole( |
| 2173 |
MLoadArgumentsObjectArgHole* ins) { |
| 2174 |
// Skip other arguments objects. |
| 2175 |
if (ins->argsObject() != args_) { |
| 2176 |
return; |
| 2177 |
} |
| 2178 |
|
| 2179 |
MDefinition* index = ins->index(); |
| 2180 |
|
| 2181 |
MInstruction* loadArg; |
| 2182 |
if (isInlinedArguments()) { |
| 2183 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2184 |
|
| 2185 |
loadArg = MGetInlinedArgumentHole::New(alloc(), index, actualArgs); |
| 2186 |
if (!loadArg) { |
| 2187 |
oom_ = true; |
| 2188 |
return; |
| 2189 |
} |
| 2190 |
} else { |
| 2191 |
auto* length = MArgumentsLength::New(alloc()); |
| 2192 |
ins->block()->insertBefore(ins, length); |
| 2193 |
|
| 2194 |
loadArg = MGetFrameArgumentHole::New(alloc(), index, length); |
| 2195 |
} |
| 2196 |
loadArg->setBailoutKind(ins->bailoutKind()); |
| 2197 |
ins->block()->insertBefore(ins, loadArg); |
| 2198 |
ins->replaceAllUsesWith(loadArg); |
| 2199 |
|
| 2200 |
// Remove original instruction. |
| 2201 |
ins->block()->discard(ins); |
| 2202 |
} |
| 2203 |
|
| 2204 |
void ArgumentsReplacer::visitInArgumentsObjectArg(MInArgumentsObjectArg* ins) { |
| 2205 |
// Skip other arguments objects. |
| 2206 |
if (ins->argsObject() != args_) { |
| 2207 |
return; |
| 2208 |
} |
| 2209 |
|
| 2210 |
MDefinition* index = ins->index(); |
| 2211 |
|
| 2212 |
// Ensure the index is non-negative. |
| 2213 |
auto* guardedIndex = MGuardInt32IsNonNegative::New(alloc(), index); |
| 2214 |
guardedIndex->setBailoutKind(ins->bailoutKind()); |
| 2215 |
ins->block()->insertBefore(ins, guardedIndex); |
| 2216 |
|
| 2217 |
MInstruction* length; |
| 2218 |
if (isInlinedArguments()) { |
| 2219 |
uint32_t argc = args_->toCreateInlinedArgumentsObject()->numActuals(); |
| 2220 |
length = MConstant::New(alloc(), Int32Value(argc)); |
| 2221 |
} else { |
| 2222 |
length = MArgumentsLength::New(alloc()); |
| 2223 |
} |
| 2224 |
ins->block()->insertBefore(ins, length); |
| 2225 |
|
| 2226 |
auto* compare = MCompare::New(alloc(), guardedIndex, length, JSOp::Lt, |
| 2227 |
MCompare::Compare_Int32); |
| 2228 |
ins->block()->insertBefore(ins, compare); |
| 2229 |
ins->replaceAllUsesWith(compare); |
| 2230 |
|
| 2231 |
// Remove original instruction. |
| 2232 |
ins->block()->discard(ins); |
| 2233 |
} |
| 2234 |
|
| 2235 |
void ArgumentsReplacer::visitArgumentsObjectLength( |
| 2236 |
MArgumentsObjectLength* ins) { |
| 2237 |
// Skip other arguments objects. |
| 2238 |
if (ins->argsObject() != args_) { |
| 2239 |
return; |
| 2240 |
} |
| 2241 |
|
| 2242 |
MInstruction* length; |
| 2243 |
if (isInlinedArguments()) { |
| 2244 |
uint32_t argc = args_->toCreateInlinedArgumentsObject()->numActuals(); |
| 2245 |
length = MConstant::New(alloc(), Int32Value(argc)); |
| 2246 |
} else { |
| 2247 |
length = MArgumentsLength::New(alloc()); |
| 2248 |
} |
| 2249 |
ins->block()->insertBefore(ins, length); |
| 2250 |
ins->replaceAllUsesWith(length); |
| 2251 |
|
| 2252 |
// Remove original instruction. |
| 2253 |
ins->block()->discard(ins); |
| 2254 |
} |
| 2255 |
|
| 2256 |
void ArgumentsReplacer::visitApplyArgsObj(MApplyArgsObj* ins) { |
| 2257 |
// Skip other arguments objects. |
| 2258 |
if (ins->getArgsObj() != args_) { |
| 2259 |
return; |
| 2260 |
} |
| 2261 |
|
| 2262 |
MInstruction* newIns; |
| 2263 |
if (isInlinedArguments()) { |
| 2264 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2265 |
CallInfo callInfo(alloc(), /*constructing=*/false, |
| 2266 |
ins->ignoresReturnValue()); |
| 2267 |
|
| 2268 |
callInfo.initForApplyInlinedArgs(ins->getFunction(), ins->getThis(), |
| 2269 |
actualArgs->numActuals()); |
| 2270 |
for (uint32_t i = 0; i < actualArgs->numActuals(); i++) { |
| 2271 |
callInfo.initArg(i, actualArgs->getArg(i)); |
| 2272 |
} |
| 2273 |
|
| 2274 |
auto addUndefined = [this, &ins]() -> MConstant* { |
| 2275 |
MConstant* undef = MConstant::New(alloc(), UndefinedValue()); |
| 2276 |
ins->block()->insertBefore(ins, undef); |
| 2277 |
return undef; |
| 2278 |
}; |
| 2279 |
|
| 2280 |
bool needsThisCheck = false; |
| 2281 |
bool isDOMCall = false; |
| 2282 |
auto* call = MakeCall(alloc(), addUndefined, callInfo, needsThisCheck, |
| 2283 |
ins->getSingleTarget(), isDOMCall); |
| 2284 |
if (!call) { |
| 2285 |
oom_ = true; |
| 2286 |
return; |
| 2287 |
} |
| 2288 |
if (!ins->maybeCrossRealm()) { |
| 2289 |
call->setNotCrossRealm(); |
| 2290 |
} |
| 2291 |
newIns = call; |
| 2292 |
} else { |
| 2293 |
auto* numArgs = MArgumentsLength::New(alloc()); |
| 2294 |
ins->block()->insertBefore(ins, numArgs); |
| 2295 |
|
| 2296 |
// TODO: Should we rename MApplyArgs? |
| 2297 |
auto* apply = MApplyArgs::New(alloc(), ins->getSingleTarget(), |
| 2298 |
ins->getFunction(), numArgs, ins->getThis()); |
| 2299 |
apply->setBailoutKind(ins->bailoutKind()); |
| 2300 |
if (!ins->maybeCrossRealm()) { |
| 2301 |
apply->setNotCrossRealm(); |
| 2302 |
} |
| 2303 |
if (ins->ignoresReturnValue()) { |
| 2304 |
apply->setIgnoresReturnValue(); |
| 2305 |
} |
| 2306 |
newIns = apply; |
| 2307 |
} |
| 2308 |
|
| 2309 |
ins->block()->insertBefore(ins, newIns); |
| 2310 |
ins->replaceAllUsesWith(newIns); |
| 2311 |
|
| 2312 |
newIns->stealResumePoint(ins); |
| 2313 |
ins->block()->discard(ins); |
| 2314 |
} |
| 2315 |
|
| 2316 |
MNewArrayObject* ArgumentsReplacer::inlineArgsArray(MInstruction* ins, |
| 2317 |
Shape* shape, |
| 2318 |
uint32_t begin, |
| 2319 |
uint32_t count) { |
| 2320 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2321 |
|
| 2322 |
// Contrary to |WarpBuilder::build_Rest()|, we can always create |
| 2323 |
// MNewArrayObject, because we're guaranteed to have a shape and all |
| 2324 |
// arguments can be stored into fixed elements. |
| 2325 |
static_assert( |
| 2326 |
gc::CanUseFixedElementsForArray(ArgumentsObject::MaxInlinedArgs)); |
| 2327 |
|
| 2328 |
gc::Heap heap = gc::Heap::Default; |
| 2329 |
|
| 2330 |
// Allocate an array of the correct size. |
| 2331 |
auto* shapeConstant = MConstant::NewShape(alloc(), shape); |
| 2332 |
ins->block()->insertBefore(ins, shapeConstant); |
| 2333 |
|
| 2334 |
auto* newArray = MNewArrayObject::New(alloc(), shapeConstant, count, heap); |
| 2335 |
ins->block()->insertBefore(ins, newArray); |
| 2336 |
|
| 2337 |
if (count) { |
| 2338 |
auto* elements = MElements::New(alloc(), newArray); |
| 2339 |
ins->block()->insertBefore(ins, elements); |
| 2340 |
|
| 2341 |
MConstant* index = nullptr; |
| 2342 |
for (uint32_t i = 0; i < count; i++) { |
| 2343 |
index = MConstant::New(alloc(), Int32Value(i)); |
| 2344 |
ins->block()->insertBefore(ins, index); |
| 2345 |
|
| 2346 |
MDefinition* arg = actualArgs->getArg(begin + i); |
| 2347 |
auto* store = MStoreElement::NewUnbarriered(alloc(), elements, index, arg, |
| 2348 |
/* needsHoleCheck = */ false); |
| 2349 |
ins->block()->insertBefore(ins, store); |
| 2350 |
|
| 2351 |
auto* barrier = MPostWriteBarrier::New(alloc(), newArray, arg); |
| 2352 |
ins->block()->insertBefore(ins, barrier); |
| 2353 |
} |
| 2354 |
|
| 2355 |
auto* initLength = MSetInitializedLength::New(alloc(), elements, index); |
| 2356 |
ins->block()->insertBefore(ins, initLength); |
| 2357 |
} |
| 2358 |
|
| 2359 |
return newArray; |
| 2360 |
} |
| 2361 |
|
| 2362 |
void ArgumentsReplacer::visitArrayFromArgumentsObject( |
| 2363 |
MArrayFromArgumentsObject* ins) { |
| 2364 |
// Skip other arguments objects. |
| 2365 |
if (ins->argsObject() != args_) { |
| 2366 |
return; |
| 2367 |
} |
| 2368 |
|
| 2369 |
// We can only replace `arguments` because we've verified that the `arguments` |
| 2370 |
// object hasn't been modified in any way. This implies that the arguments |
| 2371 |
// stored in the stack frame haven't been changed either. |
| 2372 |
// |
| 2373 |
// The idea to replace `arguments` in spread calls `f(...arguments)` is now as |
| 2374 |
// follows: |
| 2375 |
// We replace |MArrayFromArgumentsObject| with the identical instructions we |
| 2376 |
// emit when building a rest-array object, cf. |WarpBuilder::build_Rest()|. In |
| 2377 |
// a next step, scalar replacement will then replace these new instructions |
| 2378 |
// themselves. |
| 2379 |
|
| 2380 |
Shape* shape = ins->shape(); |
| 2381 |
MOZ_ASSERT(shape); |
| 2382 |
|
| 2383 |
MDefinition* replacement; |
| 2384 |
if (isInlinedArguments()) { |
| 2385 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2386 |
uint32_t numActuals = actualArgs->numActuals(); |
| 2387 |
MOZ_ASSERT(numActuals <= ArgumentsObject::MaxInlinedArgs); |
| 2388 |
|
| 2389 |
replacement = inlineArgsArray(ins, shape, 0, numActuals); |
| 2390 |
} else { |
| 2391 |
// We can use |MRest| to read all arguments, because we've guaranteed that |
| 2392 |
// the arguments stored in the stack frame haven't changed; see the comment |
| 2393 |
// at the start of this method. |
| 2394 |
|
| 2395 |
auto* numActuals = MArgumentsLength::New(alloc()); |
| 2396 |
ins->block()->insertBefore(ins, numActuals); |
| 2397 |
|
| 2398 |
// Set |numFormals| to zero to read all arguments, including any formals. |
| 2399 |
uint32_t numFormals = 0; |
| 2400 |
|
| 2401 |
auto* rest = MRest::New(alloc(), numActuals, numFormals, shape); |
| 2402 |
ins->block()->insertBefore(ins, rest); |
| 2403 |
|
| 2404 |
replacement = rest; |
| 2405 |
} |
| 2406 |
|
| 2407 |
ins->replaceAllUsesWith(replacement); |
| 2408 |
|
| 2409 |
// Remove original instruction. |
| 2410 |
ins->block()->discard(ins); |
| 2411 |
} |
| 2412 |
|
| 2413 |
static uint32_t NormalizeSlice(MDefinition* def, uint32_t length) { |
| 2414 |
int32_t value = def->toConstant()->toInt32(); |
| 2415 |
if (value < 0) { |
| 2416 |
return std::max(int32_t(uint32_t(value) + length), 0); |
| 2417 |
} |
| 2418 |
return std::min(uint32_t(value), length); |
| 2419 |
} |
| 2420 |
|
| 2421 |
void ArgumentsReplacer::visitArgumentsSlice(MArgumentsSlice* ins) { |
| 2422 |
// Skip other arguments objects. |
| 2423 |
if (ins->object() != args_) { |
| 2424 |
return; |
| 2425 |
} |
| 2426 |
|
| 2427 |
// Optimise the common pattern |Array.prototype.slice.call(arguments, begin)|, |
| 2428 |
// where |begin| is a non-negative, constant int32. |
| 2429 |
// |
| 2430 |
// An absent end-index is replaced by |arguments.length|, so we try to match |
| 2431 |
// |Array.prototype.slice.call(arguments, begin, arguments.length)|. |
| 2432 |
if (isInlinedArguments()) { |
| 2433 |
// When this is an inlined arguments, |arguments.length| has been replaced |
| 2434 |
// by a constant. |
| 2435 |
if (ins->begin()->isConstant() && ins->end()->isConstant()) { |
| 2436 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2437 |
uint32_t numActuals = actualArgs->numActuals(); |
| 2438 |
MOZ_ASSERT(numActuals <= ArgumentsObject::MaxInlinedArgs); |
| 2439 |
|
| 2440 |
uint32_t begin = NormalizeSlice(ins->begin(), numActuals); |
| 2441 |
uint32_t end = NormalizeSlice(ins->end(), numActuals); |
| 2442 |
uint32_t count = end > begin ? end - begin : 0; |
| 2443 |
MOZ_ASSERT(count <= numActuals); |
| 2444 |
|
| 2445 |
Shape* shape = ins->templateObj()->shape(); |
| 2446 |
auto* newArray = inlineArgsArray(ins, shape, begin, count); |
| 2447 |
|
| 2448 |
ins->replaceAllUsesWith(newArray); |
| 2449 |
|
| 2450 |
// Remove original instruction. |
| 2451 |
ins->block()->discard(ins); |
| 2452 |
return; |
| 2453 |
} |
| 2454 |
} else { |
| 2455 |
// Otherwise |arguments.length| is emitted as MArgumentsLength. |
| 2456 |
if (ins->begin()->isConstant() && ins->end()->isArgumentsLength()) { |
| 2457 |
int32_t begin = ins->begin()->toConstant()->toInt32(); |
| 2458 |
if (begin >= 0) { |
| 2459 |
auto* numActuals = MArgumentsLength::New(alloc()); |
| 2460 |
ins->block()->insertBefore(ins, numActuals); |
| 2461 |
|
| 2462 |
// Set |numFormals| to read all arguments starting at |begin|. |
| 2463 |
uint32_t numFormals = begin; |
| 2464 |
|
| 2465 |
Shape* shape = ins->templateObj()->shape(); |
| 2466 |
|
| 2467 |
// Use MRest because it can be scalar replaced, which enables further |
| 2468 |
// optimizations. |
| 2469 |
auto* rest = MRest::New(alloc(), numActuals, numFormals, shape); |
| 2470 |
ins->block()->insertBefore(ins, rest); |
| 2471 |
|
| 2472 |
ins->replaceAllUsesWith(rest); |
| 2473 |
|
| 2474 |
// Remove original instruction. |
| 2475 |
ins->block()->discard(ins); |
| 2476 |
return; |
| 2477 |
} |
| 2478 |
} |
| 2479 |
} |
| 2480 |
|
| 2481 |
MInstruction* numArgs; |
| 2482 |
if (isInlinedArguments()) { |
| 2483 |
uint32_t argc = args_->toCreateInlinedArgumentsObject()->numActuals(); |
| 2484 |
numArgs = MConstant::New(alloc(), Int32Value(argc)); |
| 2485 |
} else { |
| 2486 |
numArgs = MArgumentsLength::New(alloc()); |
| 2487 |
} |
| 2488 |
ins->block()->insertBefore(ins, numArgs); |
| 2489 |
|
| 2490 |
auto* begin = MNormalizeSliceTerm::New(alloc(), ins->begin(), numArgs); |
| 2491 |
ins->block()->insertBefore(ins, begin); |
| 2492 |
|
| 2493 |
auto* end = MNormalizeSliceTerm::New(alloc(), ins->end(), numArgs); |
| 2494 |
ins->block()->insertBefore(ins, end); |
| 2495 |
|
| 2496 |
bool isMax = false; |
| 2497 |
auto* beginMin = MMinMax::New(alloc(), begin, end, MIRType::Int32, isMax); |
| 2498 |
ins->block()->insertBefore(ins, beginMin); |
| 2499 |
|
| 2500 |
// Safe to truncate because both operands are positive and end >= beginMin. |
| 2501 |
auto* count = MSub::New(alloc(), end, beginMin, MIRType::Int32); |
| 2502 |
count->setTruncateKind(TruncateKind::Truncate); |
| 2503 |
ins->block()->insertBefore(ins, count); |
| 2504 |
|
| 2505 |
MInstruction* replacement; |
| 2506 |
if (isInlinedArguments()) { |
| 2507 |
auto* actualArgs = args_->toCreateInlinedArgumentsObject(); |
| 2508 |
replacement = |
| 2509 |
MInlineArgumentsSlice::New(alloc(), beginMin, count, actualArgs, |
| 2510 |
ins->templateObj(), ins->initialHeap()); |
| 2511 |
if (!replacement) { |
| 2512 |
oom_ = true; |
| 2513 |
return; |
| 2514 |
} |
| 2515 |
} else { |
| 2516 |
replacement = MFrameArgumentsSlice::New( |
| 2517 |
alloc(), beginMin, count, ins->templateObj(), ins->initialHeap()); |
| 2518 |
} |
| 2519 |
ins->block()->insertBefore(ins, replacement); |
| 2520 |
|
| 2521 |
ins->replaceAllUsesWith(replacement); |
| 2522 |
|
| 2523 |
// Remove original instruction. |
| 2524 |
ins->block()->discard(ins); |
| 2525 |
} |
| 2526 |
|
| 2527 |
void ArgumentsReplacer::visitLoadFixedSlot(MLoadFixedSlot* ins) { |
| 2528 |
// Skip other arguments objects. |
| 2529 |
if (ins->object() != args_) { |
| 2530 |
return; |
| 2531 |
} |
| 2532 |
|
| 2533 |
MOZ_ASSERT(ins->slot() == ArgumentsObject::CALLEE_SLOT); |
| 2534 |
|
| 2535 |
MDefinition* replacement; |
| 2536 |
if (isInlinedArguments()) { |
| 2537 |
replacement = args_->toCreateInlinedArgumentsObject()->getCallee(); |
| 2538 |
} else { |
| 2539 |
auto* callee = MCallee::New(alloc()); |
| 2540 |
ins->block()->insertBefore(ins, callee); |
| 2541 |
replacement = callee; |
| 2542 |
} |
| 2543 |
ins->replaceAllUsesWith(replacement); |
| 2544 |
|
| 2545 |
// Remove original instruction. |
| 2546 |
ins->block()->discard(ins); |
| 2547 |
} |
| 2548 |
|
| 2549 |
static inline bool IsOptimizableRestInstruction(MInstruction* ins) { |
| 2550 |
return ins->isRest(); |
| 2551 |
} |
| 2552 |
|
| 2553 |
class RestReplacer : public MDefinitionVisitorDefaultNoop { |
| 2554 |
private: |
| 2555 |
const MIRGenerator* mir_; |
| 2556 |
MIRGraph& graph_; |
| 2557 |
MInstruction* rest_; |
| 2558 |
|
| 2559 |
TempAllocator& alloc() { return graph_.alloc(); } |
| 2560 |
MRest* rest() const { return rest_->toRest(); } |
| 2561 |
|
| 2562 |
bool isRestElements(MDefinition* elements); |
| 2563 |
void discardInstruction(MInstruction* ins, MDefinition* elements); |
| 2564 |
MDefinition* restLength(MInstruction* ins); |
| 2565 |
void visitLength(MInstruction* ins, MDefinition* elements); |
| 2566 |
|
| 2567 |
void visitGuardToClass(MGuardToClass* ins); |
| 2568 |
void visitGuardShape(MGuardShape* ins); |
| 2569 |
void visitGuardArrayIsPacked(MGuardArrayIsPacked* ins); |
| 2570 |
void visitUnbox(MUnbox* ins); |
| 2571 |
void visitCompare(MCompare* ins); |
| 2572 |
void visitLoadElement(MLoadElement* ins); |
| 2573 |
void visitArrayLength(MArrayLength* ins); |
| 2574 |
void visitInitializedLength(MInitializedLength* ins); |
| 2575 |
void visitApplyArray(MApplyArray* ins); |
| 2576 |
void visitConstructArray(MConstructArray* ins); |
| 2577 |
|
| 2578 |
bool escapes(MElements* ins); |
| 2579 |
|
| 2580 |
public: |
| 2581 |
RestReplacer(const MIRGenerator* mir, MIRGraph& graph, MInstruction* rest) |
| 2582 |
: mir_(mir), graph_(graph), rest_(rest) { |
| 2583 |
MOZ_ASSERT(IsOptimizableRestInstruction(rest_)); |
| 2584 |
} |
| 2585 |
|
| 2586 |
bool escapes(MInstruction* ins); |
| 2587 |
bool run(); |
| 2588 |
void assertSuccess(); |
| 2589 |
}; |
| 2590 |
|
| 2591 |
// Returns false if the rest array object does not escape. |
| 2592 |
bool RestReplacer::escapes(MInstruction* ins) { |
| 2593 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 2594 |
|
| 2595 |
JitSpewDef(JitSpew_Escape, "Check rest array\n", ins); |
| 2596 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 2597 |
|
| 2598 |
// We can replace rest arrays in scripts with OSR entries, but the outermost |
| 2599 |
// rest object has already been allocated before we enter via OSR and can't be |
| 2600 |
// replaced. |
| 2601 |
// See also the same restriction when replacing |arguments|. |
| 2602 |
if (graph_.osrBlock()) { |
| 2603 |
JitSpew(JitSpew_Escape, "Can't replace outermost OSR rest array"); |
| 2604 |
return true; |
| 2605 |
} |
| 2606 |
|
| 2607 |
// Check all uses to see whether they can be supported without allocating an |
| 2608 |
// ArrayObject for the rest parameter. |
| 2609 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 2610 |
MNode* consumer = (*i)->consumer(); |
| 2611 |
|
| 2612 |
// If a resume point can observe this instruction, we can only optimize |
| 2613 |
// if it is recoverable. |
| 2614 |
if (consumer->isResumePoint()) { |
| 2615 |
if (!consumer->toResumePoint()->isRecoverableOperand(*i)) { |
| 2616 |
JitSpew(JitSpew_Escape, "Observable rest array cannot be recovered"); |
| 2617 |
return true; |
| 2618 |
} |
| 2619 |
continue; |
| 2620 |
} |
| 2621 |
|
| 2622 |
MDefinition* def = consumer->toDefinition(); |
| 2623 |
switch (def->op()) { |
| 2624 |
case MDefinition::Opcode::Elements: { |
| 2625 |
auto* elem = def->toElements(); |
| 2626 |
MOZ_ASSERT(elem->object() == ins); |
| 2627 |
if (escapes(elem)) { |
| 2628 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 2629 |
return true; |
| 2630 |
} |
| 2631 |
break; |
| 2632 |
} |
| 2633 |
|
| 2634 |
case MDefinition::Opcode::GuardShape: { |
| 2635 |
const Shape* shape = rest()->shape(); |
| 2636 |
if (!shape) { |
| 2637 |
JitSpew(JitSpew_Escape, "No shape defined."); |
| 2638 |
return true; |
| 2639 |
} |
| 2640 |
|
| 2641 |
auto* guard = def->toGuardShape(); |
| 2642 |
if (shape != guard->shape()) { |
| 2643 |
JitSpewDef(JitSpew_Escape, "has a non-matching guard shape\n", def); |
| 2644 |
return true; |
| 2645 |
} |
| 2646 |
if (escapes(guard)) { |
| 2647 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 2648 |
return true; |
| 2649 |
} |
| 2650 |
break; |
| 2651 |
} |
| 2652 |
|
| 2653 |
case MDefinition::Opcode::GuardToClass: { |
| 2654 |
auto* guard = def->toGuardToClass(); |
| 2655 |
if (guard->getClass() != &ArrayObject::class_) { |
| 2656 |
JitSpewDef(JitSpew_Escape, "has a non-matching class guard\n", def); |
| 2657 |
return true; |
| 2658 |
} |
| 2659 |
if (escapes(guard)) { |
| 2660 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 2661 |
return true; |
| 2662 |
} |
| 2663 |
break; |
| 2664 |
} |
| 2665 |
|
| 2666 |
case MDefinition::Opcode::GuardArrayIsPacked: { |
| 2667 |
// Rest arrays are always packed as long as they aren't modified. |
| 2668 |
auto* guard = def->toGuardArrayIsPacked(); |
| 2669 |
if (escapes(guard)) { |
| 2670 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 2671 |
return true; |
| 2672 |
} |
| 2673 |
break; |
| 2674 |
} |
| 2675 |
|
| 2676 |
case MDefinition::Opcode::Unbox: { |
| 2677 |
if (def->type() != MIRType::Object) { |
| 2678 |
JitSpewDef(JitSpew_Escape, "has an invalid unbox\n", def); |
| 2679 |
return true; |
| 2680 |
} |
| 2681 |
if (escapes(def->toInstruction())) { |
| 2682 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 2683 |
return true; |
| 2684 |
} |
| 2685 |
break; |
| 2686 |
} |
| 2687 |
|
| 2688 |
// This instruction is supported for |JSOp::OptimizeSpreadCall|. |
| 2689 |
case MDefinition::Opcode::Compare: { |
| 2690 |
bool canFold; |
| 2691 |
if (!def->toCompare()->tryFold(&canFold)) { |
| 2692 |
JitSpewDef(JitSpew_Escape, "has an unsupported compare\n", def); |
| 2693 |
return true; |
| 2694 |
} |
| 2695 |
break; |
| 2696 |
} |
| 2697 |
|
| 2698 |
// This instruction is a no-op used to test that scalar replacement is |
| 2699 |
// working as expected. |
| 2700 |
case MDefinition::Opcode::AssertRecoveredOnBailout: |
| 2701 |
break; |
| 2702 |
|
| 2703 |
default: |
| 2704 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 2705 |
return true; |
| 2706 |
} |
| 2707 |
} |
| 2708 |
|
| 2709 |
JitSpew(JitSpew_Escape, "Rest array object is not escaped"); |
| 2710 |
return false; |
| 2711 |
} |
| 2712 |
|
| 2713 |
bool RestReplacer::escapes(MElements* ins) { |
| 2714 |
JitSpewDef(JitSpew_Escape, "Check rest array elements\n", ins); |
| 2715 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 2716 |
|
| 2717 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 2718 |
// The MIRType::Elements cannot be captured in a resume point as it does not |
| 2719 |
// represent a value allocation. |
| 2720 |
MDefinition* def = (*i)->consumer()->toDefinition(); |
| 2721 |
|
| 2722 |
switch (def->op()) { |
| 2723 |
case MDefinition::Opcode::LoadElement: |
| 2724 |
MOZ_ASSERT(def->toLoadElement()->elements() == ins); |
| 2725 |
break; |
| 2726 |
|
| 2727 |
case MDefinition::Opcode::ArrayLength: |
| 2728 |
MOZ_ASSERT(def->toArrayLength()->elements() == ins); |
| 2729 |
break; |
| 2730 |
|
| 2731 |
case MDefinition::Opcode::InitializedLength: |
| 2732 |
MOZ_ASSERT(def->toInitializedLength()->elements() == ins); |
| 2733 |
break; |
| 2734 |
|
| 2735 |
case MDefinition::Opcode::ApplyArray: |
| 2736 |
MOZ_ASSERT(def->toApplyArray()->getElements() == ins); |
| 2737 |
break; |
| 2738 |
|
| 2739 |
case MDefinition::Opcode::ConstructArray: |
| 2740 |
MOZ_ASSERT(def->toConstructArray()->getElements() == ins); |
| 2741 |
break; |
| 2742 |
|
| 2743 |
default: |
| 2744 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 2745 |
return true; |
| 2746 |
} |
| 2747 |
} |
| 2748 |
|
| 2749 |
JitSpew(JitSpew_Escape, "Rest array object is not escaped"); |
| 2750 |
return false; |
| 2751 |
} |
| 2752 |
|
| 2753 |
// Replacing the rest array object is simpler than replacing an object or array, |
| 2754 |
// because the rest array object does not change state. |
| 2755 |
bool RestReplacer::run() { |
| 2756 |
MBasicBlock* startBlock = rest_->block(); |
| 2757 |
|
| 2758 |
// Iterate over each basic block. |
| 2759 |
for (ReversePostorderIterator block = graph_.rpoBegin(startBlock); |
| 2760 |
block != graph_.rpoEnd(); block++) { |
| 2761 |
if (mir_->shouldCancel("Scalar replacement of rest array object")) { |
| 2762 |
return false; |
| 2763 |
} |
| 2764 |
|
| 2765 |
// Iterates over phis and instructions. |
| 2766 |
// We do not have to visit resume points. Any resume points that capture the |
| 2767 |
// rest array object will be handled by the Sink pass. |
| 2768 |
for (MDefinitionIterator iter(*block); iter;) { |
| 2769 |
// Increment the iterator before visiting the instruction, as the visit |
| 2770 |
// function might discard itself from the basic block. |
| 2771 |
MDefinition* def = *iter++; |
| 2772 |
switch (def->op()) { |
| 2773 |
#define MIR_OP(op) \ |
| 2774 |
case MDefinition::Opcode::op: \ |
| 2775 |
visit##op(def->to##op()); \ |
| 2776 |
break; |
| 2777 |
MIR_OPCODE_LIST(MIR_OP) |
| 2778 |
#undef MIR_OP |
| 2779 |
} |
| 2780 |
if (!graph_.alloc().ensureBallast()) { |
| 2781 |
return false; |
| 2782 |
} |
| 2783 |
} |
| 2784 |
} |
| 2785 |
|
| 2786 |
assertSuccess(); |
| 2787 |
return true; |
| 2788 |
} |
| 2789 |
|
| 2790 |
void RestReplacer::assertSuccess() { |
| 2791 |
MOZ_ASSERT(rest_->canRecoverOnBailout()); |
| 2792 |
MOZ_ASSERT(!rest_->hasLiveDefUses()); |
| 2793 |
} |
| 2794 |
|
| 2795 |
bool RestReplacer::isRestElements(MDefinition* elements) { |
| 2796 |
return elements->isElements() && elements->toElements()->object() == rest_; |
| 2797 |
} |
| 2798 |
|
| 2799 |
void RestReplacer::discardInstruction(MInstruction* ins, |
| 2800 |
MDefinition* elements) { |
| 2801 |
MOZ_ASSERT(elements->isElements()); |
| 2802 |
ins->block()->discard(ins); |
| 2803 |
if (!elements->hasLiveDefUses()) { |
| 2804 |
elements->block()->discard(elements->toInstruction()); |
| 2805 |
} |
| 2806 |
} |
| 2807 |
|
| 2808 |
void RestReplacer::visitGuardToClass(MGuardToClass* ins) { |
| 2809 |
// Skip guards on other objects. |
| 2810 |
if (ins->object() != rest_) { |
| 2811 |
return; |
| 2812 |
} |
| 2813 |
MOZ_ASSERT(ins->getClass() == &ArrayObject::class_); |
| 2814 |
|
| 2815 |
// Replace the guard with the array object. |
| 2816 |
ins->replaceAllUsesWith(rest_); |
| 2817 |
|
| 2818 |
// Remove the guard. |
| 2819 |
ins->block()->discard(ins); |
| 2820 |
} |
| 2821 |
|
| 2822 |
void RestReplacer::visitGuardShape(MGuardShape* ins) { |
| 2823 |
// Skip guards on other objects. |
| 2824 |
if (ins->object() != rest_) { |
| 2825 |
return; |
| 2826 |
} |
| 2827 |
|
| 2828 |
// Replace the guard with the array object. |
| 2829 |
ins->replaceAllUsesWith(rest_); |
| 2830 |
|
| 2831 |
// Remove the guard. |
| 2832 |
ins->block()->discard(ins); |
| 2833 |
} |
| 2834 |
|
| 2835 |
void RestReplacer::visitGuardArrayIsPacked(MGuardArrayIsPacked* ins) { |
| 2836 |
// Skip guards on other objects. |
| 2837 |
if (ins->array() != rest_) { |
| 2838 |
return; |
| 2839 |
} |
| 2840 |
|
| 2841 |
// Replace the guard by its object. |
| 2842 |
ins->replaceAllUsesWith(rest_); |
| 2843 |
|
| 2844 |
// Remove original instruction. |
| 2845 |
ins->block()->discard(ins); |
| 2846 |
} |
| 2847 |
|
| 2848 |
void RestReplacer::visitUnbox(MUnbox* ins) { |
| 2849 |
// Skip unrelated unboxes. |
| 2850 |
if (ins->input() != rest_) { |
| 2851 |
return; |
| 2852 |
} |
| 2853 |
MOZ_ASSERT(ins->type() == MIRType::Object); |
| 2854 |
|
| 2855 |
// Replace the unbox with the array object. |
| 2856 |
ins->replaceAllUsesWith(rest_); |
| 2857 |
|
| 2858 |
// Remove the unbox. |
| 2859 |
ins->block()->discard(ins); |
| 2860 |
} |
| 2861 |
|
| 2862 |
void RestReplacer::visitCompare(MCompare* ins) { |
| 2863 |
// Skip unrelated comparisons. |
| 2864 |
if (ins->lhs() != rest_ && ins->rhs() != rest_) { |
| 2865 |
return; |
| 2866 |
} |
| 2867 |
|
| 2868 |
bool folded; |
| 2869 |
MOZ_ALWAYS_TRUE(ins->tryFold(&folded)); |
| 2870 |
|
| 2871 |
auto* cst = MConstant::New(alloc(), BooleanValue(folded)); |
| 2872 |
ins->block()->insertBefore(ins, cst); |
| 2873 |
|
| 2874 |
// Replace the comparison with a constant. |
| 2875 |
ins->replaceAllUsesWith(cst); |
| 2876 |
|
| 2877 |
// Remove original instruction. |
| 2878 |
ins->block()->discard(ins); |
| 2879 |
} |
| 2880 |
|
| 2881 |
void RestReplacer::visitLoadElement(MLoadElement* ins) { |
| 2882 |
// Skip other array objects. |
| 2883 |
MDefinition* elements = ins->elements(); |
| 2884 |
if (!isRestElements(elements)) { |
| 2885 |
return; |
| 2886 |
} |
| 2887 |
|
| 2888 |
MDefinition* index = ins->index(); |
| 2889 |
|
| 2890 |
// Adjust the index to skip any extra formals. |
| 2891 |
if (uint32_t formals = rest()->numFormals()) { |
| 2892 |
auto* numFormals = MConstant::New(alloc(), Int32Value(formals)); |
| 2893 |
ins->block()->insertBefore(ins, numFormals); |
| 2894 |
|
| 2895 |
auto* add = MAdd::New(alloc(), index, numFormals, TruncateKind::Truncate); |
| 2896 |
ins->block()->insertBefore(ins, add); |
| 2897 |
|
| 2898 |
index = add; |
| 2899 |
} |
| 2900 |
|
| 2901 |
auto* loadArg = MGetFrameArgument::New(alloc(), index); |
| 2902 |
|
| 2903 |
ins->block()->insertBefore(ins, loadArg); |
| 2904 |
ins->replaceAllUsesWith(loadArg); |
| 2905 |
|
| 2906 |
// Remove original instruction. |
| 2907 |
discardInstruction(ins, elements); |
| 2908 |
} |
| 2909 |
|
| 2910 |
MDefinition* RestReplacer::restLength(MInstruction* ins) { |
| 2911 |
// Compute |Math.max(numActuals - numFormals, 0)| for the rest array length. |
| 2912 |
|
| 2913 |
auto* numActuals = rest()->numActuals(); |
| 2914 |
|
| 2915 |
if (uint32_t formals = rest()->numFormals()) { |
| 2916 |
auto* numFormals = MConstant::New(alloc(), Int32Value(formals)); |
| 2917 |
ins->block()->insertBefore(ins, numFormals); |
| 2918 |
|
| 2919 |
auto* length = MSub::New(alloc(), numActuals, numFormals, MIRType::Int32); |
| 2920 |
length->setTruncateKind(TruncateKind::Truncate); |
| 2921 |
ins->block()->insertBefore(ins, length); |
| 2922 |
|
| 2923 |
auto* zero = MConstant::New(alloc(), Int32Value(0)); |
| 2924 |
ins->block()->insertBefore(ins, zero); |
| 2925 |
|
| 2926 |
bool isMax = true; |
| 2927 |
auto* minmax = MMinMax::New(alloc(), length, zero, MIRType::Int32, isMax); |
| 2928 |
ins->block()->insertBefore(ins, minmax); |
| 2929 |
|
| 2930 |
return minmax; |
| 2931 |
} |
| 2932 |
|
| 2933 |
return numActuals; |
| 2934 |
} |
| 2935 |
|
| 2936 |
void RestReplacer::visitLength(MInstruction* ins, MDefinition* elements) { |
| 2937 |
MOZ_ASSERT(ins->isArrayLength() || ins->isInitializedLength()); |
| 2938 |
|
| 2939 |
// Skip other array objects. |
| 2940 |
if (!isRestElements(elements)) { |
| 2941 |
return; |
| 2942 |
} |
| 2943 |
|
| 2944 |
MDefinition* replacement = restLength(ins); |
| 2945 |
|
| 2946 |
ins->replaceAllUsesWith(replacement); |
| 2947 |
|
| 2948 |
// Remove original instruction. |
| 2949 |
discardInstruction(ins, elements); |
| 2950 |
} |
| 2951 |
|
| 2952 |
void RestReplacer::visitArrayLength(MArrayLength* ins) { |
| 2953 |
visitLength(ins, ins->elements()); |
| 2954 |
} |
| 2955 |
|
| 2956 |
void RestReplacer::visitInitializedLength(MInitializedLength* ins) { |
| 2957 |
// The initialized length of a rest array is equal to its length. |
| 2958 |
visitLength(ins, ins->elements()); |
| 2959 |
} |
| 2960 |
|
| 2961 |
void RestReplacer::visitApplyArray(MApplyArray* ins) { |
| 2962 |
// Skip other array objects. |
| 2963 |
MDefinition* elements = ins->getElements(); |
| 2964 |
if (!isRestElements(elements)) { |
| 2965 |
return; |
| 2966 |
} |
| 2967 |
|
| 2968 |
auto* numActuals = restLength(ins); |
| 2969 |
|
| 2970 |
auto* apply = |
| 2971 |
MApplyArgs::New(alloc(), ins->getSingleTarget(), ins->getFunction(), |
| 2972 |
numActuals, ins->getThis(), rest()->numFormals()); |
| 2973 |
apply->setBailoutKind(ins->bailoutKind()); |
| 2974 |
if (!ins->maybeCrossRealm()) { |
| 2975 |
apply->setNotCrossRealm(); |
| 2976 |
} |
| 2977 |
if (ins->ignoresReturnValue()) { |
| 2978 |
apply->setIgnoresReturnValue(); |
| 2979 |
} |
| 2980 |
ins->block()->insertBefore(ins, apply); |
| 2981 |
|
| 2982 |
ins->replaceAllUsesWith(apply); |
| 2983 |
|
| 2984 |
apply->stealResumePoint(ins); |
| 2985 |
|
| 2986 |
// Remove original instruction. |
| 2987 |
discardInstruction(ins, elements); |
| 2988 |
} |
| 2989 |
|
| 2990 |
void RestReplacer::visitConstructArray(MConstructArray* ins) { |
| 2991 |
// Skip other array objects. |
| 2992 |
MDefinition* elements = ins->getElements(); |
| 2993 |
if (!isRestElements(elements)) { |
| 2994 |
return; |
| 2995 |
} |
| 2996 |
|
| 2997 |
auto* numActuals = restLength(ins); |
| 2998 |
|
| 2999 |
auto* construct = MConstructArgs::New( |
| 3000 |
alloc(), ins->getSingleTarget(), ins->getFunction(), numActuals, |
| 3001 |
ins->getThis(), ins->getNewTarget(), rest()->numFormals()); |
| 3002 |
construct->setBailoutKind(ins->bailoutKind()); |
| 3003 |
if (!ins->maybeCrossRealm()) { |
| 3004 |
construct->setNotCrossRealm(); |
| 3005 |
} |
| 3006 |
|
| 3007 |
ins->block()->insertBefore(ins, construct); |
| 3008 |
ins->replaceAllUsesWith(construct); |
| 3009 |
|
| 3010 |
construct->stealResumePoint(ins); |
| 3011 |
|
| 3012 |
// Remove original instruction. |
| 3013 |
discardInstruction(ins, elements); |
| 3014 |
} |
| 3015 |
|
| 3016 |
// WebAssembly only supports scalar replacement of structs with only inline |
| 3017 |
// data for now. |
| 3018 |
static inline bool IsOptimizableWasmStructInstruction(MInstruction* ins) { |
| 3019 |
return ins->isWasmNewStructObject() && |
| 3020 |
!ins->toWasmNewStructObject()->isOutline(); |
| 3021 |
} |
| 3022 |
|
| 3023 |
class WasmStructMemoryView : public MDefinitionVisitorDefaultNoop { |
| 3024 |
public: |
| 3025 |
using BlockState = MWasmStructState; |
| 3026 |
static const char phaseName[]; |
| 3027 |
|
| 3028 |
private: |
| 3029 |
TempAllocator& alloc_; |
| 3030 |
MInstruction* struct_; |
| 3031 |
MConstant* undefinedVal_; |
| 3032 |
MBasicBlock* startBlock_; |
| 3033 |
BlockState* state_; |
| 3034 |
|
| 3035 |
bool oom_; |
| 3036 |
|
| 3037 |
public: |
| 3038 |
WasmStructMemoryView(TempAllocator& alloc, MInstruction* wasmStruct); |
| 3039 |
|
| 3040 |
MBasicBlock* startingBlock(); |
| 3041 |
bool initStartingState(BlockState** pState); |
| 3042 |
|
| 3043 |
void setEntryBlockState(BlockState* state); |
| 3044 |
bool mergeIntoSuccessorState(MBasicBlock* curr, MBasicBlock* succ, |
| 3045 |
BlockState** pSuccState); |
| 3046 |
|
| 3047 |
void assertSuccess(); |
| 3048 |
|
| 3049 |
bool oom() const { return oom_; } |
| 3050 |
|
| 3051 |
public: |
| 3052 |
void visitResumePoint(MResumePoint* rp); |
| 3053 |
void visitPhi(MPhi* ins); |
| 3054 |
void visitWasmStoreField(MWasmStoreField* ins); |
| 3055 |
void visitWasmStoreFieldRef(MWasmStoreFieldRef* ins); |
| 3056 |
void visitWasmLoadField(MWasmLoadField* ins); |
| 3057 |
void visitWasmPostWriteBarrierWholeCell(MWasmPostWriteBarrierWholeCell* ins); |
| 3058 |
}; |
| 3059 |
|
| 3060 |
void WasmStructMemoryView::setEntryBlockState(BlockState* state) { |
| 3061 |
state_ = state; |
| 3062 |
} |
| 3063 |
|
| 3064 |
void WasmStructMemoryView::assertSuccess() { |
| 3065 |
// Make sure that the undefined value used as a placeholder is not used. |
| 3066 |
MOZ_RELEASE_ASSERT(!undefinedVal_->hasUses()); |
| 3067 |
|
| 3068 |
// Make sure that the MWasmNewStruct instruction is not used anymore. |
| 3069 |
MOZ_RELEASE_ASSERT(!struct_->hasUses()); |
| 3070 |
} |
| 3071 |
|
| 3072 |
MBasicBlock* WasmStructMemoryView::startingBlock() { return startBlock_; } |
| 3073 |
|
| 3074 |
bool WasmStructMemoryView::initStartingState(BlockState** pState) { |
| 3075 |
// We need this undefined value to initialize phi inputs if we create some |
| 3076 |
// later. |
| 3077 |
undefinedVal_ = MConstant::New(alloc_, UndefinedValue()); |
| 3078 |
|
| 3079 |
// Create a new block state and insert at it at the location of the new |
| 3080 |
// struct. |
| 3081 |
BlockState* state = BlockState::New(alloc_, struct_); |
| 3082 |
if (!state || !state->init()) { |
| 3083 |
return false; |
| 3084 |
} |
| 3085 |
|
| 3086 |
*pState = state; |
| 3087 |
return true; |
| 3088 |
} |
| 3089 |
|
| 3090 |
// Return true if all phi operands are equal to |newStruct|. |
| 3091 |
static bool WasmStructPhiOperandsEqualTo(MPhi* phi, MInstruction* newStruct) { |
| 3092 |
MOZ_ASSERT(IsOptimizableWasmStructInstruction(newStruct)); |
| 3093 |
|
| 3094 |
for (size_t i = 0; i < phi->numOperands(); i++) { |
| 3095 |
if (!PhiOperandEqualTo(phi->getOperand(i), newStruct)) { |
| 3096 |
return false; |
| 3097 |
} |
| 3098 |
} |
| 3099 |
return true; |
| 3100 |
} |
| 3101 |
|
| 3102 |
void WasmStructMemoryView::visitPhi(MPhi* ins) { |
| 3103 |
// Skip phis on other objects. |
| 3104 |
if (!WasmStructPhiOperandsEqualTo(ins, struct_)) { |
| 3105 |
return; |
| 3106 |
} |
| 3107 |
|
| 3108 |
// Replace the phi by its object. |
| 3109 |
ins->replaceAllUsesWith(struct_); |
| 3110 |
|
| 3111 |
// Remove original instruction. |
| 3112 |
ins->block()->discardPhi(ins); |
| 3113 |
} |
| 3114 |
|
| 3115 |
// We need to define this method for the pass to work, |
| 3116 |
// but we don't have resume points in wasm. |
| 3117 |
void WasmStructMemoryView::visitResumePoint(MResumePoint* rp) {} |
| 3118 |
|
| 3119 |
void WasmStructMemoryView::visitWasmStoreField(MWasmStoreField* ins) { |
| 3120 |
// Skip stores made on other structs. |
| 3121 |
MDefinition* base = ins->base(); |
| 3122 |
if (base != struct_) { |
| 3123 |
return; |
| 3124 |
} |
| 3125 |
|
| 3126 |
// Clone the state and update the field value. |
| 3127 |
state_ = BlockState::Copy(alloc_, state_); |
| 3128 |
if (!state_) { |
| 3129 |
oom_ = true; |
| 3130 |
return; |
| 3131 |
} |
| 3132 |
|
| 3133 |
// Update the state |
| 3134 |
state_->setField(ins->structFieldIndex().value(), ins->value()); |
| 3135 |
|
| 3136 |
// Remove original instruction. |
| 3137 |
ins->block()->discard(ins); |
| 3138 |
} |
| 3139 |
|
| 3140 |
void WasmStructMemoryView::visitWasmStoreFieldRef(MWasmStoreFieldRef* ins) { |
| 3141 |
// Skip stores made on other structs. |
| 3142 |
MDefinition* base = ins->base(); |
| 3143 |
if (base != struct_) { |
| 3144 |
return; |
| 3145 |
} |
| 3146 |
|
| 3147 |
// Clone the state and update the field value. |
| 3148 |
state_ = BlockState::Copy(alloc_, state_); |
| 3149 |
if (!state_) { |
| 3150 |
oom_ = true; |
| 3151 |
return; |
| 3152 |
} |
| 3153 |
|
| 3154 |
// Update the state |
| 3155 |
state_->setField(ins->structFieldIndex().value(), ins->value()); |
| 3156 |
|
| 3157 |
// Remove original instruction. |
| 3158 |
ins->block()->discard(ins); |
| 3159 |
} |
| 3160 |
|
| 3161 |
void WasmStructMemoryView::visitWasmLoadField(MWasmLoadField* ins) { |
| 3162 |
// Skip loads made on other structs. |
| 3163 |
MDefinition* base = ins->base(); |
| 3164 |
if (base != struct_) { |
| 3165 |
return; |
| 3166 |
} |
| 3167 |
|
| 3168 |
MDefinition* value = state_->getField(ins->structFieldIndex().value()); |
| 3169 |
|
| 3170 |
// Replace load by the field value. |
| 3171 |
ins->replaceAllUsesWith(value); |
| 3172 |
|
| 3173 |
// Remove original instruction. |
| 3174 |
ins->block()->discard(ins); |
| 3175 |
} |
| 3176 |
|
| 3177 |
void WasmStructMemoryView::visitWasmPostWriteBarrierWholeCell( |
| 3178 |
MWasmPostWriteBarrierWholeCell* ins) { |
| 3179 |
// Skip loads made on other objects. |
| 3180 |
if (ins->object() != struct_) { |
| 3181 |
return; |
| 3182 |
} |
| 3183 |
|
| 3184 |
// Remove original instruction. |
| 3185 |
ins->block()->discard(ins); |
| 3186 |
} |
| 3187 |
|
| 3188 |
bool WasmStructMemoryView::mergeIntoSuccessorState(MBasicBlock* curr, |
| 3189 |
MBasicBlock* succ, |
| 3190 |
BlockState** pSuccState) { |
| 3191 |
BlockState* succState = *pSuccState; |
| 3192 |
|
| 3193 |
// When a block has no state yet, create an empty one for the |
| 3194 |
// successor. |
| 3195 |
if (!succState) { |
| 3196 |
// If the successor is not dominated then the struct cannot flow |
| 3197 |
// in this basic block without a Phi. We know that no Phi exist |
| 3198 |
// in non-dominated successors as the conservative escaped |
| 3199 |
// analysis fails otherwise. Such condition can succeed if the |
| 3200 |
// successor is a join at the end of a if-block and the struct |
| 3201 |
// only exists within the branch. |
| 3202 |
if (!startBlock_->dominates(succ)) { |
| 3203 |
return true; |
| 3204 |
} |
| 3205 |
|
| 3206 |
// If there is only one predecessor, carry over the last state of the |
| 3207 |
// block to the successor. As the block state is immutable, if the |
| 3208 |
// current block has multiple successors, they will share the same entry |
| 3209 |
// state. |
| 3210 |
if (succ->numPredecessors() <= 1 || !state_->numFields()) { |
| 3211 |
*pSuccState = state_; |
| 3212 |
return true; |
| 3213 |
} |
| 3214 |
|
| 3215 |
// If we have multiple predecessors, then we allocate one Phi node for |
| 3216 |
// each predecessor, and create a new block state which only has phi |
| 3217 |
// nodes. These would later be removed by the removal of redundant phi |
| 3218 |
// nodes. |
| 3219 |
succState = BlockState::Copy(alloc_, state_); |
| 3220 |
if (!succState) { |
| 3221 |
return false; |
| 3222 |
} |
| 3223 |
|
| 3224 |
size_t numPreds = succ->numPredecessors(); |
| 3225 |
for (size_t index = 0; index < state_->numFields(); index++) { |
| 3226 |
MPhi* phi = MPhi::New(alloc_.fallible()); |
| 3227 |
if (!phi || !phi->reserveLength(numPreds)) { |
| 3228 |
return false; |
| 3229 |
} |
| 3230 |
|
| 3231 |
// Fill the input of the successors Phi with undefined |
| 3232 |
// values, and each block later fills the Phi inputs. |
| 3233 |
for (size_t p = 0; p < numPreds; p++) { |
| 3234 |
phi->addInput(undefinedVal_); |
| 3235 |
} |
| 3236 |
|
| 3237 |
// Add Phi in the list of Phis of the basic block. |
| 3238 |
succ->addPhi(phi); |
| 3239 |
|
| 3240 |
// Set the result type of this phi depending on the previous fields. |
| 3241 |
phi->setResultType(succState->getField(index)->type()); |
| 3242 |
succState->setField(index, phi); |
| 3243 |
} |
| 3244 |
|
| 3245 |
*pSuccState = succState; |
| 3246 |
} |
| 3247 |
|
| 3248 |
MOZ_ASSERT_IF(succ == startBlock_, startBlock_->isLoopHeader()); |
| 3249 |
if (succ->numPredecessors() > 1 && succState->numFields() && |
| 3250 |
succ != startBlock_) { |
| 3251 |
// We need to re-compute successorWithPhis as the previous EliminatePhis |
| 3252 |
// phase might have removed all the Phis from the successor block. |
| 3253 |
size_t currIndex; |
| 3254 |
MOZ_ASSERT(!succ->phisEmpty()); |
| 3255 |
if (curr->successorWithPhis()) { |
| 3256 |
MOZ_ASSERT(curr->successorWithPhis() == succ); |
| 3257 |
currIndex = curr->positionInPhiSuccessor(); |
| 3258 |
} else { |
| 3259 |
currIndex = succ->indexForPredecessor(curr); |
| 3260 |
curr->setSuccessorWithPhis(succ, currIndex); |
| 3261 |
} |
| 3262 |
MOZ_ASSERT(succ->getPredecessor(currIndex) == curr); |
| 3263 |
|
| 3264 |
// Copy the current element states to the index of current block in all |
| 3265 |
// the Phi created during the first visit of the successor. |
| 3266 |
for (size_t index = 0; index < state_->numFields(); index++) { |
| 3267 |
MPhi* phi = succState->getField(index)->toPhi(); |
| 3268 |
phi->replaceOperand(currIndex, state_->getField(index)); |
| 3269 |
} |
| 3270 |
} |
| 3271 |
|
| 3272 |
return true; |
| 3273 |
} |
| 3274 |
|
| 3275 |
// Returns False if the wasm struct is not escaped and if it is optimizable by |
| 3276 |
// ScalarReplacementOfStruct. |
| 3277 |
static bool IsWasmStructEscaped(MDefinition* ins, MInstruction* newStruct) { |
| 3278 |
MOZ_ASSERT(ins->type() == MIRType::WasmAnyRef); |
| 3279 |
MOZ_ASSERT(IsOptimizableWasmStructInstruction(newStruct)); |
| 3280 |
|
| 3281 |
JitSpewDef(JitSpew_Escape, "Check wasm struct\n", ins); |
| 3282 |
JitSpewIndent spewIndent(JitSpew_Escape); |
| 3283 |
|
| 3284 |
// Don't do scalar replacement on big structs. |
| 3285 |
if (newStruct->isWasmNewStructObject()) { |
| 3286 |
if (newStruct->toWasmNewStructObject()->structType().fields_.length() > |
| 3287 |
wasm::MaxFieldsScalarReplacementStructs) { |
| 3288 |
JitSpew(JitSpew_Escape, "struct too big for scalar replacement\n"); |
| 3289 |
return true; |
| 3290 |
} |
| 3291 |
} |
| 3292 |
|
| 3293 |
// We don't support defaultable structs for now. |
| 3294 |
if (newStruct->isWasmNewStructObject() && |
| 3295 |
newStruct->toWasmNewStructObject()->zeroFields()) { |
| 3296 |
JitSpew(JitSpew_Escape, "Struct is created with new_default\n"); |
| 3297 |
return true; |
| 3298 |
} |
| 3299 |
|
| 3300 |
// Check if the struct is escaped. If the object is not the first argument |
| 3301 |
// of either a known Store / Load, then we consider it as escaped. This is a |
| 3302 |
// cheap and conservative escape analysis. |
| 3303 |
for (MUseIterator i(ins->usesBegin()); i != ins->usesEnd(); i++) { |
| 3304 |
MNode* consumer = (*i)->consumer(); |
| 3305 |
|
| 3306 |
if (!consumer->isDefinition()) { |
| 3307 |
JitSpew(JitSpew_Escape, "Wasm struct is escaped"); |
| 3308 |
return true; |
| 3309 |
} |
| 3310 |
|
| 3311 |
MDefinition* def = consumer->toDefinition(); |
| 3312 |
switch (def->op()) { |
| 3313 |
// This instruction can only store primitive types. |
| 3314 |
// Another struct won't be able to escape through it. |
| 3315 |
case MDefinition::Opcode::WasmStoreField: { |
| 3316 |
break; |
| 3317 |
} |
| 3318 |
case MDefinition::Opcode::WasmStoreFieldRef: { |
| 3319 |
// Escaped if it's stored into another struct. |
| 3320 |
if (def->toWasmStoreFieldRef()->value() == ins) { |
| 3321 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 3322 |
return true; |
| 3323 |
} |
| 3324 |
break; |
| 3325 |
} |
| 3326 |
// Not escaped if we load into a field of this struct. |
| 3327 |
case MDefinition::Opcode::WasmLoadField: { |
| 3328 |
break; |
| 3329 |
} |
| 3330 |
// Handle phis. |
| 3331 |
case MDefinition::Opcode::Phi: { |
| 3332 |
auto* phi = def->toPhi(); |
| 3333 |
if (!WasmStructPhiOperandsEqualTo(phi, newStruct)) { |
| 3334 |
JitSpewDef(JitSpew_Escape, "has different phi operands\n", def); |
| 3335 |
return true; |
| 3336 |
} |
| 3337 |
if (IsWasmStructEscaped(phi, newStruct)) { |
| 3338 |
JitSpewDef(JitSpew_Escape, "is indirectly escaped by\n", def); |
| 3339 |
return true; |
| 3340 |
} |
| 3341 |
break; |
| 3342 |
} |
| 3343 |
|
| 3344 |
case MDefinition::Opcode::WasmPostWriteBarrierWholeCell: { |
| 3345 |
break; |
| 3346 |
} |
| 3347 |
|
| 3348 |
// By default, we consider the struct as escaped. |
| 3349 |
default: |
| 3350 |
JitSpewDef(JitSpew_Escape, "is escaped by\n", def); |
| 3351 |
return true; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
|
| 3355 |
JitSpew(JitSpew_Escape, "Struct is not escaped"); |
| 3356 |
return false; |
| 3357 |
} |
| 3358 |
|
| 3359 |
/* static */ const char WasmStructMemoryView::phaseName[] = |
| 3360 |
"Scalar Replacement of wasm structs"; |
| 3361 |
|
| 3362 |
WasmStructMemoryView::WasmStructMemoryView(TempAllocator& alloc, |
| 3363 |
MInstruction* wasmStruct) |
| 3364 |
: alloc_(alloc), |
| 3365 |
struct_(wasmStruct), |
| 3366 |
undefinedVal_(nullptr), |
| 3367 |
startBlock_(wasmStruct->block()), |
| 3368 |
state_(nullptr), |
| 3369 |
oom_(false) {} |
| 3370 |
|
| 3371 |
bool ScalarReplacement(const MIRGenerator* mir, MIRGraph& graph) { |
| 3372 |
JitSpew(JitSpew_Escape, "Begin (ScalarReplacement)"); |
| 3373 |
|
| 3374 |
EmulateStateOf<ObjectMemoryView> replaceObject(mir, graph); |
| 3375 |
EmulateStateOf<ArrayMemoryView> replaceArray(mir, graph); |
| 3376 |
EmulateStateOf<WasmStructMemoryView> replaceWasmStructs(mir, graph); |
| 3377 |
bool addedPhi = false; |
| 3378 |
|
| 3379 |
for (ReversePostorderIterator block = graph.rpoBegin(); |
| 3380 |
block != graph.rpoEnd(); block++) { |
| 3381 |
if (mir->shouldCancel("Scalar Replacement (main loop)")) { |
| 3382 |
return false; |
| 3383 |
} |
| 3384 |
|
| 3385 |
for (MInstructionIterator ins = block->begin(); ins != block->end(); |
| 3386 |
ins++) { |
| 3387 |
if (IsOptimizableObjectInstruction(*ins) && |
| 3388 |
!IsObjectEscaped(*ins, *ins)) { |
| 3389 |
ObjectMemoryView view(graph.alloc(), *ins); |
| 3390 |
if (!replaceObject.run(view)) { |
| 3391 |
return false; |
| 3392 |
} |
| 3393 |
view.assertSuccess(); |
| 3394 |
addedPhi = true; |
| 3395 |
continue; |
| 3396 |
} |
| 3397 |
|
| 3398 |
if (IsOptimizableArrayInstruction(*ins) && !IsArrayEscaped(*ins, *ins)) { |
| 3399 |
ArrayMemoryView view(graph.alloc(), *ins); |
| 3400 |
if (!replaceArray.run(view)) { |
| 3401 |
return false; |
| 3402 |
} |
| 3403 |
view.assertSuccess(); |
| 3404 |
addedPhi = true; |
| 3405 |
continue; |
| 3406 |
} |
| 3407 |
|
| 3408 |
if (IsOptimizableArgumentsInstruction(*ins)) { |
| 3409 |
ArgumentsReplacer replacer(mir, graph, *ins); |
| 3410 |
if (replacer.escapes(*ins)) { |
| 3411 |
continue; |
| 3412 |
} |
| 3413 |
if (!replacer.run()) { |
| 3414 |
return false; |
| 3415 |
} |
| 3416 |
continue; |
| 3417 |
} |
| 3418 |
|
| 3419 |
if (IsOptimizableRestInstruction(*ins)) { |
| 3420 |
RestReplacer replacer(mir, graph, *ins); |
| 3421 |
if (replacer.escapes(*ins)) { |
| 3422 |
continue; |
| 3423 |
} |
| 3424 |
if (!replacer.run()) { |
| 3425 |
return false; |
| 3426 |
} |
| 3427 |
continue; |
| 3428 |
} |
| 3429 |
|
| 3430 |
if (IsOptimizableWasmStructInstruction(*ins) && |
| 3431 |
!IsWasmStructEscaped(*ins, *ins)) { |
| 3432 |
WasmStructMemoryView view(graph.alloc(), *ins); |
| 3433 |
if (!replaceWasmStructs.run(view)) { |
| 3434 |
return false; |
| 3435 |
} |
| 3436 |
view.assertSuccess(); |
| 3437 |
addedPhi = true; |
| 3438 |
continue; |
| 3439 |
} |
| 3440 |
} |
| 3441 |
} |
| 3442 |
|
| 3443 |
if (addedPhi) { |
| 3444 |
// Phis added by Scalar Replacement are only redundant Phis which are |
| 3445 |
// not directly captured by any resume point but only by the MDefinition |
| 3446 |
// state. The conservative observability only focuses on Phis which are |
| 3447 |
// not used as resume points operands. |
| 3448 |
AssertExtendedGraphCoherency(graph); |
| 3449 |
if (!EliminatePhis(mir, graph, ConservativeObservability)) { |
| 3450 |
return false; |
| 3451 |
} |
| 3452 |
} |
| 3453 |
|
| 3454 |
return true; |
| 3455 |
} |
| 3456 |
|
| 3457 |
} /* namespace jit */ |
| 3458 |
} /* namespace js */ |