fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Apple Inc. All rights reserved. |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +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 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 27 | |
| 28 | #if ENABLE(B3_JIT) |
| 29 | |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 30 | #include "FPRInfo.h" |
| 31 | #include "GPRInfo.h" |
fpizlo@apple.com | 9ed1b63 | 2015-12-07 19:17:56 +0000 | [diff] [blame] | 32 | #include "JSCJSValue.h" |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 33 | #include "Reg.h" |
fpizlo@apple.com | 45e4565 | 2016-01-07 21:20:37 +0000 | [diff] [blame] | 34 | #include "RegisterSet.h" |
fpizlo@apple.com | a42de46 | 2015-12-22 20:03:13 +0000 | [diff] [blame] | 35 | #include "ValueRecovery.h" |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 36 | #include <wtf/PrintStream.h> |
| 37 | |
fpizlo@apple.com | 203ebdb | 2015-12-21 18:56:54 +0000 | [diff] [blame] | 38 | namespace JSC { |
| 39 | |
| 40 | class AssemblyHelpers; |
| 41 | |
| 42 | namespace B3 { |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 43 | |
| 44 | // We use this class to describe value representations at stackmaps. It's used both to force a |
| 45 | // representation and to get the representation. When the B3 client forces a representation, we say |
| 46 | // that it's an input. When B3 tells the client what representation it picked, we say that it's an |
| 47 | // output. |
| 48 | |
| 49 | class ValueRep { |
| 50 | public: |
| 51 | enum Kind { |
fpizlo@apple.com | 2b04a4f | 2015-12-04 03:29:39 +0000 | [diff] [blame] | 52 | // As an input representation, this means that B3 can pick any representation. As an output |
fpizlo@apple.com | d1d4674 | 2015-12-01 07:03:55 +0000 | [diff] [blame] | 53 | // representation, this means that we don't know. This will only arise as an output |
| 54 | // representation for the active arguments of Check/CheckAdd/CheckSub/CheckMul. |
fpizlo@apple.com | 2b04a4f | 2015-12-04 03:29:39 +0000 | [diff] [blame] | 55 | WarmAny, |
| 56 | |
| 57 | // Same as WarmAny, but implies that the use is cold. A cold use is not counted as a use for |
| 58 | // computing the priority of the used temporary. |
| 59 | ColdAny, |
| 60 | |
| 61 | // Same as ColdAny, but also implies that the use occurs after all other effects of the stackmap |
| 62 | // value. |
| 63 | LateColdAny, |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 64 | |
| 65 | // As an input representation, this means that B3 should pick some register. It could be a |
| 66 | // register that this claims to clobber! |
| 67 | SomeRegister, |
fpizlo@apple.com | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 68 | |
| 69 | // As an input representation, this means that B3 should pick some register but that this |
| 70 | // register is then cobbered with garbage. This only works for patchpoints. |
| 71 | SomeRegisterWithClobber, |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 72 | |
fpizlo@apple.com | 8cf609a | 2016-07-21 05:24:54 +0000 | [diff] [blame] | 73 | // As an input representation, this tells us that B3 should pick some register, but implies |
| 74 | // that the def happens before any of the effects of the stackmap. This is only valid for |
| 75 | // the result constraint of a Patchpoint. |
| 76 | SomeEarlyRegister, |
fpizlo@apple.com | 42624fe | 2017-03-10 17:49:42 +0000 | [diff] [blame] | 77 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 78 | // As an input representation, this forces a particular register. As an output |
| 79 | // representation, this tells us what register B3 picked. |
| 80 | Register, |
| 81 | |
sbarati@apple.com | 15af88b | 2016-05-11 20:54:09 +0000 | [diff] [blame] | 82 | // As an input representation, this forces a particular register and states that |
| 83 | // the register is used late. This means that the register is used after the result |
| 84 | // is defined (i.e, the result will interfere with this as an input). |
sbarati@apple.com | e20444a | 2016-11-30 06:24:44 +0000 | [diff] [blame] | 85 | // It's not a valid output representation. |
sbarati@apple.com | 15af88b | 2016-05-11 20:54:09 +0000 | [diff] [blame] | 86 | LateRegister, |
| 87 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 88 | // As an output representation, this tells us what stack slot B3 picked. It's not a valid |
| 89 | // input representation. |
| 90 | Stack, |
| 91 | |
| 92 | // As an input representation, this forces the value to end up in the argument area at some |
| 93 | // offset. |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 94 | StackArgument, |
| 95 | |
| 96 | // As an output representation, this tells us that B3 constant-folded the value. |
| 97 | Constant |
| 98 | }; |
| 99 | |
| 100 | ValueRep() |
fpizlo@apple.com | 2b04a4f | 2015-12-04 03:29:39 +0000 | [diff] [blame] | 101 | : m_kind(WarmAny) |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 102 | { |
| 103 | } |
| 104 | |
| 105 | explicit ValueRep(Reg reg) |
| 106 | : m_kind(Register) |
| 107 | { |
| 108 | u.reg = reg; |
| 109 | } |
| 110 | |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 111 | ValueRep(Kind kind) |
| 112 | : m_kind(kind) |
| 113 | { |
fpizlo@apple.com | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 114 | ASSERT(kind == WarmAny || kind == ColdAny || kind == LateColdAny || kind == SomeRegister || kind == SomeRegisterWithClobber || kind == SomeEarlyRegister); |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 115 | } |
| 116 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 117 | static ValueRep reg(Reg reg) |
| 118 | { |
| 119 | return ValueRep(reg); |
| 120 | } |
| 121 | |
sbarati@apple.com | 15af88b | 2016-05-11 20:54:09 +0000 | [diff] [blame] | 122 | static ValueRep lateReg(Reg reg) |
| 123 | { |
| 124 | ValueRep result(reg); |
| 125 | result.m_kind = LateRegister; |
| 126 | return result; |
| 127 | } |
| 128 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 129 | static ValueRep stack(intptr_t offsetFromFP) |
| 130 | { |
| 131 | ValueRep result; |
| 132 | result.m_kind = Stack; |
| 133 | result.u.offsetFromFP = offsetFromFP; |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | static ValueRep stackArgument(intptr_t offsetFromSP) |
| 138 | { |
| 139 | ValueRep result; |
| 140 | result.m_kind = StackArgument; |
| 141 | result.u.offsetFromSP = offsetFromSP; |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | static ValueRep constant(int64_t value) |
| 146 | { |
| 147 | ValueRep result; |
| 148 | result.m_kind = Constant; |
| 149 | result.u.value = value; |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | static ValueRep constantDouble(double value) |
| 154 | { |
| 155 | return ValueRep::constant(bitwise_cast<int64_t>(value)); |
| 156 | } |
| 157 | |
| 158 | Kind kind() const { return m_kind; } |
| 159 | |
fpizlo@apple.com | 998f4b4 | 2015-11-04 00:13:27 +0000 | [diff] [blame] | 160 | bool operator==(const ValueRep& other) const |
| 161 | { |
| 162 | if (kind() != other.kind()) |
| 163 | return false; |
| 164 | switch (kind()) { |
sbarati@apple.com | 15af88b | 2016-05-11 20:54:09 +0000 | [diff] [blame] | 165 | case LateRegister: |
fpizlo@apple.com | 998f4b4 | 2015-11-04 00:13:27 +0000 | [diff] [blame] | 166 | case Register: |
| 167 | return u.reg == other.u.reg; |
| 168 | case Stack: |
| 169 | return u.offsetFromFP == other.u.offsetFromFP; |
| 170 | case StackArgument: |
| 171 | return u.offsetFromSP == other.u.offsetFromSP; |
| 172 | case Constant: |
| 173 | return u.value == other.u.value; |
| 174 | default: |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool operator!=(const ValueRep& other) const |
| 180 | { |
| 181 | return !(*this == other); |
| 182 | } |
| 183 | |
fpizlo@apple.com | 2b04a4f | 2015-12-04 03:29:39 +0000 | [diff] [blame] | 184 | explicit operator bool() const { return kind() != WarmAny; } |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 185 | |
fpizlo@apple.com | 2b04a4f | 2015-12-04 03:29:39 +0000 | [diff] [blame] | 186 | bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; } |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 187 | |
sbarati@apple.com | 15af88b | 2016-05-11 20:54:09 +0000 | [diff] [blame] | 188 | bool isReg() const { return kind() == Register || kind() == LateRegister; } |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 189 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 190 | Reg reg() const |
| 191 | { |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 192 | ASSERT(isReg()); |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 193 | return u.reg; |
| 194 | } |
| 195 | |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 196 | bool isGPR() const { return isReg() && reg().isGPR(); } |
| 197 | bool isFPR() const { return isReg() && reg().isFPR(); } |
| 198 | |
| 199 | GPRReg gpr() const { return reg().gpr(); } |
| 200 | FPRReg fpr() const { return reg().fpr(); } |
| 201 | |
| 202 | bool isStack() const { return kind() == Stack; } |
| 203 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 204 | intptr_t offsetFromFP() const |
| 205 | { |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 206 | ASSERT(isStack()); |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 207 | return u.offsetFromFP; |
| 208 | } |
| 209 | |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 210 | bool isStackArgument() const { return kind() == StackArgument; } |
| 211 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 212 | intptr_t offsetFromSP() const |
| 213 | { |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 214 | ASSERT(isStackArgument()); |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 215 | return u.offsetFromSP; |
| 216 | } |
| 217 | |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 218 | bool isConstant() const { return kind() == Constant; } |
| 219 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 220 | int64_t value() const |
| 221 | { |
fpizlo@apple.com | d924903 | 2015-11-02 22:56:09 +0000 | [diff] [blame] | 222 | ASSERT(isConstant()); |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 223 | return u.value; |
| 224 | } |
| 225 | |
| 226 | double doubleValue() const |
| 227 | { |
| 228 | return bitwise_cast<double>(value()); |
| 229 | } |
| 230 | |
fpizlo@apple.com | 45e4565 | 2016-01-07 21:20:37 +0000 | [diff] [blame] | 231 | ValueRep withOffset(intptr_t offset) const |
fpizlo@apple.com | 9ed1b63 | 2015-12-07 19:17:56 +0000 | [diff] [blame] | 232 | { |
| 233 | switch (kind()) { |
| 234 | case Stack: |
| 235 | return stack(offsetFromFP() + offset); |
| 236 | case StackArgument: |
| 237 | return stackArgument(offsetFromSP() + offset); |
| 238 | default: |
| 239 | return *this; |
| 240 | } |
| 241 | } |
| 242 | |
fpizlo@apple.com | 45e4565 | 2016-01-07 21:20:37 +0000 | [diff] [blame] | 243 | void addUsedRegistersTo(RegisterSet&) const; |
| 244 | |
| 245 | RegisterSet usedRegisters() const; |
| 246 | |
| 247 | // Get the used registers for a vector of ValueReps. |
| 248 | template<typename VectorType> |
| 249 | static RegisterSet usedRegisters(const VectorType& vector) |
| 250 | { |
| 251 | RegisterSet result; |
| 252 | for (const ValueRep& value : vector) |
| 253 | value.addUsedRegistersTo(result); |
| 254 | return result; |
| 255 | } |
| 256 | |
fpizlo@apple.com | 5b3a994 | 2015-12-04 00:11:32 +0000 | [diff] [blame] | 257 | JS_EXPORT_PRIVATE void dump(PrintStream&) const; |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 258 | |
fpizlo@apple.com | 203ebdb | 2015-12-21 18:56:54 +0000 | [diff] [blame] | 259 | // This has a simple contract: it emits code to restore the value into the given register. This |
| 260 | // will work even if it requires moving between bits a GPR and a FPR. |
fpizlo@apple.com | a42de46 | 2015-12-22 20:03:13 +0000 | [diff] [blame] | 261 | void emitRestore(AssemblyHelpers&, Reg) const; |
| 262 | |
| 263 | // Computes the ValueRecovery assuming that the Value* was for a JSValue (i.e. Int64). |
| 264 | // NOTE: We should avoid putting JSValue-related methods in B3, but this was hard to avoid |
| 265 | // because some parts of JSC use ValueRecovery like a general "where my bits at" object, almost |
| 266 | // exactly like ValueRep. |
| 267 | ValueRecovery recoveryForJSValue() const; |
fpizlo@apple.com | 203ebdb | 2015-12-21 18:56:54 +0000 | [diff] [blame] | 268 | |
fpizlo@apple.com | a00b4d8 | 2015-10-28 23:57:04 +0000 | [diff] [blame] | 269 | private: |
| 270 | Kind m_kind; |
| 271 | union U { |
| 272 | Reg reg; |
| 273 | intptr_t offsetFromFP; |
| 274 | intptr_t offsetFromSP; |
| 275 | int64_t value; |
| 276 | |
| 277 | U() |
| 278 | { |
| 279 | memset(this, 0, sizeof(*this)); |
| 280 | } |
| 281 | } u; |
| 282 | }; |
| 283 | |
| 284 | } } // namespace JSC::B3 |
| 285 | |
| 286 | namespace WTF { |
| 287 | |
| 288 | void printInternal(PrintStream&, JSC::B3::ValueRep::Kind); |
| 289 | |
| 290 | } // namespace WTF |
| 291 | |
| 292 | #endif // ENABLE(B3_JIT) |