OLD | NEW |
| (Empty) |
1 // Copyright 2015 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/binary-operator-reducer.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "src/compiler/common-operator.h" | |
10 #include "src/compiler/graph.h" | |
11 #include "src/compiler/machine-operator.h" | |
12 #include "src/compiler/node.h" | |
13 #include "src/compiler/node-matchers.h" | |
14 #include "src/compiler/node-properties.h" | |
15 #include "src/types-inl.h" | |
16 | |
17 namespace v8 { | |
18 namespace internal { | |
19 namespace compiler { | |
20 | |
21 BinaryOperatorReducer::BinaryOperatorReducer(Editor* editor, Graph* graph, | |
22 CommonOperatorBuilder* common, | |
23 MachineOperatorBuilder* machine) | |
24 : AdvancedReducer(editor), | |
25 graph_(graph), | |
26 common_(common), | |
27 machine_(machine), | |
28 dead_(graph->NewNode(common->Dead())) {} | |
29 | |
30 | |
31 Reduction BinaryOperatorReducer::Reduce(Node* node) { | |
32 switch (node->opcode()) { | |
33 case IrOpcode::kFloat64Mul: | |
34 return ReduceFloat52Mul(node); | |
35 case IrOpcode::kFloat64Div: | |
36 return ReduceFloat52Div(node); | |
37 default: | |
38 break; | |
39 } | |
40 return NoChange(); | |
41 } | |
42 | |
43 | |
44 Reduction BinaryOperatorReducer::ReduceFloat52Mul(Node* node) { | |
45 if (!machine()->Is64()) return NoChange(); | |
46 | |
47 Float64BinopMatcher m(node); | |
48 if (!m.left().IsChangeInt32ToFloat64() || | |
49 !m.right().IsChangeInt32ToFloat64()) { | |
50 return NoChange(); | |
51 } | |
52 | |
53 Type* type = NodeProperties::GetType(node); | |
54 Type::RangeType* range = type->GetRange(); | |
55 | |
56 // JavaScript has 52 bit precision in multiplication | |
57 if (range == nullptr || range->Min() < 0.0 || | |
58 range->Max() > 0xFFFFFFFFFFFFFULL) { | |
59 return NoChange(); | |
60 } | |
61 | |
62 Node* mul = graph()->NewNode(machine()->Int64Mul(), m.left().InputAt(0), | |
63 m.right().InputAt(0)); | |
64 Revisit(mul); | |
65 | |
66 Type* range_type = Type::Range(range->Min(), range->Max(), graph()->zone()); | |
67 | |
68 // TODO(indutny): Is Type::Number() a proper thing here? It looks like | |
69 // every other place is using Type:Internal() for int64 values. | |
70 // Should we off-load range propagation to Typer? | |
71 NodeProperties::SetType( | |
72 mul, Type::Intersect(range_type, Type::Number(), graph()->zone())); | |
73 | |
74 Node* out = graph()->NewNode(machine()->RoundInt64ToFloat64(), mul); | |
75 return Replace(out); | |
76 } | |
77 | |
78 | |
79 Reduction BinaryOperatorReducer::ReduceFloat52Div(Node* node) { | |
80 if (!machine()->Is64()) return NoChange(); | |
81 | |
82 Float64BinopMatcher m(node); | |
83 if (!m.left().IsRoundInt64ToFloat64()) return NoChange(); | |
84 | |
85 // Right value should be positive... | |
86 if (!m.right().HasValue() || m.right().Value() <= 0) return NoChange(); | |
87 | |
88 // ...integer... | |
89 int64_t value = static_cast<int64_t>(m.right().Value()); | |
90 if (value != static_cast<int64_t>(m.right().Value())) return NoChange(); | |
91 | |
92 // ...and should be a power of two. | |
93 if (!base::bits::IsPowerOfTwo64(value)) return NoChange(); | |
94 | |
95 Node* left = m.left().InputAt(0); | |
96 Type::RangeType* range = NodeProperties::GetType(left)->GetRange(); | |
97 | |
98 // The result should fit into 32bit word | |
99 int64_t min = static_cast<int64_t>(range->Min()) / value; | |
100 int64_t max = static_cast<int64_t>(range->Max()) / value; | |
101 if (min < 0 || max > 0xFFFFFFFLL) { | |
102 return NoChange(); | |
103 } | |
104 | |
105 int64_t shift = WhichPowerOf2_64(static_cast<int64_t>(m.right().Value())); | |
106 | |
107 // Replace division with 64bit right shift | |
108 Node* shr = | |
109 graph()->NewNode(machine()->Word64Shr(), left, | |
110 graph()->NewNode(common()->Int64Constant(shift))); | |
111 Revisit(shr); | |
112 | |
113 Node* out = graph()->NewNode(machine()->RoundInt64ToFloat64(), shr); | |
114 return Replace(out); | |
115 } | |
116 | |
117 | |
118 Reduction BinaryOperatorReducer::Change(Node* node, Operator const* op, | |
119 Node* a) { | |
120 node->ReplaceInput(0, a); | |
121 node->TrimInputCount(1); | |
122 NodeProperties::ChangeOp(node, op); | |
123 return Changed(node); | |
124 } | |
125 | |
126 } // namespace compiler | |
127 } // namespace internal | |
128 } // namespace v8 | |
OLD | NEW |