#pragma once
#if ENABLE(B3_JIT)
#include "B3Bank.h"
#include "B3Effects.h"
#include "B3FrequentedBlock.h"
#include "B3Kind.h"
#include "B3Origin.h"
#include "B3SparseCollection.h"
#include "B3Type.h"
#include "B3ValueKey.h"
#include "B3Width.h"
#include <wtf/CommaPrinter.h>
#include <wtf/FastMalloc.h>
#include <wtf/IteratorRange.h>
#include <wtf/StdLibExtras.h>
#include <wtf/TriState.h>
namespace JSC { namespace B3 {
class BasicBlock;
class CheckValue;
class InsertionSet;
class PhiChildren;
class Procedure;
class JS_EXPORT_PRIVATE Value {
WTF_MAKE_FAST_ALLOCATED;
public:
static const char* const dumpPrefix;
static bool accepts(Kind) { return true; }
virtual ~Value();
unsigned index() const { return m_index; }
Kind kind() const { return m_kind; }
Opcode opcode() const { return kind().opcode(); }
void setKindUnsafely(Kind kind) { m_kind = kind; }
void setOpcodeUnsafely(Opcode opcode) { m_kind.setOpcode(opcode); }
bool isChill() const { return kind().isChill(); }
bool traps() const { return kind().traps(); }
Origin origin() const { return m_origin; }
void setOrigin(Origin origin) { m_origin = origin; }
Type type() const { return m_type; }
void setType(Type type) { m_type = type; }
Bank resultBank() const { return bankForType(type()); }
Width resultWidth() const { return widthForType(type()); }
unsigned numChildren() const
{
if (m_numChildren == VarArgs)
return childrenVector().size();
return m_numChildren;
}
Value*& child(unsigned index)
{
ASSERT(index < numChildren());
return m_numChildren == VarArgs ? childrenVector()[index] : childrenArray()[index];
}
Value* child(unsigned index) const
{
ASSERT(index < numChildren());
return m_numChildren == VarArgs ? childrenVector()[index] : childrenArray()[index];
}
Value*& lastChild()
{
if (m_numChildren == VarArgs)
return childrenVector().last();
ASSERT(m_numChildren >= 1);
return childrenArray()[m_numChildren - 1];
}
Value* lastChild() const
{
if (m_numChildren == VarArgs)
return childrenVector().last();
ASSERT(m_numChildren >= 1);
return childrenArray()[m_numChildren - 1];
}
WTF::IteratorRange<Value**> children()
{
if (m_numChildren == VarArgs) {
Vector<Value*, 3>& vec = childrenVector();
return WTF::makeIteratorRange(&*vec.begin(), &*vec.end());
}
Value** buffer = childrenArray();
return {buffer, buffer + m_numChildren };
}
WTF::IteratorRange<Value* const*> children() const
{
if (m_numChildren == VarArgs) {
const Vector<Value*, 3>& vec = childrenVector();
return WTF::makeIteratorRange(&*vec.begin(), &*vec.end());
}
Value* const* buffer = childrenArray();
return {buffer, buffer + m_numChildren };
}
void replaceWithIdentity(Value*);
template<typename BottomProvider>
void replaceWithBottom(const BottomProvider&);
void replaceWithBottom(InsertionSet&, size_t index);
void replaceWithNop();
void replaceWithNopIgnoringType();
void replaceWithPhi();
void replaceWithJump(BasicBlock* owner, FrequentedBlock);
void replaceWithOops(BasicBlock* owner);
void replaceWithJump(FrequentedBlock);
void replaceWithOops();
void dump(PrintStream&) const;
void deepDump(const Procedure*, PrintStream&) const;
virtual void dumpSuccessors(const BasicBlock*, PrintStream&) const;
template<typename T>
T* as();
template<typename T>
const T* as() const;
bool isConstant() const;
bool isInteger() const;
virtual Value* negConstant(Procedure&) const;
virtual Value* addConstant(Procedure&, int32_t other) const;
virtual Value* addConstant(Procedure&, const Value* other) const;
virtual Value* subConstant(Procedure&, const Value* other) const;
virtual Value* mulConstant(Procedure&, const Value* other) const;
virtual Value* checkAddConstant(Procedure&, const Value* other) const;
virtual Value* checkSubConstant(Procedure&, const Value* other) const;
virtual Value* checkMulConstant(Procedure&, const Value* other) const;
virtual Value* checkNegConstant(Procedure&) const;
virtual Value* divConstant(Procedure&, const Value* other) const; virtual Value* uDivConstant(Procedure&, const Value* other) const;
virtual Value* modConstant(Procedure&, const Value* other) const; virtual Value* uModConstant(Procedure&, const Value* other) const;
virtual Value* bitAndConstant(Procedure&, const Value* other) const;
virtual Value* bitOrConstant(Procedure&, const Value* other) const;
virtual Value* bitXorConstant(Procedure&, const Value* other) const;
virtual Value* shlConstant(Procedure&, const Value* other) const;
virtual Value* sShrConstant(Procedure&, const Value* other) const;
virtual Value* zShrConstant(Procedure&, const Value* other) const;
virtual Value* rotRConstant(Procedure&, const Value* other) const;
virtual Value* rotLConstant(Procedure&, const Value* other) const;
virtual Value* bitwiseCastConstant(Procedure&) const;
virtual Value* iToDConstant(Procedure&) const;
virtual Value* iToFConstant(Procedure&) const;
virtual Value* doubleToFloatConstant(Procedure&) const;
virtual Value* floatToDoubleConstant(Procedure&) const;
virtual Value* absConstant(Procedure&) const;
virtual Value* ceilConstant(Procedure&) const;
virtual Value* floorConstant(Procedure&) const;
virtual Value* sqrtConstant(Procedure&) const;
virtual TriState equalConstant(const Value* other) const;
virtual TriState notEqualConstant(const Value* other) const;
virtual TriState lessThanConstant(const Value* other) const;
virtual TriState greaterThanConstant(const Value* other) const;
virtual TriState lessEqualConstant(const Value* other) const;
virtual TriState greaterEqualConstant(const Value* other) const;
virtual TriState aboveConstant(const Value* other) const;
virtual TriState belowConstant(const Value* other) const;
virtual TriState aboveEqualConstant(const Value* other) const;
virtual TriState belowEqualConstant(const Value* other) const;
virtual TriState equalOrUnorderedConstant(const Value* other) const;
Value* invertedCompare(Procedure&) const;
bool hasInt32() const;
int32_t asInt32() const;
bool isInt32(int32_t) const;
bool hasInt64() const;
int64_t asInt64() const;
bool isInt64(int64_t) const;
bool hasInt() const;
int64_t asInt() const;
bool isInt(int64_t value) const;
bool hasIntPtr() const;
intptr_t asIntPtr() const;
bool isIntPtr(intptr_t) const;
bool hasDouble() const;
double asDouble() const;
bool isEqualToDouble(double) const;
bool hasFloat() const;
float asFloat() const;
bool hasNumber() const;
template<typename T> bool isRepresentableAs() const;
template<typename T> T asNumber() const;
bool returnsBool() const;
bool isNegativeZero() const;
bool isRounded() const;
TriState asTriState() const;
bool isLikeZero() const { return asTriState() == TriState::False; }
bool isLikeNonZero() const { return asTriState() == TriState::True; }
Effects effects() const;
ValueKey key() const;
Value* foldIdentity() const;
bool performSubstitution();
bool isFree() const;
enum WalkStatus {
Continue,
IgnoreChildren,
Stop
};
template<typename Functor>
void walk(const Functor& functor, PhiChildren* = nullptr);
typedef int32_t OffsetType;
template<typename Int>
struct IsLegalOffset {
static constexpr bool value = std::is_integral<Int>::value
&& std::is_signed<Int>::value
&& sizeof(Int) <= sizeof(OffsetType);
};
protected:
Value* cloneImpl() const;
void replaceWith(Kind, Type, BasicBlock*);
void replaceWith(Kind, Type, BasicBlock*, Value*);
virtual void dumpChildren(CommaPrinter&, PrintStream&) const;
virtual void dumpMeta(CommaPrinter&, PrintStream&) const;
enum NumChildren : uint8_t { Zero = 0, One = 1, Two = 2, Three = 3, VarArgs = 4};
char* childrenAlloc() { return bitwise_cast<char*>(this) + adjacencyListOffset(); }
const char* childrenAlloc() const { return bitwise_cast<const char*>(this) + adjacencyListOffset(); }
Vector<Value*, 3>& childrenVector()
{
ASSERT(m_numChildren == VarArgs);
return *bitwise_cast<Vector<Value*, 3>*>(childrenAlloc());
}
const Vector<Value*, 3>& childrenVector() const
{
ASSERT(m_numChildren == VarArgs);
return *bitwise_cast<Vector<Value*, 3> const*>(childrenAlloc());
}
Value** childrenArray()
{
ASSERT(m_numChildren != VarArgs);
return bitwise_cast<Value**>(childrenAlloc());
}
Value* const* childrenArray() const
{
ASSERT(m_numChildren != VarArgs);
return bitwise_cast<Value* const*>(childrenAlloc());
}
template<typename... Arguments>
static Opcode opcodeFromConstructor(Kind kind, Arguments...) { return kind.opcode(); }
ALWAYS_INLINE static size_t adjacencyListSpace(Kind kind)
{
switch (kind.opcode()) {
case FramePointer:
case Nop:
case Phi:
case Jump:
case Oops:
case EntrySwitch:
case ArgumentReg:
case Const32:
case Const64:
case ConstFloat:
case ConstDouble:
case BottomTuple:
case Fence:
case SlotBase:
case Get:
return 0;
case Return:
case Identity:
case Opaque:
case Neg:
case Clz:
case Abs:
case Ceil:
case Floor:
case Sqrt:
case SExt8:
case SExt16:
case Trunc:
case SExt32:
case ZExt32:
case FloatToDouble:
case IToD:
case DoubleToFloat:
case IToF:
case BitwiseCast:
case Branch:
case Depend:
case Load8Z:
case Load8S:
case Load16Z:
case Load16S:
case Load:
case Switch:
case Upsilon:
case Extract:
case Set:
case WasmAddress:
case WasmBoundsCheck:
return sizeof(Value*);
case Add:
case Sub:
case Mul:
case Div:
case UDiv:
case Mod:
case UMod:
case BitAnd:
case BitOr:
case BitXor:
case Shl:
case SShr:
case ZShr:
case RotR:
case RotL:
case Equal:
case NotEqual:
case LessThan:
case GreaterThan:
case LessEqual:
case GreaterEqual:
case Above:
case Below:
case AboveEqual:
case BelowEqual:
case EqualOrUnordered:
case AtomicXchgAdd:
case AtomicXchgAnd:
case AtomicXchgOr:
case AtomicXchgSub:
case AtomicXchgXor:
case AtomicXchg:
case Store8:
case Store16:
case Store:
return 2 * sizeof(Value*);
case Select:
case AtomicWeakCAS:
case AtomicStrongCAS:
return 3 * sizeof(Value*);
case CCall:
case Check:
case CheckAdd:
case CheckSub:
case CheckMul:
case Patchpoint:
return sizeof(Vector<Value*, 3>);
#ifdef NDEBUG
default:
break;
#endif
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
}
private:
static char* allocateSpace(Opcode opcode, size_t size)
{
size_t adjacencyListSpace = Value::adjacencyListSpace(opcode);
size_t allocIdentitySize = sizeof(Value) + sizeof(Value*);
size_t allocSize = std::max(size + adjacencyListSpace, allocIdentitySize);
return static_cast<char*>(WTF::fastMalloc(allocSize));
}
protected:
template<typename ValueType, typename... Arguments>
static ValueType* allocate(Arguments... arguments)
{
char* alloc = allocateSpace(ValueType::opcodeFromConstructor(arguments...), sizeof(ValueType));
return new (alloc) ValueType(arguments...);
}
template<typename ValueType>
static ValueType* allocate(const ValueType& valueToClone)
{
char* alloc = allocateSpace(valueToClone.opcode(), sizeof(ValueType));
ValueType* result = new (alloc) ValueType(valueToClone);
result->buildAdjacencyList(sizeof(ValueType), valueToClone);
return result;
}
Value(const Value&) = default;
Value(Value&&) = delete;
Value& operator=(const Value&) = delete;
Value& operator=(Value&&) = delete;
size_t adjacencyListOffset() const;
friend class Procedure;
friend class SparseCollection<Value>;
private:
template<typename... Arguments>
void buildAdjacencyList(NumChildren numChildren, Arguments... arguments)
{
if (numChildren == VarArgs) {
new (childrenAlloc()) Vector<Value*, 3> { arguments... };
return;
}
ASSERT(numChildren == sizeof...(arguments));
new (childrenAlloc()) Value*[sizeof...(arguments)] { arguments... };
}
void buildAdjacencyList(size_t offset, const Value& valueToClone)
{
switch (valueToClone.m_numChildren) {
case VarArgs:
new (bitwise_cast<char*>(this) + offset) Vector<Value*, 3> (valueToClone.childrenVector());
break;
case Three:
bitwise_cast<Value**>(bitwise_cast<char*>(this) + offset)[2] = valueToClone.childrenArray()[2];
FALLTHROUGH;
case Two:
bitwise_cast<Value**>(bitwise_cast<char*>(this) + offset)[1] = valueToClone.childrenArray()[1];
FALLTHROUGH;
case One:
bitwise_cast<Value**>(bitwise_cast<char*>(this) + offset)[0] = valueToClone.childrenArray()[0];
break;
case Zero:
break;
}
}
ALWAYS_INLINE static NumChildren numChildrenForKind(Kind kind, unsigned numArgs)
{
switch (kind.opcode()) {
case FramePointer:
case Nop:
case Phi:
case Jump:
case Oops:
case EntrySwitch:
if (UNLIKELY(numArgs))
badKind(kind, numArgs);
return Zero;
case Return:
if (UNLIKELY(numArgs > 1))
badKind(kind, numArgs);
return numArgs ? One : Zero;
case Identity:
case Opaque:
case Neg:
case Clz:
case Abs:
case Ceil:
case Floor:
case Sqrt:
case SExt8:
case SExt16:
case Trunc:
case SExt32:
case ZExt32:
case FloatToDouble:
case IToD:
case DoubleToFloat:
case IToF:
case BitwiseCast:
case Branch:
case Depend:
if (UNLIKELY(numArgs != 1))
badKind(kind, numArgs);
return One;
case Add:
case Sub:
case Mul:
case Div:
case UDiv:
case Mod:
case UMod:
case BitAnd:
case BitOr:
case BitXor:
case Shl:
case SShr:
case ZShr:
case RotR:
case RotL:
case Equal:
case NotEqual:
case LessThan:
case GreaterThan:
case LessEqual:
case GreaterEqual:
case Above:
case Below:
case AboveEqual:
case BelowEqual:
case EqualOrUnordered:
if (UNLIKELY(numArgs != 2))
badKind(kind, numArgs);
return Two;
case Select:
if (UNLIKELY(numArgs != 3))
badKind(kind, numArgs);
return Three;
default:
badKind(kind, numArgs);
break;
}
return VarArgs;
}
protected:
enum CheckedOpcodeTag { CheckedOpcode };
template<typename... Arguments>
explicit Value(CheckedOpcodeTag, Kind kind, Type type, NumChildren numChildren, Origin origin, Value* firstChild, Arguments... arguments)
: m_kind(kind)
, m_type(type)
, m_numChildren(numChildren)
, m_origin(origin)
{
buildAdjacencyList(numChildren, firstChild, arguments...);
}
explicit Value(CheckedOpcodeTag, Kind kind, Type type, NumChildren numChildren, Origin origin)
: m_kind(kind)
, m_type(type)
, m_numChildren(numChildren)
, m_origin(origin)
{
buildAdjacencyList(numChildren);
}
explicit Value(CheckedOpcodeTag, Kind kind, NumChildren numChildren, Origin origin)
: m_kind(kind)
, m_type(typeFor(kind, nullptr))
, m_numChildren(numChildren)
, m_origin(origin)
{
buildAdjacencyList(numChildren);
}
explicit Value(CheckedOpcodeTag, Kind kind, NumChildren numChildren, Origin origin, Value* firstChild)
: m_kind(kind)
, m_type(typeFor(kind, firstChild))
, m_numChildren(numChildren)
, m_origin(origin)
{
buildAdjacencyList(numChildren, firstChild);
}
template<typename... Arguments>
explicit Value(CheckedOpcodeTag, Kind kind, NumChildren numChildren, Origin origin, Value* firstChild, Value* secondChild, Arguments... arguments)
: m_kind(kind)
, m_type(typeFor(kind, firstChild, secondChild))
, m_numChildren(numChildren)
, m_origin(origin)
{
buildAdjacencyList(numChildren, firstChild, secondChild, arguments...);
}
explicit Value(Kind kind, Type type, Origin origin)
: Value(CheckedOpcode, kind, type, Zero, origin)
{
RELEASE_ASSERT(numChildrenForKind(kind, 0) == Zero);
}
template<typename... Arguments>
explicit Value(Kind kind, Origin origin, Arguments... arguments)
: Value(CheckedOpcode, kind, numChildrenForKind(kind, sizeof...(arguments)), origin, static_cast<Value*>(arguments)...)
{
}
template<typename... Arguments>
explicit Value(Kind kind, Type type, Origin origin, Value* firstChild, Arguments... arguments)
: Value(CheckedOpcode, kind, type, numChildrenForKind(kind, 1 + sizeof...(arguments)), origin, firstChild, static_cast<Value*>(arguments)...)
{
}
private:
friend class CheckValue;
static Type typeFor(Kind, Value* firstChild, Value* secondChild = nullptr);
protected:
unsigned m_index { UINT_MAX };
private:
Kind m_kind;
Type m_type;
protected:
NumChildren m_numChildren;
private:
Origin m_origin;
NO_RETURN_DUE_TO_CRASH static void badKind(Kind, unsigned);
public:
BasicBlock* owner { nullptr }; };
class DeepValueDump {
public:
DeepValueDump(const Procedure* proc, const Value* value)
: m_proc(proc)
, m_value(value)
{
}
void dump(PrintStream& out) const;
private:
const Procedure* m_proc;
const Value* m_value;
};
inline DeepValueDump deepDump(const Procedure& proc, const Value* value)
{
return DeepValueDump(&proc, value);
}
inline DeepValueDump deepDump(const Value* value)
{
return DeepValueDump(nullptr, value);
}
#define B3_SPECIALIZE_VALUE_FOR_NO_CHILDREN \
unsigned numChildren() const { return 0; } \
WTF::IteratorRange<Value**> children() { return {nullptr, nullptr}; } \
WTF::IteratorRange<Value* const*> children() const { return { nullptr, nullptr}; }
#define B3_SPECIALIZE_VALUE_FOR_FIXED_CHILDREN(n) \
public: \
unsigned numChildren() const { return n; } \
Value*& child(unsigned index) \
{ \
ASSERT(index <= n); \
return childrenArray()[index]; \
} \
Value* child(unsigned index) const \
{ \
ASSERT(index <= n); \
return childrenArray()[index]; \
} \
Value*& lastChild() \
{ \
return childrenArray()[n - 1]; \
} \
Value* lastChild() const \
{ \
return childrenArray()[n - 1]; \
} \
WTF::IteratorRange<Value**> children() \
{ \
Value** buffer = childrenArray(); \
return {buffer, buffer + n }; \
} \
WTF::IteratorRange<Value* const*> children() const \
{ \
Value* const* buffer = childrenArray(); \
return {buffer, buffer + n }; \
} \
#define B3_SPECIALIZE_VALUE_FOR_NON_VARARGS_CHILDREN \
public: \
unsigned numChildren() const { return m_numChildren; } \
Value*& child(unsigned index) { return childrenArray()[index]; } \
Value* child(unsigned index) const { return childrenArray()[index]; } \
Value*& lastChild() { return childrenArray()[numChildren() - 1]; } \
Value* lastChild() const { return childrenArray()[numChildren() - 1]; } \
WTF::IteratorRange<Value**> children() \
{ \
Value** buffer = childrenArray(); \
return {buffer, buffer + numChildren() }; \
} \
WTF::IteratorRange<Value* const*> children() const \
{ \
Value* const* buffer = childrenArray(); \
return {buffer, buffer + numChildren() }; \
} \
#define B3_SPECIALIZE_VALUE_FOR_VARARGS_CHILDREN \
public: \
unsigned numChildren() const { return childrenVector().size(); } \
Value*& child(unsigned index) { return childrenVector()[index]; } \
Value* child(unsigned index) const { return childrenVector()[index]; } \
Value*& lastChild() { return childrenVector().last(); } \
Value* lastChild() const { return childrenVector().last(); } \
WTF::IteratorRange<Value**> children() \
{ \
Vector<Value*, 3>& vec = childrenVector(); \
return WTF::makeIteratorRange(&*vec.begin(), &*vec.end()); \
} \
WTF::IteratorRange<Value* const*> children() const \
{ \
const Vector<Value*, 3>& vec = childrenVector(); \
return WTF::makeIteratorRange(&*vec.begin(), &*vec.end()); \
} \
#define B3_SPECIALIZE_VALUE_FOR_FINAL_SIZE_FIXED_CHILDREN \
private: \
Value** childrenArray() \
{ \
return bitwise_cast<Value**>(bitwise_cast<char*>(this) + sizeof(*this)); \
} \
Value* const* childrenArray() const \
{ \
return bitwise_cast<Value* const*>(bitwise_cast<char const*>(this) + sizeof(*this)); \
}
#define B3_SPECIALIZE_VALUE_FOR_FINAL_SIZE_VARARGS_CHILDREN \
private: \
Vector<Value*, 3>& childrenVector() \
{ \
return *bitwise_cast<Vector<Value*, 3>*>(bitwise_cast<char*>(this) + sizeof(*this)); \
} \
const Vector<Value*, 3>& childrenVector() const \
{ \
return *bitwise_cast<Vector<Value*, 3> const*>(bitwise_cast<char const*>(this) + sizeof(*this)); \
} \
} }
#endif // ENABLE(B3_JIT)