1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "jit/BytecodeAnalysis.h"
#include "jit/JitSpewer.h"
#include "jit/WarpBuilder.h"
#include "vm/BytecodeIterator.h"
#include "vm/BytecodeLocation.h"
#include "vm/BytecodeUtil.h"
#include "vm/BytecodeIterator-inl.h"
#include "vm/BytecodeLocation-inl.h"
#include "vm/JSScript-inl.h"
using namespace js;
using namespace js::jit;
BytecodeAnalysis::BytecodeAnalysis(TempAllocator& alloc, JSScript* script)
: script_(script), infos_(alloc) {}
bool BytecodeAnalysis::init(TempAllocator& alloc) {
if (!infos_.growByUninitialized(script_->length())) {
return false;
}
// Clear all BytecodeInfo.
mozilla::PodZero(infos_.begin(), infos_.length());
infos_[0].init(/*stackDepth=*/0);
// WarpBuilder can compile try blocks, but doesn't support handling
// exceptions. If exception unwinding would resume in a catch or finally
// block, we instead bail out to the baseline interpreter. Finally blocks can
// still be reached by normal means, but the catch block is unreachable and is
// not compiled. We therefore need some special machinery to prevent OSR into
// Warp code in the following cases:
//
// (1) Loops in catch blocks:
//
// try {
// ..
// } catch (e) {
// while (..) {} // Can't OSR here.
// }
//
// (2) Loops only reachable via a catch block:
//
// for (;;) {
// try {
// throw 3;
// } catch (e) {
// break;
// }
// }
// while (..) {} // Loop is only reachable via the catch-block.
//
// To deal with both of these cases, we track whether the current op is
// 'normally reachable' (reachable without exception handling).
// Forward jumps propagate this flag to their jump targets (see
// BytecodeInfo::jumpTargetNormallyReachable) and when the analysis reaches a
// jump target it updates its normallyReachable flag based on the target's
// flag.
//
// Inlining a function without a normally reachable return can cause similar
// problems. To avoid this, we mark such functions as uninlineable.
bool normallyReachable = true;
bool normallyReachableReturn = false;
for (const BytecodeLocation& it : AllBytecodesIterable(script_)) {
JSOp op = it.getOp();
uint32_t offset = it.bytecodeToOffset(script_);
JitSpew(JitSpew_BaselineOp, "Analyzing op @ %u (end=%u): %s",
unsigned(offset), unsigned(script_->length()), CodeName(op));
checkWarpSupport(op);
// If this bytecode info has not yet been initialized, it's not reachable.
if (!infos_[offset].initialized) {
continue;
}
uint32_t stackDepth = infos_[offset].stackDepth;
if (infos_[offset].jumpTarget) {
normallyReachable = infos_[offset].jumpTargetNormallyReachable;
}
#ifdef DEBUG
size_t endOffset = offset + it.length();
for (size_t checkOffset = offset + 1; checkOffset < endOffset;
checkOffset++) {
MOZ_ASSERT(!infos_[checkOffset].initialized);
}
#endif
uint32_t nuses = it.useCount();
uint32_t ndefs = it.defCount();
MOZ_ASSERT(stackDepth >= nuses);
stackDepth -= nuses;
stackDepth += ndefs;
// If stack depth exceeds max allowed by analysis, fail fast.
MOZ_ASSERT(stackDepth <= BytecodeInfo::MAX_STACK_DEPTH);
switch (op) {
case JSOp::TableSwitch: {
uint32_t defaultOffset = it.getTableSwitchDefaultOffset(script_);
int32_t low = it.getTableSwitchLow();
int32_t high = it.getTableSwitchHigh();
infos_[defaultOffset].init(stackDepth);
infos_[defaultOffset].setJumpTarget(normallyReachable);
uint32_t ncases = high - low + 1;
for (uint32_t i = 0; i < ncases; i++) {
uint32_t targetOffset = it.tableSwitchCaseOffset(script_, i);
if (targetOffset != defaultOffset) {
infos_[targetOffset].init(stackDepth);
infos_[targetOffset].setJumpTarget(normallyReachable);
}
}
break;
}
case JSOp::Try: {
for (const TryNote& tn : script_->trynotes()) {
if (tn.start == offset + JSOpLength_Try &&
(tn.kind() == TryNoteKind::Catch ||
tn.kind() == TryNoteKind::Finally)) {
uint32_t catchOrFinallyOffset = tn.start + tn.length;
uint32_t targetDepth =
tn.kind() == TryNoteKind::Finally ? stackDepth + 2 : stackDepth;
BytecodeInfo& targetInfo = infos_[catchOrFinallyOffset];
targetInfo.init(targetDepth);
targetInfo.setJumpTarget(/* normallyReachable = */ false);
}
}
break;
}
case JSOp::LoopHead:
infos_[offset].loopHeadCanOsr = normallyReachable;
break;
#ifdef DEBUG
case JSOp::Exception:
// Sanity check: ops only emitted in catch blocks are never
// normally reachable.
MOZ_ASSERT(!normallyReachable);
break;
#endif
case JSOp::Return:
case JSOp::RetRval:
if (normallyReachable) {
normallyReachableReturn = true;
}
break;
default:
break;
}
bool jump = it.isJump();
if (jump) {
// Case instructions do not push the lvalue back when branching.
uint32_t newStackDepth = stackDepth;
if (it.is(JSOp::Case)) {
newStackDepth--;
}
uint32_t targetOffset = it.getJumpTargetOffset(script_);
#ifdef DEBUG
// If this is a backedge, the target JSOp::LoopHead must have been
// analyzed already. Furthermore, if the backedge is normally reachable,
// the loop head must be normally reachable too (loopHeadCanOsr can be
// used to check this since it's equivalent).
if (targetOffset < offset) {
MOZ_ASSERT(infos_[targetOffset].initialized);
MOZ_ASSERT_IF(normallyReachable, infos_[targetOffset].loopHeadCanOsr);
}
#endif
infos_[targetOffset].init(newStackDepth);
infos_[targetOffset].setJumpTarget(normallyReachable);
}
// Handle any fallthrough from this opcode.
if (it.fallsThrough()) {
BytecodeLocation fallthroughLoc = it.next();
MOZ_ASSERT(fallthroughLoc.isInBounds(script_));
uint32_t fallthroughOffset = fallthroughLoc.bytecodeToOffset(script_);
infos_[fallthroughOffset].init(stackDepth);
// Treat the fallthrough of a branch instruction as a jump target.
if (jump) {
infos_[fallthroughOffset].setJumpTarget(normallyReachable);
}
}
}
// Flag (reachable) resume offset instructions.
for (uint32_t offset : script_->resumeOffsets()) {
BytecodeInfo& info = infos_[offset];
if (info.initialized) {
info.hasResumeOffset = true;
}
}
if (!normallyReachableReturn) {
script_->setUninlineable();
}
return true;
}
void BytecodeAnalysis::checkWarpSupport(JSOp op) {
switch (op) {
#define DEF_CASE(OP) case JSOp::OP:
WARP_UNSUPPORTED_OPCODE_LIST(DEF_CASE)
#undef DEF_CASE
if (script_->canIonCompile()) {
JitSpew(JitSpew_IonAbort, "Disabling Warp support for %s:%d:%d due to %s",
script_->filename(), script_->lineno(), script_->column(),
CodeName(op));
script_->disableIon();
}
break;
default:
break;
}
}
bool js::jit::ScriptUsesEnvironmentChain(JSScript* script) {
if (script->isModule() || script->initialEnvironmentShape() ||
(script->function() &&
script->function()->needsSomeEnvironmentObject())) {
return true;
}
AllBytecodesIterable iterator(script);
for (const BytecodeLocation& location : iterator) {
if (OpUsesEnvironmentChain(location.getOp())) {
return true;
}
}
return false;
}
|