5#ifndef V8_COMPILER_TURBOSHAFT_OPERATION_MATCHER_H_
6#define V8_COMPILER_TURBOSHAFT_OPERATION_MATCHER_H_
19class OperationMatcher;
22template <
typename T,
bool HasConstexpr>
37 std::variant<Wildcard, OpIndex, OpIndex*, constexpr_type>
v_;
50 if (
v_.index() == 1)
return std::get<1>(
v_) == matched;
56 if (
v_.index() == 2) *std::get<2>(
v_) = matched;
59 std::variant<Wildcard, OpIndex, OpIndex*>
v_;
70 std::variant<Wildcard, T, T*>
v_;
73 if (
v_.index() == 1)
return std::get<1>(
v_) == matched;
77 void bind(
const T& matched) {
79 if (
v_.index() == 2) *std::get<2>(
v_) = matched;
96 return graph_.Get(op_idx).Is<Op>();
101 return graph_.Get(op_idx).TryCast<Op>();
106 return graph_.Get(op_idx).Cast<Op>();
115 if (!op)
return false;
138 if (!op)
return false;
145 if (!op)
return false;
153 if (!op)
return false;
161 if (!op)
return false;
169 if (!op)
return false;
177 if (!op)
return false;
192 (std::isnan(k) && std::isnan(value));
197 return MatchFloat(matched, &k) && std::isnan(k);
203 if (!op)
return false;
215 uint64_t* unsigned_constant,
216 int64_t* signed_constant =
nullptr)
const {
218 if (!op)
return false;
225 if (unsigned_constant) {
226 *unsigned_constant =
static_cast<uint32_t
>(op->
integral());
228 if (signed_constant) {
233 if (unsigned_constant) {
234 *unsigned_constant = op->
integral();
236 if (signed_constant) {
249 int64_t* signed_constant)
const {
256 *constant =
static_cast<uint32_t
>(
value);
270 return static_cast<uint32_t
>(
value) == constant;
283 *constant =
static_cast<int32_t
>(
value);
289 template <
typename T =
intptr_t>
291 if constexpr (
Is64()) {
292 static_assert(
sizeof(
T) ==
sizeof(int64_t));
295 *constant =
static_cast<T
>(v);
298 static_assert(
sizeof(
T) ==
sizeof(int32_t));
301 *constant =
static_cast<T
>(v);
310 *constant = c->signed_integral();
321 *constant = c->integral();
331 if (!op)
return false;
339 if (!op)
return false;
347 template <
typename T>
354 if (!op)
return false;
355 if (input.matches(op->input(),
this) &&
kind.matches(op->kind) &&
356 assumption.matches(op->assumption) && from.matches(op->from) &&
357 to.matches(op->to)) {
358 input.bind(op->input(),
this);
360 assumption.bind(op->assumption);
374 template <
typename T>
380 if (!op)
return false;
384 right.bind(op->
right(),
this);
441 template <
typename T>
455 if (!op || op->
kind !=
kind || op->
rep != rep)
return false;
456 *input = op->
input();
469 if (!op || op->
kind !=
kind || op->
rep != rep)
return false;
471 *right = op->
right();
486 if (uint32_t rhs_constant;
488 rhs_constant < static_cast<uint64_t>(op->
rep.
bit_width())) {
489 *input = op->
left<T>();
492 *amount =
static_cast<int>(rhs_constant);
504 if (uint32_t rhs_constant;
506 (op->
rep == rep || (ShiftOp::AllowsWord64ToWord32Truncation(
kind) &&
510 rhs_constant < static_cast<uint64_t>(rep.
bit_width())) {
511 *input = op->
left<T>();
512 *amount =
static_cast<int>(rhs_constant);
524 if (uint32_t rhs_constant;
525 op && ShiftOp::IsRightShift(op->
kind) && op->
rep == rep &&
527 rhs_constant < static_cast<uint32_t>(rep.
bit_width())) {
528 *input = op->
left<T>();
529 *amount =
static_cast<int>(rhs_constant);
541 if (uint32_t rhs_constant;
542 op && op->
kind == ShiftOp::Kind::kShiftLeft && op->
rep == rep &&
544 rhs_constant < static_cast<uint32_t>(rep.
bit_width())) {
545 *input = op->
left<T>();
546 *amount =
static_cast<int>(rhs_constant);
557 uint16_t* amount)
const {
560 if (uint32_t rhs_constant;
561 op && op->
kind == ShiftOp::Kind::kShiftRightArithmeticShiftOutZeros &&
564 rhs_constant < static_cast<uint64_t>(rep.
bit_width())) {
565 *input = op->
left<T>();
566 *amount =
static_cast<uint16_t
>(rhs_constant);
573 std::optional<int> input_count = std::nullopt)
const {
575 return !input_count.has_value() || phi->input_count == *input_count;
596 DCHECK_LE(cst, std::numeric_limits<int32_t>().max());
597 *divisor =
static_cast<int32_t
>(cst);
607template <
typename T,
bool HasConstexpr>
610 switch (
v_.index()) {
614 return std::get<1>(
v_) == matched;
619 if (!c)
return false;
631template <
typename T,
bool HasConstexpr>
634 DCHECK(matches(matched, matcher));
635 if (
v_.index() == 2) *std::get<2>(
v_) = matched;
double get_scalar() const
V8_INLINE constexpr int32_t value() const
constexpr uint16_t bit_width() const
bool Is(V< AnyOrNone > op_idx) const
OperationMatcher(const Graph &graph)
bool MatchConstantRightShift(V< Any > matched, V< T > *input, WordRepresentation rep, int *amount) const
bool MatchConstantLeftShift(V< Any > matched, V< T > *input, WordRepresentation rep, int *amount) const
bool MatchBitwiseAnd(V< Any > matched, V< T > *left, V< T > *right, WordRepresentation rep) const
bool MatchIntegralWordConstant(V< Any > matched, WordRepresentation rep, int64_t *signed_constant) const
const Operation & Get(V< AnyOrNone > op_idx) const
bool MatchIntegralWord64Constant(V< Any > matched, int64_t *constant) const
bool MatchHeapConstant(V< Any > matched, Handle< HeapObject > *tagged=nullptr) const
bool MatchFloat(V< Any > matched, double *value) const
bool MatchIntegralWordConstant(V< Any > matched, WordRepresentation rep, uint64_t *unsigned_constant, int64_t *signed_constant=nullptr) const
bool MatchWordMul(V< Any > matched, V< T > *left, V< T > *right, WordRepresentation rep) const
bool MatchWordBinop(V< Any > matched, VMatch< T > left, VMatch< T > right, OMatch< WordBinopOp::Kind > kind={}, OMatch< WordRepresentation > rep={}) const
bool MatchZero(V< Any > matched) const
bool MatchFloat32Constant(V< Any > matched, float *constant) const
bool MatchIntegralWordPtrConstant(V< Any > matched, T *constant) const
bool MatchChange(V< Any > matched, VMatch< T > input, OMatch< ChangeOp::Kind > kind={}, OMatch< ChangeOp::Assumption > assumption={}, OMatch< RegisterRepresentation > from={}, OMatch< RegisterRepresentation > to={}) const
bool MatchIntegralZero(V< Any > matched) const
bool MatchSmiZero(V< Any > matched) const
bool MatchFloatRoundDown(V< Any > matched, V< Float > *input, FloatRepresentation rep) const
bool MatchPowerOfTwoWordConstant(V< Any > matched, int64_t *ret_cst, WordRepresentation rep) const
bool MatchPowerOfTwoWord32Constant(V< Any > matched, int32_t *divisor) const
const underlying_operation_t< Op > * TryCast(V< AnyOrNone > op_idx) const
detail::OptionMatch< T > OMatch
bool MatchFloatBinary(V< Any > matched, V< Float > *left, V< Float > *right, FloatBinopOp::Kind kind, FloatRepresentation rep) const
bool MatchEqual(V< Any > matched, V< T > *left, V< T > *right) const
bool MatchFloat64Constant(V< Any > matched, double *constant) const
bool MatchExternalConstant(V< Any > matched, ExternalReference *reference) const
bool MatchIntegralWord32Constant(V< Any > matched, uint32_t constant) const
bool MatchTruncateWord64ToWord32(V< Any > matched, VMatch< Word64 > input) const
bool MatchFloat(V< Any > matched, double value) const
bool MatchFloat32Constant(V< Any > matched, i::Float32 *constant) const
bool MatchConstantShift(V< Any > matched, V< T > *input, ShiftOp::Kind kind, WordRepresentation rep, int *amount) const
bool MatchWordSub(V< Any > matched, V< T > *left, V< T > *right, WordRepresentation rep) const
bool MatchConstantShift(V< Any > matched, V< T > *input, ShiftOp::Kind *kind, WordRepresentation *rep, int *amount) const
bool MatchIntegralWord64Constant(V< Any > matched, uint64_t *constant) const
const underlying_operation_t< Op > & Cast(V< AnyOrNone > op_idx) const
bool MatchIntegralWord32Constant(V< Any > matched, int32_t *constant) const
bool MatchSignedIntegralConstant(V< Any > matched, int64_t *constant) const
bool MatchFloatSub(V< Any > matched, V< Float > *left, V< Float > *right, FloatRepresentation rep) const
bool MatchBitwiseAndWithConstant(V< Any > matched, V< T > *value, uint64_t *constant, WordRepresentation rep) const
bool MatchWasmStubCallConstant(V< Any > matched, uint64_t *stub_id) const
bool MatchIntegralWord32Constant(V< Any > matched, uint32_t *constant) const
bool MatchFloat64Constant(V< Any > matched, i::Float64 *constant) const
bool MatchPhi(V< Any > matched, std::optional< int > input_count=std::nullopt) const
bool MatchWordAdd(V< Any > matched, V< T > *left, V< T > *right, WordRepresentation rep) const
bool MatchFloatUnary(V< Any > matched, V< Float > *input, FloatUnaryOp::Kind kind, FloatRepresentation rep) const
V< AnyOrNone > Index(const Operation &op) const
bool MatchUnsignedIntegralConstant(V< Any > matched, uint64_t *constant) const
bool MatchNaN(V< Float > matched) const
bool MatchConstantShiftRightArithmeticShiftOutZeros(V< Any > matched, V< T > *input, WordRepresentation rep, uint16_t *amount) const
static constexpr RegisterRepresentation Word32()
static constexpr RegisterRepresentation Word64()
static V< T > Cast(V< U > index)
static constexpr WordRepresentation Word32()
constexpr Enum value() const
static constexpr WordRepresentation Word64()
constexpr bool IsPowerOfTwo(T value)
V8_INLINE Dest bit_cast(Source const &source)
typename underlying_operation< T >::type underlying_operation_t
constexpr bool IsValidTypeFor(RegisterRepresentation repr)
bool TryCast(Tagged< From > value, Tagged< To > *out)
#define DCHECK_LE(v1, v2)
#define DCHECK(condition)
RegisterRepresentation rep
i::Float64 float64() const
i::Float32 float32() const
@ kRelocatableWasmStubCall
IndirectHandle< i::HeapObject > handle() const
i::Tagged< Smi > smi() const
union v8::internal::compiler::turboshaft::ConstantOp::Storage storage
RegisterRepresentation rep
int64_t signed_integral() const
uint64_t integral() const
V< Word32 > right() const
V< WordType > left() const
V< WordType > right() const
OptionMatch(const T &value)
bool matches(const T &matched)
void bind(const T &matched)
std::variant< Wildcard, T, T * > v_
ValueMatch(OpIndex *index)
ValueMatch(V< T > *index)
ValueMatch(OpIndex index)
void bind(OpIndex matched, const OperationMatcher *matcher)
bool matches(OpIndex matched, const OperationMatcher *matcher)
std::variant< Wildcard, OpIndex, OpIndex * > v_
ValueMatch(V< T > *index)
bool matches(OpIndex matched, const OperationMatcher *matcher)
ValueMatch(OpIndex index)
ValueMatch(constexpr_type constant)
std::variant< Wildcard, OpIndex, OpIndex *, constexpr_type > v_
ValueMatch(OpIndex *index)
typename v_traits< T >::constexpr_type constexpr_type
void bind(OpIndex matched, const OperationMatcher *matcher)
ExternalReference external