Debian Codegraph @

js/src/jit/UnrollLoops.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/UnrollLoops.h"
8
9 #include "mozilla/Maybe.h"
10
11 #include <stdio.h>
12
13 #include "jit/DominatorTree.h"
14 #include "jit/IonAnalysis.h"
15 #include "jit/MIRGraph.h"
16
17 namespace js {
18 namespace jit {
19
20 // [SMDOC] Loop unroller implementation summary
21 //
22 // This is a simple loop unroller, intended (initially at least) to handle only
23 // wasm loops, with the aims of amortizing the per-iteration interrupt check
24 // cost, and of lifting an initial iteration outside the loop so as to
25 // facilitate subsequent optimizations. Unrolling and peeling can be selected
26 // independently, so the available choices are: peeling only, unrolling only,
27 // or both peeling and unrolling.
28 //
29 // The flow of control (for a single function) is roughly:
30 //
31 // (0) The top level function is UnrollLoops.
32 //
33 // (1) UnrollLoops calls FindInitialCandidates to identify "initial candidate"
34 // loops in the function. These are innermost loops. Non-innermost loops
35 // cannot be unrolled/peeled.
36 //
37 // (2) FindInitialCandidates counts blocks and value defs in each initial
38 // candidate, creating an initial score which increases with loop size.
39 //
40 // (3) Back in UnrollLoops, initial candidates whose initial score does not
41 // exceed InitialCandidateThreshold proceed to the next stage in the
42 // process: they are passed to function AnalyzeLoop.
43 //
44 // (4) AnalyzeLoop, as called from UnrollLoops, checks many structural
45 // invariants on each loop, since the core unroll machinery depends on
46 // those invariants. It also makes a final determination for each loop, as
47 // to whether the loop should be peeled, unrolled, peeled and unrolled, or
48 // be left unchanged. In the latter case it returns one of a few reasons
49 // why the loop is to be left unchanged.
50 //
51 // (5) AnalyzeLoop also identifies, for a loop, its basic blocks, the set of
52 // blocks it jumps to when exiting, and the values defined in the loop
53 // which are used afterwards.
54 //
55 // (6) Back in UnrollLoops, we now know which loops are suitable for
56 // processing, what processing is to happen to them, and what their blocks
57 // are. This is used to construct and initialize a `struct UnrollState`
58 // for each loop candidate that made it this far.
59 //
60 // (7) Before a loop can be unrolled, single-argument phi nodes need to be
61 // added to its exit target blocks, to "catch" values defined in the loop
62 // that are used afterwards. This is a limited conversion into what is
63 // called "Loop-Closed SSA form" in the GCC/Clang literature. UnrollLoops
64 // calls AddClosingPhisForLoop to get this done. This is done for all
65 // loops before proceeding further.
66 //
67 // (8) Finally, we get to the core of the unrolling: the loops are handed to
68 // UnrollAndOrPeelLoop in turn. This has extensive comments; a summary
69 // of the actions is:
70 // - blocks, phis and normal instructions are cloned so as to create
71 // the extra loop body copies.
72 // - the new blocks are added to the loop's UnrollState::blockTable.
73 // - during cloning, value uses are remapped to the correct iteration
74 // using an MDefinitionRemapper.
75 // - also during cloning, the new values are parked in a ValueTable for
76 // later reference.
77 // - control flow edges and predecessor vectors in the new and original
78 // block sets are fixed up, using RemapEdgeSource and
79 // RemapEdgeDestination.
80 // - header block phi nodes are fixed up in both the body copies
81 // and the original.
82 // - new exiting blocks are created, so as to maintain the invariant
83 // that there are no critical edges.
84 // - for exit target blocks, both their predecessor arrays and phi nodes
85 // (as installed by AddClosingPhisForLoop) are augmented to handle
86 // the new exit edges.
87 // - Wasm interrupt checks in all but the last body copy are nop'd out.
88 // - If peeling is required, the back edge of the unrolled loop is changed
89 // so it points at the second body copy, not the first (the original).
90 // - Finally, the new blocks are installed in the MIRGraph.
91 //
92 // (9) Back in UnrollLoops, once all loops have been processed,
93 // some function-level fixups are done:
94 // - the blocks are renumbered
95 // - the dominator tree is rebuilt
96 //
97 // UnrollLoops now returns to OptimizeMIR, handing back an indication of how
98 // many loops were processed. OptimizeMIR will tidy up the resulting function
99 // by re-running GVN, empty block removal, and possibly other transformations.
100
101 // The core logic in UnrollAndOrPeelLoop is heavily commented but still
102 // nearly incomprehensible. For debugging and modification it is helpful to
103 // use the Dump{Block,Value}Table* routines provided.
104
105 // The logic below functions without reference to block IDs or value IDs.
106
107 // Much of the logic uses simple scans over vectors (classes BlockVector,
108 // Matrix, SimpleSet, MDefinitionRemapper) rather than more complex data
109 // structures. The loops we process are small, which keeps the costs of these
110 // scans low. The InitialCandidateThreshold value used limits most processed
111 // loops to about 6 blocks and 30-ish value definitions.
112
113 // The idiom `blockTable.rowContains(0, block)` in the logic below means
114 // "is `block` in the original loop?"
115
116 // =====================================================================
117 //
118 // Tunables
119
120 // As a crude first pass for filtering out unsuitable candidates, top level
121 // function UnrollLoops calculates a size-related value for each candidate,
122 // with lower values indicating more desirability for unrolling/peeling.
123 // Candidates with a score above this value will not be unrolled. Reducing the
124 // value therefore reduces the aggressiveness of the unroller.
125 const float InitialCandidateThreshold = 100.0;
126
127 // On further analysis of candidates that pass the above test, we have more
128 // detailed limitations on size. The specified action will not be taken for a
129 // loop if the associated metric exceeds the constants here. Hence increasing
130 // these values increases the aggressiveness of the unroller, but only to point
131 // where the `InitialCandidateThreshold` would have cut it off.
132
133 // Loops with more than this number of basic blocks or values (phis and normal
134 // definitions) will be excluded from peeling. The actual numbers are pretty
135 // much pulled out of a hat.
136 const size_t MaxBlocksForPeel = 12;
137 const size_t MaxValuesForPeel = 150;
138
139 // Loops with more than this number of blocks or values will be excluded from
140 // unrolling. Unrolling adds multiple copies of the loop body, whereas peeling
141 // adds only one, so it seems prudent to make unrolling less aggressive than
142 // peeling.
143 const size_t MaxBlocksForUnroll = 10;
144 const size_t MaxValuesForUnroll = 100;
145
146 // =====================================================================
147 //
148 // BlockVector, a vector of MBasicBlock*s.
149
150 using BlockVector = mozilla::Vector<MBasicBlock*, 32, SystemAllocPolicy>;
151
152 static bool BlockVectorContains(const BlockVector& vec,
153 const MBasicBlock* block) {
154 for (const MBasicBlock* b : vec) {
155 if (b == block) {
156 return true;
157 }
158 }
159 return false;
160 }
161
162 // =====================================================================
163 //
164 // Matrix, a 2-D array of pointers
165
166 template <typename T, int N, typename AP>
167 class Matrix {
168 static_assert(std::is_pointer<T>::value);
169 mozilla::Vector<T, N, AP> vec_;
170 size_t size1_ = 0;
171 size_t size2_ = 0;
172 inline size_t index(size_t ix1, size_t ix2) const {
173 MOZ_ASSERT(ix1 < size1_ && ix2 < size2_);
174 return ix1 * size2_ + ix2;
175 }
176
177 public:
178 [[nodiscard]] bool init(size_t size1, size_t size2) {
179 // Not already init'd.
180 MOZ_ASSERT(size1_ == 0 && size2_ == 0 && vec_.empty());
181 // This would be pointless.
182 MOZ_ASSERT(size1 > 0 && size2 > 0);
183 // So we can safely multiply them. InitialCandidateThreshold ensures we'll
184 // never have to process a loop anywhere near this big.
185 MOZ_RELEASE_ASSERT(size1 <= 1000 && size2 <= 1000);
186 size1_ = size1;
187 size2_ = size2;
188 if (!vec_.resize(size1 * size2)) {
189 return false;
190 }
191 // Resizing also zero-initialises all the `vec_` entries.
192 return true;
193 }
194
195 inline size_t size1() const {
196 MOZ_ASSERT(size1_ * size2_ == vec_.length());
197 return size1_;
198 }
199 inline size_t size2() const {
200 MOZ_ASSERT(size1_ * size2_ == vec_.length());
201 return size2_;
202 }
203 inline T get(size_t ix1, size_t ix2) const { return vec_[index(ix1, ix2)]; }
204 inline void set(size_t ix1, size_t ix2, T value) {
205 vec_[index(ix1, ix2)] = value;
206 }
207
208 // Does the matrix slice `[ix1, ..]` contain `value`?
209 inline bool rowContains(size_t ix1, const T value) const {
210 return findInRow(ix1, value).isSome();
211 }
212
213 // Find the column number (ix2) such that entry `[ix1, ix2] == value`.
214 mozilla::Maybe<size_t> findInRow(size_t ix1, const T value) const {
215 for (size_t ix2 = 0; ix2 < size2_; ix2++) {
216 if (get(ix1, ix2) == value) {
217 return mozilla::Some(ix2);
218 }
219 }
220 return mozilla::Nothing();
221 }
222 };
223
224 using BlockTable = Matrix<MBasicBlock*, 32, SystemAllocPolicy>;
225 using ValueTable = Matrix<MDefinition*, 128, SystemAllocPolicy>;
226
227 // =====================================================================
228 //
229 // Debug printing
230
231 #ifdef JS_JITSPEW
232
233 // For debugging, dump specific rows (loop body copies) of a block table.
234 static void DumpBlockTableRows(const BlockTable& table, int32_t firstCix,
235 int32_t lastCix, const char* tag) {
236 Fprinter& printer(JitSpewPrinter());
237 printer.printf("<<<< %s\n", tag);
238 for (int32_t cix = firstCix; cix <= lastCix; cix++) {
239 if (cix == 0) {
240 printer.printf(" -------- Original --------\n");
241 } else {
242 printer.printf(" -------- Copy %u --------\n", cix);
243 }
244 for (uint32_t bix = 0; bix < table.size2(); bix++) {
245 DumpMIRBlock(printer, table.get(uint32_t(cix), bix),
246 /*showHashedPointers=*/true);
247 }
248 }
249 printer.printf(">>>>\n");
250 }
251
252 // Dump an entire block table.
253 static void DumpBlockTable(const BlockTable& table, const char* tag) {
254 DumpBlockTableRows(table, 0, int32_t(table.size1()) - 1, tag);
255 }
256
257 // Dump just the original loop body in a block table.
258 static void DumpBlockTableRowZero(const BlockTable& table, const char* tag) {
259 DumpBlockTableRows(table, 0, 0, tag);
260 }
261
262 // Dump a value table.
263 static void DumpValueTable(const ValueTable& table, const char* tag) {
264 Fprinter& printer(JitSpewPrinter());
265 printer.printf("<<<< %s\n", tag);
266 for (uint32_t cix = 0; cix < table.size1(); cix++) {
267 if (cix == 0) {
268 printer.printf(" -------- Original --------\n");
269 } else {
270 printer.printf(" -------- Copy %u --------\n", cix);
271 }
272 for (uint32_t vix = 0; vix < table.size2(); vix++) {
273 printer.printf(" ");
274 DumpMIRDefinition(printer, table.get(cix, vix),
275 /*showHashedPointers=*/true);
276 printer.printf("\n");
277 }
278 }
279 printer.printf(">>>>\n");
280 }
281
282 #endif // JS_JITSPEW
283
284 // =====================================================================
285 //
286 // SimpleSet, a set of pointers
287
288 template <typename T, int N, typename AP>
289 class SimpleSet {
290 static_assert(std::is_pointer<T>::value);
291 mozilla::Vector<T, N, AP> vec_;
292
293 public:
294 [[nodiscard]] bool copy(const SimpleSet<T, N, AP>& other) {
295 vec_.clear();
296 for (T elem : other.vec_) {
297 if (!vec_.append(elem)) {
298 return false;
299 }
300 }
301 return true;
302 }
303 bool contains(T t) const {
304 for (auto* existing : vec_) {
305 if (existing == t) {
306 return true;
307 }
308 }
309 return false;
310 }
311 [[nodiscard]] bool add(T t) {
312 MOZ_ASSERT(t);
313 return contains(t) ? true : vec_.append(t);
314 }
315 bool empty() const { return vec_.empty(); }
316 size_t size() const { return vec_.length(); }
317 T get(size_t ix) const { return vec_[ix]; }
318 };
319
320 using BlockSet = SimpleSet<MBasicBlock*, 8, SystemAllocPolicy>;
321 using ValueSet = SimpleSet<MDefinition*, 64, SystemAllocPolicy>;
322
323 // =====================================================================
324 //
325 // MDefinitionRemapper, a very simple MDefinition*-remapping class.
326
327 class MDefinitionRemapper {
328 using Pair = std::pair<MDefinition*, MDefinition*>;
329 mozilla::Vector<Pair, 32, SystemAllocPolicy> pairs;
330
331 public:
332 MDefinitionRemapper() {}
333 // Register `original` as a key in the mapper, and map it to itself.
334 [[nodiscard]] bool enregister(MDefinition* original) {
335 MOZ_ASSERT(original);
336 for (auto& p : pairs) {
337 (void)p;
338 MOZ_ASSERT(p.first != original); // not mapped at all
339 MOZ_ASSERT(p.second == p.first); // no `update` calls happened yet
340 }
341 return pairs.append(std::pair(original, original));
342 }
343 // Look up the binding for `original` in the mapper.
344 MDefinition* lookup(const MDefinition* original) const {
345 MOZ_ASSERT(original);
346 MDefinition* res = nullptr;
347 for (auto& p : pairs) {
348 if (p.first == original) {
349 res = p.second;
350 break;
351 }
352 }
353 return res;
354 }
355 // Update the mapper so as to map `original` to `replacement`. `original`
356 // must have previously been registered with ::enregister.
357 void update(const MDefinition* original, MDefinition* replacement) {
358 MOZ_ASSERT(original && replacement);
359 MOZ_ASSERT(original != replacement);
360 for (auto& p : pairs) {
361 if (p.first == original) {
362 p.second = replacement;
363 return;
364 }
365 }
366 MOZ_CRASH(); // asked to update a non-existent key
367 }
368 };
369
370 // =====================================================================
371 //
372 // MakeReplacementInstruction / MakeReplacementPhi
373
374 // Make a cloned version of `ins`, but with its arguments remapped by `mapper`.
375 static MInstruction* MakeReplacementInstruction(
376 TempAllocator& alloc, const MDefinitionRemapper& mapper,
377 const MInstruction* ins) {
378 MDefinitionVector inputs(alloc);
379 for (size_t i = 0; i < ins->numOperands(); i++) {
380 MDefinition* old = ins->getOperand(i);
381 MDefinition* replacement = mapper.lookup(old);
382 if (!replacement) {
383 // The mapper didn't map it, which means that `old` is a value that's not
384 // defined in the loop body. So leave it unchanged.
385 replacement = old;
386 }
387 if (!inputs.append(replacement)) {
388 return nullptr;
389 }
390 }
391 return ins->clone(alloc, inputs);
392 }
393
394 // Make a cloned version of `phi`, but with its arguments remapped by `mapper`.
395 static MPhi* MakeReplacementPhi(TempAllocator& alloc,
396 const MDefinitionRemapper& mapper,
397 const MPhi* phi) {
398 MDefinitionVector inputs(alloc);
399 for (size_t i = 0; i < phi->numOperands(); i++) {
400 MDefinition* old = phi->getOperand(i);
401 MDefinition* replacement = mapper.lookup(old);
402 if (!replacement) {
403 replacement = old;
404 }
405 if (!inputs.append(replacement)) {
406 return nullptr;
407 }
408 }
409 return phi->clone(alloc, inputs);
410 }
411
412 // =====================================================================
413 //
414 // UnrollState
415
416 enum class UnrollMode { Peel, Unroll, PeelAndUnroll };
417
418 struct UnrollState {
419 // What should happen to the specified loop.
420 const UnrollMode mode;
421
422 // The central data structure: BlockTable, a 2-D array of block pointers.
423 //
424 // To create copies of the original loop body, we need to copy the original
425 // blocks and fix up the inter-block edges and phis appropriately. This
426 // edge-fixup is done without reference to the block IDs; instead it is done
427 // purely on the basis of the MBasicBlock* values themselves. Hence it does
428 // not make any assumption about the ordering of the block ID values.
429 //
430 // The block copies are managed using a 2-D array of block pointers, via
431 // which we can uniformly access the original and new blocks. The array is
432 // indexed by `(copy index, block index in copy)`. Both indices are
433 // zero-based. The sequencing of blocks implied by "block index in copy" is
434 // the sequence produced by the RPO iterator as it enumerates the blocks in
435 // the loop.
436 //
437 // Per the above, note that "block index" (`bix` in the code) is a zero-based
438 // index into the RPO sequence of blocks in the loop and is not to be
439 // confused with the pre-existing concept of "block ID".
440 //
441 // Note also that the required unroll factor (including that for the peeled
442 // copy, if any) is equal to the first dimension of `blockTable`, and the
443 // number of blocks in the original loop is implied by the second dimension
444 // of `blockTable`. Hence neither value needs to be stored explicitly.
445 BlockTable blockTable;
446
447 // The set of blocks arrived at (directly) by exiting branches in the
448 // original loop. FIXME: try to make const
449 /*const*/ BlockSet exitTargetBlocks;
450
451 // The set of values (MDefinition*s) defined in the original loop and used
452 // afterwards.
453 /*const*/ ValueSet exitingValues;
454
455 explicit UnrollState(UnrollMode mode) : mode(mode) {}
456
457 bool doPeeling() const {
458 return mode == UnrollMode::Peel || mode == UnrollMode::PeelAndUnroll;
459 }
460 bool doUnrolling() const {
461 return mode == UnrollMode::Unroll || mode == UnrollMode::PeelAndUnroll;
462 }
463 };
464
465 // =====================================================================
466 //
467 // AnalyzeLoop and AnalysisResult
468
469 // Summarises the result of a detailed analysis of a candidate loop, producing
470 // either a recommendation about what to do with it, or a reason why it should
471 // be left unchanged.
472 enum class AnalysisResult {
473 // OOM during analysis
474 OOM,
475
476 // The chosen transformation for the loop ..
477 Peel,
478 Unroll,
479 PeelAndUnroll,
480
481 // .. or .. reasons why the loop can't be unrolled:
482 //
483 // The unrolling/peeling machinery below relies on the fact that MIR loops
484 // are heavily constrained in their structure. AnalyzeLoop checks the
485 // relevant invariants carefully, with this value indicating non-compliance.
486 // Top level function UnrollLoops will MOZ_CRASH if this happens.
487 BadInvariants,
488 // Cannot be handled by the unroller: if the loop defines value(s) that are
489 // used after the loop, then we can only handle the case where it has one
490 // exiting branch.
491 TooComplex,
492 // The loop contains MIR nodes that are uncloneable
493 Uncloneable,
494 // The loop has too many blocks or values
495 TooLarge,
496 // The loop is otherwise unsuitable:
497 // * contains a call or table switch
498 // * is an infinite loop
499 Unsuitable
500 };
501
502 #ifdef JS_JITSPEW
503 static const char* Name_of_AnalysisResult(AnalysisResult res) {
504 switch (res) {
505 case AnalysisResult::OOM:
506 return "OOM";
507 case AnalysisResult::Peel:
508 return "Peel";
509 case AnalysisResult::Unroll:
510 return "Unroll";
511 case AnalysisResult::PeelAndUnroll:
512 return "PeelAndUnroll";
513 case AnalysisResult::BadInvariants:
514 return "BadInvariants";
515 case AnalysisResult::TooComplex:
516 return "TooComplex";
517 case AnalysisResult::Uncloneable:
518 return "Uncloneable";
519 case AnalysisResult::TooLarge:
520 return "TooLarge";
521 case AnalysisResult::Unsuitable:
522 return "Unsuitable";
523 default:
524 MOZ_CRASH();
525 }
526 }
527 #endif
528
529 // Examine the original loop in `originalBlocks` for unrolling suitability,
530 // and, if acceptable, collect auxiliary information:
531 //
532 // * the set of blocks that are direct exit targets for the loop
533 // * the set of values defined in the loop AND used afterwards
534
535 static AnalysisResult AnalyzeLoop(const BlockVector& originalBlocks,
536 BlockSet* exitTargetBlocks,
537 ValueSet* exitingValues) {
538 MOZ_ASSERT(exitTargetBlocks->empty());
539 MOZ_ASSERT(exitingValues->empty());
540
541 // ==== BEGIN check invariants on the loop structure ====
542
543 // The unroller/peeler below depends on these invariants.
544 // In summary (where nBlocks == originalBlocks.length()):
545 //
546 // * At least 2 blocks in original loop (1 for header phis + possible insns,
547 // >= 0 for "ordinary" blocks, 1 for the backedge jump)
548 //
549 // * originalBlocks[0] is a loop header
550 // * originalBlocks[nBlocks-1] == originalBlocks[0]->backedge
551 //
552 // * all blocks in original loop have at least one insn
553 // * all blocks in original loop have a control insn as their last insn
554 // * all blocks in original loop have non-control insns for all other insns
555 // * all blocks in original loop have loop depth > 0
556 //
557 // * none of originalBlocks[0 .. nBlocks-2] (inclusive) jumps to header
558 //
559 // * originalBlocks[nBlocks-1] jumps unconditionally to header
560 //
561 // * original header node has 2 preds
562 // * original header node pred[0] is from outside the loop, and
563 // pred[1] is from inside the loop.
564 //
565 // * original header node phis all have 2 args
566 // * original header node phis have arg[0] from outside the loop
567 // (because pred[0] is the loop-entry edge)
568 // * original header node phis have their arg[1] as
569 // -- (almost always) defined in the loop -- loop carried variable
570 // -- (very occasionally) defined outside the loop -- in which case
571 // this is just a vanilla phi, nothing to do with the loop.
572 // Hence there is nothing to check for phi arg[1]s, but leaving this
573 // here for clarity.
574 //
575 // Aside: how can a loop head phi not depend on a value defined within the
576 // loop? Consider the strange-but-valid case
577 //
578 // x = 5 ; loop { .. x = 7; .. }
579 //
580 // The loop head phi will merge the initial value (5) and the loop-defined
581 // value in the usual way. Imagine now the loop is LICMd; the loop defined
582 // value `x = 7;` is observed to be invariant and so is lifted out of the
583 // loop. Hence the loop head phi will have both args defined outside the
584 // loop.
585
586 // * At least 2 blocks in original loop (1 for header phis + possible insns,
587 // >= 0 for "ordinary" blocks, 1 for the backedge jump)
588 const size_t numBlocksInOriginal = originalBlocks.length();
589 if (numBlocksInOriginal < 2) {
590 return AnalysisResult::BadInvariants;
591 }
592
593 // * originalBlocks[0] is a loop header
594 // * originalBlocks[nBlocks-1] == originalBlocks[0]->backedge
595 {
596 MBasicBlock* header = originalBlocks[0];
597 if (!header->isLoopHeader() ||
598 header->backedge() != originalBlocks[numBlocksInOriginal - 1]) {
599 return AnalysisResult::BadInvariants;
600 }
601 }
602
603 // * all blocks in original loop have at least one insn
604 // * all blocks in original loop have a control insn as their last insn
605 // * all blocks in original loop have non-control insns for all other insns
606 // * all blocks in original loop have loop depth > 0
607 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
608 MBasicBlock* block = originalBlocks[bix];
609 // This is a bit lame, but ..
610 size_t numInsns = 0;
611 for (MInstructionIterator insnIter(block->begin());
612 insnIter != block->end(); insnIter++) {
613 numInsns++;
614 }
615 if (numInsns < 1) {
616 return AnalysisResult::BadInvariants;
617 }
618 bool ok = true;
619 size_t curInsn = 0;
620 for (MInstructionIterator insnIter(block->begin());
621 insnIter != block->end(); insnIter++) {
622 MInstruction* insn = *insnIter;
623 if (curInsn == numInsns - 1) {
624 ok = ok && insn->isControlInstruction();
625 } else {
626 ok = ok && !insn->isControlInstruction();
627 }
628 curInsn++;
629 }
630 MOZ_ASSERT(curInsn == numInsns); // internal logic
631 ok = ok && block->loopDepth() > 0;
632 if (!ok) {
633 return AnalysisResult::BadInvariants;
634 }
635 }
636
637 // * none of originalBlocks[0 .. nBlocks-2] (inclusive) jumps to header
638 {
639 MBasicBlock* header = originalBlocks[0];
640 for (uint32_t bix = 0; bix < numBlocksInOriginal - 1; bix++) {
641 MBasicBlock* block = originalBlocks[bix];
642 if (!block->hasLastIns()) {
643 return AnalysisResult::BadInvariants;
644 }
645 MControlInstruction* lastIns = block->lastIns();
646 for (size_t i = 0; i < lastIns->numSuccessors(); i++) {
647 if (lastIns->getSuccessor(i) == header) {
648 return AnalysisResult::BadInvariants;
649 }
650 }
651 }
652 }
653
654 // originalBlocks[nBlocks-1] jumps unconditionally to header
655 {
656 MBasicBlock* header = originalBlocks[0];
657 MBasicBlock* backedge = originalBlocks[numBlocksInOriginal - 1];
658 if (!backedge->hasLastIns()) {
659 return AnalysisResult::BadInvariants;
660 }
661 MControlInstruction* lastIns = backedge->lastIns();
662 if (!lastIns->isGoto() || lastIns->toGoto()->target() != header) {
663 return AnalysisResult::BadInvariants;
664 }
665 }
666
667 // * original header node has 2 preds
668 // * original header node pred[0] is from outside the loop.
669 // pred[1] is from inside the loop.
670 {
671 MBasicBlock* header = originalBlocks[0];
672 if (header->numPredecessors() != 2 ||
673 BlockVectorContains(originalBlocks, header->getPredecessor(0)) ||
674 !BlockVectorContains(originalBlocks, header->getPredecessor(1))) {
675 return AnalysisResult::BadInvariants;
676 }
677 }
678
679 // * original header node phis all have 2 args
680 // * original header node phis have arg[0] from outside the loop
681 // (because pred[0] is the loop-entry edge)
682 // * original header node phis have their arg[1] as
683 // -- (almost always) defined in the loop -- loop carried variable
684 // -- (very occasionally) defined outside the loop -- in which case
685 // this is just a vanilla phi, nothing to do with the loop.
686 // Hence there is nothing to check for phi arg[1]s, but leaving this
687 // here for clarity.
688 {
689 MBasicBlock* header = originalBlocks[0];
690 for (MPhiIterator phiIter(header->phisBegin());
691 phiIter != header->phisEnd(); phiIter++) {
692 MPhi* phi = *phiIter;
693 if (phi->numOperands() != 2 ||
694 BlockVectorContains(originalBlocks, phi->getOperand(0)->block())) {
695 return AnalysisResult::BadInvariants;
696 }
697 }
698 }
699
700 // ==== END check invariants on the loop structure ====
701
702 // Check that all the insns are cloneable.
703 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
704 MBasicBlock* block = originalBlocks[bix];
705 for (MInstructionIterator insIter(block->begin()); insIter != block->end();
706 insIter++) {
707 MInstruction* ins = *insIter;
708 if (ins->isWasmCallCatchable() || ins->isWasmCallUncatchable() ||
709 ins->isWasmReturnCall() || ins->isTableSwitch()
710 /* TODO: other node kinds unsuitable for unrolling? */) {
711 return AnalysisResult::Unsuitable;
712 }
713 if (!ins->canClone()) {
714 // `ins->opName()` will show the name of the insn kind, if you want to
715 // see it
716 return AnalysisResult::Uncloneable;
717 }
718 }
719 }
720
721 // More analysis: make up a set of blocks that are not in the loop, but which
722 // are jumped to from within the loop. We will need this later.
723 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
724 MBasicBlock* block = originalBlocks[bix];
725 MControlInstruction* lastIns = block->lastIns(); // asserted safe above
726 for (size_t i = 0; i < lastIns->numSuccessors(); i++) {
727 MBasicBlock* succ = lastIns->getSuccessor(i);
728 // See if `succ` is any of the blocks in the (original) loop. If not,
729 // add it to `exitTargetBlocks`.
730 if (!BlockVectorContains(originalBlocks, succ)) {
731 if (!exitTargetBlocks->add(succ)) {
732 return AnalysisResult::OOM;
733 }
734 }
735 }
736 }
737
738 // If the loop has zero exits, we consider it sufficiently mutant to ignore
739 // (it's an infinite loop?)
740 if (exitTargetBlocks->empty()) {
741 return AnalysisResult::Unsuitable;
742 }
743
744 // Even more analysis: make up a set of MDefinition*s that are defined in the
745 // original loop and which are used after the loop. We will later need to
746 // add phi nodes that generate the correct values for these, since, after
747 // unrolling, these values will have multiple definition points (one per
748 // unrolled iteration). While we're at it, count the number of values
749 // defined in the original loop.
750 size_t numValuesInOriginal = 0;
751 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
752 MBasicBlock* block = originalBlocks[bix];
753 // Check all phi nodes ..
754 for (MPhiIterator phiIter(block->phisBegin()); phiIter != block->phisEnd();
755 phiIter++) {
756 numValuesInOriginal++;
757 MPhi* phi = *phiIter;
758 bool usedAfterLoop = false;
759 // Is the value of `phi` used after the loop?
760 for (MUseIterator useIter(phi->usesBegin()); useIter != phi->usesEnd();
761 useIter++) {
762 MUse* use = *useIter;
763 if (!BlockVectorContains(originalBlocks, use->consumer()->block())) {
764 usedAfterLoop = true;
765 break;
766 }
767 }
768 if (usedAfterLoop && !exitingValues->add(phi)) {
769 return AnalysisResult::OOM;
770 }
771 }
772 // .. and all normal nodes ..
773 for (MInstructionIterator insnIter(block->begin());
774 insnIter != block->end(); insnIter++) {
775 numValuesInOriginal++;
776 MInstruction* insn = *insnIter;
777 bool usedAfterLoop = false;
778 // Is the value of `insn` used after the loop?
779 for (MUseIterator useIter(insn->usesBegin()); useIter != insn->usesEnd();
780 useIter++) {
781 MUse* use = *useIter;
782 if (!BlockVectorContains(originalBlocks, use->consumer()->block())) {
783 usedAfterLoop = true;
784 break;
785 }
786 }
787 if (usedAfterLoop && !exitingValues->add(insn)) {
788 return AnalysisResult::OOM;
789 }
790 }
791 }
792
793 // Due to limitations in AddClosingPhisForLoop (see comments there),
794 // currently we can only unroll loops where, either:
795 //
796 // * no value defined in the loop is used afterwards, in which case we handle
797 // any number of exits from the loop.
798 //
799 // * one or more values defined in the loop is used afterwards, in which case
800 // there must be one exit point from the loop.
801 //
802 // IOW we can't handle multiple exits *and* values defined in the loop.
803 if (exitTargetBlocks->size() >= 2 && exitingValues->size() > 0) {
804 return AnalysisResult::TooComplex;
805 }
806
807 // There is an invariant that a loop exit target block does not contain any
808 // phis. This is really just the no-critical-edges invariant.
809 //
810 // By definition, a loop exit block has at least one successor in the loop
811 // and at least one successor outside the loop. If the loop exit target has
812 // phis, that would imply it has multiple predecessors. But then the edge
813 // would be critical, because it connects a block with multiple successors to
814 // a block with multiple predecessors.
815 //
816 // So enforce that; it simplifies the addition of loop-closing phis that will
817 // happen shortly.
818 for (size_t i = 0; i < exitTargetBlocks->size(); i++) {
819 MBasicBlock* exitTargetBlock = exitTargetBlocks->get(i);
820 if (!exitTargetBlock->phisEmpty()) {
821 return AnalysisResult::BadInvariants;
822 }
823 // Also we need this, since we'll be adding single-argument phis to this
824 // block during loop-closing.
825 if (exitTargetBlock->numPredecessors() != 1) {
826 return AnalysisResult::BadInvariants;
827 }
828 }
829
830 // At this point, we've worked through all invariant- and structural- reasons
831 // why we can't/shouldn't unroll/peel this loop. Now we need to decide which
832 // (or both) of those actions should happen.
833 bool peel = true;
834 bool unroll = true;
835 if (numBlocksInOriginal > MaxBlocksForPeel ||
836 numValuesInOriginal > MaxValuesForPeel) {
837 peel = false;
838 }
839 if (numBlocksInOriginal > MaxBlocksForUnroll ||
840 numValuesInOriginal > MaxValuesForUnroll) {
841 unroll = false;
842 }
843
844 if (peel) {
845 return unroll ? AnalysisResult::PeelAndUnroll : AnalysisResult::Peel;
846 } else {
847 return unroll ? AnalysisResult::Unroll : AnalysisResult::TooLarge;
848 }
849 }
850
851 // =====================================================================
852 //
853 // AddClosingPhisForLoop
854
855 // Add "loop-closing phis" for values defined in a loop and used afterwards.
856 // In GCC parlance, the resulting form is called LCSSA (Loop-Closed SSA). This
857 // is a very limited intervention in that it can only handle a single exit in a
858 // loop, thereby restricting the places where the phis should go to the
859 // one-and-only exit block, and avoiding the need to compute iterated dominance
860 // frontiers.
861
862 [[nodiscard]]
863 static bool AddClosingPhisForLoop(TempAllocator& alloc,
864 const UnrollState& state) {
865 // If no values exit the loop, we don't care how many exit target blocks
866 // there are, and need to make no changes.
867 if (state.exitingValues.empty()) {
868 return true;
869 }
870
871 // Ensured by AnalyzeLoop
872 MOZ_ASSERT(state.exitTargetBlocks.size() == 1);
873 MBasicBlock* targetBlock = state.exitTargetBlocks.get(0);
874 // Ensured by AnalyzeLoop. Required because we're adding single-arg phis
875 // to `targetBlock`.
876 MOZ_ASSERT(targetBlock->numPredecessors() == 1);
877
878 for (size_t i = 0; i < state.exitingValues.size(); i++) {
879 MDefinition* exitingValue = state.exitingValues.get(i);
880 // In `targetBlock`, add a single-argument phi node that "catches"
881 // `exitingValue`, and change all uses of `exitingValue` to be the phi
882 // node instead.
883 MPhi* phi = MPhi::New(alloc, exitingValue->type());
884 if (!phi) {
885 return false;
886 }
887 targetBlock->addPhi(phi);
888 // Change all uses of `exitingValue` outside of the loop into uses of
889 // `phi`.
890 for (MUseIterator useIter(exitingValue->usesBegin()),
891 useIterEnd(exitingValue->usesEnd());
892 useIter != useIterEnd;
893 /**/) {
894 MUse* use = *useIter++;
895 if (state.blockTable.rowContains(0, use->consumer()->block())) {
896 continue;
897 }
898 MOZ_ASSERT(use->consumer());
899 use->replaceProducer(phi);
900 }
901 // Finally, make `phi` be the one-and-only user of `exitingValue`.
902 if (!phi->addInputFallible(exitingValue)) {
903 return false;
904 }
905 }
906
907 // Invariant that should hold at this point: for each exiting value, all uses
908 // of it outside the loop should only be phi nodes.
909 for (size_t i = 0; i < state.exitingValues.size(); i++) {
910 MDefinition* exitingValue = state.exitingValues.get(i);
911 for (MUseIterator useIter(exitingValue->usesBegin());
912 useIter != exitingValue->usesEnd(); useIter++) {
913 mozilla::DebugOnly<MUse*> use = *useIter;
914 MOZ_ASSERT_IF(!state.blockTable.rowContains(0, use->consumer()->block()),
915 use->consumer()->isDefinition() &&
916 use->consumer()->toDefinition()->isPhi());
917 }
918 }
919
920 return true;
921 }
922
923 // =====================================================================
924 //
925 // UnrollAndOrPeelLoop
926
927 [[nodiscard]]
928 static bool UnrollAndOrPeelLoop(MIRGraph& graph, UnrollState& state) {
929 // Prerequisites (assumed):
930 // * AnalyzeLoop has approved this loop for peeling and/or unrolling
931 // * AddClosingPhisForLoop has been called for it
932 //
933 // We expect `state` to be ready to go, with the caveat that only the first
934 // row of entries in `state.blockTable` have been filled in (these are the
935 // original loop blocks). All other entries are null and will be filled in
936 // by this routine.
937 //
938 // Almost all the logic here is to do with unrolling. That's because peeling
939 // (done far below) can be done by a trivial modification of the loop
940 // resulting from unrolling: make the backedge jump to the second copy of the
941 // loop body rather than the first. That leaves the first copy (that is, the
942 // original loop body) in a "will only be executed once" state, as we desire.
943
944 // We need this so often it's worth pulling out specially.
945 const MBasicBlock* originalHeader = state.blockTable.get(0, 0);
946 MOZ_ASSERT(originalHeader->isLoopHeader());
947
948 #ifdef JS_JITSPEW
949 if (JitSpewEnabled(JitSpew_UnrollDetails)) {
950 Fprinter& printer(JitSpewPrinter());
951 printer.printf(
952 "<<<< ORIGINAL FUNCTION (after LCSSA-ification of chosen loops)\n");
953 DumpMIRGraph(printer, graph, /*showHashedPointers=*/true);
954 printer.printf(">>>>\n");
955 }
956 #endif
957
958 // Decide on the total unroll factor. "Copy #0" is the original loop, so the
959 // number of new copies is `unrollFactor - 1`. For example, if the original
960 // loop was `loop { B; }` then with `unrollFactor == 3`, the unrolled loop
961 // will be `loop { B; B; B; }`. Note that the value acquired here includes
962 // the copy needed for peeling, if that has been requested.
963 const uint32_t unrollFactor = state.blockTable.size1();
964 // The logic below will fail if this doesn't hold.
965 MOZ_ASSERT(unrollFactor >= 2);
966
967 // The number of blocks in the original loop.
968 const uint32_t numBlocksInOriginal = state.blockTable.size2();
969
970 // The number of values (phis and normal values) in the original loop.
971 uint32_t numValuesInOriginal = 0;
972 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
973 MBasicBlock* block = state.blockTable.get(0, bix);
974 // This is very feeble, but I can't think of anything better.
975 for (MPhiIterator phiIter(block->phisBegin()); phiIter != block->phisEnd();
976 phiIter++) {
977 numValuesInOriginal++;
978 }
979 for (MInstructionIterator insnIter(block->begin());
980 insnIter != block->end(); insnIter++) {
981 numValuesInOriginal++;
982 }
983 }
984 // `numValuesInOriginal` is const after this point.
985
986 #ifdef JS_JITSPEW
987 if (JitSpewEnabled(JitSpew_UnrollDetails)) {
988 DumpBlockTableRowZero(state.blockTable, "ORIGINAL LOOP");
989 Fprinter& printer(JitSpewPrinter());
990 printer.printf("<<<< EXIT TARGET BLOCKS: ");
991 for (size_t i = 0; i < state.exitTargetBlocks.size(); i++) {
992 MBasicBlock* targetBlock = state.exitTargetBlocks.get(i);
993 DumpMIRBlockID(printer, targetBlock, /*showHashedPointers=*/true);
994 printer.printf(" ");
995 }
996 printer.printf(">>>>\n");
997 printer.printf("<<<< EXITING VALUES: ");
998 for (size_t i = 0; i < state.exitingValues.size(); i++) {
999 MDefinition* exitingValue = state.exitingValues.get(i);
1000 DumpMIRDefinitionID(printer, exitingValue, /*showHashedPointers=*/true);
1001 printer.printf(" ");
1002 }
1003 printer.printf(">>>>\n");
1004 }
1005 #endif
1006
1007 // At this point, `state.blockTable` row zero contains the original blocks,
1008 // and all other rows are nulled out.
1009
1010 // Set up the value table. This is a structure similar to
1011 // `state.blockTable`: a 2-D array of values. This is local to this routine
1012 // rather than being part of `state` because it is only needed here, and can
1013 // take quite a lot of space.
1014 //
1015 // As with the block table, the main (actually, only) purpose of the value
1016 // table is to make it possible to find equivalent definitions in different
1017 // iterations. Also as above, "value index" (`vix` in the code) is a
1018 // zero-based index into the sequence of values (including phis) in the loop
1019 // as generated by the RPO traversal of the loop's blocks.
1020
1021 ValueTable valueTable;
1022 if (!valueTable.init(unrollFactor, numValuesInOriginal)) {
1023 return false;
1024 }
1025
1026 // Copy the original values into row zero of `valueTable`, parking them
1027 // end-to-end, without regard to block boundaries. All other rows remain
1028 // nulled out for now.
1029 {
1030 uint32_t vix = 0;
1031 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1032 MBasicBlock* block = state.blockTable.get(0, bix);
1033 for (MPhiIterator phiIter(block->phisBegin());
1034 phiIter != block->phisEnd(); phiIter++) {
1035 valueTable.set(0, vix, *phiIter);
1036 vix++;
1037 }
1038 for (MInstructionIterator insnIter(block->begin());
1039 insnIter != block->end(); insnIter++) {
1040 valueTable.set(0, vix, *insnIter);
1041 vix++;
1042 }
1043 }
1044 MOZ_ASSERT(vix == numValuesInOriginal);
1045 }
1046
1047 // Set up the value remapper. This maps MDefinitions* in the original body
1048 // to their "current" definition in new bodies. The only allowed keys are
1049 // MDefinition*s from the original body. To enforce this, the keys have to
1050 // be "registered" before use.
1051 MDefinitionRemapper mapper;
1052 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1053 MBasicBlock* originalBlock = state.blockTable.get(0, bix);
1054 // Create initial entries in `mapper` for both the normal insns and phi
1055 // nodes in `originalBlock`.
1056 for (MPhiIterator phiIter(originalBlock->phisBegin());
1057 phiIter != originalBlock->phisEnd(); phiIter++) {
1058 MPhi* originalPhi = *phiIter;
1059 if (!mapper.enregister(originalPhi)) {
1060 return false;
1061 }
1062 }
1063 for (MInstructionIterator insnIter(originalBlock->begin());
1064 insnIter != originalBlock->end(); insnIter++) {
1065 MInstruction* originalInsn = *insnIter;
1066 if (!mapper.enregister(originalInsn)) {
1067 return false;
1068 }
1069 }
1070 }
1071
1072 // Now we begin with the very core of the unrolling activity.
1073
1074 // Create new empty blocks for copy indices 1 .. (unrollFactor-1)
1075 const CompileInfo& info = originalHeader->info();
1076 for (uint32_t cix = 1; cix < unrollFactor; cix++) {
1077 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1078 MBasicBlock* empty = MBasicBlock::New(graph, info, /*pred=*/nullptr,
1079 MBasicBlock::Kind::NORMAL);
1080 if (!empty) {
1081 return false;
1082 }
1083 empty->setLoopDepth(state.blockTable.get(0, bix)->loopDepth());
1084 // We don't bother to set the block's ID, because the logic below not
1085 // depend on it. In any case the blocks will get renumbered at the end of
1086 // UnrollLoops, so it doesn't matter what we choose here. Unrolling
1087 // still works even if we don't set the ID to anything.
1088 MOZ_ASSERT(!state.blockTable.get(cix, bix));
1089 state.blockTable.set(cix, bix, empty);
1090 }
1091 }
1092
1093 // Copy verbatim, blocks in the original loop, into our new copies.
1094 // For each copy of the loop ..
1095 for (uint32_t cix = 1; cix < unrollFactor; cix++) {
1096 // For each block in the original copy ..
1097 uint32_t vix = 0;
1098 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1099 MBasicBlock* originalBlock = state.blockTable.get(0, bix);
1100 MBasicBlock* clonedBlock = state.blockTable.get(cix, bix);
1101
1102 // Clone the phi nodes, but don't update the mapper until all original
1103 // arguments have been looked up. This is required because the phi nodes
1104 // collectively implement a parallel assignment; there is no implied
1105 // sequentiality.
1106 mozilla::Vector<std::pair<const MPhi*, MPhi*>, 16, SystemAllocPolicy>
1107 mapperUpdates;
1108 for (MPhiIterator phiIter(originalBlock->phisBegin());
1109 phiIter != originalBlock->phisEnd(); phiIter++) {
1110 const MPhi* originalPhi = *phiIter;
1111 MPhi* clonedPhi =
1112 MakeReplacementPhi(graph.alloc(), mapper, originalPhi);
1113 if (!clonedPhi) {
1114 return false;
1115 }
1116 clonedBlock->addPhi(clonedPhi);
1117 MOZ_ASSERT(!valueTable.get(cix, vix));
1118 valueTable.set(cix, vix, clonedPhi);
1119 vix++;
1120 if (!mapperUpdates.append(std::pair(originalPhi, clonedPhi))) {
1121 return false;
1122 }
1123 }
1124 for (auto& p : mapperUpdates) {
1125 // Update the value mapper. The `originalPhi` values must be part of
1126 // the original loop body and so must already have a key in `mapper`.
1127 mapper.update(p.first, p.second);
1128 }
1129
1130 // Cloning the instructions is simpler, since we can incrementally update
1131 // the mapper.
1132 for (MInstructionIterator insnIter(originalBlock->begin());
1133 insnIter != originalBlock->end(); insnIter++) {
1134 const MInstruction* originalInsn = *insnIter;
1135 MInstruction* clonedInsn =
1136 MakeReplacementInstruction(graph.alloc(), mapper, originalInsn);
1137 if (!clonedInsn) {
1138 return false;
1139 }
1140 clonedBlock->insertAtEnd(clonedInsn);
1141 MOZ_ASSERT(!valueTable.get(cix, vix));
1142 valueTable.set(cix, vix, clonedInsn);
1143 vix++;
1144 // Update the value mapper. `originalInsn` must be part of the
1145 // original loop body and so must already have a key in `mapper`.
1146 mapper.update(originalInsn, clonedInsn);
1147 }
1148
1149 // Clone the block's predecessor array
1150 MOZ_ASSERT(clonedBlock->numPredecessors() == 0);
1151 for (uint32_t i = 0; i < originalBlock->numPredecessors(); i++) {
1152 MBasicBlock* pred = originalBlock->getPredecessor(i);
1153 if (!clonedBlock->appendPredecessor(pred)) {
1154 return false;
1155 }
1156 }
1157 }
1158
1159 MOZ_ASSERT(vix == numValuesInOriginal);
1160
1161 // Check we didn't mess up the valueTable ordering relative to the original
1162 // (best effort assertion).
1163 for (vix = 0; vix < numValuesInOriginal; vix++) {
1164 MOZ_ASSERT(valueTable.get(cix, vix)->op() ==
1165 valueTable.get(0, vix)->op());
1166 }
1167 }
1168
1169 #ifdef JS_JITSPEW
1170 if (JitSpewEnabled(JitSpew_UnrollDetails)) {
1171 DumpValueTable(valueTable, "VALUE TABLE (final)");
1172 }
1173 #endif
1174
1175 // Fix up (remap) inter-block edges in both the original and copies, using
1176 // uniform indexing as above (original = index 0, copies = indices >= 1):
1177
1178 // For a block with copy-index N, inspect each edge. Determine the role the
1179 // edge-destination (target) plays in the original copy:
1180 //
1181 // * backedge
1182 // -> remap to header node of copy (N + 1) % unrollFactor
1183 // (so, generates a backedge in the last copy)
1184 // * internal jump within the original body
1185 // -> remap to corresponding block in copy N
1186 // * jump to after the original body
1187 // -> unchanged (it is an exiting edge)
1188 // * jump to before the original body
1189 // -> this can't happen
1190 // (but if it did, it would be just another exit block)
1191 auto RemapEdgeDestination =
1192 [=, &state](uint32_t cix, MBasicBlock* oldDestination) -> MBasicBlock* {
1193 // `oldDestination` is the target of a control-flow insn in the original
1194 // loop. Figure out what its replacement should be, for copy index `cix`
1195 // (with `cix == 0` meaning the original copy).
1196
1197 // If it is a back edge, remap to the header node of the next copy, with
1198 // wraparound for the last copy.
1199 if (oldDestination == state.blockTable.get(0, 0)) {
1200 MOZ_ASSERT(oldDestination == originalHeader);
1201 return state.blockTable.get((cix + 1) % unrollFactor, 0);
1202 }
1203
1204 // If it is to some other node within the original body, remap to the
1205 // corresponding node in this copy.
1206 for (uint32_t i = 1; i < numBlocksInOriginal; i++) {
1207 if (oldDestination == state.blockTable.get(0, i)) {
1208 return state.blockTable.get(cix, i);
1209 }
1210 }
1211
1212 // The target of the edge is not in the original loop. Either it's to
1213 // after the loop (an exiting branch) or it is to before the loop, although
1214 // that would be bizarre and probably illegal. Either way, leave it
1215 // unchanged.
1216 return oldDestination;
1217 };
1218
1219 // Similarly, for an edge that claims to come from `oldSource` in the
1220 // original loop, determine where it should come from if we imagine that it
1221 // applies to copy index `cix`.
1222 auto RemapEdgeSource = [=, &state](uint32_t cix,
1223 MBasicBlock* oldSource) -> MBasicBlock* {
1224 if (oldSource == state.blockTable.get(0, numBlocksInOriginal - 1)) {
1225 // Source is backedge node in the original loop. Remap to the backedge
1226 // node in the previous iteration.
1227 MOZ_ASSERT(oldSource == originalHeader->backedge());
1228 return state.blockTable.get(cix == 0 ? (unrollFactor - 1) : (cix - 1),
1229 numBlocksInOriginal - 1);
1230 }
1231 for (uint32_t i = 0; i < numBlocksInOriginal - 1; i++) {
1232 if (oldSource == state.blockTable.get(0, i)) {
1233 // Source is a non-backedge node in the original loop. Remap it to
1234 // the equivalent node in this copy.
1235 return state.blockTable.get(cix, i);
1236 }
1237 }
1238 // Source is not in the original loop. Leave unchanged.
1239 return oldSource;
1240 };
1241
1242 // Work through all control-flow instructions in all copies and remap their
1243 // edges.
1244 // For each copy of the loop, including the original ..
1245 for (uint32_t cix = 0; cix < unrollFactor; cix++) {
1246 // For each block in the copy ..
1247 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1248 MBasicBlock* b = state.blockTable.get(cix, bix);
1249 MControlInstruction* lastI = b->lastIns();
1250 if (lastI->isGoto()) {
1251 MGoto* insn = lastI->toGoto();
1252 insn->setTarget(RemapEdgeDestination(cix, insn->target()));
1253 } else if (lastI->isTest()) {
1254 MTest* insn = lastI->toTest();
1255 insn->setIfTrue(RemapEdgeDestination(cix, insn->ifTrue()));
1256 insn->setIfFalse(RemapEdgeDestination(cix, insn->ifFalse()));
1257 } else {
1258 // The other existing control instructions are MTableSwitch,
1259 // MUnreachable, MWasmTrap, MWasmReturn, and MWasmReturnVoid.
1260 // MTableSwitch is ruled out above. The others end a block without a
1261 // successor, which means that they can't be loop blocks
1262 // (MarkLoopBlocks marks blocks that are predecessors of the back
1263 // edge.) So we don't expect to see them here.
1264 MOZ_CRASH();
1265 }
1266 }
1267 }
1268
1269 // Also we need to remap within the each block's predecessors vector.
1270 // We have to do this backwards to stop RemapEdgeSource from asserting.
1271 // For each copy of the loop, including the original ..
1272 for (int32_t cix = unrollFactor - 1; cix >= 0; cix--) {
1273 // For each block in the copy ..
1274 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1275 MBasicBlock* block = state.blockTable.get(cix, bix);
1276 for (uint32_t i = 0; i < block->numPredecessors(); i++) {
1277 MBasicBlock* oldPred = block->getPredecessor(i);
1278 MBasicBlock* newPred = RemapEdgeSource(cix, oldPred);
1279 block->setPredecessor(i, newPred);
1280 }
1281 }
1282 }
1283
1284 // Clean up the header node copies (but not for the original copy) since they
1285 // all still claim to have two predecessors, the first of which is the entry
1286 // edge from outside the loop. This is no longer true, so delete that
1287 // predecessor. Remove the corresponding Phi args too. This leaves
1288 // one-argument phis, meh.
1289 for (uint32_t cix = 1; cix < unrollFactor; cix++) {
1290 MBasicBlock* block = state.blockTable.get(cix, 0);
1291 MOZ_ASSERT(block->numPredecessors() == 2); // from AnalyzeLoop
1292 block->erasePredecessor(0);
1293 MOZ_ASSERT(block->numPredecessors() == 1); // duh
1294 for (MPhiIterator phiIter(block->phisBegin()); phiIter != block->phisEnd();
1295 phiIter++) {
1296 MPhi* phi = *phiIter;
1297 MOZ_ASSERT(phi->numOperands() == 2); // from AnalyzeLoop
1298 phi->removeOperand(0);
1299 MOZ_ASSERT(phi->numOperands() == 1);
1300 }
1301 }
1302
1303 // Fix up the phi nodes in the original header; the previous step did not do
1304 // that. Specifically, the second arg in each phi -- which are values that
1305 // flow in from the backedge block -- need to be the ones from the last body
1306 // copy; but currently they are from the original copy. At this point, the
1307 // `mapper` should be able to tell us the correct value.
1308 for (MPhiIterator phiIter(originalHeader->phisBegin());
1309 phiIter != originalHeader->phisEnd(); phiIter++) {
1310 MPhi* phi = *phiIter;
1311 MOZ_ASSERT(phi->numOperands() == 2); // from AnalyzeLoop
1312 // phi arg[0] is defined before the loop. Ensured by AnalyzeLoop.
1313 MOZ_ASSERT(!state.blockTable.rowContains(0, phi->getOperand(0)->block()));
1314 // phi arg[1] is almost always defined in the loop (hence it is a
1315 // loop-carried value) but is very occasionally defined before the loop
1316 // (so it is unrelated to the loop). Proceed with maximum paranoia.
1317 MDefinition* operand1 = phi->getOperand(1);
1318 MDefinition* replacement = mapper.lookup(operand1);
1319 // If we didn't find a replacement (unlikely but possible) then
1320 // `operand1` must be defined before the loop.
1321 MOZ_ASSERT((replacement == nullptr) ==
1322 !state.blockTable.rowContains(0, operand1->block()));
1323 if (replacement) {
1324 phi->replaceOperand(1, replacement);
1325 }
1326 }
1327
1328 // Now we need to fix up the exit target blocks, by adding sources of exiting
1329 // edges to their `predecessors_` vectors and updating their phi nodes
1330 // accordingly. Recall that those phi nodes were previously installed in
1331 // these blocks by AddClosingPhisForLoop.
1332 //
1333 // Currently the exit target blocks mention only predecessors in the original
1334 // loop, but they also need to mention equivalent predecessors in each of the
1335 // loop copies.
1336 for (uint32_t cix = 0; cix < unrollFactor; cix++) {
1337 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1338 MBasicBlock* block = state.blockTable.get(cix, bix);
1339 MControlInstruction* lastIns = block->lastIns();
1340 for (size_t i = 0; i < lastIns->numSuccessors(); i++) {
1341 MBasicBlock* succ = lastIns->getSuccessor(i);
1342 if (!state.exitTargetBlocks.contains(succ)) {
1343 // This isn't an exiting edge -- not interesting.
1344 continue;
1345 }
1346 // If `cix` is zero (the original body), we expect `succ` to already
1347 // list `block` as a predecessor. If `cix` is nonzero (a body copy)
1348 // then we expect `succ` *not* to list it, and we must add it.
1349 mozilla::DebugOnly<bool> found = false;
1350 for (uint32_t j = 0; j < succ->numPredecessors(); j++) {
1351 if (block == succ->getPredecessor(j)) {
1352 found = true;
1353 break;
1354 }
1355 }
1356 MOZ_ASSERT((cix == 0) == found);
1357 if (cix > 0) {
1358 if (!succ->appendPredecessor(block)) {
1359 return false;
1360 }
1361 for (MPhiIterator phiIter(succ->phisBegin());
1362 phiIter != succ->phisEnd(); phiIter++) {
1363 MPhi* phi = *phiIter;
1364 // "+ 1" because we just added a predecessor
1365 MOZ_ASSERT(phi->numOperands() + 1 == succ->numPredecessors());
1366 MDefinition* exitingValue = phi->getOperand(0);
1367 MOZ_ASSERT(state.exitingValues.contains(exitingValue));
1368 // `exitingValue` is defined in the original loop body. We need to
1369 // find the equivalent value in copy `cix` so we can add it as an
1370 // argument to the phi. This bit is the one-and-only reason for
1371 // `valueTable`s existence.
1372 mozilla::Maybe<size_t> colIndex =
1373 valueTable.findInRow(0, exitingValue);
1374 MOZ_ASSERT(colIndex.isSome()); // We must find it!
1375 MDefinition* equivValue = valueTable.get(cix, colIndex.value());
1376 if (!phi->addInputFallible(equivValue)) {
1377 return false;
1378 }
1379 }
1380 }
1381 }
1382 }
1383 }
1384
1385 // At this point the control flow is correct. But we may now have critical
1386 // edges where none existed before. Consider a loop {Block1, Block2} with an
1387 // exit target Block9:
1388 //
1389 // Block1
1390 // stuff1
1391 // Goto Block2
1392 // Block2
1393 // stuff2
1394 // Test true->Block1(== continue) false->Block9(== break)
1395 // ...
1396 // Block9
1397 // this is a loop-exit target
1398 // preds = Block2
1399 //
1400 // After factor-of-2 unrolling:
1401 //
1402 // Block1
1403 // stuff1
1404 // Goto Block2
1405 // Block2
1406 // stuff2
1407 // Test true->Block3(== continue) false->Block9(== break)
1408 // Block3
1409 // stuff3
1410 // Goto Block4
1411 // Block4
1412 // stuff4
1413 // Test true->Block1(== continue) false->Block9(== break)
1414 // ...
1415 // Block9
1416 // preds = Block2, Block4
1417 //
1418 // Now the exiting edges (Block2 to Block9 and Block4 to Block9) are critical.
1419 //
1420 // The obvious fix is to insert a new block on each exiting edge.
1421
1422 mozilla::Vector<MBasicBlock*, 16, SystemAllocPolicy> splitterBlocks;
1423 for (uint32_t cix = 0; cix < unrollFactor; cix++) {
1424 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1425 MBasicBlock* block = state.blockTable.get(cix, bix);
1426 // Inspect all successors of `block`. If any are exiting edges, create a
1427 // new block and route through that instead.
1428 MControlInstruction* lastIns = block->lastIns();
1429 for (size_t i = 0; i < lastIns->numSuccessors(); i++) {
1430 MBasicBlock* succ = lastIns->getSuccessor(i);
1431 if (!state.exitTargetBlocks.contains(succ)) {
1432 // This isn't an exiting edge -- not interesting.
1433 continue;
1434 }
1435 // Invent a new block.
1436 MBasicBlock* splitter =
1437 MBasicBlock::New(graph, info, block, MBasicBlock::Kind::SPLIT_EDGE);
1438 if (!splitter || !splitterBlocks.append(splitter)) {
1439 return false;
1440 }
1441 splitter->setLoopDepth(succ->loopDepth());
1442 // As with empty-block creation earlier in this routine, we don't
1443 // bother to set the block's ID since none of the logic depends on it,
1444 // and it will eventually be renumbered anyway by UnrollLoops.
1445 MGoto* jump = MGoto::New(graph.alloc(), succ);
1446 if (!jump) {
1447 return false;
1448 }
1449 splitter->insertAtEnd(jump);
1450 // For the target, fix up the `predecessor_` entry.
1451 mozilla::DebugOnly<bool> found = false;
1452 for (uint32_t j = 0; j < succ->numPredecessors(); j++) {
1453 if (succ->getPredecessor(j) == block) {
1454 succ->setPredecessor(j, splitter);
1455 found = true;
1456 break;
1457 }
1458 }
1459 // Finally, reroute the jump to `succ` via `splitter`.
1460 lastIns->replaceSuccessor(i, splitter);
1461 MOZ_ASSERT(found);
1462 }
1463 }
1464 }
1465
1466 // Now the control flow is correct *and* we don't have any critical edges.
1467 // Fix up the `successorWithPhis_` and `positionInPhiSuccessor_` fields in
1468 // both the unrolled loop and the splitter blocks, thusly:
1469 //
1470 // For all blocks `B` in the unrolled loop, and for all splitter blocks, fix
1471 // up the `B.successorWithPhis_` and `B.positionInPhiSuccessor_` fields.
1472 // Each block may have at maximum one successor that has phi nodes, in which
1473 // case `B.successorWithPhis_` should point at that block. And
1474 // `B.positionInPhiSuccessor_` should indicate the index that successor's
1475 // `predecessors_` vector, of B.
1476 {
1477 // Make up a list of blocks to fix up, then process them.
1478 mozilla::Vector<MBasicBlock*, 32 + 16, SystemAllocPolicy> workList;
1479 for (uint32_t cix = 0; cix < unrollFactor; cix++) {
1480 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1481 MBasicBlock* block = state.blockTable.get(cix, bix);
1482 if (!workList.append(block)) {
1483 return false;
1484 }
1485 }
1486 }
1487 for (MBasicBlock* block : splitterBlocks) {
1488 if (!workList.append(block)) {
1489 return false;
1490 }
1491 }
1492 // Now process them.
1493 for (MBasicBlock* block : workList) {
1494 // Inspect `block`s successors.
1495 MBasicBlock* succWithPhis = nullptr;
1496 MControlInstruction* lastIns = block->lastIns();
1497 // Determine `succWithPhis`, if one exists.
1498 for (size_t i = 0; i < lastIns->numSuccessors(); i++) {
1499 MBasicBlock* succ = lastIns->getSuccessor(i);
1500 if (succ->phisEmpty()) {
1501 continue;
1502 }
1503 MOZ_ASSERT(succ);
1504 // `succ` is a successor to `block` that has phis. So we can't already
1505 // have seen a (different) successor.
1506 if (!succWithPhis) {
1507 succWithPhis = succ;
1508 } else if (succWithPhis == succ) {
1509 // ignore duplicate successors
1510 } else {
1511 // More than one successor with phis. Structural invariants
1512 // invalidated. Give up.
1513 MOZ_CRASH();
1514 }
1515 }
1516 // Annotate `block`.
1517 if (!succWithPhis) {
1518 block->setSuccessorWithPhis(nullptr, 0);
1519 } else {
1520 MOZ_ASSERT(!succWithPhis->phisEmpty());
1521 // Figure out which of `succWithPhis`s predecessors `block` is.
1522 // Should we assert `succWithPhis->predecessors_` is duplicate-free?
1523 uint32_t predIx = UINT32_MAX; // invalid
1524 for (uint32_t i = 0; i < succWithPhis->numPredecessors(); i++) {
1525 if (succWithPhis->getPredecessor(i) == block) {
1526 predIx = i;
1527 break;
1528 }
1529 }
1530 MOZ_ASSERT(predIx != UINT32_MAX);
1531 block->setSuccessorWithPhis(succWithPhis, predIx);
1532 }
1533 }
1534 }
1535
1536 // Find and remove the MWasmInterruptCheck in all but the last iteration.
1537 // We don't assume that there is an interrupt check, since
1538 // wasm::FunctionCompiler::fillArray, at least, generates a loop with no
1539 // check.
1540 for (uint32_t cix = 0; cix < unrollFactor - 1; cix++) {
1541 for (uint32_t vix = 0; vix < numValuesInOriginal; vix++) {
1542 MDefinition* ins = valueTable.get(cix, vix);
1543 if (!ins->isWasmInterruptCheck()) {
1544 continue;
1545 }
1546 MWasmInterruptCheck* ic = ins->toWasmInterruptCheck();
1547 ic->block()->discard(ic);
1548 valueTable.set(cix, vix, nullptr);
1549 }
1550 }
1551
1552 // We're all done with unrolling. If peeling was also requested, make a
1553 // minor modification to the unrolled loop: change the backedge so that,
1554 // instead of jumping to the first body copy, make it jump to the second body
1555 // copy. This leaves the first body copy in a
1556 // will-only-ever-be-executed-once state, hence achieving peeling.
1557 if (state.doPeeling()) {
1558 MBasicBlock* backedge =
1559 state.blockTable.get(unrollFactor - 1, numBlocksInOriginal - 1);
1560 MBasicBlock* header0 = state.blockTable.get(0, 0);
1561 MBasicBlock* header1 = state.blockTable.get(1, 0);
1562 MOZ_ASSERT(header0 == originalHeader);
1563
1564 // Fix up the back edge, to make it jump to the second copy of the loop
1565 // body
1566 MOZ_ASSERT(backedge->hasLastIns()); // should always hold (AnalyzeLoop)
1567 MOZ_ASSERT(backedge->lastIns()->isGoto()); // AnalyzeLoop ensures
1568 MGoto* backedgeG = backedge->lastIns()->toGoto();
1569 MOZ_ASSERT(backedgeG->target() == header0); // AnalyzeLoop ensures
1570 backedgeG->setTarget(header1);
1571 header0->clearLoopHeader();
1572 header1->setLoopHeader();
1573 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1574 MBasicBlock* originalBlock = state.blockTable.get(0, bix);
1575 MOZ_ASSERT(originalBlock->loopDepth() > 0);
1576 originalBlock->setLoopDepth(originalBlock->loopDepth() - 1);
1577 }
1578
1579 // Fix up the preds of the original header
1580 MOZ_ASSERT(header0->numPredecessors() == 2);
1581 MOZ_ASSERT(header0->getPredecessor(1) == backedge);
1582 header0->erasePredecessor(1);
1583
1584 // Fix up the preds of the new header
1585 MOZ_ASSERT(header1->numPredecessors() == 1);
1586 if (!header1->appendPredecessor(backedge)) {
1587 return false;
1588 }
1589
1590 // Fix up phis of both headers, by moving the second operand of each phi of
1591 // `header0` to be the second operand of the corresponding phi in
1592 // `header1`.
1593 MPhiIterator phiIter0(header0->phisBegin());
1594 MPhiIterator phiIter1(header1->phisBegin());
1595 while (true) {
1596 bool finished0 = phiIter0 == header0->phisEnd();
1597 mozilla::DebugOnly<bool> finished1 = phiIter1 == header1->phisEnd();
1598 // The two blocks must have the same number of phis.
1599 MOZ_ASSERT(finished0 == finished1);
1600 if (finished0) {
1601 break;
1602 }
1603 MPhi* phi0 = *phiIter0;
1604 MPhi* phi1 = *phiIter1;
1605 MOZ_ASSERT(phi0->numOperands() == 2);
1606 MOZ_ASSERT(phi1->numOperands() == 1);
1607 MDefinition* phi0arg1 = phi0->getOperand(1);
1608 phi0->removeOperand(1);
1609 if (!phi1->addInputFallible(phi0arg1)) {
1610 return false;
1611 }
1612 phiIter0++;
1613 phiIter1++;
1614 }
1615
1616 // Fix the succ-w-phis entry for `backEdge`. In very rare circumstances,
1617 // the loop may have no header phis, so this must be conditional.
1618 if (backedge->successorWithPhis()) {
1619 MOZ_ASSERT(backedge->successorWithPhis() == header0);
1620 MOZ_ASSERT(backedge->positionInPhiSuccessor() == 1);
1621 backedge->setSuccessorWithPhis(header1, 1);
1622 }
1623 }
1624
1625 #ifdef JS_JITSPEW
1626 if (JitSpewEnabled(JitSpew_UnrollDetails)) {
1627 DumpBlockTable(state.blockTable, "LOOP BLOCKS (final)");
1628 Fprinter& printer(JitSpewPrinter());
1629 printer.printf("<<<< SPLITTER BLOCKS\n");
1630 for (MBasicBlock* block : splitterBlocks) {
1631 DumpMIRBlock(printer, block, /*showHashedPointers=*/true);
1632 }
1633 printer.printf(">>>>\n");
1634 }
1635 #endif
1636
1637 // Add the new blocks to the graph.
1638 {
1639 // We want to add them to the end of the existing loop.
1640 MBasicBlock* cursor = state.blockTable.get(0, numBlocksInOriginal - 1);
1641 for (uint32_t cix = 1; cix < unrollFactor; cix++) {
1642 for (uint32_t bix = 0; bix < numBlocksInOriginal; bix++) {
1643 MBasicBlock* addMe = state.blockTable.get(cix, bix);
1644 graph.insertBlockAfter(cursor, addMe);
1645 cursor = addMe;
1646 }
1647 }
1648 for (MBasicBlock* addMe : splitterBlocks) {
1649 graph.insertBlockAfter(cursor, addMe);
1650 cursor = addMe;
1651 }
1652 }
1653
1654 // UnrollLoops will renumber the blocks and redo the dominator tree once all
1655 // loops have been unrolled.
1656
1657 #ifdef JS_JITSPEW
1658 if (JitSpewEnabled(JitSpew_UnrollDetails)) {
1659 Fprinter& printer(JitSpewPrinter());
1660 printer.printf("<<<< FUNCTION AFTER UNROLLING\n");
1661 DumpMIRGraph(printer, graph, /*showHashedPointers=*/true);
1662 printer.printf(">>>>\n");
1663 }
1664 #endif
1665
1666 return true;
1667 }
1668
1669 // =====================================================================
1670 //
1671 // FindInitialCandidates, which finds "initial candidates" for
1672 // unrolling/peeling. See SMDOC at the top of this file.
1673
1674 struct InitialCandidate {
1675 MBasicBlock* header;
1676 uint32_t numBlocks;
1677 float score;
1678 bool valid;
1679 InitialCandidate(MBasicBlock* header, uint32_t numBlocks)
1680 : header(header), numBlocks(numBlocks), score(0.0), valid(true) {}
1681 };
1682
1683 using InitialCandidateVector =
1684 mozilla::Vector<InitialCandidate, 64, SystemAllocPolicy>;
1685
1686 [[nodiscard]]
1687 bool FindInitialCandidates(MIRGraph& graph,
1688 InitialCandidateVector& initialCandidates) {
1689 // Find initial candidate loops, and place them in `initialCandidates`.
1690 MOZ_ASSERT(initialCandidates.empty());
1691
1692 // First, using a single scan over the blocks, find blocks that are the
1693 // headers of innermost loops. This uses the same logic to find innermost
1694 // loops as BacktrackingAllocator::init.
1695 //
1696 // The general idea is: we traverse the entire graph in RPO order, but ignore
1697 // all blocks except loop headers and loop backedge blocks. At a header we
1698 // make a note of the backedge block it points at. Because the loops are
1699 // properly nested, this means that we will ignore backedges for
1700 // non-innermost loops, and, so, when we do come to a block that matches
1701 // `backedge`, we know we've identified an innermost loop.
1702 //
1703 // At the same time, release-assert that the blocks are numbered
1704 // sequentially. We need this for the loop-non-overlapping check below.
1705 mozilla::Vector<MBasicBlock*, 128, SystemAllocPolicy> initialHeaders;
1706 {
1707 MBasicBlock* backedge = nullptr;
1708 uint32_t expectedNextId = 0;
1709 for (auto rpoIter(graph.rpoBegin()), rpoIterEnd(graph.rpoEnd());
1710 rpoIter != rpoIterEnd; ++rpoIter) {
1711 MBasicBlock* block = *rpoIter;
1712 MOZ_RELEASE_ASSERT(block->id() == expectedNextId);
1713 expectedNextId++;
1714
1715 if (block->isLoopHeader()) {
1716 backedge = block->backedge();
1717 }
1718 if (block == backedge) {
1719 if (!initialHeaders.append(block->loopHeaderOfBackedge())) {
1720 return false;
1721 }
1722 }
1723 }
1724 }
1725
1726 // Now use MarkLoopBlocks to check each potential loop in more detail, and,
1727 // for each non-empty, non-OSR one, create an entry in initialCandidates for
1728 // it.
1729 for (MBasicBlock* header : initialHeaders) {
1730 MOZ_ASSERT(header->isLoopHeader());
1731
1732 bool hasOsrEntry;
1733 size_t numBlocks = MarkLoopBlocks(graph, header, &hasOsrEntry);
1734 if (numBlocks == 0) {
1735 // Bogus; skip.
1736 continue;
1737 }
1738 UnmarkLoopBlocks(graph, header);
1739 if (hasOsrEntry) {
1740 // Unhandled; skip.
1741 continue;
1742 }
1743
1744 MOZ_RELEASE_ASSERT(numBlocks <= UINT32_MAX);
1745 if (!initialCandidates.append(InitialCandidate(header, numBlocks))) {
1746 return false;
1747 }
1748 }
1749
1750 // Sort the loops by header ID.
1751 std::sort(initialCandidates.begin(), initialCandidates.end(),
1752 [](const InitialCandidate& cand1, const InitialCandidate& cand2) {
1753 return cand1.header->id() < cand2.header->id();
1754 });
1755
1756 // Now we can conveniently assert non-overlappingness. Note that this
1757 // depends on the property that each loop is a sequential sequence of block
1758 // IDs, as we release-asserted above. Presence of an overlap might indicate
1759 // that a non-innermost loop made it this far.
1760 for (size_t i = 1; i < initialCandidates.length(); i++) {
1761 MOZ_RELEASE_ASSERT(initialCandidates[i - 1].header->id() +
1762 initialCandidates[i - 1].numBlocks <=
1763 initialCandidates[i].header->id());
1764 }
1765
1766 // Heuristics: calculate a "score" for each loop, indicating our desire to
1767 // unroll it. **Lower** values indicate a higher desirability for unrolling.
1768 //
1769 // A loop gets a lower score if:
1770 // * it is small
1771 // * it has few blocks
1772 // * it is deeply nested (currently ignored)
1773
1774 for (InitialCandidate& cand : initialCandidates) {
1775 uint32_t nBlocks = cand.numBlocks;
1776 uint32_t nInsns = 0;
1777 uint32_t nPhis = 0;
1778
1779 // Visit each block in the loop
1780 mozilla::DebugOnly<uint32_t> numBlocksVisited = 0;
1781 const MBasicBlock* backedge = cand.header->backedge();
1782
1783 for (auto i(graph.rpoBegin(cand.header)); /**/; ++i) {
1784 MOZ_ASSERT(i != graph.rpoEnd(),
1785 "UnrollLoops: loop blocks overrun graph end");
1786 MBasicBlock* block = *i;
1787 numBlocksVisited++;
1788 for (MInstructionIterator iter(block->begin()); iter != block->end();
1789 iter++) {
1790 nInsns++;
1791 }
1792 for (MPhiIterator iter(block->phisBegin()); iter != block->phisEnd();
1793 iter++) {
1794 nPhis++;
1795 }
1796
1797 if (block == backedge) {
1798 break;
1799 }
1800 }
1801
1802 MOZ_ASSERT(numBlocksVisited == nBlocks);
1803
1804 cand.score =
1805 10.0 * float(nBlocks) + 2.0 * float(nInsns) + 1.0 * float(nPhis);
1806 }
1807
1808 // Sort the loops by `score`.
1809 std::sort(initialCandidates.begin(), initialCandidates.end(),
1810 [](const InitialCandidate& cand1, const InitialCandidate& cand2) {
1811 return cand1.score < cand2.score;
1812 });
1813
1814 return true;
1815 }
1816
1817 // =====================================================================
1818 //
1819 // UnrollLoops, the top level of the unroller.
1820
1821 bool UnrollLoops(const MIRGenerator* mir, MIRGraph& graph, bool* changed) {
1822 *changed = false;
1823
1824 #ifdef JS_JITSPEW
1825 if (JitSpewEnabled(JitSpew_Unroll)) {
1826 JitSpew(JitSpew_Unroll, "BEGIN UnrollLoops");
1827 }
1828 #endif
1829
1830 // Make up a vector of initial candidates, which are innermost loops only,
1831 // and not too big.
1832
1833 InitialCandidateVector initialCandidates;
1834 if (!FindInitialCandidates(graph, initialCandidates)) {
1835 return false;
1836 }
1837
1838 #ifdef JS_JITSPEW
1839 if (JitSpewEnabled(JitSpew_Unroll)) {
1840 for (const InitialCandidate& cand : initialCandidates) {
1841 MOZ_ASSERT(cand.valid);
1842 JitSpew(JitSpew_Unroll, " initial cand: score=%-5.1f blocks=%u-%u",
1843 cand.score, cand.header->id(),
1844 cand.header->id() + cand.numBlocks - 1);
1845 }
1846 }
1847 #endif
1848
1849 // Perform a more expensive analysis of the candidates, leading to a further
1850 // reduction in the set. For those remaining, an UnrollState is created and
1851 // initialized. Also a decision is made whether to unroll only, peel only,
1852 // or both.
1853
1854 mozilla::Vector<UnrollState, 32, SystemAllocPolicy> unrollStates;
1855
1856 for (const InitialCandidate& cand : initialCandidates) {
1857 if (cand.score > InitialCandidateThreshold) {
1858 // Give up. We just sorted them by `score`, so the rest will be even
1859 // more undesirable for unrolling.
1860 break;
1861 }
1862
1863 // Park the loop's blocks in a vector, so we can hand it off to
1864 // AnalyzeLoops for examination. This is the place where the original
1865 // block sequence, as seen by the entire rest of the machinery, is fixed.
1866 BlockVector originalBlocks;
1867
1868 const MBasicBlock* backedge = cand.header->backedge();
1869
1870 for (auto blockIter(graph.rpoBegin(cand.header)); /**/; ++blockIter) {
1871 MOZ_ASSERT(blockIter != graph.rpoEnd());
1872 MBasicBlock* block = *blockIter;
1873 if (!originalBlocks.append(block)) {
1874 return false;
1875 }
1876 if (block == backedge) {
1877 break;
1878 }
1879 }
1880
1881 // Analyze the candidate in detail. If unrolling and/or peeling is
1882 // approved, this also collects up for us, the loop's target block set and
1883 // exiting value set.
1884 BlockSet exitTargetBlocks;
1885 ValueSet exitingValues;
1886
1887 AnalysisResult res =
1888 AnalyzeLoop(originalBlocks, &exitTargetBlocks, &exitingValues);
1889
1890 #ifdef JS_JITSPEW
1891 if (JitSpewEnabled(JitSpew_Unroll)) {
1892 MOZ_ASSERT(cand.valid);
1893 JitSpew(JitSpew_Unroll, " %13s: score=%-5.1f blocks=%u-%u",
1894 Name_of_AnalysisResult(res), cand.score, cand.header->id(),
1895 cand.header->id() + cand.numBlocks - 1);
1896 }
1897 #endif
1898
1899 switch (res) {
1900 case AnalysisResult::OOM:
1901 return false;
1902 case AnalysisResult::Peel:
1903 case AnalysisResult::Unroll:
1904 case AnalysisResult::PeelAndUnroll:
1905 break;
1906 case AnalysisResult::BadInvariants:
1907 MOZ_CRASH("js::jit::UnrollLoops: unexpected incoming MIR");
1908 case AnalysisResult::TooComplex:
1909 case AnalysisResult::Uncloneable:
1910 case AnalysisResult::TooLarge:
1911 case AnalysisResult::Unsuitable:
1912 // Ignore this loop
1913 continue;
1914 default:
1915 MOZ_CRASH();
1916 }
1917
1918 // The candidate has been accepted for unrolling and/or peeling. Make an
1919 // initial UnrollState for it and add it to our collection thereof.
1920
1921 UnrollMode mode;
1922 if (res == AnalysisResult::Peel) {
1923 mode = UnrollMode::Peel;
1924 } else if (res == AnalysisResult::Unroll) {
1925 mode = UnrollMode::Unroll;
1926 } else if (res == AnalysisResult::PeelAndUnroll) {
1927 mode = UnrollMode::PeelAndUnroll;
1928 } else {
1929 MOZ_CRASH();
1930 }
1931
1932 if (!unrollStates.emplaceBack(mode)) {
1933 return false;
1934 }
1935 UnrollState* state = &unrollStates.back();
1936 if (!state->exitTargetBlocks.copy(exitTargetBlocks) ||
1937 !state->exitingValues.copy(exitingValues)) {
1938 return false;
1939 }
1940
1941 // Ignoring peeling, how many times should we unroll a loop?
1942 // For example, if the original loop was `loop { B; }` then with
1943 // `basicUnrollingFactor = 3`, the unrolled loop will be `loop { B; B; B;
1944 // }`. If peeling is also requested then we will unroll one more time than
1945 // this, giving overall result `B; loop { B; B; B; }`.
1946 uint32_t basicUnrollingFactor = JS::Prefs::wasm_unroll_factor();
1947 if (basicUnrollingFactor < 2) {
1948 // It needs to be at least 2, else we're not unrolling at all.
1949 basicUnrollingFactor = 2;
1950 } else if (basicUnrollingFactor > 8) {
1951 // Unrolling gives rapidly diminishing marginal gains. Even beyond 4
1952 // there's little benefit to be had.
1953 basicUnrollingFactor = 8;
1954 }
1955
1956 // Decide on the total number of duplicates of the loop body we'll need
1957 // (including the original).
1958 uint32_t unrollingFactor;
1959 if (res == AnalysisResult::Peel) {
1960 // 1 for the peeled iteration,
1961 // 1 for the loop (since we are not unrolling it)
1962 unrollingFactor = 1 + 1;
1963 } else if (res == AnalysisResult::Unroll) {
1964 // No peeled iteration, we unroll only
1965 unrollingFactor = 0 + basicUnrollingFactor;
1966 } else if (res == AnalysisResult::PeelAndUnroll) {
1967 // 1 peeled iteration, and we unroll the loop
1968 unrollingFactor = 1 + basicUnrollingFactor;
1969 } else {
1970 MOZ_CRASH();
1971 }
1972
1973 // Set up UnrollState::blockTable and copy in the initial loop blocks.
1974 if (!state->blockTable.init(unrollingFactor, originalBlocks.length())) {
1975 return false;
1976 }
1977 for (uint32_t bix = 0; bix < originalBlocks.length(); bix++) {
1978 state->blockTable.set(0, bix, originalBlocks[bix]);
1979 }
1980 }
1981
1982 // Now, finally, we have an initialized vector of UnrollStates ready to go.
1983 // After this point, `originalBlocks` is no longer used. Also, because of
1984 // the checking done in AnalyzeLoop, we know the transformations can't fail
1985 // (apart from OOMing).
1986
1987 // Add "loop-closing phis" for values defined in the loop and used
1988 // afterwards. See comments on AddClosingPhisForLoop.
1989 for (const UnrollState& state : unrollStates) {
1990 if (!AddClosingPhisForLoop(graph.alloc(), state)) {
1991 return false;
1992 }
1993 }
1994
1995 // Actually do the unrolling and/or peeling.
1996 uint32_t numLoopsPeeled = 0;
1997 uint32_t numLoopsUnrolled = 0;
1998 uint32_t numLoopsPeeledAndUnrolled = 0;
1999 for (UnrollState& state : unrollStates) {
2000 if (!UnrollAndOrPeelLoop(graph, state)) {
2001 return false;
2002 }
2003 // Update stats.
2004 switch (state.mode) {
2005 case UnrollMode::Peel:
2006 numLoopsPeeled++;
2007 break;
2008 case UnrollMode::Unroll:
2009 numLoopsUnrolled++;
2010 break;
2011 case UnrollMode::PeelAndUnroll:
2012 numLoopsPeeledAndUnrolled++;
2013 break;
2014 default:
2015 MOZ_CRASH();
2016 }
2017 }
2018
2019 // Do function-level fixups, if anything changed.
2020 if (!unrollStates.empty()) {
2021 RenumberBlocks(graph);
2022 ClearDominatorTree(graph);
2023 if (!BuildDominatorTree(mir, graph)) {
2024 return false;
2025 }
2026 }
2027
2028 uint32_t numLoopsChanged =
2029 numLoopsPeeled + numLoopsUnrolled + numLoopsPeeledAndUnrolled;
2030
2031 #ifdef JS_JITSPEW
2032 if (JitSpewEnabled(JitSpew_Unroll)) {
2033 if (numLoopsChanged == 0) {
2034 JitSpew(JitSpew_Unroll, "END UnrollLoops");
2035 } else {
2036 JitSpew(JitSpew_Unroll,
2037 "END UnrollLoops, %u processed (P=%u, U=%u, P&U=%u)",
2038 numLoopsChanged, numLoopsPeeled, numLoopsUnrolled,
2039 numLoopsPeeledAndUnrolled);
2040 }
2041 }
2042 #endif
2043
2044 *changed = numLoopsChanged > 0;
2045 return true;
2046 }
2047
2048 } // namespace jit
2049 } // namespace js