| 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 |
#ifndef jit_CompileInfo_h |
| 8 |
#define jit_CompileInfo_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 11 |
#include "mozilla/Maybe.h" // mozilla::Maybe, mozilla::Some |
| 12 |
|
| 13 |
#include <algorithm> // std::max |
| 14 |
#include <stdint.h> // uint32_t |
| 15 |
|
| 16 |
#include "jit/CompileWrappers.h" // CompileRuntime |
| 17 |
#include "jit/JitFrames.h" // MinJITStackSize |
| 18 |
#include "jit/shared/Assembler-shared.h" |
| 19 |
#include "js/TypeDecls.h" // jsbytecode |
| 20 |
#include "vm/BindingKind.h" // BindingLocation |
| 21 |
#include "vm/JSAtomState.h" // JSAtomState |
| 22 |
#include "vm/JSFunction.h" // JSFunction |
| 23 |
#include "vm/JSScript.h" // JSScript |
| 24 |
#include "vm/Opcodes.h" // JSOp |
| 25 |
#include "vm/Scope.h" // BindingIter |
| 26 |
|
| 27 |
namespace js { |
| 28 |
|
| 29 |
class ModuleObject; |
| 30 |
|
| 31 |
namespace jit { |
| 32 |
|
| 33 |
class InlineScriptTree; |
| 34 |
|
| 35 |
inline unsigned StartArgSlot(JSScript* script) { |
| 36 |
// Reserved slots: |
| 37 |
// Slot 0: Environment chain. |
| 38 |
// Slot 1: Return value. |
| 39 |
|
| 40 |
// When needed: |
| 41 |
// Slot 2: Argumentsobject. |
| 42 |
|
| 43 |
// Note: when updating this, please also update the assert in |
| 44 |
// SnapshotWriter::startFrame |
| 45 |
return 2 + (script->needsArgsObj() ? 1 : 0); |
| 46 |
} |
| 47 |
|
| 48 |
inline unsigned CountArgSlots(JSScript* script, JSFunction* fun) { |
| 49 |
// Slot x + 0: This value. |
| 50 |
// Slot x + 1: Argument 1. |
| 51 |
// ... |
| 52 |
// Slot x + n: Argument n. |
| 53 |
|
| 54 |
// Note: when updating this, please also update the assert in |
| 55 |
// SnapshotWriter::startFrame |
| 56 |
return StartArgSlot(script) + (fun ? fun->nargs() + 1 : 0); |
| 57 |
} |
| 58 |
|
| 59 |
inline unsigned CountArgSlots(JSScript* script, bool hasFun, |
| 60 |
uint32_t funArgCount) { |
| 61 |
// Same as the previous function, for use when the JSFunction is not |
| 62 |
// available. |
| 63 |
return StartArgSlot(script) + (hasFun ? funArgCount + 1 : 0); |
| 64 |
} |
| 65 |
|
| 66 |
// Contains information about the compilation source for IR being generated. |
| 67 |
class CompileInfo { |
| 68 |
public: |
| 69 |
CompileInfo(CompileRuntime* runtime, JSScript* script, JSFunction* fun, |
| 70 |
jsbytecode* osrPc, bool scriptNeedsArgsObj, |
| 71 |
InlineScriptTree* inlineScriptTree) |
| 72 |
: script_(script), |
| 73 |
fun_(fun), |
| 74 |
osrPc_(osrPc), |
| 75 |
scriptNeedsArgsObj_(scriptNeedsArgsObj), |
| 76 |
hadEagerTruncationBailout_(script->hadEagerTruncationBailout()), |
| 77 |
hadSpeculativePhiBailout_(script->hadSpeculativePhiBailout()), |
| 78 |
hadLICMInvalidation_(script->hadLICMInvalidation()), |
| 79 |
hadReorderingBailout_(script->hadReorderingBailout()), |
| 80 |
hadBoundsCheckBailout_(script->failedBoundsCheck()), |
| 81 |
hadUnboxFoldingBailout_(script->hadUnboxFoldingBailout()), |
| 82 |
mayReadFrameArgsDirectly_(script->mayReadFrameArgsDirectly()), |
| 83 |
anyFormalIsForwarded_(script->anyFormalIsForwarded()), |
| 84 |
isDerivedClassConstructor_(script->isDerivedClassConstructor()), |
| 85 |
inlineScriptTree_(inlineScriptTree), |
| 86 |
hasSeenObjectEmulateUndefinedFuseIntact_( |
| 87 |
runtime->hasSeenObjectEmulateUndefinedFuseIntact()), |
| 88 |
hasSeenArrayExceedsInt32LengthFuseIntact_( |
| 89 |
runtime->hasSeenArrayExceedsInt32LengthFuseIntact()) { |
| 90 |
MOZ_ASSERT_IF(osrPc, JSOp(*osrPc) == JSOp::LoopHead); |
| 91 |
|
| 92 |
// The function here can flow in from anywhere so look up the canonical |
| 93 |
// function to ensure that we do not try to embed a nursery pointer in |
| 94 |
// jit-code. Precisely because it can flow in from anywhere, it's not |
| 95 |
// guaranteed to be non-lazy. Hence, don't access its script! |
| 96 |
if (fun_) { |
| 97 |
fun_ = fun_->baseScript()->function(); |
| 98 |
MOZ_ASSERT(fun_->isTenured()); |
| 99 |
} |
| 100 |
|
| 101 |
nimplicit_ = StartArgSlot(script) /* env chain and argument obj */ |
| 102 |
+ (fun ? 1 : 0); /* this */ |
| 103 |
nargs_ = fun ? fun->nargs() : 0; |
| 104 |
nlocals_ = script->nfixed(); |
| 105 |
|
| 106 |
// An extra slot is needed for global scopes because InitGLexical (stack |
| 107 |
// depth 1) is compiled as a SetProp (stack depth 2) on the global lexical |
| 108 |
// scope. |
| 109 |
uint32_t extra = script->isGlobalCode() ? 1 : 0; |
| 110 |
nstack_ = std::max<unsigned>(script->nslots() - script->nfixed(), |
| 111 |
MinJITStackSize) + |
| 112 |
extra; |
| 113 |
nslots_ = nimplicit_ + nargs_ + nlocals_ + nstack_; |
| 114 |
|
| 115 |
// For derived class constructors, find and cache the frame slot for |
| 116 |
// the .this binding. This slot is assumed to be always |
| 117 |
// observable. See isObservableFrameSlot. |
| 118 |
if (script->isDerivedClassConstructor()) { |
| 119 |
MOZ_ASSERT(script->functionHasThisBinding()); |
| 120 |
for (BindingIter bi(script); bi; bi++) { |
| 121 |
if (bi.name() != runtime->names().dot_this_) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
BindingLocation loc = bi.location(); |
| 125 |
if (loc.kind() == BindingLocation::Kind::Frame) { |
| 126 |
thisSlotForDerivedClassConstructor_ = |
| 127 |
mozilla::Some(localSlot(loc.slot())); |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// If the script uses an environment in body, the environment chain |
| 134 |
// will need to be observable. |
| 135 |
needsBodyEnvironmentObject_ = script->needsBodyEnvironment(); |
| 136 |
funNeedsSomeEnvironmentObject_ = |
| 137 |
fun ? fun->needsSomeEnvironmentObject() : false; |
| 138 |
} |
| 139 |
|
| 140 |
explicit CompileInfo(unsigned nlocals) |
| 141 |
: script_(nullptr), |
| 142 |
fun_(nullptr), |
| 143 |
osrPc_(nullptr), |
| 144 |
scriptNeedsArgsObj_(false), |
| 145 |
hadEagerTruncationBailout_(false), |
| 146 |
hadSpeculativePhiBailout_(false), |
| 147 |
hadLICMInvalidation_(false), |
| 148 |
hadReorderingBailout_(false), |
| 149 |
hadBoundsCheckBailout_(false), |
| 150 |
hadUnboxFoldingBailout_(false), |
| 151 |
branchHintingEnabled_(false), |
| 152 |
mayReadFrameArgsDirectly_(false), |
| 153 |
anyFormalIsForwarded_(false), |
| 154 |
inlineScriptTree_(nullptr), |
| 155 |
needsBodyEnvironmentObject_(false), |
| 156 |
funNeedsSomeEnvironmentObject_(false), |
| 157 |
hasSeenObjectEmulateUndefinedFuseIntact_(false), |
| 158 |
hasSeenArrayExceedsInt32LengthFuseIntact_(false) { |
| 159 |
nimplicit_ = 0; |
| 160 |
nargs_ = 0; |
| 161 |
nlocals_ = nlocals; |
| 162 |
nstack_ = 1; /* For FunctionCompiler::pushPhiInput/popPhiOutput */ |
| 163 |
nslots_ = nlocals_ + nstack_; |
| 164 |
} |
| 165 |
|
| 166 |
JSScript* script() const { return script_; } |
| 167 |
bool compilingWasm() const { return script() == nullptr; } |
| 168 |
ModuleObject* module() const { return script_->module(); } |
| 169 |
jsbytecode* osrPc() const { return osrPc_; } |
| 170 |
InlineScriptTree* inlineScriptTree() const { return inlineScriptTree_; } |
| 171 |
|
| 172 |
// It's not safe to access the JSFunction off main thread. |
| 173 |
bool hasFunMaybeLazy() const { return fun_; } |
| 174 |
ImmGCPtr funMaybeLazy() const { return ImmGCPtr(fun_); } |
| 175 |
|
| 176 |
const char* filename() const { return script_->filename(); } |
| 177 |
|
| 178 |
unsigned lineno() const { return script_->lineno(); } |
| 179 |
|
| 180 |
// Total number of slots: args, locals, and stack. |
| 181 |
unsigned nslots() const { return nslots_; } |
| 182 |
|
| 183 |
// Number of slots needed for env chain, return value, |
| 184 |
// maybe argumentsobject and this value. |
| 185 |
unsigned nimplicit() const { return nimplicit_; } |
| 186 |
// Number of arguments (without counting this value). |
| 187 |
unsigned nargs() const { return nargs_; } |
| 188 |
// Number of slots needed for all local variables. This includes "fixed |
| 189 |
// vars" (see above) and also block-scoped locals. |
| 190 |
unsigned nlocals() const { return nlocals_; } |
| 191 |
unsigned ninvoke() const { return nslots_ - nstack_; } |
| 192 |
|
| 193 |
uint32_t environmentChainSlot() const { |
| 194 |
MOZ_ASSERT(script()); |
| 195 |
return 0; |
| 196 |
} |
| 197 |
uint32_t returnValueSlot() const { |
| 198 |
MOZ_ASSERT(script()); |
| 199 |
return 1; |
| 200 |
} |
| 201 |
uint32_t argsObjSlot() const { |
| 202 |
MOZ_ASSERT(needsArgsObj()); |
| 203 |
return 2; |
| 204 |
} |
| 205 |
uint32_t thisSlot() const { |
| 206 |
MOZ_ASSERT(hasFunMaybeLazy()); |
| 207 |
MOZ_ASSERT(nimplicit_ > 0); |
| 208 |
return nimplicit_ - 1; |
| 209 |
} |
| 210 |
uint32_t firstArgSlot() const { return nimplicit_; } |
| 211 |
uint32_t argSlotUnchecked(uint32_t i) const { |
| 212 |
// During initialization, some routines need to get at arg |
| 213 |
// slots regardless of how regular argument access is done. |
| 214 |
MOZ_ASSERT(i < nargs_); |
| 215 |
return nimplicit_ + i; |
| 216 |
} |
| 217 |
uint32_t argSlot(uint32_t i) const { |
| 218 |
// This should only be accessed when compiling functions for |
| 219 |
// which argument accesses don't need to go through the |
| 220 |
// argument object. |
| 221 |
MOZ_ASSERT(!argsObjAliasesFormals()); |
| 222 |
return argSlotUnchecked(i); |
| 223 |
} |
| 224 |
uint32_t firstLocalSlot() const { return nimplicit_ + nargs_; } |
| 225 |
uint32_t localSlot(uint32_t i) const { return firstLocalSlot() + i; } |
| 226 |
uint32_t firstStackSlot() const { return firstLocalSlot() + nlocals(); } |
| 227 |
uint32_t stackSlot(uint32_t i) const { return firstStackSlot() + i; } |
| 228 |
|
| 229 |
uint32_t totalSlots() const { |
| 230 |
MOZ_ASSERT(script() && hasFunMaybeLazy()); |
| 231 |
return nimplicit() + nargs() + nlocals(); |
| 232 |
} |
| 233 |
|
| 234 |
bool hasMappedArgsObj() const { return script()->hasMappedArgsObj(); } |
| 235 |
bool needsArgsObj() const { return scriptNeedsArgsObj_; } |
| 236 |
bool argsObjAliasesFormals() const { |
| 237 |
return scriptNeedsArgsObj_ && script()->hasMappedArgsObj(); |
| 238 |
} |
| 239 |
|
| 240 |
bool needsBodyEnvironmentObject() const { |
| 241 |
return needsBodyEnvironmentObject_; |
| 242 |
} |
| 243 |
|
| 244 |
enum class SlotObservableKind { |
| 245 |
// This slot must be preserved because it's observable outside SSA uses. |
| 246 |
// It can't be recovered before or during bailout. |
| 247 |
ObservableNotRecoverable, |
| 248 |
|
| 249 |
// This slot must be preserved because it's observable, but it can be |
| 250 |
// recovered. |
| 251 |
ObservableRecoverable, |
| 252 |
|
| 253 |
// This slot is not observable outside SSA uses. |
| 254 |
NotObservable, |
| 255 |
}; |
| 256 |
|
| 257 |
inline SlotObservableKind getSlotObservableKind(uint32_t slot) const { |
| 258 |
// Locals and expression stack slots. |
| 259 |
if (slot >= firstLocalSlot()) { |
| 260 |
// The |this| slot for a derived class constructor is a local slot. |
| 261 |
// It should never be optimized out, as a Debugger might need to perform |
| 262 |
// TDZ checks on it via, e.g., an exceptionUnwind handler. The TDZ check |
| 263 |
// is required for correctness if the handler decides to continue |
| 264 |
// execution. |
| 265 |
if (thisSlotForDerivedClassConstructor_ && |
| 266 |
*thisSlotForDerivedClassConstructor_ == slot) { |
| 267 |
return SlotObservableKind::ObservableNotRecoverable; |
| 268 |
} |
| 269 |
return SlotObservableKind::NotObservable; |
| 270 |
} |
| 271 |
|
| 272 |
// Formal argument slots. |
| 273 |
if (slot >= firstArgSlot()) { |
| 274 |
MOZ_ASSERT(hasFunMaybeLazy()); |
| 275 |
MOZ_ASSERT(slot - firstArgSlot() < nargs()); |
| 276 |
|
| 277 |
// Preserve formal arguments if they might be read when creating a rest or |
| 278 |
// arguments object. In non-strict scripts, Function.arguments can create |
| 279 |
// an arguments object dynamically so we always preserve the arguments. |
| 280 |
if (mayReadFrameArgsDirectly_ || !script()->strict()) { |
| 281 |
return SlotObservableKind::ObservableRecoverable; |
| 282 |
} |
| 283 |
return SlotObservableKind::NotObservable; |
| 284 |
} |
| 285 |
|
| 286 |
// |this| slot is observable but it can be recovered. |
| 287 |
if (hasFunMaybeLazy() && slot == thisSlot()) { |
| 288 |
return SlotObservableKind::ObservableRecoverable; |
| 289 |
} |
| 290 |
|
| 291 |
// Environment chain slot. |
| 292 |
if (slot == environmentChainSlot()) { |
| 293 |
// If environments can be added in the body (after the prologue) we need |
| 294 |
// to preserve the environment chain slot. It can't be recovered. |
| 295 |
if (needsBodyEnvironmentObject()) { |
| 296 |
return SlotObservableKind::ObservableNotRecoverable; |
| 297 |
} |
| 298 |
// If the function may need an arguments object, also preserve the |
| 299 |
// environment chain because it may be needed to reconstruct the arguments |
| 300 |
// object during bailout. |
| 301 |
if (funNeedsSomeEnvironmentObject_ || needsArgsObj()) { |
| 302 |
return SlotObservableKind::ObservableRecoverable; |
| 303 |
} |
| 304 |
return SlotObservableKind::NotObservable; |
| 305 |
} |
| 306 |
|
| 307 |
// The arguments object is observable. If it does not escape, it can |
| 308 |
// be recovered. |
| 309 |
if (needsArgsObj() && slot == argsObjSlot()) { |
| 310 |
MOZ_ASSERT(hasFunMaybeLazy()); |
| 311 |
return SlotObservableKind::ObservableRecoverable; |
| 312 |
} |
| 313 |
|
| 314 |
MOZ_ASSERT(slot == returnValueSlot()); |
| 315 |
return SlotObservableKind::NotObservable; |
| 316 |
} |
| 317 |
|
| 318 |
// Returns true if a slot can be observed out-side the current frame while |
| 319 |
// the frame is active on the stack. This implies that these definitions |
| 320 |
// would have to be executed and that they cannot be removed even if they |
| 321 |
// are unused. |
| 322 |
inline bool isObservableSlot(uint32_t slot) const { |
| 323 |
SlotObservableKind kind = getSlotObservableKind(slot); |
| 324 |
return (kind == SlotObservableKind::ObservableNotRecoverable || |
| 325 |
kind == SlotObservableKind::ObservableRecoverable); |
| 326 |
} |
| 327 |
|
| 328 |
// Returns true if a slot can be recovered before or during a bailout. A |
| 329 |
// definition which can be observed and recovered, implies that this |
| 330 |
// definition can be optimized away as long as we can compute its values. |
| 331 |
bool isRecoverableOperand(uint32_t slot) const { |
| 332 |
SlotObservableKind kind = getSlotObservableKind(slot); |
| 333 |
return (kind == SlotObservableKind::ObservableRecoverable || |
| 334 |
kind == SlotObservableKind::NotObservable); |
| 335 |
} |
| 336 |
|
| 337 |
// Check previous bailout states to prevent doing the same bailout in the |
| 338 |
// next compilation. |
| 339 |
bool hadEagerTruncationBailout() const { return hadEagerTruncationBailout_; } |
| 340 |
bool hadSpeculativePhiBailout() const { return hadSpeculativePhiBailout_; } |
| 341 |
bool hadLICMInvalidation() const { return hadLICMInvalidation_; } |
| 342 |
bool hadReorderingBailout() const { return hadReorderingBailout_; } |
| 343 |
bool hadBoundsCheckBailout() const { return hadBoundsCheckBailout_; } |
| 344 |
bool hadUnboxFoldingBailout() const { return hadUnboxFoldingBailout_; } |
| 345 |
|
| 346 |
bool branchHintingEnabled() const { |
| 347 |
return compilingWasm() && branchHintingEnabled_; |
| 348 |
} |
| 349 |
|
| 350 |
void setBranchHinting(bool value) { branchHintingEnabled_ = value; } |
| 351 |
|
| 352 |
bool mayReadFrameArgsDirectly() const { return mayReadFrameArgsDirectly_; } |
| 353 |
bool anyFormalIsForwarded() const { return anyFormalIsForwarded_; } |
| 354 |
|
| 355 |
bool isDerivedClassConstructor() const { return isDerivedClassConstructor_; } |
| 356 |
|
| 357 |
bool hasSeenObjectEmulateUndefinedFuseIntact() const { |
| 358 |
return hasSeenObjectEmulateUndefinedFuseIntact_; |
| 359 |
} |
| 360 |
|
| 361 |
bool hasSeenArrayExceedsInt32LengthFuseIntact() const { |
| 362 |
return hasSeenArrayExceedsInt32LengthFuseIntact_; |
| 363 |
} |
| 364 |
|
| 365 |
private: |
| 366 |
unsigned nimplicit_; |
| 367 |
unsigned nargs_; |
| 368 |
unsigned nlocals_; |
| 369 |
unsigned nstack_; |
| 370 |
unsigned nslots_; |
| 371 |
mozilla::Maybe<unsigned> thisSlotForDerivedClassConstructor_; |
| 372 |
JSScript* script_; |
| 373 |
JSFunction* fun_; |
| 374 |
jsbytecode* osrPc_; |
| 375 |
|
| 376 |
bool scriptNeedsArgsObj_; |
| 377 |
|
| 378 |
// Record the state of previous bailouts in order to prevent compiling the |
| 379 |
// same function identically the next time. |
| 380 |
bool hadEagerTruncationBailout_; |
| 381 |
bool hadSpeculativePhiBailout_; |
| 382 |
bool hadLICMInvalidation_; |
| 383 |
bool hadReorderingBailout_; |
| 384 |
bool hadBoundsCheckBailout_; |
| 385 |
bool hadUnboxFoldingBailout_; |
| 386 |
|
| 387 |
bool branchHintingEnabled_; |
| 388 |
|
| 389 |
bool mayReadFrameArgsDirectly_; |
| 390 |
bool anyFormalIsForwarded_; |
| 391 |
|
| 392 |
bool isDerivedClassConstructor_; |
| 393 |
|
| 394 |
InlineScriptTree* inlineScriptTree_; |
| 395 |
|
| 396 |
// Whether a script needs environments within its body. This informs us |
| 397 |
// that the environment chain is not easy to reconstruct. |
| 398 |
bool needsBodyEnvironmentObject_; |
| 399 |
bool funNeedsSomeEnvironmentObject_; |
| 400 |
|
| 401 |
bool hasSeenObjectEmulateUndefinedFuseIntact_; |
| 402 |
bool hasSeenArrayExceedsInt32LengthFuseIntact_; |
| 403 |
}; |
| 404 |
|
| 405 |
} // namespace jit |
| 406 |
} // namespace js |
| 407 |
|
| 408 |
#endif /* jit_CompileInfo_h */ |