deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / simplified-operator-reducer.cc
1 // 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
5 #include "src/compiler/simplified-operator-reducer.h"
6
7 #include "src/compiler/js-graph.h"
8 #include "src/compiler/machine-operator.h"
9 #include "src/compiler/node-matchers.h"
10 #include "src/compiler/operator-properties.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 SimplifiedOperatorReducer::SimplifiedOperatorReducer(JSGraph* jsgraph)
17     : jsgraph_(jsgraph), simplified_(jsgraph->zone()) {}
18
19
20 SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {}
21
22
23 Reduction SimplifiedOperatorReducer::Reduce(Node* node) {
24   switch (node->opcode()) {
25     case IrOpcode::kBooleanNot: {
26       HeapObjectMatcher<HeapObject> m(node->InputAt(0));
27       if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->false_value()))) {
28         return Replace(jsgraph()->TrueConstant());
29       }
30       if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->true_value()))) {
31         return Replace(jsgraph()->FalseConstant());
32       }
33       if (m.IsBooleanNot()) return Replace(m.node()->InputAt(0));
34       break;
35     }
36     case IrOpcode::kChangeBitToBool: {
37       Int32Matcher m(node->InputAt(0));
38       if (m.Is(0)) return Replace(jsgraph()->FalseConstant());
39       if (m.Is(1)) return Replace(jsgraph()->TrueConstant());
40       if (m.IsChangeBoolToBit()) return Replace(m.node()->InputAt(0));
41       break;
42     }
43     case IrOpcode::kChangeBoolToBit: {
44       HeapObjectMatcher<HeapObject> m(node->InputAt(0));
45       if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->false_value()))) {
46         return ReplaceInt32(0);
47       }
48       if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->true_value()))) {
49         return ReplaceInt32(1);
50       }
51       if (m.IsChangeBitToBool()) return Replace(m.node()->InputAt(0));
52       break;
53     }
54     case IrOpcode::kChangeFloat64ToTagged: {
55       Float64Matcher m(node->InputAt(0));
56       if (m.HasValue()) return ReplaceNumber(m.Value());
57       break;
58     }
59     case IrOpcode::kChangeInt32ToTagged: {
60       Int32Matcher m(node->InputAt(0));
61       if (m.HasValue()) return ReplaceNumber(m.Value());
62       break;
63     }
64     case IrOpcode::kChangeTaggedToFloat64: {
65       NumberMatcher m(node->InputAt(0));
66       if (m.HasValue()) return ReplaceFloat64(m.Value());
67       if (m.IsChangeFloat64ToTagged()) return Replace(m.node()->InputAt(0));
68       if (m.IsChangeInt32ToTagged()) {
69         return Change(node, machine()->ChangeInt32ToFloat64(),
70                       m.node()->InputAt(0));
71       }
72       if (m.IsChangeUint32ToTagged()) {
73         return Change(node, machine()->ChangeUint32ToFloat64(),
74                       m.node()->InputAt(0));
75       }
76       break;
77     }
78     case IrOpcode::kChangeTaggedToInt32: {
79       NumberMatcher m(node->InputAt(0));
80       if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
81       if (m.IsChangeFloat64ToTagged()) {
82         return Change(node, machine()->ChangeFloat64ToInt32(),
83                       m.node()->InputAt(0));
84       }
85       if (m.IsChangeInt32ToTagged()) return Replace(m.node()->InputAt(0));
86       break;
87     }
88     case IrOpcode::kChangeTaggedToUint32: {
89       NumberMatcher m(node->InputAt(0));
90       if (m.HasValue()) return ReplaceUint32(DoubleToUint32(m.Value()));
91       if (m.IsChangeFloat64ToTagged()) {
92         return Change(node, machine()->ChangeFloat64ToUint32(),
93                       m.node()->InputAt(0));
94       }
95       if (m.IsChangeUint32ToTagged()) return Replace(m.node()->InputAt(0));
96       break;
97     }
98     case IrOpcode::kChangeUint32ToTagged: {
99       Uint32Matcher m(node->InputAt(0));
100       if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value()));
101       break;
102     }
103     default:
104       break;
105   }
106   return NoChange();
107 }
108
109
110 Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
111                                             Node* a) {
112   DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
113   DCHECK_LE(1, node->InputCount());
114   node->set_op(op);
115   node->ReplaceInput(0, a);
116   return Changed(node);
117 }
118
119
120 Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) {
121   return Replace(jsgraph()->Float64Constant(value));
122 }
123
124
125 Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) {
126   return Replace(jsgraph()->Int32Constant(value));
127 }
128
129
130 Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) {
131   return Replace(jsgraph()->Constant(value));
132 }
133
134
135 Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) {
136   return Replace(jsgraph()->Constant(value));
137 }
138
139
140 Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); }
141
142
143 Factory* SimplifiedOperatorReducer::factory() const {
144   return jsgraph()->isolate()->factory();
145 }
146
147
148 MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const {
149   return jsgraph()->machine();
150 }
151
152 }  // namespace compiler
153 }  // namespace internal
154 }  // namespace v8