1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* Copyright (C) 2015-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(B3_JIT) || ENABLE(WEBASSEMBLY_BBQJIT)
#include "FPRInfo.h"
#include "GPRInfo.h"
#include "JSCJSValue.h"
#include "Reg.h"
#include "RegisterSet.h"
#include "ValueRecovery.h"
#include <wtf/PrintStream.h>
#include <wtf/TZoneMalloc.h>
#if ENABLE(WEBASSEMBLY)
#include "WasmValueLocation.h"
#endif
namespace JSC {
class AssemblyHelpers;
namespace B3 {
// We use this class to describe value representations at stackmaps. It's used both to force a
// representation and to get the representation. When the B3 client forces a representation, we say
// that it's an input. When B3 tells the client what representation it picked, we say that it's an
// output.
class ValueRep {
WTF_MAKE_TZONE_ALLOCATED(ValueRep);
public:
enum Kind : uint8_t {
// As an input representation, this means that B3 can pick any representation. As an output
// representation, this means that we don't know. This will only arise as an output
// representation for the active arguments of Check/CheckAdd/CheckSub/CheckMul.
WarmAny,
// Same as WarmAny, but implies that the use is cold. A cold use is not counted as a use for
// computing the priority of the used temporary.
ColdAny,
// Same as ColdAny, but also implies that the use occurs after all other effects of the stackmap
// value.
LateColdAny,
// As an input representation, this means that B3 should pick some register. It could be a
// register that this claims to clobber!
SomeRegister,
// As an input representation, this means that B3 should pick some register but that this
// register is then cobbered with garbage. This only works for patchpoints.
SomeRegisterWithClobber,
// As an input representation, this tells us that B3 should pick some register, but implies
// that the def happens before any of the effects of the stackmap. This is only valid for
// the result constraint of a Patchpoint.
SomeEarlyRegister,
// As an input representation, this tells us that B3 should pick some register, but implies
// the use happens after any defs. This is only works for patchpoints.
SomeLateRegister,
// As an input representation, this forces a particular register. As an output
// representation, this tells us what register B3 picked.
Register,
#if USE(JSVALUE32_64)
// This is only used for BBQ OSR on 32-bits.
// LLInt uses 64-bit stack values to represent I64s.
// BBQ uses register pairs.
// OMG treats I64 values as tuples, and tries to agressively de-structure them. It
// should never see this representation, except when tiering up from BBQ.
RegisterPair,
#endif
// As an input representation, this forces a particular register and states that
// the register is used late. This means that the register is used after the result
// is defined (i.e, the result will interfere with this as an input).
// It's not a valid output representation.
LateRegister,
// As an output representation, this tells us what stack slot B3 picked. It's not a valid
// input representation.
Stack,
// As an input representation, this forces the value to end up in the argument area at some
// offset. As an output representation this tells us what offset from SP B3 picked.
StackArgument,
// As an output representation, this tells us that B3 constant-folded the value.
Constant,
};
ValueRep()
: m_kind(WarmAny)
{
}
explicit ValueRep(Reg reg)
: m_kind(Register)
{
u.reg = reg;
}
ValueRep(Kind kind)
: m_kind(kind)
{
ASSERT(kind == WarmAny
|| kind == ColdAny
|| kind == LateColdAny
|| kind == SomeRegister
|| kind == SomeRegisterWithClobber
|| kind == SomeEarlyRegister
|| kind == SomeLateRegister
);
}
#if ENABLE(WEBASSEMBLY)
ValueRep(Wasm::ValueLocation location)
{
switch (location.kind()) {
case Wasm::ValueLocation::Kind::GPRRegister:
m_kind = Register;
u.reg = location.jsr().payloadGPR();
break;
case Wasm::ValueLocation::Kind::FPRRegister:
m_kind = Register;
u.reg = location.fpr();
break;
case Wasm::ValueLocation::Kind::Stack:
m_kind = Stack;
u.offsetFromFP = location.offsetFromFP();
break;
case Wasm::ValueLocation::Kind::StackArgument:
m_kind = StackArgument;
u.offsetFromSP = location.offsetFromSP();
break;
default:
ASSERT_NOT_REACHED();
}
}
#if USE(JSVALUE32_64)
// Only use this for OSR stackmaps.
enum OSRValueRepTag { OSRValueRep };
ValueRep(OSRValueRepTag, Reg lo, Reg hi)
{
m_kind = RegisterPair;
u.regPair.regLo = lo;
u.regPair.regHi = hi;
}
#endif // USE(JSVALUE32_64)
#endif // ENABLE(WEBASSEMBLY)
static ValueRep reg(Reg reg)
{
return ValueRep(reg);
}
static ValueRep lateReg(Reg reg)
{
ValueRep result(reg);
result.m_kind = LateRegister;
return result;
}
static ValueRep stack(intptr_t offsetFromFP)
{
ValueRep result;
result.m_kind = Stack;
result.u.offsetFromFP = offsetFromFP;
return result;
}
static ValueRep stackArgument(intptr_t offsetFromSP)
{
ValueRep result;
result.m_kind = StackArgument;
result.u.offsetFromSP = offsetFromSP;
return result;
}
static ValueRep constant(int64_t value)
{
ValueRep result;
result.m_kind = Constant;
result.u.value = value;
return result;
}
static ValueRep constantDouble(double value)
{
return ValueRep::constant(std::bit_cast<int64_t>(value));
}
static ValueRep constantFloat(float value)
{
return ValueRep::constant(static_cast<uint64_t>(std::bit_cast<uint32_t>(value)));
}
Kind kind() const { return m_kind; }
bool operator==(const ValueRep& other) const
{
if (kind() != other.kind())
return false;
switch (kind()) {
case LateRegister:
case Register:
return u.reg == other.u.reg;
case Stack:
return u.offsetFromFP == other.u.offsetFromFP;
case StackArgument:
return u.offsetFromSP == other.u.offsetFromSP;
case Constant:
return u.value == other.u.value;
default:
return true;
}
}
explicit operator bool() const { return kind() != WarmAny; }
bool isAny() const { return kind() == WarmAny || kind() == ColdAny || kind() == LateColdAny; }
bool isReg() const { return kind() == Register || kind() == LateRegister || kind() == SomeLateRegister; }
#if USE(JSVALUE32_64)
bool isRegPair(OSRValueRepTag) const { return kind() == RegisterPair; }
GPRReg gprLo(OSRValueRepTag) const
{
ASSERT(isRegPair(OSRValueRep));
return u.regPair.regLo.gpr();
}
GPRReg gprHi(OSRValueRepTag) const
{
ASSERT(isRegPair(OSRValueRep));
return u.regPair.regHi.gpr();
}
#endif
Reg reg() const
{
ASSERT(isReg());
return u.reg;
}
bool isGPR() const { return isReg() && reg().isGPR(); }
bool isFPR() const { return isReg() && reg().isFPR(); }
GPRReg gpr() const { return reg().gpr(); }
FPRReg fpr() const { return reg().fpr(); }
bool isStack() const { return kind() == Stack; }
intptr_t offsetFromFP() const
{
ASSERT(isStack());
return u.offsetFromFP;
}
bool isStackArgument() const { return kind() == StackArgument; }
intptr_t offsetFromSP() const
{
ASSERT(isStackArgument());
return u.offsetFromSP;
}
bool isConstant() const { return kind() == Constant; }
int64_t value() const
{
ASSERT(isConstant());
return u.value;
}
double doubleValue() const
{
return std::bit_cast<double>(value());
}
float floatValue() const
{
return std::bit_cast<float>(static_cast<uint32_t>(static_cast<uint64_t>(value())));
}
ValueRep withOffset(intptr_t offset) const
{
switch (kind()) {
case Stack:
return stack(offsetFromFP() + offset);
case StackArgument:
return stackArgument(offsetFromSP() + offset);
default:
return *this;
}
}
void addUsedRegistersTo(bool isSIMDContext, RegisterSetBuilder&) const;
RegisterSetBuilder usedRegisters(bool isSIMDContext) const;
// Get the used registers for a vector of ValueReps.
template<typename VectorType>
static RegisterSetBuilder usedRegisters(bool isSIMDContext, const VectorType& vector)
{
RegisterSetBuilder result;
for (const ValueRep& value : vector)
value.addUsedRegistersTo(isSIMDContext, result);
return result;
}
JS_EXPORT_PRIVATE void dump(PrintStream&) const;
// This has a simple contract: it emits code to restore the value into the given register. This
// will work even if it requires moving between bits a GPR and a FPR.
void emitRestore(AssemblyHelpers&, Reg) const;
// Computes the ValueRecovery assuming that the Value* was for a JSValue (i.e. Int64).
// NOTE: We should avoid putting JSValue-related methods in B3, but this was hard to avoid
// because some parts of JSC use ValueRecovery like a general "where my bits at" object, almost
// exactly like ValueRep.
ValueRecovery recoveryForJSValue() const;
private:
union U {
Reg reg;
intptr_t offsetFromFP;
intptr_t offsetFromSP;
int64_t value;
struct RegisterPair {
Reg regLo;
Reg regHi;
};
RegisterPair regPair;
U()
{
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
memset(static_cast<void*>(this), 0, sizeof(*this));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
}
} u;
Kind m_kind;
};
} } // namespace JSC::B3
namespace WTF {
void printInternal(PrintStream&, JSC::B3::ValueRep::Kind);
} // namespace WTF
#endif // ENABLE(B3_JIT)
|