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 case IrOpcode::kTruncateFloat64ToInt32: | |
38 return ReduceTruncateFloat64ToInt32(node); | |
39 default: | |
40 break; | |
41 } | |
42 return NoChange(); | |
43 } | |
44 | |
45 | |
46 Reduction BinaryOperatorReducer::ReduceFloat52Mul(Node* node) { | |
47 if (!machine()->Is64()) return NoChange(); | |
titzer
2015/10/16 18:44:06
You can use a BinopMatcher here to make this a bit
fedor.indutny
2015/10/16 22:30:38
I wasn't able to use BinopMatcher, but I have decl
| |
48 | |
49 if (node->InputAt(0)->opcode() != IrOpcode::kChangeInt32ToFloat64 || | |
50 node->InputAt(1)->opcode() != IrOpcode::kChangeInt32ToFloat64) { | |
titzer
2015/10/16 18:44:06
My earlier comment about using the range types app
fedor.indutny
2015/10/16 18:52:35
But the idea here is that the original values were
| |
51 return NoChange(); | |
52 } | |
53 | |
54 Type* type = NodeProperties::GetType(node); | |
55 Type::RangeType* range = type->GetRange(); | |
56 | |
57 // JavaScript has 52 bit precision in multiplication | |
58 if (range == nullptr || range->Min() < 0.0 || | |
59 range->Max() > 0xFFFFFFFFFFFFFULL) { | |
60 return NoChange(); | |
61 } | |
62 | |
63 Node* mul = | |
64 graph()->NewNode(machine()->Int64Mul(), node->InputAt(0)->InputAt(0), | |
65 node->InputAt(1)->InputAt(0)); | |
66 Revisit(mul); | |
67 | |
68 Type* range_type = Type::Range(range->Min(), range->Max(), graph()->zone()); | |
69 | |
70 // TODO(indutny): Is Type::Number() a proper thing here? It looks like | |
71 // every other place is using Type:Internal() for int64 values. | |
72 // Should we off-load range propagation to Typer? | |
73 NodeProperties::SetType( | |
74 mul, Type::Intersect(range_type, Type::Number(), graph()->zone())); | |
75 | |
76 Node* out = graph()->NewNode(machine()->TruncateInt64ToFloat64(), mul); | |
77 Revisit(out); | |
titzer
2015/10/16 18:44:06
No need for revisits; the graph reducer takes care
fedor.indutny
2015/10/16 18:52:35
Acknowledged.
| |
78 return Replace(out); | |
79 } | |
80 | |
81 | |
82 Reduction BinaryOperatorReducer::ReduceFloat52Div(Node* node) { | |
titzer
2015/10/16 18:44:06
The optimization here with division is only sound
fedor.indutny
2015/10/16 18:52:35
Hm... Not sure I understand why. I have modeled it
titzer
2015/10/16 19:51:54
Because the fractional bits would be lost. Which i
fedor.indutny
2015/10/16 19:54:19
Oooh, this makes sense! However this is not going
| |
83 if (!machine()->Is64()) return NoChange(); | |
84 if (node->InputAt(0)->opcode() != IrOpcode::kTruncateInt64ToFloat64) { | |
85 return NoChange(); | |
86 } | |
87 | |
88 Float64BinopMatcher m(node); | |
89 | |
90 // Right value should be positive... | |
91 if (!m.right().HasValue() || m.right().Value() <= 0) return NoChange(); | |
92 | |
93 // ...integer... | |
94 int64_t value = static_cast<int64_t>(m.right().Value()); | |
95 if (value != static_cast<int64_t>(m.right().Value())) return NoChange(); | |
96 | |
97 // ...and should be a power of two. | |
98 if (!base::bits::IsPowerOfTwo64(value)) return NoChange(); | |
99 | |
100 Node* left = node->InputAt(0)->InputAt(0); | |
101 Type::RangeType* range = NodeProperties::GetType(left)->GetRange(); | |
102 | |
103 // The result should fit into 32bit word | |
104 if ((static_cast<int64_t>(range->Max()) / value) > 0xFFFFFFFULL) { | |
105 return NoChange(); | |
106 } | |
107 | |
108 int64_t shift = WhichPowerOf2_64(static_cast<int64_t>(m.right().Value())); | |
109 | |
110 // Replace division with 64bit right shift | |
111 Node* shr = | |
112 graph()->NewNode(machine()->Word64Shr(), left, | |
113 graph()->NewNode(common()->Int64Constant(shift))); | |
114 Revisit(shr); | |
115 | |
116 Node* out = graph()->NewNode(machine()->TruncateInt64ToFloat64(), shr); | |
117 Revisit(out); | |
118 return Replace(out); | |
119 } | |
120 | |
121 | |
122 Reduction BinaryOperatorReducer::ReduceTruncateFloat64ToInt32(Node* node) { | |
123 Node* input = node->InputAt(0); | |
124 if (input->opcode() != IrOpcode::kTruncateInt64ToFloat64) return NoChange(); | |
125 | |
126 return Replace(input->InputAt(0)); | |
127 } | |
128 | |
129 | |
130 Reduction BinaryOperatorReducer::Change(Node* node, Operator const* op, | |
131 Node* a) { | |
132 node->set_op(op); | |
133 node->ReplaceInput(0, a); | |
134 node->TrimInputCount(1); | |
135 return Changed(node); | |
136 } | |
137 | |
138 } // namespace compiler | |
139 } // namespace internal | |
140 } // namespace v8 | |
OLD | NEW |