#pragma once
#if ENABLE(B3_JIT)
#include "AirArg.h"
#include "B3Effects.h"
#include "B3FrequentedBlock.h"
#include "B3Kind.h"
#include "B3Origin.h"
#include "B3SparseCollection.h"
#include "B3Type.h"
#include "B3ValueKey.h"
#include <wtf/CommaPrinter.h>
#include <wtf/FastMalloc.h>
#include <wtf/Noncopyable.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:
typedef Vector<Value*, 3> AdjacencyList;
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(); }
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; }
Value*& child(unsigned index) { return m_children[index]; }
Value* child(unsigned index) const { return m_children[index]; }
Value*& lastChild() { return m_children.last(); }
Value* lastChild() const { return m_children.last(); }
unsigned numChildren() const { return m_children.size(); }
Type type() const { return m_type; }
void setType(Type type) { m_type = type; }
Air::Arg::Type airType() const { return Air::Arg::typeForB3Type(type()); }
Air::Arg::Width airWidth() const { return Air::Arg::widthForB3Type(type()); }
AdjacencyList& children() { return m_children; }
const AdjacencyList& children() const { return m_children; }
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() == FalseTriState; }
bool isLikeNonZero() const { return asTriState() == TrueTriState; }
Effects effects() const;
ValueKey key() const;
void performSubstitution();
bool isFree() const;
enum WalkStatus {
Continue,
IgnoreChildren,
Stop
};
template<typename Functor>
void walk(const Functor& functor, PhiChildren* = nullptr);
protected:
virtual Value* cloneImpl() const;
virtual void dumpChildren(CommaPrinter&, PrintStream&) const;
virtual void dumpMeta(CommaPrinter&, PrintStream&) const;
private:
friend class Procedure;
friend class SparseCollection<Value>;
ALWAYS_INLINE static void checkKind(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);
break;
case Return:
if (UNLIKELY(numArgs > 1))
badKind(kind, numArgs);
break;
case Identity:
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:
if (UNLIKELY(numArgs != 1))
badKind(kind, numArgs);
break;
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);
break;
case Select:
if (UNLIKELY(numArgs != 3))
badKind(kind, numArgs);
break;
default:
badKind(kind, numArgs);
break;
}
}
protected:
enum CheckedOpcodeTag { CheckedOpcode };
Value(const Value&) = default;
Value& operator=(const Value&) = default;
template<typename... Arguments>
explicit Value(CheckedOpcodeTag, Kind kind, Type type, Origin origin, Value* firstChild, Arguments... arguments)
: m_kind(kind)
, m_type(type)
, m_origin(origin)
, m_children{ firstChild, arguments... }
{
}
explicit Value(CheckedOpcodeTag, Kind kind, Type type, Origin origin)
: m_kind(kind)
, m_type(type)
, m_origin(origin)
{
}
template<typename... Arguments>
explicit Value(CheckedOpcodeTag, Kind kind, Origin origin, Value* firstChild)
: m_kind(kind)
, m_type(typeFor(kind, firstChild))
, m_origin(origin)
, m_children{ firstChild }
{
}
template<typename... Arguments>
explicit Value(CheckedOpcodeTag, Kind kind, Origin origin, Value* firstChild, Value* secondChild, Arguments... arguments)
: m_kind(kind)
, m_type(typeFor(kind, firstChild, secondChild))
, m_origin(origin)
, m_children{ firstChild, secondChild, arguments... }
{
}
explicit Value(CheckedOpcodeTag, Kind kind, Origin origin)
: m_kind(kind)
, m_type(typeFor(kind, nullptr))
, m_origin(origin)
{
}
explicit Value(CheckedOpcodeTag, Kind kind, Type type, Origin origin, const AdjacencyList& children)
: m_kind(kind)
, m_type(type)
, m_origin(origin)
, m_children(children)
{
}
explicit Value(CheckedOpcodeTag, Kind kind, Type type, Origin origin, AdjacencyList&& children)
: m_kind(kind)
, m_type(type)
, m_origin(origin)
, m_children(WTFMove(children))
{
}
template<typename... Arguments>
explicit Value(Kind kind, Type type, Origin origin)
: Value(CheckedOpcode, kind, type, origin)
{
checkKind(kind, 0);
}
template<typename... Arguments>
explicit Value(Kind kind, Type type, Origin origin, Value* firstChild, Arguments&&... arguments)
: Value(CheckedOpcode, kind, type, origin, firstChild, std::forward<Arguments>(arguments)...)
{
checkKind(kind, 1 + sizeof...(arguments));
}
template<typename... Arguments>
explicit Value(Kind kind, Type type, Origin origin, const AdjacencyList& children)
: Value(CheckedOpcode, kind, type, origin, children)
{
checkKind(kind, children.size());
}
template<typename... Arguments>
explicit Value(Kind kind, Type type, Origin origin, AdjacencyList&& children)
: Value(CheckedOpcode, kind, type, origin, WTFMove(children))
{
checkKind(kind, m_children.size());
}
template<typename... Arguments>
explicit Value(Kind kind, Origin origin, Arguments&&... arguments)
: Value(CheckedOpcode, kind, origin, std::forward<Arguments>(arguments)...)
{
checkKind(kind, sizeof...(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;
Origin m_origin;
AdjacencyList m_children;
JS_EXPORT_PRIVATE 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
{
if (m_value)
m_value->deepDump(m_proc, out);
else
out.print("<null>");
}
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);
}
} }
#endif // ENABLE(B3_JIT)