blob: 30bcf19c646fdccbbede6e88cf255baf2ae8ef1a [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 */
Nicolas Geoffraya2d15b52015-03-31 18:13:51 +010016#include <iostream>
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010017
18#include "parallel_move_resolver.h"
Vladimir Marko225b6462015-09-28 12:17:40 +010019
20#include "base/stl_util.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "nodes.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010022
23namespace art {
24
Zheng Xuad4450e2015-04-17 18:48:56 +080025void ParallelMoveResolver::BuildInitialMoveList(HParallelMove* parallel_move) {
26 // Perform a linear sweep of the moves to add them to the initial list of
27 // moves to perform, ignoring any move that is redundant (the source is
28 // the same as the destination, the destination is ignored and
29 // unallocated, or the move was already eliminated).
30 for (size_t i = 0; i < parallel_move->NumMoves(); ++i) {
31 MoveOperands* move = parallel_move->MoveOperandsAt(i);
32 if (!move->IsRedundant()) {
Vladimir Marko225b6462015-09-28 12:17:40 +010033 moves_.push_back(move);
Zheng Xuad4450e2015-04-17 18:48:56 +080034 }
35 }
36}
37
38void ParallelMoveResolverWithSwap::EmitNativeCode(HParallelMove* parallel_move) {
Vladimir Marko225b6462015-09-28 12:17:40 +010039 DCHECK(moves_.empty());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040 // Build up a worklist of moves.
41 BuildInitialMoveList(parallel_move);
42
Mark Mendell6e18dcb2015-07-28 17:26:55 -040043 // Move stack/stack slot to take advantage of a free register on constrained machines.
Vladimir Marko225b6462015-09-28 12:17:40 +010044 for (size_t i = 0; i < moves_.size(); ++i) {
45 const MoveOperands& move = *moves_[i];
Mark Mendell6e18dcb2015-07-28 17:26:55 -040046 // Ignore constants and moves already eliminated.
47 if (move.IsEliminated() || move.GetSource().IsConstant()) {
48 continue;
49 }
50
51 if ((move.GetSource().IsStackSlot() || move.GetSource().IsDoubleStackSlot()) &&
52 (move.GetDestination().IsStackSlot() || move.GetDestination().IsDoubleStackSlot())) {
53 PerformMove(i);
54 }
55 }
56
Vladimir Marko225b6462015-09-28 12:17:40 +010057 for (size_t i = 0; i < moves_.size(); ++i) {
58 const MoveOperands& move = *moves_[i];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010059 // Skip constants to perform them last. They don't block other moves
60 // and skipping such moves with register destinations keeps those
61 // registers free for the whole algorithm.
62 if (!move.IsEliminated() && !move.GetSource().IsConstant()) {
63 PerformMove(i);
64 }
65 }
66
67 // Perform the moves with constant sources.
Vladimir Marko225b6462015-09-28 12:17:40 +010068 for (size_t i = 0; i < moves_.size(); ++i) {
69 MoveOperands* move = moves_[i];
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000070 if (!move->IsEliminated()) {
71 DCHECK(move->GetSource().IsConstant());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010072 EmitMove(i);
Nicolas Geoffray48c310c2015-01-14 10:45:05 +000073 // Eliminate the move, in case following moves need a scratch register.
74 move->Eliminate();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010075 }
76 }
77
Vladimir Marko225b6462015-09-28 12:17:40 +010078 moves_.clear();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010079}
80
Nicolas Geoffraya2d15b52015-03-31 18:13:51 +010081Location LowOf(Location location) {
82 if (location.IsRegisterPair()) {
83 return Location::RegisterLocation(location.low());
84 } else if (location.IsFpuRegisterPair()) {
85 return Location::FpuRegisterLocation(location.low());
86 } else if (location.IsDoubleStackSlot()) {
87 return Location::StackSlot(location.GetStackIndex());
88 } else {
89 return Location::NoLocation();
90 }
91}
92
93Location HighOf(Location location) {
94 if (location.IsRegisterPair()) {
95 return Location::RegisterLocation(location.high());
96 } else if (location.IsFpuRegisterPair()) {
97 return Location::FpuRegisterLocation(location.high());
98 } else if (location.IsDoubleStackSlot()) {
99 return Location::StackSlot(location.GetHighStackIndex(4));
100 } else {
101 return Location::NoLocation();
102 }
103}
104
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000105// Update the source of `move`, knowing that `updated_location` has been swapped
106// with `new_source`. Note that `updated_location` can be a pair, therefore if
107// `move` is non-pair, we need to extract which register to use.
108static void UpdateSourceOf(MoveOperands* move, Location updated_location, Location new_source) {
109 Location source = move->GetSource();
Nicolas Geoffraya2d15b52015-03-31 18:13:51 +0100110 if (LowOf(updated_location).Equals(source)) {
111 move->SetSource(LowOf(new_source));
112 } else if (HighOf(updated_location).Equals(source)) {
113 move->SetSource(HighOf(new_source));
114 } else {
115 DCHECK(updated_location.Equals(source)) << updated_location << " " << source;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000116 move->SetSource(new_source);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000117 }
118}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100119
Zheng Xuad4450e2015-04-17 18:48:56 +0800120MoveOperands* ParallelMoveResolverWithSwap::PerformMove(size_t index) {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100121 // Each call to this function performs a move and deletes it from the move
122 // graph. We first recursively perform any move blocking this one. We
123 // mark a move as "pending" on entry to PerformMove in order to detect
124 // cycles in the move graph. We use operand swaps to resolve cycles,
125 // which means that a call to PerformMove could change any source operand
126 // in the move graph.
127
Vladimir Marko225b6462015-09-28 12:17:40 +0100128 MoveOperands* move = moves_[index];
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000129 DCHECK(!move->IsPending());
130 if (move->IsRedundant()) {
131 // Because we swap register pairs first, following, un-pending
132 // moves may become redundant.
133 move->Eliminate();
134 return nullptr;
135 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100136
137 // Clear this move's destination to indicate a pending move. The actual
138 // destination is saved in a stack-allocated local. Recursion may allow
139 // multiple moves to be pending.
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000140 DCHECK(!move->GetSource().IsInvalid());
141 Location destination = move->MarkPending();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100142
143 // Perform a depth-first traversal of the move graph to resolve
144 // dependencies. Any unperformed, unpending move with a source the same
145 // as this one's destination blocks this one so recursively perform all
146 // such moves.
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000147 MoveOperands* required_swap = nullptr;
Vladimir Marko225b6462015-09-28 12:17:40 +0100148 for (size_t i = 0; i < moves_.size(); ++i) {
149 const MoveOperands& other_move = *moves_[i];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100150 if (other_move.Blocks(destination) && !other_move.IsPending()) {
151 // Though PerformMove can change any source operand in the move graph,
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000152 // calling `PerformMove` cannot create a blocking move via a swap
153 // (this loop does not miss any).
154 // For example, assume there is a non-blocking move with source A
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100155 // and this move is blocked on source B and there is a swap of A and
156 // B. Then A and B must be involved in the same cycle (or they would
157 // not be swapped). Since this move's destination is B and there is
158 // only a single incoming edge to an operand, this move must also be
159 // involved in the same cycle. In that case, the blocking move will
160 // be created but will be "pending" when we return from PerformMove.
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000161 required_swap = PerformMove(i);
162
163 if (required_swap == move) {
164 // If this move is required to swap, we do so without looking
165 // at the next moves. Swapping is not blocked by anything, it just
166 // updates other moves's source.
167 break;
Vladimir Marko225b6462015-09-28 12:17:40 +0100168 } else if (required_swap == moves_[i]) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000169 // If `other_move` was swapped, we iterate again to find a new
170 // potential cycle.
171 required_swap = nullptr;
172 i = 0;
173 } else if (required_swap != nullptr) {
174 // A move is required to swap. We walk back the cycle to find the
175 // move by just returning from this `PerforrmMove`.
Vladimir Marko225b6462015-09-28 12:17:40 +0100176 moves_[index]->ClearPending(destination);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000177 return required_swap;
178 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100179 }
180 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100181
182 // We are about to resolve this move and don't need it marked as
183 // pending, so restore its destination.
184 move->ClearPending(destination);
185
186 // This move's source may have changed due to swaps to resolve cycles and
187 // so it may now be the last move in the cycle. If so remove it.
188 if (move->GetSource().Equals(destination)) {
189 move->Eliminate();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000190 DCHECK(required_swap == nullptr);
191 return nullptr;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100192 }
193
194 // The move may be blocked on a (at most one) pending move, in which case
195 // we have a cycle. Search for such a blocking move and perform a swap to
196 // resolve it.
197 bool do_swap = false;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000198 if (required_swap != nullptr) {
199 DCHECK_EQ(required_swap, move);
200 do_swap = true;
201 } else {
Vladimir Marko225b6462015-09-28 12:17:40 +0100202 for (MoveOperands* other_move : moves_) {
203 if (other_move->Blocks(destination)) {
204 DCHECK(other_move->IsPending());
205 if (!move->Is64BitMove() && other_move->Is64BitMove()) {
Nicolas Geoffray90218252015-04-15 11:56:51 +0100206 // We swap 64bits moves before swapping 32bits moves. Go back from the
207 // cycle by returning the move that must be swapped.
Vladimir Marko225b6462015-09-28 12:17:40 +0100208 return other_move;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000209 }
210 do_swap = true;
211 break;
212 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100213 }
214 }
215
216 if (do_swap) {
217 EmitSwap(index);
218 // Any unperformed (including pending) move with a source of either
219 // this move's source or destination needs to have their source
220 // changed to reflect the state of affairs after the swap.
221 Location source = move->GetSource();
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800222 Location swap_destination = move->GetDestination();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100223 move->Eliminate();
Vladimir Marko225b6462015-09-28 12:17:40 +0100224 for (MoveOperands* other_move : moves_) {
225 if (other_move->Blocks(source)) {
226 UpdateSourceOf(other_move, source, swap_destination);
227 } else if (other_move->Blocks(swap_destination)) {
228 UpdateSourceOf(other_move, swap_destination, source);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100229 }
230 }
Nicolas Geoffray90218252015-04-15 11:56:51 +0100231 // If the swap was required because of a 64bits move in the middle of a cycle,
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000232 // we return the swapped move, so that the caller knows it needs to re-iterate
233 // its dependency loop.
234 return required_swap;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100235 } else {
236 // This move is not blocked.
237 EmitMove(index);
238 move->Eliminate();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +0000239 DCHECK(required_swap == nullptr);
240 return nullptr;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100241 }
242}
243
Zheng Xuad4450e2015-04-17 18:48:56 +0800244bool ParallelMoveResolverWithSwap::IsScratchLocation(Location loc) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100245 for (MoveOperands* move : moves_) {
246 if (move->Blocks(loc)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100247 return false;
248 }
249 }
250
Vladimir Marko225b6462015-09-28 12:17:40 +0100251 for (MoveOperands* move : moves_) {
252 if (move->GetDestination().Equals(loc)) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100253 return true;
254 }
255 }
256
257 return false;
258}
259
Zheng Xuad4450e2015-04-17 18:48:56 +0800260int ParallelMoveResolverWithSwap::AllocateScratchRegister(int blocked,
261 int register_count,
262 int if_scratch,
263 bool* spilled) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100264 DCHECK_NE(blocked, if_scratch);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100265 int scratch = -1;
266 for (int reg = 0; reg < register_count; ++reg) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100267 if ((blocked != reg) && IsScratchLocation(Location::RegisterLocation(reg))) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100268 scratch = reg;
269 break;
270 }
271 }
272
273 if (scratch == -1) {
274 *spilled = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100275 scratch = if_scratch;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100276 } else {
277 *spilled = false;
278 }
279
280 return scratch;
281}
282
283
Zheng Xuad4450e2015-04-17 18:48:56 +0800284ParallelMoveResolverWithSwap::ScratchRegisterScope::ScratchRegisterScope(
285 ParallelMoveResolverWithSwap* resolver, int blocked, int if_scratch, int number_of_registers)
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100286 : resolver_(resolver),
287 reg_(kNoRegister),
288 spilled_(false) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100289 reg_ = resolver_->AllocateScratchRegister(blocked, number_of_registers, if_scratch, &spilled_);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100290
291 if (spilled_) {
292 resolver->SpillScratch(reg_);
293 }
294}
295
296
Zheng Xuad4450e2015-04-17 18:48:56 +0800297ParallelMoveResolverWithSwap::ScratchRegisterScope::~ScratchRegisterScope() {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100298 if (spilled_) {
299 resolver_->RestoreScratch(reg_);
300 }
301}
302
Zheng Xuad4450e2015-04-17 18:48:56 +0800303void ParallelMoveResolverNoSwap::EmitNativeCode(HParallelMove* parallel_move) {
304 DCHECK_EQ(GetNumberOfPendingMoves(), 0u);
Vladimir Marko225b6462015-09-28 12:17:40 +0100305 DCHECK(moves_.empty());
306 DCHECK(scratches_.empty());
Zheng Xuad4450e2015-04-17 18:48:56 +0800307
308 // Backend dependent initialization.
309 PrepareForEmitNativeCode();
310
311 // Build up a worklist of moves.
312 BuildInitialMoveList(parallel_move);
313
Vladimir Marko225b6462015-09-28 12:17:40 +0100314 for (size_t i = 0; i < moves_.size(); ++i) {
315 const MoveOperands& move = *moves_[i];
Zheng Xuad4450e2015-04-17 18:48:56 +0800316 // Skip constants to perform them last. They don't block other moves and
317 // skipping such moves with register destinations keeps those registers
318 // free for the whole algorithm.
319 if (!move.IsEliminated() && !move.GetSource().IsConstant()) {
320 PerformMove(i);
321 }
322 }
323
324 // Perform the moves with constant sources and register destinations with UpdateMoveSource()
325 // to reduce the number of literal loads. Stack destinations are skipped since we won't be benefit
326 // from changing the constant sources to stack locations.
Vladimir Marko225b6462015-09-28 12:17:40 +0100327 for (size_t i = 0; i < moves_.size(); ++i) {
328 MoveOperands* move = moves_[i];
Zheng Xuad4450e2015-04-17 18:48:56 +0800329 Location destination = move->GetDestination();
330 if (!move->IsEliminated() && !destination.IsStackSlot() && !destination.IsDoubleStackSlot()) {
331 Location source = move->GetSource();
332 EmitMove(i);
333 move->Eliminate();
334 // This may introduce additional instruction dependency, but reduce number
335 // of moves and possible literal loads. For example,
336 // Original moves:
337 // 1234.5678 -> D0
338 // 1234.5678 -> D1
339 // Updated moves:
340 // 1234.5678 -> D0
341 // D0 -> D1
342 UpdateMoveSource(source, destination);
343 }
344 }
345
346 // Perform the rest of the moves.
Vladimir Marko225b6462015-09-28 12:17:40 +0100347 for (size_t i = 0; i < moves_.size(); ++i) {
348 MoveOperands* move = moves_[i];
Zheng Xuad4450e2015-04-17 18:48:56 +0800349 if (!move->IsEliminated()) {
350 EmitMove(i);
351 move->Eliminate();
352 }
353 }
354
355 // All pending moves that we have added for resolve cycles should be performed.
356 DCHECK_EQ(GetNumberOfPendingMoves(), 0u);
357
358 // Backend dependent cleanup.
359 FinishEmitNativeCode();
360
Vladimir Marko225b6462015-09-28 12:17:40 +0100361 moves_.clear();
362 scratches_.clear();
Zheng Xuad4450e2015-04-17 18:48:56 +0800363}
364
365Location ParallelMoveResolverNoSwap::GetScratchLocation(Location::Kind kind) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100366 for (Location loc : scratches_) {
Zheng Xuad4450e2015-04-17 18:48:56 +0800367 if (loc.GetKind() == kind && !IsBlockedByMoves(loc)) {
368 return loc;
369 }
370 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100371 for (MoveOperands* move : moves_) {
372 Location loc = move->GetDestination();
Zheng Xuad4450e2015-04-17 18:48:56 +0800373 if (loc.GetKind() == kind && !IsBlockedByMoves(loc)) {
374 return loc;
375 }
376 }
377 return Location::NoLocation();
378}
379
380void ParallelMoveResolverNoSwap::AddScratchLocation(Location loc) {
381 if (kIsDebugBuild) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100382 for (Location scratch : scratches_) {
383 CHECK(!loc.Equals(scratch));
Zheng Xuad4450e2015-04-17 18:48:56 +0800384 }
385 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100386 scratches_.push_back(loc);
Zheng Xuad4450e2015-04-17 18:48:56 +0800387}
388
389void ParallelMoveResolverNoSwap::RemoveScratchLocation(Location loc) {
390 DCHECK(!IsBlockedByMoves(loc));
Vladimir Marko225b6462015-09-28 12:17:40 +0100391 for (auto it = scratches_.begin(), end = scratches_.end(); it != end; ++it) {
392 if (loc.Equals(*it)) {
393 scratches_.erase(it);
Zheng Xuad4450e2015-04-17 18:48:56 +0800394 break;
395 }
396 }
397}
398
399void ParallelMoveResolverNoSwap::PerformMove(size_t index) {
400 // Each call to this function performs a move and deletes it from the move
401 // graph. We first recursively perform any move blocking this one. We mark
402 // a move as "pending" on entry to PerformMove in order to detect cycles
403 // in the move graph. We use scratch location to resolve cycles, also
404 // additional pending moves might be added. After move has been performed,
405 // we will update source operand in the move graph to reduce dependencies in
406 // the graph.
407
Vladimir Marko225b6462015-09-28 12:17:40 +0100408 MoveOperands* move = moves_[index];
Zheng Xuad4450e2015-04-17 18:48:56 +0800409 DCHECK(!move->IsPending());
410 DCHECK(!move->IsEliminated());
411 if (move->IsRedundant()) {
412 // Previous operations on the list of moves have caused this particular move
413 // to become a no-op, so we can safely eliminate it. Consider for example
414 // (0 -> 1) (1 -> 0) (1 -> 2). There is a cycle (0 -> 1) (1 -> 0), that we will
415 // resolve as (1 -> scratch) (0 -> 1) (scratch -> 0). If, by chance, '2' is
416 // used as the scratch location, the move (1 -> 2) will occur while resolving
417 // the cycle. When that move is emitted, the code will update moves with a '1'
418 // as their source to use '2' instead (see `UpdateMoveSource()`. In our example
419 // the initial move (1 -> 2) would then become the no-op (2 -> 2) that can be
420 // eliminated here.
421 move->Eliminate();
422 return;
423 }
424
425 // Clear this move's destination to indicate a pending move. The actual
426 // destination is saved in a stack-allocated local. Recursion may allow
427 // multiple moves to be pending.
428 DCHECK(!move->GetSource().IsInvalid());
429 Location destination = move->MarkPending();
430
431 // Perform a depth-first traversal of the move graph to resolve
432 // dependencies. Any unperformed, unpending move with a source the same
433 // as this one's destination blocks this one so recursively perform all
434 // such moves.
Vladimir Marko225b6462015-09-28 12:17:40 +0100435 for (size_t i = 0; i < moves_.size(); ++i) {
436 const MoveOperands& other_move = *moves_[i];
Zheng Xuad4450e2015-04-17 18:48:56 +0800437 if (other_move.Blocks(destination) && !other_move.IsPending()) {
438 PerformMove(i);
439 }
440 }
441
442 // We are about to resolve this move and don't need it marked as
443 // pending, so restore its destination.
444 move->ClearPending(destination);
445
446 // No one else should write to the move destination when the it is pending.
447 DCHECK(!move->IsRedundant());
448
449 Location source = move->GetSource();
450 // The move may be blocked on several pending moves, in case we have a cycle.
451 if (IsBlockedByMoves(destination)) {
452 // For a cycle like: (A -> B) (B -> C) (C -> A), we change it to following
453 // sequence:
454 // (C -> scratch) # Emit right now.
455 // (A -> B) (B -> C) # Unblocked.
456 // (scratch -> A) # Add to pending_moves_, blocked by (A -> B).
457 Location::Kind kind = source.GetKind();
458 DCHECK_NE(kind, Location::kConstant);
459 Location scratch = AllocateScratchLocationFor(kind);
460 // We only care about the move size.
461 Primitive::Type type = move->Is64BitMove() ? Primitive::kPrimLong : Primitive::kPrimInt;
462 // Perform (C -> scratch)
463 move->SetDestination(scratch);
464 EmitMove(index);
465 move->Eliminate();
466 UpdateMoveSource(source, scratch);
467 // Add (scratch -> A).
468 AddPendingMove(scratch, destination, type);
469 } else {
470 // This move is not blocked.
471 EmitMove(index);
472 move->Eliminate();
473 UpdateMoveSource(source, destination);
474 }
475
476 // Moves in the pending list should not block any other moves. But performing
477 // unblocked moves in the pending list can free scratch registers, so we do this
478 // as early as possible.
479 MoveOperands* pending_move;
480 while ((pending_move = GetUnblockedPendingMove(source)) != nullptr) {
481 Location pending_source = pending_move->GetSource();
482 Location pending_destination = pending_move->GetDestination();
483 // We do not depend on the pending move index. So just delete the move instead
484 // of eliminating it to make the pending list cleaner.
485 DeletePendingMove(pending_move);
486 move->SetSource(pending_source);
487 move->SetDestination(pending_destination);
488 EmitMove(index);
489 move->Eliminate();
490 UpdateMoveSource(pending_source, pending_destination);
491 // Free any unblocked locations in the scratch location list.
Vladimir Marko225b6462015-09-28 12:17:40 +0100492 // Note: Fetch size() on each iteration because scratches_ can be modified inside the loop.
493 // FIXME: If FreeScratchLocation() removes the location from scratches_,
494 // we skip the next location. This happens for arm64.
495 for (size_t i = 0; i < scratches_.size(); ++i) {
496 Location scratch = scratches_[i];
Zheng Xuad4450e2015-04-17 18:48:56 +0800497 // Only scratch overlapping with performed move source can be unblocked.
498 if (scratch.OverlapsWith(pending_source) && !IsBlockedByMoves(scratch)) {
499 FreeScratchLocation(pending_source);
500 }
501 }
502 }
503}
504
505void ParallelMoveResolverNoSwap::UpdateMoveSource(Location from, Location to) {
506 // This function is used to reduce the dependencies in the graph after
507 // (from -> to) has been performed. Since we ensure there is no move with the same
508 // destination, (to -> X) can not be blocked while (from -> X) might still be
509 // blocked. Consider for example the moves (0 -> 1) (1 -> 2) (1 -> 3). After
510 // (1 -> 2) has been performed, the moves left are (0 -> 1) and (1 -> 3). There is
511 // a dependency between the two. If we update the source location from 1 to 2, we
512 // will get (0 -> 1) and (2 -> 3). There is no dependency between the two.
513 //
514 // This is not something we must do, but we can use fewer scratch locations with
515 // this trick. For example, we can avoid using additional scratch locations for
516 // moves (0 -> 1), (1 -> 2), (1 -> 0).
Vladimir Marko225b6462015-09-28 12:17:40 +0100517 for (MoveOperands* move : moves_) {
Zheng Xuad4450e2015-04-17 18:48:56 +0800518 if (move->GetSource().Equals(from)) {
519 move->SetSource(to);
520 }
521 }
522}
523
524void ParallelMoveResolverNoSwap::AddPendingMove(Location source,
525 Location destination, Primitive::Type type) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100526 pending_moves_.push_back(new (allocator_) MoveOperands(source, destination, type, nullptr));
Zheng Xuad4450e2015-04-17 18:48:56 +0800527}
528
529void ParallelMoveResolverNoSwap::DeletePendingMove(MoveOperands* move) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100530 RemoveElement(pending_moves_, move);
Zheng Xuad4450e2015-04-17 18:48:56 +0800531}
532
533MoveOperands* ParallelMoveResolverNoSwap::GetUnblockedPendingMove(Location loc) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100534 for (MoveOperands* move : pending_moves_) {
Zheng Xuad4450e2015-04-17 18:48:56 +0800535 Location destination = move->GetDestination();
536 // Only moves with destination overlapping with input loc can be unblocked.
537 if (destination.OverlapsWith(loc) && !IsBlockedByMoves(destination)) {
538 return move;
539 }
540 }
541 return nullptr;
542}
543
544bool ParallelMoveResolverNoSwap::IsBlockedByMoves(Location loc) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100545 for (MoveOperands* move : pending_moves_) {
546 if (move->Blocks(loc)) {
Zheng Xuad4450e2015-04-17 18:48:56 +0800547 return true;
548 }
549 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100550 for (MoveOperands* move : moves_) {
551 if (move->Blocks(loc)) {
Zheng Xuad4450e2015-04-17 18:48:56 +0800552 return true;
553 }
554 }
555 return false;
556}
557
558// So far it is only used for debugging purposes to make sure all pending moves
559// have been performed.
560size_t ParallelMoveResolverNoSwap::GetNumberOfPendingMoves() {
Vladimir Marko225b6462015-09-28 12:17:40 +0100561 return pending_moves_.size();
Zheng Xuad4450e2015-04-17 18:48:56 +0800562}
563
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100564} // namespace art