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 #include "src/compiler/simplified-operator.h"
12 #include "src/compiler/type-cache.h"
13 #include "src/conversions-inl.h"
14 
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18 
19 namespace {
20 
DecideObjectIsSmi(Node * const input)21 Decision DecideObjectIsSmi(Node* const input) {
22   NumberMatcher m(input);
23   if (m.HasValue()) {
24     return IsSmiDouble(m.Value()) ? Decision::kTrue : Decision::kFalse;
25   }
26   if (m.IsAllocate()) return Decision::kFalse;
27   if (m.IsChangeBitToTagged()) return Decision::kFalse;
28   if (m.IsChangeInt31ToTaggedSigned()) return Decision::kTrue;
29   if (m.IsHeapConstant()) return Decision::kFalse;
30   return Decision::kUnknown;
31 }
32 
33 }  // namespace
34 
SimplifiedOperatorReducer(Editor * editor,JSGraph * jsgraph)35 SimplifiedOperatorReducer::SimplifiedOperatorReducer(Editor* editor,
36                                                      JSGraph* jsgraph)
37     : AdvancedReducer(editor), jsgraph_(jsgraph) {}
38 
~SimplifiedOperatorReducer()39 SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {}
40 
41 
Reduce(Node * node)42 Reduction SimplifiedOperatorReducer::Reduce(Node* node) {
43   switch (node->opcode()) {
44     case IrOpcode::kBooleanNot: {
45       HeapObjectMatcher m(node->InputAt(0));
46       if (m.Is(factory()->true_value())) return ReplaceBoolean(false);
47       if (m.Is(factory()->false_value())) return ReplaceBoolean(true);
48       if (m.IsBooleanNot()) return Replace(m.InputAt(0));
49       break;
50     }
51     case IrOpcode::kChangeBitToTagged: {
52       Int32Matcher m(node->InputAt(0));
53       if (m.Is(0)) return Replace(jsgraph()->FalseConstant());
54       if (m.Is(1)) return Replace(jsgraph()->TrueConstant());
55       if (m.IsChangeTaggedToBit()) return Replace(m.InputAt(0));
56       break;
57     }
58     case IrOpcode::kChangeTaggedToBit: {
59       HeapObjectMatcher m(node->InputAt(0));
60       if (m.HasValue()) return ReplaceInt32(m.Value()->BooleanValue());
61       if (m.IsChangeBitToTagged()) return Replace(m.InputAt(0));
62       break;
63     }
64     case IrOpcode::kChangeFloat64ToTagged: {
65       Float64Matcher m(node->InputAt(0));
66       if (m.HasValue()) return ReplaceNumber(m.Value());
67       if (m.IsChangeTaggedToFloat64()) return Replace(m.node()->InputAt(0));
68       break;
69     }
70     case IrOpcode::kChangeInt31ToTaggedSigned:
71     case IrOpcode::kChangeInt32ToTagged: {
72       Int32Matcher m(node->InputAt(0));
73       if (m.HasValue()) return ReplaceNumber(m.Value());
74       if (m.IsChangeTaggedToInt32() || m.IsChangeTaggedSignedToInt32()) {
75         return Replace(m.InputAt(0));
76       }
77       break;
78     }
79     case IrOpcode::kChangeTaggedToFloat64:
80     case IrOpcode::kTruncateTaggedToFloat64: {
81       NumberMatcher m(node->InputAt(0));
82       if (m.HasValue()) return ReplaceFloat64(m.Value());
83       if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
84         return Replace(m.node()->InputAt(0));
85       }
86       if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) {
87         return Change(node, machine()->ChangeInt32ToFloat64(), m.InputAt(0));
88       }
89       if (m.IsChangeUint32ToTagged()) {
90         return Change(node, machine()->ChangeUint32ToFloat64(), m.InputAt(0));
91       }
92       break;
93     }
94     case IrOpcode::kChangeTaggedSignedToInt32:
95     case IrOpcode::kChangeTaggedToInt32: {
96       NumberMatcher m(node->InputAt(0));
97       if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
98       if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
99         return Change(node, machine()->ChangeFloat64ToInt32(), m.InputAt(0));
100       }
101       if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) {
102         return Replace(m.InputAt(0));
103       }
104       break;
105     }
106     case IrOpcode::kChangeTaggedToUint32: {
107       NumberMatcher m(node->InputAt(0));
108       if (m.HasValue()) return ReplaceUint32(DoubleToUint32(m.Value()));
109       if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
110         return Change(node, machine()->ChangeFloat64ToUint32(), m.InputAt(0));
111       }
112       if (m.IsChangeUint32ToTagged()) return Replace(m.InputAt(0));
113       break;
114     }
115     case IrOpcode::kChangeUint32ToTagged: {
116       Uint32Matcher m(node->InputAt(0));
117       if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value()));
118       break;
119     }
120     case IrOpcode::kTruncateTaggedToWord32: {
121       NumberMatcher m(node->InputAt(0));
122       if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
123       if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged() ||
124           m.IsChangeUint32ToTagged()) {
125         return Replace(m.InputAt(0));
126       }
127       if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
128         return Change(node, machine()->TruncateFloat64ToWord32(), m.InputAt(0));
129       }
130       break;
131     }
132     case IrOpcode::kCheckedTaggedToInt32:
133     case IrOpcode::kCheckedTaggedSignedToInt32: {
134       NodeMatcher m(node->InputAt(0));
135       if (m.IsConvertTaggedHoleToUndefined()) {
136         node->ReplaceInput(0, m.InputAt(0));
137         return Changed(node);
138       }
139       break;
140     }
141     case IrOpcode::kCheckIf: {
142       HeapObjectMatcher m(node->InputAt(0));
143       if (m.Is(factory()->true_value())) {
144         Node* const effect = NodeProperties::GetEffectInput(node);
145         return Replace(effect);
146       }
147       break;
148     }
149     case IrOpcode::kCheckNumber: {
150       NodeMatcher m(node->InputAt(0));
151       if (m.IsConvertTaggedHoleToUndefined()) {
152         node->ReplaceInput(0, m.InputAt(0));
153         return Changed(node);
154       }
155       break;
156     }
157     case IrOpcode::kCheckHeapObject: {
158       Node* const input = node->InputAt(0);
159       if (DecideObjectIsSmi(input) == Decision::kFalse) {
160         ReplaceWithValue(node, input);
161         return Replace(input);
162       }
163       NodeMatcher m(input);
164       if (m.IsCheckHeapObject()) {
165         ReplaceWithValue(node, input);
166         return Replace(input);
167       }
168       break;
169     }
170     case IrOpcode::kCheckSmi: {
171       Node* const input = node->InputAt(0);
172       if (DecideObjectIsSmi(input) == Decision::kTrue) {
173         ReplaceWithValue(node, input);
174         return Replace(input);
175       }
176       NodeMatcher m(input);
177       if (m.IsCheckSmi()) {
178         ReplaceWithValue(node, input);
179         return Replace(input);
180       } else if (m.IsConvertTaggedHoleToUndefined()) {
181         node->ReplaceInput(0, m.InputAt(0));
182         return Changed(node);
183       }
184       break;
185     }
186     case IrOpcode::kObjectIsSmi: {
187       Node* const input = node->InputAt(0);
188       switch (DecideObjectIsSmi(input)) {
189         case Decision::kTrue:
190           return ReplaceBoolean(true);
191         case Decision::kFalse:
192           return ReplaceBoolean(false);
193         case Decision::kUnknown:
194           break;
195       }
196       break;
197     }
198     case IrOpcode::kNumberAbs: {
199       NumberMatcher m(node->InputAt(0));
200       if (m.HasValue()) return ReplaceNumber(std::fabs(m.Value()));
201       break;
202     }
203     case IrOpcode::kReferenceEqual: {
204       HeapObjectBinopMatcher m(node);
205       if (m.left().node() == m.right().node()) return ReplaceBoolean(true);
206       break;
207     }
208     default:
209       break;
210   }
211   return NoChange();
212 }
213 
Change(Node * node,const Operator * op,Node * a)214 Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
215                                             Node* a) {
216   DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
217   DCHECK_LE(1, node->InputCount());
218   node->ReplaceInput(0, a);
219   NodeProperties::ChangeOp(node, op);
220   return Changed(node);
221 }
222 
ReplaceBoolean(bool value)223 Reduction SimplifiedOperatorReducer::ReplaceBoolean(bool value) {
224   return Replace(jsgraph()->BooleanConstant(value));
225 }
226 
ReplaceFloat64(double value)227 Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) {
228   return Replace(jsgraph()->Float64Constant(value));
229 }
230 
231 
ReplaceInt32(int32_t value)232 Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) {
233   return Replace(jsgraph()->Int32Constant(value));
234 }
235 
236 
ReplaceNumber(double value)237 Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) {
238   return Replace(jsgraph()->Constant(value));
239 }
240 
241 
ReplaceNumber(int32_t value)242 Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) {
243   return Replace(jsgraph()->Constant(value));
244 }
245 
factory() const246 Factory* SimplifiedOperatorReducer::factory() const {
247   return isolate()->factory();
248 }
249 
graph() const250 Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); }
251 
isolate() const252 Isolate* SimplifiedOperatorReducer::isolate() const {
253   return jsgraph()->isolate();
254 }
255 
machine() const256 MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const {
257   return jsgraph()->machine();
258 }
259 
260 }  // namespace compiler
261 }  // namespace internal
262 }  // namespace v8
263