mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | acdb63e | 2016-05-10 02:01:28 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Apple Inc. All rights reserved. |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 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 | #include "config.h" |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 27 | #include "JITAddGenerator.h" |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 28 | |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 29 | #include "ArithProfile.h" |
| 30 | #include "JITMathIC.h" |
| 31 | #include "LinkBuffer.h" |
| 32 | |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 33 | #if ENABLE(JIT) |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 34 | |
| 35 | namespace JSC { |
| 36 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 37 | JITMathICInlineResult JITAddGenerator::generateInline(CCallHelpers& jit, MathICGenerationState& state, const ArithProfile* arithProfile) |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 38 | { |
| 39 | // We default to speculating int32. |
| 40 | ObservedType lhs = ObservedType().withInt32(); |
| 41 | ObservedType rhs = ObservedType().withInt32(); |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 42 | if (arithProfile) { |
| 43 | lhs = arithProfile->lhsObservedType(); |
| 44 | rhs = arithProfile->rhsObservedType(); |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (lhs.isOnlyNonNumber() && rhs.isOnlyNonNumber()) |
| 48 | return JITMathICInlineResult::DontGenerate; |
| 49 | |
| 50 | if ((lhs.isOnlyInt32() || m_leftOperand.isConstInt32()) && (rhs.isOnlyInt32() || m_rightOperand.isConstInt32())) { |
| 51 | ASSERT(!m_leftOperand.isConstInt32() || !m_rightOperand.isConstInt32()); |
| 52 | if (!m_leftOperand.isConstInt32()) |
| 53 | state.slowPathJumps.append(jit.branchIfNotInt32(m_left)); |
| 54 | if (!m_rightOperand.isConstInt32()) |
| 55 | state.slowPathJumps.append(jit.branchIfNotInt32(m_right)); |
| 56 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 57 | GPRReg scratch = m_scratchGPR; |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 58 | if (m_leftOperand.isConstInt32() || m_rightOperand.isConstInt32()) { |
| 59 | JSValueRegs var = m_leftOperand.isConstInt32() ? m_right : m_left; |
| 60 | int32_t constValue = m_leftOperand.isConstInt32() ? m_leftOperand.asConstInt32() : m_rightOperand.asConstInt32(); |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 61 | if (var.payloadGPR() != m_result.payloadGPR()) |
| 62 | scratch = m_result.payloadGPR(); |
| 63 | state.slowPathJumps.append(jit.branchAdd32(CCallHelpers::Overflow, var.payloadGPR(), CCallHelpers::Imm32(constValue), scratch)); |
| 64 | } else { |
| 65 | if (m_left.payloadGPR() != m_result.payloadGPR() && m_right.payloadGPR() != m_result.payloadGPR()) |
| 66 | scratch = m_result.payloadGPR(); |
| 67 | state.slowPathJumps.append(jit.branchAdd32(CCallHelpers::Overflow, m_right.payloadGPR(), m_left.payloadGPR(), scratch)); |
| 68 | } |
| 69 | jit.boxInt32(scratch, m_result); |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 70 | return JITMathICInlineResult::GeneratedFastPath; |
| 71 | } |
| 72 | |
| 73 | return JITMathICInlineResult::GenerateFullSnippet; |
| 74 | } |
| 75 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 76 | bool JITAddGenerator::generateFastPath(CCallHelpers& jit, CCallHelpers::JumpList& endJumpList, CCallHelpers::JumpList& slowPathJumpList, const ArithProfile* arithProfile, bool shouldEmitProfiling) |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 77 | { |
| 78 | ASSERT(m_scratchGPR != InvalidGPRReg); |
| 79 | ASSERT(m_scratchGPR != m_left.payloadGPR()); |
| 80 | ASSERT(m_scratchGPR != m_right.payloadGPR()); |
mark.lam@apple.com | 102cbf2 | 2015-11-17 21:55:33 +0000 | [diff] [blame] | 81 | #if USE(JSVALUE32_64) |
| 82 | ASSERT(m_scratchGPR != m_left.tagGPR()); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 83 | ASSERT(m_scratchGPR != m_right.tagGPR()); |
| 84 | ASSERT(m_scratchFPR != InvalidFPRReg); |
| 85 | #endif |
| 86 | |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 87 | ASSERT(!m_leftOperand.isConstInt32() || !m_rightOperand.isConstInt32()); |
mark.lam@apple.com | 102cbf2 | 2015-11-17 21:55:33 +0000 | [diff] [blame] | 88 | |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 89 | if (!m_leftOperand.mightBeNumber() || !m_rightOperand.mightBeNumber()) |
| 90 | return false; |
mark.lam@apple.com | 175b90f | 2015-11-19 00:30:37 +0000 | [diff] [blame] | 91 | |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 92 | if (m_leftOperand.isConstInt32() || m_rightOperand.isConstInt32()) { |
| 93 | JSValueRegs var = m_leftOperand.isConstInt32() ? m_right : m_left; |
| 94 | SnippetOperand& varOpr = m_leftOperand.isConstInt32() ? m_rightOperand : m_leftOperand; |
| 95 | SnippetOperand& constOpr = m_leftOperand.isConstInt32() ? m_leftOperand : m_rightOperand; |
mark.lam@apple.com | 102cbf2 | 2015-11-17 21:55:33 +0000 | [diff] [blame] | 96 | |
| 97 | // Try to do intVar + intConstant. |
| 98 | CCallHelpers::Jump notInt32 = jit.branchIfNotInt32(var); |
| 99 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 100 | GPRReg scratch = m_scratchGPR; |
| 101 | if (var.payloadGPR() != m_result.payloadGPR()) |
| 102 | scratch = m_result.payloadGPR(); |
| 103 | slowPathJumpList.append(jit.branchAdd32(CCallHelpers::Overflow, var.payloadGPR(), CCallHelpers::Imm32(constOpr.asConstInt32()), scratch)); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 104 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 105 | jit.boxInt32(scratch, m_result); |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 106 | endJumpList.append(jit.jump()); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 107 | |
| 108 | if (!jit.supportsFloatingPoint()) { |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 109 | slowPathJumpList.append(notInt32); |
| 110 | return true; |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Try to do doubleVar + double(intConstant). |
| 114 | notInt32.link(&jit); |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 115 | if (!varOpr.definitelyIsNumber()) |
commit-queue@webkit.org | 10503ed | 2018-01-05 08:26:02 +0000 | [diff] [blame] | 116 | slowPathJumpList.append(jit.branchIfNotNumber(var, m_scratchGPR)); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 117 | |
mark.lam@apple.com | 102cbf2 | 2015-11-17 21:55:33 +0000 | [diff] [blame] | 118 | jit.unboxDoubleNonDestructive(var, m_leftFPR, m_scratchGPR, m_scratchFPR); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 119 | |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 120 | jit.move(CCallHelpers::Imm32(constOpr.asConstInt32()), m_scratchGPR); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 121 | jit.convertInt32ToDouble(m_scratchGPR, m_rightFPR); |
| 122 | |
| 123 | // Fall thru to doubleVar + doubleVar. |
| 124 | |
| 125 | } else { |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 126 | ASSERT(!m_leftOperand.isConstInt32() && !m_rightOperand.isConstInt32()); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 127 | CCallHelpers::Jump leftNotInt; |
| 128 | CCallHelpers::Jump rightNotInt; |
| 129 | |
| 130 | // Try to do intVar + intVar. |
| 131 | leftNotInt = jit.branchIfNotInt32(m_left); |
| 132 | rightNotInt = jit.branchIfNotInt32(m_right); |
| 133 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 134 | GPRReg scratch = m_scratchGPR; |
| 135 | if (m_left.payloadGPR() != m_result.payloadGPR() && m_right.payloadGPR() != m_result.payloadGPR()) |
| 136 | scratch = m_result.payloadGPR(); |
| 137 | slowPathJumpList.append(jit.branchAdd32(CCallHelpers::Overflow, m_right.payloadGPR(), m_left.payloadGPR(), scratch)); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 138 | |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 139 | jit.boxInt32(scratch, m_result); |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 140 | endJumpList.append(jit.jump()); |
| 141 | |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 142 | |
| 143 | if (!jit.supportsFloatingPoint()) { |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 144 | slowPathJumpList.append(leftNotInt); |
| 145 | slowPathJumpList.append(rightNotInt); |
| 146 | return true; |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | leftNotInt.link(&jit); |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 150 | if (!m_leftOperand.definitelyIsNumber()) |
commit-queue@webkit.org | 10503ed | 2018-01-05 08:26:02 +0000 | [diff] [blame] | 151 | slowPathJumpList.append(jit.branchIfNotNumber(m_left, m_scratchGPR)); |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 152 | if (!m_rightOperand.definitelyIsNumber()) |
commit-queue@webkit.org | 10503ed | 2018-01-05 08:26:02 +0000 | [diff] [blame] | 153 | slowPathJumpList.append(jit.branchIfNotNumber(m_right, m_scratchGPR)); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 154 | |
| 155 | jit.unboxDoubleNonDestructive(m_left, m_leftFPR, m_scratchGPR, m_scratchFPR); |
| 156 | CCallHelpers::Jump rightIsDouble = jit.branchIfNotInt32(m_right); |
| 157 | |
| 158 | jit.convertInt32ToDouble(m_right.payloadGPR(), m_rightFPR); |
| 159 | CCallHelpers::Jump rightWasInteger = jit.jump(); |
| 160 | |
| 161 | rightNotInt.link(&jit); |
mark.lam@apple.com | 92730cc | 2015-12-01 00:07:39 +0000 | [diff] [blame] | 162 | if (!m_rightOperand.definitelyIsNumber()) |
commit-queue@webkit.org | 10503ed | 2018-01-05 08:26:02 +0000 | [diff] [blame] | 163 | slowPathJumpList.append(jit.branchIfNotNumber(m_right, m_scratchGPR)); |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 164 | |
| 165 | jit.convertInt32ToDouble(m_left.payloadGPR(), m_leftFPR); |
| 166 | |
| 167 | rightIsDouble.link(&jit); |
| 168 | jit.unboxDoubleNonDestructive(m_right, m_rightFPR, m_scratchGPR, m_scratchFPR); |
| 169 | |
| 170 | rightWasInteger.link(&jit); |
| 171 | |
| 172 | // Fall thru to doubleVar + doubleVar. |
| 173 | } |
| 174 | |
| 175 | // Do doubleVar + doubleVar. |
| 176 | jit.addDouble(m_rightFPR, m_leftFPR); |
benjamin@webkit.org | 626f77e | 2016-09-26 21:11:31 +0000 | [diff] [blame] | 177 | if (arithProfile && shouldEmitProfiling) |
| 178 | arithProfile->emitSetDouble(jit); |
| 179 | |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 180 | jit.boxDouble(m_leftFPR, m_result); |
sbarati@apple.com | b0b1eb5 | 2016-07-21 23:41:44 +0000 | [diff] [blame] | 181 | |
| 182 | return true; |
mark.lam@apple.com | cb5d6e1 | 2015-11-02 21:10:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace JSC |
| 186 | |
| 187 | #endif // ENABLE(JIT) |