| 1 | /* |
| 2 | * Copyright (C) 2016-2019 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 | #include "GPRInfo.h" |
| 29 | #include "JSCJSValue.h" |
| 30 | #include "ResultType.h" |
| 31 | #include "TagRegistersMode.h" |
| 32 | |
| 33 | namespace JSC { |
| 34 | |
| 35 | class CCallHelpers; |
| 36 | |
| 37 | struct ObservedType { |
| 38 | constexpr ObservedType(uint8_t bits = TypeEmpty) |
| 39 | : m_bits(bits) |
| 40 | { } |
| 41 | |
| 42 | constexpr bool sawInt32() const { return m_bits & TypeInt32; } |
| 43 | constexpr bool isOnlyInt32() const { return m_bits == TypeInt32; } |
| 44 | constexpr bool sawNumber() const { return m_bits & TypeNumber; } |
| 45 | constexpr bool isOnlyNumber() const { return m_bits == TypeNumber; } |
| 46 | constexpr bool sawNonNumber() const { return m_bits & TypeNonNumber; } |
| 47 | constexpr bool isOnlyNonNumber() const { return m_bits == TypeNonNumber; } |
| 48 | constexpr bool isEmpty() const { return !m_bits; } |
| 49 | constexpr uint8_t bits() const { return m_bits; } |
| 50 | |
| 51 | constexpr ObservedType withInt32() const { return ObservedType(m_bits | TypeInt32); } |
| 52 | constexpr ObservedType withNumber() const { return ObservedType(m_bits | TypeNumber); } |
| 53 | constexpr ObservedType withNonNumber() const { return ObservedType(m_bits | TypeNonNumber); } |
| 54 | constexpr ObservedType withoutNonNumber() const { return ObservedType(m_bits & ~TypeNonNumber); } |
| 55 | |
| 56 | constexpr bool operator==(const ObservedType& other) const { return m_bits == other.m_bits; } |
| 57 | |
| 58 | static constexpr uint8_t TypeEmpty = 0x0; |
| 59 | static constexpr uint8_t TypeInt32 = 0x1; |
| 60 | static constexpr uint8_t TypeNumber = 0x02; |
| 61 | static constexpr uint8_t TypeNonNumber = 0x04; |
| 62 | |
| 63 | static constexpr uint32_t numBitsNeeded = 3; |
| 64 | |
| 65 | private: |
| 66 | uint8_t m_bits { 0 }; |
| 67 | }; |
| 68 | |
| 69 | struct ArithProfile { |
| 70 | private: |
| 71 | static constexpr uint32_t numberOfFlagBits = 6; |
| 72 | static constexpr uint32_t rhsResultTypeShift = numberOfFlagBits; |
| 73 | static constexpr uint32_t lhsResultTypeShift = rhsResultTypeShift + ResultType::numBitsNeeded; |
| 74 | static constexpr uint32_t rhsObservedTypeShift = lhsResultTypeShift + ResultType::numBitsNeeded; |
| 75 | static constexpr uint32_t lhsObservedTypeShift = rhsObservedTypeShift + ObservedType::numBitsNeeded; |
| 76 | |
| 77 | static_assert(ObservedType::numBitsNeeded == 3, "We make a hard assumption about that here." ); |
| 78 | static constexpr uint32_t clearRhsObservedTypeBitMask = static_cast<uint32_t>(~((1 << rhsObservedTypeShift) | (1 << (rhsObservedTypeShift + 1)) | (1 << (rhsObservedTypeShift + 2)))); |
| 79 | static constexpr uint32_t clearLhsObservedTypeBitMask = static_cast<uint32_t>(~((1 << lhsObservedTypeShift) | (1 << (lhsObservedTypeShift + 1)) | (1 << (lhsObservedTypeShift + 2)))); |
| 80 | |
| 81 | static constexpr uint32_t resultTypeMask = (1 << ResultType::numBitsNeeded) - 1; |
| 82 | static constexpr uint32_t observedTypeMask = (1 << ObservedType::numBitsNeeded) - 1; |
| 83 | |
| 84 | enum class ConstantTag { Constant }; |
| 85 | |
| 86 | public: |
| 87 | static constexpr uint32_t specialFastPathBit = 1 << (lhsObservedTypeShift + ObservedType::numBitsNeeded); |
| 88 | static_assert((lhsObservedTypeShift + ObservedType::numBitsNeeded) <= (sizeof(uint32_t) * 8) - 1, "Should fit in a uint32_t." ); |
| 89 | static_assert(!(specialFastPathBit & ~clearLhsObservedTypeBitMask), "These bits should not intersect." ); |
| 90 | static_assert(specialFastPathBit & clearLhsObservedTypeBitMask, "These bits should intersect." ); |
| 91 | static_assert(specialFastPathBit > ~clearLhsObservedTypeBitMask, "These bits should not intersect and specialFastPathBit should be a higher bit." ); |
| 92 | |
| 93 | ArithProfile(ResultType arg) |
| 94 | : ArithProfile(ConstantTag::Constant, arg) |
| 95 | { |
| 96 | ASSERT(lhsResultType().bits() == arg.bits()); |
| 97 | ASSERT(lhsObservedType().isEmpty()); |
| 98 | ASSERT(rhsObservedType().isEmpty()); |
| 99 | } |
| 100 | |
| 101 | ArithProfile(ResultType lhs, ResultType rhs) |
| 102 | : ArithProfile(ConstantTag::Constant, lhs, rhs) |
| 103 | { |
| 104 | ASSERT(lhsResultType().bits() == lhs.bits() && rhsResultType().bits() == rhs.bits()); |
| 105 | ASSERT(lhsObservedType().isEmpty()); |
| 106 | ASSERT(rhsObservedType().isEmpty()); |
| 107 | } |
| 108 | |
| 109 | ArithProfile(OperandTypes types) |
| 110 | : ArithProfile(types.first(), types.second()) |
| 111 | { } |
| 112 | |
| 113 | ArithProfile() = default; |
| 114 | |
| 115 | static constexpr ArithProfile fromInt(uint32_t bits) |
| 116 | { |
| 117 | return ArithProfile { ConstantTag::Constant, bits }; |
| 118 | } |
| 119 | |
| 120 | static constexpr ArithProfile observedUnaryInt() |
| 121 | { |
| 122 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 123 | constexpr uint32_t bits = observedInt32.bits() << lhsObservedTypeShift; |
| 124 | static_assert(bits == 0x800000, "" ); |
| 125 | return fromInt(bits); |
| 126 | } |
| 127 | static constexpr ArithProfile observedUnaryNumber() |
| 128 | { |
| 129 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 130 | constexpr uint32_t bits = observedNumber.bits() << lhsObservedTypeShift; |
| 131 | static_assert(bits == 0x1000000, "" ); |
| 132 | return fromInt(bits); |
| 133 | } |
| 134 | static constexpr ArithProfile observedBinaryIntInt() |
| 135 | { |
| 136 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 137 | constexpr uint32_t bits = (observedInt32.bits() << lhsObservedTypeShift) | (observedInt32.bits() << rhsObservedTypeShift); |
| 138 | static_assert(bits == 0x900000, "" ); |
| 139 | return fromInt(bits); |
| 140 | } |
| 141 | static constexpr ArithProfile observedBinaryNumberInt() |
| 142 | { |
| 143 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 144 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 145 | constexpr uint32_t bits = (observedNumber.bits() << lhsObservedTypeShift) | (observedInt32.bits() << rhsObservedTypeShift); |
| 146 | static_assert(bits == 0x1100000, "" ); |
| 147 | return fromInt(bits); |
| 148 | } |
| 149 | static constexpr ArithProfile observedBinaryIntNumber() |
| 150 | { |
| 151 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 152 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 153 | constexpr uint32_t bits = (observedInt32.bits() << lhsObservedTypeShift) | (observedNumber.bits() << rhsObservedTypeShift); |
| 154 | static_assert(bits == 0xa00000, "" ); |
| 155 | return fromInt(bits); |
| 156 | } |
| 157 | static constexpr ArithProfile observedBinaryNumberNumber() |
| 158 | { |
| 159 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 160 | constexpr uint32_t bits = (observedNumber.bits() << lhsObservedTypeShift) | (observedNumber.bits() << rhsObservedTypeShift); |
| 161 | static_assert(bits == 0x1200000, "" ); |
| 162 | return fromInt(bits); |
| 163 | } |
| 164 | |
| 165 | enum ObservedResults { |
| 166 | NonNegZeroDouble = 1 << 0, |
| 167 | NegZeroDouble = 1 << 1, |
| 168 | NonNumeric = 1 << 2, |
| 169 | Int32Overflow = 1 << 3, |
| 170 | Int52Overflow = 1 << 4, |
| 171 | BigInt = 1 << 5, |
| 172 | }; |
| 173 | |
| 174 | ResultType lhsResultType() const { return ResultType((m_bits >> lhsResultTypeShift) & resultTypeMask); } |
| 175 | ResultType rhsResultType() const { return ResultType((m_bits >> rhsResultTypeShift) & resultTypeMask); } |
| 176 | |
| 177 | constexpr ObservedType lhsObservedType() const { return ObservedType((m_bits >> lhsObservedTypeShift) & observedTypeMask); } |
| 178 | constexpr ObservedType rhsObservedType() const { return ObservedType((m_bits >> rhsObservedTypeShift) & observedTypeMask); } |
| 179 | void setLhsObservedType(ObservedType type) |
| 180 | { |
| 181 | uint32_t bits = m_bits; |
| 182 | bits &= clearLhsObservedTypeBitMask; |
| 183 | bits |= type.bits() << lhsObservedTypeShift; |
| 184 | m_bits = bits; |
| 185 | ASSERT(lhsObservedType() == type); |
| 186 | } |
| 187 | |
| 188 | void setRhsObservedType(ObservedType type) |
| 189 | { |
| 190 | uint32_t bits = m_bits; |
| 191 | bits &= clearRhsObservedTypeBitMask; |
| 192 | bits |= type.bits() << rhsObservedTypeShift; |
| 193 | m_bits = bits; |
| 194 | ASSERT(rhsObservedType() == type); |
| 195 | } |
| 196 | |
| 197 | bool tookSpecialFastPath() const { return m_bits & specialFastPathBit; } |
| 198 | |
| 199 | bool didObserveNonInt32() const { return hasBits(NonNegZeroDouble | NegZeroDouble | NonNumeric | BigInt); } |
| 200 | bool didObserveDouble() const { return hasBits(NonNegZeroDouble | NegZeroDouble); } |
| 201 | bool didObserveNonNegZeroDouble() const { return hasBits(NonNegZeroDouble); } |
| 202 | bool didObserveNegZeroDouble() const { return hasBits(NegZeroDouble); } |
| 203 | bool didObserveNonNumeric() const { return hasBits(NonNumeric); } |
| 204 | bool didObserveBigInt() const { return hasBits(BigInt); } |
| 205 | bool didObserveInt32Overflow() const { return hasBits(Int32Overflow); } |
| 206 | bool didObserveInt52Overflow() const { return hasBits(Int52Overflow); } |
| 207 | |
| 208 | void setObservedNonNegZeroDouble() { setBit(NonNegZeroDouble); } |
| 209 | void setObservedNegZeroDouble() { setBit(NegZeroDouble); } |
| 210 | void setObservedNonNumeric() { setBit(NonNumeric); } |
| 211 | void setObservedBigInt() { setBit(BigInt); } |
| 212 | void setObservedInt32Overflow() { setBit(Int32Overflow); } |
| 213 | void setObservedInt52Overflow() { setBit(Int52Overflow); } |
| 214 | |
| 215 | const void* addressOfBits() const { return &m_bits; } |
| 216 | |
| 217 | void observeResult(JSValue value) |
| 218 | { |
| 219 | if (value.isInt32()) |
| 220 | return; |
| 221 | if (value.isNumber()) { |
| 222 | m_bits |= Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble; |
| 223 | return; |
| 224 | } |
| 225 | if (value && value.isBigInt()) { |
| 226 | m_bits |= BigInt; |
| 227 | return; |
| 228 | } |
| 229 | m_bits |= NonNumeric; |
| 230 | } |
| 231 | |
| 232 | void lhsSawInt32() { setLhsObservedType(lhsObservedType().withInt32()); } |
| 233 | void lhsSawNumber() { setLhsObservedType(lhsObservedType().withNumber()); } |
| 234 | void lhsSawNonNumber() { setLhsObservedType(lhsObservedType().withNonNumber()); } |
| 235 | void rhsSawInt32() { setRhsObservedType(rhsObservedType().withInt32()); } |
| 236 | void rhsSawNumber() { setRhsObservedType(rhsObservedType().withNumber()); } |
| 237 | void rhsSawNonNumber() { setRhsObservedType(rhsObservedType().withNonNumber()); } |
| 238 | |
| 239 | void observeLHS(JSValue lhs) |
| 240 | { |
| 241 | ArithProfile newProfile = *this; |
| 242 | if (lhs.isNumber()) { |
| 243 | if (lhs.isInt32()) |
| 244 | newProfile.lhsSawInt32(); |
| 245 | else |
| 246 | newProfile.lhsSawNumber(); |
| 247 | } else |
| 248 | newProfile.lhsSawNonNumber(); |
| 249 | |
| 250 | m_bits = newProfile.bits(); |
| 251 | } |
| 252 | |
| 253 | void observeLHSAndRHS(JSValue lhs, JSValue rhs) |
| 254 | { |
| 255 | observeLHS(lhs); |
| 256 | |
| 257 | ArithProfile newProfile = *this; |
| 258 | if (rhs.isNumber()) { |
| 259 | if (rhs.isInt32()) |
| 260 | newProfile.rhsSawInt32(); |
| 261 | else |
| 262 | newProfile.rhsSawNumber(); |
| 263 | } else |
| 264 | newProfile.rhsSawNonNumber(); |
| 265 | |
| 266 | m_bits = newProfile.bits(); |
| 267 | } |
| 268 | |
| 269 | #if ENABLE(JIT) |
| 270 | // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble) if it sees a |
| 271 | // double. Sets NonNumeric if it sees a non-numeric. |
| 272 | void emitObserveResult(CCallHelpers&, JSValueRegs, TagRegistersMode = HaveTagRegisters); |
| 273 | |
| 274 | // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble). |
| 275 | bool shouldEmitSetDouble() const; |
| 276 | void emitSetDouble(CCallHelpers&) const; |
| 277 | |
| 278 | // Sets NonNumber. |
| 279 | void emitSetNonNumeric(CCallHelpers&) const; |
| 280 | bool shouldEmitSetNonNumeric() const; |
| 281 | |
| 282 | // Sets BigInt |
| 283 | void emitSetBigInt(CCallHelpers&) const; |
| 284 | bool shouldEmitSetBigInt() const; |
| 285 | #endif // ENABLE(JIT) |
| 286 | |
| 287 | constexpr uint32_t bits() const { return m_bits; } |
| 288 | |
| 289 | private: |
| 290 | constexpr explicit ArithProfile(ConstantTag, uint32_t bits) |
| 291 | : m_bits(bits) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | constexpr ArithProfile(ConstantTag, ResultType arg) |
| 296 | : m_bits(arg.bits() << lhsResultTypeShift) |
| 297 | { |
| 298 | } |
| 299 | |
| 300 | constexpr ArithProfile(ConstantTag, ResultType lhs, ResultType rhs) |
| 301 | : m_bits((lhs.bits() << lhsResultTypeShift) | (rhs.bits() << rhsResultTypeShift)) |
| 302 | { |
| 303 | } |
| 304 | |
| 305 | bool hasBits(int mask) const { return m_bits & mask; } |
| 306 | void setBit(int mask) { m_bits |= mask; } |
| 307 | |
| 308 | 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. |
| 309 | |
| 310 | friend class JSC::LLIntOffsetsExtractor; |
| 311 | }; |
| 312 | |
| 313 | } // namespace JSC |
| 314 | |
| 315 | namespace WTF { |
| 316 | |
| 317 | void printInternal(PrintStream&, const JSC::ArithProfile&); |
| 318 | void printInternal(PrintStream&, const JSC::ObservedType&); |
| 319 | |
| 320 | } // namespace WTF |
| 321 | |