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.
5 #include "src/compiler/simplified-operator-reducer.h"
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"
16 SimplifiedOperatorReducer::SimplifiedOperatorReducer(JSGraph* jsgraph)
17 : jsgraph_(jsgraph), simplified_(jsgraph->zone()) {}
20 SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {}
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());
30 if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->true_value()))) {
31 return Replace(jsgraph()->FalseConstant());
33 if (m.IsBooleanNot()) return Replace(m.node()->InputAt(0));
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));
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);
48 if (m.Is(Unique<HeapObject>::CreateImmovable(factory()->true_value()))) {
49 return ReplaceInt32(1);
51 if (m.IsChangeBitToBool()) return Replace(m.node()->InputAt(0));
54 case IrOpcode::kChangeFloat64ToTagged: {
55 Float64Matcher m(node->InputAt(0));
56 if (m.HasValue()) return ReplaceNumber(m.Value());
59 case IrOpcode::kChangeInt32ToTagged: {
60 Int32Matcher m(node->InputAt(0));
61 if (m.HasValue()) return ReplaceNumber(m.Value());
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));
72 if (m.IsChangeUint32ToTagged()) {
73 return Change(node, machine()->ChangeUint32ToFloat64(),
74 m.node()->InputAt(0));
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));
85 if (m.IsChangeInt32ToTagged()) return Replace(m.node()->InputAt(0));
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));
95 if (m.IsChangeUint32ToTagged()) return Replace(m.node()->InputAt(0));
98 case IrOpcode::kChangeUint32ToTagged: {
99 Uint32Matcher m(node->InputAt(0));
100 if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value()));
110 Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
112 DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
113 DCHECK_LE(1, node->InputCount());
115 node->ReplaceInput(0, a);
116 return Changed(node);
120 Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) {
121 return Replace(jsgraph()->Float64Constant(value));
125 Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) {
126 return Replace(jsgraph()->Int32Constant(value));
130 Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) {
131 return Replace(jsgraph()->Constant(value));
135 Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) {
136 return Replace(jsgraph()->Constant(value));
140 Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); }
143 Factory* SimplifiedOperatorReducer::factory() const {
144 return jsgraph()->isolate()->factory();
148 MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const {
149 return jsgraph()->machine();
152 } // namespace compiler
153 } // namespace internal