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(); | |
48 | |
49 Float64BinopMatcher m(node); | |
50 if (!m.left().IsChangeInt32ToFloat64() || | |
51 !m.right().IsChangeInt32ToFloat64()) { | |
52 return NoChange(); | |
53 } | |
54 | |
55 Type* type = NodeProperties::GetType(node); | |
56 Type::RangeType* range = type->GetRange(); | |
57 | |
58 // JavaScript has 52 bit precision in multiplication | |
59 if (range == nullptr || range->Min() < 0.0 || | |
60 range->Max() > 0xFFFFFFFFFFFFFULL) { | |
61 return NoChange(); | |
62 } | |
63 | |
64 Node* mul = graph()->NewNode(machine()->Int64Mul(), m.left().InputAt(0), | |
65 m.right().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()->RoundInt64ToFloat64(), mul); | |
77 return Replace(out); | |
78 } | |
79 | |
80 | |
81 Reduction BinaryOperatorReducer::ReduceFloat52Div(Node* node) { | |
82 if (!machine()->Is64()) return NoChange(); | |
83 | |
84 Float64BinopMatcher m(node); | |
85 if (!m.left().IsRoundInt64ToFloat64()) return NoChange(); | |
86 | |
87 // Right value should be positive... | |
88 if (!m.right().HasValue() || m.right().Value() <= 0) return NoChange(); | |
89 | |
90 // ...integer... | |
91 int64_t value = static_cast<int64_t>(m.right().Value()); | |
92 if (value != static_cast<int64_t>(m.right().Value())) return NoChange(); | |
93 | |
94 // ...and should be a power of two. | |
95 if (!base::bits::IsPowerOfTwo64(value)) return NoChange(); | |
96 | |
97 Node* left = m.left().InputAt(0); | |
98 Type::RangeType* range = NodeProperties::GetType(left)->GetRange(); | |
99 | |
100 // The result should fit into 32bit word | |
101 int64_t min = static_cast<int64_t>(range->Min()) / value; | |
102 int64_t max = static_cast<int64_t>(range->Max()) / value; | |
103 if (min < 0 || max > 0xFFFFFFFLL) { | |
104 return NoChange(); | |
105 } | |
106 | |
107 int64_t shift = WhichPowerOf2_64(static_cast<int64_t>(m.right().Value())); | |
108 | |
109 // Replace division with 64bit right shift | |
110 Node* shr = | |
111 graph()->NewNode(machine()->Word64Shr(), left, | |
112 graph()->NewNode(common()->Int64Constant(shift))); | |
113 Revisit(shr); | |
114 | |
115 Node* out = graph()->NewNode(machine()->RoundInt64ToFloat64(), shr); | |
116 return Replace(out); | |
117 } | |
118 | |
119 | |
120 Reduction BinaryOperatorReducer::ReduceTruncateFloat64ToInt32(Node* node) { | |
titzer
2015/11/09 18:53:08
Can you move this transformation to machine-operat
fedor.indutny
2015/11/09 18:57:57
Of course, done!
| |
121 Node* input = node->InputAt(0); | |
122 if (input->opcode() == IrOpcode::kRoundInt64ToFloat64) { | |
123 return Replace(input->InputAt(0)); | |
124 } | |
125 | |
126 return NoChange(); | |
127 } | |
128 | |
129 | |
130 Reduction BinaryOperatorReducer::Change(Node* node, Operator const* op, | |
131 Node* a) { | |
132 node->ReplaceInput(0, a); | |
133 node->TrimInputCount(1); | |
134 NodeProperties::ChangeOp(node, op); | |
135 return Changed(node); | |
136 } | |
137 | |
138 } // namespace compiler | |
139 } // namespace internal | |
140 } // namespace v8 | |
OLD | NEW |