1/*
2 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(DFG_JIT)
29
30#include <wtf/PrintStream.h>
31#include <wtf/StdLibExtras.h>
32
33namespace JSC { namespace DFG {
34
35// Entries in the NodeType enum (below) are composed of an id, a result type (possibly none)
36// and some additional informative flags (must generate, is constant, etc).
37#define NodeResultMask 0x0007
38#define NodeResultJS 0x0001
39#define NodeResultNumber 0x0002
40#define NodeResultDouble 0x0003
41#define NodeResultInt32 0x0004
42#define NodeResultInt52 0x0005
43#define NodeResultBoolean 0x0006
44#define NodeResultStorage 0x0007
45
46#define NodeMustGenerate 0x0008 // set on nodes that have side effects, and may not trivially be removed by DCE.
47#define NodeHasVarArgs 0x0010
48
49#define NodeBehaviorMask 0x007e0
50#define NodeMayHaveDoubleResult 0x00020
51#define NodeMayOverflowInt52 0x00040
52#define NodeMayOverflowInt32InBaseline 0x00080
53#define NodeMayOverflowInt32InDFG 0x00100
54#define NodeMayNegZeroInBaseline 0x00200
55#define NodeMayNegZeroInDFG 0x00400
56#define NodeMayHaveNonNumericResult 0x00800
57#define NodeMayHaveBigIntResult 0x01000
58#define NodeMayHaveNonIntResult (NodeMayHaveDoubleResult | NodeMayHaveNonNumericResult | NodeMayHaveBigIntResult)
59
60#define NodeBytecodeBackPropMask 0x3e000
61#define NodeBytecodeUseBottom 0x00000
62#define NodeBytecodeUsesAsNumber 0x02000 // The result of this computation may be used in a context that observes fractional, or bigger-than-int32, results.
63#define NodeBytecodeNeedsNegZero 0x04000 // The result of this computation may be used in a context that observes -0.
64#define NodeBytecodeUsesAsOther 0x08000 // The result of this computation may be used in a context that distinguishes between NaN and other things (like undefined).
65#define NodeBytecodeUsesAsValue (NodeBytecodeUsesAsNumber | NodeBytecodeNeedsNegZero | NodeBytecodeUsesAsOther)
66#define NodeBytecodeUsesAsInt 0x10000 // The result of this computation is known to be used in a context that prefers, but does not require, integer values.
67#define NodeBytecodeUsesAsArrayIndex 0x20000 // The result of this computation is known to be used in a context that strongly prefers integer values, to the point that we should avoid using doubles if at all possible.
68
69#define NodeArithFlagsMask (NodeBehaviorMask | NodeBytecodeBackPropMask)
70
71#define NodeIsFlushed 0x40000 // Computed by CPSRethreadingPhase, will tell you which local nodes are backwards-reachable from a Flush.
72
73#define NodeMiscFlag1 0x80000
74#define NodeMiscFlag2 0x100000
75
76typedef uint32_t NodeFlags;
77
78static inline bool bytecodeUsesAsNumber(NodeFlags flags)
79{
80 return !!(flags & NodeBytecodeUsesAsNumber);
81}
82
83static inline bool bytecodeCanTruncateInteger(NodeFlags flags)
84{
85 return !bytecodeUsesAsNumber(flags);
86}
87
88static inline bool bytecodeCanIgnoreNegativeZero(NodeFlags flags)
89{
90 return !(flags & NodeBytecodeNeedsNegZero);
91}
92
93enum RareCaseProfilingSource {
94 BaselineRareCase, // Comes from slow case counting in the baseline JIT.
95 DFGRareCase, // Comes from OSR exit profiles.
96 AllRareCases
97};
98
99static inline bool nodeMayOverflowInt52(NodeFlags flags, RareCaseProfilingSource)
100{
101 return !!(flags & NodeMayOverflowInt52);
102}
103
104static inline bool nodeMayOverflowInt32(NodeFlags flags, RareCaseProfilingSource source)
105{
106 NodeFlags mask = 0;
107 switch (source) {
108 case BaselineRareCase:
109 mask = NodeMayOverflowInt32InBaseline;
110 break;
111 case DFGRareCase:
112 mask = NodeMayOverflowInt32InDFG;
113 break;
114 case AllRareCases:
115 mask = NodeMayOverflowInt32InBaseline | NodeMayOverflowInt32InDFG;
116 break;
117 }
118 return !!(flags & mask);
119}
120
121static inline bool nodeMayNegZero(NodeFlags flags, RareCaseProfilingSource source)
122{
123 NodeFlags mask = 0;
124 switch (source) {
125 case BaselineRareCase:
126 mask = NodeMayNegZeroInBaseline;
127 break;
128 case DFGRareCase:
129 mask = NodeMayNegZeroInDFG;
130 break;
131 case AllRareCases:
132 mask = NodeMayNegZeroInBaseline | NodeMayNegZeroInDFG;
133 break;
134 }
135 return !!(flags & mask);
136}
137
138static inline bool nodeCanSpeculateInt32(NodeFlags flags, RareCaseProfilingSource source)
139{
140 if (nodeMayOverflowInt32(flags, source))
141 return !bytecodeUsesAsNumber(flags);
142
143 if (nodeMayNegZero(flags, source))
144 return bytecodeCanIgnoreNegativeZero(flags);
145
146 return true;
147}
148
149static inline bool nodeCanSpeculateInt52(NodeFlags flags, RareCaseProfilingSource source)
150{
151 if (nodeMayOverflowInt52(flags, source))
152 return false;
153
154 if (nodeMayNegZero(flags, source))
155 return bytecodeCanIgnoreNegativeZero(flags);
156
157 return true;
158}
159
160// FIXME: Get rid of this.
161// https://bugs.webkit.org/show_bug.cgi?id=131689
162static inline NodeFlags canonicalResultRepresentation(NodeFlags flags)
163{
164 switch (flags) {
165 case NodeResultDouble:
166 case NodeResultInt52:
167 case NodeResultStorage:
168 return flags;
169 default:
170 return NodeResultJS;
171 }
172}
173
174void dumpNodeFlags(PrintStream&, NodeFlags);
175MAKE_PRINT_ADAPTOR(NodeFlagsDump, NodeFlags, dumpNodeFlags);
176
177} } // namespace JSC::DFG
178
179#endif // ENABLE(DFG_JIT)
180