1/*
2 * Copyright (C) 2016 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef ArithProfile_h
30#define ArithProfile_h
31
32#include "GPRInfo.h"
33#include "JSCJSValue.h"
34#include "ResultType.h"
35#include "TagRegistersMode.h"
36
37namespace JSC {
38
39class CCallHelpers;
40
41struct ObservedType {
42 ObservedType(uint8_t bits = TypeEmpty)
43 : m_bits(bits)
44 { }
45
46 bool sawInt32() const { return m_bits & TypeInt32; }
47 bool isOnlyInt32() const { return m_bits == TypeInt32; }
48 bool sawNumber() const { return m_bits & TypeNumber; }
49 bool isOnlyNumber() const { return m_bits == TypeNumber; }
50 bool sawNonNumber() const { return m_bits & TypeNonNumber; }
51 bool isOnlyNonNumber() const { return m_bits == TypeNonNumber; }
52 bool isEmpty() const { return !m_bits; }
53 uint8_t bits() const { return m_bits; }
54
55 ObservedType withInt32() const { return ObservedType(m_bits | TypeInt32); }
56 ObservedType withNumber() const { return ObservedType(m_bits | TypeNumber); }
57 ObservedType withNonNumber() const { return ObservedType(m_bits | TypeNonNumber); }
58 ObservedType withoutNonNumber() const { return ObservedType(m_bits & ~TypeNonNumber); }
59
60 bool operator==(const ObservedType& other) const { return m_bits == other.m_bits; }
61
62 static const uint8_t TypeEmpty = 0x0;
63 static const uint8_t TypeInt32 = 0x1;
64 static const uint8_t TypeNumber = 0x02;
65 static const uint8_t TypeNonNumber = 0x04;
66
67private:
68 uint8_t m_bits;
69};
70
71struct ArithProfile {
72private:
73 static const uint32_t numberOfFlagBits = 5;
74 static const uint32_t rhsResultTypeShift = 5;
75 static const uint32_t lhsResultTypeShift = 11;
76 static const uint32_t rhsObservedTypeShift = 17;
77 static const uint32_t lhsObservedTypeShift = 20;
78 static const uint32_t bottom6Bits = (1 << 6) - 1;
79 static const uint32_t bottom3Bits = (1 << 3) - 1;
80 static const uint32_t clearRhsObservedTypeBits = ~((1 << 17) | (1 << 18) | (1 << 19));
81 static const uint32_t clearLhsObservedTypeBits = ~((1 << 20) | (1 << 21) | (1 << 22));
82
83 static_assert(ResultType::numBitsNeeded == 6, "We make a strong assumption here that ResultType fits in 6 bis.");
84
85public:
86 static const uint32_t specialFastPathBit = 1 << 23;
87
88 ArithProfile(ResultType lhs, ResultType rhs)
89 {
90 m_bits = (lhs.bits() << lhsResultTypeShift) | (rhs.bits() << rhsResultTypeShift);
91 ASSERT(lhsResultType().bits() == lhs.bits() && rhsResultType().bits() == rhs.bits());
92 ASSERT(lhsObservedType().isEmpty());
93 ASSERT(rhsObservedType().isEmpty());
94 }
95 ArithProfile()
96 { }
97
98 static ArithProfile fromInt(uint32_t bits)
99 {
100 ArithProfile result;
101 result.m_bits = bits;
102 return result;
103 }
104
105 enum ObservedResults {
106 NonNegZeroDouble = 1 << 0,
107 NegZeroDouble = 1 << 1,
108 NonNumber = 1 << 2,
109 Int32Overflow = 1 << 3,
110 Int52Overflow = 1 << 4,
111 };
112
113 ResultType lhsResultType() const { return ResultType((m_bits >> lhsResultTypeShift) & bottom6Bits); }
114 ResultType rhsResultType() const { return ResultType((m_bits >> rhsResultTypeShift) & bottom6Bits); }
115
116 ObservedType lhsObservedType() const { return ObservedType((m_bits >> lhsObservedTypeShift) & bottom3Bits); }
117 ObservedType rhsObservedType() const { return ObservedType((m_bits >> rhsObservedTypeShift) & bottom3Bits); }
118 void setLhsObservedType(ObservedType type)
119 {
120 uint32_t bits = m_bits;
121 bits &= clearLhsObservedTypeBits;
122 bits |= type.bits() << lhsObservedTypeShift;
123 m_bits = bits;
124 ASSERT(lhsObservedType() == type);
125 }
126
127 void setRhsObservedType(ObservedType type)
128 {
129 uint32_t bits = m_bits;
130 bits &= clearRhsObservedTypeBits;
131 bits |= type.bits() << rhsObservedTypeShift;
132 m_bits = bits;
133 ASSERT(rhsObservedType() == type);
134 }
135
136 unsigned specialFastPathCount() const
137 {
138 // FIXME:
139 return m_bits & specialFastPathBit ? UINT_MAX : 0;
140 }
141
142 bool didObserveNonInt32() const { return hasBits(NonNegZeroDouble | NegZeroDouble | NonNumber); }
143 bool didObserveDouble() const { return hasBits(NonNegZeroDouble | NegZeroDouble); }
144 bool didObserveNonNegZeroDouble() const { return hasBits(NonNegZeroDouble); }
145 bool didObserveNegZeroDouble() const { return hasBits(NegZeroDouble); }
146 bool didObserveNonNumber() const { return hasBits(NonNumber); }
147 bool didObserveInt32Overflow() const { return hasBits(Int32Overflow); }
148 bool didObserveInt52Overflow() const { return hasBits(Int52Overflow); }
149
150 void setObservedNonNegZeroDouble() { setBit(NonNegZeroDouble); }
151 void setObservedNegZeroDouble() { setBit(NegZeroDouble); }
152 void setObservedNonNumber() { setBit(NonNumber); }
153 void setObservedInt32Overflow() { setBit(Int32Overflow); }
154 void setObservedInt52Overflow() { setBit(Int52Overflow); }
155
156 void* addressOfBits() { return &m_bits; }
157
158 void detectNumericness(JSValue value)
159 {
160 if (value.isInt32())
161 return;
162 if (value.isNumber()) {
163 m_bits |= Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble;
164 return;
165 }
166 m_bits |= NonNumber;
167 }
168
169 void lhsSawInt32() { setLhsObservedType(lhsObservedType().withInt32()); }
170 void lhsSawNumber() { setLhsObservedType(lhsObservedType().withNumber()); }
171 void lhsSawNonNumber() { setLhsObservedType(lhsObservedType().withNonNumber()); }
172 void rhsSawInt32() { setRhsObservedType(rhsObservedType().withInt32()); }
173 void rhsSawNumber() { setRhsObservedType(rhsObservedType().withNumber()); }
174 void rhsSawNonNumber() { setRhsObservedType(rhsObservedType().withNonNumber()); }
175
176 void observeLHSAndRHS(JSValue lhs, JSValue rhs)
177 {
178 ArithProfile newProfile = *this;
179 if (lhs.isNumber()) {
180 if (lhs.isInt32())
181 newProfile.lhsSawInt32();
182 else
183 newProfile.lhsSawNumber();
184 } else
185 newProfile.lhsSawNonNumber();
186
187 if (rhs.isNumber()) {
188 if (rhs.isInt32())
189 newProfile.rhsSawInt32();
190 else
191 newProfile.rhsSawNumber();
192 } else
193 newProfile.rhsSawNonNumber();
194
195 m_bits = newProfile.bits();
196 }
197
198#if ENABLE(JIT)
199 // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble) if it sees a
200 // double. Sets NonNumber if it sees a non-number.
201 void emitDetectNumericness(CCallHelpers&, JSValueRegs, TagRegistersMode = HaveTagRegisters);
202
203 // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble).
204 void emitSetDouble(CCallHelpers&);
205
206 // Sets NonNumber.
207 void emitSetNonNumber(CCallHelpers&);
208#endif // ENABLE(JIT)
209
210 uint32_t bits() const { return m_bits; }
211
212private:
213 bool hasBits(int mask) const { return m_bits & mask; }
214 void setBit(int mask) { m_bits |= mask; }
215
216 uint32_t m_bits { 0 }; // We take care to update m_bits only in a single operation. We don't ever store an inconsistent bit representation to it.
217};
218
219} // namespace JSC
220
221namespace WTF {
222
223void printInternal(PrintStream&, const JSC::ArithProfile&);
224
225} // namespace WTF
226
227#endif // ArithProfile_h