1/*
2 * Copyright (C) 2015-2018 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#if ENABLE(B3_JIT)
29
30#include "FPRInfo.h"
31#include "GPRInfo.h"
32#include "JSCJSValue.h"
33#include "Reg.h"
34#include "RegisterSet.h"
35#include "ValueRecovery.h"
36#include <wtf/PrintStream.h>
37
38namespace JSC {
39
40class AssemblyHelpers;
41
42namespace B3 {
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
49class ValueRep {
50public:
51 enum Kind {
52 // As an input representation, this means that B3 can pick any representation. As an output
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.
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,
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,
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,
72
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,
77
78 // As an input representation, this tells us that B3 should pick some register, but implies
79 // the use happens after any defs. This is only works for patchpoints.
80 SomeLateRegister,
81
82 // As an input representation, this forces a particular register. As an output
83 // representation, this tells us what register B3 picked.
84 Register,
85
86 // As an input representation, this forces a particular register and states that
87 // the register is used late. This means that the register is used after the result
88 // is defined (i.e, the result will interfere with this as an input).
89 // It's not a valid output representation.
90 LateRegister,
91
92 // As an output representation, this tells us what stack slot B3 picked. It's not a valid
93 // input representation.
94 Stack,
95
96 // As an input representation, this forces the value to end up in the argument area at some
97 // offset.
98 StackArgument,
99
100 // As an output representation, this tells us that B3 constant-folded the value.
101 Constant
102 };
103
104 ValueRep()
105 : m_kind(WarmAny)
106 {
107 }
108
109 explicit ValueRep(Reg reg)
110 : m_kind(Register)
111 {
112 u.reg = reg;
113 }
114
115 ValueRep(Kind kind)
116 : m_kind(kind)
117 {
118 ASSERT(kind == WarmAny || kind == ColdAny || kind == LateColdAny || kind == SomeRegister || kind == SomeRegisterWithClobber || kind == SomeEarlyRegister || kind == SomeLateRegister);
119 }
120
121 static ValueRep reg(Reg reg)
122 {
123 return ValueRep(reg);
124 }
125
126 static ValueRep lateReg(Reg reg)
127 {
128 ValueRep result(reg);
129 result.m_kind = LateRegister;
130 return result;
131 }
132
133 static ValueRep stack(intptr_t offsetFromFP)
134 {
135 ValueRep result;
136 result.m_kind = Stack;
137 result.u.offsetFromFP = offsetFromFP;
138 return result;
139 }
140
141 static ValueRep stackArgument(intptr_t offsetFromSP)
142 {
143 ValueRep result;
144 result.m_kind = StackArgument;
145 result.u.offsetFromSP = offsetFromSP;
146 return result;
147 }
148
149 static ValueRep constant(int64_t value)
150 {
151 ValueRep result;
152 result.m_kind = Constant;
153 result.u.value = value;
154 return result;
155 }
156
157 static ValueRep constantDouble(double value)
158 {
159 return ValueRep::constant(bitwise_cast<int64_t>(value));
160 }
161
162 Kind kind() const { return m_kind; }
163
164 bool operator==(const ValueRep& other) const
165 {
166 if (kind() != other.kind())
167 return false;
168 switch (kind()) {
169 case LateRegister:
170 case Register:
171 return u.reg == other.u.reg;
172 case Stack:
173 return u.offsetFromFP == other.u.offsetFromFP;
174 case StackArgument:
175 return u.offsetFromSP == other.u.offsetFromSP;
176 case Constant:
177 return u.value == other.u.value;
178 default:
179 return true;
180 }
181 }
182
183 bool operator!=(const ValueRep& other) const
184 {
185 return !(*this == other);
186 }
187
188 explicit operator bool() const { return kind() != WarmAny; }
189
190 bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; }
191
192 bool isReg() const { return kind() == Register || kind() == LateRegister || kind() == SomeLateRegister; }
193
194 Reg reg() const
195 {
196 ASSERT(isReg());
197 return u.reg;
198 }
199
200 bool isGPR() const { return isReg() && reg().isGPR(); }
201 bool isFPR() const { return isReg() && reg().isFPR(); }
202
203 GPRReg gpr() const { return reg().gpr(); }
204 FPRReg fpr() const { return reg().fpr(); }
205
206 bool isStack() const { return kind() == Stack; }
207
208 intptr_t offsetFromFP() const
209 {
210 ASSERT(isStack());
211 return u.offsetFromFP;
212 }
213
214 bool isStackArgument() const { return kind() == StackArgument; }
215
216 intptr_t offsetFromSP() const
217 {
218 ASSERT(isStackArgument());
219 return u.offsetFromSP;
220 }
221
222 bool isConstant() const { return kind() == Constant; }
223
224 int64_t value() const
225 {
226 ASSERT(isConstant());
227 return u.value;
228 }
229
230 double doubleValue() const
231 {
232 return bitwise_cast<double>(value());
233 }
234
235 ValueRep withOffset(intptr_t offset) const
236 {
237 switch (kind()) {
238 case Stack:
239 return stack(offsetFromFP() + offset);
240 case StackArgument:
241 return stackArgument(offsetFromSP() + offset);
242 default:
243 return *this;
244 }
245 }
246
247 void addUsedRegistersTo(RegisterSet&) const;
248
249 RegisterSet usedRegisters() const;
250
251 // Get the used registers for a vector of ValueReps.
252 template<typename VectorType>
253 static RegisterSet usedRegisters(const VectorType& vector)
254 {
255 RegisterSet result;
256 for (const ValueRep& value : vector)
257 value.addUsedRegistersTo(result);
258 return result;
259 }
260
261 JS_EXPORT_PRIVATE void dump(PrintStream&) const;
262
263 // This has a simple contract: it emits code to restore the value into the given register. This
264 // will work even if it requires moving between bits a GPR and a FPR.
265 void emitRestore(AssemblyHelpers&, Reg) const;
266
267 // Computes the ValueRecovery assuming that the Value* was for a JSValue (i.e. Int64).
268 // NOTE: We should avoid putting JSValue-related methods in B3, but this was hard to avoid
269 // because some parts of JSC use ValueRecovery like a general "where my bits at" object, almost
270 // exactly like ValueRep.
271 ValueRecovery recoveryForJSValue() const;
272
273private:
274 Kind m_kind;
275 union U {
276 Reg reg;
277 intptr_t offsetFromFP;
278 intptr_t offsetFromSP;
279 int64_t value;
280
281 U()
282 {
283 memset(static_cast<void*>(this), 0, sizeof(*this));
284 }
285 } u;
286};
287
288} } // namespace JSC::B3
289
290namespace WTF {
291
292void printInternal(PrintStream&, JSC::B3::ValueRep::Kind);
293
294} // namespace WTF
295
296#endif // ENABLE(B3_JIT)
297