v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
typer.cc
Go to the documentation of this file.
1// Copyright 2023 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8
10 bool then_branch, Zone* zone) {
11 if (const ComparisonOp* comparison = condition.TryCast<ComparisonOp>()) {
12 Type lhs = type_getter_(comparison->left());
13 Type rhs = type_getter_(comparison->right());
14
15 bool is_signed, is_less_than;
16 switch (comparison->kind) {
18 // TODO(nicohartmann@): Add support for equality.
19 return;
21 is_signed = true;
22 is_less_than = true;
23 break;
25 is_signed = true;
26 is_less_than = false;
27 break;
29 is_signed = false;
30 is_less_than = true;
31 break;
33 is_signed = false;
34 is_less_than = false;
35 break;
36 }
37
38 Type l_refined;
39 Type r_refined;
40
41 if (lhs.IsNone() || rhs.IsNone()) {
42 type_refiner_(comparison->left(), Type::None());
43 type_refiner_(comparison->right(), Type::None());
44 return;
45 } else if (lhs.IsAny() || rhs.IsAny()) {
46 // If either side has any type, there is not much we can do.
47 return;
48 }
49
50 switch (comparison->rep.value()) {
52 if (is_signed) {
53 // TODO(nicohartmann@): Support signed comparison.
54 return;
55 }
56 Word32Type l = Typer::TruncateWord32Input(lhs, true, zone).AsWord32();
58 Type l_restrict, r_restrict;
59 using OpTyper = WordOperationTyper<32>;
60 if (is_less_than) {
61 std::tie(l_restrict, r_restrict) =
62 then_branch
63 ? OpTyper::RestrictionForUnsignedLessThan_True(l, r, zone)
64 : OpTyper::RestrictionForUnsignedLessThan_False(l, r, zone);
65 } else {
66 std::tie(l_restrict, r_restrict) =
67 then_branch
68 ? OpTyper::RestrictionForUnsignedLessThanOrEqual_True(l, r,
69 zone)
70 : OpTyper::RestrictionForUnsignedLessThanOrEqual_False(l, r,
71 zone);
72 }
73
74 // Special handling for word32 restriction, because the inputs might
75 // have been truncated from word64 implicitly.
76 l_refined = RefineWord32Type<true>(lhs, l_restrict, zone);
77 r_refined = RefineWord32Type<true>(rhs, r_restrict, zone);
78 break;
79 }
81 Float64Type l = lhs.AsFloat64();
82 Float64Type r = rhs.AsFloat64();
83 Type l_restrict, r_restrict;
84 using OpTyper = FloatOperationTyper<64>;
85 if (is_less_than) {
86 std::tie(l_restrict, r_restrict) =
87 then_branch ? OpTyper::RestrictionForLessThan_True(l, r, zone)
88 : OpTyper::RestrictionForLessThan_False(l, r, zone);
89 } else {
90 std::tie(l_restrict, r_restrict) =
91 then_branch
92 ? OpTyper::RestrictionForLessThanOrEqual_True(l, r, zone)
93 : OpTyper::RestrictionForLessThanOrEqual_False(l, r, zone);
94 }
95
96 l_refined = l_restrict.IsNone() ? Type::None()
98 l, l_restrict.AsFloat64(), zone);
99 r_refined = r_restrict.IsNone() ? Type::None()
101 r, r_restrict.AsFloat64(), zone);
102 break;
103 }
104 default:
105 return;
106 }
107
108 // In some cases, the refined type is not a subtype of the old type,
109 // because it cannot be represented precisely. In this case we keep the
110 // old type to be stable.
111 if (l_refined.IsSubtypeOf(lhs)) {
112 type_refiner_(comparison->left(), l_refined);
113 }
114 if (r_refined.IsSubtypeOf(rhs)) {
115 type_refiner_(comparison->right(), r_refined);
116 }
117 }
118}
119
120} // namespace v8::internal::compiler::turboshaft
static Type Intersect(const FloatType &lhs, const FloatType &rhs, Zone *zone)
Definition types.cc:578
static constexpr RegisterRepresentation Word32()
static constexpr RegisterRepresentation Float64()
const Word32Type & AsWord32() const
Definition types.h:860
bool IsSubtypeOf(const Type &other) const
Definition types.cc:51
const Float64Type & AsFloat64() const
Definition types.h:875
void RefineTypes(const Operation &condition, bool then_branch, Zone *zone)
Definition typer.cc:9
Type RefineWord32Type(const Type &type, const Type &refinement, Zone *zone)
Definition typer.h:1572
static Word32Type TruncateWord32Input(const Type &input, bool implicit_word64_narrowing, Zone *zone)
Definition typer.h:1515
int r
Definition mul-fft.cc:298
bool is_signed(Condition cond)
const underlying_operation_t< Op > * TryCast() const
Definition operations.h:990