v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bitcast-elider.cc
Go to the documentation of this file.
1// Copyright 2016 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
9namespace v8 {
10namespace internal {
11namespace compiler {
12
13namespace {
14
15bool IsBitcast(Node* node) {
16 // We can only elide kBitcastTaggedToWordForTagAndSmiBits and
17 // kBitcastWordToTaggedSigned because others might affect GC / safepoint
18 // tables.
19 return node->opcode() == IrOpcode::kBitcastTaggedToWordForTagAndSmiBits ||
20 node->opcode() == IrOpcode::kBitcastWordToTaggedSigned;
21}
22
23bool OwnedByWord32Op(Node* node) {
24#if V8_TARGET_ARCH_LOONG64 || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_RISCV64
25 return false;
26#else
27 for (Node* const use : node->uses()) {
28 switch (use->opcode()) {
29 case IrOpcode::kWord32Equal:
30 case IrOpcode::kInt32LessThan:
31 case IrOpcode::kInt32LessThanOrEqual:
32 case IrOpcode::kUint32LessThan:
33 case IrOpcode::kUint32LessThanOrEqual:
34 case IrOpcode::kChangeInt32ToInt64:
35#define Word32Op(Name) case IrOpcode::k##Name:
37#undef Word32Op
38 break;
39 default:
40 return false;
41 }
42 }
43 return true;
44#endif
45}
46
47void Replace(Node* node, Node* replacement) {
48 for (Edge edge : node->use_edges()) {
49 edge.UpdateTo(replacement);
50 }
51 node->Kill();
52}
53
54} // namespace
55
57 if (seen_.Get(node)) return;
58 seen_.Set(node, true);
59 to_visit_.push(node);
60}
61
62void BitcastElider::Revisit(Node* node) { to_visit_.push(node); }
63
65 for (int i = 0; i < node->InputCount(); i++) {
66 Node* input = node->InputAt(i);
67 // This can happen as a result of previous replacements.
68 if (input == nullptr) continue;
69 if (input->opcode() == IrOpcode::kTruncateInt64ToInt32 &&
70 OwnedByWord32Op(input)) {
71 Replace(input, input->InputAt(0));
72 Revisit(node);
73 } else if (is_builtin_ && IsBitcast(input)) {
74 Replace(input, input->InputAt(0));
75 Revisit(node);
76 } else {
77 Enqueue(input);
78 }
79 }
80}
81
83 Enqueue(graph_->end());
84 while (!to_visit_.empty()) {
85 Node* node = to_visit_.front();
86 to_visit_.pop();
87 VisitNode(node);
88 }
89}
90
91BitcastElider::BitcastElider(Zone* zone, TFGraph* graph, bool is_builtin)
92 : graph_(graph),
93 to_visit_(zone),
94 seen_(graph, 2),
95 is_builtin_(is_builtin) {}
96
98
99} // namespace compiler
100} // namespace internal
101} // namespace v8
#define Word32Op(Name)
BitcastElider(Zone *zone, TFGraph *graph, bool is_builtin)
V8_INLINE void Set(Node *node, State state)
Definition node-marker.h:69
V8_INLINE State Get(const Node *node)
Definition node-marker.h:65
Node * InputAt(int index) const
Definition node.h:70
#define MACHINE_BINOP_32_LIST(V)
Definition opcodes.h:641
TFGraph * graph_