blob: 012004a8af6c1c895fd680c589501cd19c300bb1 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include "src/compiler/simplified-operator-reducer.h"
6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/compiler/js-graph.h"
8#include "src/compiler/machine-operator.h"
9#include "src/compiler/node-matchers.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/compiler/operator-properties.h"
11#include "src/conversions-inl.h"
Ben Murdochda12d292016-06-02 14:46:10 +010012#include "src/type-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14namespace v8 {
15namespace internal {
16namespace compiler {
17
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018SimplifiedOperatorReducer::SimplifiedOperatorReducer(JSGraph* jsgraph)
Ben Murdochda12d292016-06-02 14:46:10 +010019 : jsgraph_(jsgraph), type_cache_(TypeCache::Get()) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040020
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {}
22
23
24Reduction SimplifiedOperatorReducer::Reduce(Node* node) {
25 switch (node->opcode()) {
26 case IrOpcode::kBooleanNot: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 HeapObjectMatcher m(node->InputAt(0));
28 if (m.HasValue()) {
29 return Replace(jsgraph()->BooleanConstant(!m.Value()->BooleanValue()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 if (m.IsBooleanNot()) return Replace(m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 break;
33 }
34 case IrOpcode::kChangeBitToBool: {
35 Int32Matcher m(node->InputAt(0));
36 if (m.Is(0)) return Replace(jsgraph()->FalseConstant());
37 if (m.Is(1)) return Replace(jsgraph()->TrueConstant());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 if (m.IsChangeBoolToBit()) return Replace(m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 break;
40 }
41 case IrOpcode::kChangeBoolToBit: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 HeapObjectMatcher m(node->InputAt(0));
43 if (m.HasValue()) return ReplaceInt32(m.Value()->BooleanValue());
44 if (m.IsChangeBitToBool()) return Replace(m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 break;
46 }
47 case IrOpcode::kChangeFloat64ToTagged: {
48 Float64Matcher m(node->InputAt(0));
49 if (m.HasValue()) return ReplaceNumber(m.Value());
50 break;
51 }
52 case IrOpcode::kChangeInt32ToTagged: {
53 Int32Matcher m(node->InputAt(0));
54 if (m.HasValue()) return ReplaceNumber(m.Value());
55 break;
56 }
57 case IrOpcode::kChangeTaggedToFloat64: {
58 NumberMatcher m(node->InputAt(0));
59 if (m.HasValue()) return ReplaceFloat64(m.Value());
60 if (m.IsChangeFloat64ToTagged()) return Replace(m.node()->InputAt(0));
61 if (m.IsChangeInt32ToTagged()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 return Change(node, machine()->ChangeInt32ToFloat64(), m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 }
64 if (m.IsChangeUint32ToTagged()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 return Change(node, machine()->ChangeUint32ToFloat64(), m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 }
67 break;
68 }
69 case IrOpcode::kChangeTaggedToInt32: {
70 NumberMatcher m(node->InputAt(0));
71 if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
72 if (m.IsChangeFloat64ToTagged()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 return Change(node, machine()->ChangeFloat64ToInt32(), m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 if (m.IsChangeInt32ToTagged()) return Replace(m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 break;
77 }
78 case IrOpcode::kChangeTaggedToUint32: {
79 NumberMatcher m(node->InputAt(0));
80 if (m.HasValue()) return ReplaceUint32(DoubleToUint32(m.Value()));
81 if (m.IsChangeFloat64ToTagged()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 return Change(node, machine()->ChangeFloat64ToUint32(), m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 if (m.IsChangeUint32ToTagged()) return Replace(m.InputAt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 break;
86 }
87 case IrOpcode::kChangeUint32ToTagged: {
88 Uint32Matcher m(node->InputAt(0));
89 if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value()));
90 break;
91 }
Ben Murdochda12d292016-06-02 14:46:10 +010092 case IrOpcode::kNumberCeil:
93 case IrOpcode::kNumberFloor:
94 case IrOpcode::kNumberRound:
95 case IrOpcode::kNumberTrunc: {
96 Node* const input = NodeProperties::GetValueInput(node, 0);
97 Type* const input_type = NodeProperties::GetType(input);
98 if (input_type->Is(type_cache_.kIntegerOrMinusZeroOrNaN)) {
99 return Replace(input);
100 }
101 break;
102 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103 case IrOpcode::kReferenceEqual:
104 return ReduceReferenceEqual(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 default:
106 break;
107 }
108 return NoChange();
109}
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111Reduction SimplifiedOperatorReducer::ReduceReferenceEqual(Node* node) {
112 DCHECK_EQ(IrOpcode::kReferenceEqual, node->opcode());
113 Node* const left = NodeProperties::GetValueInput(node, 0);
114 Node* const right = NodeProperties::GetValueInput(node, 1);
115 HeapObjectMatcher match_left(left);
116 HeapObjectMatcher match_right(right);
117 if (match_left.HasValue() && match_right.HasValue()) {
118 if (match_left.Value().is_identical_to(match_right.Value())) {
119 return Replace(jsgraph()->TrueConstant());
120 } else {
121 return Replace(jsgraph()->FalseConstant());
122 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400123 }
124 return NoChange();
125}
126
127
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
129 Node* a) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400130 DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
131 DCHECK_LE(1, node->InputCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 node->ReplaceInput(0, a);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 NodeProperties::ChangeOp(node, op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 return Changed(node);
135}
136
137
138Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) {
139 return Replace(jsgraph()->Float64Constant(value));
140}
141
142
143Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) {
144 return Replace(jsgraph()->Int32Constant(value));
145}
146
147
148Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) {
149 return Replace(jsgraph()->Constant(value));
150}
151
152
153Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) {
154 return Replace(jsgraph()->Constant(value));
155}
156
157
158Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); }
159
160
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const {
162 return jsgraph()->machine();
163}
164
165} // namespace compiler
166} // namespace internal
167} // namespace v8