| 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 | class ObservedResults { |
| 70 | public: |
| 71 | enum Tags : uint8_t { |
| 72 | NonNegZeroDouble = 1 << 0, |
| 73 | NegZeroDouble = 1 << 1, |
| 74 | NonNumeric = 1 << 2, |
| 75 | Int32Overflow = 1 << 3, |
| 76 | Int52Overflow = 1 << 4, |
| 77 | BigInt = 1 << 5, |
| 78 | }; |
| 79 | static constexpr uint32_t numBitsNeeded = 6; |
| 80 | |
| 81 | ObservedResults() = default; |
| 82 | explicit ObservedResults(uint8_t bits) |
| 83 | : m_bits(bits) |
| 84 | { } |
| 85 | |
| 86 | bool didObserveNonInt32() { return m_bits & (NonNegZeroDouble | NegZeroDouble | NonNumeric | BigInt); } |
| 87 | bool didObserveDouble() { return m_bits & (NonNegZeroDouble | NegZeroDouble); } |
| 88 | bool didObserveNonNegZeroDouble() { return m_bits & NonNegZeroDouble; } |
| 89 | bool didObserveNegZeroDouble() { return m_bits & NegZeroDouble; } |
| 90 | bool didObserveNonNumeric() { return m_bits & NonNumeric; } |
| 91 | bool didObserveBigInt() { return m_bits & BigInt; } |
| 92 | bool didObserveInt32Overflow() { return m_bits & Int32Overflow; } |
| 93 | bool didObserveInt52Overflow() { return m_bits & Int52Overflow; } |
| 94 | |
| 95 | private: |
| 96 | uint8_t m_bits { 0 }; |
| 97 | }; |
| 98 | |
| 99 | template <typename BitfieldType> |
| 100 | class ArithProfile { |
| 101 | public: |
| 102 | ObservedResults observedResults() const |
| 103 | { |
| 104 | return ObservedResults(m_bits & ((1 << ObservedResults::numBitsNeeded) - 1)); |
| 105 | } |
| 106 | bool didObserveNonInt32() const { return observedResults().didObserveNonInt32();} |
| 107 | bool didObserveDouble() const { return observedResults().didObserveDouble(); } |
| 108 | bool didObserveNonNegZeroDouble() const { return observedResults().didObserveNonNegZeroDouble(); } |
| 109 | bool didObserveNegZeroDouble() const { return observedResults().didObserveNegZeroDouble(); } |
| 110 | bool didObserveNonNumeric() const { return observedResults().didObserveNonNumeric(); } |
| 111 | bool didObserveBigInt() const { return observedResults().didObserveBigInt(); } |
| 112 | bool didObserveInt32Overflow() const { return observedResults().didObserveInt32Overflow(); } |
| 113 | bool didObserveInt52Overflow() const { return observedResults().didObserveInt52Overflow(); } |
| 114 | |
| 115 | void setObservedNonNegZeroDouble() { setBit(ObservedResults::NonNegZeroDouble); } |
| 116 | void setObservedNegZeroDouble() { setBit(ObservedResults::NegZeroDouble); } |
| 117 | void setObservedNonNumeric() { setBit(ObservedResults::NonNumeric); } |
| 118 | void setObservedBigInt() { setBit(ObservedResults::BigInt); } |
| 119 | void setObservedInt32Overflow() { setBit(ObservedResults::Int32Overflow); } |
| 120 | void setObservedInt52Overflow() { setBit(ObservedResults::Int52Overflow); } |
| 121 | |
| 122 | void observeResult(JSValue value) |
| 123 | { |
| 124 | if (value.isInt32()) |
| 125 | return; |
| 126 | if (value.isNumber()) { |
| 127 | m_bits |= ObservedResults::Int32Overflow | ObservedResults::Int52Overflow | ObservedResults::NonNegZeroDouble | ObservedResults::NegZeroDouble; |
| 128 | return; |
| 129 | } |
| 130 | if (value && value.isBigInt()) { |
| 131 | m_bits |= ObservedResults::BigInt; |
| 132 | return; |
| 133 | } |
| 134 | m_bits |= ObservedResults::NonNumeric; |
| 135 | } |
| 136 | |
| 137 | const void* addressOfBits() const { return &m_bits; } |
| 138 | |
| 139 | #if ENABLE(JIT) |
| 140 | // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble) if it sees a |
| 141 | // double. Sets NonNumeric if it sees a non-numeric. |
| 142 | void emitObserveResult(CCallHelpers&, JSValueRegs, TagRegistersMode = HaveTagRegisters); |
| 143 | |
| 144 | // Sets (Int32Overflow | Int52Overflow | NonNegZeroDouble | NegZeroDouble). |
| 145 | bool shouldEmitSetDouble() const; |
| 146 | void emitSetDouble(CCallHelpers&) const; |
| 147 | |
| 148 | // Sets NonNumeric |
| 149 | void emitSetNonNumeric(CCallHelpers&) const; |
| 150 | bool shouldEmitSetNonNumeric() const; |
| 151 | |
| 152 | // Sets BigInt |
| 153 | void emitSetBigInt(CCallHelpers&) const; |
| 154 | bool shouldEmitSetBigInt() const; |
| 155 | |
| 156 | void emitUnconditionalSet(CCallHelpers&, BitfieldType) const; |
| 157 | #endif // ENABLE(JIT) |
| 158 | |
| 159 | constexpr uint32_t bits() const { return m_bits; } |
| 160 | |
| 161 | protected: |
| 162 | ArithProfile() = default; |
| 163 | |
| 164 | bool hasBits(int mask) const { return m_bits & mask; } |
| 165 | void setBit(int mask) { m_bits |= mask; } |
| 166 | |
| 167 | BitfieldType 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. |
| 168 | }; |
| 169 | |
| 170 | /* This class stores the following components in 16 bits: |
| 171 | * - ObservedResults |
| 172 | * - ObservedType for the argument |
| 173 | */ |
| 174 | using UnaryArithProfileBase = uint16_t; |
| 175 | class UnaryArithProfile : public ArithProfile<UnaryArithProfileBase> { |
| 176 | static constexpr unsigned argObservedTypeShift = ObservedResults::numBitsNeeded; |
| 177 | |
| 178 | static_assert(argObservedTypeShift + ObservedType::numBitsNeeded <= sizeof(UnaryArithProfileBase) * 8, "Should fit in a the type of the underlying bitfield." ); |
| 179 | |
| 180 | static constexpr UnaryArithProfileBase clearArgObservedTypeBitMask = static_cast<UnaryArithProfileBase>(~(0b111 << argObservedTypeShift)); |
| 181 | |
| 182 | static constexpr UnaryArithProfileBase observedTypeMask = (1 << ObservedType::numBitsNeeded) - 1; |
| 183 | |
| 184 | public: |
| 185 | UnaryArithProfile() |
| 186 | : ArithProfile<UnaryArithProfileBase>() |
| 187 | { |
| 188 | ASSERT(argObservedType().isEmpty()); |
| 189 | ASSERT(argObservedType().isEmpty()); |
| 190 | } |
| 191 | |
| 192 | static constexpr UnaryArithProfileBase observedIntBits() |
| 193 | { |
| 194 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 195 | constexpr UnaryArithProfileBase bits = observedInt32.bits() << argObservedTypeShift; |
| 196 | return bits; |
| 197 | } |
| 198 | static constexpr UnaryArithProfileBase observedNumberBits() |
| 199 | { |
| 200 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 201 | constexpr UnaryArithProfileBase bits = observedNumber.bits() << argObservedTypeShift; |
| 202 | return bits; |
| 203 | } |
| 204 | |
| 205 | constexpr ObservedType argObservedType() const { return ObservedType((m_bits >> argObservedTypeShift) & observedTypeMask); } |
| 206 | void setArgObservedType(ObservedType type) |
| 207 | { |
| 208 | UnaryArithProfileBase bits = m_bits; |
| 209 | bits &= clearArgObservedTypeBitMask; |
| 210 | bits |= type.bits() << argObservedTypeShift; |
| 211 | m_bits = bits; |
| 212 | ASSERT(argObservedType() == type); |
| 213 | } |
| 214 | |
| 215 | void argSawInt32() { setArgObservedType(argObservedType().withInt32()); } |
| 216 | void argSawNumber() { setArgObservedType(argObservedType().withNumber()); } |
| 217 | void argSawNonNumber() { setArgObservedType(argObservedType().withNonNumber()); } |
| 218 | |
| 219 | void observeArg(JSValue arg) |
| 220 | { |
| 221 | UnaryArithProfile newProfile = *this; |
| 222 | if (arg.isNumber()) { |
| 223 | if (arg.isInt32()) |
| 224 | newProfile.argSawInt32(); |
| 225 | else |
| 226 | newProfile.argSawNumber(); |
| 227 | } else |
| 228 | newProfile.argSawNonNumber(); |
| 229 | |
| 230 | m_bits = newProfile.bits(); |
| 231 | } |
| 232 | |
| 233 | bool isObservedTypeEmpty() |
| 234 | { |
| 235 | return argObservedType().isEmpty(); |
| 236 | } |
| 237 | |
| 238 | friend class JSC::LLIntOffsetsExtractor; |
| 239 | }; |
| 240 | |
| 241 | /* This class stores the following components in 16 bits: |
| 242 | * - ObservedResults |
| 243 | * - ObservedType for right-hand-side |
| 244 | * - ObservedType for left-hand-side |
| 245 | * - a bit used by division to indicate whether a special fast path was taken |
| 246 | */ |
| 247 | using BinaryArithProfileBase = uint16_t; |
| 248 | class BinaryArithProfile : public ArithProfile<BinaryArithProfileBase> { |
| 249 | static constexpr uint32_t rhsObservedTypeShift = ObservedResults::numBitsNeeded; |
| 250 | static constexpr uint32_t lhsObservedTypeShift = rhsObservedTypeShift + ObservedType::numBitsNeeded; |
| 251 | |
| 252 | static_assert(ObservedType::numBitsNeeded == 3, "We make a hard assumption about that here." ); |
| 253 | static constexpr BinaryArithProfileBase clearRhsObservedTypeBitMask = static_cast<BinaryArithProfileBase>(~(0b111 << rhsObservedTypeShift)); |
| 254 | static constexpr BinaryArithProfileBase clearLhsObservedTypeBitMask = static_cast<BinaryArithProfileBase>(~(0b111 << lhsObservedTypeShift)); |
| 255 | |
| 256 | static constexpr BinaryArithProfileBase observedTypeMask = (1 << ObservedType::numBitsNeeded) - 1; |
| 257 | |
| 258 | public: |
| 259 | static constexpr BinaryArithProfileBase specialFastPathBit = 1 << (lhsObservedTypeShift + ObservedType::numBitsNeeded); |
| 260 | static_assert((lhsObservedTypeShift + ObservedType::numBitsNeeded + 1) <= sizeof(BinaryArithProfileBase) * 8, "Should fit in a uint32_t." ); |
| 261 | static_assert(!(specialFastPathBit & ~clearLhsObservedTypeBitMask), "These bits should not intersect." ); |
| 262 | static_assert(specialFastPathBit & clearLhsObservedTypeBitMask, "These bits should intersect." ); |
| 263 | static_assert(specialFastPathBit > ~clearLhsObservedTypeBitMask, "These bits should not intersect and specialFastPathBit should be a higher bit." ); |
| 264 | |
| 265 | BinaryArithProfile() |
| 266 | : ArithProfile<BinaryArithProfileBase> () |
| 267 | { |
| 268 | ASSERT(lhsObservedType().isEmpty()); |
| 269 | ASSERT(rhsObservedType().isEmpty()); |
| 270 | } |
| 271 | |
| 272 | static constexpr BinaryArithProfileBase observedIntIntBits() |
| 273 | { |
| 274 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 275 | constexpr BinaryArithProfileBase bits = (observedInt32.bits() << lhsObservedTypeShift) | (observedInt32.bits() << rhsObservedTypeShift); |
| 276 | return bits; |
| 277 | } |
| 278 | static constexpr BinaryArithProfileBase observedNumberIntBits() |
| 279 | { |
| 280 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 281 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 282 | constexpr BinaryArithProfileBase bits = (observedNumber.bits() << lhsObservedTypeShift) | (observedInt32.bits() << rhsObservedTypeShift); |
| 283 | return bits; |
| 284 | } |
| 285 | static constexpr BinaryArithProfileBase observedIntNumberBits() |
| 286 | { |
| 287 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 288 | constexpr ObservedType observedInt32 { ObservedType().withInt32() }; |
| 289 | constexpr BinaryArithProfileBase bits = (observedInt32.bits() << lhsObservedTypeShift) | (observedNumber.bits() << rhsObservedTypeShift); |
| 290 | return bits; |
| 291 | } |
| 292 | static constexpr BinaryArithProfileBase observedNumberNumberBits() |
| 293 | { |
| 294 | constexpr ObservedType observedNumber { ObservedType().withNumber() }; |
| 295 | constexpr BinaryArithProfileBase bits = (observedNumber.bits() << lhsObservedTypeShift) | (observedNumber.bits() << rhsObservedTypeShift); |
| 296 | return bits; |
| 297 | } |
| 298 | |
| 299 | constexpr ObservedType lhsObservedType() const { return ObservedType((m_bits >> lhsObservedTypeShift) & observedTypeMask); } |
| 300 | constexpr ObservedType rhsObservedType() const { return ObservedType((m_bits >> rhsObservedTypeShift) & observedTypeMask); } |
| 301 | void setLhsObservedType(ObservedType type) |
| 302 | { |
| 303 | BinaryArithProfileBase bits = m_bits; |
| 304 | bits &= clearLhsObservedTypeBitMask; |
| 305 | bits |= type.bits() << lhsObservedTypeShift; |
| 306 | m_bits = bits; |
| 307 | ASSERT(lhsObservedType() == type); |
| 308 | } |
| 309 | |
| 310 | void setRhsObservedType(ObservedType type) |
| 311 | { |
| 312 | BinaryArithProfileBase bits = m_bits; |
| 313 | bits &= clearRhsObservedTypeBitMask; |
| 314 | bits |= type.bits() << rhsObservedTypeShift; |
| 315 | m_bits = bits; |
| 316 | ASSERT(rhsObservedType() == type); |
| 317 | } |
| 318 | |
| 319 | bool tookSpecialFastPath() const { return m_bits & specialFastPathBit; } |
| 320 | |
| 321 | void lhsSawInt32() { setLhsObservedType(lhsObservedType().withInt32()); } |
| 322 | void lhsSawNumber() { setLhsObservedType(lhsObservedType().withNumber()); } |
| 323 | void lhsSawNonNumber() { setLhsObservedType(lhsObservedType().withNonNumber()); } |
| 324 | void rhsSawInt32() { setRhsObservedType(rhsObservedType().withInt32()); } |
| 325 | void rhsSawNumber() { setRhsObservedType(rhsObservedType().withNumber()); } |
| 326 | void rhsSawNonNumber() { setRhsObservedType(rhsObservedType().withNonNumber()); } |
| 327 | |
| 328 | void observeLHS(JSValue lhs) |
| 329 | { |
| 330 | BinaryArithProfile newProfile = *this; |
| 331 | if (lhs.isNumber()) { |
| 332 | if (lhs.isInt32()) |
| 333 | newProfile.lhsSawInt32(); |
| 334 | else |
| 335 | newProfile.lhsSawNumber(); |
| 336 | } else |
| 337 | newProfile.lhsSawNonNumber(); |
| 338 | |
| 339 | m_bits = newProfile.bits(); |
| 340 | } |
| 341 | |
| 342 | void observeLHSAndRHS(JSValue lhs, JSValue rhs) |
| 343 | { |
| 344 | observeLHS(lhs); |
| 345 | |
| 346 | BinaryArithProfile newProfile = *this; |
| 347 | if (rhs.isNumber()) { |
| 348 | if (rhs.isInt32()) |
| 349 | newProfile.rhsSawInt32(); |
| 350 | else |
| 351 | newProfile.rhsSawNumber(); |
| 352 | } else |
| 353 | newProfile.rhsSawNonNumber(); |
| 354 | |
| 355 | m_bits = newProfile.bits(); |
| 356 | } |
| 357 | |
| 358 | bool isObservedTypeEmpty() |
| 359 | { |
| 360 | return lhsObservedType().isEmpty() && rhsObservedType().isEmpty(); |
| 361 | } |
| 362 | |
| 363 | friend class JSC::LLIntOffsetsExtractor; |
| 364 | }; |
| 365 | |
| 366 | } // namespace JSC |
| 367 | |
| 368 | namespace WTF { |
| 369 | |
| 370 | void printInternal(PrintStream&, const JSC::UnaryArithProfile&); |
| 371 | void printInternal(PrintStream&, const JSC::BinaryArithProfile&); |
| 372 | void printInternal(PrintStream&, const JSC::ObservedType&); |
| 373 | |
| 374 | } // namespace WTF |
| 375 | |