blob: 76938c6fecabfd271277abee39fea93a92f7b660 [file] [log] [blame]
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +00001/*
fpizlo@apple.com47d0cf72018-01-25 19:32:00 +00002 * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +00003 *
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.com22104f52016-09-28 17:08:17 +000026#pragma once
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000027
28#if ENABLE(B3_JIT)
29
fpizlo@apple.comd9249032015-11-02 22:56:09 +000030#include "FPRInfo.h"
31#include "GPRInfo.h"
fpizlo@apple.com9ed1b632015-12-07 19:17:56 +000032#include "JSCJSValue.h"
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000033#include "Reg.h"
fpizlo@apple.com45e45652016-01-07 21:20:37 +000034#include "RegisterSet.h"
fpizlo@apple.coma42de462015-12-22 20:03:13 +000035#include "ValueRecovery.h"
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000036#include <wtf/PrintStream.h>
commit-queue@webkit.org79d3b0f2021-01-14 22:48:43 +000037#if ENABLE(WEBASSEMBLY)
38#include "WasmValueLocation.h"
39#endif
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000040
fpizlo@apple.com203ebdb2015-12-21 18:56:54 +000041namespace JSC {
42
43class AssemblyHelpers;
44
45namespace B3 {
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000046
47// We use this class to describe value representations at stackmaps. It's used both to force a
48// representation and to get the representation. When the B3 client forces a representation, we say
49// that it's an input. When B3 tells the client what representation it picked, we say that it's an
50// output.
51
52class ValueRep {
ysuzuki@apple.comb4f2f732019-08-20 00:21:29 +000053 WTF_MAKE_FAST_ALLOCATED;
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000054public:
ysuzuki@apple.comb4f2f732019-08-20 00:21:29 +000055 enum Kind : uint8_t {
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +000056 // As an input representation, this means that B3 can pick any representation. As an output
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000057 // representation, this means that we don't know. This will only arise as an output
58 // representation for the active arguments of Check/CheckAdd/CheckSub/CheckMul.
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +000059 WarmAny,
60
61 // Same as WarmAny, but implies that the use is cold. A cold use is not counted as a use for
62 // computing the priority of the used temporary.
63 ColdAny,
64
65 // Same as ColdAny, but also implies that the use occurs after all other effects of the stackmap
66 // value.
67 LateColdAny,
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000068
69 // As an input representation, this means that B3 should pick some register. It could be a
70 // register that this claims to clobber!
71 SomeRegister,
fpizlo@apple.com47d0cf72018-01-25 19:32:00 +000072
73 // As an input representation, this means that B3 should pick some register but that this
74 // register is then cobbered with garbage. This only works for patchpoints.
75 SomeRegisterWithClobber,
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000076
fpizlo@apple.com8cf609a2016-07-21 05:24:54 +000077 // As an input representation, this tells us that B3 should pick some register, but implies
78 // that the def happens before any of the effects of the stackmap. This is only valid for
79 // the result constraint of a Patchpoint.
80 SomeEarlyRegister,
commit-queue@webkit.orgf79caa42019-06-12 18:40:56 +000081
82 // As an input representation, this tells us that B3 should pick some register, but implies
83 // the use happens after any defs. This is only works for patchpoints.
84 SomeLateRegister,
85
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000086 // As an input representation, this forces a particular register. As an output
87 // representation, this tells us what register B3 picked.
88 Register,
89
sbarati@apple.com15af88b2016-05-11 20:54:09 +000090 // As an input representation, this forces a particular register and states that
91 // the register is used late. This means that the register is used after the result
92 // is defined (i.e, the result will interfere with this as an input).
sbarati@apple.come20444a2016-11-30 06:24:44 +000093 // It's not a valid output representation.
sbarati@apple.com15af88b2016-05-11 20:54:09 +000094 LateRegister,
95
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000096 // As an output representation, this tells us what stack slot B3 picked. It's not a valid
97 // input representation.
98 Stack,
99
100 // As an input representation, this forces the value to end up in the argument area at some
keith_miller@apple.com70c51b82019-08-02 21:02:05 +0000101 // offset. As an output representation this tells us what offset from SP B3 picked.
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000102 StackArgument,
103
104 // As an output representation, this tells us that B3 constant-folded the value.
105 Constant
106 };
107
108 ValueRep()
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +0000109 : m_kind(WarmAny)
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000110 {
111 }
112
113 explicit ValueRep(Reg reg)
114 : m_kind(Register)
115 {
116 u.reg = reg;
117 }
118
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000119 ValueRep(Kind kind)
120 : m_kind(kind)
121 {
commit-queue@webkit.orgf79caa42019-06-12 18:40:56 +0000122 ASSERT(kind == WarmAny || kind == ColdAny || kind == LateColdAny || kind == SomeRegister || kind == SomeRegisterWithClobber || kind == SomeEarlyRegister || kind == SomeLateRegister);
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000123 }
124
commit-queue@webkit.org79d3b0f2021-01-14 22:48:43 +0000125#if ENABLE(WEBASSEMBLY)
126 ValueRep(Wasm::ValueLocation location)
127 {
128 switch (location.kind()) {
commit-queue@webkit.orgdcadf9d2022-05-27 08:47:21 +0000129 case Wasm::ValueLocation::Kind::GPRRegister:
commit-queue@webkit.org79d3b0f2021-01-14 22:48:43 +0000130 m_kind = Register;
commit-queue@webkit.orgdcadf9d2022-05-27 08:47:21 +0000131 u.reg = location.jsr().payloadGPR();
132 break;
133 case Wasm::ValueLocation::Kind::FPRRegister:
134 m_kind = Register;
135 u.reg = location.fpr();
commit-queue@webkit.org79d3b0f2021-01-14 22:48:43 +0000136 break;
137 case Wasm::ValueLocation::Kind::Stack:
138 m_kind = Stack;
139 u.offsetFromFP = location.offsetFromFP();
140 break;
141 case Wasm::ValueLocation::Kind::StackArgument:
142 m_kind = StackArgument;
143 u.offsetFromSP = location.offsetFromSP();
144 break;
145 default:
146 ASSERT_NOT_REACHED();
147 }
148 }
149#endif
150
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000151 static ValueRep reg(Reg reg)
152 {
153 return ValueRep(reg);
154 }
155
sbarati@apple.com15af88b2016-05-11 20:54:09 +0000156 static ValueRep lateReg(Reg reg)
157 {
158 ValueRep result(reg);
159 result.m_kind = LateRegister;
160 return result;
161 }
162
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000163 static ValueRep stack(intptr_t offsetFromFP)
164 {
165 ValueRep result;
166 result.m_kind = Stack;
167 result.u.offsetFromFP = offsetFromFP;
168 return result;
169 }
170
171 static ValueRep stackArgument(intptr_t offsetFromSP)
172 {
173 ValueRep result;
174 result.m_kind = StackArgument;
175 result.u.offsetFromSP = offsetFromSP;
176 return result;
177 }
178
179 static ValueRep constant(int64_t value)
180 {
181 ValueRep result;
182 result.m_kind = Constant;
183 result.u.value = value;
184 return result;
185 }
186
187 static ValueRep constantDouble(double value)
188 {
189 return ValueRep::constant(bitwise_cast<int64_t>(value));
190 }
191
ysuzuki@apple.comb1c742b2019-08-15 06:39:28 +0000192 static ValueRep constantFloat(float value)
193 {
194 return ValueRep::constant(static_cast<uint64_t>(bitwise_cast<uint32_t>(value)));
195 }
196
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000197 Kind kind() const { return m_kind; }
198
fpizlo@apple.com998f4b42015-11-04 00:13:27 +0000199 bool operator==(const ValueRep& other) const
200 {
201 if (kind() != other.kind())
202 return false;
203 switch (kind()) {
sbarati@apple.com15af88b2016-05-11 20:54:09 +0000204 case LateRegister:
fpizlo@apple.com998f4b42015-11-04 00:13:27 +0000205 case Register:
206 return u.reg == other.u.reg;
207 case Stack:
208 return u.offsetFromFP == other.u.offsetFromFP;
209 case StackArgument:
210 return u.offsetFromSP == other.u.offsetFromSP;
211 case Constant:
212 return u.value == other.u.value;
213 default:
214 return true;
215 }
216 }
217
218 bool operator!=(const ValueRep& other) const
219 {
220 return !(*this == other);
221 }
222
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +0000223 explicit operator bool() const { return kind() != WarmAny; }
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000224
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +0000225 bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; }
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000226
commit-queue@webkit.orgf79caa42019-06-12 18:40:56 +0000227 bool isReg() const { return kind() == Register || kind() == LateRegister || kind() == SomeLateRegister; }
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000228
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000229 Reg reg() const
230 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000231 ASSERT(isReg());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000232 return u.reg;
233 }
234
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000235 bool isGPR() const { return isReg() && reg().isGPR(); }
236 bool isFPR() const { return isReg() && reg().isFPR(); }
237
238 GPRReg gpr() const { return reg().gpr(); }
239 FPRReg fpr() const { return reg().fpr(); }
240
241 bool isStack() const { return kind() == Stack; }
242
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000243 intptr_t offsetFromFP() const
244 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000245 ASSERT(isStack());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000246 return u.offsetFromFP;
247 }
248
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000249 bool isStackArgument() const { return kind() == StackArgument; }
250
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000251 intptr_t offsetFromSP() const
252 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000253 ASSERT(isStackArgument());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000254 return u.offsetFromSP;
255 }
256
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000257 bool isConstant() const { return kind() == Constant; }
258
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000259 int64_t value() const
260 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000261 ASSERT(isConstant());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000262 return u.value;
263 }
264
265 double doubleValue() const
266 {
267 return bitwise_cast<double>(value());
268 }
269
ysuzuki@apple.comb1c742b2019-08-15 06:39:28 +0000270 float floatValue() const
271 {
272 return bitwise_cast<float>(static_cast<uint32_t>(static_cast<uint64_t>(value())));
273 }
274
fpizlo@apple.com45e45652016-01-07 21:20:37 +0000275 ValueRep withOffset(intptr_t offset) const
fpizlo@apple.com9ed1b632015-12-07 19:17:56 +0000276 {
277 switch (kind()) {
278 case Stack:
279 return stack(offsetFromFP() + offset);
280 case StackArgument:
281 return stackArgument(offsetFromSP() + offset);
282 default:
283 return *this;
284 }
285 }
286
fpizlo@apple.com45e45652016-01-07 21:20:37 +0000287 void addUsedRegistersTo(RegisterSet&) const;
288
289 RegisterSet usedRegisters() const;
290
291 // Get the used registers for a vector of ValueReps.
292 template<typename VectorType>
293 static RegisterSet usedRegisters(const VectorType& vector)
294 {
295 RegisterSet result;
296 for (const ValueRep& value : vector)
297 value.addUsedRegistersTo(result);
298 return result;
299 }
300
fpizlo@apple.com5b3a9942015-12-04 00:11:32 +0000301 JS_EXPORT_PRIVATE void dump(PrintStream&) const;
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000302
fpizlo@apple.com203ebdb2015-12-21 18:56:54 +0000303 // This has a simple contract: it emits code to restore the value into the given register. This
304 // will work even if it requires moving between bits a GPR and a FPR.
fpizlo@apple.coma42de462015-12-22 20:03:13 +0000305 void emitRestore(AssemblyHelpers&, Reg) const;
306
307 // Computes the ValueRecovery assuming that the Value* was for a JSValue (i.e. Int64).
308 // NOTE: We should avoid putting JSValue-related methods in B3, but this was hard to avoid
309 // because some parts of JSC use ValueRecovery like a general "where my bits at" object, almost
310 // exactly like ValueRep.
311 ValueRecovery recoveryForJSValue() const;
fpizlo@apple.com203ebdb2015-12-21 18:56:54 +0000312
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000313private:
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000314 union U {
315 Reg reg;
316 intptr_t offsetFromFP;
317 intptr_t offsetFromSP;
318 int64_t value;
319
320 U()
321 {
mcatanzaro@igalia.coma7ade272019-03-19 20:04:34 +0000322 memset(static_cast<void*>(this), 0, sizeof(*this));
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000323 }
324 } u;
ysuzuki@apple.comb4f2f732019-08-20 00:21:29 +0000325 Kind m_kind;
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000326};
327
328} } // namespace JSC::B3
329
330namespace WTF {
331
332void printInternal(PrintStream&, JSC::B3::ValueRep::Kind);
333
334} // namespace WTF
335
336#endif // ENABLE(B3_JIT)