tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

BacktrackingAllocator.cpp (184110B)


      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 ///////////////////////////////////////////////////////////////////////////////
      8 //                                                                           //
      9 // Documentation.  Code starts about 670 lines down from here.               //
     10 //                                                                           //
     11 ///////////////////////////////////////////////////////////////////////////////
     12 
     13 // [SMDOC] An overview of Ion's register allocator
     14 //
     15 // The intent of this documentation is to give maintainers a map with which to
     16 // navigate the allocator.  As further understanding is obtained, it should be
     17 // added to this overview.
     18 //
     19 // Where possible, invariants are stated and are marked "(INVAR)".  Many
     20 // details are omitted because their workings are currently unknown.  In
     21 // particular, this overview doesn't explain how Intel-style "modify" (tied)
     22 // operands are handled.  Facts or invariants that are speculative -- believed
     23 // to be true, but not verified at the time of writing -- are marked "(SPEC)".
     24 //
     25 // The various concepts are interdependent, so a single forwards reading of the
     26 // following won't make much sense.  Many concepts are explained only after
     27 // they are mentioned.
     28 //
     29 // Where possible examples are shown.  Without those the description is
     30 // excessively abstract.
     31 //
     32 // Names of the form ::name mean BacktrackingAllocator::name.
     33 //
     34 // The description falls into two sections:
     35 //
     36 // * Section 1: A tour of the data structures
     37 // * Section 2: The core allocation loop, and bundle splitting
     38 //
     39 // The allocator sometimes produces poor allocations, with excessive spilling
     40 // and register-to-register moves (bugs 1752520, bug 1714280 bug 1746596).
     41 // Work in bug 1752582 shows we can get better quality allocations from this
     42 // framework without having to make any large (conceptual) changes, by having
     43 // better splitting heuristics.
     44 //
     45 // At https://bugzilla.mozilla.org/show_bug.cgi?id=1758274#c17
     46 // (https://bugzilla.mozilla.org/attachment.cgi?id=9288467) is a document
     47 // written at the same time as these comments.  It describes some improvements
     48 // we could make to our splitting heuristics, particularly in the presence of
     49 // loops and calls, and shows why the current implementation sometimes produces
     50 // excessive spilling.  It builds on the commentary in this SMDOC.
     51 //
     52 //
     53 // Top level pipeline
     54 // ~~~~~~~~~~~~~~~~~~
     55 // There are three major phases in allocation.  They run sequentially, at a
     56 // per-function granularity.
     57 //
     58 // (1) Liveness analysis and bundle formation
     59 // (2) Bundle allocation and last-chance allocation
     60 // (3) Rewriting the function to create MoveGroups and to "install"
     61 //     the allocation
     62 //
     63 // The input language (LIR) is in SSA form, and phases (1) and (3) depend on
     64 // that SSAness.  Without it the allocator wouldn't work.
     65 //
     66 // The top level function is ::go.  The phases are divided into functions as
     67 // follows:
     68 //
     69 // (1) ::buildLivenessInfo, ::mergeAndQueueRegisters
     70 // (2) ::processBundle, ::tryAllocatingRegistersForSpillBundles,
     71 //     ::pickStackSlots
     72 // (3) ::createMoveGroupsFromLiveRangeTransitions, ::installAllocationsInLIR,
     73 //     ::populateSafepoints, ::annotateMoveGroups
     74 //
     75 // The code in this file is structured as much as possible in the same sequence
     76 // as flow through the pipeline.  Hence, top level function ::go is right at
     77 // the end.  Where a function depends on helper function(s), the helpers appear
     78 // first.
     79 //
     80 //
     81 // ========================================================================
     82 // ====                                                                ====
     83 // ==== Section 1: A tour of the data structures                       ====
     84 // ====                                                                ====
     85 // ========================================================================
     86 //
     87 // Here are the key data structures necessary for understanding what follows.
     88 //
     89 // Some basic data structures
     90 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
     91 //
     92 // CodePosition
     93 // ------------
     94 // A CodePosition is an unsigned 32-bit int that indicates an instruction index
     95 // in the incoming LIR.  Each LIR actually has two code positions, one to
     96 // denote the "input" point (where, one might imagine, the operands are read,
     97 // at least useAtStart ones) and the "output" point, where operands are
     98 // written.  Eg:
     99 //
    100 //    Block 0 [successor 2] [successor 1]
    101 //      2-3 WasmParameter [def v1<g>:r14]
    102 //      4-5 WasmCall [use v1:F:r14]
    103 //      6-7 WasmLoadTls [def v2<o>] [use v1:R]
    104 //      8-9 WasmNullConstant [def v3<o>]
    105 //      10-11 Compare [def v4<i>] [use v2:R] [use v3:A]
    106 //      12-13 TestIAndBranch [use v4:R]
    107 //
    108 // So for example the WasmLoadTls insn has its input CodePosition as 6 and
    109 // output point as 7.  Input points are even numbered, output points are odd
    110 // numbered.  CodePositions 0 and 1 never appear, because LIR instruction IDs
    111 // start at 1.  Indeed, CodePosition 0 is assumed to be invalid and hence is
    112 // used as a marker for "unusual conditions" in various places.
    113 //
    114 // Phi nodes exist in the instruction stream too.  They always appear at the
    115 // start of blocks (of course) (SPEC), but their start and end points are
    116 // printed for the group as a whole.  This is to emphasise that they are really
    117 // parallel assignments and that printing them sequentially would misleadingly
    118 // imply that they are executed sequentially.  Example:
    119 //
    120 //    Block 6 [successor 7] [successor 8]
    121 //      56-59 Phi [def v19<o>] [use v2:A] [use v5:A] [use v13:A]
    122 //      56-59 Phi [def v20<o>] [use v7:A] [use v14:A] [use v12:A]
    123 //      60-61 WasmLoadSlot [def v21<o>] [use v1:R]
    124 //      62-63 Compare [def v22<i>] [use v20:R] [use v21:A]
    125 //      64-65 TestIAndBranch [use v22:R]
    126 //
    127 // See that both Phis are printed with limits 56-59, even though they are
    128 // stored in the LIR array like regular LIRs and so have code points 56-57 and
    129 // 58-59 in reality.
    130 //
    131 // The process of allocation adds MoveGroup LIRs to the function.  Each
    132 // incoming LIR has its own private list of MoveGroups (actually, 3 lists; two
    133 // for moves that conceptually take place before the instruction, and one for
    134 // moves after it).  Hence the CodePositions for LIRs (the "62-63", etc, above)
    135 // do not change as a result of allocation.
    136 //
    137 // Virtual registers (vregs) in LIR
    138 // --------------------------------
    139 // The MIR from which the LIR is derived, is standard SSA.  That SSAness is
    140 // carried through into the LIR (SPEC).  In the examples here, LIR SSA names
    141 // (virtual registers, a.k.a. vregs) are printed as "v<number>".  v0 never
    142 // appears and is presumed to be a special value, perhaps "invalid" (SPEC).
    143 //
    144 // The allocator core has a type VirtualRegister, but this is private to the
    145 // allocator and not part of the LIR.  It carries far more information than
    146 // merely the name of the vreg.  The allocator creates one VirtualRegister
    147 // structure for each vreg in the LIR.
    148 //
    149 // LDefinition and LUse
    150 // --------------------
    151 // These are part of the incoming LIR.  Each LIR instruction defines zero or
    152 // more values, and contains one LDefinition for each defined value (SPEC).
    153 // Each instruction has zero or more input operands, each of which has its own
    154 // LUse (SPEC).
    155 //
    156 // Both LDefinition and LUse hold both a virtual register name and, in general,
    157 // a real (physical) register identity.  The incoming LIR has the real register
    158 // fields unset, except in places where the incoming LIR has fixed register
    159 // constraints (SPEC).  Phase 3 of allocation will visit all of the
    160 // LDefinitions and LUses so as to write into the real register fields the
    161 // decisions made by the allocator.  For LUses, this is done by overwriting the
    162 // complete LUse with a different LAllocation, for example LStackSlot.  That's
    163 // possible because LUse is a child class of LAllocation.
    164 //
    165 // This action of reading and then later updating LDefinition/LUses is the core
    166 // of the allocator's interface to the outside world.
    167 //
    168 // To make visiting of LDefinitions/LUses possible, the allocator doesn't work
    169 // with LDefinition and LUse directly.  Rather it has pointers to them
    170 // (VirtualRegister::def_, UsePosition::use_).  Hence Phase 3 can modify the
    171 // LIR in-place.
    172 //
    173 // (INVARs, all SPEC):
    174 //
    175 // - The collective VirtualRegister::def_ values should be unique, and there
    176 //   should be a 1:1 mapping between the VirtualRegister::def_ values and the
    177 //   LDefinitions in the LIR.  (So that the LIR LDefinition has exactly one
    178 //   VirtualRegister::def_ to track it).  But only for the valid LDefinitions.
    179 //   If isBogusTemp() is true, the definition is invalid and doesn't have a
    180 //   vreg.
    181 //
    182 // - The same for uses: there must be a 1:1 correspondence between the
    183 //   CodePosition::use_ values and the LIR LUses.
    184 //
    185 // - The allocation process must preserve these 1:1 mappings.  That implies
    186 //   (weaker) that the number of VirtualRegisters and of UsePositions must
    187 //   remain constant through allocation.  (Eg: losing them would mean that some
    188 //   LIR def or use would necessarily not get annotated with its final
    189 //   allocation decision.  Duplicating them would lead to the possibility of
    190 //   conflicting allocation decisions.)
    191 //
    192 // Other comments regarding LIR
    193 // ----------------------------
    194 // The incoming LIR is structured into basic blocks and a CFG, as one would
    195 // expect.  These (insns, block boundaries, block edges etc) are available
    196 // through the BacktrackingAllocator object.  They are important for Phases 1
    197 // and 3 but not for Phase 2.
    198 //
    199 // Phase 3 "rewrites" the input LIR so as to "install" the final allocation.
    200 // It has to insert MoveGroup instructions, but that isn't done by pushing them
    201 // into the instruction array.  Rather, each LIR has 3 auxiliary sets of
    202 // MoveGroups (SPEC): two that "happen" conceptually before the LIR, and one
    203 // that happens after it.  The rewriter inserts MoveGroups into one of these 3
    204 // sets, and later code generation phases presumably insert the sets (suitably
    205 // translated) into the final machine code (SPEC).
    206 //
    207 //
    208 // Key data structures: LiveRange, VirtualRegister and LiveBundle
    209 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    210 //
    211 // These three have central roles in allocation.  Of them, LiveRange is the
    212 // most central.  VirtualRegister is conceptually important throughout, but
    213 // appears less frequently in the allocator code.  LiveBundle is important only
    214 // in Phase 2 (where it is central) and at the end of Phase 1, but plays no
    215 // role in Phase 3.
    216 //
    217 // It's important to understand that LiveRange and VirtualRegister correspond
    218 // to concepts visible in the incoming LIR, which is in SSA form.  LiveBundle
    219 // by comparison is related to neither the structure of LIR nor its SSA
    220 // properties.  Instead, LiveBundle is an essentially adminstrative structure
    221 // used to accelerate allocation and to implement a crude form of
    222 // move-coalescing.
    223 //
    224 // VirtualRegisters and LiveRanges are (almost) static throughout the process,
    225 // because they reflect aspects of the incoming LIR, which does not change.
    226 // LiveBundles by contrast come and go; they are created, but may be split up
    227 // into new bundles, and old ones abandoned.
    228 //
    229 // Each LiveRange is a member of two different lists.
    230 //
    231 // A VirtualRegister (described in detail below) has a vector of LiveRanges that
    232 // it "owns".
    233 //
    234 // A LiveBundle (also described below) has a linked list of LiveRanges that it
    235 // "owns".
    236 //
    237 // Hence each LiveRange is "owned" by one VirtualRegister and one LiveBundle.
    238 // LiveRanges may have their owning LiveBundle changed as a result of
    239 // splitting.  By contrast a LiveRange stays with its "owning" VirtualRegister
    240 // for ever.
    241 //
    242 // A few LiveRanges have no VirtualRegister.  This is used to implement
    243 // register spilling for calls.  Each physical register that's not preserved
    244 // across a call has a small range that covers the call.  It is
    245 // ::buildLivenessInfo that adds these small ranges.
    246 //
    247 // Iterating over every VirtualRegister in the system is a common operation and
    248 // is straightforward because (somewhat redundantly?) the LIRGraph knows the
    249 // number of vregs, and more importantly because BacktrackingAllocator::vregs
    250 // is a vector of all VirtualRegisters.  By contrast iterating over every
    251 // LiveBundle in the system is more complex because there is no top-level
    252 // registry of them.  It is still possible though.  See ::dumpLiveRangesByVReg
    253 // and ::dumpLiveRangesByBundle for example code.
    254 //
    255 // LiveRange
    256 // ---------
    257 // Fundamentally, a LiveRange (often written just "range") is a request for
    258 // storage of a LIR vreg for some contiguous sequence of LIRs.  A LiveRange
    259 // generally covers only a fraction of a vreg's overall lifetime, so multiple
    260 // LiveRanges are generally needed to cover the whole lifetime.
    261 //
    262 // A LiveRange contains (amongst other things):
    263 //
    264 // * the vreg for which it is for, as a VirtualRegister*
    265 //
    266 // * the range of CodePositions for which it is for, as a LiveRange::Range
    267 //
    268 // * auxiliary information:
    269 //
    270 //   - a boolean that indicates whether this LiveRange defines a value for the
    271 //     vreg.  If so, that definition is regarded as taking place at the first
    272 //     CodePoint of the range.
    273 //
    274 //   - a linked list of uses of the vreg within this range.  Each use is a pair
    275 //     of a CodePosition and an LUse*.  (INVAR): the uses are maintained in
    276 //     increasing order of CodePosition.  Multiple uses at the same
    277 //     CodePosition are permitted, since that is necessary to represent an LIR
    278 //     that uses the same vreg in more than one of its operand slots.
    279 //
    280 // Some important facts about LiveRanges are best illustrated with examples:
    281 //
    282 //   v25 75-82 { 75_def:R 78_v25:A 82_v25:A }
    283 //
    284 // This LiveRange is for vreg v25.  The value is defined at CodePosition 75,
    285 // with the LIR requiring that it be in a register.  It is used twice at
    286 // positions 78 and 82, both with no constraints (A meaning "any").  The range
    287 // runs from position 75 to 82 inclusive.  Note however that LiveRange::Range
    288 // uses non-inclusive range ends; hence its .to field would be 83, not 82.
    289 //
    290 //   v26 84-85 { 85_v26:R }
    291 //
    292 // This LiveRange is for vreg v26.  Here, there's only a single use of it at
    293 // position 85.  Presumably it is defined in some other LiveRange.
    294 //
    295 //   v19 74-79 { }
    296 //
    297 // This LiveRange is for vreg v19.  There is no def and no uses, so at first
    298 // glance this seems redundant.  But it isn't: it still expresses a request for
    299 // storage for v19 across 74-79, because Phase 1 regards v19 as being live in
    300 // this range (meaning: having a value that, if changed in this range, would
    301 // cause the program to fail).
    302 //
    303 // Other points:
    304 //
    305 // * (INVAR) Each vreg/VirtualRegister has at least one LiveRange.
    306 //
    307 // * (INVAR) Exactly one LiveRange of a vreg gives a definition for the value.
    308 //   All other LiveRanges must consist only of uses (including zero uses, for a
    309 //   "flow-though" range as mentioned above).  This requirement follows from
    310 //   the fact that LIR is in SSA form.
    311 //
    312 // * It follows from this, that the LiveRanges for a VirtualRegister must form
    313 //   a tree, where the parent-child relationship is "control flows directly
    314 //   from a parent LiveRange (anywhere in the LiveRange) to a child LiveRange
    315 //   (start)".  The entire tree carries only one value.  This is a use of
    316 //   SSAness in the allocator which is fundamental: without SSA input, this
    317 //   design would not work.
    318 //
    319 //   The root node (LiveRange) in the tree must be one that defines the value,
    320 //   and all other nodes must only use or be flow-throughs for the value.  It's
    321 //   OK for LiveRanges in the tree to overlap, providing that there is a unique
    322 //   root node -- otherwise it would be unclear which LiveRange provides the
    323 //   value.
    324 //
    325 //   The function ::createMoveGroupsFromLiveRangeTransitions runs after all
    326 //   LiveBundles have been allocated.  It visits each VirtualRegister tree in
    327 //   turn.  For every parent->child edge in a tree, it creates a MoveGroup that
    328 //   copies the value from the parent into the child -- this is how the
    329 //   allocator decides where to put MoveGroups.  There are various other
    330 //   details not described here.
    331 //
    332 // * It's important to understand that a LiveRange carries no meaning about
    333 //   control flow beyond that implied by the SSA (hence, dominance)
    334 //   relationship between a def and its uses.  In particular, there's no
    335 //   implication that execution "flowing into" the start of the range implies
    336 //   that it will "flow out" of the end.  Or that any particular use will or
    337 //   will not be executed.
    338 //
    339 // * (very SPEC) Indeed, even if a range has a def, there's no implication that
    340 //   a use later in the range will have been immediately preceded by execution
    341 //   of the def.  It could be that the def is executed, flow jumps somewhere
    342 //   else, and later jumps back into the middle of the range, where there are
    343 //   then some uses.
    344 //
    345 // * Uses of a vreg by a phi node are not mentioned in the use list of a
    346 //   LiveRange.  The reasons for this are unknown, but it is speculated that
    347 //   this is because we don't need to know about phi uses where we use the list
    348 //   of positions.  See comments on VirtualRegister::usedByPhi_.
    349 //
    350 // * Similarly, a definition of a vreg by a phi node is not regarded as being a
    351 //   definition point (why not?), at least as the view of
    352 //   LiveRange::hasDefinition_.
    353 //
    354 // * LiveRanges that nevertheless include a phi-defined value have their first
    355 //   point set to the first of the block of phis, even if the var isn't defined
    356 //   by that specific phi.  Eg:
    357 //
    358 //    Block 6 [successor 7] [successor 8]
    359 //      56-59 Phi [def v19<o>] [use v2:A] [use v5:A] [use v13:A]
    360 //      56-59 Phi [def v20<o>] [use v7:A] [use v14:A] [use v12:A]
    361 //      60-61 WasmLoadSlot [def v21<o>] [use v1:R]
    362 //      62-63 Compare [def v22<i>] [use v20:R] [use v21:A]
    363 //
    364 //   The relevant live range for v20 is
    365 //
    366 //    v20 56-65 { 63_v20:R }
    367 //
    368 //   Observe that it starts at 56, not 58.
    369 //
    370 // VirtualRegister
    371 // ---------------
    372 // Each VirtualRegister is associated with an SSA value created by the LIR.
    373 // Fundamentally it is a container to hold all of the LiveRanges that together
    374 // indicate where the value must be kept live.  The live ranges are stored in a
    375 // vector, VirtualRegister::ranges_.  The set of LiveRanges must logically form
    376 // a tree, rooted at the LiveRange which defines the value.
    377 //
    378 // The live ranges are sorted in order of decreasing start point by construction
    379 // after buildLivenessInfo until the main register allocation loops.  At that
    380 // point they can become unsorted and this is important for performance because
    381 // it allows the core allocation code to add/remove ranges more efficiently.
    382 // After all bundles are allocated, sortVirtualRegisterRanges is called to
    383 // ensure the ranges are sorted again.
    384 //
    385 // There are various auxiliary fields, most importantly the LIR node and the
    386 // associated LDefinition that define the value.
    387 //
    388 // It is OK, and quite common, for LiveRanges of a VirtualRegister to overlap.
    389 // The effect will be that, in an overlapped area, there are two storage
    390 // locations holding the value.  This is OK -- although wasteful of storage
    391 // resources -- because the SSAness means the value must be the same in both
    392 // locations.  Hence there's no questions like "which LiveRange holds the most
    393 // up-to-date value?", since it's all just one value anyway.
    394 //
    395 // Note by contrast, it is *not* OK for the LiveRanges of a LiveBundle to
    396 // overlap.
    397 //
    398 // LiveBundle
    399 // ----------
    400 // Similar to VirtualRegister, a LiveBundle is also, fundamentally, a container
    401 // for a set of LiveRanges.  The set is stored as a linked list, rooted at
    402 // LiveBundle::ranges_.
    403 //
    404 // However, the similarity ends there:
    405 //
    406 // * The LiveRanges in a LiveBundle absolutely must not overlap.  They must
    407 //   indicate disjoint sets of CodePositions, and must be stored in the list in
    408 //   order of increasing CodePosition.  Because of the no-overlap requirement,
    409 //   these ranges form a total ordering regardless of whether one uses the
    410 //   LiveRange::Range::from_ or ::to_ fields for comparison.
    411 //
    412 // * The LiveRanges in a LiveBundle can otherwise be entirely arbitrary and
    413 //   unrelated.  They can be from different VirtualRegisters and can have no
    414 //   particular mutual significance w.r.t. the SSAness or structure of the
    415 //   input LIR.
    416 //
    417 // LiveBundles are the fundamental unit of allocation.  The allocator attempts
    418 // to find a single storage location that will work for all LiveRanges in the
    419 // bundle.  That's why the ranges must not overlap.  If no such location can be
    420 // found, the allocator may decide to split the bundle into multiple smaller
    421 // bundles.  Each of those may be allocated independently.
    422 //
    423 // The other really important field is LiveBundle::alloc_, indicating the
    424 // chosen storage location.
    425 //
    426 // Here's an example, for a LiveBundle before it has been allocated:
    427 //
    428 //   LB2(parent=none v3 8-21 { 16_v3:A } ## v3 24-25 { 25_v3:F:xmm0 })
    429 //
    430 // LB merely indicates "LiveBundle", and the 2 is the id_ value (see
    431 // below).  This bundle has two LiveRanges
    432 //
    433 //   v3 8-21 { 16_v3:A }
    434 //   v3 24-25 { 25_v3:F:xmm0 }
    435 //
    436 // both of which (coincidentally) are for the same VirtualRegister, v3.The
    437 // second LiveRange has a fixed use in `xmm0`, whilst the first one doesn't
    438 // care (A meaning "any location") so the allocator *could* choose `xmm0` for
    439 // the bundle as a whole.
    440 //
    441 // One might ask: why bother with LiveBundle at all?  After all, it would be
    442 // possible to get correct allocations by allocating each LiveRange
    443 // individually, then leaving ::createMoveGroupsFromLiveRangeTransitions to add
    444 // MoveGroups to join up LiveRanges that form each SSA value tree (that is,
    445 // LiveRanges belonging to each VirtualRegister).
    446 //
    447 // There are two reasons:
    448 //
    449 // (1) By putting multiple LiveRanges into each LiveBundle, we can end up with
    450 //     many fewer LiveBundles than LiveRanges.  Since the cost of allocating a
    451 //     LiveBundle is substantially less than the cost of allocating each of its
    452 //     LiveRanges individually, the allocator will run faster.
    453 //
    454 // (2) It provides a crude form of move-coalescing.  There are situations where
    455 //     it would be beneficial, although not mandatory, to have two LiveRanges
    456 //     assigned to the same storage unit.  Most importantly: (a) LiveRanges
    457 //     that form all of the inputs, and the output, of a phi node.  (b)
    458 //     LiveRanges for the output and first-operand values in the case where we
    459 //     are targetting Intel-style instructions.
    460 //
    461 //     In such cases, if the bundle can be allocated as-is, then no extra moves
    462 //     are necessary.  If not (and the bundle is split), then
    463 //     ::createMoveGroupsFromLiveRangeTransitions will later fix things up by
    464 //     inserting MoveGroups in the right places.
    465 //
    466 // Merging of LiveRanges into LiveBundles is done in Phase 1, by
    467 // ::mergeAndQueueRegisters, after liveness analysis (which generates only
    468 // LiveRanges).
    469 //
    470 // For the bundle mentioned above, viz
    471 //
    472 //   LB2(parent=none v3 8-21 { 16_v3:A } ## v3 24-25 { 25_v3:F:xmm0 })
    473 //
    474 // the allocator did not in the end choose to allocate it to `xmm0`.  Instead
    475 // it was split into two bundles, LB6 (a "spill parent", or root node in the
    476 // trees described above), and LB9, a leaf node, that points to its parent LB6:
    477 //
    478 //   LB6(parent=none v3 8-21 %xmm1.s { 16_v3:A } ## v3 24-25 %xmm1.s { })
    479 //   LB9(parent=LB6 v3 24-25 %xmm0.s { 25_v3:F:rax })
    480 //
    481 // Note that both bundles now have an allocation, and that is printed,
    482 // redundantly, for each LiveRange in the bundle -- hence the repeated
    483 // `%xmm1.s` in the lines above.  Since all LiveRanges in a LiveBundle must be
    484 // allocated to the same storage location, we never expect to see output like
    485 // this
    486 //
    487 //   LB6(parent=none v3 8-21 %xmm1.s { 16_v3:A } ## v3 24-25 %xmm2.s { })
    488 //
    489 // and that is in any case impossible, since a LiveRange doesn't have an
    490 // LAllocation field.  Instead it has a pointer to its LiveBundle, and the
    491 // LAllocation lives in the LiveBundle.
    492 //
    493 // For the resulting allocation (LB6 and LB9), all the LiveRanges are use-only
    494 // or flow-through.  There are no definitions.  But they are all for the same
    495 // VirtualReg, v3, so they all have the same value.  An important question is
    496 // where they "get their value from".  The answer is that
    497 // ::createMoveGroupsFromLiveRangeTransitions will have to insert suitable
    498 // MoveGroups so that each LiveRange for v3 can "acquire" the value from a
    499 // previously-executed LiveRange, except for the range that defines it.  The
    500 // defining LiveRange is not visible here; either it is in some other
    501 // LiveBundle, not shown, or (more likely) the value is defined by a phi-node,
    502 // in which case, as mentioned previously, it is not shown as having an
    503 // explicit definition in any LiveRange.
    504 //
    505 // LiveBundles also have a `SpillSet* spill_` field (see below) and a
    506 // `LiveBundle* spillParent_`.  The latter is used to ensure that all bundles
    507 // originating from an "original" bundle share the same spill slot.  The
    508 // `spillParent_` pointers can be thought of creating a 1-level tree, where
    509 // each node points at its parent.  Since the tree can be only 1-level, the
    510 // following invariant (INVAR) must be maintained:
    511 //
    512 // * if a LiveBundle has a non-null spillParent_ field, then it is a leaf node,
    513 //   and no other LiveBundle can point at this one.
    514 //
    515 // * else (it has a null spillParent_ field) it is a root node, and so other
    516 //   LiveBundles may point at it.
    517 //
    518 // LiveBundle has a 32-bit `id_` field. This is used for debug printing, and
    519 // makes it easier to see parent-child relationships induced by the
    520 // `spillParent_` pointers. It's also used for sorting live ranges in
    521 // VirtualRegister::sortRanges.
    522 //
    523 // The "life cycle" of LiveBundles is described in Section 2 below.
    524 //
    525 //
    526 // Secondary data structures: SpillSet, Requirement
    527 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    528 //
    529 // SpillSet
    530 // --------
    531 // A SpillSet is a container for a set of LiveBundles that have been spilled,
    532 // all of which are assigned the same spill slot.  The set is represented as a
    533 // vector of points to LiveBundles.  SpillSet also contains the identity of the
    534 // spill slot (its LAllocation).
    535 //
    536 // A LiveBundle, if it is to be spilled, has a pointer to the relevant
    537 // SpillSet, and the SpillSet in turn has a pointer back to the LiveBundle in
    538 // its vector thereof.  So LiveBundles (that are to be spilled) and SpillSets
    539 // point at each other.
    540 //
    541 // (SPEC) LiveBundles that are not to be spilled (or for which the decision has
    542 // yet to be made, have their SpillSet pointers as null.  (/SPEC)
    543 //
    544 // Requirement
    545 // -----------
    546 // Requirements are used transiently during the main allocation loop.  It
    547 // summarises the set of constraints on storage location (must be any register,
    548 // must be this specific register, must be stack, etc) for a LiveBundle.  This
    549 // is so that the main allocation loop knows what kind of storage location it
    550 // must choose in order to satisfy all of the defs and uses within the bundle.
    551 //
    552 // What Requirement provides is (a) a partially ordered set of locations, and
    553 // (b) a constraint-merging method `merge`.
    554 //
    555 // Requirement needs a rewrite (and, in fact, that has already happened in
    556 // un-landed code in bug 1758274) for the following reasons:
    557 //
    558 // * it's potentially buggy (bug 1761654), although that doesn't currently
    559 //   affect us, for reasons which are unclear.
    560 //
    561 // * the partially ordered set has a top point, meaning "no constraint", but it
    562 //   doesn't have a corresponding bottom point, meaning "impossible
    563 //   constraints".  (So it's a partially ordered set, but not a lattice).  This
    564 //   leads to awkward coding in some places, which would be simplified if there
    565 //   were an explicit way to indicate "impossible constraint".
    566 //
    567 //
    568 // Some ASCII art
    569 // ~~~~~~~~~~~~~~
    570 //
    571 // Here's some not-very-good ASCII art that tries to summarise the data
    572 // structures that persist for the entire allocation of a function:
    573 //
    574 //     BacktrackingAllocator
    575 //        |
    576 //      (vregs)
    577 //        |
    578 //        v
    579 //        |
    580 //     VirtualRegister -->--(ins)--> LNode
    581 //     |            |  `->--(def)--> LDefinition
    582 //     v            ^
    583 //     |            |
    584 // (ranges vector)  |
    585 //     |            |
    586 //     |          (vreg)
    587 //     `--v->--.    |
    588 //              \   |
    589 //               LiveRange -->--b-->-- LiveRange -->--b-->-- NULL
    590 //              /    |
    591 //     ,--b->--'    /
    592 //     |         (bundle)
    593 //     ^          /
    594 //     |         v
    595 //  (ranges)    /
    596 //     |       /
    597 //     LiveBundle --s-->- LiveBundle
    598 //       |      \           /    |
    599 //       |       \         /     |
    600 //      (spill)   ^       ^     (spill)
    601 //       |         \     /       |
    602 //       v          \   /        ^
    603 //       |          (list)       |
    604 //       \            |          /
    605 //        `--->---> SpillSet <--'
    606 //
    607 // --b-- LiveRange::next_: links in the list of LiveRanges that belong to
    608 //       a LiveBundle
    609 //
    610 // --v-- VirtualRegister::ranges_: vector of LiveRanges that belong to a
    611 //       VirtualRegister
    612 //
    613 // --s-- LiveBundle::spillParent: a possible link to my "spill parent bundle"
    614 //
    615 //
    616 // * LiveRange is in the center.  Each LiveRange is a member of a linked list,
    617 //   the --b-- list, and is also stored in VirtualRegister's `ranges` vector.
    618 //
    619 // * VirtualRegister has a `ranges` vector that contains its LiveRanges.
    620 //
    621 // * LiveBundle has a pointer `ranges` that points to the start of its --b--
    622 //   list of LiveRanges.
    623 //
    624 // * LiveRange points back at both its owning VirtualRegister (`vreg`) and its
    625 //   owning LiveBundle (`bundle`).
    626 //
    627 // * LiveBundle has a pointer --s-- `spillParent`, which may be null, to its
    628 //   conceptual "spill parent bundle", as discussed in detail above.
    629 //
    630 // * LiveBundle has a pointer `spill` to its SpillSet.
    631 //
    632 // * SpillSet has a vector `list` of pointers back to the LiveBundles that
    633 //   point at it.
    634 //
    635 // * VirtualRegister has pointers `ins` to the LNode that defines the value and
    636 //   `def` to the LDefinition within that LNode.
    637 //
    638 // * BacktrackingAllocator has a vector `vregs` of pointers to all the
    639 //   VirtualRegisters for the function.  There is no equivalent top-level table
    640 //   of all the LiveBundles for the function.
    641 //
    642 // Note that none of these pointers are "owning" in the C++-storage-management
    643 // sense.  Rather, everything is stored in single arena which is freed when
    644 // compilation of the function is complete.  For this reason,
    645 // BacktrackingAllocator.{h,cpp} is almost completely free of the usual C++
    646 // storage-management artefacts one would normally expect to see.
    647 //
    648 //
    649 // ========================================================================
    650 // ====                                                                ====
    651 // ==== Section 2: The core allocation loop, and bundle splitting      ====
    652 // ====                                                                ====
    653 // ========================================================================
    654 //
    655 // Phase 1 of the allocator (described at the start of this SMDOC) computes
    656 // live ranges, merges them into bundles, and places the bundles in a priority
    657 // queue ::allocationQueue, ordered by what ::computePriority computes.
    658 //
    659 //
    660 // The allocation loops
    661 // ~~~~~~~~~~~~~~~~~~~~
    662 // The core of the allocation machinery consists of two loops followed by a
    663 // call to ::pickStackSlots.  The latter is uninteresting.  The two loops live
    664 // in ::go and are documented in detail there.
    665 //
    666 //
    667 // Bundle splitting
    668 // ~~~~~~~~~~~~~~~~
    669 // If the first of the two abovementioned loops cannot find a register for a
    670 // bundle, either directly or as a result of evicting conflicting bundles, then
    671 // it will have to either split or spill the bundle.  The entry point to the
    672 // split/spill subsystem is ::chooseBundleSplit.  See comments there.
    673 
    674 ///////////////////////////////////////////////////////////////////////////////
    675 //                                                                           //
    676 // End of documentation                                                      //
    677 //                                                                           //
    678 ///////////////////////////////////////////////////////////////////////////////
    679 
    680 #include "jit/BacktrackingAllocator.h"
    681 
    682 #include "mozilla/BinarySearch.h"
    683 #include "mozilla/Maybe.h"
    684 
    685 #include <algorithm>
    686 
    687 #include "jit/BitSet.h"
    688 #include "jit/CompileInfo.h"
    689 #include "js/Printf.h"
    690 
    691 using namespace js;
    692 using namespace js::jit;
    693 
    694 using mozilla::DebugOnly;
    695 using mozilla::Maybe;
    696 
    697 // This is a big, complex file.  Code is grouped into various sections, each
    698 // preceded by a box comment.  Sections not marked as "Misc helpers" are
    699 // pretty much the top level flow, and are presented roughly in the same order
    700 // in which the allocation pipeline operates.  BacktrackingAllocator::go,
    701 // right at the end of the file, is a good starting point.
    702 
    703 ///////////////////////////////////////////////////////////////////////////////
    704 //                                                                           //
    705 // Misc helpers: linked-list management                                      //
    706 //                                                                           //
    707 ///////////////////////////////////////////////////////////////////////////////
    708 
    709 static inline bool SortBefore(UsePosition* a, UsePosition* b) {
    710  return a->pos <= b->pos;
    711 }
    712 
    713 static inline bool SortBefore(LiveRange* a, LiveRange* b) {
    714  MOZ_ASSERT(!a->intersects(b));
    715  return a->from() < b->from();
    716 }
    717 
    718 template <typename T>
    719 static void InsertSortedList(InlineForwardList<T>& list, T* value,
    720                             T* startAt = nullptr) {
    721  if (list.empty()) {
    722    MOZ_ASSERT(!startAt);
    723    list.pushFront(value);
    724    return;
    725  }
    726 
    727 #ifdef DEBUG
    728  if (startAt) {
    729    // `startAt` must be an element in `list` that sorts before `value`.
    730    MOZ_ASSERT(SortBefore(startAt, value));
    731    MOZ_ASSERT_IF(*list.begin() == list.back(), list.back() == startAt);
    732    MOZ_ASSERT_IF(startAt != *list.begin(), SortBefore(*list.begin(), startAt));
    733    MOZ_ASSERT_IF(startAt != list.back(), SortBefore(startAt, list.back()));
    734  }
    735 #endif
    736 
    737  if (SortBefore(list.back(), value)) {
    738    list.pushBack(value);
    739    return;
    740  }
    741 
    742  // If `startAt` is non-nullptr, we can start iterating there.
    743  T* prev = nullptr;
    744  InlineForwardListIterator<T> iter =
    745      startAt ? list.begin(startAt) : list.begin();
    746  if (startAt) {
    747    // `value` must sort after `startAt` so skip to the next element.
    748    MOZ_ASSERT(!SortBefore(value, *iter));
    749    ++iter;
    750    prev = startAt;
    751  }
    752  for (; iter; iter++) {
    753    if (SortBefore(value, *iter)) {
    754      break;
    755    }
    756    prev = *iter;
    757  }
    758 
    759  if (prev) {
    760    list.insertAfter(prev, value);
    761  } else {
    762    list.pushFront(value);
    763  }
    764 }
    765 
    766 ///////////////////////////////////////////////////////////////////////////////
    767 //                                                                           //
    768 // Misc helpers: methods for class SpillSet                                  //
    769 //                                                                           //
    770 ///////////////////////////////////////////////////////////////////////////////
    771 
    772 void SpillSet::setAllocation(LAllocation alloc) {
    773  for (size_t i = 0; i < numSpilledBundles(); i++) {
    774    spilledBundle(i)->setAllocation(alloc);
    775  }
    776 }
    777 
    778 ///////////////////////////////////////////////////////////////////////////////
    779 //                                                                           //
    780 // Misc helpers: methods for class LiveRange                                 //
    781 //                                                                           //
    782 ///////////////////////////////////////////////////////////////////////////////
    783 
    784 static size_t SpillWeightFromUsePolicy(LUse::Policy policy) {
    785  switch (policy) {
    786    case LUse::ANY:
    787      return 1000;
    788 
    789    case LUse::REGISTER:
    790    case LUse::FIXED:
    791      return 2000;
    792 
    793    default:
    794      return 0;
    795  }
    796 }
    797 
    798 inline void LiveRange::noteAddedUse(UsePosition* use) {
    799  LUse::Policy policy = use->usePolicy();
    800  usesSpillWeight_ += SpillWeightFromUsePolicy(policy);
    801  if (policy == LUse::FIXED) {
    802    ++numFixedUses_;
    803  }
    804 }
    805 
    806 inline void LiveRange::noteRemovedUse(UsePosition* use) {
    807  LUse::Policy policy = use->usePolicy();
    808  usesSpillWeight_ -= SpillWeightFromUsePolicy(policy);
    809  if (policy == LUse::FIXED) {
    810    --numFixedUses_;
    811  }
    812  MOZ_ASSERT_IF(!hasUses(), !usesSpillWeight_ && !numFixedUses_);
    813 }
    814 
    815 void LiveRange::addUse(UsePosition* use) {
    816  MOZ_ASSERT(covers(use->pos));
    817  InsertSortedList(uses_, use);
    818  noteAddedUse(use);
    819 }
    820 
    821 UsePosition* LiveRange::popUse() {
    822  UsePosition* ret = uses_.popFront();
    823  noteRemovedUse(ret);
    824  return ret;
    825 }
    826 
    827 void LiveRange::tryToMoveDefAndUsesInto(LiveRange* other) {
    828  MOZ_ASSERT(&other->vreg() == &vreg());
    829  MOZ_ASSERT(this != other);
    830 
    831  // This method shouldn't be called for two non-intersecting live ranges
    832  // because it's a no-op in that case.
    833  MOZ_ASSERT(intersects(other));
    834 
    835  CodePosition otherFrom = other->from();
    836  CodePosition otherTo = other->to();
    837 
    838  // Copy the has-definition flag to |other|, if possible.
    839  if (hasDefinition() && from() == otherFrom) {
    840    other->setHasDefinition();
    841  }
    842 
    843  // If we have no uses, we're done.
    844  if (!hasUses()) {
    845    return;
    846  }
    847 
    848  // Fast path for when we can use |moveAllUsesToTheEndOf|. This is very common
    849  // for the non-Wasm code path in |trySplitAcrossHotcode|. This fast path had a
    850  // hit rate of 75% when running Octane in the JS shell.
    851  //
    852  // Note: the |!other->hasUses()| check could be more precise, but this didn't
    853  // improve the hit rate at all.
    854  if (!other->hasUses() && usesBegin()->pos >= otherFrom &&
    855      lastUse()->pos < otherTo) {
    856    moveAllUsesToTheEndOf(other);
    857    return;
    858  }
    859 
    860  // The uses are sorted by position, so first skip all uses before |other|
    861  // starts.
    862  UsePositionIterator iter = usesBegin();
    863  while (iter && iter->pos < otherFrom) {
    864    iter++;
    865  }
    866 
    867  // Move over all uses which fit in |other|'s boundaries.
    868  while (iter && iter->pos < otherTo) {
    869    UsePosition* use = *iter;
    870    MOZ_ASSERT(other->covers(use->pos));
    871    uses_.removeAndIncrement(iter);
    872    noteRemovedUse(use);
    873    other->addUse(use);
    874  }
    875 
    876  MOZ_ASSERT_IF(iter, !other->covers(iter->pos));
    877 }
    878 
    879 void LiveRange::moveAllUsesToTheEndOf(LiveRange* other) {
    880  MOZ_ASSERT(&other->vreg() == &vreg());
    881  MOZ_ASSERT(this != other);
    882  MOZ_ASSERT(intersects(other));
    883 
    884  if (uses_.empty()) {
    885    return;
    886  }
    887 
    888  // Assert |other| covers all of our uses.
    889  MOZ_ASSERT(other->covers(uses_.begin()->pos));
    890  MOZ_ASSERT(other->covers(uses_.back()->pos));
    891 
    892  // Assert |other->uses_| remains sorted after adding our uses at the end.
    893  MOZ_ASSERT_IF(!other->uses_.empty(),
    894                SortBefore(other->uses_.back(), *uses_.begin()));
    895 
    896  other->uses_.extendBack(std::move(uses_));
    897  MOZ_ASSERT(!hasUses());
    898 
    899  other->usesSpillWeight_ += usesSpillWeight_;
    900  other->numFixedUses_ += numFixedUses_;
    901  usesSpillWeight_ = 0;
    902  numFixedUses_ = 0;
    903 }
    904 
    905 bool LiveRange::contains(LiveRange* other) const {
    906  return from() <= other->from() && to() >= other->to();
    907 }
    908 
    909 void LiveRange::intersect(LiveRange* other, Range* pre, Range* inside,
    910                          Range* post) const {
    911  MOZ_ASSERT(pre->empty() && inside->empty() && post->empty());
    912 
    913  CodePosition innerFrom = from();
    914  if (from() < other->from()) {
    915    if (to() < other->from()) {
    916      *pre = range_;
    917      return;
    918    }
    919    *pre = Range(from(), other->from());
    920    innerFrom = other->from();
    921  }
    922 
    923  CodePosition innerTo = to();
    924  if (to() > other->to()) {
    925    if (from() >= other->to()) {
    926      *post = range_;
    927      return;
    928    }
    929    *post = Range(other->to(), to());
    930    innerTo = other->to();
    931  }
    932 
    933  if (innerFrom != innerTo) {
    934    *inside = Range(innerFrom, innerTo);
    935  }
    936 }
    937 
    938 bool LiveRange::intersects(LiveRange* other) const {
    939  Range pre, inside, post;
    940  intersect(other, &pre, &inside, &post);
    941  return !inside.empty();
    942 }
    943 
    944 ///////////////////////////////////////////////////////////////////////////////
    945 //                                                                           //
    946 // Misc helpers: methods for class LiveBundle                                //
    947 //                                                                           //
    948 ///////////////////////////////////////////////////////////////////////////////
    949 
    950 #ifdef DEBUG
    951 size_t LiveBundle::numRanges() const {
    952  size_t count = 0;
    953  for (RangeIterator iter = rangesBegin(); iter; iter++) {
    954    count++;
    955  }
    956  return count;
    957 }
    958 #endif
    959 
    960 LiveRange* LiveBundle::rangeFor(CodePosition pos) const {
    961  for (RangeIterator iter = rangesBegin(); iter; iter++) {
    962    LiveRange* range = *iter;
    963    if (range->covers(pos)) {
    964      return range;
    965    }
    966  }
    967  return nullptr;
    968 }
    969 
    970 void LiveBundle::addRange(LiveRange* range,
    971                          LiveRange* startAt /* = nullptr */) {
    972  MOZ_ASSERT(!range->bundle());
    973  MOZ_ASSERT(range->hasVreg());
    974  MOZ_ASSERT_IF(startAt, startAt->bundle() == this);
    975  range->setBundle(this);
    976  InsertSortedList(ranges_, range, startAt);
    977 }
    978 
    979 void LiveBundle::addRangeAtEnd(LiveRange* range) {
    980  // Note: this method is functionally equivalent to `addRange`, but can be used
    981  // when we know `range` must be added after all existing ranges in the bundle.
    982  MOZ_ASSERT(!range->bundle());
    983  MOZ_ASSERT(range->hasVreg());
    984  MOZ_ASSERT_IF(!ranges_.empty(), SortBefore(ranges_.back(), range));
    985  range->setBundle(this);
    986  ranges_.pushBack(range);
    987 }
    988 
    989 bool LiveBundle::addRangeAtEnd(TempAllocator& alloc, VirtualRegister* vreg,
    990                               CodePosition from, CodePosition to) {
    991  LiveRange* range = LiveRange::FallibleNew(alloc, vreg, from, to);
    992  if (!range) {
    993    return false;
    994  }
    995  addRangeAtEnd(range);
    996  return true;
    997 }
    998 
    999 bool LiveBundle::addRangeAndDistributeUses(TempAllocator& alloc,
   1000                                           LiveRange* oldRange,
   1001                                           CodePosition from, CodePosition to) {
   1002  LiveRange* range = LiveRange::FallibleNew(alloc, &oldRange->vreg(), from, to);
   1003  if (!range) {
   1004    return false;
   1005  }
   1006  addRange(range);
   1007  oldRange->tryToMoveDefAndUsesInto(range);
   1008  return true;
   1009 }
   1010 
   1011 LiveRange* LiveBundle::popFirstRange() {
   1012  RangeIterator iter = rangesBegin();
   1013  if (!iter) {
   1014    return nullptr;
   1015  }
   1016 
   1017  LiveRange* range = *iter;
   1018  ranges_.removeAt(iter);
   1019 
   1020  range->setBundle(nullptr);
   1021  return range;
   1022 }
   1023 
   1024 void LiveBundle::removeRange(LiveRange* range) {
   1025  for (RangeIterator iter = rangesBegin(); iter; iter++) {
   1026    LiveRange* existing = *iter;
   1027    if (existing == range) {
   1028      ranges_.removeAt(iter);
   1029      return;
   1030    }
   1031  }
   1032  MOZ_CRASH();
   1033 }
   1034 
   1035 void LiveBundle::removeAllRangesFromVirtualRegisters() {
   1036  VirtualRegister* prevVreg = nullptr;
   1037  for (RangeIterator iter = rangesBegin(); iter; iter++) {
   1038    LiveRange* range = *iter;
   1039    MOZ_ASSERT(!range->hasUses());
   1040    if (&range->vreg() != prevVreg) {
   1041      // As an optimization, remove all ranges owned by this bundle from the
   1042      // register's list, instead of a single range at a time.
   1043      range->vreg().removeRangesForBundle(this);
   1044      prevVreg = &range->vreg();
   1045    }
   1046  }
   1047 }
   1048 
   1049 ///////////////////////////////////////////////////////////////////////////////
   1050 //                                                                           //
   1051 // Misc helpers: methods for class VirtualRegister                           //
   1052 //                                                                           //
   1053 ///////////////////////////////////////////////////////////////////////////////
   1054 
   1055 bool VirtualRegister::addInitialRange(TempAllocator& alloc, CodePosition from,
   1056                                      CodePosition to) {
   1057  MOZ_ASSERT(rangesSorted_, "ranges stay sorted during live range building");
   1058 
   1059  MOZ_ASSERT(from < to);
   1060  MOZ_ASSERT_IF(hasRanges(), from < ranges_.back()->to());
   1061 
   1062  // Mark [from,to) as a live range for this register during the initial
   1063  // liveness analysis, coalescing with any existing overlapping ranges.
   1064 
   1065  LiveRange* merged = nullptr;
   1066  for (RangeIterator iter(*this); iter; iter++) {
   1067    LiveRange* existing = *iter;
   1068 
   1069    MOZ_ASSERT(from < existing->to());
   1070 
   1071    if (to < existing->from()) {
   1072      // All other ranges start after |to| so can't overlap.
   1073      break;
   1074    }
   1075 
   1076    if (!merged) {
   1077      // This is the first old range we've found that overlaps the new
   1078      // range. Extend this one to cover its union with the new range.
   1079      merged = existing;
   1080 
   1081      if (from < existing->from()) {
   1082        existing->setFrom(from);
   1083      }
   1084      if (to > existing->to()) {
   1085        existing->setTo(to);
   1086      }
   1087 
   1088      // Continue searching to see if any other old ranges can be
   1089      // coalesced with the new merged range.
   1090    } else {
   1091      // Coalesce this range into the previous range we merged into.
   1092      MOZ_ASSERT(existing->from() >= merged->from());
   1093      if (existing->to() > merged->to()) {
   1094        merged->setTo(existing->to());
   1095      }
   1096 
   1097      MOZ_ASSERT(!existing->hasDefinition());
   1098      existing->moveAllUsesToTheEndOf(merged);
   1099      MOZ_ASSERT(!existing->hasUses());
   1100    }
   1101 
   1102    removeFirstRange(iter);
   1103  }
   1104 
   1105  if (merged) {
   1106    // Re-append the merged range.
   1107    if (!ranges_.append(merged)) {
   1108      return false;
   1109    }
   1110  } else {
   1111    // The new range does not overlap any existing range for the vreg.
   1112    MOZ_ASSERT_IF(hasRanges(), to < ranges_.back()->from());
   1113 
   1114    LiveRange* range = LiveRange::FallibleNew(alloc, this, from, to);
   1115    if (!range) {
   1116      return false;
   1117    }
   1118 
   1119    if (!ranges_.append(range)) {
   1120      return false;
   1121    }
   1122  }
   1123 
   1124  MOZ_ASSERT(rangesSorted_, "ranges are still sorted");
   1125 
   1126 #ifdef DEBUG
   1127  // Check the last few ranges don't overlap and are in the correct order.
   1128  size_t len = ranges_.length();
   1129  static constexpr size_t MaxIterations = 4;
   1130  size_t start = len > MaxIterations ? (len - MaxIterations) : 1;
   1131 
   1132  for (size_t i = start; i < len; i++) {
   1133    LiveRange* range = ranges_[i];
   1134    LiveRange* prev = ranges_[i - 1];
   1135    MOZ_ASSERT(range->from() < range->to());
   1136    MOZ_ASSERT(range->to() < prev->from());
   1137  }
   1138 #endif
   1139 
   1140  return true;
   1141 }
   1142 
   1143 void VirtualRegister::addInitialUse(UsePosition* use) {
   1144  MOZ_ASSERT(rangesSorted_, "ranges stay sorted during live range building");
   1145  ranges_.back()->addUse(use);
   1146 }
   1147 
   1148 void VirtualRegister::setInitialDefinition(CodePosition from) {
   1149  MOZ_ASSERT(rangesSorted_, "ranges stay sorted during live range building");
   1150  LiveRange* first = ranges_.back();
   1151  MOZ_ASSERT(from >= first->from());
   1152  first->setFrom(from);
   1153  first->setHasDefinition();
   1154 }
   1155 
   1156 LiveRange* VirtualRegister::rangeFor(CodePosition pos,
   1157                                     bool preferRegister /* = false */) const {
   1158  assertRangesSorted();
   1159 
   1160  size_t len = ranges_.length();
   1161 
   1162  // Because the ranges are sorted in order of descending start position, we use
   1163  // binary search to find the first range where |range->from <= pos|. All
   1164  // ranges before that definitely don't cover |pos|.
   1165  auto compare = [pos](LiveRange* other) {
   1166    if (pos < other->from()) {
   1167      return 1;
   1168    }
   1169    if (pos > other->from()) {
   1170      return -1;
   1171    }
   1172    return 0;
   1173  };
   1174  size_t index;
   1175  mozilla::BinarySearchIf(ranges_, 0, len, compare, &index);
   1176 
   1177  if (index == len) {
   1178    // None of the ranges overlap.
   1179    MOZ_ASSERT(ranges_.back()->from() > pos);
   1180    return nullptr;
   1181  }
   1182 
   1183  // There can be multiple ranges with |range->from == pos|. We want to start at
   1184  // the first one.
   1185  while (index > 0 && ranges_[index - 1]->from() == pos) {
   1186    index--;
   1187  }
   1188 
   1189  // Verify the above code is correct:
   1190  // * The range at |index| starts before or at |pos| and needs to be searched.
   1191  // * All ranges before |index| start after |pos| and can be ignored.
   1192  MOZ_ASSERT(ranges_[index]->from() <= pos);
   1193  MOZ_ASSERT_IF(index > 0, ranges_[index - 1]->from() > pos);
   1194 
   1195  LiveRange* found = nullptr;
   1196  do {
   1197    LiveRange* range = ranges_[index];
   1198    if (range->covers(pos)) {
   1199      if (!preferRegister || range->bundle()->allocation().isAnyRegister()) {
   1200        return range;
   1201      }
   1202      if (!found) {
   1203        found = range;
   1204      }
   1205    }
   1206    index++;
   1207  } while (index < len);
   1208 
   1209  return found;
   1210 }
   1211 
   1212 void VirtualRegister::sortRanges() {
   1213  if (rangesSorted_) {
   1214    assertRangesSorted();
   1215    return;
   1216  }
   1217 
   1218  // Sort ranges by start position in descending order.
   1219  //
   1220  // It would be correct to only compare the start position, but std::sort is
   1221  // not a stable sort and we don't want the order of ranges with the same start
   1222  // position to depend on the sort implementation. Use the end position and the
   1223  // bundle's id to ensure ranges are always sorted the same way.
   1224  auto compareRanges = [](LiveRange* a, LiveRange* b) -> bool {
   1225    if (a->from() != b->from()) {
   1226      return a->from() > b->from();
   1227    }
   1228    if (a->to() != b->to()) {
   1229      return a->to() > b->to();
   1230    }
   1231    // Overlapping live ranges must belong to different bundles.
   1232    MOZ_ASSERT_IF(a != b, a->bundle()->id() != b->bundle()->id());
   1233    return a->bundle()->id() > b->bundle()->id();
   1234  };
   1235  std::sort(ranges_.begin(), ranges_.end(), compareRanges);
   1236 
   1237  rangesSorted_ = true;
   1238 }
   1239 
   1240 #ifdef DEBUG
   1241 void VirtualRegister::assertRangesSorted() const {
   1242  MOZ_ASSERT(rangesSorted_);
   1243 
   1244  // Assert the last N ranges in the vector are sorted correctly. We don't check
   1245  // the whole vector to not slow down debug builds too much.
   1246 
   1247  size_t len = ranges_.length();
   1248  static constexpr size_t MaxIterations = 4;
   1249  size_t start = len > MaxIterations ? (len - MaxIterations) : 1;
   1250 
   1251  for (size_t i = start; i < len; i++) {
   1252    LiveRange* prev = ranges_[i - 1];
   1253    LiveRange* range = ranges_[i];
   1254    MOZ_ASSERT(range->from() <= prev->from());
   1255 
   1256    // If two ranges have the same |from| position, neither must be defining.
   1257    // This ensures the defining range, if any, always comes first after
   1258    // sorting.
   1259    MOZ_ASSERT_IF(range->from() == prev->from(),
   1260                  !range->hasDefinition() && !prev->hasDefinition());
   1261  }
   1262 }
   1263 #endif
   1264 
   1265 bool VirtualRegister::addRange(LiveRange* range) {
   1266  bool sorted = ranges_.empty() ||
   1267                (rangesSorted_ && ranges_.back()->from() >= range->from());
   1268  if (!ranges_.append(range)) {
   1269    return false;
   1270  }
   1271  rangesSorted_ = sorted;
   1272  return true;
   1273 }
   1274 
   1275 void VirtualRegister::removeFirstRange(RangeIterator& iter) {
   1276  MOZ_ASSERT(iter.index() == ranges_.length() - 1);
   1277  ranges_.popBack();
   1278 }
   1279 
   1280 void VirtualRegister::removeRangesForBundle(LiveBundle* bundle) {
   1281  auto bundleMatches = [bundle](LiveRange* range) {
   1282    return range->bundle() == bundle;
   1283  };
   1284  ranges_.eraseIf(bundleMatches);
   1285 }
   1286 
   1287 template <typename Pred>
   1288 void VirtualRegister::removeRangesIf(Pred&& pred) {
   1289  assertRangesSorted();
   1290  ranges_.eraseIf([&](LiveRange* range) { return pred(ranges_, range); });
   1291 }
   1292 
   1293 // Helper for ::tryMergeReusedRegister.
   1294 bool VirtualRegister::replaceLastRangeLinear(LiveRange* old, LiveRange* newPre,
   1295                                             LiveRange* newPost) {
   1296  assertRangesSorted();
   1297 
   1298  // |old| is currently the first range in the Vector (the range with the
   1299  // highest start position). This range is being replaced with two new ranges,
   1300  // |newPre| and |newPost|. The relative order of these ranges by start
   1301  // position is:
   1302  //
   1303  //   old <= newPre <= newPost
   1304  //
   1305  // To keep the vector sorted by descending start position, |newPost| must
   1306  // become the new last range and |newPre| must be inserted after it.
   1307 
   1308  MOZ_ASSERT(ranges_[0] == old);
   1309  MOZ_ASSERT(old->from() <= newPre->from());
   1310  MOZ_ASSERT(newPre->from() <= newPost->from());
   1311 
   1312  ranges_[0] = newPost;
   1313 
   1314  if (!ranges_.insert(ranges_.begin() + 1, newPre)) {
   1315    return false;
   1316  }
   1317 
   1318  assertRangesSorted();
   1319  return true;
   1320 }
   1321 
   1322 ///////////////////////////////////////////////////////////////////////////////
   1323 //                                                                           //
   1324 // Misc helpers: queries about uses                                          //
   1325 //                                                                           //
   1326 ///////////////////////////////////////////////////////////////////////////////
   1327 
   1328 static inline LDefinition* FindReusingDefOrTemp(LNode* node,
   1329                                                LAllocation* alloc) {
   1330  if (node->isPhi()) {
   1331    MOZ_ASSERT(node->toPhi()->numDefs() == 1);
   1332    MOZ_ASSERT(node->toPhi()->getDef(0)->policy() !=
   1333               LDefinition::MUST_REUSE_INPUT);
   1334    return nullptr;
   1335  }
   1336 
   1337  LInstruction* ins = node->toInstruction();
   1338 
   1339  for (size_t i = 0; i < ins->numDefs(); i++) {
   1340    LDefinition* def = ins->getDef(i);
   1341    if (def->policy() == LDefinition::MUST_REUSE_INPUT &&
   1342        ins->getOperand(def->getReusedInput()) == alloc) {
   1343      return def;
   1344    }
   1345  }
   1346  for (size_t i = 0; i < ins->numTemps(); i++) {
   1347    LDefinition* def = ins->getTemp(i);
   1348    if (def->policy() == LDefinition::MUST_REUSE_INPUT &&
   1349        ins->getOperand(def->getReusedInput()) == alloc) {
   1350      return def;
   1351    }
   1352  }
   1353  return nullptr;
   1354 }
   1355 
   1356 bool BacktrackingAllocator::isReusedInput(LUse* use, LNode* ins,
   1357                                          bool considerCopy) {
   1358  if (LDefinition* def = FindReusingDefOrTemp(ins, use)) {
   1359    return considerCopy || !vregs[def->virtualRegister()].mustCopyInput();
   1360  }
   1361  return false;
   1362 }
   1363 
   1364 bool BacktrackingAllocator::isRegisterUse(UsePosition* use, LNode* ins,
   1365                                          bool considerCopy) {
   1366  switch (use->usePolicy()) {
   1367    case LUse::ANY:
   1368      return isReusedInput(use->use(), ins, considerCopy);
   1369 
   1370    case LUse::REGISTER:
   1371    case LUse::FIXED:
   1372      return true;
   1373 
   1374    default:
   1375      return false;
   1376  }
   1377 }
   1378 
   1379 bool BacktrackingAllocator::isRegisterDefinition(LiveRange* range) {
   1380  if (!range->hasDefinition()) {
   1381    return false;
   1382  }
   1383 
   1384  VirtualRegister& reg = range->vreg();
   1385  if (reg.ins()->isPhi()) {
   1386    return false;
   1387  }
   1388 
   1389  if (reg.def()->policy() == LDefinition::FIXED &&
   1390      !reg.def()->output()->isAnyRegister()) {
   1391    return false;
   1392  }
   1393 
   1394  return true;
   1395 }
   1396 
   1397 ///////////////////////////////////////////////////////////////////////////////
   1398 //                                                                           //
   1399 // Misc helpers: atomic LIR groups                                           //
   1400 //                                                                           //
   1401 ///////////////////////////////////////////////////////////////////////////////
   1402 
   1403 // The following groupings contain implicit (invisible to ::buildLivenessInfo)
   1404 // value flows, and therefore no split points may be requested inside them.
   1405 // This is an otherwise unstated part of the contract between LIR generation
   1406 // and the allocator.
   1407 //
   1408 // (1) (any insn) ; OsiPoint
   1409 //
   1410 // [Further group definitions and supporting code to come, pending rework
   1411 //  of the wasm atomic-group situation.]
   1412 
   1413 CodePosition BacktrackingAllocator::minimalDefEnd(LNode* ins) const {
   1414  // Compute the shortest interval that captures vregs defined by ins.
   1415  // Watch for instructions that are followed by an OSI point.
   1416  // If moves are introduced between the instruction and the OSI point then
   1417  // safepoint information for the instruction may be incorrect.
   1418  while (true) {
   1419    LNode* next = insData[ins->id() + 1];
   1420    if (!next->isOsiPoint()) {
   1421      break;
   1422    }
   1423    ins = next;
   1424  }
   1425 
   1426  return outputOf(ins);
   1427 }
   1428 
   1429 ///////////////////////////////////////////////////////////////////////////////
   1430 //                                                                           //
   1431 // Misc helpers: computation of bundle priorities and spill weights          //
   1432 //                                                                           //
   1433 ///////////////////////////////////////////////////////////////////////////////
   1434 
   1435 size_t BacktrackingAllocator::computePriority(LiveBundle* bundle) {
   1436  // The priority of a bundle is its total length, so that longer lived
   1437  // bundles will be processed before shorter ones (even if the longer ones
   1438  // have a low spill weight). See processBundle().
   1439  size_t lifetimeTotal = 0;
   1440 
   1441  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   1442    LiveRange* range = *iter;
   1443    lifetimeTotal += range->to() - range->from();
   1444  }
   1445 
   1446  return lifetimeTotal;
   1447 }
   1448 
   1449 bool BacktrackingAllocator::minimalDef(LiveRange* range, LNode* ins) {
   1450  // Whether this is a minimal range capturing a definition at ins.
   1451  return (range->to() <= minimalDefEnd(ins).next()) &&
   1452         ((!ins->isPhi() && range->from() == inputOf(ins)) ||
   1453          range->from() == outputOf(ins));
   1454 }
   1455 
   1456 bool BacktrackingAllocator::minimalUse(LiveRange* range, UsePosition* use) {
   1457  // Whether this is a minimal range capturing |use|.
   1458  LNode* ins = insData[use->pos];
   1459  return (range->from() == inputOf(ins)) &&
   1460         (range->to() ==
   1461          (use->use()->usedAtStart() ? outputOf(ins) : outputOf(ins).next()));
   1462 }
   1463 
   1464 bool BacktrackingAllocator::minimalBundle(LiveBundle* bundle, bool* pfixed) {
   1465  // Return true iff |bundle| is a minimal bundle. A minimal bundle is a bundle
   1466  // that can't be split further. Minimal bundles have a single (very short)
   1467  // range.
   1468  //
   1469  // If the bundle is minimal we also set the |*pfixed| outparam to indicate
   1470  // whether the bundle must be allocated to a fixed register. The value of
   1471  // |*pfixed| is undefined if this function returns false.
   1472 
   1473  LiveBundle::RangeIterator iter = bundle->rangesBegin();
   1474  LiveRange* range = *iter;
   1475 
   1476  MOZ_ASSERT(range->hasVreg(), "Call ranges are not added to LiveBundles");
   1477 
   1478  // If a bundle contains multiple ranges, splitAtAllRegisterUses will split
   1479  // each range into a separate bundle.
   1480  if (++iter) {
   1481    return false;
   1482  }
   1483 
   1484  if (range->hasDefinition()) {
   1485    VirtualRegister& reg = range->vreg();
   1486    if (!minimalDef(range, reg.ins())) {
   1487      return false;
   1488    }
   1489    if (pfixed) {
   1490      *pfixed = reg.def()->policy() == LDefinition::FIXED &&
   1491                reg.def()->output()->isAnyRegister();
   1492    }
   1493    return true;
   1494  }
   1495 
   1496  // Performance optimization: |minimalUse| will only return true for length-1
   1497  // or length-2 ranges so if this is a longer range it can't be minimal.
   1498  if (range->to() - range->from() > 2) {
   1499 #ifdef DEBUG
   1500    for (UsePositionIterator iter = range->usesBegin(); iter; iter++) {
   1501      MOZ_ASSERT(!minimalUse(range, *iter));
   1502    }
   1503 #endif
   1504    return false;
   1505  }
   1506 
   1507  bool fixed = false, minimal = false, multiple = false;
   1508 
   1509  for (UsePositionIterator iter = range->usesBegin(); iter; iter++) {
   1510    if (iter != range->usesBegin()) {
   1511      multiple = true;
   1512    }
   1513 
   1514    switch (iter->usePolicy()) {
   1515      case LUse::FIXED:
   1516        if (fixed) {
   1517          return false;
   1518        }
   1519        fixed = true;
   1520        if (minimalUse(range, *iter)) {
   1521          minimal = true;
   1522        }
   1523        break;
   1524 
   1525      case LUse::REGISTER:
   1526        if (minimalUse(range, *iter)) {
   1527          minimal = true;
   1528        }
   1529        break;
   1530 
   1531      default:
   1532        break;
   1533    }
   1534  }
   1535 
   1536  // If a range contains a fixed use and at least one other use,
   1537  // splitAtAllRegisterUses will split each use into a different bundle.
   1538  if (multiple && fixed) {
   1539    return false;
   1540  }
   1541 
   1542  if (!minimal) {
   1543    return false;
   1544  }
   1545  if (pfixed) {
   1546    *pfixed = fixed;
   1547  }
   1548  return true;
   1549 }
   1550 
   1551 size_t BacktrackingAllocator::computeSpillWeight(LiveBundle* bundle) {
   1552  // Minimal bundles have an extremely high spill weight, to ensure they
   1553  // can evict any other bundles and be allocated to a register.
   1554  bool fixed;
   1555  if (minimalBundle(bundle, &fixed)) {
   1556    return fixed ? 2000000 : 1000000;
   1557  }
   1558 
   1559  size_t usesTotal = 0;
   1560  fixed = false;
   1561 
   1562  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   1563    LiveRange* range = *iter;
   1564 
   1565    if (range->hasDefinition()) {
   1566      VirtualRegister& reg = range->vreg();
   1567      if (reg.def()->policy() == LDefinition::FIXED &&
   1568          reg.def()->output()->isAnyRegister()) {
   1569        usesTotal += 2000;
   1570        fixed = true;
   1571      } else if (!reg.ins()->isPhi()) {
   1572        usesTotal += 2000;
   1573      }
   1574    }
   1575 
   1576    usesTotal += range->usesSpillWeight();
   1577    if (range->numFixedUses() > 0) {
   1578      fixed = true;
   1579    }
   1580  }
   1581 
   1582  // Compute spill weight as a use density, lowering the weight for long
   1583  // lived bundles with relatively few uses.
   1584  size_t lifetimeTotal = computePriority(bundle);
   1585  return lifetimeTotal ? usesTotal / lifetimeTotal : 0;
   1586 }
   1587 
   1588 size_t BacktrackingAllocator::maximumSpillWeight(
   1589    const LiveBundleVector& bundles) {
   1590  size_t maxWeight = 0;
   1591  for (size_t i = 0; i < bundles.length(); i++) {
   1592    maxWeight = std::max(maxWeight, computeSpillWeight(bundles[i]));
   1593  }
   1594  return maxWeight;
   1595 }
   1596 
   1597 ///////////////////////////////////////////////////////////////////////////////
   1598 //                                                                           //
   1599 // Initialization of the allocator                                           //
   1600 //                                                                           //
   1601 ///////////////////////////////////////////////////////////////////////////////
   1602 
   1603 // This function pre-allocates and initializes as much global state as possible
   1604 // to avoid littering the algorithms with memory management cruft.
   1605 bool BacktrackingAllocator::init() {
   1606  if (!insData.init(mir, graph.numInstructions())) {
   1607    return false;
   1608  }
   1609 
   1610  if (!entryPositions.reserve(graph.numBlocks()) ||
   1611      !exitPositions.reserve(graph.numBlocks())) {
   1612    return false;
   1613  }
   1614 
   1615  for (size_t i = 0; i < graph.numBlocks(); i++) {
   1616    LBlock* block = graph.getBlock(i);
   1617    for (LInstructionIterator ins = block->begin(); ins != block->end();
   1618         ins++) {
   1619      insData[ins->id()] = *ins;
   1620    }
   1621    for (size_t j = 0; j < block->numPhis(); j++) {
   1622      LPhi* phi = block->getPhi(j);
   1623      insData[phi->id()] = phi;
   1624    }
   1625 
   1626    CodePosition entry =
   1627        block->numPhis() != 0
   1628            ? CodePosition(block->getPhi(0)->id(), CodePosition::INPUT)
   1629            : inputOf(block->firstInstructionWithId());
   1630    CodePosition exit = outputOf(block->lastInstructionWithId());
   1631 
   1632    MOZ_ASSERT(block->mir()->id() == i);
   1633    entryPositions.infallibleAppend(entry);
   1634    exitPositions.infallibleAppend(exit);
   1635  }
   1636 
   1637  uint32_t numBlocks = graph.numBlockIds();
   1638  MOZ_ASSERT(liveIn.empty());
   1639  if (!liveIn.growBy(numBlocks)) {
   1640    return false;
   1641  }
   1642 
   1643  size_t numVregs = graph.numVirtualRegisters();
   1644  if (!vregs.initCapacity(numVregs)) {
   1645    return false;
   1646  }
   1647  for (uint32_t i = 0; i < numVregs; i++) {
   1648    vregs.infallibleEmplaceBack();
   1649  }
   1650 
   1651  // Build virtual register objects.
   1652  for (size_t i = 0; i < graph.numBlocks(); i++) {
   1653    if (mir->shouldCancel("Create data structures (main loop)")) {
   1654      return false;
   1655    }
   1656 
   1657    LBlock* block = graph.getBlock(i);
   1658    for (LInstructionIterator ins = block->begin(); ins != block->end();
   1659         ins++) {
   1660      if (mir->shouldCancel("Create data structures (inner loop 1)")) {
   1661        return false;
   1662      }
   1663 
   1664      for (LInstruction::OutputIter output(*ins); !output.done(); output++) {
   1665        vreg(*output).init(*ins, *output, /* isTemp = */ false);
   1666      }
   1667      for (LInstruction::TempIter temp(*ins); !temp.done(); temp++) {
   1668        vreg(*temp).init(*ins, *temp, /* isTemp = */ true);
   1669      }
   1670    }
   1671    for (size_t j = 0; j < block->numPhis(); j++) {
   1672      LPhi* phi = block->getPhi(j);
   1673      LDefinition* def = phi->getDef(0);
   1674      vreg(def).init(phi, def, /* isTemp = */ false);
   1675    }
   1676  }
   1677 
   1678  LiveRegisterSet remainingRegisters(allRegisters_.asLiveSet());
   1679  while (!remainingRegisters.emptyGeneral()) {
   1680    AnyRegister reg = AnyRegister(remainingRegisters.takeAnyGeneral());
   1681    registers[reg.code()].allocatable = true;
   1682  }
   1683  while (!remainingRegisters.emptyFloat()) {
   1684    AnyRegister reg =
   1685        AnyRegister(remainingRegisters.takeAnyFloat<RegTypeName::Any>());
   1686    registers[reg.code()].allocatable = true;
   1687  }
   1688 
   1689  LifoAlloc* lifoAlloc = mir->alloc().lifoAlloc();
   1690  for (size_t i = 0; i < AnyRegister::Total; i++) {
   1691    registers[i].reg = AnyRegister::FromCode(i);
   1692    registers[i].allocations.setAllocator(lifoAlloc);
   1693  }
   1694 
   1695  hotcode.setAllocator(lifoAlloc);
   1696 
   1697  // Partition the graph into hot and cold sections, for helping to make
   1698  // splitting decisions. Since we don't have any profiling data this is a
   1699  // crapshoot, so just mark the bodies of inner loops as hot and everything
   1700  // else as cold.
   1701 
   1702  LBlock* backedge = nullptr;
   1703  for (size_t i = 0; i < graph.numBlocks(); i++) {
   1704    LBlock* block = graph.getBlock(i);
   1705 
   1706    // If we see a loop header, mark the backedge so we know when we have
   1707    // hit the end of the loop. Don't process the loop immediately, so that
   1708    // if there is an inner loop we will ignore the outer backedge.
   1709    if (block->mir()->isLoopHeader()) {
   1710      backedge = block->mir()->backedge()->lir();
   1711    }
   1712 
   1713    if (block == backedge) {
   1714      LBlock* header = block->mir()->loopHeaderOfBackedge()->lir();
   1715      LiveRange* range = LiveRange::FallibleNew(
   1716          alloc(), nullptr, entryOf(header), exitOf(block).next());
   1717      if (!range || !hotcode.insert(range)) {
   1718        return false;
   1719      }
   1720    }
   1721  }
   1722 
   1723  return true;
   1724 }
   1725 
   1726 ///////////////////////////////////////////////////////////////////////////////
   1727 //                                                                           //
   1728 // Liveness analysis                                                         //
   1729 //                                                                           //
   1730 ///////////////////////////////////////////////////////////////////////////////
   1731 
   1732 // Helper for ::buildLivenessInfo
   1733 bool BacktrackingAllocator::addInitialFixedRange(AnyRegister reg,
   1734                                                 CodePosition from,
   1735                                                 CodePosition to) {
   1736  LiveRange* range = LiveRange::FallibleNew(alloc(), nullptr, from, to);
   1737  if (!range) {
   1738    return false;
   1739  }
   1740  LiveRangePlus rangePlus(range);
   1741  return registers[reg.code()].allocations.insert(rangePlus);
   1742 }
   1743 
   1744 // Helper for ::buildLivenessInfo
   1745 #ifdef DEBUG
   1746 // Returns true iff ins has a def/temp reusing the input allocation.
   1747 static bool IsInputReused(LInstruction* ins, LUse* use) {
   1748  for (size_t i = 0; i < ins->numDefs(); i++) {
   1749    if (ins->getDef(i)->policy() == LDefinition::MUST_REUSE_INPUT &&
   1750        ins->getOperand(ins->getDef(i)->getReusedInput())->toUse() == use) {
   1751      return true;
   1752    }
   1753  }
   1754 
   1755  for (size_t i = 0; i < ins->numTemps(); i++) {
   1756    if (ins->getTemp(i)->policy() == LDefinition::MUST_REUSE_INPUT &&
   1757        ins->getOperand(ins->getTemp(i)->getReusedInput())->toUse() == use) {
   1758      return true;
   1759    }
   1760  }
   1761 
   1762  return false;
   1763 }
   1764 #endif
   1765 
   1766 /*
   1767 * This function builds up liveness ranges for all virtual registers
   1768 * defined in the function.
   1769 *
   1770 * The algorithm is based on the one published in:
   1771 *
   1772 * Wimmer, Christian, and Michael Franz. "Linear Scan Register Allocation on
   1773 *     SSA Form." Proceedings of the International Symposium on Code Generation
   1774 *     and Optimization. Toronto, Ontario, Canada, ACM. 2010. 170-79. PDF.
   1775 *
   1776 * The algorithm operates on blocks ordered such that dominators of a block
   1777 * are before the block itself, and such that all blocks of a loop are
   1778 * contiguous. It proceeds backwards over the instructions in this order,
   1779 * marking registers live at their uses, ending their live ranges at
   1780 * definitions, and recording which registers are live at the top of every
   1781 * block. To deal with loop backedges, registers live at the beginning of
   1782 * a loop gain a range covering the entire loop.
   1783 */
   1784 bool BacktrackingAllocator::buildLivenessInfo() {
   1785  JitSpew(JitSpew_RegAlloc, "Beginning liveness analysis");
   1786 
   1787  // The callPositions vector and the safepoint vectors are initialized from
   1788  // index |length - 1| to 0, to ensure they are sorted.
   1789  if (!callPositions.growByUninitialized(graph.numCallInstructions()) ||
   1790      !safepoints_.growByUninitialized(graph.numSafepoints()) ||
   1791      !nonCallSafepoints_.growByUninitialized(graph.numNonCallSafepoints())) {
   1792    return false;
   1793  }
   1794  size_t prevCallPositionIndex = callPositions.length();
   1795  size_t prevSafepointIndex = safepoints_.length();
   1796  size_t prevNonCallSafepointIndex = nonCallSafepoints_.length();
   1797 
   1798  for (size_t i = graph.numBlocks(); i > 0; i--) {
   1799    if (mir->shouldCancel("Build Liveness Info (main loop)")) {
   1800      return false;
   1801    }
   1802 
   1803    LBlock* block = graph.getBlock(i - 1);
   1804    MBasicBlock* mblock = block->mir();
   1805 
   1806    VirtualRegBitSet& live = liveIn[mblock->id()];
   1807 
   1808    // Propagate liveIn from our successors to us.
   1809    for (size_t i = 0; i < mblock->lastIns()->numSuccessors(); i++) {
   1810      MBasicBlock* successor = mblock->lastIns()->getSuccessor(i);
   1811      // Skip backedges, as we fix them up at the loop header.
   1812      if (mblock->id() < successor->id()) {
   1813        if (!live.insertAll(liveIn[successor->id()])) {
   1814          return false;
   1815        }
   1816      }
   1817    }
   1818 
   1819    // Add successor phis.
   1820    if (mblock->successorWithPhis()) {
   1821      LBlock* phiSuccessor = mblock->successorWithPhis()->lir();
   1822      for (unsigned int j = 0; j < phiSuccessor->numPhis(); j++) {
   1823        LPhi* phi = phiSuccessor->getPhi(j);
   1824        LAllocation* use = phi->getOperand(mblock->positionInPhiSuccessor());
   1825        uint32_t reg = use->toUse()->virtualRegister();
   1826        if (!live.insert(reg)) {
   1827          return false;
   1828        }
   1829        vreg(use).setUsedByPhi();
   1830      }
   1831    }
   1832 
   1833    // Registers are assumed alive for the entire block, a define shortens
   1834    // the range to the point of definition.
   1835    for (VirtualRegBitSet::Iterator liveRegId(live); liveRegId; ++liveRegId) {
   1836      if (!vregs[*liveRegId].addInitialRange(alloc(), entryOf(block),
   1837                                             exitOf(block).next())) {
   1838        return false;
   1839      }
   1840    }
   1841 
   1842    // Shorten the front end of ranges for live variables to their point of
   1843    // definition, if found.
   1844    for (LInstructionReverseIterator ins = block->rbegin();
   1845         ins != block->rend(); ins++) {
   1846      // Calls may clobber registers, so force a spill and reload around the
   1847      // callsite.
   1848      if (ins->isCall()) {
   1849        for (AnyRegisterIterator iter(allRegisters_.asLiveSet()); iter.more();
   1850             ++iter) {
   1851          bool found = false;
   1852          for (size_t i = 0; i < ins->numDefs(); i++) {
   1853            if (ins->getDef(i)->isFixed() &&
   1854                ins->getDef(i)->output()->aliases(LAllocation(*iter))) {
   1855              found = true;
   1856              break;
   1857            }
   1858          }
   1859          // If this register doesn't have an explicit def above, mark
   1860          // it as clobbered by the call unless it is actually
   1861          // call-preserved.
   1862          if (!found && !ins->isCallPreserved(*iter)) {
   1863            if (!addInitialFixedRange(*iter, outputOf(*ins),
   1864                                      outputOf(*ins).next())) {
   1865              return false;
   1866            }
   1867          }
   1868        }
   1869 
   1870        // Add the call position before the previous one to ensure the vector is
   1871        // sorted.
   1872        MOZ_ASSERT(prevCallPositionIndex > 0);
   1873        MOZ_ASSERT_IF(prevCallPositionIndex < callPositions.length(),
   1874                      outputOf(*ins) < callPositions[prevCallPositionIndex]);
   1875        prevCallPositionIndex--;
   1876        callPositions[prevCallPositionIndex] = outputOf(*ins);
   1877      }
   1878 
   1879      for (LInstruction::OutputIter output(*ins); !output.done(); output++) {
   1880        LDefinition* def = *output;
   1881        CodePosition from = outputOf(*ins);
   1882 
   1883        if (def->policy() == LDefinition::MUST_REUSE_INPUT) {
   1884          ins->changePolicyOfReusedInputToAny(def);
   1885        }
   1886 
   1887        if (!vreg(def).addInitialRange(alloc(), from, from.next())) {
   1888          return false;
   1889        }
   1890        vreg(def).setInitialDefinition(from);
   1891        live.remove(def->virtualRegister());
   1892      }
   1893 
   1894      for (LInstruction::TempIter tempIter(*ins); !tempIter.done();
   1895           tempIter++) {
   1896        LDefinition* temp = *tempIter;
   1897 
   1898        // Normally temps are considered to cover both the input
   1899        // and output of the associated instruction. In some cases
   1900        // though we want to use a fixed register as both an input
   1901        // and clobbered register in the instruction, so watch for
   1902        // this and shorten the temp to cover only the output.
   1903        CodePosition from = inputOf(*ins);
   1904        if (temp->policy() == LDefinition::FIXED) {
   1905          AnyRegister reg = temp->output()->toAnyRegister();
   1906          for (LInstruction::NonSnapshotInputIter alloc(**ins); alloc.more();
   1907               alloc.next()) {
   1908            if (alloc->isUse()) {
   1909              LUse* use = alloc->toUse();
   1910              if (use->isFixedRegister()) {
   1911                if (GetFixedRegister(vreg(use).def(), use) == reg) {
   1912                  from = outputOf(*ins);
   1913                }
   1914              }
   1915            }
   1916          }
   1917        }
   1918 
   1919        // * For non-call instructions, temps cover both the input and output,
   1920        //   so temps never alias uses (even at-start uses) or defs.
   1921        // * For call instructions, temps only cover the input (the output is
   1922        //   used for the force-spill ranges added above). This means temps
   1923        //   still don't alias uses but they can alias the (fixed) defs. For now
   1924        //   we conservatively require temps to have a fixed register for call
   1925        //   instructions to prevent a footgun.
   1926        MOZ_ASSERT_IF(ins->isCall(), temp->policy() == LDefinition::FIXED);
   1927        CodePosition to =
   1928            ins->isCall() ? outputOf(*ins) : outputOf(*ins).next();
   1929 
   1930        if (!vreg(temp).addInitialRange(alloc(), from, to)) {
   1931          return false;
   1932        }
   1933        vreg(temp).setInitialDefinition(from);
   1934      }
   1935 
   1936      DebugOnly<bool> hasUseRegister = false;
   1937      DebugOnly<bool> hasUseRegisterAtStart = false;
   1938 
   1939      for (LInstruction::InputIter inputAlloc(**ins); inputAlloc.more();
   1940           inputAlloc.next()) {
   1941        if (inputAlloc->isUse()) {
   1942          LUse* use = inputAlloc->toUse();
   1943 
   1944          // Call uses should always be at-start, since calls use all
   1945          // registers.
   1946          MOZ_ASSERT_IF(ins->isCall() && !inputAlloc.isSnapshotInput(),
   1947                        use->usedAtStart());
   1948 
   1949 #ifdef DEBUG
   1950          // If there are both useRegisterAtStart(x) and useRegister(y)
   1951          // uses, we may assign the same register to both operands
   1952          // (bug 772830). Don't allow this for now.
   1953          if (use->policy() == LUse::REGISTER) {
   1954            if (use->usedAtStart()) {
   1955              if (!IsInputReused(*ins, use)) {
   1956                hasUseRegisterAtStart = true;
   1957              }
   1958            } else {
   1959              hasUseRegister = true;
   1960            }
   1961          }
   1962          MOZ_ASSERT(!(hasUseRegister && hasUseRegisterAtStart));
   1963 #endif
   1964 
   1965          // Don't treat RECOVERED_INPUT uses as keeping the vreg alive.
   1966          if (use->policy() == LUse::RECOVERED_INPUT) {
   1967            continue;
   1968          }
   1969 
   1970          CodePosition to = use->usedAtStart() ? inputOf(*ins) : outputOf(*ins);
   1971          if (use->isFixedRegister()) {
   1972            LAllocation reg(AnyRegister::FromCode(use->registerCode()));
   1973            for (size_t i = 0; i < ins->numDefs(); i++) {
   1974              LDefinition* def = ins->getDef(i);
   1975              if (def->policy() == LDefinition::FIXED &&
   1976                  *def->output() == reg) {
   1977                to = inputOf(*ins);
   1978              }
   1979            }
   1980          }
   1981 
   1982          if (!vreg(use).addInitialRange(alloc(), entryOf(block), to.next())) {
   1983            return false;
   1984          }
   1985          UsePosition* usePosition =
   1986              new (alloc().fallible()) UsePosition(use, to);
   1987          if (!usePosition) {
   1988            return false;
   1989          }
   1990          vreg(use).addInitialUse(usePosition);
   1991          if (!live.insert(use->virtualRegister())) {
   1992            return false;
   1993          }
   1994        }
   1995      }
   1996 
   1997      if (ins->safepoint()) {
   1998        MOZ_ASSERT(prevSafepointIndex > 0);
   1999        prevSafepointIndex--;
   2000        safepoints_[prevSafepointIndex] = *ins;
   2001        if (!ins->isCall()) {
   2002          MOZ_ASSERT(prevNonCallSafepointIndex > 0);
   2003          prevNonCallSafepointIndex--;
   2004          nonCallSafepoints_[prevNonCallSafepointIndex] = *ins;
   2005        }
   2006      }
   2007    }
   2008 
   2009    // Phis have simultaneous assignment semantics at block begin, so at
   2010    // the beginning of the block we can be sure that liveIn does not
   2011    // contain any phi outputs.
   2012    for (unsigned int i = 0; i < block->numPhis(); i++) {
   2013      LDefinition* def = block->getPhi(i)->getDef(0);
   2014      if (live.contains(def->virtualRegister())) {
   2015        live.remove(def->virtualRegister());
   2016      } else {
   2017        // This is a dead phi, so add a dummy range over all phis. This
   2018        // can go away if we have an earlier dead code elimination pass.
   2019        CodePosition entryPos = entryOf(block);
   2020        if (!vreg(def).addInitialRange(alloc(), entryPos, entryPos.next())) {
   2021          return false;
   2022        }
   2023      }
   2024    }
   2025 
   2026    if (mblock->isLoopHeader()) {
   2027      // MakeLoopsContiguous ensures blocks of a loop are contiguous, so add a
   2028      // single live range spanning the loop. Because we require liveIn in a
   2029      // later pass for resolution, we also have to fix that up for all loop
   2030      // blocks.
   2031 
   2032      MBasicBlock* backedge = mblock->backedge();
   2033 
   2034      // Add a range for this entire loop
   2035      CodePosition from = entryOf(mblock->lir());
   2036      CodePosition to = exitOf(backedge->lir()).next();
   2037 
   2038      for (VirtualRegBitSet::Iterator liveRegId(live); liveRegId; ++liveRegId) {
   2039        if (!vregs[*liveRegId].addInitialRange(alloc(), from, to)) {
   2040          return false;
   2041        }
   2042      }
   2043 
   2044      if (mblock != backedge) {
   2045        // Start at the block after |mblock|.
   2046        MOZ_ASSERT(graph.getBlock(i - 1) == mblock->lir());
   2047        size_t j = i;
   2048        while (true) {
   2049          MBasicBlock* loopBlock = graph.getBlock(j)->mir();
   2050 
   2051          // Fix up the liveIn set.
   2052          if (!liveIn[loopBlock->id()].insertAll(live)) {
   2053            return false;
   2054          }
   2055 
   2056          if (loopBlock == backedge) {
   2057            break;
   2058          }
   2059          j++;
   2060        }
   2061      }
   2062    }
   2063 
   2064    MOZ_ASSERT_IF(!mblock->numPredecessors(), live.empty());
   2065  }
   2066 
   2067  MOZ_RELEASE_ASSERT(prevCallPositionIndex == 0,
   2068                     "Must have initialized all call positions");
   2069  MOZ_RELEASE_ASSERT(prevSafepointIndex == 0,
   2070                     "Must have initialized all safepoints");
   2071  MOZ_RELEASE_ASSERT(prevNonCallSafepointIndex == 0,
   2072                     "Must have initialized all safepoints");
   2073 
   2074  JitSpew(JitSpew_RegAlloc, "Completed liveness analysis");
   2075  return true;
   2076 }
   2077 
   2078 Maybe<size_t> BacktrackingAllocator::lookupFirstCallPositionInRange(
   2079    CodePosition from, CodePosition to) {
   2080  MOZ_ASSERT(from < to);
   2081 
   2082  // Use binary search to find the first call position in range [from, to).
   2083  size_t len = callPositions.length();
   2084  size_t index;
   2085  mozilla::BinarySearch(callPositions, 0, len, from, &index);
   2086  MOZ_ASSERT(index <= len);
   2087 
   2088  if (index == len) {
   2089    // Last call position in the vector is before this range.
   2090    MOZ_ASSERT_IF(len > 0, callPositions.back() < from);
   2091    return {};
   2092  }
   2093 
   2094  if (callPositions[index] >= to) {
   2095    // The next call position comes after this range.
   2096    return {};
   2097  }
   2098 
   2099  MOZ_ASSERT(callPositions[index] >= from && callPositions[index] < to);
   2100  return mozilla::Some(index);
   2101 }
   2102 
   2103 ///////////////////////////////////////////////////////////////////////////////
   2104 //                                                                           //
   2105 // Merging and queueing of LiveRange groups                                  //
   2106 //                                                                           //
   2107 ///////////////////////////////////////////////////////////////////////////////
   2108 
   2109 // Helper for ::tryMergeBundles
   2110 static bool IsArgumentSlotDefinition(LDefinition* def) {
   2111  return def->policy() == LDefinition::FIXED && def->output()->isArgument();
   2112 }
   2113 
   2114 // Helper for ::tryMergeBundles
   2115 static bool IsThisSlotDefinition(LDefinition* def) {
   2116  return IsArgumentSlotDefinition(def) &&
   2117         def->output()->toArgument()->index() <
   2118             THIS_FRAME_ARGSLOT + sizeof(Value);
   2119 }
   2120 
   2121 // Helper for ::tryMergeBundles
   2122 static bool HasStackPolicy(LDefinition* def) {
   2123  return def->policy() == LDefinition::STACK;
   2124 }
   2125 
   2126 // Helper for ::tryMergeBundles
   2127 static bool CanMergeTypesInBundle(LDefinition::Type a, LDefinition::Type b) {
   2128  // Fast path for the common case.
   2129  if (a == b) {
   2130    return true;
   2131  }
   2132 
   2133  // Only merge if the sizes match, so that we don't get confused about the
   2134  // width of spill slots.
   2135  return LStackSlot::width(a) == LStackSlot::width(b);
   2136 }
   2137 
   2138 // Helper for ::tryMergeReusedRegister
   2139 void BacktrackingAllocator::tryMergeBundles(LiveBundle* bundle0,
   2140                                            LiveBundle* bundle1) {
   2141  // See if bundle0 and bundle1 can be merged together.
   2142  if (bundle0 == bundle1) {
   2143    return;
   2144  }
   2145 
   2146  // Get a representative virtual register from each bundle.
   2147  VirtualRegister& reg0 = bundle0->firstRange()->vreg();
   2148  VirtualRegister& reg1 = bundle1->firstRange()->vreg();
   2149 
   2150  MOZ_ASSERT(CanMergeTypesInBundle(reg0.type(), reg1.type()));
   2151  MOZ_ASSERT(reg0.isCompatible(reg1));
   2152 
   2153  if (!compilingWasm()) {
   2154    // Registers which might spill to the frame's |this| slot can only be
   2155    // grouped with other such registers. The frame's |this| slot must always
   2156    // hold the |this| value, as required by JitFrame tracing and by the Ion
   2157    // constructor calling convention.
   2158    if (IsThisSlotDefinition(reg0.def()) || IsThisSlotDefinition(reg1.def())) {
   2159      if (*reg0.def()->output() != *reg1.def()->output()) {
   2160        return;
   2161      }
   2162    }
   2163 
   2164    // Registers which might spill to the frame's argument slots can only be
   2165    // grouped with other such registers in the following cases:
   2166    //
   2167    // * If the frame might access those arguments through a lazy arguments
   2168    //   object or rest parameter.
   2169    //
   2170    // * On 32-bit platforms, to prevent creating garbage Values in the formal
   2171    //   argument slots when spilling just the type or payload of a different
   2172    //   Value. GC tracing of ArraySortData relies on the argument slots holding
   2173    //   valid JS values. See bug 1911858.
   2174    if (IsArgumentSlotDefinition(reg0.def()) ||
   2175        IsArgumentSlotDefinition(reg1.def())) {
   2176 #ifdef JS_PUNBOX64
   2177      MOZ_ASSERT(reg0.type() == LDefinition::Type::BOX);
   2178      MOZ_ASSERT(reg1.type() == LDefinition::Type::BOX);
   2179      bool canSpillToArgSlots =
   2180          !graph.mir().entryBlock()->info().mayReadFrameArgsDirectly();
   2181 #else
   2182      bool canSpillToArgSlots = false;
   2183 #endif
   2184      if (!canSpillToArgSlots) {
   2185        if (*reg0.def()->output() != *reg1.def()->output()) {
   2186          return;
   2187        }
   2188      }
   2189    }
   2190  }
   2191 
   2192  // When we make a call to a WebAssembly function that returns multiple
   2193  // results, some of those results can go on the stack.  The callee is passed a
   2194  // pointer to this stack area, which is represented as having policy
   2195  // LDefinition::STACK (with type LDefinition::STACKRESULTS).  Individual
   2196  // results alias parts of the stack area with a value-appropriate type, but
   2197  // policy LDefinition::STACK.  This aliasing between allocations makes it
   2198  // unsound to merge anything with a LDefinition::STACK policy.
   2199  if (HasStackPolicy(reg0.def()) || HasStackPolicy(reg1.def())) {
   2200    return;
   2201  }
   2202 
   2203  // Limit the number of times we compare ranges if there are many ranges in
   2204  // one of the bundles, to avoid quadratic behavior.
   2205  static const size_t MAX_RANGES = 200;
   2206 
   2207  // Make sure that ranges in the bundles do not overlap.
   2208  LiveBundle::RangeIterator iter0 = bundle0->rangesBegin(),
   2209                            iter1 = bundle1->rangesBegin();
   2210  size_t count = 0;
   2211  while (iter0 && iter1) {
   2212    if (++count >= MAX_RANGES) {
   2213      return;
   2214    }
   2215 
   2216    LiveRange* range0 = *iter0;
   2217    LiveRange* range1 = *iter1;
   2218 
   2219    if (range0->from() >= range1->to()) {
   2220      iter1++;
   2221    } else if (range1->from() >= range0->to()) {
   2222      iter0++;
   2223    } else {
   2224      return;
   2225    }
   2226  }
   2227 
   2228  // Move all ranges from bundle1 into bundle0. Use a fast path for the case
   2229  // where we can add all ranges from bundle1 at the end of bundle0.
   2230  if (SortBefore(bundle0->lastRange(), bundle1->firstRange())) {
   2231    while (LiveRange* range = bundle1->popFirstRange()) {
   2232      bundle0->addRangeAtEnd(range);
   2233    }
   2234  } else {
   2235    LiveRange* prevRange = nullptr;
   2236    while (LiveRange* range = bundle1->popFirstRange()) {
   2237      bundle0->addRange(range, prevRange);
   2238      prevRange = range;
   2239    }
   2240  }
   2241 }
   2242 
   2243 // Helper for ::mergeAndQueueRegisters
   2244 void BacktrackingAllocator::allocateStackDefinition(VirtualRegister& reg) {
   2245  LInstruction* ins = reg.ins()->toInstruction();
   2246  if (reg.def()->type() == LDefinition::STACKRESULTS) {
   2247    LStackArea alloc(ins->toInstruction());
   2248    stackSlotAllocator.allocateStackArea(&alloc);
   2249    reg.def()->setOutput(alloc);
   2250  } else {
   2251    // Because the definitions are visited in order, the area has been allocated
   2252    // before we reach this result, so we know the operand is an LStackArea.
   2253    const LUse* use = ins->getOperand(0)->toUse();
   2254    VirtualRegister& area = vregs[use->virtualRegister()];
   2255    const LStackArea* areaAlloc = area.def()->output()->toStackArea();
   2256    reg.def()->setOutput(areaAlloc->resultAlloc(ins, reg.def()));
   2257  }
   2258 }
   2259 
   2260 // Helper for ::mergeAndQueueRegisters
   2261 bool BacktrackingAllocator::tryMergeReusedRegister(VirtualRegister& def,
   2262                                                   VirtualRegister& input) {
   2263  // def is a vreg which reuses input for its output physical register. Try
   2264  // to merge ranges for def with those of input if possible, as avoiding
   2265  // copies before def's instruction is crucial for generated code quality
   2266  // (MUST_REUSE_INPUT is used for all arithmetic on x86/x64).
   2267 
   2268  // Don't try to merge if the definition starts at the input point of the
   2269  // instruction.
   2270  if (def.firstRange()->from() == inputOf(def.ins())) {
   2271    MOZ_ASSERT(def.isTemp());
   2272    def.setMustCopyInput();
   2273    return true;
   2274  }
   2275  MOZ_ASSERT(def.firstRange()->from() == outputOf(def.ins()));
   2276 
   2277  if (!CanMergeTypesInBundle(def.type(), input.type())) {
   2278    def.setMustCopyInput();
   2279    return true;
   2280  }
   2281 
   2282  LiveRange* inputRange = input.rangeFor(outputOf(def.ins()));
   2283  if (!inputRange) {
   2284    // The input is not live after the instruction, either in a safepoint
   2285    // for the instruction or in subsequent code. The input and output
   2286    // can thus be in the same group.
   2287    tryMergeBundles(def.firstBundle(), input.firstBundle());
   2288    return true;
   2289  }
   2290 
   2291  // Avoid merging in very large live ranges as merging has non-linear
   2292  // complexity.  The cutoff value is hard to gauge.  1M was chosen because it
   2293  // is "large" and yet usefully caps compile time on AutoCad-for-the-web to
   2294  // something reasonable on a 2017-era desktop system.  See bug 1728781.
   2295  //
   2296  // Subsequently -- bug 1897427 comment 9 -- even this threshold seems too
   2297  // high, resulting in delays of around 10 seconds.  Setting it to 500000
   2298  // "fixes" that one, but it seems prudent to reduce it to 250000 so as to
   2299  // avoid having to change it yet again at some future point.
   2300  const uint32_t RANGE_SIZE_CUTOFF = 250000;
   2301  if (inputRange->to() - inputRange->from() > RANGE_SIZE_CUTOFF) {
   2302    def.setMustCopyInput();
   2303    return true;
   2304  }
   2305 
   2306  // The input is live afterwards, either in future instructions or in a
   2307  // safepoint for the reusing instruction. This is impossible to satisfy
   2308  // without copying the input.
   2309  //
   2310  // It may or may not be better to split the input into two bundles at the
   2311  // point of the definition, which may permit merging. One case where it is
   2312  // definitely better to split is if the input never has any register uses
   2313  // after the instruction. Handle this splitting eagerly.
   2314 
   2315  LBlock* block = def.ins()->block();
   2316 
   2317  // The input's lifetime must end within the same block as the definition,
   2318  // otherwise it could live on in phis elsewhere.
   2319  if (inputRange != input.lastRange() || inputRange->to() > exitOf(block)) {
   2320    def.setMustCopyInput();
   2321    return true;
   2322  }
   2323 
   2324  // If we already split the input for some other register, don't make a
   2325  // third bundle.
   2326  if (inputRange->bundle() != input.firstRange()->bundle()) {
   2327    def.setMustCopyInput();
   2328    return true;
   2329  }
   2330 
   2331  // If the input will start out in memory then adding a separate bundle for
   2332  // memory uses after the def won't help.
   2333  if (input.def()->isFixed() && !input.def()->output()->isAnyRegister()) {
   2334    def.setMustCopyInput();
   2335    return true;
   2336  }
   2337 
   2338  // The input cannot have register or reused uses after the definition.
   2339  for (UsePositionIterator iter = inputRange->usesBegin(); iter; iter++) {
   2340    if (iter->pos <= inputOf(def.ins())) {
   2341      continue;
   2342    }
   2343 
   2344    LUse* use = iter->use();
   2345    if (FindReusingDefOrTemp(insData[iter->pos], use)) {
   2346      def.setMustCopyInput();
   2347      return true;
   2348    }
   2349    if (iter->usePolicy() != LUse::ANY &&
   2350        iter->usePolicy() != LUse::KEEPALIVE) {
   2351      def.setMustCopyInput();
   2352      return true;
   2353    }
   2354  }
   2355 
   2356  LiveRange* preRange = LiveRange::FallibleNew(
   2357      alloc(), &input, inputRange->from(), outputOf(def.ins()));
   2358  if (!preRange) {
   2359    return false;
   2360  }
   2361 
   2362  // The new range starts at reg's input position, which means it overlaps
   2363  // with the old range at one position. This is what we want, because we
   2364  // need to copy the input before the instruction.
   2365  LiveRange* postRange = LiveRange::FallibleNew(
   2366      alloc(), &input, inputOf(def.ins()), inputRange->to());
   2367  if (!postRange) {
   2368    return false;
   2369  }
   2370 
   2371  inputRange->tryToMoveDefAndUsesInto(preRange);
   2372  inputRange->tryToMoveDefAndUsesInto(postRange);
   2373  MOZ_ASSERT(!inputRange->hasUses());
   2374 
   2375  JitSpewIfEnabled(JitSpew_RegAlloc,
   2376                   "  splitting reused input at %u to try to help grouping",
   2377                   inputOf(def.ins()).bits());
   2378 
   2379  LiveBundle* firstBundle = inputRange->bundle();
   2380 
   2381  // Note: this inserts an item at the beginning of the ranges vector. That's
   2382  // okay in this case because the checks above ensure this happens at most once
   2383  // for the |input| virtual register.
   2384  if (!input.replaceLastRangeLinear(inputRange, preRange, postRange)) {
   2385    return false;
   2386  }
   2387 
   2388  firstBundle->removeRange(inputRange);
   2389  firstBundle->addRange(preRange);
   2390 
   2391  // The new range goes in a separate bundle, where it will be spilled during
   2392  // allocation.
   2393  LiveBundle* secondBundle =
   2394      LiveBundle::FallibleNew(alloc(), nullptr, nullptr, getNextBundleId());
   2395  if (!secondBundle) {
   2396    return false;
   2397  }
   2398  secondBundle->addRangeAtEnd(postRange);
   2399 
   2400  tryMergeBundles(def.firstBundle(), input.firstBundle());
   2401  return true;
   2402 }
   2403 
   2404 bool BacktrackingAllocator::mergeAndQueueRegisters() {
   2405  MOZ_ASSERT(!vregs[0u].hasRanges());
   2406 
   2407  // Create a bundle for each register containing all its ranges.
   2408  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   2409    VirtualRegister& reg = vregs[i];
   2410    if (!reg.hasRanges()) {
   2411      continue;
   2412    }
   2413 
   2414    LiveBundle* bundle =
   2415        LiveBundle::FallibleNew(alloc(), nullptr, nullptr, getNextBundleId());
   2416    if (!bundle) {
   2417      return false;
   2418    }
   2419    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   2420      bundle->addRangeAtEnd(*iter);
   2421    }
   2422  }
   2423 
   2424  // If there is an OSR block, merge parameters in that block with the
   2425  // corresponding parameters in the initial block.
   2426  if (MBasicBlock* osr = graph.mir().osrBlock()) {
   2427    size_t original = 1;
   2428    for (LInstructionIterator iter = osr->lir()->begin();
   2429         iter != osr->lir()->end(); iter++) {
   2430      if (iter->isParameter()) {
   2431        for (size_t i = 0; i < iter->numDefs(); i++) {
   2432          DebugOnly<bool> found = false;
   2433          VirtualRegister& paramVreg = vreg(iter->getDef(i));
   2434          for (; original < paramVreg.vreg(); original++) {
   2435            VirtualRegister& originalVreg = vregs[original];
   2436            if (*originalVreg.def()->output() == *iter->getDef(i)->output()) {
   2437              MOZ_ASSERT(originalVreg.ins()->isParameter());
   2438              tryMergeBundles(originalVreg.firstBundle(),
   2439                              paramVreg.firstBundle());
   2440              found = true;
   2441              break;
   2442            }
   2443          }
   2444          MOZ_ASSERT(found);
   2445        }
   2446      }
   2447    }
   2448  }
   2449 
   2450  // Try to merge registers with their reused inputs.
   2451  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   2452    VirtualRegister& reg = vregs[i];
   2453    if (!reg.hasRanges()) {
   2454      continue;
   2455    }
   2456 
   2457    if (reg.def()->policy() == LDefinition::MUST_REUSE_INPUT) {
   2458      LUse* use = reg.ins()
   2459                      ->toInstruction()
   2460                      ->getOperand(reg.def()->getReusedInput())
   2461                      ->toUse();
   2462      if (!tryMergeReusedRegister(reg, vreg(use))) {
   2463        return false;
   2464      }
   2465    }
   2466  }
   2467 
   2468  // Try to merge phis with their inputs.
   2469  for (size_t i = 0; i < graph.numBlocks(); i++) {
   2470    LBlock* block = graph.getBlock(i);
   2471    for (size_t j = 0; j < block->numPhis(); j++) {
   2472      LPhi* phi = block->getPhi(j);
   2473      VirtualRegister& outputVreg = vreg(phi->getDef(0));
   2474      for (size_t k = 0, kend = phi->numOperands(); k < kend; k++) {
   2475        VirtualRegister& inputVreg = vreg(phi->getOperand(k)->toUse());
   2476        tryMergeBundles(inputVreg.firstBundle(), outputVreg.firstBundle());
   2477      }
   2478    }
   2479  }
   2480 
   2481  // Add all bundles to the allocation queue, and create spill sets for them.
   2482  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   2483    VirtualRegister& reg = vregs[i];
   2484 
   2485    // Eagerly allocate stack result areas and their component stack results.
   2486    if (reg.def() && reg.def()->policy() == LDefinition::STACK) {
   2487      allocateStackDefinition(reg);
   2488    }
   2489 
   2490    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   2491      LiveRange* range = *iter;
   2492      LiveBundle* bundle = range->bundle();
   2493      if (range == bundle->firstRange()) {
   2494        if (!alloc().ensureBallast()) {
   2495          return false;
   2496        }
   2497 
   2498        SpillSet* spill = SpillSet::New(alloc());
   2499        if (!spill) {
   2500          return false;
   2501        }
   2502        bundle->setSpillSet(spill);
   2503 
   2504        size_t priority = computePriority(bundle);
   2505        if (!allocationQueue.insert(QueueItem(bundle, priority))) {
   2506          return false;
   2507        }
   2508      }
   2509    }
   2510  }
   2511 
   2512  return true;
   2513 }
   2514 
   2515 ///////////////////////////////////////////////////////////////////////////////
   2516 //                                                                           //
   2517 // Code for the splitting/spilling subsystem begins here.                    //
   2518 //                                                                           //
   2519 // The code that follows is structured in the following sequence:            //
   2520 //                                                                           //
   2521 // (1) Routines that are helpers for ::splitAt.                              //
   2522 // (2) ::splitAt itself, which implements splitting decisions.               //
   2523 // (3) heuristic routines (eg ::splitAcrossCalls), which decide where        //
   2524 //     splits should be made.  They then call ::splitAt to perform the       //
   2525 //     chosen split.                                                         //
   2526 // (4) The top level driver, ::chooseBundleSplit.                            //
   2527 //                                                                           //
   2528 // There are further comments on ::splitAt and ::chooseBundleSplit below.    //
   2529 //                                                                           //
   2530 ///////////////////////////////////////////////////////////////////////////////
   2531 
   2532 ///////////////////////////////////////////////////////////////////////////////
   2533 //                                                                           //
   2534 // Implementation of splitting decisions, but not the making of those        //
   2535 // decisions: various helper functions                                       //
   2536 //                                                                           //
   2537 ///////////////////////////////////////////////////////////////////////////////
   2538 
   2539 bool BacktrackingAllocator::updateVirtualRegisterListsThenRequeueBundles(
   2540    LiveBundle* bundle, const LiveBundleVector& newBundles) {
   2541 #ifdef DEBUG
   2542  if (newBundles.length() == 1) {
   2543    LiveBundle* newBundle = newBundles[0];
   2544    if (newBundle->numRanges() == bundle->numRanges() &&
   2545        computePriority(newBundle) == computePriority(bundle)) {
   2546      bool different = false;
   2547      LiveBundle::RangeIterator oldRanges = bundle->rangesBegin();
   2548      LiveBundle::RangeIterator newRanges = newBundle->rangesBegin();
   2549      while (oldRanges) {
   2550        LiveRange* oldRange = *oldRanges;
   2551        LiveRange* newRange = *newRanges;
   2552        if (oldRange->from() != newRange->from() ||
   2553            oldRange->to() != newRange->to()) {
   2554          different = true;
   2555          break;
   2556        }
   2557        oldRanges++;
   2558        newRanges++;
   2559      }
   2560 
   2561      // This is likely to trigger an infinite loop in register allocation. This
   2562      // can be the result of invalid register constraints, making regalloc
   2563      // impossible; consider relaxing those.
   2564      MOZ_ASSERT(different,
   2565                 "Split results in the same bundle with the same priority");
   2566    }
   2567  }
   2568 #endif
   2569 
   2570  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   2571    JitSpew(JitSpew_RegAlloc, "  .. into:");
   2572    for (size_t i = 0; i < newBundles.length(); i++) {
   2573      JitSpew(JitSpew_RegAlloc, "    %s", newBundles[i]->toString().get());
   2574    }
   2575  }
   2576 
   2577  // Remove all ranges in the old bundle from their register's list.
   2578  bundle->removeAllRangesFromVirtualRegisters();
   2579 
   2580  // Add all ranges in the new bundles to their register's list.
   2581  for (size_t i = 0; i < newBundles.length(); i++) {
   2582    LiveBundle* newBundle = newBundles[i];
   2583    for (LiveBundle::RangeIterator iter = newBundle->rangesBegin(); iter;
   2584         iter++) {
   2585      LiveRange* range = *iter;
   2586      if (!range->vreg().addRange(range)) {
   2587        return false;
   2588      }
   2589    }
   2590  }
   2591 
   2592  // Queue the new bundles for register assignment.
   2593  for (size_t i = 0; i < newBundles.length(); i++) {
   2594    LiveBundle* newBundle = newBundles[i];
   2595    size_t priority = computePriority(newBundle);
   2596    if (!allocationQueue.insert(QueueItem(newBundle, priority))) {
   2597      return false;
   2598    }
   2599  }
   2600 
   2601  return true;
   2602 }
   2603 
   2604 // Helper for ::splitAt
   2605 // When splitting a bundle according to a list of split positions, return
   2606 // whether a use or range at |pos| should use a different bundle than the last
   2607 // position this was called for.
   2608 static bool UseNewBundle(const SplitPositionVector& splitPositions,
   2609                         CodePosition pos, size_t* activeSplitPosition) {
   2610  if (splitPositions.empty()) {
   2611    // When the split positions are empty we are splitting at all uses.
   2612    return true;
   2613  }
   2614 
   2615  if (*activeSplitPosition == splitPositions.length()) {
   2616    // We've advanced past all split positions.
   2617    return false;
   2618  }
   2619 
   2620  if (splitPositions[*activeSplitPosition] > pos) {
   2621    // We haven't gotten to the next split position yet.
   2622    return false;
   2623  }
   2624 
   2625  // We've advanced past the next split position, find the next one which we
   2626  // should split at.
   2627  while (*activeSplitPosition < splitPositions.length() &&
   2628         splitPositions[*activeSplitPosition] <= pos) {
   2629    (*activeSplitPosition)++;
   2630  }
   2631  return true;
   2632 }
   2633 
   2634 // Helper for ::splitAt
   2635 static bool HasPrecedingRangeSharingVreg(LiveBundle* bundle, LiveRange* range) {
   2636  MOZ_ASSERT(range->bundle() == bundle);
   2637 
   2638  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   2639    LiveRange* prevRange = *iter;
   2640    if (prevRange == range) {
   2641      return false;
   2642    }
   2643    if (&prevRange->vreg() == &range->vreg()) {
   2644      return true;
   2645    }
   2646  }
   2647 
   2648  MOZ_CRASH();
   2649 }
   2650 
   2651 // Helper for ::splitAt
   2652 static bool HasFollowingRangeSharingVreg(LiveBundle* bundle, LiveRange* range) {
   2653  MOZ_ASSERT(range->bundle() == bundle);
   2654 
   2655  LiveBundle::RangeIterator iter = bundle->rangesBegin(range);
   2656  MOZ_ASSERT(*iter == range);
   2657  iter++;
   2658 
   2659  for (; iter; iter++) {
   2660    LiveRange* nextRange = *iter;
   2661    if (&nextRange->vreg() == &range->vreg()) {
   2662      return true;
   2663    }
   2664  }
   2665 
   2666  return false;
   2667 }
   2668 
   2669 ///////////////////////////////////////////////////////////////////////////////
   2670 //                                                                           //
   2671 // Implementation of splitting decisions, but not the making of those        //
   2672 // decisions:                                                                //
   2673 //   ::splitAt                                                               //
   2674 //                                                                           //
   2675 ///////////////////////////////////////////////////////////////////////////////
   2676 
   2677 // ::splitAt
   2678 // ---------
   2679 // It would be nice to be able to interpret ::splitAt as simply performing
   2680 // whatever split the heuristic routines decide on.  Unfortunately it
   2681 // tries to "improve" on the initial locations, which as
   2682 // https://bugzilla.mozilla.org/show_bug.cgi?id=1758274#c17 shows, often
   2683 // leads to excessive spilling.  So there is no clean distinction between
   2684 // policy (where to split, computed by the heuristic routines) and
   2685 // implementation (done by ::splitAt).
   2686 //
   2687 // ::splitAt -- creation of spill parent bundles
   2688 // ---------------------------------------------
   2689 // To understand what ::splitAt does, we must refer back to Section 1's
   2690 // description of LiveBundle::spillParent_.
   2691 //
   2692 // Initially (as created by Phase 1), all bundles have `spillParent_` being
   2693 // NULL.  If ::splitAt is asked to split such a bundle, it will first create a
   2694 // "spill bundle" or "spill parent" bundle.  This is a copy of the original,
   2695 // with two changes:
   2696 //
   2697 // * all register uses have been removed, so that only stack-compatible uses
   2698 //   remain.
   2699 //
   2700 // * for all LiveRanges in the bundle that define a register, the start point
   2701 //   of the range is moved one CodePosition forwards, thusly:
   2702 //
   2703 //   from = minimalDefEnd(insData[from]).next();
   2704 //
   2705 // The reason for the latter relates to the idea described in Section 1, that
   2706 // all LiveRanges for any given VirtualRegister must form a tree rooted at the
   2707 // defining LiveRange.  If the spill-bundle definition range start points are
   2708 // the same as those in the original bundle, then we will end up with two roots
   2709 // for the tree, and it is then unclear which one should supply "the value".
   2710 //
   2711 // Putting the spill-bundle start point one CodePosition further along causes
   2712 // the range containing the register def (after splitting) to still be the
   2713 // defining point.  ::createMoveGroupsFromLiveRangeTransitions will observe the
   2714 // equivalent spill-bundle range starting one point later and add a MoveGroup
   2715 // to move the value into it.  Since the spill bundle is intended to be stack
   2716 // resident, the effect is to force creation of the MoveGroup that will
   2717 // actually spill this value onto the stack.
   2718 //
   2719 // If the bundle provided to ::splitAt already has a spill parent, then
   2720 // ::splitAt doesn't create a new spill parent.  This situation will happen
   2721 // when the bundle to be split was itself created by splitting.  The effect is
   2722 // that *all* bundles created from an "original bundle" share the same spill
   2723 // parent, and hence they will share the same spill slot, which guarantees that
   2724 // all the spilled fragments of a VirtualRegister share the same spill slot,
   2725 // which means we'll never have to move a VirtualRegister between different
   2726 // spill slots during its lifetime.
   2727 //
   2728 // ::splitAt -- creation of other bundles
   2729 // --------------------------------------
   2730 // With the spill parent bundle question out of the way, ::splitAt then goes on
   2731 // to create the remaining new bundles, using near-incomprehensible logic
   2732 // steered by `UseNewBundle`.
   2733 //
   2734 // This supposedly splits the bundle at the positions given by the
   2735 // `SplitPositionVector` parameter to ::splitAt, putting them in a temporary
   2736 // vector `newBundles`.  Whether it really splits at the requested positions or
   2737 // not is hard to say; more important is what happens next.
   2738 //
   2739 // ::splitAt -- "improvement" ("filtering") of the split bundles
   2740 // -------------------------------------------------------------
   2741 // ::splitAt now tries to reduce the length of the LiveRanges that make up the
   2742 // new bundles (not including the "spill parent").  I assume this is to remove
   2743 // sections of the bundles that carry no useful value (eg, extending after the
   2744 // last using a range), thereby removing the demand for registers in those
   2745 // parts.  This does however mean that ::splitAt is no longer really splitting
   2746 // where the heuristic routines wanted, and that can lead to a big increase in
   2747 // spilling in loops, as
   2748 // https://bugzilla.mozilla.org/show_bug.cgi?id=1758274#c17 describes.
   2749 //
   2750 // ::splitAt -- meaning of the incoming `SplitPositionVector`
   2751 // ----------------------------------------------------------
   2752 // ::splitAt has one last mystery which is important to document.  The split
   2753 // positions are specified as CodePositions, but this leads to ambiguity
   2754 // because, in a sequence of N (LIR) instructions, there are 2N valid
   2755 // CodePositions.  For example:
   2756 //
   2757 //    6-7 WasmLoadTls [def v2<o>] [use v1:R]
   2758 //    8-9 WasmNullConstant [def v3<o>]
   2759 //
   2760 // Consider splitting the range for `v2`, which starts at CodePosition 7.
   2761 // What's the difference between saying "split it at 7" and "split it at 8" ?
   2762 // Not much really, since in both cases what we intend is for the range to be
   2763 // split in between the two instructions.
   2764 //
   2765 // Hence I believe the semantics is:
   2766 //
   2767 // * splitting at an even numbered CodePosition (eg, 8), which is an input-side
   2768 //   position, means "split before the instruction containing this position".
   2769 //
   2770 // * splitting at an odd numbered CodePositin (eg, 7), which is an output-side
   2771 //   position, means "split after the instruction containing this position".
   2772 //
   2773 // Hence in the example, we could specify either 7 or 8 to mean the same
   2774 // placement of the split.  Well, almost true, but actually:
   2775 //
   2776 // (SPEC) specifying 8 means
   2777 //
   2778 //   "split between these two insns, and any resulting MoveGroup goes in the
   2779 //    list to be emitted before the start of the second insn"
   2780 //
   2781 // (SPEC) specifying 7 means
   2782 //
   2783 //   "split between these two insns, and any resulting MoveGroup goes in the
   2784 //    list to be emitted after the end of the first insn"
   2785 //
   2786 // In most cases we don't care on which "side of the valley" the MoveGroup ends
   2787 // up, in which case we can use either convention.
   2788 //
   2789 // (SPEC) I believe these semantics are implied by the logic in
   2790 // ::createMoveGroupsFromLiveRangeTransitions.  They are certainly not
   2791 // documented anywhere in the code.
   2792 
   2793 bool BacktrackingAllocator::splitAt(LiveBundle* bundle,
   2794                                    const SplitPositionVector& splitPositions) {
   2795  // Split the bundle at the given split points. Register uses which have no
   2796  // intervening split points are consolidated into the same bundle. If the
   2797  // list of split points is empty, then all register uses are placed in
   2798  // minimal bundles.
   2799 
   2800  // splitPositions should be sorted.
   2801  for (size_t i = 1; i < splitPositions.length(); ++i) {
   2802    MOZ_ASSERT(splitPositions[i - 1] < splitPositions[i]);
   2803  }
   2804 
   2805  // We don't need to create a new spill bundle if there already is one.
   2806  bool spillBundleIsNew = false;
   2807  LiveBundle* spillBundle = bundle->spillParent();
   2808  if (!spillBundle) {
   2809    spillBundle = LiveBundle::FallibleNew(alloc(), bundle->spillSet(), nullptr,
   2810                                          getNextBundleId());
   2811    if (!spillBundle) {
   2812      return false;
   2813    }
   2814    spillBundleIsNew = true;
   2815 
   2816    for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   2817      LiveRange* range = *iter;
   2818 
   2819      CodePosition from = range->from();
   2820      if (isRegisterDefinition(range)) {
   2821        from = minimalDefEnd(insData[from]).next();
   2822      }
   2823 
   2824      if (from < range->to()) {
   2825        if (!spillBundle->addRangeAtEnd(alloc(), &range->vreg(), from,
   2826                                        range->to())) {
   2827          return false;
   2828        }
   2829 
   2830        if (range->hasDefinition() && !isRegisterDefinition(range)) {
   2831          spillBundle->lastRange()->setHasDefinition();
   2832        }
   2833      }
   2834    }
   2835  }
   2836 
   2837  LiveBundleVector newBundles;
   2838 
   2839  // The bundle which ranges are currently being added to.
   2840  LiveBundle* activeBundle = LiveBundle::FallibleNew(
   2841      alloc(), bundle->spillSet(), spillBundle, getNextBundleId());
   2842  if (!activeBundle || !newBundles.append(activeBundle)) {
   2843    return false;
   2844  }
   2845 
   2846  // State for use by UseNewBundle.
   2847  size_t activeSplitPosition = 0;
   2848 
   2849  // Make new bundles according to the split positions, and distribute ranges
   2850  // and uses to them.
   2851  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   2852    LiveRange* range = *iter;
   2853 
   2854    if (UseNewBundle(splitPositions, range->from(), &activeSplitPosition)) {
   2855      activeBundle = LiveBundle::FallibleNew(alloc(), bundle->spillSet(),
   2856                                             spillBundle, getNextBundleId());
   2857      if (!activeBundle || !newBundles.append(activeBundle)) {
   2858        return false;
   2859      }
   2860    }
   2861 
   2862    LiveRange* activeRange = LiveRange::FallibleNew(alloc(), &range->vreg(),
   2863                                                    range->from(), range->to());
   2864    if (!activeRange) {
   2865      return false;
   2866    }
   2867    activeBundle->addRangeAtEnd(activeRange);
   2868 
   2869    if (isRegisterDefinition(range)) {
   2870      activeRange->setHasDefinition();
   2871    }
   2872 
   2873    while (range->hasUses()) {
   2874      UsePosition* use = range->popUse();
   2875      LNode* ins = insData[use->pos];
   2876 
   2877      // Any uses of a register that appear before its definition has
   2878      // finished must be associated with the range for that definition.
   2879      if (isRegisterDefinition(range) &&
   2880          use->pos <= minimalDefEnd(insData[range->from()])) {
   2881        activeRange->addUse(use);
   2882      } else if (isRegisterUse(use, ins)) {
   2883        // Place this register use into a different bundle from the
   2884        // last one if there are any split points between the two uses.
   2885        // UseNewBundle always returns true if we are splitting at all
   2886        // register uses, but we can still reuse the last range and
   2887        // bundle if they have uses at the same position, except when
   2888        // either use is fixed (the two uses might require incompatible
   2889        // registers.)
   2890        if (UseNewBundle(splitPositions, use->pos, &activeSplitPosition) &&
   2891            (!activeRange->hasUses() ||
   2892             activeRange->usesBegin()->pos != use->pos ||
   2893             activeRange->usesBegin()->usePolicy() == LUse::FIXED ||
   2894             use->usePolicy() == LUse::FIXED)) {
   2895          activeBundle = LiveBundle::FallibleNew(
   2896              alloc(), bundle->spillSet(), spillBundle, getNextBundleId());
   2897          if (!activeBundle || !newBundles.append(activeBundle)) {
   2898            return false;
   2899          }
   2900          activeRange = LiveRange::FallibleNew(alloc(), &range->vreg(),
   2901                                               range->from(), range->to());
   2902          if (!activeRange) {
   2903            return false;
   2904          }
   2905          activeBundle->addRangeAtEnd(activeRange);
   2906        }
   2907 
   2908        activeRange->addUse(use);
   2909      } else {
   2910        MOZ_ASSERT(spillBundleIsNew);
   2911        spillBundle->rangeFor(use->pos)->addUse(use);
   2912      }
   2913    }
   2914  }
   2915 
   2916  LiveBundleVector filteredBundles;
   2917 
   2918  // Trim the ends of ranges in each new bundle when there are no other
   2919  // earlier or later ranges in the same bundle with the same vreg.
   2920  for (size_t i = 0; i < newBundles.length(); i++) {
   2921    LiveBundle* bundle = newBundles[i];
   2922 
   2923    for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter;) {
   2924      LiveRange* range = *iter;
   2925 
   2926      if (!range->hasDefinition()) {
   2927        if (!HasPrecedingRangeSharingVreg(bundle, range)) {
   2928          if (range->hasUses()) {
   2929            UsePosition* use = *range->usesBegin();
   2930            range->setFrom(inputOf(insData[use->pos]));
   2931          } else {
   2932            bundle->removeRangeAndIncrementIterator(iter);
   2933            continue;
   2934          }
   2935        }
   2936      }
   2937 
   2938      if (!HasFollowingRangeSharingVreg(bundle, range)) {
   2939        if (range->hasUses()) {
   2940          UsePosition* use = range->lastUse();
   2941          range->setTo(use->pos.next());
   2942        } else if (range->hasDefinition()) {
   2943          range->setTo(minimalDefEnd(insData[range->from()]).next());
   2944        } else {
   2945          bundle->removeRangeAndIncrementIterator(iter);
   2946          continue;
   2947        }
   2948      }
   2949 
   2950      iter++;
   2951    }
   2952 
   2953    if (bundle->hasRanges() && !filteredBundles.append(bundle)) {
   2954      return false;
   2955    }
   2956  }
   2957 
   2958  if (spillBundleIsNew && !filteredBundles.append(spillBundle)) {
   2959    return false;
   2960  }
   2961 
   2962  return updateVirtualRegisterListsThenRequeueBundles(bundle, filteredBundles);
   2963 }
   2964 
   2965 ///////////////////////////////////////////////////////////////////////////////
   2966 //                                                                           //
   2967 // Creation of splitting decisions, but not their implementation:            //
   2968 //   ::splitAcrossCalls                                                      //
   2969 //   ::trySplitAcrossHotcode                                                 //
   2970 //   ::trySplitAfterLastRegisterUse                                          //
   2971 //   ::trySplitBeforeFirstRegisterUse                                        //
   2972 //                                                                           //
   2973 ///////////////////////////////////////////////////////////////////////////////
   2974 
   2975 bool BacktrackingAllocator::splitAcrossCalls(LiveBundle* bundle) {
   2976  // Split the bundle to separate register uses and non-register uses and
   2977  // allow the vreg to be spilled across its range.
   2978 
   2979  // Locations of all calls in the bundle's range. This vector is sorted in
   2980  // ascending order because ranges in the bundle are sorted by start position
   2981  // and don't overlap
   2982  SplitPositionVector bundleCallPositions;
   2983 
   2984  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   2985    LiveRange* range = *iter;
   2986 
   2987    // Append call positions in range [from+1, to) to bundleCallPositions.
   2988    // Calls at the beginning of the range are ignored because there is no
   2989    // splitting to do in this case.
   2990 
   2991    CodePosition from = range->from().next();
   2992    if (from == range->to()) {
   2993      // Empty range can't contain any calls.
   2994      continue;
   2995    }
   2996 
   2997    // Use binary search to find the first call position.
   2998    Maybe<size_t> index = lookupFirstCallPositionInRange(from, range->to());
   2999    if (!index.isSome()) {
   3000      // There are no calls inside this range.
   3001      continue;
   3002    }
   3003 
   3004    // Find the index of the last call within this range.
   3005    size_t startIndex = *index;
   3006    size_t endIndex = startIndex;
   3007    while (endIndex < callPositions.length() - 1) {
   3008      if (callPositions[endIndex + 1] >= range->to()) {
   3009        break;
   3010      }
   3011      endIndex++;
   3012    }
   3013 
   3014    MOZ_ASSERT(startIndex <= endIndex);
   3015 
   3016 #ifdef DEBUG
   3017    auto inRange = [range](CodePosition pos) {
   3018      return range->covers(pos) && pos != range->from();
   3019    };
   3020 
   3021    // Assert startIndex is the first call position in this range.
   3022    MOZ_ASSERT(inRange(callPositions[startIndex]));
   3023    MOZ_ASSERT_IF(startIndex > 0, !inRange(callPositions[startIndex - 1]));
   3024 
   3025    // Assert endIndex is the last call position in this range.
   3026    MOZ_ASSERT(inRange(callPositions[endIndex]));
   3027    MOZ_ASSERT_IF(endIndex + 1 < callPositions.length(),
   3028                  !inRange(callPositions[endIndex + 1]));
   3029 
   3030    // Assert bundleCallPositions stays sorted.
   3031    MOZ_ASSERT_IF(!bundleCallPositions.empty(),
   3032                  bundleCallPositions.back() < callPositions[startIndex]);
   3033 #endif
   3034 
   3035    const CodePosition* start = &callPositions[startIndex];
   3036    size_t count = endIndex - startIndex + 1;
   3037    if (!bundleCallPositions.append(start, start + count)) {
   3038      return false;
   3039    }
   3040  }
   3041 
   3042  MOZ_ASSERT(!bundleCallPositions.empty());
   3043 
   3044 #ifdef JS_JITSPEW
   3045  JitSpewStart(JitSpew_RegAlloc, "  .. split across calls at ");
   3046  for (size_t i = 0; i < bundleCallPositions.length(); ++i) {
   3047    JitSpewCont(JitSpew_RegAlloc, "%s%u", i != 0 ? ", " : "",
   3048                bundleCallPositions[i].bits());
   3049  }
   3050  JitSpewFin(JitSpew_RegAlloc);
   3051 #endif
   3052 
   3053  return splitAt(bundle, bundleCallPositions);
   3054 }
   3055 
   3056 bool BacktrackingAllocator::trySplitAcrossHotcode(LiveBundle* bundle,
   3057                                                  bool* success) {
   3058  // If this bundle has portions that are hot and portions that are cold,
   3059  // split it at the boundaries between hot and cold code.
   3060 
   3061  LiveRange* hotRange = nullptr;
   3062 
   3063  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3064    LiveRange* range = *iter;
   3065    if (hotcode.contains(range, &hotRange)) {
   3066      break;
   3067    }
   3068  }
   3069 
   3070  // Don't split if there is no hot code in the bundle.
   3071  if (!hotRange) {
   3072    JitSpew(JitSpew_RegAlloc, "  .. bundle does not contain hot code");
   3073    return true;
   3074  }
   3075 
   3076  // Don't split if there is no cold code in the bundle.
   3077  bool coldCode = false;
   3078  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3079    LiveRange* range = *iter;
   3080    if (!hotRange->contains(range)) {
   3081      coldCode = true;
   3082      break;
   3083    }
   3084  }
   3085  if (!coldCode) {
   3086    JitSpew(JitSpew_RegAlloc, "  .. bundle does not contain cold code");
   3087    return true;
   3088  }
   3089 
   3090  JitSpewIfEnabled(JitSpew_RegAlloc, "  .. split across hot range %s",
   3091                   hotRange->toString().get());
   3092 
   3093  // Tweak the splitting method when compiling wasm code to look at actual
   3094  // uses within the hot/cold code. This heuristic is in place as the below
   3095  // mechanism regresses several asm.js tests. Hopefully this will be fixed
   3096  // soon and this special case removed. See bug 948838.
   3097  if (compilingWasm()) {
   3098    SplitPositionVector splitPositions;
   3099    if (!splitPositions.append(hotRange->from()) ||
   3100        !splitPositions.append(hotRange->to())) {
   3101      return false;
   3102    }
   3103    *success = true;
   3104    return splitAt(bundle, splitPositions);
   3105  }
   3106 
   3107  LiveBundle* hotBundle = LiveBundle::FallibleNew(
   3108      alloc(), bundle->spillSet(), bundle->spillParent(), getNextBundleId());
   3109  if (!hotBundle) {
   3110    return false;
   3111  }
   3112  LiveBundle* preBundle = nullptr;
   3113  LiveBundle* postBundle = nullptr;
   3114 
   3115  // Accumulate the ranges of hot and cold code in the bundle. Note that
   3116  // we are only comparing with the single hot range found, so the cold code
   3117  // may contain separate hot ranges.
   3118  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3119    LiveRange* range = *iter;
   3120    LiveRange::Range hot, coldPre, coldPost;
   3121    range->intersect(hotRange, &coldPre, &hot, &coldPost);
   3122 
   3123    if (!hot.empty()) {
   3124      if (!hotBundle->addRangeAndDistributeUses(alloc(), range, hot.from,
   3125                                                hot.to)) {
   3126        return false;
   3127      }
   3128    }
   3129 
   3130    if (!coldPre.empty()) {
   3131      if (!preBundle) {
   3132        preBundle =
   3133            LiveBundle::FallibleNew(alloc(), bundle->spillSet(),
   3134                                    bundle->spillParent(), getNextBundleId());
   3135        if (!preBundle) {
   3136          return false;
   3137        }
   3138      }
   3139      if (!preBundle->addRangeAndDistributeUses(alloc(), range, coldPre.from,
   3140                                                coldPre.to)) {
   3141        return false;
   3142      }
   3143    }
   3144 
   3145    if (!coldPost.empty()) {
   3146      if (!postBundle) {
   3147        postBundle =
   3148            LiveBundle::FallibleNew(alloc(), bundle->spillSet(),
   3149                                    bundle->spillParent(), getNextBundleId());
   3150        if (!postBundle) {
   3151          return false;
   3152        }
   3153      }
   3154      if (!postBundle->addRangeAndDistributeUses(alloc(), range, coldPost.from,
   3155                                                 coldPost.to)) {
   3156        return false;
   3157      }
   3158    }
   3159  }
   3160 
   3161  MOZ_ASSERT(hotBundle->numRanges() != 0);
   3162 
   3163  LiveBundleVector newBundles;
   3164  if (!newBundles.append(hotBundle)) {
   3165    return false;
   3166  }
   3167 
   3168  MOZ_ASSERT(preBundle || postBundle);
   3169  if (preBundle && !newBundles.append(preBundle)) {
   3170    return false;
   3171  }
   3172  if (postBundle && !newBundles.append(postBundle)) {
   3173    return false;
   3174  }
   3175 
   3176  *success = true;
   3177  return updateVirtualRegisterListsThenRequeueBundles(bundle, newBundles);
   3178 }
   3179 
   3180 bool BacktrackingAllocator::trySplitAfterLastRegisterUse(LiveBundle* bundle,
   3181                                                         LiveBundle* conflict,
   3182                                                         bool* success) {
   3183  // If this bundle's later uses do not require it to be in a register,
   3184  // split it after the last use which does require a register. If conflict
   3185  // is specified, only consider register uses before the conflict starts.
   3186 
   3187  CodePosition lastRegisterFrom, lastRegisterTo, lastUse;
   3188 
   3189  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3190    LiveRange* range = *iter;
   3191 
   3192    // If the range defines a register, consider that a register use for
   3193    // our purposes here.
   3194    if (isRegisterDefinition(range)) {
   3195      CodePosition spillStart = minimalDefEnd(insData[range->from()]).next();
   3196      if (!conflict || spillStart < conflict->firstRange()->from()) {
   3197        lastUse = lastRegisterFrom = range->from();
   3198        lastRegisterTo = spillStart;
   3199      }
   3200    }
   3201 
   3202    for (UsePositionIterator iter(range->usesBegin()); iter; iter++) {
   3203      LNode* ins = insData[iter->pos];
   3204 
   3205      // Uses in the bundle should be sorted.
   3206      MOZ_ASSERT(iter->pos >= lastUse);
   3207      lastUse = inputOf(ins);
   3208 
   3209      if (!conflict || outputOf(ins) < conflict->firstRange()->from()) {
   3210        if (isRegisterUse(*iter, ins, /* considerCopy = */ true)) {
   3211          lastRegisterFrom = inputOf(ins);
   3212          lastRegisterTo = iter->pos.next();
   3213        }
   3214      }
   3215    }
   3216  }
   3217 
   3218  // Can't trim non-register uses off the end by splitting.
   3219  if (!lastRegisterFrom.bits()) {
   3220    JitSpew(JitSpew_RegAlloc, "  .. bundle has no register uses");
   3221    return true;
   3222  }
   3223  if (lastUse < lastRegisterTo) {
   3224    JitSpew(JitSpew_RegAlloc, "  .. bundle's last use is a register use");
   3225    return true;
   3226  }
   3227 
   3228  JitSpewIfEnabled(JitSpew_RegAlloc, "  .. split after last register use at %u",
   3229                   lastRegisterTo.bits());
   3230 
   3231  SplitPositionVector splitPositions;
   3232  if (!splitPositions.append(lastRegisterTo)) {
   3233    return false;
   3234  }
   3235  *success = true;
   3236  return splitAt(bundle, splitPositions);
   3237 }
   3238 
   3239 bool BacktrackingAllocator::trySplitBeforeFirstRegisterUse(LiveBundle* bundle,
   3240                                                           LiveBundle* conflict,
   3241                                                           bool* success) {
   3242  // If this bundle's earlier uses do not require it to be in a register,
   3243  // split it before the first use which does require a register. If conflict
   3244  // is specified, only consider register uses after the conflict ends.
   3245 
   3246  if (isRegisterDefinition(bundle->firstRange())) {
   3247    JitSpew(JitSpew_RegAlloc, "  .. bundle is defined by a register");
   3248    return true;
   3249  }
   3250  if (!bundle->firstRange()->hasDefinition()) {
   3251    JitSpew(JitSpew_RegAlloc, "  .. bundle does not have definition");
   3252    return true;
   3253  }
   3254 
   3255  CodePosition firstRegisterFrom;
   3256 
   3257  CodePosition conflictEnd;
   3258  if (conflict) {
   3259    for (LiveBundle::RangeIterator iter = conflict->rangesBegin(); iter;
   3260         iter++) {
   3261      LiveRange* range = *iter;
   3262      if (range->to() > conflictEnd) {
   3263        conflictEnd = range->to();
   3264      }
   3265    }
   3266  }
   3267 
   3268  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3269    LiveRange* range = *iter;
   3270 
   3271    if (!conflict || range->from() > conflictEnd) {
   3272      if (range->hasDefinition() && isRegisterDefinition(range)) {
   3273        firstRegisterFrom = range->from();
   3274        break;
   3275      }
   3276    }
   3277 
   3278    for (UsePositionIterator iter(range->usesBegin()); iter; iter++) {
   3279      LNode* ins = insData[iter->pos];
   3280 
   3281      if (!conflict || outputOf(ins) >= conflictEnd) {
   3282        if (isRegisterUse(*iter, ins, /* considerCopy = */ true)) {
   3283          firstRegisterFrom = inputOf(ins);
   3284          break;
   3285        }
   3286      }
   3287    }
   3288    if (firstRegisterFrom.bits()) {
   3289      break;
   3290    }
   3291  }
   3292 
   3293  if (!firstRegisterFrom.bits()) {
   3294    // Can't trim non-register uses off the beginning by splitting.
   3295    JitSpew(JitSpew_RegAlloc, "  bundle has no register uses");
   3296    return true;
   3297  }
   3298 
   3299  JitSpewIfEnabled(JitSpew_RegAlloc,
   3300                   "  .. split before first register use at %u",
   3301                   firstRegisterFrom.bits());
   3302 
   3303  SplitPositionVector splitPositions;
   3304  if (!splitPositions.append(firstRegisterFrom)) {
   3305    return false;
   3306  }
   3307  *success = true;
   3308  return splitAt(bundle, splitPositions);
   3309 }
   3310 
   3311 ///////////////////////////////////////////////////////////////////////////////
   3312 //                                                                           //
   3313 // The top level driver for the splitting machinery                          //
   3314 //                                                                           //
   3315 ///////////////////////////////////////////////////////////////////////////////
   3316 
   3317 // ::chooseBundleSplit
   3318 // -------------------
   3319 // If the first allocation loop (in ::go) can't allocate a bundle, it hands it
   3320 // off to ::chooseBundleSplit, which is the "entry point" of the bundle-split
   3321 // machinery.  This tries four heuristics in turn, to see if any can split the
   3322 // bundle:
   3323 //
   3324 // * ::trySplitAcrossHotcode
   3325 // * ::splitAcrossCalls (in some cases)
   3326 // * ::trySplitBeforeFirstRegisterUse
   3327 // * ::trySplitAfterLastRegisterUse
   3328 //
   3329 // These routines have similar structure: they try to decide on one or more
   3330 // CodePositions at which to split the bundle, using whatever heuristics they
   3331 // have to hand.  If suitable CodePosition(s) are found, they are put into a
   3332 // `SplitPositionVector`, and the bundle and the vector are handed off to
   3333 // ::splitAt, which performs the split (at least in theory) at the chosen
   3334 // positions.  It also arranges for the new bundles to be added to
   3335 // ::allocationQueue.
   3336 //
   3337 // ::trySplitAcrossHotcode has a special case for JS -- it modifies the
   3338 // bundle(s) itself, rather than using ::splitAt.
   3339 //
   3340 // If none of the heuristic routines apply, then ::splitAt is called with an
   3341 // empty vector of split points.  This is interpreted to mean "split at all
   3342 // register uses".  When combined with how ::splitAt works, the effect is to
   3343 // spill the bundle.
   3344 
   3345 bool BacktrackingAllocator::chooseBundleSplit(LiveBundle* bundle, bool hasCall,
   3346                                              LiveBundle* conflict) {
   3347  bool success = false;
   3348 
   3349  JitSpewIfEnabled(JitSpew_RegAlloc, "  Splitting %s ..",
   3350                   bundle->toString().get());
   3351 
   3352  if (!trySplitAcrossHotcode(bundle, &success)) {
   3353    return false;
   3354  }
   3355  if (success) {
   3356    return true;
   3357  }
   3358 
   3359  if (hasCall) {
   3360    return splitAcrossCalls(bundle);
   3361  }
   3362 
   3363  if (!trySplitBeforeFirstRegisterUse(bundle, conflict, &success)) {
   3364    return false;
   3365  }
   3366  if (success) {
   3367    return true;
   3368  }
   3369 
   3370  if (!trySplitAfterLastRegisterUse(bundle, conflict, &success)) {
   3371    return false;
   3372  }
   3373  if (success) {
   3374    return true;
   3375  }
   3376 
   3377  // Split at all register uses.
   3378  SplitPositionVector emptyPositions;
   3379  return splitAt(bundle, emptyPositions);
   3380 }
   3381 
   3382 ///////////////////////////////////////////////////////////////////////////////
   3383 //                                                                           //
   3384 // Bundle allocation                                                         //
   3385 //                                                                           //
   3386 ///////////////////////////////////////////////////////////////////////////////
   3387 
   3388 static const size_t MAX_ATTEMPTS = 2;
   3389 
   3390 bool BacktrackingAllocator::computeRequirement(LiveBundle* bundle,
   3391                                               Requirement* requirement,
   3392                                               Requirement* hint) {
   3393  // Set any requirement or hint on bundle according to its definition and
   3394  // uses. Return false if there are conflicting requirements which will
   3395  // require the bundle to be split.
   3396 
   3397  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3398    LiveRange* range = *iter;
   3399    VirtualRegister& reg = range->vreg();
   3400 
   3401    if (range->hasDefinition()) {
   3402      // Deal with any definition constraints/hints.
   3403      LDefinition::Policy policy = reg.def()->policy();
   3404      if (policy == LDefinition::FIXED || policy == LDefinition::STACK) {
   3405        // Fixed and stack policies get a FIXED requirement.  (In the stack
   3406        // case, the allocation should have been performed already by
   3407        // mergeAndQueueRegisters.)
   3408        JitSpewIfEnabled(JitSpew_RegAlloc,
   3409                         "  Requirement %s, fixed by definition",
   3410                         reg.def()->output()->toString().get());
   3411        if (!requirement->merge(Requirement(*reg.def()->output()))) {
   3412          return false;
   3413        }
   3414      } else if (reg.ins()->isPhi()) {
   3415        // Phis don't have any requirements, but they should prefer their
   3416        // input allocations. This is captured by the group hints above.
   3417      } else {
   3418        // Non-phis get a REGISTER requirement.
   3419        if (!requirement->merge(Requirement(Requirement::REGISTER))) {
   3420          return false;
   3421        }
   3422      }
   3423    }
   3424 
   3425    // Search uses for requirements.
   3426    for (UsePositionIterator iter = range->usesBegin(); iter; iter++) {
   3427      LUse::Policy policy = iter->usePolicy();
   3428      if (policy == LUse::FIXED) {
   3429        AnyRegister required = GetFixedRegister(reg.def(), iter->use());
   3430 
   3431        JitSpewIfEnabled(JitSpew_RegAlloc, "  Requirement %s, due to use at %u",
   3432                         required.name(), iter->pos.bits());
   3433 
   3434        // If there are multiple fixed registers which the bundle is
   3435        // required to use, fail. The bundle will need to be split before
   3436        // it can be allocated.
   3437        if (!requirement->merge(Requirement(LAllocation(required)))) {
   3438          return false;
   3439        }
   3440      } else if (policy == LUse::REGISTER) {
   3441        if (!requirement->merge(Requirement(Requirement::REGISTER))) {
   3442          return false;
   3443        }
   3444      } else if (policy == LUse::ANY) {
   3445        // ANY differs from KEEPALIVE by actively preferring a register.
   3446        if (!hint->merge(Requirement(Requirement::REGISTER))) {
   3447          return false;
   3448        }
   3449      }
   3450 
   3451      // The only case of STACK use policies is individual stack results using
   3452      // their containing stack result area, which is given a fixed allocation
   3453      // above.
   3454      MOZ_ASSERT_IF(policy == LUse::STACK,
   3455                    requirement->kind() == Requirement::FIXED);
   3456      MOZ_ASSERT_IF(policy == LUse::STACK,
   3457                    requirement->allocation().isStackArea());
   3458    }
   3459  }
   3460 
   3461  return true;
   3462 }
   3463 
   3464 bool BacktrackingAllocator::tryAllocateRegister(PhysicalRegister& r,
   3465                                                LiveBundle* bundle,
   3466                                                bool* success, bool* hasCall,
   3467                                                LiveBundleVector& conflicting) {
   3468  *success = false;
   3469 
   3470  // If we know this bundle contains a call (where all registers are spilled) we
   3471  // shouldn't try again to allocate a register.
   3472  MOZ_ASSERT(!*hasCall);
   3473 
   3474  if (!r.allocatable) {
   3475    return true;
   3476  }
   3477 
   3478  LiveBundleVector aliasedConflicting;
   3479 
   3480  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3481    LiveRange* range = *iter;
   3482    LiveRangePlus rangePlus(range);
   3483 
   3484    // All ranges in the bundle must be compatible with the physical register.
   3485    MOZ_ASSERT(range->vreg().isCompatible(r.reg));
   3486 
   3487    const size_t numAliased = r.reg.numAliased();
   3488    for (size_t a = 0; a < numAliased; a++) {
   3489      PhysicalRegister& rAlias = registers[r.reg.aliased(a).code()];
   3490      LiveRangePlus existingPlus;
   3491      if (!rAlias.allocations.contains(rangePlus, &existingPlus)) {
   3492        continue;
   3493      }
   3494      const LiveRange* existing = existingPlus.liveRange();
   3495      if (existing->hasVreg()) {
   3496        MOZ_ASSERT(existing->bundle()->allocation().toAnyRegister() ==
   3497                   rAlias.reg);
   3498        bool duplicate = false;
   3499        for (size_t i = 0; i < aliasedConflicting.length(); i++) {
   3500          if (aliasedConflicting[i] == existing->bundle()) {
   3501            duplicate = true;
   3502            break;
   3503          }
   3504        }
   3505        if (!duplicate && !aliasedConflicting.append(existing->bundle())) {
   3506          return false;
   3507        }
   3508      } else {
   3509        // This bundle contains a call instruction.
   3510        MOZ_ASSERT(lookupFirstCallPositionInRange(range->from(), range->to()));
   3511        JitSpewIfEnabled(JitSpew_RegAlloc, "  %s collides with fixed use %s",
   3512                         rAlias.reg.name(), existing->toString().get());
   3513        *hasCall = true;
   3514        return true;
   3515      }
   3516      MOZ_ASSERT(r.reg.numAliased() == numAliased);
   3517    }
   3518  }
   3519 
   3520  if (!aliasedConflicting.empty()) {
   3521    // One or more aliased registers is allocated to another bundle
   3522    // overlapping this one. Keep track of the conflicting set, and in the
   3523    // case of multiple conflicting sets keep track of the set with the
   3524    // lowest maximum spill weight.
   3525 
   3526    // The #ifdef guards against "unused variable 'existing'" bustage.
   3527 #ifdef JS_JITSPEW
   3528    if (JitSpewEnabled(JitSpew_RegAlloc)) {
   3529      if (aliasedConflicting.length() == 1) {
   3530        LiveBundle* existing = aliasedConflicting[0];
   3531        JitSpew(JitSpew_RegAlloc, "  %s collides with %s [weight %zu]",
   3532                r.reg.name(), existing->toString().get(),
   3533                computeSpillWeight(existing));
   3534      } else {
   3535        JitSpew(JitSpew_RegAlloc, "  %s collides with the following",
   3536                r.reg.name());
   3537        for (size_t i = 0; i < aliasedConflicting.length(); i++) {
   3538          LiveBundle* existing = aliasedConflicting[i];
   3539          JitSpew(JitSpew_RegAlloc, "    %s [weight %zu]",
   3540                  existing->toString().get(), computeSpillWeight(existing));
   3541        }
   3542      }
   3543    }
   3544 #endif
   3545 
   3546    if (conflicting.empty()) {
   3547      conflicting = std::move(aliasedConflicting);
   3548    } else {
   3549      if (maximumSpillWeight(aliasedConflicting) <
   3550          maximumSpillWeight(conflicting)) {
   3551        conflicting = std::move(aliasedConflicting);
   3552      }
   3553    }
   3554    return true;
   3555  }
   3556 
   3557  JitSpewIfEnabled(JitSpew_RegAlloc, "  allocated to %s", r.reg.name());
   3558 
   3559  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3560    LiveRange* range = *iter;
   3561    if (!alloc().ensureBallast()) {
   3562      return false;
   3563    }
   3564    LiveRangePlus rangePlus(range);
   3565    if (!r.allocations.insert(rangePlus)) {
   3566      return false;
   3567    }
   3568  }
   3569 
   3570  bundle->setAllocation(LAllocation(r.reg));
   3571  *success = true;
   3572  return true;
   3573 }
   3574 
   3575 bool BacktrackingAllocator::tryAllocateAnyRegister(
   3576    LiveBundle* bundle, bool* success, bool* hasCall,
   3577    LiveBundleVector& conflicting) {
   3578  // Search for any available register which the bundle can be allocated to.
   3579 
   3580  LDefinition::Type type = bundle->firstRange()->vreg().type();
   3581 
   3582  if (LDefinition::isFloatReg(type)) {
   3583    for (size_t i = AnyRegister::FirstFloatReg; i < AnyRegister::Total; i++) {
   3584      if (!LDefinition::isFloatRegCompatible(type, registers[i].reg.fpu())) {
   3585        continue;
   3586      }
   3587      if (!tryAllocateRegister(registers[i], bundle, success, hasCall,
   3588                               conflicting)) {
   3589        return false;
   3590      }
   3591      if (*success) {
   3592        break;
   3593      }
   3594      if (*hasCall) {
   3595        // This bundle contains a call instruction. Calls require spilling all
   3596        // registers, so we have to split or spill this bundle.
   3597        break;
   3598      }
   3599    }
   3600    return true;
   3601  }
   3602 
   3603  for (size_t i = 0; i < AnyRegister::FirstFloatReg; i++) {
   3604    if (!tryAllocateRegister(registers[i], bundle, success, hasCall,
   3605                             conflicting)) {
   3606      return false;
   3607    }
   3608    if (*success) {
   3609      break;
   3610    }
   3611    if (*hasCall) {
   3612      // This bundle contains a call instruction. Calls require spilling all
   3613      // registers, so we have to split or spill this bundle.
   3614      break;
   3615    }
   3616  }
   3617  return true;
   3618 }
   3619 
   3620 bool BacktrackingAllocator::evictBundle(LiveBundle* bundle) {
   3621  JitSpewIfEnabled(JitSpew_RegAlloc,
   3622                   "  Evicting %s [priority %zu] [weight %zu]",
   3623                   bundle->toString().get(), computePriority(bundle),
   3624                   computeSpillWeight(bundle));
   3625 
   3626  AnyRegister reg(bundle->allocation().toAnyRegister());
   3627  PhysicalRegister& physical = registers[reg.code()];
   3628  MOZ_ASSERT(physical.reg == reg && physical.allocatable);
   3629 
   3630  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3631    LiveRange* range = *iter;
   3632    LiveRangePlus rangePlus(range);
   3633    physical.allocations.remove(rangePlus);
   3634  }
   3635 
   3636  bundle->setAllocation(LAllocation());
   3637 
   3638  size_t priority = computePriority(bundle);
   3639  return allocationQueue.insert(QueueItem(bundle, priority));
   3640 }
   3641 
   3642 bool BacktrackingAllocator::tryAllocateFixed(LiveBundle* bundle,
   3643                                             Requirement requirement,
   3644                                             bool* success, bool* hasCall,
   3645                                             LiveBundleVector& conflicting) {
   3646  // Spill bundles which are required to be in a certain stack slot.
   3647  if (!requirement.allocation().isAnyRegister()) {
   3648    JitSpew(JitSpew_RegAlloc, "  stack allocation requirement");
   3649    bundle->setAllocation(requirement.allocation());
   3650    *success = true;
   3651    return true;
   3652  }
   3653 
   3654  AnyRegister reg = requirement.allocation().toAnyRegister();
   3655  return tryAllocateRegister(registers[reg.code()], bundle, success, hasCall,
   3656                             conflicting);
   3657 }
   3658 
   3659 bool BacktrackingAllocator::tryAllocateNonFixed(LiveBundle* bundle,
   3660                                                Requirement requirement,
   3661                                                Requirement hint, bool* success,
   3662                                                bool* hasCall,
   3663                                                LiveBundleVector& conflicting) {
   3664  MOZ_ASSERT(hint.kind() != Requirement::FIXED);
   3665  MOZ_ASSERT(conflicting.empty());
   3666 
   3667  // Spill bundles which have no hint or register requirement.
   3668  if (requirement.kind() == Requirement::NONE &&
   3669      hint.kind() == Requirement::NONE) {
   3670    JitSpew(JitSpew_RegAlloc,
   3671            "  postponed spill (no hint or register requirement)");
   3672    if (!spilledBundles.append(bundle)) {
   3673      return false;
   3674    }
   3675    *success = true;
   3676    return true;
   3677  }
   3678 
   3679  if (!tryAllocateAnyRegister(bundle, success, hasCall, conflicting)) {
   3680    return false;
   3681  }
   3682  if (*success) {
   3683    return true;
   3684  }
   3685 
   3686  // Spill bundles which have no register requirement if they didn't get
   3687  // allocated.
   3688  if (requirement.kind() == Requirement::NONE) {
   3689    JitSpew(JitSpew_RegAlloc, "  postponed spill (no register requirement)");
   3690    if (!spilledBundles.append(bundle)) {
   3691      return false;
   3692    }
   3693    *success = true;
   3694    return true;
   3695  }
   3696 
   3697  // We failed to allocate this bundle.
   3698  MOZ_ASSERT(!*success);
   3699  return true;
   3700 }
   3701 
   3702 bool BacktrackingAllocator::processBundle(const MIRGenerator* mir,
   3703                                          LiveBundle* bundle) {
   3704  JitSpewIfEnabled(JitSpew_RegAlloc,
   3705                   "Allocating %s [priority %zu] [weight %zu]",
   3706                   bundle->toString().get(), computePriority(bundle),
   3707                   computeSpillWeight(bundle));
   3708 
   3709  // A bundle can be processed by doing any of the following:
   3710  //
   3711  // - Assigning the bundle a register. The bundle cannot overlap any other
   3712  //   bundle allocated for that physical register.
   3713  //
   3714  // - Spilling the bundle, provided it has no register uses.
   3715  //
   3716  // - Splitting the bundle into two or more bundles which cover the original
   3717  //   one. The new bundles are placed back onto the priority queue for later
   3718  //   processing.
   3719  //
   3720  // - Evicting one or more existing allocated bundles, and then doing one
   3721  //   of the above operations. Evicted bundles are placed back on the
   3722  //   priority queue. Any evicted bundles must have a lower spill weight
   3723  //   than the bundle being processed.
   3724  //
   3725  // As long as this structure is followed, termination is guaranteed.
   3726  // In general, we want to minimize the amount of bundle splitting (which
   3727  // generally necessitates spills), so allocate longer lived, lower weight
   3728  // bundles first and evict and split them later if they prevent allocation
   3729  // for higher weight bundles.
   3730 
   3731  Requirement requirement, hint;
   3732  bool doesNotHaveFixedConflict =
   3733      computeRequirement(bundle, &requirement, &hint);
   3734 
   3735  bool hasCall = false;
   3736  LiveBundleVector conflicting;
   3737 
   3738  if (doesNotHaveFixedConflict) {
   3739    for (size_t attempt = 0;; attempt++) {
   3740      if (mir->shouldCancel("Backtracking Allocation (processBundle loop)")) {
   3741        return false;
   3742      }
   3743 
   3744      bool success = false;
   3745      hasCall = false;
   3746      conflicting.clear();
   3747 
   3748      // Ok, let's try allocating for this bundle.
   3749      if (requirement.kind() == Requirement::FIXED) {
   3750        if (!tryAllocateFixed(bundle, requirement, &success, &hasCall,
   3751                              conflicting)) {
   3752          return false;
   3753        }
   3754      } else {
   3755        if (!tryAllocateNonFixed(bundle, requirement, hint, &success, &hasCall,
   3756                                 conflicting)) {
   3757          return false;
   3758        }
   3759      }
   3760 
   3761      // If that worked, we're done!
   3762      if (success) {
   3763        return true;
   3764      }
   3765 
   3766      // If that didn't work, but we have one or more non-call bundles known to
   3767      // be conflicting, maybe we can evict them and try again.
   3768      if ((attempt < MAX_ATTEMPTS || minimalBundle(bundle)) && !hasCall &&
   3769          !conflicting.empty() &&
   3770          maximumSpillWeight(conflicting) < computeSpillWeight(bundle)) {
   3771        for (size_t i = 0; i < conflicting.length(); i++) {
   3772          if (!evictBundle(conflicting[i])) {
   3773            return false;
   3774          }
   3775        }
   3776        continue;
   3777      }
   3778 
   3779      // We have to split this bundle.
   3780      break;
   3781    }
   3782  }
   3783 
   3784  // A minimal bundle cannot be split any further. If we try to split it
   3785  // it at this point we will just end up with the same bundle and will
   3786  // enter an infinite loop. Weights and the initial live ranges must
   3787  // be constructed so that any minimal bundle is allocatable.
   3788  MOZ_ASSERT(!minimalBundle(bundle));
   3789 
   3790  LiveBundle* conflict = conflicting.empty() ? nullptr : conflicting[0];
   3791  return chooseBundleSplit(bundle, hasCall, conflict);
   3792 }
   3793 
   3794 // Helper for ::tryAllocatingRegistersForSpillBundles
   3795 bool BacktrackingAllocator::spill(LiveBundle* bundle) {
   3796  JitSpew(JitSpew_RegAlloc, "  Spilling bundle");
   3797  MOZ_ASSERT(bundle->allocation().isBogus());
   3798 
   3799  if (LiveBundle* spillParent = bundle->spillParent()) {
   3800    JitSpew(JitSpew_RegAlloc, "    Using existing spill bundle");
   3801    for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3802      LiveRange* range = *iter;
   3803      LiveRange* parentRange = spillParent->rangeFor(range->from());
   3804      MOZ_ASSERT(parentRange->contains(range));
   3805      MOZ_ASSERT(&range->vreg() == &parentRange->vreg());
   3806      range->tryToMoveDefAndUsesInto(parentRange);
   3807    }
   3808    bundle->removeAllRangesFromVirtualRegisters();
   3809    return true;
   3810  }
   3811 
   3812  return bundle->spillSet()->addSpilledBundle(bundle);
   3813 }
   3814 
   3815 bool BacktrackingAllocator::tryAllocatingRegistersForSpillBundles() {
   3816  for (auto it = spilledBundles.begin(); it != spilledBundles.end(); it++) {
   3817    LiveBundle* bundle = *it;
   3818    LiveBundleVector conflicting;
   3819    bool hasCall = false;
   3820    bool success = false;
   3821 
   3822    if (mir->shouldCancel("Backtracking Try Allocating Spilled Bundles")) {
   3823      return false;
   3824    }
   3825 
   3826    JitSpewIfEnabled(JitSpew_RegAlloc, "Spill or allocate %s",
   3827                     bundle->toString().get());
   3828 
   3829    if (!tryAllocateAnyRegister(bundle, &success, &hasCall, conflicting)) {
   3830      return false;
   3831    }
   3832 
   3833    // If the bundle still has no register, spill the bundle.
   3834    if (!success && !spill(bundle)) {
   3835      return false;
   3836    }
   3837  }
   3838 
   3839  return true;
   3840 }
   3841 
   3842 ///////////////////////////////////////////////////////////////////////////////
   3843 //                                                                           //
   3844 // Rewriting of the LIR after bundle processing is done:                     //
   3845 //   ::pickStackSlots                                                        //
   3846 //   ::createMoveGroupsFromLiveRangeTransitions                              //
   3847 //   ::installAllocationsInLIR                                               //
   3848 //   ::populateSafepoints                                                    //
   3849 //   ::annotateMoveGroups                                                    //
   3850 //                                                                           //
   3851 ///////////////////////////////////////////////////////////////////////////////
   3852 
   3853 // Helper for ::pickStackSlot
   3854 bool BacktrackingAllocator::insertAllRanges(LiveRangePlusSet& set,
   3855                                            LiveBundle* bundle) {
   3856  for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3857    LiveRange* range = *iter;
   3858    if (!alloc().ensureBallast()) {
   3859      return false;
   3860    }
   3861    LiveRangePlus rangePlus(range);
   3862    if (!set.insert(rangePlus)) {
   3863      return false;
   3864    }
   3865  }
   3866  return true;
   3867 }
   3868 
   3869 void BacktrackingAllocator::sortVirtualRegisterRanges() {
   3870  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   3871    VirtualRegister& reg = vregs[i];
   3872    reg.sortRanges();
   3873  }
   3874 }
   3875 
   3876 // Helper for ::pickStackSlots
   3877 bool BacktrackingAllocator::pickStackSlot(SpillSet* spillSet) {
   3878  // Look through all ranges that have been spilled in this set for a
   3879  // register definition which is fixed to a stack or argument slot. If we
   3880  // find one, use it for all bundles that have been spilled. tryMergeBundles
   3881  // makes sure this reuse is possible when an initial bundle contains ranges
   3882  // from multiple virtual registers.
   3883  for (size_t i = 0; i < spillSet->numSpilledBundles(); i++) {
   3884    LiveBundle* bundle = spillSet->spilledBundle(i);
   3885    for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter; iter++) {
   3886      LiveRange* range = *iter;
   3887      if (range->hasDefinition()) {
   3888        LDefinition* def = range->vreg().def();
   3889        if (def->policy() == LDefinition::FIXED) {
   3890          MOZ_ASSERT(!def->output()->isAnyRegister());
   3891          MOZ_ASSERT(!def->output()->isStackSlot());
   3892          spillSet->setAllocation(*def->output());
   3893          return true;
   3894        }
   3895      }
   3896    }
   3897  }
   3898 
   3899  LDefinition::Type type =
   3900      spillSet->spilledBundle(0)->firstRange()->vreg().type();
   3901 
   3902  SpillSlotList* slotList;
   3903  switch (LStackSlot::width(type)) {
   3904    case LStackSlot::Word:
   3905      slotList = &normalSlots;
   3906      break;
   3907    case LStackSlot::DoubleWord:
   3908      slotList = &doubleSlots;
   3909      break;
   3910    case LStackSlot::QuadWord:
   3911      slotList = &quadSlots;
   3912      break;
   3913    default:
   3914      MOZ_CRASH("Bad width");
   3915  }
   3916 
   3917  // Maximum number of existing spill slots we will look at before giving up
   3918  // and allocating a new slot.
   3919  static const size_t MAX_SEARCH_COUNT = 10;
   3920 
   3921  size_t searches = 0;
   3922  SpillSlot* stop = nullptr;
   3923  while (!slotList->empty()) {
   3924    SpillSlot* spillSlot = *slotList->begin();
   3925    if (!stop) {
   3926      stop = spillSlot;
   3927    } else if (stop == spillSlot) {
   3928      // We looked through every slot in the list.
   3929      break;
   3930    }
   3931 
   3932    bool success = true;
   3933    for (size_t i = 0; i < spillSet->numSpilledBundles(); i++) {
   3934      LiveBundle* bundle = spillSet->spilledBundle(i);
   3935      for (LiveBundle::RangeIterator iter = bundle->rangesBegin(); iter;
   3936           iter++) {
   3937        LiveRange* range = *iter;
   3938        LiveRangePlus rangePlus(range);
   3939        LiveRangePlus existingPlus;
   3940        if (spillSlot->allocated.contains(rangePlus, &existingPlus)) {
   3941          success = false;
   3942          break;
   3943        }
   3944      }
   3945      if (!success) {
   3946        break;
   3947      }
   3948    }
   3949    if (success) {
   3950      // We can reuse this physical stack slot for the new bundles.
   3951      // Update the allocated ranges for the slot.
   3952      for (size_t i = 0; i < spillSet->numSpilledBundles(); i++) {
   3953        LiveBundle* bundle = spillSet->spilledBundle(i);
   3954        if (!insertAllRanges(spillSlot->allocated, bundle)) {
   3955          return false;
   3956        }
   3957      }
   3958      spillSet->setAllocation(spillSlot->alloc);
   3959      return true;
   3960    }
   3961 
   3962    // On a miss, move the spill to the end of the list. This will cause us
   3963    // to make fewer attempts to allocate from slots with a large and
   3964    // highly contended range.
   3965    slotList->popFront();
   3966    slotList->pushBack(spillSlot);
   3967 
   3968    if (++searches == MAX_SEARCH_COUNT) {
   3969      break;
   3970    }
   3971  }
   3972 
   3973  // We need a new physical stack slot.
   3974  LStackSlot::Width width = LStackSlot::width(type);
   3975  uint32_t stackSlot = stackSlotAllocator.allocateSlot(width);
   3976 
   3977  SpillSlot* spillSlot =
   3978      new (alloc().fallible()) SpillSlot(stackSlot, width, alloc().lifoAlloc());
   3979  if (!spillSlot) {
   3980    return false;
   3981  }
   3982 
   3983  for (size_t i = 0; i < spillSet->numSpilledBundles(); i++) {
   3984    LiveBundle* bundle = spillSet->spilledBundle(i);
   3985    if (!insertAllRanges(spillSlot->allocated, bundle)) {
   3986      return false;
   3987    }
   3988  }
   3989 
   3990  spillSet->setAllocation(spillSlot->alloc);
   3991 
   3992  slotList->pushFront(spillSlot);
   3993  return true;
   3994 }
   3995 
   3996 bool BacktrackingAllocator::pickStackSlots() {
   3997  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   3998    VirtualRegister& reg = vregs[i];
   3999 
   4000    if (mir->shouldCancel("Backtracking Pick Stack Slots")) {
   4001      return false;
   4002    }
   4003 
   4004    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   4005      LiveBundle* bundle = iter->bundle();
   4006 
   4007      if (bundle->allocation().isBogus()) {
   4008        if (!pickStackSlot(bundle->spillSet())) {
   4009          return false;
   4010        }
   4011        MOZ_ASSERT(!bundle->allocation().isBogus());
   4012      }
   4013    }
   4014  }
   4015 
   4016  return true;
   4017 }
   4018 
   4019 // Helper for ::createMoveGroupsFromLiveRangeTransitions
   4020 bool BacktrackingAllocator::moveAtEdge(LBlock* predecessor, LBlock* successor,
   4021                                       LiveRange* from, LiveRange* to,
   4022                                       LDefinition::Type type) {
   4023  if (successor->mir()->numPredecessors() > 1) {
   4024    MOZ_ASSERT(predecessor->mir()->numSuccessors() == 1);
   4025    return moveAtExit(predecessor, from, to, type);
   4026  }
   4027 
   4028  return moveAtEntry(successor, from, to, type);
   4029 }
   4030 
   4031 // Helper for ::createMoveGroupsFromLiveRangeTransitions
   4032 void BacktrackingAllocator::removeDeadRanges(VirtualRegister& reg) {
   4033  auto isDeadRange = [&](VirtualRegister::RangeVector& ranges,
   4034                         LiveRange* range) {
   4035    // Check for direct uses of this range.
   4036    if (range->hasUses() || range->hasDefinition()) {
   4037      return false;
   4038    }
   4039 
   4040    // Check if there are later ranges for this vreg. The first item in the list
   4041    // must have the highest start position so we compare against that one.
   4042    CodePosition start = range->from();
   4043    CodePosition maxFrom = ranges[0]->from();
   4044    if (maxFrom > start) {
   4045      return false;
   4046    }
   4047 
   4048    LNode* ins = insData[start];
   4049    if (start == entryOf(ins->block())) {
   4050      return false;
   4051    }
   4052 
   4053    // Check if this range ends at a loop backedge.
   4054    LNode* last = insData[range->to().previous()];
   4055    if (last->isGoto() &&
   4056        last->toGoto()->target()->id() < last->block()->mir()->id()) {
   4057      return false;
   4058    }
   4059 
   4060    // Check if there are phis which this vreg flows to.
   4061    if (reg.usedByPhi()) {
   4062      return false;
   4063    }
   4064 
   4065    return true;
   4066  };
   4067 
   4068  reg.removeRangesIf(isDeadRange);
   4069 }
   4070 
   4071 static void AssertCorrectRangeForPosition(const VirtualRegister& reg,
   4072                                          CodePosition pos,
   4073                                          const LiveRange* range) {
   4074  MOZ_ASSERT(range->covers(pos));
   4075 #ifdef DEBUG
   4076  // Assert the result is consistent with rangeFor. The ranges can be different
   4077  // but must be equivalent (both register or both non-register ranges).
   4078  LiveRange* expected = reg.rangeFor(pos, /* preferRegister = */ true);
   4079  MOZ_ASSERT(range->bundle()->allocation().isAnyRegister() ==
   4080             expected->bundle()->allocation().isAnyRegister());
   4081 #endif
   4082 }
   4083 
   4084 // Helper for ::createMoveGroupsFromLiveRangeTransitions
   4085 bool BacktrackingAllocator::createMoveGroupsForControlFlowEdges(
   4086    const VirtualRegister& reg, const ControlFlowEdgeVector& edges) {
   4087  // Iterate over both the virtual register ranges (sorted by start position)
   4088  // and the control flow edges (sorted by predecessorExit). When we find the
   4089  // predecessor range for the next edge, add a move from predecessor range to
   4090  // successor range.
   4091 
   4092  VirtualRegister::RangeIterator iter(reg);
   4093  LiveRange* nonRegisterRange = nullptr;
   4094 
   4095  for (const ControlFlowEdge& edge : edges) {
   4096    CodePosition pos = edge.predecessorExit;
   4097    LAllocation successorAllocation =
   4098        edge.successorRange->bundle()->allocation();
   4099 
   4100    // We don't need to insert a move if nonRegisterRange covers the predecessor
   4101    // block exit and has the same allocation as the successor block. This check
   4102    // is not required for correctness but it reduces the number of generated
   4103    // moves.
   4104    if (nonRegisterRange && pos < nonRegisterRange->to() &&
   4105        nonRegisterRange->bundle()->allocation() == successorAllocation) {
   4106      MOZ_ASSERT(nonRegisterRange->covers(pos));
   4107      continue;
   4108    }
   4109 
   4110    // Search for a matching range. Prefer a register range.
   4111    LiveRange* predecessorRange = nullptr;
   4112    bool foundSameAllocation = false;
   4113    while (true) {
   4114      if (iter.done() || iter->from() > pos) {
   4115        // No register range covers this edge.
   4116        predecessorRange = nonRegisterRange;
   4117        break;
   4118      }
   4119      if (iter->to() <= pos) {
   4120        // Skip ranges that end before this edge (and later edges).
   4121        iter++;
   4122        continue;
   4123      }
   4124      MOZ_ASSERT(iter->covers(pos));
   4125      if (iter->bundle()->allocation() == successorAllocation) {
   4126        // There's a range covering the predecessor block exit that has the same
   4127        // allocation, so we don't need to insert a move. This check is not
   4128        // required for correctness but it reduces the number of generated
   4129        // moves.
   4130        foundSameAllocation = true;
   4131        break;
   4132      }
   4133      if (iter->bundle()->allocation().isAnyRegister()) {
   4134        predecessorRange = *iter;
   4135        break;
   4136      }
   4137      if (!nonRegisterRange || iter->to() > nonRegisterRange->to()) {
   4138        nonRegisterRange = *iter;
   4139      }
   4140      iter++;
   4141    }
   4142 
   4143    if (foundSameAllocation) {
   4144      continue;
   4145    }
   4146 
   4147    MOZ_ASSERT(predecessorRange);
   4148    AssertCorrectRangeForPosition(reg, pos, predecessorRange);
   4149 
   4150    if (!alloc().ensureBallast()) {
   4151      return false;
   4152    }
   4153    JitSpew(JitSpew_RegAlloc, "    (moveAtEdge#2)");
   4154    if (!moveAtEdge(edge.predecessor, edge.successor, predecessorRange,
   4155                    edge.successorRange, reg.type())) {
   4156      return false;
   4157    }
   4158  }
   4159 
   4160  return true;
   4161 }
   4162 
   4163 bool BacktrackingAllocator::createMoveGroupsFromLiveRangeTransitions() {
   4164  // Add moves to handle changing assignments for vregs over their lifetime.
   4165  JitSpew(JitSpew_RegAlloc, "ResolveControlFlow: begin");
   4166 
   4167  JitSpew(JitSpew_RegAlloc,
   4168          "  ResolveControlFlow: adding MoveGroups within blocks");
   4169 
   4170  // Look for places where a register's assignment changes in the middle of a
   4171  // basic block.
   4172  MOZ_ASSERT(!vregs[0u].hasRanges());
   4173  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4174    VirtualRegister& reg = vregs[i];
   4175 
   4176    if (mir->shouldCancel(
   4177            "Backtracking Resolve Control Flow (vreg outer loop)")) {
   4178      return false;
   4179    }
   4180 
   4181    // Remove ranges which will never be used.
   4182    removeDeadRanges(reg);
   4183 
   4184    LiveRange* registerRange = nullptr;
   4185    LiveRange* nonRegisterRange = nullptr;
   4186    VirtualRegister::RangeIterator iter(reg);
   4187 
   4188    // Keep track of the register and non-register ranges with the highest end
   4189    // position before advancing the iterator. These are predecessor ranges for
   4190    // later ranges.
   4191    auto moveToNextRange = [&](LiveRange* range) {
   4192      MOZ_ASSERT(*iter == range);
   4193      if (range->bundle()->allocation().isAnyRegister()) {
   4194        if (!registerRange || range->to() > registerRange->to()) {
   4195          registerRange = range;
   4196        }
   4197      } else {
   4198        if (!nonRegisterRange || range->to() > nonRegisterRange->to()) {
   4199          nonRegisterRange = range;
   4200        }
   4201      }
   4202      iter++;
   4203    };
   4204 
   4205    // Iterate over all ranges.
   4206    while (!iter.done()) {
   4207      LiveRange* range = *iter;
   4208 
   4209      if (mir->shouldCancel(
   4210              "Backtracking Resolve Control Flow (vreg inner loop)")) {
   4211        return false;
   4212      }
   4213 
   4214      // The range which defines the register does not have a predecessor
   4215      // to add moves from.
   4216      if (range->hasDefinition()) {
   4217        moveToNextRange(range);
   4218        continue;
   4219      }
   4220 
   4221      // Ignore ranges that start at block boundaries. We will handle
   4222      // these in the next phase.
   4223      CodePosition start = range->from();
   4224      LNode* ins = insData[start];
   4225      if (start == entryOf(ins->block())) {
   4226        moveToNextRange(range);
   4227        continue;
   4228      }
   4229 
   4230      // Determine the predecessor range to use for this range and other ranges
   4231      // starting at the same position. Prefer a register range.
   4232      LiveRange* predecessorRange = nullptr;
   4233      if (registerRange && start.previous() < registerRange->to()) {
   4234        predecessorRange = registerRange;
   4235      } else {
   4236        MOZ_ASSERT(nonRegisterRange);
   4237        MOZ_ASSERT(start.previous() < nonRegisterRange->to());
   4238        predecessorRange = nonRegisterRange;
   4239      }
   4240      AssertCorrectRangeForPosition(reg, start.previous(), predecessorRange);
   4241 
   4242      // Add moves from predecessorRange to all ranges that start here.
   4243      do {
   4244        range = *iter;
   4245        MOZ_ASSERT(!range->hasDefinition());
   4246 
   4247        if (!alloc().ensureBallast()) {
   4248          return false;
   4249        }
   4250 
   4251 #ifdef DEBUG
   4252        // If we already saw a range which covers the start of this range, it
   4253        // must have a different allocation.
   4254        for (VirtualRegister::RangeIterator prevIter(reg); *prevIter != range;
   4255             prevIter++) {
   4256          MOZ_ASSERT_IF(prevIter->covers(start),
   4257                        prevIter->bundle()->allocation() !=
   4258                            range->bundle()->allocation());
   4259        }
   4260 #endif
   4261 
   4262        if (start.subpos() == CodePosition::INPUT) {
   4263          JitSpewIfEnabled(JitSpew_RegAlloc, "    moveInput (%s) <- (%s)",
   4264                           range->toString().get(),
   4265                           predecessorRange->toString().get());
   4266          if (!moveInput(ins->toInstruction(), predecessorRange, range,
   4267                         reg.type())) {
   4268            return false;
   4269          }
   4270        } else {
   4271          JitSpew(JitSpew_RegAlloc, "    (moveAfter)");
   4272          if (!moveAfter(ins->toInstruction(), predecessorRange, range,
   4273                         reg.type())) {
   4274            return false;
   4275          }
   4276        }
   4277 
   4278        moveToNextRange(range);
   4279      } while (!iter.done() && iter->from() == start);
   4280    }
   4281  }
   4282 
   4283  JitSpew(JitSpew_RegAlloc,
   4284          "  ResolveControlFlow: adding MoveGroups for phi nodes");
   4285 
   4286  for (size_t i = 0; i < graph.numBlocks(); i++) {
   4287    if (mir->shouldCancel("Backtracking Resolve Control Flow (block loop)")) {
   4288      return false;
   4289    }
   4290 
   4291    LBlock* successor = graph.getBlock(i);
   4292    MBasicBlock* mSuccessor = successor->mir();
   4293    if (mSuccessor->numPredecessors() < 1) {
   4294      continue;
   4295    }
   4296 
   4297    // Resolve phis to moves.
   4298    for (size_t j = 0; j < successor->numPhis(); j++) {
   4299      LPhi* phi = successor->getPhi(j);
   4300      MOZ_ASSERT(phi->numDefs() == 1);
   4301      LDefinition* def = phi->getDef(0);
   4302      VirtualRegister& reg = vreg(def);
   4303      LiveRange* to = reg.firstRange();
   4304      MOZ_ASSERT(to->from() == entryOf(successor));
   4305 
   4306      for (size_t k = 0; k < mSuccessor->numPredecessors(); k++) {
   4307        LBlock* predecessor = mSuccessor->getPredecessor(k)->lir();
   4308        MOZ_ASSERT(predecessor->mir()->numSuccessors() == 1);
   4309 
   4310        LAllocation* input = phi->getOperand(k);
   4311        LiveRange* from = vreg(input).rangeFor(exitOf(predecessor),
   4312                                               /* preferRegister = */ true);
   4313        MOZ_ASSERT(from);
   4314 
   4315        if (!alloc().ensureBallast()) {
   4316          return false;
   4317        }
   4318 
   4319        // Note: we have to use moveAtEdge both here and below (for edge
   4320        // resolution) to avoid conflicting moves. See bug 1493900.
   4321        JitSpew(JitSpew_RegAlloc, "    (moveAtEdge#1)");
   4322        if (!moveAtEdge(predecessor, successor, from, to, def->type())) {
   4323          return false;
   4324        }
   4325      }
   4326    }
   4327  }
   4328 
   4329  JitSpew(JitSpew_RegAlloc,
   4330          "  ResolveControlFlow: adding MoveGroups to fix conflicted edges");
   4331 
   4332  // Add moves to resolve graph edges with different allocations at their
   4333  // source and target.
   4334  ControlFlowEdgeVector edges;
   4335  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4336    VirtualRegister& reg = vregs[i];
   4337 
   4338    // First collect all control flow edges we need to resolve. This loop knows
   4339    // the range on the successor side, but looking up the corresponding
   4340    // predecessor range with rangeFor is quadratic so we handle that
   4341    // differently.
   4342    edges.clear();
   4343    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   4344      LiveRange* targetRange = *iter;
   4345 
   4346      size_t firstBlockId = insData[targetRange->from()]->block()->mir()->id();
   4347      if (!targetRange->covers(entryOf(graph.getBlock(firstBlockId)))) {
   4348        firstBlockId++;
   4349      }
   4350      for (size_t id = firstBlockId; id < graph.numBlocks(); id++) {
   4351        LBlock* successor = graph.getBlock(id);
   4352        if (!targetRange->covers(entryOf(successor))) {
   4353          break;
   4354        }
   4355 
   4356        VirtualRegBitSet& live = liveIn[id];
   4357        if (!live.contains(i)) {
   4358          continue;
   4359        }
   4360 
   4361        for (size_t j = 0; j < successor->mir()->numPredecessors(); j++) {
   4362          LBlock* predecessor = successor->mir()->getPredecessor(j)->lir();
   4363          CodePosition predecessorExit = exitOf(predecessor);
   4364          if (targetRange->covers(predecessorExit)) {
   4365            continue;
   4366          }
   4367          if (!edges.emplaceBack(predecessor, successor, targetRange,
   4368                                 predecessorExit)) {
   4369            return false;
   4370          }
   4371        }
   4372      }
   4373    }
   4374 
   4375    if (edges.empty()) {
   4376      continue;
   4377    }
   4378 
   4379    // Sort edges by predecessor position. This doesn't need to be a stable sort
   4380    // because createMoveGroupsForControlFlowEdges will use the same predecessor
   4381    // range if there are multiple edges with the same predecessor position.
   4382    auto compareEdges = [](const ControlFlowEdge& a, const ControlFlowEdge& b) {
   4383      return a.predecessorExit < b.predecessorExit;
   4384    };
   4385    std::sort(edges.begin(), edges.end(), compareEdges);
   4386 
   4387    // Resolve edges and add move groups.
   4388    if (!createMoveGroupsForControlFlowEdges(reg, edges)) {
   4389      return false;
   4390    }
   4391  }
   4392 
   4393  JitSpew(JitSpew_RegAlloc, "ResolveControlFlow: end");
   4394  return true;
   4395 }
   4396 
   4397 // Helper for ::addLiveRegistersForRange
   4398 size_t BacktrackingAllocator::findFirstNonCallSafepoint(CodePosition pos,
   4399                                                        size_t startFrom) {
   4400  // Assert startFrom is valid.
   4401  MOZ_ASSERT_IF(startFrom > 0,
   4402                inputOf(nonCallSafepoints_[startFrom - 1]) < pos);
   4403 
   4404  size_t i = startFrom;
   4405  for (; i < nonCallSafepoints_.length(); i++) {
   4406    const LInstruction* ins = nonCallSafepoints_[i];
   4407    if (pos <= inputOf(ins)) {
   4408      break;
   4409    }
   4410  }
   4411  return i;
   4412 }
   4413 
   4414 // Helper for ::installAllocationsInLIR
   4415 void BacktrackingAllocator::addLiveRegistersForRange(
   4416    VirtualRegister& reg, LiveRange* range, size_t* firstNonCallSafepoint) {
   4417  // Fill in the live register sets for all non-call safepoints.
   4418  LAllocation a = range->bundle()->allocation();
   4419  if (!a.isAnyRegister()) {
   4420    return;
   4421  }
   4422 
   4423  // Don't add output registers to the safepoint.
   4424  CodePosition start = range->from();
   4425  if (range->hasDefinition()) {
   4426 #ifdef CHECK_OSIPOINT_REGISTERS
   4427    // Add output and temp registers to the safepoint's clobberedRegs.
   4428    // Note: the outputs aren't added to the safepoint's liveRegs here, but the
   4429    // same register might still be added to liveRegs for one of the inputs, so
   4430    // we have to add outputs to clobberedRegs here.
   4431    if (reg.ins()->isInstruction() && !reg.ins()->isCall()) {
   4432      if (LSafepoint* safepoint = reg.ins()->toInstruction()->safepoint()) {
   4433        safepoint->addClobberedRegister(a.toAnyRegister());
   4434      }
   4435    }
   4436 #endif
   4437    if (!reg.isTemp()) {
   4438      start = start.next();
   4439    }
   4440  }
   4441 
   4442  *firstNonCallSafepoint =
   4443      findFirstNonCallSafepoint(start, *firstNonCallSafepoint);
   4444 
   4445  for (size_t i = *firstNonCallSafepoint; i < nonCallSafepoints_.length();
   4446       i++) {
   4447    LInstruction* ins = nonCallSafepoints_[i];
   4448    CodePosition pos = inputOf(ins);
   4449 
   4450    // Safepoints are sorted, so we can shortcut out of this loop
   4451    // if we go out of range.
   4452    if (range->to() <= pos) {
   4453      break;
   4454    }
   4455 
   4456    MOZ_ASSERT(range->covers(pos));
   4457 
   4458    LSafepoint* safepoint = ins->safepoint();
   4459    safepoint->addLiveRegister(a.toAnyRegister());
   4460  }
   4461 }
   4462 
   4463 // Helper for ::installAllocationsInLIR
   4464 static inline size_t NumReusingDefs(LInstruction* ins) {
   4465  size_t num = 0;
   4466  for (size_t i = 0; i < ins->numDefs(); i++) {
   4467    LDefinition* def = ins->getDef(i);
   4468    if (def->policy() == LDefinition::MUST_REUSE_INPUT) {
   4469      num++;
   4470    }
   4471  }
   4472  return num;
   4473 }
   4474 
   4475 bool BacktrackingAllocator::installAllocationsInLIR() {
   4476  JitSpew(JitSpew_RegAlloc, "Installing Allocations");
   4477 
   4478  // The virtual registers and safepoints are both ordered by position. To avoid
   4479  // quadratic behavior in findFirstNonCallSafepoint, we use
   4480  // firstNonCallSafepoint as cursor to start the search at the safepoint
   4481  // returned by the previous call.
   4482  size_t firstNonCallSafepoint = 0;
   4483 
   4484  MOZ_ASSERT(!vregs[0u].hasRanges());
   4485  for (size_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4486    VirtualRegister& reg = vregs[i];
   4487 
   4488    if (mir->shouldCancel("Backtracking Install Allocations (main loop)")) {
   4489      return false;
   4490    }
   4491 
   4492    firstNonCallSafepoint =
   4493        findFirstNonCallSafepoint(inputOf(reg.ins()), firstNonCallSafepoint);
   4494 
   4495    // The ranges are sorted by start position, so we can use the same
   4496    // findFirstNonCallSafepoint optimization here.
   4497    size_t firstNonCallSafepointForRange = firstNonCallSafepoint;
   4498 
   4499    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   4500      LiveRange* range = *iter;
   4501 
   4502      if (range->hasDefinition()) {
   4503        reg.def()->setOutput(range->bundle()->allocation());
   4504        if (reg.ins()->recoversInput()) {
   4505          LSnapshot* snapshot = reg.ins()->toInstruction()->snapshot();
   4506          for (size_t i = 0; i < snapshot->numEntries(); i++) {
   4507            LAllocation* entry = snapshot->getEntry(i);
   4508            if (entry->isUse() &&
   4509                entry->toUse()->policy() == LUse::RECOVERED_INPUT) {
   4510              *entry = *reg.def()->output();
   4511            }
   4512          }
   4513        }
   4514      }
   4515 
   4516      for (UsePositionIterator iter(range->usesBegin()); iter; iter++) {
   4517        LAllocation* alloc = iter->use();
   4518        *alloc = range->bundle()->allocation();
   4519 
   4520        // For any uses which feed into MUST_REUSE_INPUT definitions,
   4521        // add copies if the use and def have different allocations.
   4522        LNode* ins = insData[iter->pos];
   4523        if (LDefinition* def = FindReusingDefOrTemp(ins, alloc)) {
   4524          LiveRange* outputRange = vreg(def).firstRange();
   4525          MOZ_ASSERT(outputRange->covers(outputOf(ins)));
   4526          LAllocation res = outputRange->bundle()->allocation();
   4527          LAllocation sourceAlloc = range->bundle()->allocation();
   4528 
   4529          if (res != *alloc) {
   4530            if (!this->alloc().ensureBallast()) {
   4531              return false;
   4532            }
   4533            if (NumReusingDefs(ins->toInstruction()) <= 1) {
   4534              LMoveGroup* group = getInputMoveGroup(ins->toInstruction());
   4535              if (!group->addAfter(sourceAlloc, res, reg.type())) {
   4536                return false;
   4537              }
   4538            } else {
   4539              LMoveGroup* group = getFixReuseMoveGroup(ins->toInstruction());
   4540              if (!group->add(sourceAlloc, res, reg.type())) {
   4541                return false;
   4542              }
   4543            }
   4544            *alloc = res;
   4545          }
   4546        }
   4547      }
   4548 
   4549      addLiveRegistersForRange(reg, range, &firstNonCallSafepointForRange);
   4550    }
   4551  }
   4552 
   4553  graph.setLocalSlotsSize(stackSlotAllocator.stackHeight());
   4554  return true;
   4555 }
   4556 
   4557 // Helper for ::populateSafepoints
   4558 size_t BacktrackingAllocator::findFirstSafepoint(CodePosition pos,
   4559                                                 size_t startFrom) {
   4560  // Assert startFrom is valid.
   4561  MOZ_ASSERT_IF(startFrom > 0, inputOf(safepoints_[startFrom - 1]) < pos);
   4562 
   4563  size_t i = startFrom;
   4564  for (; i < safepoints_.length(); i++) {
   4565    LInstruction* ins = safepoints_[i];
   4566    if (pos <= inputOf(ins)) {
   4567      break;
   4568    }
   4569  }
   4570  return i;
   4571 }
   4572 
   4573 bool BacktrackingAllocator::populateSafepoints() {
   4574  JitSpew(JitSpew_RegAlloc, "Populating Safepoints");
   4575 
   4576  // The virtual registers and safepoints are both ordered by position. To avoid
   4577  // quadratic behavior in findFirstSafepoint, we use firstSafepoint as cursor
   4578  // to start the search at the safepoint returned by the previous call.
   4579  size_t firstSafepoint = 0;
   4580 
   4581  MOZ_ASSERT(!vregs[0u].def());
   4582  for (uint32_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4583    VirtualRegister& reg = vregs[i];
   4584 
   4585    if (!reg.def() || !reg.def()->isSafepointGCType(reg.ins())) {
   4586      continue;
   4587    }
   4588 
   4589    firstSafepoint = findFirstSafepoint(inputOf(reg.ins()), firstSafepoint);
   4590    if (firstSafepoint >= graph.numSafepoints()) {
   4591      break;
   4592    }
   4593 
   4594    // The ranges are sorted by start position, so we can use the same
   4595    // findFirstSafepoint optimization here.
   4596    size_t firstSafepointForRange = firstSafepoint;
   4597 
   4598    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   4599      LiveRange* range = *iter;
   4600 
   4601      firstSafepointForRange =
   4602          findFirstSafepoint(range->from(), firstSafepointForRange);
   4603 
   4604      for (size_t j = firstSafepointForRange; j < graph.numSafepoints(); j++) {
   4605        LInstruction* ins = safepoints_[j];
   4606 
   4607        if (inputOf(ins) >= range->to()) {
   4608          break;
   4609        }
   4610 
   4611        MOZ_ASSERT(range->covers(inputOf(ins)));
   4612 
   4613        // Include temps but not instruction outputs. Also make sure
   4614        // MUST_REUSE_INPUT is not used with gcthings or nunboxes, or
   4615        // we would have to add the input reg to this safepoint.
   4616        if (ins == reg.ins() && !reg.isTemp()) {
   4617          DebugOnly<LDefinition*> def = reg.def();
   4618          MOZ_ASSERT_IF(def->policy() == LDefinition::MUST_REUSE_INPUT,
   4619                        def->type() == LDefinition::GENERAL ||
   4620                            def->type() == LDefinition::INT32 ||
   4621                            def->type() == LDefinition::FLOAT32 ||
   4622                            def->type() == LDefinition::DOUBLE ||
   4623                            def->type() == LDefinition::SIMD128);
   4624          continue;
   4625        }
   4626 
   4627        LSafepoint* safepoint = ins->safepoint();
   4628 
   4629        LAllocation a = range->bundle()->allocation();
   4630        if (a.isGeneralReg() && ins->isCall()) {
   4631          continue;
   4632        }
   4633 
   4634        if (!safepoint->addGCAllocation(i, reg.def(), a)) {
   4635          return false;
   4636        }
   4637      }
   4638    }
   4639  }
   4640 
   4641  return true;
   4642 }
   4643 
   4644 bool BacktrackingAllocator::annotateMoveGroups() {
   4645  // Annotate move groups in the LIR graph with any register that is not
   4646  // allocated at that point and can be used as a scratch register. This is
   4647  // only required for x86, as other platforms always have scratch registers
   4648  // available for use.
   4649 #ifdef JS_CODEGEN_X86
   4650  LiveRange* range = LiveRange::FallibleNew(alloc(), nullptr, CodePosition(),
   4651                                            CodePosition().next());
   4652  if (!range) {
   4653    return false;
   4654  }
   4655 
   4656  for (size_t i = 0; i < graph.numBlocks(); i++) {
   4657    if (mir->shouldCancel("Backtracking Annotate Move Groups")) {
   4658      return false;
   4659    }
   4660 
   4661    LBlock* block = graph.getBlock(i);
   4662    LInstruction* last = nullptr;
   4663    for (LInstructionIterator iter = block->begin(); iter != block->end();
   4664         ++iter) {
   4665      if (iter->isMoveGroup()) {
   4666        CodePosition from = last ? outputOf(last) : entryOf(block);
   4667        range->setTo(from.next());
   4668        range->setFrom(from);
   4669 
   4670        for (size_t i = 0; i < AnyRegister::Total; i++) {
   4671          PhysicalRegister& reg = registers[i];
   4672          if (reg.reg.isFloat() || !reg.allocatable) {
   4673            continue;
   4674          }
   4675 
   4676          // This register is unavailable for use if (a) it is in use
   4677          // by some live range immediately before the move group,
   4678          // or (b) it is an operand in one of the group's moves. The
   4679          // latter case handles live ranges which end immediately
   4680          // before the move group or start immediately after.
   4681          // For (b) we need to consider move groups immediately
   4682          // preceding or following this one.
   4683 
   4684          if (iter->toMoveGroup()->uses(reg.reg.gpr())) {
   4685            continue;
   4686          }
   4687          bool found = false;
   4688          LInstructionIterator niter(iter);
   4689          for (niter++; niter != block->end(); niter++) {
   4690            if (niter->isMoveGroup()) {
   4691              if (niter->toMoveGroup()->uses(reg.reg.gpr())) {
   4692                found = true;
   4693                break;
   4694              }
   4695            } else {
   4696              break;
   4697            }
   4698          }
   4699          if (iter != block->begin()) {
   4700            LInstructionIterator riter(iter);
   4701            do {
   4702              riter--;
   4703              if (riter->isMoveGroup()) {
   4704                if (riter->toMoveGroup()->uses(reg.reg.gpr())) {
   4705                  found = true;
   4706                  break;
   4707                }
   4708              } else {
   4709                break;
   4710              }
   4711            } while (riter != block->begin());
   4712          }
   4713 
   4714          if (found) {
   4715            continue;
   4716          }
   4717          LiveRangePlus existingPlus;
   4718          LiveRangePlus rangePlus(range);
   4719          if (reg.allocations.contains(rangePlus, &existingPlus)) {
   4720            continue;
   4721          }
   4722 
   4723          iter->toMoveGroup()->setScratchRegister(reg.reg.gpr());
   4724          break;
   4725        }
   4726      } else {
   4727        last = *iter;
   4728      }
   4729    }
   4730  }
   4731 #endif
   4732 
   4733  return true;
   4734 }
   4735 
   4736 ///////////////////////////////////////////////////////////////////////////////
   4737 //                                                                           //
   4738 // Debug-printing support                                                    //
   4739 //                                                                           //
   4740 ///////////////////////////////////////////////////////////////////////////////
   4741 
   4742 #ifdef JS_JITSPEW
   4743 
   4744 UniqueChars LiveRange::toString() const {
   4745  AutoEnterOOMUnsafeRegion oomUnsafe;
   4746 
   4747  UniqueChars buf = JS_smprintf("v%u %u-%u", hasVreg() ? vreg().vreg() : 0,
   4748                                from().bits(), to().bits() - 1);
   4749 
   4750  if (buf && bundle() && !bundle()->allocation().isBogus()) {
   4751    buf = JS_sprintf_append(std::move(buf), " %s",
   4752                            bundle()->allocation().toString().get());
   4753  }
   4754 
   4755  buf = JS_sprintf_append(std::move(buf), " {");
   4756 
   4757  if (buf && hasDefinition()) {
   4758    buf = JS_sprintf_append(std::move(buf), " %u_def", from().bits());
   4759    if (hasVreg()) {
   4760      // If the definition has a fixed requirement, print it too.
   4761      const LDefinition* def = vreg().def();
   4762      LDefinition::Policy policy = def->policy();
   4763      if (policy == LDefinition::FIXED || policy == LDefinition::STACK) {
   4764        if (buf) {
   4765          buf = JS_sprintf_append(std::move(buf), ":F:%s",
   4766                                  def->output()->toString().get());
   4767        }
   4768      }
   4769    }
   4770  }
   4771 
   4772  for (UsePositionIterator iter = usesBegin(); buf && iter; iter++) {
   4773    buf = JS_sprintf_append(std::move(buf), " %u_%s", iter->pos.bits(),
   4774                            iter->use()->toString().get());
   4775  }
   4776 
   4777  buf = JS_sprintf_append(std::move(buf), " }");
   4778 
   4779  if (!buf) {
   4780    oomUnsafe.crash("LiveRange::toString()");
   4781  }
   4782 
   4783  return buf;
   4784 }
   4785 
   4786 UniqueChars LiveBundle::toString() const {
   4787  AutoEnterOOMUnsafeRegion oomUnsafe;
   4788 
   4789  UniqueChars buf = JS_smprintf("LB%u(", id());
   4790 
   4791  if (buf) {
   4792    if (spillParent()) {
   4793      buf =
   4794          JS_sprintf_append(std::move(buf), "parent=LB%u", spillParent()->id());
   4795    } else {
   4796      buf = JS_sprintf_append(std::move(buf), "parent=none");
   4797    }
   4798  }
   4799 
   4800  for (LiveBundle::RangeIterator iter = rangesBegin(); buf && iter; iter++) {
   4801    if (buf) {
   4802      buf = JS_sprintf_append(std::move(buf), "%s %s",
   4803                              (iter == rangesBegin()) ? "" : " ##",
   4804                              iter->toString().get());
   4805    }
   4806  }
   4807 
   4808  if (buf) {
   4809    buf = JS_sprintf_append(std::move(buf), ")");
   4810  }
   4811 
   4812  if (!buf) {
   4813    oomUnsafe.crash("LiveBundle::toString()");
   4814  }
   4815 
   4816  return buf;
   4817 }
   4818 
   4819 void BacktrackingAllocator::dumpLiveRangesByVReg(const char* who) {
   4820  MOZ_ASSERT(!vregs[0u].hasRanges());
   4821 
   4822  JitSpewCont(JitSpew_RegAlloc, "\n");
   4823  JitSpew(JitSpew_RegAlloc, "Live ranges by virtual register (%s):", who);
   4824 
   4825  for (uint32_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4826    JitSpewHeader(JitSpew_RegAlloc);
   4827    JitSpewCont(JitSpew_RegAlloc, "  ");
   4828    VirtualRegister& reg = vregs[i];
   4829    for (VirtualRegister::RangeIterator iter(reg); iter; iter++) {
   4830      if (*iter != reg.firstRange()) {
   4831        JitSpewCont(JitSpew_RegAlloc, " ## ");
   4832      }
   4833      JitSpewCont(JitSpew_RegAlloc, "%s", iter->toString().get());
   4834    }
   4835    JitSpewCont(JitSpew_RegAlloc, "\n");
   4836  }
   4837 }
   4838 
   4839 void BacktrackingAllocator::dumpLiveRangesByBundle(const char* who) {
   4840  MOZ_ASSERT(!vregs[0u].hasRanges());
   4841 
   4842  JitSpewCont(JitSpew_RegAlloc, "\n");
   4843  JitSpew(JitSpew_RegAlloc, "Live ranges by bundle (%s):", who);
   4844 
   4845  for (uint32_t i = 1; i < graph.numVirtualRegisters(); i++) {
   4846    VirtualRegister& reg = vregs[i];
   4847    for (VirtualRegister::RangeIterator baseIter(reg); baseIter; baseIter++) {
   4848      LiveRange* range = *baseIter;
   4849      LiveBundle* bundle = range->bundle();
   4850      if (range == bundle->firstRange()) {
   4851        JitSpew(JitSpew_RegAlloc, "  %s", bundle->toString().get());
   4852      }
   4853    }
   4854  }
   4855 }
   4856 
   4857 void BacktrackingAllocator::dumpAllocations() {
   4858  JitSpew(JitSpew_RegAlloc, "Allocations:");
   4859 
   4860  dumpLiveRangesByBundle("in dumpAllocations()");
   4861 
   4862  JitSpewCont(JitSpew_RegAlloc, "\n");
   4863  JitSpew(JitSpew_RegAlloc, "Allocations by physical register:");
   4864 
   4865  for (size_t i = 0; i < AnyRegister::Total; i++) {
   4866    if (registers[i].allocatable && !registers[i].allocations.empty()) {
   4867      JitSpewHeader(JitSpew_RegAlloc);
   4868      JitSpewCont(JitSpew_RegAlloc, "  %s:", AnyRegister::FromCode(i).name());
   4869      bool first = true;
   4870      LiveRangePlusSet::Iter lrpIter(&registers[i].allocations);
   4871      while (lrpIter.hasMore()) {
   4872        LiveRange* range = lrpIter.next().liveRange();
   4873        if (first) {
   4874          first = false;
   4875        } else {
   4876          fprintf(stderr, " /");
   4877        }
   4878        fprintf(stderr, " %s", range->toString().get());
   4879      }
   4880      JitSpewCont(JitSpew_RegAlloc, "\n");
   4881    }
   4882  }
   4883 
   4884  JitSpewCont(JitSpew_RegAlloc, "\n");
   4885 }
   4886 
   4887 #endif  // JS_JITSPEW
   4888 
   4889 ///////////////////////////////////////////////////////////////////////////////
   4890 //                                                                           //
   4891 // Top level of the register allocation machinery                            //
   4892 //                                                                           //
   4893 ///////////////////////////////////////////////////////////////////////////////
   4894 
   4895 bool BacktrackingAllocator::go() {
   4896  JitSpewCont(JitSpew_RegAlloc, "\n");
   4897  JitSpew(JitSpew_RegAlloc, "Beginning register allocation");
   4898 
   4899  JitSpewCont(JitSpew_RegAlloc, "\n");
   4900  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   4901    dumpInstructions("(Pre-allocation LIR)");
   4902  }
   4903 
   4904  if (!init()) {
   4905    return false;
   4906  }
   4907 
   4908  if (!buildLivenessInfo()) {
   4909    return false;
   4910  }
   4911 
   4912 #ifdef JS_JITSPEW
   4913  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   4914    dumpLiveRangesByVReg("after liveness analysis");
   4915  }
   4916 #endif
   4917 
   4918  if (!allocationQueue.reserve(graph.numVirtualRegisters() * 3 / 2)) {
   4919    return false;
   4920  }
   4921 
   4922  JitSpewCont(JitSpew_RegAlloc, "\n");
   4923  JitSpew(JitSpew_RegAlloc, "Beginning grouping and queueing registers");
   4924  if (!mergeAndQueueRegisters()) {
   4925    return false;
   4926  }
   4927  JitSpew(JitSpew_RegAlloc, "Completed grouping and queueing registers");
   4928 
   4929 #ifdef JS_JITSPEW
   4930  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   4931    dumpLiveRangesByBundle("after grouping/queueing regs");
   4932  }
   4933 #endif
   4934 
   4935  // There now follow two allocation loops, which are really the heart of the
   4936  // allocator.  First, the "main" allocation loop.  This does almost all of
   4937  // the allocation work, by repeatedly pulling bundles out of
   4938  // ::allocationQueue and calling ::processBundle on it, until there are no
   4939  // bundles left in the queue.  Note that ::processBundle can add new smaller
   4940  // bundles to the queue if it needs to split or spill a bundle.
   4941  //
   4942  // For each bundle in turn pulled out of ::allocationQueue, ::processBundle:
   4943  //
   4944  // * calls ::computeRequirement to discover the overall constraint for the
   4945  //   bundle.
   4946  //
   4947  // * tries to find a register for it, by calling either ::tryAllocateFixed or
   4948  //   ::tryAllocateNonFixed.
   4949  //
   4950  // * if that fails, but ::tryAllocateFixed / ::tryAllocateNonFixed indicate
   4951  //   that there is some other bundle with lower spill weight that can be
   4952  //   evicted, then that bundle is evicted (hence, put back into
   4953  //   ::allocationQueue), and we try again.
   4954  //
   4955  // * at most MAX_ATTEMPTS may be made.
   4956  //
   4957  // * If that still fails to find a register, then the bundle is handed off to
   4958  //   ::chooseBundleSplit.  That will choose to either split the bundle,
   4959  //   yielding multiple pieces which are put back into ::allocationQueue, or
   4960  //   it will spill the bundle.  Note that the same mechanism applies to both;
   4961  //   there's no clear boundary between splitting and spilling, because
   4962  //   spilling can be interpreted as an extreme form of splitting.
   4963  //
   4964  // ::processBundle and its callees contains much gnarly and logic which isn't
   4965  // easy to understand, particularly in the area of how eviction candidates
   4966  // are chosen.  But it works well enough, and tinkering doesn't seem to
   4967  // improve the resulting allocations.  More important is the splitting logic,
   4968  // because that controls where spill/reload instructions are placed.
   4969  //
   4970  // Eventually ::allocationQueue becomes empty, and each LiveBundle has either
   4971  // been allocated a register or is marked for spilling.  In the latter case
   4972  // it will have been added to ::spilledBundles.
   4973 
   4974  JitSpewCont(JitSpew_RegAlloc, "\n");
   4975  JitSpew(JitSpew_RegAlloc, "Beginning main allocation loop");
   4976  JitSpewCont(JitSpew_RegAlloc, "\n");
   4977 
   4978  // Allocate, spill and split bundles until finished.
   4979  while (!allocationQueue.empty()) {
   4980    if (mir->shouldCancel("Backtracking Allocation")) {
   4981      return false;
   4982    }
   4983 
   4984    QueueItem item = allocationQueue.removeHighest();
   4985    if (!processBundle(mir, item.bundle)) {
   4986      return false;
   4987    }
   4988  }
   4989 
   4990  // And here's the second allocation loop (hidden inside
   4991  // ::tryAllocatingRegistersForSpillBundles).  It makes one last attempt to
   4992  // find a register for each spill bundle.  There's no attempt to free up
   4993  // registers by eviction.  In at least 99% of cases this attempt fails, in
   4994  // which case the bundle is handed off to ::spill.  The lucky remaining 1%
   4995  // get a register.  Unfortunately this scheme interacts badly with the
   4996  // splitting strategy, leading to excessive register-to-register copying in
   4997  // some very simple cases.  See bug 1752520.
   4998  //
   4999  // A modest but probably worthwhile amount of allocation time can be saved by
   5000  // making ::tryAllocatingRegistersForSpillBundles use specialised versions of
   5001  // ::tryAllocateAnyRegister and its callees, that don't bother to create sets
   5002  // of conflicting bundles.  Creating those sets is expensive and, here,
   5003  // pointless, since we're not going to do any eviction based on them.  This
   5004  // refinement is implemented in the un-landed patch at bug 1758274 comment
   5005  // 15.
   5006 
   5007  JitSpewCont(JitSpew_RegAlloc, "\n");
   5008  JitSpew(JitSpew_RegAlloc,
   5009          "Main allocation loop complete; "
   5010          "beginning spill-bundle allocation loop");
   5011  JitSpewCont(JitSpew_RegAlloc, "\n");
   5012 
   5013  if (!tryAllocatingRegistersForSpillBundles()) {
   5014    return false;
   5015  }
   5016 
   5017  JitSpewCont(JitSpew_RegAlloc, "\n");
   5018  JitSpew(JitSpew_RegAlloc, "Spill-bundle allocation loop complete");
   5019  JitSpewCont(JitSpew_RegAlloc, "\n");
   5020 
   5021  // After this point, the VirtualRegister ranges are sorted and must stay
   5022  // sorted.
   5023  sortVirtualRegisterRanges();
   5024 
   5025  if (!pickStackSlots()) {
   5026    return false;
   5027  }
   5028 
   5029 #ifdef JS_JITSPEW
   5030  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   5031    dumpAllocations();
   5032  }
   5033 #endif
   5034 
   5035  if (!createMoveGroupsFromLiveRangeTransitions()) {
   5036    return false;
   5037  }
   5038 
   5039  if (!installAllocationsInLIR()) {
   5040    return false;
   5041  }
   5042 
   5043  if (!populateSafepoints()) {
   5044    return false;
   5045  }
   5046 
   5047  if (!annotateMoveGroups()) {
   5048    return false;
   5049  }
   5050 
   5051  JitSpewCont(JitSpew_RegAlloc, "\n");
   5052  if (JitSpewEnabled(JitSpew_RegAlloc)) {
   5053    dumpInstructions("(Post-allocation LIR)");
   5054  }
   5055 
   5056  JitSpew(JitSpew_RegAlloc, "Finished register allocation");
   5057 
   5058  return true;
   5059 }
   5060 
   5061 ///////////////////////////////////////////////////////////////////////////////
   5062 //                                                                           //
   5063 ///////////////////////////////////////////////////////////////////////////////