1 // Copyright 2018 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/type-narrowing-reducer.h"
6 
7 #include "src/compiler/js-graph.h"
8 #include "src/objects-inl.h"
9 
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13 
TypeNarrowingReducer(Editor * editor,JSGraph * jsgraph,JSHeapBroker * js_heap_broker)14 TypeNarrowingReducer::TypeNarrowingReducer(Editor* editor, JSGraph* jsgraph,
15                                            JSHeapBroker* js_heap_broker)
16     : AdvancedReducer(editor),
17       jsgraph_(jsgraph),
18       op_typer_(jsgraph->isolate(), js_heap_broker, zone()) {}
19 
~TypeNarrowingReducer()20 TypeNarrowingReducer::~TypeNarrowingReducer() {}
21 
Reduce(Node * node)22 Reduction TypeNarrowingReducer::Reduce(Node* node) {
23   DisallowHeapAccess no_heap_access;
24 
25   Type new_type = Type::Any();
26 
27   switch (node->opcode()) {
28     case IrOpcode::kNumberLessThan: {
29       // TODO(turbofan) Reuse the logic from typer.cc (by integrating relational
30       // comparisons with the operation typer).
31       Type left_type = NodeProperties::GetType(node->InputAt(0));
32       Type right_type = NodeProperties::GetType(node->InputAt(1));
33       if (left_type.Is(Type::PlainNumber()) &&
34           right_type.Is(Type::PlainNumber())) {
35         if (left_type.Max() < right_type.Min()) {
36           new_type = op_typer_.singleton_true();
37         } else if (left_type.Min() >= right_type.Max()) {
38           new_type = op_typer_.singleton_false();
39         }
40       }
41       break;
42     }
43 
44     case IrOpcode::kTypeGuard: {
45       new_type = op_typer_.TypeTypeGuard(
46           node->op(), NodeProperties::GetType(node->InputAt(0)));
47       break;
48     }
49 
50 #define DECLARE_CASE(Name)                                                \
51   case IrOpcode::k##Name: {                                               \
52     new_type = op_typer_.Name(NodeProperties::GetType(node->InputAt(0)),  \
53                               NodeProperties::GetType(node->InputAt(1))); \
54     break;                                                                \
55   }
56       SIMPLIFIED_NUMBER_BINOP_LIST(DECLARE_CASE)
57       DECLARE_CASE(SameValue)
58 #undef DECLARE_CASE
59 
60 #define DECLARE_CASE(Name)                                                \
61   case IrOpcode::k##Name: {                                               \
62     new_type = op_typer_.Name(NodeProperties::GetType(node->InputAt(0))); \
63     break;                                                                \
64   }
65       SIMPLIFIED_NUMBER_UNOP_LIST(DECLARE_CASE)
66       DECLARE_CASE(ToBoolean)
67 #undef DECLARE_CASE
68 
69     default:
70       return NoChange();
71   }
72 
73   Type original_type = NodeProperties::GetType(node);
74   Type restricted = Type::Intersect(new_type, original_type, zone());
75   if (!original_type.Is(restricted)) {
76     NodeProperties::SetType(node, restricted);
77     return Changed(node);
78   }
79   return NoChange();
80 }
81 
graph() const82 Graph* TypeNarrowingReducer::graph() const { return jsgraph()->graph(); }
83 
zone() const84 Zone* TypeNarrowingReducer::zone() const { return graph()->zone(); }
85 
86 }  // namespace compiler
87 }  // namespace internal
88 }  // namespace v8
89