blob: ced604ecd126630529ad7c967a590b8bc1597f4c [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>
37
fpizlo@apple.com203ebdb2015-12-21 18:56:54 +000038namespace JSC {
39
40class AssemblyHelpers;
41
42namespace B3 {
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000043
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
49class ValueRep {
50public:
51 enum Kind {
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +000052 // As an input representation, this means that B3 can pick any representation. As an output
fpizlo@apple.comd1d46742015-12-01 07:03:55 +000053 // 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.com2b04a4f2015-12-04 03:29:39 +000055 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.coma00b4d82015-10-28 23:57:04 +000064
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.com47d0cf72018-01-25 19:32:00 +000068
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.coma00b4d82015-10-28 23:57:04 +000072
fpizlo@apple.com8cf609a2016-07-21 05:24:54 +000073 // 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.com42624fe2017-03-10 17:49:42 +000077
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000078 // 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.com15af88b2016-05-11 20:54:09 +000082 // 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.come20444a2016-11-30 06:24:44 +000085 // It's not a valid output representation.
sbarati@apple.com15af88b2016-05-11 20:54:09 +000086 LateRegister,
87
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +000088 // 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.coma00b4d82015-10-28 23:57:04 +000094 StackArgument,
95
96 // As an output representation, this tells us that B3 constant-folded the value.
97 Constant
98 };
99
100 ValueRep()
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +0000101 : m_kind(WarmAny)
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000102 {
103 }
104
105 explicit ValueRep(Reg reg)
106 : m_kind(Register)
107 {
108 u.reg = reg;
109 }
110
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000111 ValueRep(Kind kind)
112 : m_kind(kind)
113 {
fpizlo@apple.com47d0cf72018-01-25 19:32:00 +0000114 ASSERT(kind == WarmAny || kind == ColdAny || kind == LateColdAny || kind == SomeRegister || kind == SomeRegisterWithClobber || kind == SomeEarlyRegister);
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000115 }
116
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000117 static ValueRep reg(Reg reg)
118 {
119 return ValueRep(reg);
120 }
121
sbarati@apple.com15af88b2016-05-11 20:54:09 +0000122 static ValueRep lateReg(Reg reg)
123 {
124 ValueRep result(reg);
125 result.m_kind = LateRegister;
126 return result;
127 }
128
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000129 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.com998f4b42015-11-04 00:13:27 +0000160 bool operator==(const ValueRep& other) const
161 {
162 if (kind() != other.kind())
163 return false;
164 switch (kind()) {
sbarati@apple.com15af88b2016-05-11 20:54:09 +0000165 case LateRegister:
fpizlo@apple.com998f4b42015-11-04 00:13:27 +0000166 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.com2b04a4f2015-12-04 03:29:39 +0000184 explicit operator bool() const { return kind() != WarmAny; }
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000185
fpizlo@apple.com2b04a4f2015-12-04 03:29:39 +0000186 bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; }
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000187
sbarati@apple.com15af88b2016-05-11 20:54:09 +0000188 bool isReg() const { return kind() == Register || kind() == LateRegister; }
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000189
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000190 Reg reg() const
191 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000192 ASSERT(isReg());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000193 return u.reg;
194 }
195
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000196 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.coma00b4d82015-10-28 23:57:04 +0000204 intptr_t offsetFromFP() const
205 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000206 ASSERT(isStack());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000207 return u.offsetFromFP;
208 }
209
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000210 bool isStackArgument() const { return kind() == StackArgument; }
211
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000212 intptr_t offsetFromSP() const
213 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000214 ASSERT(isStackArgument());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000215 return u.offsetFromSP;
216 }
217
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000218 bool isConstant() const { return kind() == Constant; }
219
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000220 int64_t value() const
221 {
fpizlo@apple.comd9249032015-11-02 22:56:09 +0000222 ASSERT(isConstant());
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000223 return u.value;
224 }
225
226 double doubleValue() const
227 {
228 return bitwise_cast<double>(value());
229 }
230
fpizlo@apple.com45e45652016-01-07 21:20:37 +0000231 ValueRep withOffset(intptr_t offset) const
fpizlo@apple.com9ed1b632015-12-07 19:17:56 +0000232 {
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.com45e45652016-01-07 21:20:37 +0000243 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.com5b3a9942015-12-04 00:11:32 +0000257 JS_EXPORT_PRIVATE void dump(PrintStream&) const;
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000258
fpizlo@apple.com203ebdb2015-12-21 18:56:54 +0000259 // 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.coma42de462015-12-22 20:03:13 +0000261 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.com203ebdb2015-12-21 18:56:54 +0000268
fpizlo@apple.coma00b4d82015-10-28 23:57:04 +0000269private:
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
286namespace WTF {
287
288void printInternal(PrintStream&, JSC::B3::ValueRep::Kind);
289
290} // namespace WTF
291
292#endif // ENABLE(B3_JIT)