| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/gap-resolver.h" | 5 #include "src/compiler/gap-resolver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 namespace compiler { | 13 namespace compiler { |
| 14 | 14 |
| 15 typedef ZoneList<MoveOperands>::iterator op_iterator; | 15 namespace { |
| 16 | 16 |
| 17 #ifdef ENABLE_SLOW_DCHECKS | 17 inline bool Blocks(MoveOperands* move, InstructionOperand destination) { |
| 18 struct InstructionOperandComparator { | 18 return move->Blocks(destination); |
| 19 bool operator()(const InstructionOperand* x, | |
| 20 const InstructionOperand* y) const { | |
| 21 return *x < *y; | |
| 22 } | |
| 23 }; | |
| 24 #endif | |
| 25 | |
| 26 // No operand should be the destination for more than one move. | |
| 27 static void VerifyMovesAreInjective(ZoneList<MoveOperands>* moves) { | |
| 28 #ifdef ENABLE_SLOW_DCHECKS | |
| 29 std::set<InstructionOperand*, InstructionOperandComparator> seen; | |
| 30 for (op_iterator i = moves->begin(); i != moves->end(); ++i) { | |
| 31 SLOW_DCHECK(seen.find(i->destination()) == seen.end()); | |
| 32 seen.insert(i->destination()); | |
| 33 } | |
| 34 #endif | |
| 35 } | 19 } |
| 36 | 20 |
| 37 | 21 |
| 38 void GapResolver::Resolve(ParallelMove* parallel_move) const { | 22 inline bool IsRedundant(MoveOperands* move) { return move->IsRedundant(); } |
| 39 ZoneList<MoveOperands>* moves = parallel_move->move_operands(); | |
| 40 // TODO(svenpanne) Use the member version of remove_if when we use real lists. | |
| 41 op_iterator end = | |
| 42 std::remove_if(moves->begin(), moves->end(), | |
| 43 std::mem_fun_ref(&MoveOperands::IsRedundant)); | |
| 44 moves->Rewind(static_cast<int>(end - moves->begin())); | |
| 45 | 23 |
| 46 VerifyMovesAreInjective(moves); | 24 } // namespace |
| 47 | 25 |
| 48 for (op_iterator move = moves->begin(); move != moves->end(); ++move) { | 26 |
| 49 if (!move->IsEliminated()) PerformMove(moves, &*move); | 27 void GapResolver::Resolve(ParallelMove* moves) const { |
| 28 // Clear redundant moves. |
| 29 auto it = |
| 30 std::remove_if(moves->begin(), moves->end(), std::ptr_fun(IsRedundant)); |
| 31 moves->erase(it, moves->end()); |
| 32 for (auto move : *moves) { |
| 33 if (!move->IsEliminated()) PerformMove(moves, move); |
| 50 } | 34 } |
| 51 } | 35 } |
| 52 | 36 |
| 53 | 37 |
| 54 void GapResolver::PerformMove(ZoneList<MoveOperands>* moves, | 38 void GapResolver::PerformMove(ParallelMove* moves, MoveOperands* move) const { |
| 55 MoveOperands* move) const { | |
| 56 // Each call to this function performs a move and deletes it from the move | 39 // Each call to this function performs a move and deletes it from the move |
| 57 // graph. We first recursively perform any move blocking this one. We mark a | 40 // graph. We first recursively perform any move blocking this one. We mark a |
| 58 // move as "pending" on entry to PerformMove in order to detect cycles in the | 41 // move as "pending" on entry to PerformMove in order to detect cycles in the |
| 59 // move graph. We use operand swaps to resolve cycles, which means that a | 42 // move graph. We use operand swaps to resolve cycles, which means that a |
| 60 // call to PerformMove could change any source operand in the move graph. | 43 // call to PerformMove could change any source operand in the move graph. |
| 61 DCHECK(!move->IsPending()); | 44 DCHECK(!move->IsPending()); |
| 62 DCHECK(!move->IsRedundant()); | 45 DCHECK(!move->IsRedundant()); |
| 63 | 46 |
| 64 // Clear this move's destination to indicate a pending move. The actual | 47 // Clear this move's destination to indicate a pending move. The actual |
| 65 // destination is saved on the side. | 48 // destination is saved on the side. |
| 66 DCHECK_NOT_NULL(move->source()); // Or else it will look eliminated. | 49 DCHECK(!move->source().IsInvalid()); // Or else it will look eliminated. |
| 67 InstructionOperand* destination = move->destination(); | 50 InstructionOperand destination = move->destination(); |
| 68 move->set_destination(NULL); | 51 move->SetPending(); |
| 69 | 52 |
| 70 // Perform a depth-first traversal of the move graph to resolve dependencies. | 53 // Perform a depth-first traversal of the move graph to resolve dependencies. |
| 71 // Any unperformed, unpending move with a source the same as this one's | 54 // Any unperformed, unpending move with a source the same as this one's |
| 72 // destination blocks this one so recursively perform all such moves. | 55 // destination blocks this one so recursively perform all such moves. |
| 73 for (op_iterator other = moves->begin(); other != moves->end(); ++other) { | 56 for (auto other : *moves) { |
| 74 if (other->Blocks(destination) && !other->IsPending()) { | 57 if (other->Blocks(destination) && !other->IsPending()) { |
| 75 // Though PerformMove can change any source operand in the move graph, | 58 // Though PerformMove can change any source operand in the move graph, |
| 76 // this call cannot create a blocking move via a swap (this loop does not | 59 // this call cannot create a blocking move via a swap (this loop does not |
| 77 // miss any). Assume there is a non-blocking move with source A and this | 60 // miss any). Assume there is a non-blocking move with source A and this |
| 78 // move is blocked on source B and there is a swap of A and B. Then A and | 61 // move is blocked on source B and there is a swap of A and B. Then A and |
| 79 // B must be involved in the same cycle (or they would not be swapped). | 62 // B must be involved in the same cycle (or they would not be swapped). |
| 80 // Since this move's destination is B and there is only a single incoming | 63 // Since this move's destination is B and there is only a single incoming |
| 81 // edge to an operand, this move must also be involved in the same cycle. | 64 // edge to an operand, this move must also be involved in the same cycle. |
| 82 // In that case, the blocking move will be created but will be "pending" | 65 // In that case, the blocking move will be created but will be "pending" |
| 83 // when we return from PerformMove. | 66 // when we return from PerformMove. |
| 84 PerformMove(moves, other); | 67 PerformMove(moves, other); |
| 85 } | 68 } |
| 86 } | 69 } |
| 87 | 70 |
| 88 // We are about to resolve this move and don't need it marked as pending, so | 71 // We are about to resolve this move and don't need it marked as pending, so |
| 89 // restore its destination. | 72 // restore its destination. |
| 90 move->set_destination(destination); | 73 move->set_destination(destination); |
| 91 | 74 |
| 92 // This move's source may have changed due to swaps to resolve cycles and so | 75 // This move's source may have changed due to swaps to resolve cycles and so |
| 93 // it may now be the last move in the cycle. If so remove it. | 76 // it may now be the last move in the cycle. If so remove it. |
| 94 InstructionOperand* source = move->source(); | 77 InstructionOperand source = move->source(); |
| 95 if (source->Equals(destination)) { | 78 if (source == destination) { |
| 96 move->Eliminate(); | 79 move->Eliminate(); |
| 97 return; | 80 return; |
| 98 } | 81 } |
| 99 | 82 |
| 100 // The move may be blocked on a (at most one) pending move, in which case we | 83 // The move may be blocked on a (at most one) pending move, in which case we |
| 101 // have a cycle. Search for such a blocking move and perform a swap to | 84 // have a cycle. Search for such a blocking move and perform a swap to |
| 102 // resolve it. | 85 // resolve it. |
| 103 op_iterator blocker = std::find_if( | 86 auto blocker = std::find_if(moves->begin(), moves->end(), |
| 104 moves->begin(), moves->end(), | 87 std::bind2nd(std::ptr_fun(&Blocks), destination)); |
| 105 std::bind2nd(std::mem_fun_ref(&MoveOperands::Blocks), destination)); | |
| 106 if (blocker == moves->end()) { | 88 if (blocker == moves->end()) { |
| 107 // The easy case: This move is not blocked. | 89 // The easy case: This move is not blocked. |
| 108 assembler_->AssembleMove(source, destination); | 90 assembler_->AssembleMove(&source, &destination); |
| 109 move->Eliminate(); | 91 move->Eliminate(); |
| 110 return; | 92 return; |
| 111 } | 93 } |
| 112 | 94 |
| 113 DCHECK(blocker->IsPending()); | 95 DCHECK((*blocker)->IsPending()); |
| 114 // Ensure source is a register or both are stack slots, to limit swap cases. | 96 // Ensure source is a register or both are stack slots, to limit swap cases. |
| 115 if (source->IsStackSlot() || source->IsDoubleStackSlot()) { | 97 if (source.IsStackSlot() || source.IsDoubleStackSlot()) { |
| 116 std::swap(source, destination); | 98 std::swap(source, destination); |
| 117 } | 99 } |
| 118 assembler_->AssembleSwap(source, destination); | 100 assembler_->AssembleSwap(&source, &destination); |
| 119 move->Eliminate(); | 101 move->Eliminate(); |
| 120 | 102 |
| 121 // Any unperformed (including pending) move with a source of either this | 103 // Any unperformed (including pending) move with a source of either this |
| 122 // move's source or destination needs to have their source changed to | 104 // move's source or destination needs to have their source changed to |
| 123 // reflect the state of affairs after the swap. | 105 // reflect the state of affairs after the swap. |
| 124 for (op_iterator other = moves->begin(); other != moves->end(); ++other) { | 106 for (auto other : *moves) { |
| 125 if (other->Blocks(source)) { | 107 if (other->Blocks(source)) { |
| 126 other->set_source(destination); | 108 other->set_source(destination); |
| 127 } else if (other->Blocks(destination)) { | 109 } else if (other->Blocks(destination)) { |
| 128 other->set_source(source); | 110 other->set_source(source); |
| 129 } | 111 } |
| 130 } | 112 } |
| 131 } | 113 } |
| 132 } | 114 } |
| 133 } | 115 } |
| 134 } // namespace v8::internal::compiler | 116 } // namespace v8::internal::compiler |
| OLD | NEW |