Debian Codegraph @

js/src/jit/MIRGraph.cpp

raw
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/MIRGraph.h"
8
9 #include "jit/CompileInfo.h"
10 #include "jit/InlineScriptTree.h"
11 #include "jit/IonOptimizationLevels.h"
12 #include "jit/JitSpewer.h"
13 #include "jit/MIR-wasm.h"
14 #include "jit/MIR.h"
15 #include "jit/MIRGenerator.h"
16 #include "wasm/WasmMetadata.h"
17
18 using namespace js;
19 using namespace js::jit;
20
21 MIRGenerator::MIRGenerator(CompileRealm* realm,
22 const JitCompileOptions& options,
23 TempAllocator* alloc, MIRGraph* graph,
24 const CompileInfo* info,
25 const OptimizationInfo* optimizationInfo,
26 const wasm::CodeMetadata* wasmCodeMeta)
27 : realm(realm),
28 runtime(realm ? realm->runtime() : nullptr),
29 outerInfo_(info),
30 optimizationInfo_(optimizationInfo),
31 alloc_(alloc),
32 graph_(graph),
33 offThreadStatus_(Ok()),
34 cancelBuild_(false),
35 wasmMaxStackArgBytes_(0),
36 needsOverrecursedCheck_(false),
37 needsStaticStackAlignment_(false),
38 instrumentedProfiling_(false),
39 instrumentedProfilingIsCached_(false),
40 stringsCanBeInNursery_(realm ? realm->zone()->canNurseryAllocateStrings()
41 : false),
42 bigIntsCanBeInNursery_(realm ? realm->zone()->canNurseryAllocateBigInts()
43 : false),
44 minWasmMemory0Length_(0),
45 options(options),
46 gs_(alloc, wasmCodeMeta) {}
47
48 bool MIRGenerator::licmEnabled() const {
49 return optimizationInfo().licmEnabled() && !disableLICM_ &&
50 !outerInfo().hadLICMInvalidation();
51 }
52
53 bool MIRGenerator::branchHintingEnabled() const {
54 return outerInfo().branchHintingEnabled();
55 }
56
57 mozilla::GenericErrorResult<AbortReason> MIRGenerator::abort(AbortReason r) {
58 if (JitSpewEnabled(JitSpew_IonAbort)) {
59 switch (r) {
60 case AbortReason::Alloc:
61 JitSpew(JitSpew_IonAbort, "AbortReason::Alloc");
62 break;
63 case AbortReason::Disable:
64 JitSpew(JitSpew_IonAbort, "AbortReason::Disable");
65 break;
66 case AbortReason::Error:
67 JitSpew(JitSpew_IonAbort, "AbortReason::Error");
68 break;
69 case AbortReason::NoAbort:
70 MOZ_CRASH("Abort with AbortReason::NoAbort");
71 break;
72 }
73 }
74 return Err(std::move(r));
75 }
76
77 mozilla::GenericErrorResult<AbortReason> MIRGenerator::abortFmt(
78 AbortReason r, const char* message, va_list ap) {
79 JitSpewVA(JitSpew_IonAbort, message, ap);
80 return Err(std::move(r));
81 }
82
83 mozilla::GenericErrorResult<AbortReason> MIRGenerator::abort(
84 AbortReason r, const char* message, ...) {
85 va_list ap;
86 va_start(ap, message);
87 auto forward = abortFmt(r, message, ap);
88 va_end(ap);
89 return forward;
90 }
91
92 void MIRGraph::addBlock(MBasicBlock* block) {
93 MOZ_ASSERT(block);
94 block->setId(blockIdGen_++);
95 blocks_.pushBack(block);
96 numBlocks_++;
97 }
98
99 void MIRGraph::insertBlockAfter(MBasicBlock* at, MBasicBlock* block) {
100 block->setId(blockIdGen_++);
101 blocks_.insertAfter(at, block);
102 numBlocks_++;
103 }
104
105 void MIRGraph::insertBlockBefore(MBasicBlock* at, MBasicBlock* block) {
106 block->setId(blockIdGen_++);
107 blocks_.insertBefore(at, block);
108 numBlocks_++;
109 }
110
111 void MIRGraph::removeBlock(MBasicBlock* block) {
112 // Remove a block from the graph. It will also cleanup the block.
113
114 if (block == osrBlock_) {
115 osrBlock_ = nullptr;
116 }
117
118 if (returnAccumulator_) {
119 size_t i = 0;
120 while (i < returnAccumulator_->length()) {
121 if ((*returnAccumulator_)[i] == block) {
122 returnAccumulator_->erase(returnAccumulator_->begin() + i);
123 } else {
124 i++;
125 }
126 }
127 }
128
129 block->clear();
130 block->markAsDead();
131
132 if (block->isInList()) {
133 blocks_.remove(block);
134 numBlocks_--;
135 }
136 }
137
138 void MIRGraph::unmarkBlocks() {
139 for (MBasicBlockIterator i(blocks_.begin()); i != blocks_.end(); i++) {
140 i->unmark();
141 }
142 }
143
144 MBasicBlock* MBasicBlock::New(MIRGraph& graph, size_t stackDepth,
145 const CompileInfo& info, MBasicBlock* maybePred,
146 BytecodeSite* site, Kind kind) {
147 MOZ_ASSERT(site->pc() != nullptr);
148
149 MBasicBlock* block = new (graph.alloc()) MBasicBlock(graph, info, site, kind);
150 if (!block->init()) {
151 return nullptr;
152 }
153
154 if (!block->inherit(graph.alloc(), stackDepth, maybePred, 0)) {
155 return nullptr;
156 }
157
158 return block;
159 }
160
161 MBasicBlock* MBasicBlock::NewPopN(MIRGraph& graph, const CompileInfo& info,
162 MBasicBlock* pred, BytecodeSite* site,
163 Kind kind, uint32_t popped) {
164 MOZ_ASSERT(site->pc() != nullptr);
165
166 MBasicBlock* block = new (graph.alloc()) MBasicBlock(graph, info, site, kind);
167 if (!block->init()) {
168 return nullptr;
169 }
170
171 if (!block->inherit(graph.alloc(), pred->stackDepth(), pred, popped)) {
172 return nullptr;
173 }
174
175 return block;
176 }
177
178 MBasicBlock* MBasicBlock::NewPendingLoopHeader(MIRGraph& graph,
179 const CompileInfo& info,
180 MBasicBlock* pred,
181 BytecodeSite* site) {
182 MOZ_ASSERT(site->pc() != nullptr);
183
184 MBasicBlock* block =
185 new (graph.alloc()) MBasicBlock(graph, info, site, PENDING_LOOP_HEADER);
186 if (!block->init()) {
187 return nullptr;
188 }
189
190 if (!block->inherit(graph.alloc(), pred->stackDepth(), pred, 0)) {
191 return nullptr;
192 }
193
194 return block;
195 }
196
197 MBasicBlock* MBasicBlock::NewSplitEdge(MIRGraph& graph, MBasicBlock* pred,
198 size_t predEdgeIdx, MBasicBlock* succ) {
199 MBasicBlock* split = nullptr;
200 if (!succ->pc()) {
201 // The predecessor does not have a PC, this is a Wasm compilation.
202 split = MBasicBlock::New(graph, succ->info(), pred, SPLIT_EDGE);
203 if (!split) {
204 return nullptr;
205 }
206
207 // Insert the split edge block in-between.
208 split->end(MGoto::New(graph.alloc(), succ));
209 } else {
210 // The predecessor has a PC, this is a Warp compilation.
211 MResumePoint* succEntry = succ->entryResumePoint();
212
213 BytecodeSite* site =
214 new (graph.alloc()) BytecodeSite(succ->trackedTree(), succEntry->pc());
215 split =
216 new (graph.alloc()) MBasicBlock(graph, succ->info(), site, SPLIT_EDGE);
217
218 if (!split->init()) {
219 return nullptr;
220 }
221
222 // A split edge is used to simplify the graph to avoid having a
223 // predecessor with multiple successors as well as a successor with
224 // multiple predecessors. As instructions can be moved in this
225 // split-edge block, we need to give this block a resume point. To do
226 // so, we copy the entry resume points of the successor and filter the
227 // phis to keep inputs from the current edge.
228
229 // Propagate the caller resume point from the inherited block.
230 split->callerResumePoint_ = succ->callerResumePoint();
231
232 // Split-edge are created after the interpreter stack emulation. Thus,
233 // there is no need for creating slots.
234 split->stackPosition_ = succEntry->stackDepth();
235
236 // Create a resume point using our initial stack position.
237 MResumePoint* splitEntry = new (graph.alloc())
238 MResumePoint(split, succEntry->pc(), ResumeMode::ResumeAt);
239 if (!splitEntry->init(graph.alloc())) {
240 return nullptr;
241 }
242 split->entryResumePoint_ = splitEntry;
243
244 // Insert the split edge block in-between.
245 split->end(MGoto::New(graph.alloc(), succ));
246
247 // The target entry resume point might have phi operands, keep the
248 // operands of the phi coming from our edge.
249 size_t succEdgeIdx = succ->indexForPredecessor(pred);
250
251 for (size_t i = 0, e = splitEntry->numOperands(); i < e; i++) {
252 MDefinition* def = succEntry->getOperand(i);
253 // This early in the pipeline, we have no recover instructions in
254 // any entry resume point.
255 if (def->block() == succ) {
256 if (def->isPhi()) {
257 def = def->toPhi()->getOperand(succEdgeIdx);
258 } else {
259 // The phi-operand may already have been optimized out.
260 MOZ_ASSERT(def->isConstant());
261 MOZ_ASSERT(def->type() == MIRType::MagicOptimizedOut);
262
263 def = split->optimizedOutConstant(graph.alloc());
264 }
265 }
266
267 splitEntry->initOperand(i, def);
268 }
269
270 // This is done in the New variant for wasm, so we cannot keep this
271 // line below, where the rest of the graph is modified.
272 if (!split->predecessors_.append(pred)) {
273 return nullptr;
274 }
275 }
276
277 split->setLoopDepth(succ->loopDepth());
278
279 graph.insertBlockAfter(pred, split);
280
281 pred->replaceSuccessor(predEdgeIdx, split);
282 succ->replacePredecessor(pred, split);
283 return split;
284 }
285
286 void MBasicBlock::moveToNewBlock(MInstruction* ins, MBasicBlock* dst) {
287 MOZ_ASSERT(ins->block() == this);
288 MOZ_ASSERT(!dst->hasLastIns());
289 instructions_.remove(ins);
290 ins->setInstructionBlock(dst, dst->trackedSite());
291 if (MResumePoint* rp = ins->resumePoint()) {
292 removeResumePoint(rp);
293 dst->addResumePoint(rp);
294 rp->setBlock(dst);
295 }
296 dst->instructions_.pushBack(ins);
297 }
298
299 void MBasicBlock::moveOuterResumePointTo(MBasicBlock* dest) {
300 if (MResumePoint* outer = outerResumePoint()) {
301 removeResumePoint(outer);
302 outerResumePoint_ = nullptr;
303 dest->setOuterResumePoint(outer);
304 dest->addResumePoint(outer);
305 outer->setBlock(dest);
306 }
307 }
308
309 bool MBasicBlock::wrapInstructionInFastpath(MInstruction* ins,
310 MInstruction* fastpath,
311 MInstruction* condition) {
312 MOZ_ASSERT(ins->block() == this);
313 MOZ_ASSERT(!ins->isControlInstruction());
314
315 MInstructionIterator rest(begin(ins));
316 rest++;
317
318 MResumePoint* resumeBeforeIns = activeResumePoint(ins);
319 MResumePoint* resumeAfterIns = activeResumePoint(*rest);
320
321 // Create the join block.
322 MBasicBlock* join = MBasicBlock::NewInternal(graph_, this, resumeAfterIns);
323 if (!join) {
324 return false;
325 }
326
327 // Update the successors of the current block.
328 for (uint32_t i = 0; i < numSuccessors(); i++) {
329 getSuccessor(i)->replacePredecessor(this, join);
330 }
331 if (successorWithPhis()) {
332 join->setSuccessorWithPhis(successorWithPhis(), positionInPhiSuccessor());
333 clearSuccessorWithPhis();
334 }
335
336 // Copy all instructions after |ins| into the join block.
337 while (rest != end()) {
338 MInstruction* ins = *rest++;
339 moveToNewBlock(ins, join);
340 }
341 MOZ_ASSERT(!hasLastIns());
342 MOZ_ASSERT(join->hasLastIns());
343
344 graph_.insertBlockAfter(this, join);
345
346 // Create the fast path block.
347 MBasicBlock* fastpathBlock =
348 MBasicBlock::NewInternal(graph_, this, resumeBeforeIns);
349 if (!fastpathBlock) {
350 return false;
351 }
352 graph_.insertBlockAfter(this, fastpathBlock);
353 fastpathBlock->add(fastpath);
354 fastpathBlock->end(MGoto::New(graph_.alloc(), join));
355
356 // Create the slowpath block.
357 MBasicBlock* slowpathBlock =
358 MBasicBlock::NewInternal(graph_, this, resumeBeforeIns);
359 if (!slowpathBlock) {
360 return false;
361 }
362 graph_.insertBlockAfter(fastpathBlock, slowpathBlock);
363 moveToNewBlock(ins, slowpathBlock);
364 slowpathBlock->end(MGoto::New(graph_.alloc(), join));
365
366 // Connect current block to fastpath and slowpath.
367 add(condition);
368 end(MTest::New(graph_.alloc(), condition, fastpathBlock, slowpathBlock));
369
370 // Update predecessors.
371 if (!fastpathBlock->addPredecessorWithoutPhis(this) ||
372 !slowpathBlock->addPredecessorWithoutPhis(this) ||
373 !join->addPredecessorWithoutPhis(fastpathBlock) ||
374 !join->addPredecessorWithoutPhis(slowpathBlock)) {
375 return false;
376 }
377
378 if (ins->hasUses()) {
379 // Insert phi.
380 MPhi* phi = MPhi::New(graph_.alloc());
381 if (!phi->reserveLength(2)) {
382 return false;
383 }
384 phi->addInput(fastpath);
385 fastpathBlock->setSuccessorWithPhis(join, 0);
386 phi->addInput(ins);
387 slowpathBlock->setSuccessorWithPhis(join, 1);
388 join->addPhi(phi);
389
390 for (MUseIterator i(ins->usesBegin()), e(ins->usesEnd()); i != e;) {
391 MUse* use = *i++;
392 if (use->consumer() != phi && use->consumer() != ins->resumePoint()) {
393 use->replaceProducer(phi);
394 }
395 }
396 }
397
398 moveOuterResumePointTo(join);
399
400 return true;
401 }
402
403 MBasicBlock* MBasicBlock::NewInternal(MIRGraph& graph, MBasicBlock* orig,
404 MResumePoint* resumePoint) {
405 jsbytecode* pc = IsResumeAfter(resumePoint->mode())
406 ? GetNextPc(resumePoint->pc())
407 : resumePoint->pc();
408
409 BytecodeSite* site =
410 new (graph.alloc()) BytecodeSite(orig->trackedTree(), pc);
411 MBasicBlock* block =
412 new (graph.alloc()) MBasicBlock(graph, orig->info(), site, INTERNAL);
413 if (!block->init()) {
414 return nullptr;
415 }
416
417 // Propagate the caller resume point from the original block.
418 block->callerResumePoint_ = orig->callerResumePoint();
419
420 // Copy the resume point.
421 block->stackPosition_ = resumePoint->stackDepth();
422 MResumePoint* entryResumePoint =
423 new (graph.alloc()) MResumePoint(block, pc, ResumeMode::ResumeAt);
424 if (!entryResumePoint->init(graph.alloc())) {
425 return nullptr;
426 }
427 for (size_t i = 0; i < resumePoint->stackDepth(); i++) {
428 entryResumePoint->initOperand(i, resumePoint->getOperand(i));
429 }
430 block->entryResumePoint_ = entryResumePoint;
431
432 block->setLoopDepth(orig->loopDepth());
433
434 return block;
435 }
436
437 MBasicBlock* MBasicBlock::New(MIRGraph& graph, const CompileInfo& info,
438 MBasicBlock* pred, Kind kind) {
439 BytecodeSite* site = new (graph.alloc()) BytecodeSite();
440 MBasicBlock* block = new (graph.alloc()) MBasicBlock(graph, info, site, kind);
441 if (!block->init()) {
442 return nullptr;
443 }
444
445 if (pred) {
446 block->stackPosition_ = pred->stackPosition_;
447
448 if (block->kind_ == PENDING_LOOP_HEADER) {
449 size_t nphis = block->stackPosition_;
450
451 size_t nfree = graph.phiFreeListLength();
452
453 TempAllocator& alloc = graph.alloc();
454 MPhi* phis = nullptr;
455 if (nphis > nfree) {
456 phis = alloc.allocateArray<MPhi>(nphis - nfree);
457 if (!phis) {
458 return nullptr;
459 }
460 }
461
462 // Note: Phis are inserted in the same order as the slots.
463 for (size_t i = 0; i < nphis; i++) {
464 MDefinition* predSlot = pred->getSlot(i);
465
466 MOZ_ASSERT(predSlot->type() != MIRType::Value);
467
468 MPhi* phi;
469 if (i < nfree) {
470 phi = graph.takePhiFromFreeList();
471 } else {
472 phi = phis + (i - nfree);
473 }
474 new (phi) MPhi(alloc, predSlot->type());
475
476 phi->addInlineInput(predSlot);
477
478 // Add append Phis in the block.
479 block->addPhi(phi);
480 block->setSlot(i, phi);
481 }
482 } else {
483 if (!block->ensureHasSlots(0)) {
484 return nullptr;
485 }
486 block->copySlots(pred);
487 }
488
489 if (!block->predecessors_.append(pred)) {
490 return nullptr;
491 }
492 }
493
494 return block;
495 }
496
497 // Create an empty and unreachable block which jumps to |header|. Used
498 // when the normal entry into a loop is removed (but the loop is still
499 // reachable due to OSR) to preserve the invariant that every loop
500 // header has two predecessors, which is needed for building the
501 // dominator tree. The new block is inserted immediately before the
502 // header, which preserves the graph ordering (post-order/RPO). These
503 // blocks will all be removed before lowering.
504 MBasicBlock* MBasicBlock::NewFakeLoopPredecessor(MIRGraph& graph,
505 MBasicBlock* header) {
506 MOZ_ASSERT(graph.osrBlock());
507
508 MBasicBlock* backedge = header->backedge();
509 MBasicBlock* fake = MBasicBlock::New(graph, header->info(), nullptr,
510 MBasicBlock::FAKE_LOOP_PRED);
511 if (!fake) {
512 return nullptr;
513 }
514
515 graph.insertBlockBefore(header, fake);
516 fake->setUnreachable();
517
518 // Create fake defs to use as inputs for any phis in |header|.
519 for (MPhiIterator iter(header->phisBegin()), end(header->phisEnd());
520 iter != end; ++iter) {
521 if (!graph.alloc().ensureBallast()) {
522 return nullptr;
523 }
524 MPhi* phi = *iter;
525 auto* fakeDef = MUnreachableResult::New(graph.alloc(), phi->type());
526 fake->add(fakeDef);
527 if (!phi->addInputSlow(fakeDef)) {
528 return nullptr;
529 }
530 }
531
532 fake->end(MGoto::New(graph.alloc(), header));
533
534 if (!header->addPredecessorWithoutPhis(fake)) {
535 return nullptr;
536 }
537
538 // The backedge is always the last predecessor, but we have added a
539 // new pred. Restore |backedge| as |header|'s loop backedge.
540 header->clearLoopHeader();
541 header->setLoopHeader(backedge);
542
543 return fake;
544 }
545
546 void MIRGraph::removeFakeLoopPredecessors() {
547 MOZ_ASSERT(osrBlock());
548 size_t id = 0;
549 for (ReversePostorderIterator it = rpoBegin(); it != rpoEnd();) {
550 MBasicBlock* block = *it++;
551 if (block->isFakeLoopPred()) {
552 MOZ_ASSERT(block->unreachable());
553 MBasicBlock* succ = block->getSingleSuccessor();
554 succ->removePredecessor(block);
555 removeBlock(block);
556 } else {
557 block->setId(id++);
558 }
559 }
560 #ifdef DEBUG
561 canBuildDominators_ = false;
562 #endif
563 }
564
565 MBasicBlock::MBasicBlock(MIRGraph& graph, const CompileInfo& info,
566 BytecodeSite* site, Kind kind)
567 : graph_(graph),
568 info_(info),
569 predecessors_(graph.alloc()),
570 stackPosition_(info_.firstStackSlot()),
571 id_(0),
572 domIndex_(0),
573 numDominated_(0),
574 lir_(nullptr),
575 callerResumePoint_(nullptr),
576 entryResumePoint_(nullptr),
577 outerResumePoint_(nullptr),
578 successorWithPhis_(nullptr),
579 positionInPhiSuccessor_(0),
580 loopDepth_(0),
581 kind_(kind),
582 mark_(false),
583 immediatelyDominated_(graph.alloc()),
584 immediateDominator_(nullptr),
585 trackedSite_(site) {
586 MOZ_ASSERT(trackedSite_, "trackedSite_ is non-nullptr");
587 }
588
589 bool MBasicBlock::init() { return slots_.init(graph_.alloc(), info_.nslots()); }
590
591 bool MBasicBlock::increaseSlots(size_t num) {
592 return slots_.growBy(graph_.alloc(), num);
593 }
594
595 bool MBasicBlock::ensureHasSlots(size_t num) {
596 size_t depth = stackDepth() + num;
597 if (depth > nslots()) {
598 if (!increaseSlots(depth - nslots())) {
599 return false;
600 }
601 }
602 return true;
603 }
604
605 void MBasicBlock::copySlots(MBasicBlock* from) {
606 MOZ_ASSERT(stackPosition_ <= from->stackPosition_);
607 MOZ_ASSERT(stackPosition_ <= nslots());
608
609 MDefinition** thisSlots = slots_.begin();
610 MDefinition** fromSlots = from->slots_.begin();
611 for (size_t i = 0, e = stackPosition_; i < e; ++i) {
612 thisSlots[i] = fromSlots[i];
613 }
614 }
615
616 bool MBasicBlock::inherit(TempAllocator& alloc, size_t stackDepth,
617 MBasicBlock* maybePred, uint32_t popped) {
618 MOZ_ASSERT_IF(maybePred, maybePred->stackDepth() == stackDepth);
619
620 MOZ_ASSERT(stackDepth >= popped);
621 stackDepth -= popped;
622 stackPosition_ = stackDepth;
623
624 if (maybePred && kind_ != PENDING_LOOP_HEADER) {
625 copySlots(maybePred);
626 }
627
628 MOZ_ASSERT(info_.nslots() >= stackPosition_);
629 MOZ_ASSERT(!entryResumePoint_);
630
631 // Propagate the caller resume point from the inherited block.
632 callerResumePoint_ = maybePred ? maybePred->callerResumePoint() : nullptr;
633
634 // Create a resume point using our initial stack state.
635 entryResumePoint_ =
636 new (alloc) MResumePoint(this, pc(), ResumeMode::ResumeAt);
637 if (!entryResumePoint_->init(alloc)) {
638 return false;
639 }
640
641 if (maybePred) {
642 if (!predecessors_.append(maybePred)) {
643 return false;
644 }
645
646 if (kind_ == PENDING_LOOP_HEADER) {
647 for (size_t i = 0; i < stackDepth; i++) {
648 MPhi* phi = MPhi::New(alloc.fallible());
649 if (!phi) {
650 return false;
651 }
652 phi->addInlineInput(maybePred->getSlot(i));
653 addPhi(phi);
654 setSlot(i, phi);
655 entryResumePoint()->initOperand(i, phi);
656 }
657 } else {
658 for (size_t i = 0; i < stackDepth; i++) {
659 entryResumePoint()->initOperand(i, getSlot(i));
660 }
661 }
662 } else {
663 /*
664 * Don't leave the operands uninitialized for the caller, as it may not
665 * initialize them later on.
666 */
667 for (size_t i = 0; i < stackDepth; i++) {
668 entryResumePoint()->clearOperand(i);
669 }
670 }
671
672 return true;
673 }
674
675 void MBasicBlock::inheritSlots(MBasicBlock* parent) {
676 stackPosition_ = parent->stackPosition_;
677 copySlots(parent);
678 }
679
680 bool MBasicBlock::initEntrySlots(TempAllocator& alloc) {
681 // Remove the previous resume point.
682 discardResumePoint(entryResumePoint_);
683
684 // Create a resume point using our initial stack state.
685 entryResumePoint_ =
686 MResumePoint::New(alloc, this, pc(), ResumeMode::ResumeAt);
687 if (!entryResumePoint_) {
688 return false;
689 }
690 return true;
691 }
692
693 MDefinition* MBasicBlock::environmentChain() {
694 return getSlot(info().environmentChainSlot());
695 }
696
697 MDefinition* MBasicBlock::argumentsObject() {
698 return getSlot(info().argsObjSlot());
699 }
700
701 void MBasicBlock::setEnvironmentChain(MDefinition* scopeObj) {
702 setSlot(info().environmentChainSlot(), scopeObj);
703 }
704
705 void MBasicBlock::setArgumentsObject(MDefinition* argsObj) {
706 setSlot(info().argsObjSlot(), argsObj);
707 }
708
709 void MBasicBlock::pick(int32_t depth) {
710 // pick takes a value and moves it to the top.
711 // pick(-2):
712 // A B C D E
713 // A B D C E [ swapAt(-2) ]
714 // A B D E C [ swapAt(-1) ]
715 for (; depth < 0; depth++) {
716 swapAt(depth);
717 }
718 }
719
720 void MBasicBlock::unpick(int32_t depth) {
721 // unpick takes the value on top of the stack and moves it under the depth-th
722 // element;
723 // unpick(-2):
724 // A B C D E
725 // A B C E D [ swapAt(-1) ]
726 // A B E C D [ swapAt(-2) ]
727 for (int32_t n = -1; n >= depth; n--) {
728 swapAt(n);
729 }
730 }
731
732 void MBasicBlock::swapAt(int32_t depth) {
733 uint32_t lhsDepth = stackPosition_ + depth - 1;
734 uint32_t rhsDepth = stackPosition_ + depth;
735
736 MDefinition* temp = slots_[lhsDepth];
737 slots_[lhsDepth] = slots_[rhsDepth];
738 slots_[rhsDepth] = temp;
739 }
740
741 void MBasicBlock::discardLastIns() { discard(lastIns()); }
742
743 MConstant* MBasicBlock::optimizedOutConstant(TempAllocator& alloc) {
744 // If the first instruction is a MConstant(MagicValue(JS_OPTIMIZED_OUT))
745 // then reuse it.
746 MInstruction* ins = *begin();
747 if (ins->type() == MIRType::MagicOptimizedOut) {
748 return ins->toConstant();
749 }
750
751 MConstant* constant = MConstant::New(alloc, MagicValue(JS_OPTIMIZED_OUT));
752 insertBefore(ins, constant);
753 return constant;
754 }
755
756 void MBasicBlock::moveBefore(MInstruction* at, MInstruction* ins) {
757 // Remove |ins| from the current block.
758 MOZ_ASSERT(ins->block() == this);
759 instructions_.remove(ins);
760
761 // Insert into new block, which may be distinct.
762 // Uses and operands are untouched.
763 ins->setInstructionBlock(at->block(), at->trackedSite());
764 at->block()->instructions_.insertBefore(at, ins);
765 }
766
767 MInstruction* MBasicBlock::safeInsertTop(MDefinition* ins, IgnoreTop ignore) {
768 MOZ_ASSERT(graph().osrBlock() != this,
769 "We are not supposed to add any instruction in OSR blocks.");
770
771 // Beta nodes and interrupt checks are required to be located at the
772 // beginnings of basic blocks, so we must insert new instructions after any
773 // such instructions.
774 MInstructionIterator insertIter =
775 !ins || ins->isPhi() ? begin() : begin(ins->toInstruction());
776 while (insertIter->isBeta() || insertIter->isInterruptCheck() ||
777 insertIter->isConstant() || insertIter->isParameter() ||
778 (!(ignore & IgnoreRecover) && insertIter->isRecoveredOnBailout())) {
779 insertIter++;
780 }
781
782 return *insertIter;
783 }
784
785 void MBasicBlock::discardResumePoint(
786 MResumePoint* rp, ReferencesType refType /* = RefType_Default */) {
787 if (refType & RefType_DiscardOperands) {
788 rp->releaseUses();
789 }
790 rp->setDiscarded();
791 removeResumePoint(rp);
792 }
793
794 void MBasicBlock::removeResumePoint(MResumePoint* rp) {
795 #ifdef DEBUG
796 MResumePointIterator iter = resumePointsBegin();
797 while (*iter != rp) {
798 // We should reach it before reaching the end.
799 MOZ_ASSERT(iter != resumePointsEnd());
800 iter++;
801 }
802 resumePoints_.removeAt(iter);
803 #endif
804 }
805
806 void MBasicBlock::prepareForDiscard(
807 MInstruction* ins, ReferencesType refType /* = RefType_Default */) {
808 // Only remove instructions from the same basic block. This is needed for
809 // correctly removing the resume point if any.
810 MOZ_ASSERT(ins->block() == this);
811
812 MResumePoint* rp = ins->resumePoint();
813 if ((refType & RefType_DiscardResumePoint) && rp) {
814 discardResumePoint(rp, refType);
815 }
816
817 // We need to assert that instructions have no uses after removing the their
818 // resume points operands as they could be captured by their own resume
819 // point.
820 MOZ_ASSERT_IF(refType & RefType_AssertNoUses, !ins->hasUses());
821
822 const uint32_t InstructionOperands =
823 RefType_DiscardOperands | RefType_DiscardInstruction;
824 if ((refType & InstructionOperands) == InstructionOperands) {
825 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
826 ins->releaseOperand(i);
827 }
828 }
829
830 ins->setDiscarded();
831 }
832
833 void MBasicBlock::discard(MInstruction* ins) {
834 prepareForDiscard(ins);
835 instructions_.remove(ins);
836 }
837
838 void MBasicBlock::discardIgnoreOperands(MInstruction* ins) {
839 #ifdef DEBUG
840 for (size_t i = 0, e = ins->numOperands(); i < e; i++) {
841 MOZ_ASSERT(!ins->hasOperand(i));
842 }
843 #endif
844
845 prepareForDiscard(ins, RefType_IgnoreOperands);
846 instructions_.remove(ins);
847 }
848
849 void MBasicBlock::discardAllInstructions() {
850 MInstructionIterator iter = begin();
851 discardAllInstructionsStartingAt(iter);
852 }
853
854 void MBasicBlock::discardAllInstructionsStartingAt(MInstructionIterator iter) {
855 while (iter != end()) {
856 // Discard operands and resume point operands and flag the instruction
857 // as discarded. Also we do not assert that we have no uses as blocks
858 // might be removed in reverse post order.
859 MInstruction* ins = *iter++;
860 prepareForDiscard(ins, RefType_DefaultNoAssert);
861 instructions_.remove(ins);
862 }
863 }
864
865 void MBasicBlock::discardAllPhis() {
866 for (MPhiIterator iter = phisBegin(); iter != phisEnd(); iter++) {
867 iter->removeAllOperands();
868 }
869
870 for (MBasicBlock** pred = predecessors_.begin(); pred != predecessors_.end();
871 pred++) {
872 (*pred)->clearSuccessorWithPhis();
873 }
874
875 phis_.clear();
876 }
877
878 void MBasicBlock::discardAllResumePoints(bool discardEntry) {
879 if (outerResumePoint_) {
880 clearOuterResumePoint();
881 }
882
883 if (discardEntry && entryResumePoint_) {
884 clearEntryResumePoint();
885 }
886
887 #ifdef DEBUG
888 if (!entryResumePoint()) {
889 MOZ_ASSERT(resumePointsEmpty());
890 } else {
891 MResumePointIterator iter(resumePointsBegin());
892 MOZ_ASSERT(iter != resumePointsEnd());
893 iter++;
894 MOZ_ASSERT(iter == resumePointsEnd());
895 }
896 #endif
897 }
898
899 void MBasicBlock::clear() {
900 discardAllInstructions();
901 discardAllResumePoints();
902 discardAllPhis();
903 }
904
905 void MBasicBlock::insertBefore(MInstruction* at, MInstruction* ins) {
906 MOZ_ASSERT(at->block() == this);
907 ins->setInstructionBlock(this, at->trackedSite());
908 graph().allocDefinitionId(ins);
909 instructions_.insertBefore(at, ins);
910 }
911
912 void MBasicBlock::insertAfter(MInstruction* at, MInstruction* ins) {
913 MOZ_ASSERT(at->block() == this);
914 ins->setInstructionBlock(this, at->trackedSite());
915 graph().allocDefinitionId(ins);
916 instructions_.insertAfter(at, ins);
917 }
918
919 void MBasicBlock::insertAtEnd(MInstruction* ins) {
920 if (hasLastIns()) {
921 insertBefore(lastIns(), ins);
922 } else {
923 add(ins);
924 }
925 }
926
927 void MBasicBlock::addPhi(MPhi* phi) {
928 phis_.pushBack(phi);
929 phi->setPhiBlock(this);
930 graph().allocDefinitionId(phi);
931 }
932
933 void MBasicBlock::discardPhi(MPhi* phi) {
934 MOZ_ASSERT(!phis_.empty());
935
936 phi->removeAllOperands();
937 phi->setDiscarded();
938
939 phis_.remove(phi);
940
941 if (phis_.empty()) {
942 for (MBasicBlock* pred : predecessors_) {
943 pred->clearSuccessorWithPhis();
944 }
945 }
946 }
947
948 MResumePoint* MBasicBlock::activeResumePoint(MInstruction* ins) {
949 for (MInstructionReverseIterator iter = rbegin(ins); iter != rend(); iter++) {
950 if (iter->resumePoint() && *iter != ins) {
951 return iter->resumePoint();
952 }
953 }
954
955 // If none, take the entry resume point.
956 return entryResumePoint();
957 }
958
959 void MBasicBlock::flagOperandsOfPrunedBranches(MInstruction* ins) {
960 MResumePoint* rp = activeResumePoint(ins);
961
962 // The only blocks which do not have any entryResumePoint in Ion, are the
963 // SplitEdge blocks. SplitEdge blocks only have a Goto instruction before
964 // Range Analysis phase. In adjustInputs, we are manipulating instructions
965 // which have a TypePolicy. So, as a Goto has no operand and no type
966 // policy, the entry resume point should exist.
967 MOZ_ASSERT(rp);
968
969 // Flag all operands as being potentially used.
970 while (rp) {
971 for (size_t i = 0, end = rp->numOperands(); i < end; i++) {
972 rp->getOperand(i)->setImplicitlyUsedUnchecked();
973 }
974 rp = rp->caller();
975 }
976 }
977
978 bool MBasicBlock::addPredecessor(TempAllocator& alloc, MBasicBlock* pred) {
979 return addPredecessorPopN(alloc, pred, 0);
980 }
981
982 bool MBasicBlock::addPredecessorPopN(TempAllocator& alloc, MBasicBlock* pred,
983 uint32_t popped) {
984 MOZ_ASSERT(pred);
985 MOZ_ASSERT(predecessors_.length() > 0);
986
987 // Predecessors must be finished, and at the correct stack depth.
988 MOZ_ASSERT(pred->hasLastIns());
989 MOZ_ASSERT(pred->stackPosition_ == stackPosition_ + popped);
990
991 for (uint32_t i = 0, e = stackPosition_; i < e; ++i) {
992 MDefinition* mine = getSlot(i);
993 MDefinition* other = pred->getSlot(i);
994
995 if (mine != other) {
996 MIRType phiType = mine->type();
997 if (phiType != other->type()) {
998 phiType = MIRType::Value;
999 }
1000
1001 // If the current instruction is a phi, and it was created in this
1002 // basic block, then we have already placed this phi and should
1003 // instead append to its operands.
1004 if (mine->isPhi() && mine->block() == this) {
1005 MOZ_ASSERT(predecessors_.length());
1006 MOZ_ASSERT(!mine->hasDefUses(),
1007 "should only change type of newly created phis");
1008 mine->setResultType(phiType);
1009 if (!mine->toPhi()->addInputSlow(other)) {
1010 return false;
1011 }
1012 } else {
1013 // Otherwise, create a new phi node.
1014 MPhi* phi = MPhi::New(alloc.fallible(), phiType);
1015 if (!phi) {
1016 return false;
1017 }
1018 addPhi(phi);
1019
1020 // Prime the phi for each predecessor, so input(x) comes from
1021 // predecessor(x).
1022 if (!phi->reserveLength(predecessors_.length() + 1)) {
1023 return false;
1024 }
1025
1026 for (size_t j = 0, numPreds = predecessors_.length(); j < numPreds;
1027 ++j) {
1028 MOZ_ASSERT(predecessors_[j]->getSlot(i) == mine);
1029 phi->addInput(mine);
1030 }
1031 phi->addInput(other);
1032
1033 setSlot(i, phi);
1034 if (entryResumePoint()) {
1035 entryResumePoint()->replaceOperand(i, phi);
1036 }
1037 }
1038 }
1039 }
1040
1041 return predecessors_.append(pred);
1042 }
1043
1044 bool MBasicBlock::addPredecessorSameInputsAs(MBasicBlock* pred,
1045 MBasicBlock* existingPred) {
1046 MOZ_ASSERT(pred);
1047 MOZ_ASSERT(predecessors_.length() > 0);
1048
1049 // Predecessors must be finished, and at the correct stack depth.
1050 MOZ_ASSERT(pred->hasLastIns());
1051 MOZ_ASSERT(!pred->successorWithPhis());
1052
1053 if (!phisEmpty()) {
1054 size_t existingPosition = indexForPredecessor(existingPred);
1055 for (MPhiIterator iter = phisBegin(); iter != phisEnd(); iter++) {
1056 if (!iter->addInputSlow(iter->getOperand(existingPosition))) {
1057 return false;
1058 }
1059 }
1060 }
1061
1062 if (!predecessors_.append(pred)) {
1063 return false;
1064 }
1065 return true;
1066 }
1067
1068 bool MBasicBlock::addPredecessorWithoutPhis(MBasicBlock* pred) {
1069 // Predecessors must be finished.
1070 MOZ_ASSERT(pred && pred->hasLastIns());
1071 return predecessors_.append(pred);
1072 }
1073
1074 bool MBasicBlock::addImmediatelyDominatedBlock(MBasicBlock* child) {
1075 return immediatelyDominated_.append(child);
1076 }
1077
1078 void MBasicBlock::removeImmediatelyDominatedBlock(MBasicBlock* child) {
1079 for (size_t i = 0;; ++i) {
1080 MOZ_ASSERT(i < immediatelyDominated_.length(),
1081 "Dominated block to remove not present");
1082 if (immediatelyDominated_[i] == child) {
1083 immediatelyDominated_[i] = immediatelyDominated_.back();
1084 immediatelyDominated_.popBack();
1085 return;
1086 }
1087 }
1088 }
1089
1090 bool MBasicBlock::setBackedge(MBasicBlock* pred) {
1091 // Predecessors must be finished, and at the correct stack depth.
1092 MOZ_ASSERT(hasLastIns());
1093 MOZ_ASSERT(pred->hasLastIns());
1094 MOZ_ASSERT(pred->stackDepth() == entryResumePoint()->stackDepth());
1095
1096 // We must be a pending loop header
1097 MOZ_ASSERT(kind_ == PENDING_LOOP_HEADER);
1098
1099 // Add exit definitions to each corresponding phi at the entry.
1100 if (!inheritPhisFromBackedge(pred)) {
1101 return false;
1102 }
1103
1104 // We are now a loop header proper
1105 kind_ = LOOP_HEADER;
1106
1107 return predecessors_.append(pred);
1108 }
1109
1110 bool MBasicBlock::setBackedgeWasm(MBasicBlock* pred, size_t paramCount) {
1111 // Predecessors must be finished, and at the correct stack depth.
1112 MOZ_ASSERT(hasLastIns());
1113 MOZ_ASSERT(pred->hasLastIns());
1114 MOZ_ASSERT(stackDepth() + paramCount == pred->stackDepth());
1115
1116 // We must be a pending loop header
1117 MOZ_ASSERT(kind_ == PENDING_LOOP_HEADER);
1118
1119 // Add exit definitions to each corresponding phi at the entry.
1120 // Note: Phis are inserted in the same order as the slots. (see
1121 // MBasicBlock::New)
1122 size_t slot = 0;
1123 for (MPhiIterator phi = phisBegin(); phi != phisEnd(); phi++, slot++) {
1124 MPhi* entryDef = *phi;
1125 MDefinition* exitDef = pred->getSlot(slot);
1126
1127 // Assert that we already placed phis for each slot.
1128 MOZ_ASSERT(entryDef->block() == this);
1129
1130 // Assert that the phi already has the correct type.
1131 MOZ_ASSERT(entryDef->type() == exitDef->type());
1132 MOZ_ASSERT(entryDef->type() != MIRType::Value);
1133
1134 if (entryDef == exitDef) {
1135 // If the exit def is the same as the entry def, make a redundant
1136 // phi. Since loop headers have exactly two incoming edges, we
1137 // know that that's just the first input.
1138 //
1139 // Note that we eliminate later rather than now, to avoid any
1140 // weirdness around pending continue edges which might still hold
1141 // onto phis.
1142 exitDef = entryDef->getOperand(0);
1143 }
1144
1145 // Phis always have room for 2 operands, so this can't fail.
1146 MOZ_ASSERT(phi->numOperands() == 1);
1147 entryDef->addInlineInput(exitDef);
1148
1149 // Two cases here: phis that correspond to locals, and phis that correspond
1150 // to loop parameters. Only phis for locals go in slots.
1151 if (slot < stackDepth()) {
1152 setSlot(slot, entryDef);
1153 }
1154 }
1155
1156 // We are now a loop header proper
1157 kind_ = LOOP_HEADER;
1158
1159 return predecessors_.append(pred);
1160 }
1161
1162 void MBasicBlock::clearLoopHeader() {
1163 MOZ_ASSERT(isLoopHeader());
1164 kind_ = NORMAL;
1165 }
1166
1167 void MBasicBlock::setLoopHeader(MBasicBlock* newBackedge) {
1168 MOZ_ASSERT(!isLoopHeader());
1169 kind_ = LOOP_HEADER;
1170
1171 size_t numPreds = numPredecessors();
1172 MOZ_ASSERT(numPreds != 0);
1173
1174 size_t lastIndex = numPreds - 1;
1175 size_t oldIndex = 0;
1176 for (;; ++oldIndex) {
1177 MOZ_ASSERT(oldIndex < numPreds);
1178 MBasicBlock* pred = getPredecessor(oldIndex);
1179 if (pred == newBackedge) {
1180 break;
1181 }
1182 }
1183
1184 // Set the loop backedge to be the last element in predecessors_.
1185 std::swap(predecessors_[oldIndex], predecessors_[lastIndex]);
1186
1187 // If we have phis, reorder their operands accordingly.
1188 if (!phisEmpty()) {
1189 getPredecessor(oldIndex)->setSuccessorWithPhis(this, oldIndex);
1190 getPredecessor(lastIndex)->setSuccessorWithPhis(this, lastIndex);
1191 for (MPhiIterator iter(phisBegin()), end(phisEnd()); iter != end; ++iter) {
1192 MPhi* phi = *iter;
1193 MDefinition* last = phi->getOperand(oldIndex);
1194 MDefinition* old = phi->getOperand(lastIndex);
1195 phi->replaceOperand(oldIndex, old);
1196 phi->replaceOperand(lastIndex, last);
1197 }
1198 }
1199
1200 MOZ_ASSERT(newBackedge->loopHeaderOfBackedge() == this);
1201 MOZ_ASSERT(backedge() == newBackedge);
1202 }
1203
1204 size_t MBasicBlock::getSuccessorIndex(MBasicBlock* block) const {
1205 MOZ_ASSERT(lastIns());
1206 for (size_t i = 0; i < numSuccessors(); i++) {
1207 if (getSuccessor(i) == block) {
1208 return i;
1209 }
1210 }
1211 MOZ_CRASH("Invalid successor");
1212 }
1213
1214 size_t MBasicBlock::getPredecessorIndex(MBasicBlock* block) const {
1215 for (size_t i = 0, e = numPredecessors(); i < e; ++i) {
1216 if (getPredecessor(i) == block) {
1217 return i;
1218 }
1219 }
1220 MOZ_CRASH("Invalid predecessor");
1221 }
1222
1223 void MBasicBlock::replaceSuccessor(size_t pos, MBasicBlock* split) {
1224 MOZ_ASSERT(lastIns());
1225
1226 // Note, during split-critical-edges, successors-with-phis is not yet set.
1227 // During PAA, this case is handled before we enter.
1228 MOZ_ASSERT_IF(successorWithPhis_, successorWithPhis_ != getSuccessor(pos));
1229
1230 lastIns()->replaceSuccessor(pos, split);
1231 }
1232
1233 void MBasicBlock::replacePredecessor(MBasicBlock* old, MBasicBlock* split) {
1234 for (size_t i = 0; i < numPredecessors(); i++) {
1235 if (getPredecessor(i) == old) {
1236 predecessors_[i] = split;
1237
1238 #ifdef DEBUG
1239 // The same block should not appear twice in the predecessor list.
1240 for (size_t j = i; j < numPredecessors(); j++) {
1241 MOZ_ASSERT(predecessors_[j] != old);
1242 }
1243 #endif
1244
1245 return;
1246 }
1247 }
1248
1249 MOZ_CRASH("predecessor was not found");
1250 }
1251
1252 void MBasicBlock::clearDominatorInfo() {
1253 setImmediateDominator(nullptr);
1254 immediatelyDominated_.clear();
1255 numDominated_ = 0;
1256 }
1257
1258 void MBasicBlock::removePredecessorWithoutPhiOperands(MBasicBlock* pred,
1259 size_t predIndex) {
1260 // If we're removing the last backedge, this is no longer a loop.
1261 if (isLoopHeader() && hasUniqueBackedge() && backedge() == pred) {
1262 clearLoopHeader();
1263 }
1264
1265 // Adjust phis. Note that this can leave redundant phis behind.
1266 // Don't adjust successorWithPhis() if we haven't constructed this
1267 // information yet.
1268 if (pred->successorWithPhis()) {
1269 MOZ_ASSERT(pred->positionInPhiSuccessor() == predIndex);
1270 pred->clearSuccessorWithPhis();
1271 for (size_t j = predIndex + 1; j < numPredecessors(); j++) {
1272 getPredecessor(j)->setSuccessorWithPhis(this, j - 1);
1273 }
1274 }
1275
1276 // Remove from pred list.
1277 predecessors_.erase(predecessors_.begin() + predIndex);
1278 }
1279
1280 void MBasicBlock::removePredecessor(MBasicBlock* pred) {
1281 size_t predIndex = getPredecessorIndex(pred);
1282
1283 // Remove the phi operands.
1284 for (MPhiIterator iter(phisBegin()), end(phisEnd()); iter != end; ++iter) {
1285 iter->removeOperand(predIndex);
1286 }
1287
1288 // Now we can call the underlying function, which expects that phi
1289 // operands have been removed.
1290 removePredecessorWithoutPhiOperands(pred, predIndex);
1291 }
1292
1293 bool MBasicBlock::inheritPhisFromBackedge(MBasicBlock* backedge) {
1294 // We must be a pending loop header
1295 MOZ_ASSERT(kind_ == PENDING_LOOP_HEADER);
1296
1297 size_t stackDepth = entryResumePoint()->stackDepth();
1298 for (size_t slot = 0; slot < stackDepth; slot++) {
1299 // Get the value stack-slot of the back edge.
1300 MDefinition* exitDef = backedge->getSlot(slot);
1301
1302 // Get the value of the loop header.
1303 MDefinition* loopDef = entryResumePoint()->getOperand(slot);
1304 if (loopDef->block() != this) {
1305 // If we are finishing a pending loop header, then we need to ensure
1306 // that all operands are phis. This is usualy the case, except for
1307 // object/arrays build with generators, in which case we share the
1308 // same allocations across all blocks.
1309 MOZ_ASSERT(loopDef->block()->id() < id());
1310 MOZ_ASSERT(loopDef == exitDef);
1311 continue;
1312 }
1313
1314 // Phis are allocated by NewPendingLoopHeader.
1315 MPhi* entryDef = loopDef->toPhi();
1316 MOZ_ASSERT(entryDef->block() == this);
1317
1318 if (entryDef == exitDef) {
1319 // If the exit def is the same as the entry def, make a redundant
1320 // phi. Since loop headers have exactly two incoming edges, we
1321 // know that that's just the first input.
1322 //
1323 // Note that we eliminate later rather than now, to avoid any
1324 // weirdness around pending continue edges which might still hold
1325 // onto phis.
1326 exitDef = entryDef->getOperand(0);
1327 }
1328
1329 if (!entryDef->addInputSlow(exitDef)) {
1330 return false;
1331 }
1332 }
1333
1334 return true;
1335 }
1336
1337 MTest* MBasicBlock::immediateDominatorBranch(BranchDirection* pdirection) {
1338 *pdirection = FALSE_BRANCH;
1339
1340 if (numPredecessors() != 1) {
1341 return nullptr;
1342 }
1343
1344 MBasicBlock* dom = immediateDominator();
1345 if (dom != getPredecessor(0)) {
1346 return nullptr;
1347 }
1348
1349 // Look for a trailing MTest branching to this block.
1350 MInstruction* ins = dom->lastIns();
1351 if (ins->isTest()) {
1352 MTest* test = ins->toTest();
1353
1354 MOZ_ASSERT(test->ifTrue() == this || test->ifFalse() == this);
1355 if (test->ifTrue() == this && test->ifFalse() == this) {
1356 return nullptr;
1357 }
1358
1359 *pdirection = (test->ifTrue() == this) ? TRUE_BRANCH : FALSE_BRANCH;
1360 return test;
1361 }
1362
1363 return nullptr;
1364 }
1365
1366 void MBasicBlock::dumpStack(GenericPrinter& out) {
1367 #ifdef DEBUG
1368 out.printf(" %-3s %-16s %-6s %-10s\n", "#", "name", "copyOf", "first/next");
1369 out.printf("-------------------------------------------\n");
1370 for (uint32_t i = 0; i < stackPosition_; i++) {
1371 out.printf(" %-3u", i);
1372 out.printf(" %-16p\n", (void*)slots_[i]);
1373 }
1374 #endif
1375 }
1376
1377 void MBasicBlock::dumpStack() {
1378 Fprinter out(stderr);
1379 dumpStack(out);
1380 out.finish();
1381 }
1382
1383 void MIRGraph::dump(GenericPrinter& out) {
1384 #ifdef JS_JITSPEW
1385 for (MBasicBlockIterator iter(begin()); iter != end(); iter++) {
1386 iter->dump(out);
1387 out.printf("\n");
1388 }
1389 #endif
1390 }
1391
1392 void MIRGraph::dump() {
1393 Fprinter out(stderr);
1394 dump(out);
1395 out.finish();
1396 }
1397
1398 void MBasicBlock::dump(GenericPrinter& out) {
1399 #ifdef JS_JITSPEW
1400 out.printf("block%u:%s%s%s\n", id(), isLoopHeader() ? " (loop header)" : "",
1401 unreachable() ? " (unreachable)" : "",
1402 isMarked() ? " (marked)" : "");
1403 if (MResumePoint* resume = entryResumePoint()) {
1404 resume->dump(out);
1405 }
1406 for (MPhiIterator iter(phisBegin()); iter != phisEnd(); iter++) {
1407 iter->dump(out);
1408 }
1409 for (MInstructionIterator iter(begin()); iter != end(); iter++) {
1410 iter->dump(out);
1411 }
1412 #endif
1413 }
1414
1415 void MBasicBlock::dump() {
1416 Fprinter out(stderr);
1417 dump(out);
1418 out.finish();
1419 }