Debian Codegraph @

js/src/jit/WasmBCE.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/WasmBCE.h"
8
9 #include "jit/JitSpewer.h"
10 #include "jit/MIRGenerator.h"
11 #include "jit/MIRGraph.h"
12
13 using namespace js;
14 using namespace js::jit;
15
16 using LastSeenMap = js::HashMap<uint32_t, MDefinition*, DefaultHasher<uint32_t>,
17 SystemAllocPolicy>;
18
19 // The Wasm Bounds Check Elimination (BCE) pass looks for bounds checks
20 // on SSA values that have already been checked. (in the same block or in a
21 // dominating block). These bounds checks are redundant and thus eliminated.
22 //
23 // Note: This is safe in the presense of dynamic memory sizes as long as they
24 // can ONLY GROW. If we allow SHRINKING the heap, this pass should be
25 // RECONSIDERED.
26 //
27 // TODO (dbounov): Are there a lot of cases where there is no single dominating
28 // check, but a set of checks that together dominate a redundant check?
29 //
30 // TODO (dbounov): Generalize to constant additions relative to one base
31 bool jit::EliminateBoundsChecks(const MIRGenerator* mir, MIRGraph& graph) {
32 JitSpew(JitSpew_WasmBCE, "Begin");
33 // Map for dominating block where a given definition was checked
34 LastSeenMap lastSeen;
35
36 for (ReversePostorderIterator bIter(graph.rpoBegin());
37 bIter != graph.rpoEnd(); bIter++) {
38 MBasicBlock* block = *bIter;
39 for (MDefinitionIterator dIter(block); dIter;) {
40 MDefinition* def = *dIter++;
41
42 switch (def->op()) {
43 case MDefinition::Opcode::WasmBoundsCheck: {
44 MWasmBoundsCheck* bc = def->toWasmBoundsCheck();
45 MDefinition* addr = bc->index();
46
47 // We only support bounds check elimination on wasm memory 0, not
48 // other memories or tables. See bug 1625891.
49 if (!bc->isMemory0()) {
50 continue;
51 }
52
53 // Eliminate constant-address memory bounds checks to addresses below
54 // the heap minimum.
55 //
56 // The payload of the MConstant will be Double if the constant
57 // result is above 2^31-1, but we don't care about that for BCE.
58
59 if (addr->isConstant() &&
60 ((addr->toConstant()->type() == MIRType::Int32 &&
61 uint64_t(addr->toConstant()->toInt32()) <
62 mir->minWasmMemory0Length()) ||
63 (addr->toConstant()->type() == MIRType::Int64 &&
64 uint64_t(addr->toConstant()->toInt64()) <
65 mir->minWasmMemory0Length()))) {
66 bc->setRedundant();
67 if (JitOptions.spectreIndexMasking) {
68 bc->replaceAllUsesWith(addr);
69 } else {
70 MOZ_ASSERT(!bc->hasUses());
71 }
72 } else {
73 LastSeenMap::AddPtr ptr = lastSeen.lookupForAdd(addr->id());
74 if (ptr) {
75 MDefinition* prevCheckOrPhi = ptr->value();
76 if (prevCheckOrPhi->block()->dominates(block)) {
77 bc->setRedundant();
78 if (JitOptions.spectreIndexMasking) {
79 bc->replaceAllUsesWith(prevCheckOrPhi);
80 } else {
81 MOZ_ASSERT(!bc->hasUses());
82 }
83 }
84 } else {
85 if (!lastSeen.add(ptr, addr->id(), def)) {
86 return false;
87 }
88 }
89 }
90 break;
91 }
92 case MDefinition::Opcode::Phi: {
93 MPhi* phi = def->toPhi();
94 bool phiChecked = true;
95
96 MOZ_ASSERT(phi->numOperands() > 0);
97
98 // If all incoming values to a phi node are safe (i.e. have a
99 // check that dominates this block) then we can consider this
100 // phi node checked.
101 //
102 // Note that any phi that is part of a cycle
103 // will not be "safe" since the value coming on the backedge
104 // cannot be in lastSeen because its block hasn't been traversed yet.
105 for (int i = 0, nOps = phi->numOperands(); i < nOps; i++) {
106 MDefinition* src = phi->getOperand(i);
107
108 if (JitOptions.spectreIndexMasking) {
109 if (src->isWasmBoundsCheck()) {
110 src = src->toWasmBoundsCheck()->index();
111 }
112 } else {
113 MOZ_ASSERT(!src->isWasmBoundsCheck());
114 }
115
116 LastSeenMap::Ptr checkPtr = lastSeen.lookup(src->id());
117 if (!checkPtr || !checkPtr->value()->block()->dominates(block)) {
118 phiChecked = false;
119 break;
120 }
121 }
122
123 if (phiChecked) {
124 if (!lastSeen.put(def->id(), def)) {
125 return false;
126 }
127 }
128
129 break;
130 }
131 default:
132 break;
133 }
134 }
135 }
136
137 return true;
138 }