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