v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
typed-optimizations-reducer.h
Go to the documentation of this file.
1// Copyright 2022 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
5#ifndef V8_COMPILER_TURBOSHAFT_TYPED_OPTIMIZATIONS_REDUCER_H_
6#define V8_COMPILER_TURBOSHAFT_TYPED_OPTIMIZATIONS_REDUCER_H_
7
13
15
16template <typename>
17class TypeInferenceReducer;
18
19template <typename Next>
21 : public UniformReducerAdapter<TypedOptimizationsReducer, Next> {
22#if defined(__clang__)
23 // Typed optimizations require a typed graph.
25#endif
26
27 public:
28 TURBOSHAFT_REDUCER_BOILERPLATE(TypedOptimizations)
29
31
32 OpIndex ReduceInputGraphBranch(OpIndex ig_index, const BranchOp& operation) {
34 Type condition_type = GetType(operation.condition());
35 if (!condition_type.IsInvalid()) {
36 if (condition_type.IsNone()) {
37 Asm().Unreachable();
38 return OpIndex::Invalid();
39 }
40 condition_type = Typer::TruncateWord32Input(condition_type, true,
41 Asm().graph_zone());
42 DCHECK(condition_type.IsWord32());
43 if (auto c = condition_type.AsWord32().try_get_constant()) {
44 Block* goto_target = *c == 0 ? operation.if_false : operation.if_true;
45 Asm().Goto(Asm().MapToNewGraph(goto_target));
46 return OpIndex::Invalid();
47 }
48 }
49 }
50 return Adapter::ReduceInputGraphBranch(ig_index, operation);
51 }
52
53 template <typename Op, typename Continuation>
54 OpIndex ReduceInputGraphOperation(OpIndex ig_index, const Op& operation) {
56 Type type = GetType(ig_index);
57 if (type.IsNone()) {
58 // This operation is dead. Remove it.
59 DCHECK(CanBeTyped(operation));
60 Asm().Unreachable();
61 return OpIndex::Invalid();
62 } else if (!type.IsInvalid()) {
63 // See if we can replace the operation by a constant.
64 if (OpIndex constant = TryAssembleConstantForType(type);
65 constant.valid()) {
66 return constant;
67 }
68 }
69 }
70
71 // Otherwise just continue with reduction.
72 return Continuation{this}.ReduceInputGraph(ig_index, operation);
73 }
74
75 private:
76 // If {type} is a single value that can be respresented by a constant, this
77 // function returns the index for a corresponding ConstantOp. It returns
78 // OpIndex::Invalid otherwise.
80 switch (type.kind()) {
82 auto w32 = type.AsWord32();
83 if (auto c = w32.try_get_constant()) {
84 return Asm().Word32Constant(*c);
85 }
86 break;
87 }
89 auto w64 = type.AsWord64();
90 if (auto c = w64.try_get_constant()) {
91 return Asm().Word64Constant(*c);
92 }
93 break;
94 }
96 auto f32 = type.AsFloat32();
97 if (f32.is_only_nan()) {
98 return Asm().Float32Constant(nan_v<32>);
99 } else if (f32.is_only_minus_zero()) {
100 return Asm().Float32Constant(-0.0f);
101 } else if (auto c = f32.try_get_constant()) {
102 return Asm().Float32Constant(*c);
103 }
104 break;
105 }
107 auto f64 = type.AsFloat64();
108 if (f64.is_only_nan()) {
109 return Asm().Float64Constant(nan_v<64>);
110 } else if (f64.is_only_minus_zero()) {
111 return Asm().Float64Constant(-0.0);
112 } else if (auto c = f64.try_get_constant()) {
113 return Asm().Float64Constant(*c);
114 }
115 break;
116 }
117 default:
118 break;
119 }
120 return OpIndex::Invalid();
121 }
122
123 Type GetType(const OpIndex index) {
124 // Typed optimizations use the types of the input graph.
125 return Asm().GetInputGraphType(index);
126 }
127};
128
129} // namespace v8::internal::compiler::turboshaft
130
131#endif // V8_COMPILER_TURBOSHAFT_TYPED_OPTIMIZATIONS_REDUCER_H_
static constexpr OpIndex Invalid()
Definition index.h:88
const Word32Type & AsWord32() const
Definition types.h:860
OpIndex ReduceInputGraphBranch(OpIndex ig_index, const BranchOp &operation)
OpIndex ReduceInputGraphOperation(OpIndex ig_index, const Op &operation)
static Word32Type TruncateWord32Input(const Type &input, bool implicit_word64_narrowing, Zone *zone)
Definition typer.h:1515
#define TURBOSHAFT_REDUCER_BOILERPLATE(Name)
Definition assembler.h:823
Zone * graph_zone
V8_INLINE bool CanBeTyped(const Op &operation)
constexpr float_type< Bits > nan_v
Definition types.h:161
V8_EXPORT_PRIVATE bool ShouldSkipOptimizationStep()
Definition utils.h:84
#define DCHECK(condition)
Definition logging.h:482