Chromium Code Reviews| 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 | |
| 16 namespace v8 { | |
| 17 namespace internal { | |
| 18 namespace compiler { | |
| 19 | |
| 20 BinaryOperatorReducer::BinaryOperatorReducer(Editor* editor, Graph* graph, | |
| 21 CommonOperatorBuilder* common, | |
| 22 MachineOperatorBuilder* machine) | |
| 23 : AdvancedReducer(editor), | |
| 24 graph_(graph), | |
| 25 common_(common), | |
| 26 machine_(machine), | |
| 27 dead_(graph->NewNode(common->Dead())) {} | |
| 28 | |
| 29 | |
| 30 Reduction BinaryOperatorReducer::Reduce(Node* node) { | |
| 31 switch (node->opcode()) { | |
| 32 case IrOpcode::kFloat64Mul: | |
| 33 return ReduceFloat64Mul(node); | |
| 34 default: | |
| 35 break; | |
| 36 } | |
| 37 return NoChange(); | |
| 38 } | |
| 39 | |
| 40 | |
| 41 Reduction BinaryOperatorReducer::ReduceFloat64Mul(Node* node) { | |
|
titzer
2015/09/29 11:18:06
If you do this optimization at the site of the div
fedor.indutny
2015/10/02 02:54:30
On one hand - yes, on other hand - doing it at the
| |
| 42 if (node->InputAt(0)->opcode() != IrOpcode::kChangeInt32ToFloat64) { | |
| 43 return NoChange(); | |
| 44 } | |
| 45 if (node->InputAt(1)->opcode() != IrOpcode::kChangeInt32ToFloat64) { | |
| 46 return NoChange(); | |
| 47 } | |
| 48 | |
| 49 Type::RangeType* range = NodeProperties::GetType(node)->GetRange(); | |
| 50 | |
| 51 // JavaScript has 52 bit precision in multiplication | |
| 52 if (range == nullptr || range->Min() < 0.0 || | |
| 53 range->Max() > 0xFFFFFFFFFFFFFULL) { | |
| 54 return NoChange(); | |
| 55 } | |
| 56 | |
| 57 // Verify that the uses cast result to Int32 | |
| 58 for (Node* use : node->uses()) { | |
|
titzer
2015/09/29 11:18:06
Generally reductions shouldn't be iterating over t
fedor.indutny
2015/10/02 02:54:30
I'm looking both up in the tree and down in the tr
titzer
2015/10/06 23:14:10
Yes. What I meant is that reductions are applied i
| |
| 59 // XXX: How to handle this properly? | |
| 60 if (use->opcode() == IrOpcode::kStateValues) continue; | |
| 61 | |
| 62 if (use->opcode() == IrOpcode::kTruncateFloat64ToInt32) continue; | |
| 63 if (use->opcode() != IrOpcode::kFloat64Div) return NoChange(); | |
| 64 | |
| 65 // Verify division | |
| 66 // | |
| 67 // The RHS value should be positive integer | |
| 68 Float64BinopMatcher m(use); | |
| 69 if (!m.right().HasValue() || m.right().Value() <= 0) return NoChange(); | |
| 70 | |
| 71 int64_t value = static_cast<int64_t>(m.right().Value()); | |
| 72 if (value != static_cast<int64_t>(m.right().Value())) return NoChange(); | |
| 73 if (!base::bits::IsPowerOfTwo64(value)) return NoChange(); | |
| 74 | |
| 75 // The result should fit into 32bit word | |
| 76 if ((static_cast<int64_t>(range->Max()) / value) > 0xFFFFFFFULL) { | |
| 77 return NoChange(); | |
| 78 } | |
| 79 | |
| 80 // Check that uses of division are cast to Int32 | |
| 81 for (Node* subuse : use->uses()) { | |
| 82 // XXX: How to handle this properly? | |
| 83 if (subuse->opcode() == IrOpcode::kStateValues) continue; | |
| 84 | |
| 85 if (subuse->opcode() != IrOpcode::kTruncateFloat64ToInt32) { | |
| 86 return NoChange(); | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // The mul+div can be optimized | |
| 92 for (Node* use : node->uses()) { | |
| 93 // XXX: How to handle this properly? | |
| 94 if (use->opcode() == IrOpcode::kStateValues) continue; | |
| 95 | |
| 96 if (use->opcode() == IrOpcode::kTruncateFloat64ToInt32) { | |
| 97 use->set_op(machine()->TruncateInt64ToInt32()); | |
| 98 Revisit(use); | |
| 99 continue; | |
| 100 } | |
| 101 | |
| 102 Float64BinopMatcher m(use); | |
| 103 int64_t shift = WhichPowerOf2_64(static_cast<int64_t>(m.right().Value())); | |
| 104 | |
| 105 use->set_op(machine()->Word64Shr()); | |
| 106 use->ReplaceInput(1, graph()->NewNode(common()->Int64Constant(shift))); | |
| 107 Revisit(use); | |
| 108 | |
| 109 for (Node* subuse : use->uses()) { | |
| 110 // XXX: How to handle this properly? | |
| 111 if (subuse->opcode() == IrOpcode::kStateValues) continue; | |
| 112 | |
| 113 subuse->set_op(machine()->TruncateInt64ToInt32()); | |
| 114 Revisit(subuse); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 return Change(node, machine()->Int64Mul(), node->InputAt(0)->InputAt(0), | |
| 119 node->InputAt(1)->InputAt(0)); | |
|
titzer
2015/09/29 11:21:59
TurboFan also only supports Int64Mul on 64-bit pla
fedor.indutny
2015/10/02 02:54:30
What would be the best way to make this a platform
titzer
2015/10/06 23:14:10
Just check machine()->Is64()
| |
| 120 } | |
| 121 | |
| 122 | |
| 123 Reduction BinaryOperatorReducer::Change(Node* node, Operator const* op, | |
| 124 Node* a) { | |
| 125 node->set_op(op); | |
| 126 node->ReplaceInput(0, a); | |
| 127 node->TrimInputCount(1); | |
| 128 return Changed(node); | |
| 129 } | |
| 130 | |
| 131 | |
| 132 Reduction BinaryOperatorReducer::Change(Node* node, Operator const* op, Node* a, | |
| 133 Node* b) { | |
| 134 node->set_op(op); | |
| 135 node->ReplaceInput(0, a); | |
| 136 node->ReplaceInput(1, b); | |
| 137 node->TrimInputCount(2); | |
| 138 return Changed(node); | |
| 139 } | |
| 140 | |
| 141 } // namespace compiler | |
| 142 } // namespace internal | |
| 143 } // namespace v8 | |
| OLD | NEW |