1 // Copyright 2019 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 #include "src/compiler/add-type-assertions-reducer.h"
6
7 #include "src/compiler/node-properties.h"
8
9 namespace v8 {
10 namespace internal {
11 namespace compiler {
12
AddTypeAssertionsReducer(Editor * editor,JSGraph * jsgraph,Zone * zone)13 AddTypeAssertionsReducer::AddTypeAssertionsReducer(Editor* editor,
14 JSGraph* jsgraph, Zone* zone)
15 : AdvancedReducer(editor),
16 jsgraph_(jsgraph),
17 visited_(jsgraph->graph()->NodeCount(), zone) {}
18
19 AddTypeAssertionsReducer::~AddTypeAssertionsReducer() = default;
20
Reduce(Node * node)21 Reduction AddTypeAssertionsReducer::Reduce(Node* node) {
22 if (node->opcode() == IrOpcode::kAssertType ||
23 node->opcode() == IrOpcode::kAllocate ||
24 node->opcode() == IrOpcode::kObjectState ||
25 node->opcode() == IrOpcode::kObjectId ||
26 node->opcode() == IrOpcode::kPhi || !NodeProperties::IsTyped(node) ||
27 visited_.Get(node)) {
28 return NoChange();
29 }
30 visited_.Set(node, true);
31
32 Type type = NodeProperties::GetType(node);
33 if (!type.CanBeAsserted()) return NoChange();
34
35 Node* assertion = graph()->NewNode(simplified()->AssertType(type), node);
36 NodeProperties::SetType(assertion, type);
37
38 for (Edge edge : node->use_edges()) {
39 Node* const user = edge.from();
40 DCHECK(!user->IsDead());
41 if (NodeProperties::IsValueEdge(edge) && user != assertion) {
42 edge.UpdateTo(assertion);
43 Revisit(user);
44 }
45 }
46
47 return NoChange();
48 }
49
50 } // namespace compiler
51 } // namespace internal
52 } // namespace v8
53