Debian Codegraph @

js/src/jit/CacheIR.h

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 #ifndef jit_CacheIR_h
8 #define jit_CacheIR_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include "jstypes.h"
17
18 #include "jit/CacheIROpsGenerated.h"
19 #include "js/GCAnnotations.h"
20 #include "js/Value.h"
21
22 struct JS_PUBLIC_API JSContext;
23
24 namespace js {
25 namespace jit {
26
27 // [SMDOC] CacheIR
28 //
29 // CacheIR is an (extremely simple) linear IR language for inline caches.
30 // From this IR, we can generate machine code for Baseline or Ion IC stubs.
31 //
32 // IRWriter
33 // --------
34 // CacheIR bytecode is written using IRWriter. This class also records some
35 // metadata that's used by the Baseline and Ion code generators to generate
36 // (efficient) machine code.
37 //
38 // Sharing Baseline stub code
39 // --------------------------
40 // Baseline stores data (like Shape* and fixed slot offsets) inside the ICStub
41 // structure, instead of embedding them directly in the JitCode. This makes
42 // Baseline IC code slightly slower, but allows us to share IC code between
43 // caches. CacheIR makes it easy to share code between stubs: stubs that have
44 // the same CacheIR (and CacheKind), will have the same Baseline stub code.
45 //
46 // Baseline stubs that share JitCode also share a CacheIRStubInfo structure.
47 // This class stores the CacheIR and the location of GC things stored in the
48 // stub, for the GC.
49 //
50 // JitZone has a CacheIRStubInfo* -> JitCode* weak map that's used to share both
51 // the IR and JitCode between Baseline CacheIR stubs. This HashMap owns the
52 // stubInfo (it uses UniquePtr), so once there are no references left to the
53 // shared stub code, we can also free the CacheIRStubInfo.
54 //
55 // Ion stubs
56 // ---------
57 // Unlike Baseline stubs, Ion stubs do not share stub code, and data stored in
58 // the IonICStub is baked into JIT code. This is one of the reasons Ion stubs
59 // are faster than Baseline stubs. Also note that Ion ICs contain more state
60 // (see IonGetPropertyIC for example) and use dynamic input/output registers,
61 // so sharing stub code for Ion would be much more difficult.
62
63 // An OperandId represents either a cache input or a value returned by a
64 // CacheIR instruction. Most code should use the ValOperandId and ObjOperandId
65 // classes below. The ObjOperandId class represents an operand that's known to
66 // be an object, just as StringOperandId represents a known string, etc.
67 class OperandId {
68 protected:
69 static const uint16_t InvalidId = UINT16_MAX;
70 uint16_t id_;
71
72 explicit OperandId(uint16_t id) : id_(id) {}
73
74 public:
75 OperandId() : id_(InvalidId) {}
76 uint16_t id() const { return id_; }
77 bool valid() const { return id_ != InvalidId; }
78 };
79
80 class ValOperandId : public OperandId {
81 public:
82 ValOperandId() = default;
83 explicit ValOperandId(uint16_t id) : OperandId(id) {}
84 };
85
86 class ValueTagOperandId : public OperandId {
87 public:
88 ValueTagOperandId() = default;
89 explicit ValueTagOperandId(uint16_t id) : OperandId(id) {}
90 };
91
92 class IntPtrOperandId : public OperandId {
93 public:
94 IntPtrOperandId() = default;
95 explicit IntPtrOperandId(uint16_t id) : OperandId(id) {}
96 };
97
98 class ObjOperandId : public OperandId {
99 public:
100 ObjOperandId() = default;
101 explicit ObjOperandId(uint16_t id) : OperandId(id) {}
102
103 bool operator==(const ObjOperandId& other) const { return id_ == other.id_; }
104 bool operator!=(const ObjOperandId& other) const { return id_ != other.id_; }
105 };
106
107 class NumberOperandId : public ValOperandId {
108 public:
109 NumberOperandId() = default;
110 explicit NumberOperandId(uint16_t id) : ValOperandId(id) {}
111 };
112
113 class StringOperandId : public OperandId {
114 public:
115 StringOperandId() = default;
116 explicit StringOperandId(uint16_t id) : OperandId(id) {}
117 };
118
119 class SymbolOperandId : public OperandId {
120 public:
121 SymbolOperandId() = default;
122 explicit SymbolOperandId(uint16_t id) : OperandId(id) {}
123 };
124
125 class BigIntOperandId : public OperandId {
126 public:
127 BigIntOperandId() = default;
128 explicit BigIntOperandId(uint16_t id) : OperandId(id) {}
129 };
130
131 class BooleanOperandId : public OperandId {
132 public:
133 BooleanOperandId() = default;
134 explicit BooleanOperandId(uint16_t id) : OperandId(id) {}
135 };
136
137 class Int32OperandId : public OperandId {
138 public:
139 Int32OperandId() = default;
140 explicit Int32OperandId(uint16_t id) : OperandId(id) {}
141 };
142
143 class TypedOperandId : public OperandId {
144 JSValueType type_;
145
146 public:
147 MOZ_IMPLICIT TypedOperandId(ObjOperandId id)
148 : OperandId(id.id()), type_(JSVAL_TYPE_OBJECT) {}
149 MOZ_IMPLICIT TypedOperandId(StringOperandId id)
150 : OperandId(id.id()), type_(JSVAL_TYPE_STRING) {}
151 MOZ_IMPLICIT TypedOperandId(SymbolOperandId id)
152 : OperandId(id.id()), type_(JSVAL_TYPE_SYMBOL) {}
153 MOZ_IMPLICIT TypedOperandId(BigIntOperandId id)
154 : OperandId(id.id()), type_(JSVAL_TYPE_BIGINT) {}
155 MOZ_IMPLICIT TypedOperandId(BooleanOperandId id)
156 : OperandId(id.id()), type_(JSVAL_TYPE_BOOLEAN) {}
157 MOZ_IMPLICIT TypedOperandId(Int32OperandId id)
158 : OperandId(id.id()), type_(JSVAL_TYPE_INT32) {}
159
160 MOZ_IMPLICIT TypedOperandId(ValueTagOperandId val)
161 : OperandId(val.id()), type_(JSVAL_TYPE_UNKNOWN) {}
162 MOZ_IMPLICIT TypedOperandId(IntPtrOperandId id)
163 : OperandId(id.id()), type_(JSVAL_TYPE_UNKNOWN) {}
164
165 TypedOperandId(ValOperandId val, JSValueType type)
166 : OperandId(val.id()), type_(type) {}
167
168 JSValueType type() const { return type_; }
169 };
170
171 #define CACHE_IR_KINDS(_) \
172 _(GetProp) \
173 _(GetElem) \
174 _(GetName) \
175 _(GetPropSuper) \
176 _(GetElemSuper) \
177 _(GetImport) \
178 _(LazyConstant) \
179 _(SetProp) \
180 _(SetElem) \
181 _(BindName) \
182 _(In) \
183 _(HasOwn) \
184 _(CheckPrivateField) \
185 _(TypeOf) \
186 _(TypeOfEq) \
187 _(ToPropertyKey) \
188 _(InstanceOf) \
189 _(GetIterator) \
190 _(CloseIter) \
191 _(OptimizeGetIterator) \
192 _(OptimizeSpreadCall) \
193 _(Compare) \
194 _(ToBool) \
195 _(Call) \
196 _(UnaryArith) \
197 _(BinaryArith) \
198 _(NewObject) \
199 _(NewArray) \
200 _(Lambda)
201
202 enum class CacheKind : uint8_t {
203 #define DEFINE_KIND(kind) kind,
204 CACHE_IR_KINDS(DEFINE_KIND)
205 #undef DEFINE_KIND
206 };
207
208 extern const char* const CacheKindNames[];
209
210 extern size_t NumInputsForCacheKind(CacheKind kind);
211
212 enum class CacheOp : uint16_t {
213 #define DEFINE_OP(op, ...) op,
214 CACHE_IR_OPS(DEFINE_OP)
215 #undef DEFINE_OP
216 NumOpcodes,
217 };
218
219 // CacheIR opcode info that's read in performance-sensitive code. Stored as a
220 // single byte per op for better cache locality.
221 struct CacheIROpInfo {
222 uint8_t argLength : 7;
223 bool transpile : 1;
224 };
225 static_assert(sizeof(CacheIROpInfo) == 1);
226 extern const CacheIROpInfo CacheIROpInfos[];
227
228 extern const char* const CacheIROpNames[];
229
230 inline const char* CacheIRCodeName(CacheOp op) {
231 return CacheIROpNames[static_cast<size_t>(op)];
232 }
233
234 extern const uint32_t CacheIROpHealth[];
235
236 class StubField {
237 public:
238 enum class Type : uint8_t {
239 // These fields take up a single word.
240 RawInt32,
241 RawPointer,
242 ICScript,
243 Shape,
244 WeakShape,
245 WeakGetterSetter,
246 JSObject,
247 WeakObject,
248 Symbol,
249 String,
250 WeakBaseScript,
251 JitCode,
252
253 Id,
254 AllocSite,
255
256 // These fields take up 64 bits on all platforms.
257 RawInt64,
258 First64BitType = RawInt64,
259 Value,
260 Double,
261
262 Limit
263 };
264
265 static bool sizeIsWord(Type type) {
266 MOZ_ASSERT(type != Type::Limit);
267 return type < Type::First64BitType;
268 }
269
270 static bool sizeIsInt64(Type type) {
271 MOZ_ASSERT(type != Type::Limit);
272 return type >= Type::First64BitType;
273 }
274
275 static size_t sizeInBytes(Type type) {
276 if (sizeIsWord(type)) {
277 return sizeof(uintptr_t);
278 }
279 MOZ_ASSERT(sizeIsInt64(type));
280 return sizeof(int64_t);
281 }
282
283 private:
284 uint64_t data_;
285 Type type_;
286
287 public:
288 StubField(uint64_t data, Type type) : data_(data), type_(type) {
289 MOZ_ASSERT_IF(sizeIsWord(), data <= UINTPTR_MAX);
290 }
291
292 Type type() const { return type_; }
293
294 bool sizeIsWord() const { return sizeIsWord(type_); }
295 bool sizeIsInt64() const { return sizeIsInt64(type_); }
296
297 size_t sizeInBytes() const { return sizeInBytes(type_); }
298
299 uintptr_t asWord() const {
300 MOZ_ASSERT(sizeIsWord());
301 return uintptr_t(data_);
302 }
303 uint64_t asInt64() const {
304 MOZ_ASSERT(sizeIsInt64());
305 return data_;
306 }
307 uint64_t rawData() const { return data_; }
308 } JS_HAZ_GC_POINTER;
309
310 inline const char* StubFieldTypeName(StubField::Type ty) {
311 switch (ty) {
312 case StubField::Type::RawInt32:
313 return "RawInt32";
314 case StubField::Type::RawPointer:
315 return "RawPointer";
316 case StubField::Type::ICScript:
317 return "ICScript";
318 case StubField::Type::Shape:
319 return "Shape";
320 case StubField::Type::WeakShape:
321 return "WeakShape";
322 case StubField::Type::WeakGetterSetter:
323 return "WeakGetterSetter";
324 case StubField::Type::JSObject:
325 return "JSObject";
326 case StubField::Type::WeakObject:
327 return "WeakObject";
328 case StubField::Type::Symbol:
329 return "Symbol";
330 case StubField::Type::String:
331 return "String";
332 case StubField::Type::WeakBaseScript:
333 return "WeakBaseScript";
334 case StubField::Type::JitCode:
335 return "JitCode";
336 case StubField::Type::Id:
337 return "Id";
338 case StubField::Type::AllocSite:
339 return "AllocSite";
340 case StubField::Type::RawInt64:
341 return "RawInt64";
342 case StubField::Type::Value:
343 return "Value";
344 case StubField::Type::Double:
345 return "Double";
346 case StubField::Type::Limit:
347 return "Limit";
348 }
349 MOZ_CRASH("Unknown StubField::Type");
350 }
351
352 // This class is used to wrap up information about a call to make it
353 // easier to convey from one function to another. (In particular,
354 // CacheIRWriter encodes the CallFlags in CacheIR, and CacheIRReader
355 // decodes them and uses them for compilation.)
356 class CallFlags {
357 public:
358 enum ArgFormat : uint8_t {
359 Unknown,
360 Standard,
361 Spread,
362 FunCall,
363 FunApplyArgsObj,
364 FunApplyArray,
365 FunApplyNullUndefined,
366 LastArgFormat = FunApplyNullUndefined
367 };
368
369 CallFlags() = default;
370 explicit CallFlags(ArgFormat format) : argFormat_(format) {}
371 CallFlags(ArgFormat format, bool isConstructing, bool isSameRealm,
372 bool needsUninitializedThis)
373 : argFormat_(format),
374 isConstructing_(isConstructing),
375 isSameRealm_(isSameRealm),
376 needsUninitializedThis_(needsUninitializedThis) {}
377 CallFlags(bool isConstructing, bool isSpread, bool isSameRealm = false,
378 bool needsUninitializedThis = false)
379 : argFormat_(isSpread ? Spread : Standard),
380 isConstructing_(isConstructing),
381 isSameRealm_(isSameRealm),
382 needsUninitializedThis_(needsUninitializedThis) {}
383
384 ArgFormat getArgFormat() const { return argFormat_; }
385 bool isConstructing() const {
386 MOZ_ASSERT_IF(isConstructing_,
387 argFormat_ == Standard || argFormat_ == Spread);
388 return isConstructing_;
389 }
390 bool isSameRealm() const { return isSameRealm_; }
391 void setIsSameRealm() { isSameRealm_ = true; }
392
393 bool needsUninitializedThis() const { return needsUninitializedThis_; }
394 void setNeedsUninitializedThis() { needsUninitializedThis_ = true; }
395
396 uint8_t toByte() const {
397 // See CacheIRReader::callFlags()
398 MOZ_ASSERT(argFormat_ != ArgFormat::Unknown);
399 uint8_t value = getArgFormat();
400 if (isConstructing()) {
401 value |= CallFlags::IsConstructing;
402 }
403 if (isSameRealm()) {
404 value |= CallFlags::IsSameRealm;
405 }
406 if (needsUninitializedThis()) {
407 value |= CallFlags::NeedsUninitializedThis;
408 }
409 return value;
410 }
411
412 private:
413 ArgFormat argFormat_ = ArgFormat::Unknown;
414 bool isConstructing_ = false;
415 bool isSameRealm_ = false;
416 bool needsUninitializedThis_ = false;
417
418 // Used for encoding/decoding
419 static const uint8_t ArgFormatBits = 4;
420 static const uint8_t ArgFormatMask = (1 << ArgFormatBits) - 1;
421 static_assert(LastArgFormat <= ArgFormatMask, "Not enough arg format bits");
422 static const uint8_t IsConstructing = 1 << 5;
423 static const uint8_t IsSameRealm = 1 << 6;
424 static const uint8_t NeedsUninitializedThis = 1 << 7;
425
426 friend class CacheIRReader;
427 friend class CacheIRWriter;
428 };
429
430 // In baseline, we have to copy args onto the stack. Below this threshold, we
431 // will unroll the arg copy loop. We need to clamp this before providing it as
432 // an arg to a CacheIR op so that everything 5 or greater can share an IC.
433 const uint32_t MaxUnrolledArgCopy = 5;
434 inline uint32_t ClampFixedArgc(uint32_t argc) {
435 return std::min(argc, MaxUnrolledArgCopy);
436 }
437
438 enum class AttachDecision {
439 // We cannot attach a stub.
440 NoAction,
441
442 // We can attach a stub.
443 Attach,
444
445 // We cannot currently attach a stub, but we expect to be able to do so in the
446 // future. In this case, we do not call trackNotAttached().
447 TemporarilyUnoptimizable,
448
449 // We want to attach a stub, but the result of the operation is
450 // needed to generate that stub. For example, AddSlot needs to know
451 // the resulting shape. Note: the attached stub will inspect the
452 // inputs to the operation, so most input checks should be done
453 // before the actual operation, with only minimal checks remaining
454 // for the deferred portion. This prevents arbitrary scripted code
455 // run by the operation from interfering with the conditions being
456 // checked.
457 Deferred
458 };
459
460 // If the input expression evaluates to an AttachDecision other than NoAction,
461 // return that AttachDecision. If it is NoAction, do nothing.
462 #define TRY_ATTACH(expr) \
463 do { \
464 AttachDecision tryAttachTempResult_ = expr; \
465 if (tryAttachTempResult_ != AttachDecision::NoAction) { \
466 return tryAttachTempResult_; \
467 } \
468 } while (0)
469
470 // Set of arguments supported by GetIndexOfArgument.
471 // Support for higher argument indices can be added easily, but is currently
472 // unneeded.
473 enum class ArgumentKind : uint8_t {
474 Callee,
475 This,
476 NewTarget,
477 Arg0,
478 Arg1,
479 Arg2,
480 Arg3,
481 Arg4,
482 Arg5,
483 Arg6,
484 Arg7,
485 NumKinds
486 };
487
488 const uint8_t ArgumentKindArgIndexLimit =
489 uint8_t(ArgumentKind::NumKinds) - uint8_t(ArgumentKind::Arg0);
490
491 inline ArgumentKind ArgumentKindForArgIndex(uint32_t idx) {
492 MOZ_ASSERT(idx < ArgumentKindArgIndexLimit);
493 return ArgumentKind(uint32_t(ArgumentKind::Arg0) + idx);
494 }
495
496 // This function calculates the index of an argument based on the call flags.
497 // addArgc is an out-parameter, indicating whether the value of argc should
498 // be added to the return value to find the actual index.
499 inline int32_t GetIndexOfArgument(ArgumentKind kind, CallFlags flags,
500 bool* addArgc) {
501 // *** STACK LAYOUT (bottom to top) *** ******** INDEX ********
502 // Callee <-- argc+1 + isConstructing
503 // ThisValue <-- argc + isConstructing
504 // Args: | Arg0 | | ArgArray | <-- argc-1 + isConstructing
505 // | Arg1 | --or-- | | <-- argc-2 + isConstructing
506 // | ... | | (if spread | <-- ...
507 // | ArgN | | call) | <-- 0 + isConstructing
508 // NewTarget (only if constructing) <-- 0 (if it exists)
509 //
510 // If this is a spread call, then argc is always 1, and we can calculate the
511 // index directly. If this is not a spread call, then the index of any
512 // argument other than NewTarget depends on argc.
513
514 // First we determine whether the caller needs to add argc.
515 switch (flags.getArgFormat()) {
516 case CallFlags::Standard:
517 *addArgc = true;
518 break;
519 case CallFlags::Spread:
520 // Spread calls do not have Arg1 or higher.
521 MOZ_ASSERT(kind <= ArgumentKind::Arg0);
522 *addArgc = false;
523 break;
524 case CallFlags::Unknown:
525 case CallFlags::FunCall:
526 case CallFlags::FunApplyArgsObj:
527 case CallFlags::FunApplyArray:
528 case CallFlags::FunApplyNullUndefined:
529 MOZ_CRASH("Currently unreachable");
530 break;
531 }
532
533 // Second, we determine the offset relative to argc.
534 bool hasArgumentArray = !*addArgc;
535 switch (kind) {
536 case ArgumentKind::Callee:
537 return flags.isConstructing() + hasArgumentArray + 1;
538 case ArgumentKind::This:
539 return flags.isConstructing() + hasArgumentArray;
540 case ArgumentKind::Arg0:
541 return flags.isConstructing() + hasArgumentArray - 1;
542 case ArgumentKind::Arg1:
543 return flags.isConstructing() + hasArgumentArray - 2;
544 case ArgumentKind::Arg2:
545 return flags.isConstructing() + hasArgumentArray - 3;
546 case ArgumentKind::Arg3:
547 return flags.isConstructing() + hasArgumentArray - 4;
548 case ArgumentKind::Arg4:
549 return flags.isConstructing() + hasArgumentArray - 5;
550 case ArgumentKind::Arg5:
551 return flags.isConstructing() + hasArgumentArray - 6;
552 case ArgumentKind::Arg6:
553 return flags.isConstructing() + hasArgumentArray - 7;
554 case ArgumentKind::Arg7:
555 return flags.isConstructing() + hasArgumentArray - 8;
556 case ArgumentKind::NewTarget:
557 MOZ_ASSERT(flags.isConstructing());
558 *addArgc = false;
559 return 0;
560 default:
561 MOZ_CRASH("Invalid argument kind");
562 }
563 }
564
565 // We use this enum as GuardClass operand, instead of storing Class* pointers
566 // in the IR, to keep the IR compact and the same size on all platforms.
567 enum class GuardClassKind : uint8_t {
568 Array,
569 PlainObject,
570 FixedLengthArrayBuffer,
571 ResizableArrayBuffer,
572 FixedLengthSharedArrayBuffer,
573 GrowableSharedArrayBuffer,
574 FixedLengthDataView,
575 ResizableDataView,
576 MappedArguments,
577 UnmappedArguments,
578 WindowProxy,
579 JSFunction,
580 BoundFunction,
581 Set,
582 Map,
583 Date,
584 };
585
586 const JSClass* ClassFor(GuardClassKind kind);
587
588 enum class ArrayBufferViewKind : uint8_t {
589 FixedLength,
590 Resizable,
591 };
592
593 inline const char* GuardClassKindEnumName(GuardClassKind kind) {
594 switch (kind) {
595 case GuardClassKind::Array:
596 return "Array";
597 case GuardClassKind::PlainObject:
598 return "PlainObject";
599 case GuardClassKind::FixedLengthArrayBuffer:
600 return "FixedLengthArrayBuffer";
601 case GuardClassKind::ResizableArrayBuffer:
602 return "ResizableArrayBuffer";
603 case GuardClassKind::FixedLengthSharedArrayBuffer:
604 return "FixedLengthSharedArrayBuffer";
605 case GuardClassKind::GrowableSharedArrayBuffer:
606 return "GrowableSharedArrayBuffer";
607 case GuardClassKind::FixedLengthDataView:
608 return "FixedLengthDataView";
609 case GuardClassKind::ResizableDataView:
610 return "ResizableDataView";
611 case GuardClassKind::MappedArguments:
612 return "MappedArguments";
613 case GuardClassKind::UnmappedArguments:
614 return "UnmappedArguments";
615 case GuardClassKind::WindowProxy:
616 return "WindowProxy";
617 case GuardClassKind::JSFunction:
618 return "JSFunction";
619 case GuardClassKind::BoundFunction:
620 return "BoundFunction";
621 case GuardClassKind::Set:
622 return "Set";
623 case GuardClassKind::Map:
624 return "Map";
625 case GuardClassKind::Date:
626 return "Date";
627 }
628 MOZ_CRASH("Unknown GuardClassKind");
629 }
630
631 } // namespace jit
632 } // namespace js
633
634 #endif /* jit_CacheIR_h */