1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "parallel_move_resolver.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
20*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
23*795d594fSAndroid Build Coastguard Worker
BuildInitialMoveList(HParallelMove * parallel_move)24*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolver::BuildInitialMoveList(HParallelMove* parallel_move) {
25*795d594fSAndroid Build Coastguard Worker // Perform a linear sweep of the moves to add them to the initial list of
26*795d594fSAndroid Build Coastguard Worker // moves to perform, ignoring any move that is redundant (the source is
27*795d594fSAndroid Build Coastguard Worker // the same as the destination, the destination is ignored and
28*795d594fSAndroid Build Coastguard Worker // unallocated, or the move was already eliminated).
29*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < parallel_move->NumMoves(); ++i) {
30*795d594fSAndroid Build Coastguard Worker MoveOperands* move = parallel_move->MoveOperandsAt(i);
31*795d594fSAndroid Build Coastguard Worker if (!move->IsRedundant()) {
32*795d594fSAndroid Build Coastguard Worker moves_.push_back(move);
33*795d594fSAndroid Build Coastguard Worker }
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker
EmitNativeCode(HParallelMove * parallel_move)37*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverWithSwap::EmitNativeCode(HParallelMove* parallel_move) {
38*795d594fSAndroid Build Coastguard Worker DCHECK(moves_.empty());
39*795d594fSAndroid Build Coastguard Worker // Build up a worklist of moves.
40*795d594fSAndroid Build Coastguard Worker BuildInitialMoveList(parallel_move);
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker // Move stack/stack slot to take advantage of a free register on constrained machines.
43*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
44*795d594fSAndroid Build Coastguard Worker const MoveOperands& move = *moves_[i];
45*795d594fSAndroid Build Coastguard Worker // Ignore constants and moves already eliminated.
46*795d594fSAndroid Build Coastguard Worker if (move.IsEliminated() || move.GetSource().IsConstant()) {
47*795d594fSAndroid Build Coastguard Worker continue;
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker if ((move.GetSource().IsStackSlot() || move.GetSource().IsDoubleStackSlot()) &&
51*795d594fSAndroid Build Coastguard Worker (move.GetDestination().IsStackSlot() || move.GetDestination().IsDoubleStackSlot())) {
52*795d594fSAndroid Build Coastguard Worker PerformMove(i);
53*795d594fSAndroid Build Coastguard Worker }
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
57*795d594fSAndroid Build Coastguard Worker const MoveOperands& move = *moves_[i];
58*795d594fSAndroid Build Coastguard Worker // Skip constants to perform them last. They don't block other moves
59*795d594fSAndroid Build Coastguard Worker // and skipping such moves with register destinations keeps those
60*795d594fSAndroid Build Coastguard Worker // registers free for the whole algorithm.
61*795d594fSAndroid Build Coastguard Worker if (!move.IsEliminated() && !move.GetSource().IsConstant()) {
62*795d594fSAndroid Build Coastguard Worker PerformMove(i);
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker // Perform the moves with constant sources.
67*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
68*795d594fSAndroid Build Coastguard Worker MoveOperands* move = moves_[i];
69*795d594fSAndroid Build Coastguard Worker if (!move->IsEliminated()) {
70*795d594fSAndroid Build Coastguard Worker DCHECK(move->GetSource().IsConstant());
71*795d594fSAndroid Build Coastguard Worker EmitMove(i);
72*795d594fSAndroid Build Coastguard Worker // Eliminate the move, in case following moves need a scratch register.
73*795d594fSAndroid Build Coastguard Worker move->Eliminate();
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Worker moves_.clear();
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker
LowOf(Location location)80*795d594fSAndroid Build Coastguard Worker Location LowOf(Location location) {
81*795d594fSAndroid Build Coastguard Worker if (location.IsRegisterPair()) {
82*795d594fSAndroid Build Coastguard Worker return Location::RegisterLocation(location.low());
83*795d594fSAndroid Build Coastguard Worker } else if (location.IsFpuRegisterPair()) {
84*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterLocation(location.low());
85*795d594fSAndroid Build Coastguard Worker } else if (location.IsDoubleStackSlot()) {
86*795d594fSAndroid Build Coastguard Worker return Location::StackSlot(location.GetStackIndex());
87*795d594fSAndroid Build Coastguard Worker } else {
88*795d594fSAndroid Build Coastguard Worker return Location::NoLocation();
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
HighOf(Location location)92*795d594fSAndroid Build Coastguard Worker Location HighOf(Location location) {
93*795d594fSAndroid Build Coastguard Worker if (location.IsRegisterPair()) {
94*795d594fSAndroid Build Coastguard Worker return Location::RegisterLocation(location.high());
95*795d594fSAndroid Build Coastguard Worker } else if (location.IsFpuRegisterPair()) {
96*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterLocation(location.high());
97*795d594fSAndroid Build Coastguard Worker } else if (location.IsDoubleStackSlot()) {
98*795d594fSAndroid Build Coastguard Worker return Location::StackSlot(location.GetHighStackIndex(4));
99*795d594fSAndroid Build Coastguard Worker } else {
100*795d594fSAndroid Build Coastguard Worker return Location::NoLocation();
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker }
103*795d594fSAndroid Build Coastguard Worker
104*795d594fSAndroid Build Coastguard Worker // Update the source of `move`, knowing that `updated_location` has been swapped
105*795d594fSAndroid Build Coastguard Worker // with `new_source`. Note that `updated_location` can be a pair, therefore if
106*795d594fSAndroid Build Coastguard Worker // `move` is non-pair, we need to extract which register to use.
UpdateSourceOf(MoveOperands * move,Location updated_location,Location new_source)107*795d594fSAndroid Build Coastguard Worker static void UpdateSourceOf(MoveOperands* move, Location updated_location, Location new_source) {
108*795d594fSAndroid Build Coastguard Worker Location source = move->GetSource();
109*795d594fSAndroid Build Coastguard Worker if (LowOf(updated_location).Equals(source)) {
110*795d594fSAndroid Build Coastguard Worker move->SetSource(LowOf(new_source));
111*795d594fSAndroid Build Coastguard Worker } else if (HighOf(updated_location).Equals(source)) {
112*795d594fSAndroid Build Coastguard Worker move->SetSource(HighOf(new_source));
113*795d594fSAndroid Build Coastguard Worker } else {
114*795d594fSAndroid Build Coastguard Worker DCHECK(updated_location.Equals(source)) << updated_location << " " << source;
115*795d594fSAndroid Build Coastguard Worker move->SetSource(new_source);
116*795d594fSAndroid Build Coastguard Worker }
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker
PerformMove(size_t index)119*795d594fSAndroid Build Coastguard Worker MoveOperands* ParallelMoveResolverWithSwap::PerformMove(size_t index) {
120*795d594fSAndroid Build Coastguard Worker // Each call to this function performs a move and deletes it from the move
121*795d594fSAndroid Build Coastguard Worker // graph. We first recursively perform any move blocking this one. We
122*795d594fSAndroid Build Coastguard Worker // mark a move as "pending" on entry to PerformMove in order to detect
123*795d594fSAndroid Build Coastguard Worker // cycles in the move graph. We use operand swaps to resolve cycles,
124*795d594fSAndroid Build Coastguard Worker // which means that a call to PerformMove could change any source operand
125*795d594fSAndroid Build Coastguard Worker // in the move graph.
126*795d594fSAndroid Build Coastguard Worker
127*795d594fSAndroid Build Coastguard Worker MoveOperands* move = moves_[index];
128*795d594fSAndroid Build Coastguard Worker DCHECK(!move->IsPending());
129*795d594fSAndroid Build Coastguard Worker if (move->IsRedundant()) {
130*795d594fSAndroid Build Coastguard Worker // Because we swap register pairs first, following, un-pending
131*795d594fSAndroid Build Coastguard Worker // moves may become redundant.
132*795d594fSAndroid Build Coastguard Worker move->Eliminate();
133*795d594fSAndroid Build Coastguard Worker return nullptr;
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker
136*795d594fSAndroid Build Coastguard Worker // Clear this move's destination to indicate a pending move. The actual
137*795d594fSAndroid Build Coastguard Worker // destination is saved in a stack-allocated local. Recursion may allow
138*795d594fSAndroid Build Coastguard Worker // multiple moves to be pending.
139*795d594fSAndroid Build Coastguard Worker DCHECK(!move->GetSource().IsInvalid());
140*795d594fSAndroid Build Coastguard Worker Location destination = move->MarkPending();
141*795d594fSAndroid Build Coastguard Worker
142*795d594fSAndroid Build Coastguard Worker // Perform a depth-first traversal of the move graph to resolve
143*795d594fSAndroid Build Coastguard Worker // dependencies. Any unperformed, unpending move with a source the same
144*795d594fSAndroid Build Coastguard Worker // as this one's destination blocks this one so recursively perform all
145*795d594fSAndroid Build Coastguard Worker // such moves.
146*795d594fSAndroid Build Coastguard Worker MoveOperands* required_swap = nullptr;
147*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
148*795d594fSAndroid Build Coastguard Worker const MoveOperands& other_move = *moves_[i];
149*795d594fSAndroid Build Coastguard Worker if (other_move.Blocks(destination) && !other_move.IsPending()) {
150*795d594fSAndroid Build Coastguard Worker // Though PerformMove can change any source operand in the move graph,
151*795d594fSAndroid Build Coastguard Worker // calling `PerformMove` cannot create a blocking move via a swap
152*795d594fSAndroid Build Coastguard Worker // (this loop does not miss any).
153*795d594fSAndroid Build Coastguard Worker // For example, assume there is a non-blocking move with source A
154*795d594fSAndroid Build Coastguard Worker // and this move is blocked on source B and there is a swap of A and
155*795d594fSAndroid Build Coastguard Worker // B. Then A and B must be involved in the same cycle (or they would
156*795d594fSAndroid Build Coastguard Worker // not be swapped). Since this move's destination is B and there is
157*795d594fSAndroid Build Coastguard Worker // only a single incoming edge to an operand, this move must also be
158*795d594fSAndroid Build Coastguard Worker // involved in the same cycle. In that case, the blocking move will
159*795d594fSAndroid Build Coastguard Worker // be created but will be "pending" when we return from PerformMove.
160*795d594fSAndroid Build Coastguard Worker required_swap = PerformMove(i);
161*795d594fSAndroid Build Coastguard Worker
162*795d594fSAndroid Build Coastguard Worker if (required_swap == move) {
163*795d594fSAndroid Build Coastguard Worker // If this move is required to swap, we do so without looking
164*795d594fSAndroid Build Coastguard Worker // at the next moves. Swapping is not blocked by anything, it just
165*795d594fSAndroid Build Coastguard Worker // updates other moves's source.
166*795d594fSAndroid Build Coastguard Worker break;
167*795d594fSAndroid Build Coastguard Worker } else if (required_swap == moves_[i]) {
168*795d594fSAndroid Build Coastguard Worker // If `other_move` was swapped, we iterate again to find a new
169*795d594fSAndroid Build Coastguard Worker // potential cycle.
170*795d594fSAndroid Build Coastguard Worker required_swap = nullptr;
171*795d594fSAndroid Build Coastguard Worker i = -1;
172*795d594fSAndroid Build Coastguard Worker } else if (required_swap != nullptr) {
173*795d594fSAndroid Build Coastguard Worker // A move is required to swap. We walk back the cycle to find the
174*795d594fSAndroid Build Coastguard Worker // move by just returning from this `PerformMove`.
175*795d594fSAndroid Build Coastguard Worker moves_[index]->ClearPending(destination);
176*795d594fSAndroid Build Coastguard Worker return required_swap;
177*795d594fSAndroid Build Coastguard Worker }
178*795d594fSAndroid Build Coastguard Worker }
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker
181*795d594fSAndroid Build Coastguard Worker // We are about to resolve this move and don't need it marked as
182*795d594fSAndroid Build Coastguard Worker // pending, so restore its destination.
183*795d594fSAndroid Build Coastguard Worker move->ClearPending(destination);
184*795d594fSAndroid Build Coastguard Worker
185*795d594fSAndroid Build Coastguard Worker // This move's source may have changed due to swaps to resolve cycles and
186*795d594fSAndroid Build Coastguard Worker // so it may now be the last move in the cycle. If so remove it.
187*795d594fSAndroid Build Coastguard Worker if (move->GetSource().Equals(destination)) {
188*795d594fSAndroid Build Coastguard Worker move->Eliminate();
189*795d594fSAndroid Build Coastguard Worker DCHECK(required_swap == nullptr);
190*795d594fSAndroid Build Coastguard Worker return nullptr;
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker
193*795d594fSAndroid Build Coastguard Worker // The move may be blocked on a (at most one) pending move, in which case
194*795d594fSAndroid Build Coastguard Worker // we have a cycle. Search for such a blocking move and perform a swap to
195*795d594fSAndroid Build Coastguard Worker // resolve it.
196*795d594fSAndroid Build Coastguard Worker bool do_swap = false;
197*795d594fSAndroid Build Coastguard Worker if (required_swap != nullptr) {
198*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(required_swap, move);
199*795d594fSAndroid Build Coastguard Worker do_swap = true;
200*795d594fSAndroid Build Coastguard Worker } else {
201*795d594fSAndroid Build Coastguard Worker for (MoveOperands* other_move : moves_) {
202*795d594fSAndroid Build Coastguard Worker if (other_move->Blocks(destination)) {
203*795d594fSAndroid Build Coastguard Worker DCHECK(other_move->IsPending()) << "move=" << *move << " other_move=" << *other_move;
204*795d594fSAndroid Build Coastguard Worker if (!move->Is64BitMove() && other_move->Is64BitMove()) {
205*795d594fSAndroid Build Coastguard Worker // We swap 64bits moves before swapping 32bits moves. Go back from the
206*795d594fSAndroid Build Coastguard Worker // cycle by returning the move that must be swapped.
207*795d594fSAndroid Build Coastguard Worker return other_move;
208*795d594fSAndroid Build Coastguard Worker }
209*795d594fSAndroid Build Coastguard Worker do_swap = true;
210*795d594fSAndroid Build Coastguard Worker break;
211*795d594fSAndroid Build Coastguard Worker }
212*795d594fSAndroid Build Coastguard Worker }
213*795d594fSAndroid Build Coastguard Worker }
214*795d594fSAndroid Build Coastguard Worker
215*795d594fSAndroid Build Coastguard Worker if (do_swap) {
216*795d594fSAndroid Build Coastguard Worker EmitSwap(index);
217*795d594fSAndroid Build Coastguard Worker // Any unperformed (including pending) move with a source of either
218*795d594fSAndroid Build Coastguard Worker // this move's source or destination needs to have their source
219*795d594fSAndroid Build Coastguard Worker // changed to reflect the state of affairs after the swap.
220*795d594fSAndroid Build Coastguard Worker Location source = move->GetSource();
221*795d594fSAndroid Build Coastguard Worker Location swap_destination = move->GetDestination();
222*795d594fSAndroid Build Coastguard Worker move->Eliminate();
223*795d594fSAndroid Build Coastguard Worker for (MoveOperands* other_move : moves_) {
224*795d594fSAndroid Build Coastguard Worker if (other_move->Blocks(source)) {
225*795d594fSAndroid Build Coastguard Worker UpdateSourceOf(other_move, source, swap_destination);
226*795d594fSAndroid Build Coastguard Worker } else if (other_move->Blocks(swap_destination)) {
227*795d594fSAndroid Build Coastguard Worker UpdateSourceOf(other_move, swap_destination, source);
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker }
230*795d594fSAndroid Build Coastguard Worker // If the swap was required because of a 64bits move in the middle of a cycle,
231*795d594fSAndroid Build Coastguard Worker // we return the swapped move, so that the caller knows it needs to re-iterate
232*795d594fSAndroid Build Coastguard Worker // its dependency loop.
233*795d594fSAndroid Build Coastguard Worker return required_swap;
234*795d594fSAndroid Build Coastguard Worker } else {
235*795d594fSAndroid Build Coastguard Worker // This move is not blocked.
236*795d594fSAndroid Build Coastguard Worker EmitMove(index);
237*795d594fSAndroid Build Coastguard Worker move->Eliminate();
238*795d594fSAndroid Build Coastguard Worker DCHECK(required_swap == nullptr);
239*795d594fSAndroid Build Coastguard Worker return nullptr;
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker
IsScratchLocation(Location loc)243*795d594fSAndroid Build Coastguard Worker bool ParallelMoveResolverWithSwap::IsScratchLocation(Location loc) {
244*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : moves_) {
245*795d594fSAndroid Build Coastguard Worker if (move->Blocks(loc)) {
246*795d594fSAndroid Build Coastguard Worker return false;
247*795d594fSAndroid Build Coastguard Worker }
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker
250*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : moves_) {
251*795d594fSAndroid Build Coastguard Worker if (move->GetDestination().Equals(loc)) {
252*795d594fSAndroid Build Coastguard Worker return true;
253*795d594fSAndroid Build Coastguard Worker }
254*795d594fSAndroid Build Coastguard Worker }
255*795d594fSAndroid Build Coastguard Worker
256*795d594fSAndroid Build Coastguard Worker return false;
257*795d594fSAndroid Build Coastguard Worker }
258*795d594fSAndroid Build Coastguard Worker
AllocateScratchRegister(int blocked,int register_count,int if_scratch,bool * spilled)259*795d594fSAndroid Build Coastguard Worker int ParallelMoveResolverWithSwap::AllocateScratchRegister(int blocked,
260*795d594fSAndroid Build Coastguard Worker int register_count,
261*795d594fSAndroid Build Coastguard Worker int if_scratch,
262*795d594fSAndroid Build Coastguard Worker bool* spilled) {
263*795d594fSAndroid Build Coastguard Worker DCHECK_NE(blocked, if_scratch);
264*795d594fSAndroid Build Coastguard Worker int scratch = -1;
265*795d594fSAndroid Build Coastguard Worker for (int reg = 0; reg < register_count; ++reg) {
266*795d594fSAndroid Build Coastguard Worker if ((blocked != reg) && IsScratchLocation(Location::RegisterLocation(reg))) {
267*795d594fSAndroid Build Coastguard Worker scratch = reg;
268*795d594fSAndroid Build Coastguard Worker break;
269*795d594fSAndroid Build Coastguard Worker }
270*795d594fSAndroid Build Coastguard Worker }
271*795d594fSAndroid Build Coastguard Worker
272*795d594fSAndroid Build Coastguard Worker if (scratch == -1) {
273*795d594fSAndroid Build Coastguard Worker *spilled = true;
274*795d594fSAndroid Build Coastguard Worker scratch = if_scratch;
275*795d594fSAndroid Build Coastguard Worker } else {
276*795d594fSAndroid Build Coastguard Worker *spilled = false;
277*795d594fSAndroid Build Coastguard Worker }
278*795d594fSAndroid Build Coastguard Worker
279*795d594fSAndroid Build Coastguard Worker return scratch;
280*795d594fSAndroid Build Coastguard Worker }
281*795d594fSAndroid Build Coastguard Worker
282*795d594fSAndroid Build Coastguard Worker
ScratchRegisterScope(ParallelMoveResolverWithSwap * resolver,int blocked,int if_scratch,int number_of_registers)283*795d594fSAndroid Build Coastguard Worker ParallelMoveResolverWithSwap::ScratchRegisterScope::ScratchRegisterScope(
284*795d594fSAndroid Build Coastguard Worker ParallelMoveResolverWithSwap* resolver, int blocked, int if_scratch, int number_of_registers)
285*795d594fSAndroid Build Coastguard Worker : resolver_(resolver),
286*795d594fSAndroid Build Coastguard Worker reg_(kNoRegister),
287*795d594fSAndroid Build Coastguard Worker spilled_(false) {
288*795d594fSAndroid Build Coastguard Worker reg_ = resolver_->AllocateScratchRegister(blocked, number_of_registers, if_scratch, &spilled_);
289*795d594fSAndroid Build Coastguard Worker
290*795d594fSAndroid Build Coastguard Worker if (spilled_) {
291*795d594fSAndroid Build Coastguard Worker resolver->SpillScratch(reg_);
292*795d594fSAndroid Build Coastguard Worker }
293*795d594fSAndroid Build Coastguard Worker }
294*795d594fSAndroid Build Coastguard Worker
295*795d594fSAndroid Build Coastguard Worker
~ScratchRegisterScope()296*795d594fSAndroid Build Coastguard Worker ParallelMoveResolverWithSwap::ScratchRegisterScope::~ScratchRegisterScope() {
297*795d594fSAndroid Build Coastguard Worker if (spilled_) {
298*795d594fSAndroid Build Coastguard Worker resolver_->RestoreScratch(reg_);
299*795d594fSAndroid Build Coastguard Worker }
300*795d594fSAndroid Build Coastguard Worker }
301*795d594fSAndroid Build Coastguard Worker
EmitNativeCode(HParallelMove * parallel_move)302*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::EmitNativeCode(HParallelMove* parallel_move) {
303*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(GetNumberOfPendingMoves(), 0u);
304*795d594fSAndroid Build Coastguard Worker DCHECK(moves_.empty());
305*795d594fSAndroid Build Coastguard Worker DCHECK(scratches_.empty());
306*795d594fSAndroid Build Coastguard Worker
307*795d594fSAndroid Build Coastguard Worker // Backend dependent initialization.
308*795d594fSAndroid Build Coastguard Worker PrepareForEmitNativeCode();
309*795d594fSAndroid Build Coastguard Worker
310*795d594fSAndroid Build Coastguard Worker // Build up a worklist of moves.
311*795d594fSAndroid Build Coastguard Worker BuildInitialMoveList(parallel_move);
312*795d594fSAndroid Build Coastguard Worker
313*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
314*795d594fSAndroid Build Coastguard Worker const MoveOperands& move = *moves_[i];
315*795d594fSAndroid Build Coastguard Worker // Skip constants to perform them last. They don't block other moves and
316*795d594fSAndroid Build Coastguard Worker // skipping such moves with register destinations keeps those registers
317*795d594fSAndroid Build Coastguard Worker // free for the whole algorithm.
318*795d594fSAndroid Build Coastguard Worker if (!move.IsEliminated() && !move.GetSource().IsConstant()) {
319*795d594fSAndroid Build Coastguard Worker PerformMove(i);
320*795d594fSAndroid Build Coastguard Worker }
321*795d594fSAndroid Build Coastguard Worker }
322*795d594fSAndroid Build Coastguard Worker
323*795d594fSAndroid Build Coastguard Worker // Perform the moves with constant sources and register destinations with UpdateMoveSource()
324*795d594fSAndroid Build Coastguard Worker // to reduce the number of literal loads. Stack destinations are skipped since we won't be benefit
325*795d594fSAndroid Build Coastguard Worker // from changing the constant sources to stack locations.
326*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
327*795d594fSAndroid Build Coastguard Worker MoveOperands* move = moves_[i];
328*795d594fSAndroid Build Coastguard Worker Location destination = move->GetDestination();
329*795d594fSAndroid Build Coastguard Worker if (!move->IsEliminated() && !destination.IsStackSlot() && !destination.IsDoubleStackSlot()) {
330*795d594fSAndroid Build Coastguard Worker Location source = move->GetSource();
331*795d594fSAndroid Build Coastguard Worker EmitMove(i);
332*795d594fSAndroid Build Coastguard Worker move->Eliminate();
333*795d594fSAndroid Build Coastguard Worker // This may introduce additional instruction dependency, but reduce number
334*795d594fSAndroid Build Coastguard Worker // of moves and possible literal loads. For example,
335*795d594fSAndroid Build Coastguard Worker // Original moves:
336*795d594fSAndroid Build Coastguard Worker // 1234.5678 -> D0
337*795d594fSAndroid Build Coastguard Worker // 1234.5678 -> D1
338*795d594fSAndroid Build Coastguard Worker // Updated moves:
339*795d594fSAndroid Build Coastguard Worker // 1234.5678 -> D0
340*795d594fSAndroid Build Coastguard Worker // D0 -> D1
341*795d594fSAndroid Build Coastguard Worker UpdateMoveSource(source, destination);
342*795d594fSAndroid Build Coastguard Worker }
343*795d594fSAndroid Build Coastguard Worker }
344*795d594fSAndroid Build Coastguard Worker
345*795d594fSAndroid Build Coastguard Worker // Perform the rest of the moves.
346*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
347*795d594fSAndroid Build Coastguard Worker MoveOperands* move = moves_[i];
348*795d594fSAndroid Build Coastguard Worker if (!move->IsEliminated()) {
349*795d594fSAndroid Build Coastguard Worker EmitMove(i);
350*795d594fSAndroid Build Coastguard Worker move->Eliminate();
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker }
353*795d594fSAndroid Build Coastguard Worker
354*795d594fSAndroid Build Coastguard Worker // All pending moves that we have added for resolve cycles should be performed.
355*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(GetNumberOfPendingMoves(), 0u);
356*795d594fSAndroid Build Coastguard Worker
357*795d594fSAndroid Build Coastguard Worker // Backend dependent cleanup.
358*795d594fSAndroid Build Coastguard Worker FinishEmitNativeCode();
359*795d594fSAndroid Build Coastguard Worker
360*795d594fSAndroid Build Coastguard Worker moves_.clear();
361*795d594fSAndroid Build Coastguard Worker scratches_.clear();
362*795d594fSAndroid Build Coastguard Worker }
363*795d594fSAndroid Build Coastguard Worker
GetScratchLocation(Location::Kind kind)364*795d594fSAndroid Build Coastguard Worker Location ParallelMoveResolverNoSwap::GetScratchLocation(Location::Kind kind) {
365*795d594fSAndroid Build Coastguard Worker for (Location loc : scratches_) {
366*795d594fSAndroid Build Coastguard Worker if (loc.GetKind() == kind && !IsBlockedByMoves(loc)) {
367*795d594fSAndroid Build Coastguard Worker return loc;
368*795d594fSAndroid Build Coastguard Worker }
369*795d594fSAndroid Build Coastguard Worker }
370*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : moves_) {
371*795d594fSAndroid Build Coastguard Worker Location loc = move->GetDestination();
372*795d594fSAndroid Build Coastguard Worker if (loc.GetKind() == kind && !IsBlockedByMoves(loc)) {
373*795d594fSAndroid Build Coastguard Worker return loc;
374*795d594fSAndroid Build Coastguard Worker }
375*795d594fSAndroid Build Coastguard Worker }
376*795d594fSAndroid Build Coastguard Worker return Location::NoLocation();
377*795d594fSAndroid Build Coastguard Worker }
378*795d594fSAndroid Build Coastguard Worker
AddScratchLocation(Location loc)379*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::AddScratchLocation(Location loc) {
380*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
381*795d594fSAndroid Build Coastguard Worker for (Location scratch : scratches_) {
382*795d594fSAndroid Build Coastguard Worker CHECK(!loc.Equals(scratch));
383*795d594fSAndroid Build Coastguard Worker }
384*795d594fSAndroid Build Coastguard Worker }
385*795d594fSAndroid Build Coastguard Worker scratches_.push_back(loc);
386*795d594fSAndroid Build Coastguard Worker }
387*795d594fSAndroid Build Coastguard Worker
RemoveScratchLocation(Location loc)388*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::RemoveScratchLocation(Location loc) {
389*795d594fSAndroid Build Coastguard Worker DCHECK(!IsBlockedByMoves(loc));
390*795d594fSAndroid Build Coastguard Worker for (auto it = scratches_.begin(), end = scratches_.end(); it != end; ++it) {
391*795d594fSAndroid Build Coastguard Worker if (loc.Equals(*it)) {
392*795d594fSAndroid Build Coastguard Worker scratches_.erase(it);
393*795d594fSAndroid Build Coastguard Worker break;
394*795d594fSAndroid Build Coastguard Worker }
395*795d594fSAndroid Build Coastguard Worker }
396*795d594fSAndroid Build Coastguard Worker }
397*795d594fSAndroid Build Coastguard Worker
PerformMove(size_t index)398*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::PerformMove(size_t index) {
399*795d594fSAndroid Build Coastguard Worker // Each call to this function performs a move and deletes it from the move
400*795d594fSAndroid Build Coastguard Worker // graph. We first recursively perform any move blocking this one. We mark
401*795d594fSAndroid Build Coastguard Worker // a move as "pending" on entry to PerformMove in order to detect cycles
402*795d594fSAndroid Build Coastguard Worker // in the move graph. We use scratch location to resolve cycles, also
403*795d594fSAndroid Build Coastguard Worker // additional pending moves might be added. After move has been performed,
404*795d594fSAndroid Build Coastguard Worker // we will update source operand in the move graph to reduce dependencies in
405*795d594fSAndroid Build Coastguard Worker // the graph.
406*795d594fSAndroid Build Coastguard Worker
407*795d594fSAndroid Build Coastguard Worker MoveOperands* move = moves_[index];
408*795d594fSAndroid Build Coastguard Worker DCHECK(!move->IsPending());
409*795d594fSAndroid Build Coastguard Worker DCHECK(!move->IsEliminated());
410*795d594fSAndroid Build Coastguard Worker if (move->IsRedundant()) {
411*795d594fSAndroid Build Coastguard Worker // Previous operations on the list of moves have caused this particular move
412*795d594fSAndroid Build Coastguard Worker // to become a no-op, so we can safely eliminate it. Consider for example
413*795d594fSAndroid Build Coastguard Worker // (0 -> 1) (1 -> 0) (1 -> 2). There is a cycle (0 -> 1) (1 -> 0), that we will
414*795d594fSAndroid Build Coastguard Worker // resolve as (1 -> scratch) (0 -> 1) (scratch -> 0). If, by chance, '2' is
415*795d594fSAndroid Build Coastguard Worker // used as the scratch location, the move (1 -> 2) will occur while resolving
416*795d594fSAndroid Build Coastguard Worker // the cycle. When that move is emitted, the code will update moves with a '1'
417*795d594fSAndroid Build Coastguard Worker // as their source to use '2' instead (see `UpdateMoveSource()`. In our example
418*795d594fSAndroid Build Coastguard Worker // the initial move (1 -> 2) would then become the no-op (2 -> 2) that can be
419*795d594fSAndroid Build Coastguard Worker // eliminated here.
420*795d594fSAndroid Build Coastguard Worker move->Eliminate();
421*795d594fSAndroid Build Coastguard Worker return;
422*795d594fSAndroid Build Coastguard Worker }
423*795d594fSAndroid Build Coastguard Worker
424*795d594fSAndroid Build Coastguard Worker // Clear this move's destination to indicate a pending move. The actual
425*795d594fSAndroid Build Coastguard Worker // destination is saved in a stack-allocated local. Recursion may allow
426*795d594fSAndroid Build Coastguard Worker // multiple moves to be pending.
427*795d594fSAndroid Build Coastguard Worker DCHECK(!move->GetSource().IsInvalid());
428*795d594fSAndroid Build Coastguard Worker Location destination = move->MarkPending();
429*795d594fSAndroid Build Coastguard Worker
430*795d594fSAndroid Build Coastguard Worker // Perform a depth-first traversal of the move graph to resolve
431*795d594fSAndroid Build Coastguard Worker // dependencies. Any unperformed, unpending move with a source the same
432*795d594fSAndroid Build Coastguard Worker // as this one's destination blocks this one so recursively perform all
433*795d594fSAndroid Build Coastguard Worker // such moves.
434*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < moves_.size(); ++i) {
435*795d594fSAndroid Build Coastguard Worker const MoveOperands& other_move = *moves_[i];
436*795d594fSAndroid Build Coastguard Worker if (other_move.Blocks(destination) && !other_move.IsPending()) {
437*795d594fSAndroid Build Coastguard Worker PerformMove(i);
438*795d594fSAndroid Build Coastguard Worker }
439*795d594fSAndroid Build Coastguard Worker }
440*795d594fSAndroid Build Coastguard Worker
441*795d594fSAndroid Build Coastguard Worker // We are about to resolve this move and don't need it marked as
442*795d594fSAndroid Build Coastguard Worker // pending, so restore its destination.
443*795d594fSAndroid Build Coastguard Worker move->ClearPending(destination);
444*795d594fSAndroid Build Coastguard Worker
445*795d594fSAndroid Build Coastguard Worker // No one else should write to the move destination when the it is pending.
446*795d594fSAndroid Build Coastguard Worker DCHECK(!move->IsRedundant());
447*795d594fSAndroid Build Coastguard Worker
448*795d594fSAndroid Build Coastguard Worker Location source = move->GetSource();
449*795d594fSAndroid Build Coastguard Worker // The move may be blocked on several pending moves, in case we have a cycle.
450*795d594fSAndroid Build Coastguard Worker if (IsBlockedByMoves(destination)) {
451*795d594fSAndroid Build Coastguard Worker // For a cycle like: (A -> B) (B -> C) (C -> A), we change it to following
452*795d594fSAndroid Build Coastguard Worker // sequence:
453*795d594fSAndroid Build Coastguard Worker // (C -> scratch) # Emit right now.
454*795d594fSAndroid Build Coastguard Worker // (A -> B) (B -> C) # Unblocked.
455*795d594fSAndroid Build Coastguard Worker // (scratch -> A) # Add to pending_moves_, blocked by (A -> B).
456*795d594fSAndroid Build Coastguard Worker Location::Kind kind = source.GetKind();
457*795d594fSAndroid Build Coastguard Worker DCHECK_NE(kind, Location::kConstant);
458*795d594fSAndroid Build Coastguard Worker Location scratch = AllocateScratchLocationFor(kind);
459*795d594fSAndroid Build Coastguard Worker // We only care about the move size.
460*795d594fSAndroid Build Coastguard Worker DataType::Type type = move->Is64BitMove() ? DataType::Type::kInt64 : DataType::Type::kInt32;
461*795d594fSAndroid Build Coastguard Worker // Perform (C -> scratch)
462*795d594fSAndroid Build Coastguard Worker move->SetDestination(scratch);
463*795d594fSAndroid Build Coastguard Worker EmitMove(index);
464*795d594fSAndroid Build Coastguard Worker move->Eliminate();
465*795d594fSAndroid Build Coastguard Worker UpdateMoveSource(source, scratch);
466*795d594fSAndroid Build Coastguard Worker // Add (scratch -> A).
467*795d594fSAndroid Build Coastguard Worker AddPendingMove(scratch, destination, type);
468*795d594fSAndroid Build Coastguard Worker } else {
469*795d594fSAndroid Build Coastguard Worker // This move is not blocked.
470*795d594fSAndroid Build Coastguard Worker EmitMove(index);
471*795d594fSAndroid Build Coastguard Worker move->Eliminate();
472*795d594fSAndroid Build Coastguard Worker UpdateMoveSource(source, destination);
473*795d594fSAndroid Build Coastguard Worker }
474*795d594fSAndroid Build Coastguard Worker
475*795d594fSAndroid Build Coastguard Worker // Moves in the pending list should not block any other moves. But performing
476*795d594fSAndroid Build Coastguard Worker // unblocked moves in the pending list can free scratch registers, so we do this
477*795d594fSAndroid Build Coastguard Worker // as early as possible.
478*795d594fSAndroid Build Coastguard Worker MoveOperands* pending_move;
479*795d594fSAndroid Build Coastguard Worker while ((pending_move = GetUnblockedPendingMove(source)) != nullptr) {
480*795d594fSAndroid Build Coastguard Worker Location pending_source = pending_move->GetSource();
481*795d594fSAndroid Build Coastguard Worker Location pending_destination = pending_move->GetDestination();
482*795d594fSAndroid Build Coastguard Worker // We do not depend on the pending move index. So just delete the move instead
483*795d594fSAndroid Build Coastguard Worker // of eliminating it to make the pending list cleaner.
484*795d594fSAndroid Build Coastguard Worker DeletePendingMove(pending_move);
485*795d594fSAndroid Build Coastguard Worker move->SetSource(pending_source);
486*795d594fSAndroid Build Coastguard Worker move->SetDestination(pending_destination);
487*795d594fSAndroid Build Coastguard Worker EmitMove(index);
488*795d594fSAndroid Build Coastguard Worker move->Eliminate();
489*795d594fSAndroid Build Coastguard Worker UpdateMoveSource(pending_source, pending_destination);
490*795d594fSAndroid Build Coastguard Worker // Free any unblocked locations in the scratch location list.
491*795d594fSAndroid Build Coastguard Worker // Note: Fetch size() on each iteration because scratches_ can be modified inside the loop.
492*795d594fSAndroid Build Coastguard Worker // FIXME: If FreeScratchLocation() removes the location from scratches_,
493*795d594fSAndroid Build Coastguard Worker // we skip the next location. This happens for arm64.
494*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < scratches_.size(); ++i) {
495*795d594fSAndroid Build Coastguard Worker Location scratch = scratches_[i];
496*795d594fSAndroid Build Coastguard Worker // Only scratch overlapping with performed move source can be unblocked.
497*795d594fSAndroid Build Coastguard Worker if (scratch.OverlapsWith(pending_source) && !IsBlockedByMoves(scratch)) {
498*795d594fSAndroid Build Coastguard Worker FreeScratchLocation(pending_source);
499*795d594fSAndroid Build Coastguard Worker }
500*795d594fSAndroid Build Coastguard Worker }
501*795d594fSAndroid Build Coastguard Worker }
502*795d594fSAndroid Build Coastguard Worker }
503*795d594fSAndroid Build Coastguard Worker
UpdateMoveSource(Location from,Location to)504*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::UpdateMoveSource(Location from, Location to) {
505*795d594fSAndroid Build Coastguard Worker // This function is used to reduce the dependencies in the graph after
506*795d594fSAndroid Build Coastguard Worker // (from -> to) has been performed. Since we ensure there is no move with the same
507*795d594fSAndroid Build Coastguard Worker // destination, (to -> X) cannot be blocked while (from -> X) might still be
508*795d594fSAndroid Build Coastguard Worker // blocked. Consider for example the moves (0 -> 1) (1 -> 2) (1 -> 3). After
509*795d594fSAndroid Build Coastguard Worker // (1 -> 2) has been performed, the moves left are (0 -> 1) and (1 -> 3). There is
510*795d594fSAndroid Build Coastguard Worker // a dependency between the two. If we update the source location from 1 to 2, we
511*795d594fSAndroid Build Coastguard Worker // will get (0 -> 1) and (2 -> 3). There is no dependency between the two.
512*795d594fSAndroid Build Coastguard Worker //
513*795d594fSAndroid Build Coastguard Worker // This is not something we must do, but we can use fewer scratch locations with
514*795d594fSAndroid Build Coastguard Worker // this trick. For example, we can avoid using additional scratch locations for
515*795d594fSAndroid Build Coastguard Worker // moves (0 -> 1), (1 -> 2), (1 -> 0).
516*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : moves_) {
517*795d594fSAndroid Build Coastguard Worker if (move->GetSource().Equals(from)) {
518*795d594fSAndroid Build Coastguard Worker move->SetSource(to);
519*795d594fSAndroid Build Coastguard Worker }
520*795d594fSAndroid Build Coastguard Worker }
521*795d594fSAndroid Build Coastguard Worker }
522*795d594fSAndroid Build Coastguard Worker
AddPendingMove(Location source,Location destination,DataType::Type type)523*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::AddPendingMove(Location source,
524*795d594fSAndroid Build Coastguard Worker Location destination,
525*795d594fSAndroid Build Coastguard Worker DataType::Type type) {
526*795d594fSAndroid Build Coastguard Worker pending_moves_.push_back(new (allocator_) MoveOperands(source, destination, type, nullptr));
527*795d594fSAndroid Build Coastguard Worker }
528*795d594fSAndroid Build Coastguard Worker
DeletePendingMove(MoveOperands * move)529*795d594fSAndroid Build Coastguard Worker void ParallelMoveResolverNoSwap::DeletePendingMove(MoveOperands* move) {
530*795d594fSAndroid Build Coastguard Worker RemoveElement(pending_moves_, move);
531*795d594fSAndroid Build Coastguard Worker }
532*795d594fSAndroid Build Coastguard Worker
GetUnblockedPendingMove(Location loc)533*795d594fSAndroid Build Coastguard Worker MoveOperands* ParallelMoveResolverNoSwap::GetUnblockedPendingMove(Location loc) {
534*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : pending_moves_) {
535*795d594fSAndroid Build Coastguard Worker Location destination = move->GetDestination();
536*795d594fSAndroid Build Coastguard Worker // Only moves with destination overlapping with input loc can be unblocked.
537*795d594fSAndroid Build Coastguard Worker if (destination.OverlapsWith(loc) && !IsBlockedByMoves(destination)) {
538*795d594fSAndroid Build Coastguard Worker return move;
539*795d594fSAndroid Build Coastguard Worker }
540*795d594fSAndroid Build Coastguard Worker }
541*795d594fSAndroid Build Coastguard Worker return nullptr;
542*795d594fSAndroid Build Coastguard Worker }
543*795d594fSAndroid Build Coastguard Worker
IsBlockedByMoves(Location loc)544*795d594fSAndroid Build Coastguard Worker bool ParallelMoveResolverNoSwap::IsBlockedByMoves(Location loc) {
545*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : pending_moves_) {
546*795d594fSAndroid Build Coastguard Worker if (move->Blocks(loc)) {
547*795d594fSAndroid Build Coastguard Worker return true;
548*795d594fSAndroid Build Coastguard Worker }
549*795d594fSAndroid Build Coastguard Worker }
550*795d594fSAndroid Build Coastguard Worker for (MoveOperands* move : moves_) {
551*795d594fSAndroid Build Coastguard Worker if (move->Blocks(loc)) {
552*795d594fSAndroid Build Coastguard Worker return true;
553*795d594fSAndroid Build Coastguard Worker }
554*795d594fSAndroid Build Coastguard Worker }
555*795d594fSAndroid Build Coastguard Worker return false;
556*795d594fSAndroid Build Coastguard Worker }
557*795d594fSAndroid Build Coastguard Worker
558*795d594fSAndroid Build Coastguard Worker // So far it is only used for debugging purposes to make sure all pending moves
559*795d594fSAndroid Build Coastguard Worker // have been performed.
GetNumberOfPendingMoves()560*795d594fSAndroid Build Coastguard Worker size_t ParallelMoveResolverNoSwap::GetNumberOfPendingMoves() {
561*795d594fSAndroid Build Coastguard Worker return pending_moves_.size();
562*795d594fSAndroid Build Coastguard Worker }
563*795d594fSAndroid Build Coastguard Worker
564*795d594fSAndroid Build Coastguard Worker } // namespace art
565