blob: debe46656076048a34d4b27d3b1793e5a953840c [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);
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +000060 // The parallel move resolver algorithm does not work with register pairs.
61 DCHECK(!move->GetSource().IsPair());
62 DCHECK(!move->GetDestination().IsPair());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010063 if (!move->IsRedundant()) {
64 moves_.Add(move);
65 }
66 }
67}
68
69
70void ParallelMoveResolver::PerformMove(size_t index) {
71 // Each call to this function performs a move and deletes it from the move
72 // graph. We first recursively perform any move blocking this one. We
73 // mark a move as "pending" on entry to PerformMove in order to detect
74 // cycles in the move graph. We use operand swaps to resolve cycles,
75 // which means that a call to PerformMove could change any source operand
76 // in the move graph.
77
78 DCHECK(!moves_.Get(index)->IsPending());
79 DCHECK(!moves_.Get(index)->IsRedundant());
80
81 // Clear this move's destination to indicate a pending move. The actual
82 // destination is saved in a stack-allocated local. Recursion may allow
83 // multiple moves to be pending.
84 DCHECK(!moves_.Get(index)->GetSource().IsInvalid());
85 Location destination = moves_.Get(index)->MarkPending();
86
87 // Perform a depth-first traversal of the move graph to resolve
88 // dependencies. Any unperformed, unpending move with a source the same
89 // as this one's destination blocks this one so recursively perform all
90 // such moves.
91 for (size_t i = 0; i < moves_.Size(); ++i) {
92 const MoveOperands& other_move = *moves_.Get(i);
93 if (other_move.Blocks(destination) && !other_move.IsPending()) {
94 // Though PerformMove can change any source operand in the move graph,
95 // this call cannot create a blocking move via a swap (this loop does
96 // not miss any). Assume there is a non-blocking move with source A
97 // and this move is blocked on source B and there is a swap of A and
98 // B. Then A and B must be involved in the same cycle (or they would
99 // not be swapped). Since this move's destination is B and there is
100 // only a single incoming edge to an operand, this move must also be
101 // involved in the same cycle. In that case, the blocking move will
102 // be created but will be "pending" when we return from PerformMove.
103 PerformMove(i);
104 }
105 }
106 MoveOperands* move = moves_.Get(index);
107
108 // We are about to resolve this move and don't need it marked as
109 // pending, so restore its destination.
110 move->ClearPending(destination);
111
112 // This move's source may have changed due to swaps to resolve cycles and
113 // so it may now be the last move in the cycle. If so remove it.
114 if (move->GetSource().Equals(destination)) {
115 move->Eliminate();
116 return;
117 }
118
119 // The move may be blocked on a (at most one) pending move, in which case
120 // we have a cycle. Search for such a blocking move and perform a swap to
121 // resolve it.
122 bool do_swap = false;
123 for (size_t i = 0; i < moves_.Size(); ++i) {
124 const MoveOperands& other_move = *moves_.Get(i);
125 if (other_move.Blocks(destination)) {
126 DCHECK(other_move.IsPending());
127 do_swap = true;
128 break;
129 }
130 }
131
132 if (do_swap) {
133 EmitSwap(index);
134 // Any unperformed (including pending) move with a source of either
135 // this move's source or destination needs to have their source
136 // changed to reflect the state of affairs after the swap.
137 Location source = move->GetSource();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800138 Location swap_destination = move->GetDestination();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100139 move->Eliminate();
140 for (size_t i = 0; i < moves_.Size(); ++i) {
141 const MoveOperands& other_move = *moves_.Get(i);
142 if (other_move.Blocks(source)) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800143 moves_.Get(i)->SetSource(swap_destination);
144 } else if (other_move.Blocks(swap_destination)) {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100145 moves_.Get(i)->SetSource(source);
146 }
147 }
148 } else {
149 // This move is not blocked.
150 EmitMove(index);
151 move->Eliminate();
152 }
153}
154
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100155bool ParallelMoveResolver::IsScratchLocation(Location loc) {
156 for (size_t i = 0; i < moves_.Size(); ++i) {
157 if (moves_.Get(i)->Blocks(loc)) {
158 return false;
159 }
160 }
161
162 for (size_t i = 0; i < moves_.Size(); ++i) {
163 if (moves_.Get(i)->GetDestination().Equals(loc)) {
164 return true;
165 }
166 }
167
168 return false;
169}
170
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100171int ParallelMoveResolver::AllocateScratchRegister(int blocked,
172 int register_count,
173 int if_scratch,
174 bool* spilled) {
175 DCHECK_NE(blocked, if_scratch);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100176 int scratch = -1;
177 for (int reg = 0; reg < register_count; ++reg) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100178 if ((blocked != reg) && IsScratchLocation(Location::RegisterLocation(reg))) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100179 scratch = reg;
180 break;
181 }
182 }
183
184 if (scratch == -1) {
185 *spilled = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100186 scratch = if_scratch;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100187 } else {
188 *spilled = false;
189 }
190
191 return scratch;
192}
193
194
195ParallelMoveResolver::ScratchRegisterScope::ScratchRegisterScope(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100196 ParallelMoveResolver* resolver, int blocked, int if_scratch, int number_of_registers)
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100197 : resolver_(resolver),
198 reg_(kNoRegister),
199 spilled_(false) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100200 reg_ = resolver_->AllocateScratchRegister(blocked, number_of_registers, if_scratch, &spilled_);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100201
202 if (spilled_) {
203 resolver->SpillScratch(reg_);
204 }
205}
206
207
208ParallelMoveResolver::ScratchRegisterScope::~ScratchRegisterScope() {
209 if (spilled_) {
210 resolver_->RestoreScratch(reg_);
211 }
212}
213
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100214} // namespace art