Debian Codegraph @

js/src/jit/BranchHinting.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/BranchHinting.h"
8
9 #include "jit/IonAnalysis.h"
10 #include "jit/JitSpewer.h"
11 #include "jit/MIRGenerator.h"
12 #include "jit/MIRGraph.h"
13
14 using namespace js;
15 using namespace js::jit;
16
17 // Implementation of the branch hinting proposal
18 // Some control instructions (if and br_if) can have a hint of the form
19 // Likely or unlikely. That means a specific branch will be likely/unlikely
20 // to be executed at runtime.
21
22 // In a first pass, we tag the basic blocks if we have a hint.
23 // In a Mir to Mir transformation, we read the hints and do something with it:
24 // - Unlikely blocks are pushed to the end of the function.
25 // Because of Ion's structure, we don't do that for blocks inside a loop.
26 // - TODO: do something for likely blocks.
27 // - TODO: register allocator can be tuned depending on the hints.
28 bool jit::BranchHinting(const MIRGenerator* mir, MIRGraph& graph) {
29 JitSpew(JitSpew_BranchHint, "Beginning BranchHinting pass");
30
31 // Move towards the end all blocks marked as unlikely
32 mozilla::Vector<MBasicBlock*, 0> toBeMoved;
33
34 for (MBasicBlock* block : graph) {
35 // If this block has a return instruction, it's safe to push it
36 // to the end of the graph.
37 // If the block doesn't contain a return, a backedge outside a loop will be
38 // created, which would break ReversePostOrder assertions.
39 // Avoid moving a block if it's in the middle of a loop as well.
40 if (block->branchHintingUnlikely() && block->loopDepth() == 0 &&
41 block->hasLastIns() && block->lastIns()->is<js::jit::MWasmReturn>()) {
42 if (!toBeMoved.append(block)) {
43 return false;
44 }
45 }
46 }
47
48 for (MBasicBlock* block : toBeMoved) {
49 #ifdef JS_JITSPEW
50 JitSpew(JitSpew_BranchHint, "Moving block%u to the end", block->id());
51 #endif
52 graph.moveBlockToEnd(block);
53 }
54
55 if (!toBeMoved.empty()) {
56 // Renumber blocks after moving them around.
57 RenumberBlocks(graph);
58 }
59
60 return true;
61 }