blob: b8f50709999cf880ee9a3db04ef25f57c529f86a [file] [log] [blame]
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "parallel_move_resolver.h"
18#include "nodes.h"
19#include "locations.h"
20
21namespace art {
22
23void ParallelMoveResolver::EmitNativeCode(HParallelMove* parallel_move) {
24 DCHECK(moves_.IsEmpty());
25 // Build up a worklist of moves.
26 BuildInitialMoveList(parallel_move);
27
28 for (size_t i = 0; i < moves_.Size(); ++i) {
29 const MoveOperands& move = *moves_.Get(i);
30 // Skip constants to perform them last. They don't block other moves
31 // and skipping such moves with register destinations keeps those
32 // registers free for the whole algorithm.
33 if (!move.IsEliminated() && !move.GetSource().IsConstant()) {
34 PerformMove(i);
35 }
36 }
37
38 // Perform the moves with constant sources.
39 for (size_t i = 0; i < moves_.Size(); ++i) {
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000040 MoveOperands* move = moves_.Get(i);
41 if (!move->IsEliminated()) {
42 DCHECK(move->GetSource().IsConstant());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010043 EmitMove(i);
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000044 // Eliminate the move, in case following moves need a scratch register.
45 move->Eliminate();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010046 }
47 }
48
49 moves_.Reset();
50}
51
52
53void ParallelMoveResolver::BuildInitialMoveList(HParallelMove* parallel_move) {
54 // Perform a linear sweep of the moves to add them to the initial list of
55 // moves to perform, ignoring any move that is redundant (the source is
56 // the same as the destination, the destination is ignored and
57 // unallocated, or the move was already eliminated).
58 for (size_t i = 0; i < parallel_move->NumMoves(); ++i) {
59 MoveOperands* move = parallel_move->MoveOperandsAt(i);
60 if (!move->IsRedundant()) {
61 moves_.Add(move);
62 }
63 }
64}
65
66
67void ParallelMoveResolver::PerformMove(size_t index) {
68 // Each call to this function performs a move and deletes it from the move
69 // graph. We first recursively perform any move blocking this one. We
70 // mark a move as "pending" on entry to PerformMove in order to detect
71 // cycles in the move graph. We use operand swaps to resolve cycles,
72 // which means that a call to PerformMove could change any source operand
73 // in the move graph.
74
75 DCHECK(!moves_.Get(index)->IsPending());
76 DCHECK(!moves_.Get(index)->IsRedundant());
77
78 // Clear this move's destination to indicate a pending move. The actual
79 // destination is saved in a stack-allocated local. Recursion may allow
80 // multiple moves to be pending.
81 DCHECK(!moves_.Get(index)->GetSource().IsInvalid());
82 Location destination = moves_.Get(index)->MarkPending();
83
84 // Perform a depth-first traversal of the move graph to resolve
85 // dependencies. Any unperformed, unpending move with a source the same
86 // as this one's destination blocks this one so recursively perform all
87 // such moves.
88 for (size_t i = 0; i < moves_.Size(); ++i) {
89 const MoveOperands& other_move = *moves_.Get(i);
90 if (other_move.Blocks(destination) && !other_move.IsPending()) {
91 // Though PerformMove can change any source operand in the move graph,
92 // this call cannot create a blocking move via a swap (this loop does
93 // not miss any). Assume there is a non-blocking move with source A
94 // and this move is blocked on source B and there is a swap of A and
95 // B. Then A and B must be involved in the same cycle (or they would
96 // not be swapped). Since this move's destination is B and there is
97 // only a single incoming edge to an operand, this move must also be
98 // involved in the same cycle. In that case, the blocking move will
99 // be created but will be "pending" when we return from PerformMove.
100 PerformMove(i);
101 }
102 }
103 MoveOperands* move = moves_.Get(index);
104
105 // We are about to resolve this move and don't need it marked as
106 // pending, so restore its destination.
107 move->ClearPending(destination);
108
109 // This move's source may have changed due to swaps to resolve cycles and
110 // so it may now be the last move in the cycle. If so remove it.
111 if (move->GetSource().Equals(destination)) {
112 move->Eliminate();
113 return;
114 }
115
116 // The move may be blocked on a (at most one) pending move, in which case
117 // we have a cycle. Search for such a blocking move and perform a swap to
118 // resolve it.
119 bool do_swap = false;
120 for (size_t i = 0; i < moves_.Size(); ++i) {
121 const MoveOperands& other_move = *moves_.Get(i);
122 if (other_move.Blocks(destination)) {
123 DCHECK(other_move.IsPending());
124 do_swap = true;
125 break;
126 }
127 }
128
129 if (do_swap) {
130 EmitSwap(index);
131 // Any unperformed (including pending) move with a source of either
132 // this move's source or destination needs to have their source
133 // changed to reflect the state of affairs after the swap.
134 Location source = move->GetSource();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800135 Location swap_destination = move->GetDestination();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100136 move->Eliminate();
137 for (size_t i = 0; i < moves_.Size(); ++i) {
138 const MoveOperands& other_move = *moves_.Get(i);
139 if (other_move.Blocks(source)) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800140 moves_.Get(i)->SetSource(swap_destination);
141 } else if (other_move.Blocks(swap_destination)) {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100142 moves_.Get(i)->SetSource(source);
143 }
144 }
145 } else {
146 // This move is not blocked.
147 EmitMove(index);
148 move->Eliminate();
149 }
150}
151
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100152bool ParallelMoveResolver::IsScratchLocation(Location loc) {
153 for (size_t i = 0; i < moves_.Size(); ++i) {
154 if (moves_.Get(i)->Blocks(loc)) {
155 return false;
156 }
157 }
158
159 for (size_t i = 0; i < moves_.Size(); ++i) {
160 if (moves_.Get(i)->GetDestination().Equals(loc)) {
161 return true;
162 }
163 }
164
165 return false;
166}
167
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100168int ParallelMoveResolver::AllocateScratchRegister(int blocked,
169 int register_count,
170 int if_scratch,
171 bool* spilled) {
172 DCHECK_NE(blocked, if_scratch);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100173 int scratch = -1;
174 for (int reg = 0; reg < register_count; ++reg) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100175 if ((blocked != reg) && IsScratchLocation(Location::RegisterLocation(reg))) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100176 scratch = reg;
177 break;
178 }
179 }
180
181 if (scratch == -1) {
182 *spilled = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100183 scratch = if_scratch;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100184 } else {
185 *spilled = false;
186 }
187
188 return scratch;
189}
190
191
192ParallelMoveResolver::ScratchRegisterScope::ScratchRegisterScope(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100193 ParallelMoveResolver* resolver, int blocked, int if_scratch, int number_of_registers)
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100194 : resolver_(resolver),
195 reg_(kNoRegister),
196 spilled_(false) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100197 reg_ = resolver_->AllocateScratchRegister(blocked, number_of_registers, if_scratch, &spilled_);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100198
199 if (spilled_) {
200 resolver->SpillScratch(reg_);
201 }
202}
203
204
205ParallelMoveResolver::ScratchRegisterScope::~ScratchRegisterScope() {
206 if (spilled_) {
207 resolver_->RestoreScratch(reg_);
208 }
209}
210
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100211} // namespace art