Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace art { |
| 22 | |
| 23 | void 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 Geoffray | 48c310c | 2015-01-14 10:45:05 +0000 | [diff] [blame] | 40 | MoveOperands* move = moves_.Get(i); |
| 41 | if (!move->IsEliminated()) { |
| 42 | DCHECK(move->GetSource().IsConstant()); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 43 | EmitMove(i); |
Nicolas Geoffray | 48c310c | 2015-01-14 10:45:05 +0000 | [diff] [blame] | 44 | // Eliminate the move, in case following moves need a scratch register. |
| 45 | move->Eliminate(); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | moves_.Reset(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void 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 | |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 66 | // Update the source of `move`, knowing that `updated_location` has been swapped |
| 67 | // with `new_source`. Note that `updated_location` can be a pair, therefore if |
| 68 | // `move` is non-pair, we need to extract which register to use. |
| 69 | static void UpdateSourceOf(MoveOperands* move, Location updated_location, Location new_source) { |
| 70 | Location source = move->GetSource(); |
| 71 | if (new_source.GetKind() == source.GetKind()) { |
| 72 | DCHECK(updated_location.Equals(source)); |
| 73 | move->SetSource(new_source); |
| 74 | } else if (new_source.IsStackSlot() |
| 75 | || new_source.IsDoubleStackSlot() |
| 76 | || source.IsStackSlot() |
| 77 | || source.IsDoubleStackSlot()) { |
| 78 | // Stack slots never take part of a pair/non-pair swap. |
| 79 | DCHECK(updated_location.Equals(source)); |
| 80 | move->SetSource(new_source); |
| 81 | } else if (source.IsRegister()) { |
| 82 | DCHECK(new_source.IsRegisterPair()) << new_source; |
| 83 | DCHECK(updated_location.IsRegisterPair()) << updated_location; |
| 84 | if (updated_location.low() == source.reg()) { |
| 85 | move->SetSource(Location::RegisterLocation(new_source.low())); |
| 86 | } else { |
| 87 | DCHECK_EQ(updated_location.high(), source.reg()); |
| 88 | move->SetSource(Location::RegisterLocation(new_source.high())); |
| 89 | } |
| 90 | } else if (source.IsFpuRegister()) { |
| 91 | DCHECK(new_source.IsFpuRegisterPair()) << new_source; |
| 92 | DCHECK(updated_location.IsFpuRegisterPair()) << updated_location; |
| 93 | if (updated_location.low() == source.reg()) { |
| 94 | move->SetSource(Location::FpuRegisterLocation(new_source.low())); |
| 95 | } else { |
| 96 | DCHECK_EQ(updated_location.high(), source.reg()); |
| 97 | move->SetSource(Location::FpuRegisterLocation(new_source.high())); |
| 98 | } |
| 99 | } |
| 100 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 101 | |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 102 | MoveOperands* ParallelMoveResolver::PerformMove(size_t index) { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 103 | // Each call to this function performs a move and deletes it from the move |
| 104 | // graph. We first recursively perform any move blocking this one. We |
| 105 | // mark a move as "pending" on entry to PerformMove in order to detect |
| 106 | // cycles in the move graph. We use operand swaps to resolve cycles, |
| 107 | // which means that a call to PerformMove could change any source operand |
| 108 | // in the move graph. |
| 109 | |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 110 | MoveOperands* move = moves_.Get(index); |
| 111 | DCHECK(!move->IsPending()); |
| 112 | if (move->IsRedundant()) { |
| 113 | // Because we swap register pairs first, following, un-pending |
| 114 | // moves may become redundant. |
| 115 | move->Eliminate(); |
| 116 | return nullptr; |
| 117 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 118 | |
| 119 | // Clear this move's destination to indicate a pending move. The actual |
| 120 | // destination is saved in a stack-allocated local. Recursion may allow |
| 121 | // multiple moves to be pending. |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 122 | DCHECK(!move->GetSource().IsInvalid()); |
| 123 | Location destination = move->MarkPending(); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 124 | |
| 125 | // Perform a depth-first traversal of the move graph to resolve |
| 126 | // dependencies. Any unperformed, unpending move with a source the same |
| 127 | // as this one's destination blocks this one so recursively perform all |
| 128 | // such moves. |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 129 | MoveOperands* required_swap = nullptr; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 130 | for (size_t i = 0; i < moves_.Size(); ++i) { |
| 131 | const MoveOperands& other_move = *moves_.Get(i); |
| 132 | if (other_move.Blocks(destination) && !other_move.IsPending()) { |
| 133 | // Though PerformMove can change any source operand in the move graph, |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 134 | // calling `PerformMove` cannot create a blocking move via a swap |
| 135 | // (this loop does not miss any). |
| 136 | // For example, assume there is a non-blocking move with source A |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 137 | // and this move is blocked on source B and there is a swap of A and |
| 138 | // B. Then A and B must be involved in the same cycle (or they would |
| 139 | // not be swapped). Since this move's destination is B and there is |
| 140 | // only a single incoming edge to an operand, this move must also be |
| 141 | // involved in the same cycle. In that case, the blocking move will |
| 142 | // be created but will be "pending" when we return from PerformMove. |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 143 | required_swap = PerformMove(i); |
| 144 | |
| 145 | if (required_swap == move) { |
| 146 | // If this move is required to swap, we do so without looking |
| 147 | // at the next moves. Swapping is not blocked by anything, it just |
| 148 | // updates other moves's source. |
| 149 | break; |
| 150 | } else if (required_swap == moves_.Get(i)) { |
| 151 | // If `other_move` was swapped, we iterate again to find a new |
| 152 | // potential cycle. |
| 153 | required_swap = nullptr; |
| 154 | i = 0; |
| 155 | } else if (required_swap != nullptr) { |
| 156 | // A move is required to swap. We walk back the cycle to find the |
| 157 | // move by just returning from this `PerforrmMove`. |
| 158 | moves_.Get(index)->ClearPending(destination); |
| 159 | return required_swap; |
| 160 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 163 | |
| 164 | // We are about to resolve this move and don't need it marked as |
| 165 | // pending, so restore its destination. |
| 166 | move->ClearPending(destination); |
| 167 | |
| 168 | // This move's source may have changed due to swaps to resolve cycles and |
| 169 | // so it may now be the last move in the cycle. If so remove it. |
| 170 | if (move->GetSource().Equals(destination)) { |
| 171 | move->Eliminate(); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 172 | DCHECK(required_swap == nullptr); |
| 173 | return nullptr; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // The move may be blocked on a (at most one) pending move, in which case |
| 177 | // we have a cycle. Search for such a blocking move and perform a swap to |
| 178 | // resolve it. |
| 179 | bool do_swap = false; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 180 | if (required_swap != nullptr) { |
| 181 | DCHECK_EQ(required_swap, move); |
| 182 | do_swap = true; |
| 183 | } else { |
| 184 | for (size_t i = 0; i < moves_.Size(); ++i) { |
| 185 | const MoveOperands& other_move = *moves_.Get(i); |
| 186 | if (other_move.Blocks(destination)) { |
| 187 | DCHECK(other_move.IsPending()); |
| 188 | if (!destination.IsPair() && other_move.GetSource().IsPair()) { |
| 189 | // We swap pairs before swapping non-pairs. Go back from the |
| 190 | // cycle by returning the pair that must be swapped. |
| 191 | return moves_.Get(i); |
| 192 | } |
| 193 | do_swap = true; |
| 194 | break; |
| 195 | } |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | if (do_swap) { |
| 200 | EmitSwap(index); |
| 201 | // Any unperformed (including pending) move with a source of either |
| 202 | // this move's source or destination needs to have their source |
| 203 | // changed to reflect the state of affairs after the swap. |
| 204 | Location source = move->GetSource(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 205 | Location swap_destination = move->GetDestination(); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 206 | move->Eliminate(); |
| 207 | for (size_t i = 0; i < moves_.Size(); ++i) { |
| 208 | const MoveOperands& other_move = *moves_.Get(i); |
| 209 | if (other_move.Blocks(source)) { |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 210 | UpdateSourceOf(moves_.Get(i), source, swap_destination); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 211 | } else if (other_move.Blocks(swap_destination)) { |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 212 | UpdateSourceOf(moves_.Get(i), swap_destination, source); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 215 | // If the swap was required because of a pair in the middle of a cycle, |
| 216 | // we return the swapped move, so that the caller knows it needs to re-iterate |
| 217 | // its dependency loop. |
| 218 | return required_swap; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 219 | } else { |
| 220 | // This move is not blocked. |
| 221 | EmitMove(index); |
| 222 | move->Eliminate(); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame^] | 223 | DCHECK(required_swap == nullptr); |
| 224 | return nullptr; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 228 | bool ParallelMoveResolver::IsScratchLocation(Location loc) { |
| 229 | for (size_t i = 0; i < moves_.Size(); ++i) { |
| 230 | if (moves_.Get(i)->Blocks(loc)) { |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (size_t i = 0; i < moves_.Size(); ++i) { |
| 236 | if (moves_.Get(i)->GetDestination().Equals(loc)) { |
| 237 | return true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 244 | int ParallelMoveResolver::AllocateScratchRegister(int blocked, |
| 245 | int register_count, |
| 246 | int if_scratch, |
| 247 | bool* spilled) { |
| 248 | DCHECK_NE(blocked, if_scratch); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 249 | int scratch = -1; |
| 250 | for (int reg = 0; reg < register_count; ++reg) { |
Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 251 | if ((blocked != reg) && IsScratchLocation(Location::RegisterLocation(reg))) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 252 | scratch = reg; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (scratch == -1) { |
| 258 | *spilled = true; |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 259 | scratch = if_scratch; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 260 | } else { |
| 261 | *spilled = false; |
| 262 | } |
| 263 | |
| 264 | return scratch; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | ParallelMoveResolver::ScratchRegisterScope::ScratchRegisterScope( |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 269 | ParallelMoveResolver* resolver, int blocked, int if_scratch, int number_of_registers) |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 270 | : resolver_(resolver), |
| 271 | reg_(kNoRegister), |
| 272 | spilled_(false) { |
Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 273 | reg_ = resolver_->AllocateScratchRegister(blocked, number_of_registers, if_scratch, &spilled_); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 274 | |
| 275 | if (spilled_) { |
| 276 | resolver->SpillScratch(reg_); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | ParallelMoveResolver::ScratchRegisterScope::~ScratchRegisterScope() { |
| 282 | if (spilled_) { |
| 283 | resolver_->RestoreScratch(reg_); |
| 284 | } |
| 285 | } |
| 286 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 287 | } // namespace art |