erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 28 | #include "v8.h" |
| 29 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_IA32) |
| 31 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 32 | #include "ia32/lithium-gap-resolver-ia32.h" |
| 33 | #include "ia32/lithium-codegen-ia32.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | LGapResolver::LGapResolver(LCodeGen* owner) |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 39 | : cgen_(owner), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 40 | moves_(32, owner->zone()), |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 41 | source_uses_(), |
| 42 | destination_uses_(), |
| 43 | spilled_register_(-1) {} |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | void LGapResolver::Resolve(LParallelMove* parallel_move) { |
| 47 | ASSERT(HasBeenReset()); |
| 48 | // Build up a worklist of moves. |
| 49 | BuildInitialMoveList(parallel_move); |
| 50 | |
| 51 | for (int i = 0; i < moves_.length(); ++i) { |
| 52 | LMoveOperands move = moves_[i]; |
| 53 | // Skip constants to perform them last. They don't block other moves |
| 54 | // and skipping such moves with register destinations keeps those |
| 55 | // registers free for the whole algorithm. |
| 56 | if (!move.IsEliminated() && !move.source()->IsConstantOperand()) { |
| 57 | PerformMove(i); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Perform the moves with constant sources. |
| 62 | for (int i = 0; i < moves_.length(); ++i) { |
| 63 | if (!moves_[i].IsEliminated()) { |
| 64 | ASSERT(moves_[i].source()->IsConstantOperand()); |
| 65 | EmitMove(i); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | Finish(); |
| 70 | ASSERT(HasBeenReset()); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void LGapResolver::BuildInitialMoveList(LParallelMove* parallel_move) { |
| 75 | // Perform a linear sweep of the moves to add them to the initial list of |
| 76 | // moves to perform, ignoring any move that is redundant (the source is |
| 77 | // the same as the destination, the destination is ignored and |
| 78 | // unallocated, or the move was already eliminated). |
| 79 | const ZoneList<LMoveOperands>* moves = parallel_move->move_operands(); |
| 80 | for (int i = 0; i < moves->length(); ++i) { |
| 81 | LMoveOperands move = moves->at(i); |
| 82 | if (!move.IsRedundant()) AddMove(move); |
| 83 | } |
| 84 | Verify(); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | void LGapResolver::PerformMove(int index) { |
| 89 | // Each call to this function performs a move and deletes it from the move |
| 90 | // graph. We first recursively perform any move blocking this one. We |
| 91 | // mark a move as "pending" on entry to PerformMove in order to detect |
| 92 | // cycles in the move graph. We use operand swaps to resolve cycles, |
| 93 | // which means that a call to PerformMove could change any source operand |
| 94 | // in the move graph. |
| 95 | |
| 96 | ASSERT(!moves_[index].IsPending()); |
| 97 | ASSERT(!moves_[index].IsRedundant()); |
| 98 | |
| 99 | // Clear this move's destination to indicate a pending move. The actual |
| 100 | // destination is saved on the side. |
| 101 | ASSERT(moves_[index].source() != NULL); // Or else it will look eliminated. |
| 102 | LOperand* destination = moves_[index].destination(); |
| 103 | moves_[index].set_destination(NULL); |
| 104 | |
| 105 | // Perform a depth-first traversal of the move graph to resolve |
| 106 | // dependencies. Any unperformed, unpending move with a source the same |
| 107 | // as this one's destination blocks this one so recursively perform all |
| 108 | // such moves. |
| 109 | for (int i = 0; i < moves_.length(); ++i) { |
| 110 | LMoveOperands other_move = moves_[i]; |
| 111 | if (other_move.Blocks(destination) && !other_move.IsPending()) { |
| 112 | // Though PerformMove can change any source operand in the move graph, |
| 113 | // this call cannot create a blocking move via a swap (this loop does |
| 114 | // not miss any). Assume there is a non-blocking move with source A |
| 115 | // and this move is blocked on source B and there is a swap of A and |
| 116 | // B. Then A and B must be involved in the same cycle (or they would |
| 117 | // not be swapped). Since this move's destination is B and there is |
| 118 | // only a single incoming edge to an operand, this move must also be |
| 119 | // involved in the same cycle. In that case, the blocking move will |
| 120 | // be created but will be "pending" when we return from PerformMove. |
| 121 | PerformMove(i); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // We are about to resolve this move and don't need it marked as |
| 126 | // pending, so restore its destination. |
| 127 | moves_[index].set_destination(destination); |
| 128 | |
| 129 | // This move's source may have changed due to swaps to resolve cycles and |
| 130 | // so it may now be the last move in the cycle. If so remove it. |
| 131 | if (moves_[index].source()->Equals(destination)) { |
| 132 | RemoveMove(index); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // The move may be blocked on a (at most one) pending move, in which case |
| 137 | // we have a cycle. Search for such a blocking move and perform a swap to |
| 138 | // resolve it. |
| 139 | for (int i = 0; i < moves_.length(); ++i) { |
| 140 | LMoveOperands other_move = moves_[i]; |
| 141 | if (other_move.Blocks(destination)) { |
| 142 | ASSERT(other_move.IsPending()); |
| 143 | EmitSwap(index); |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // This move is not blocked. |
| 149 | EmitMove(index); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void LGapResolver::AddMove(LMoveOperands move) { |
| 154 | LOperand* source = move.source(); |
| 155 | if (source->IsRegister()) ++source_uses_[source->index()]; |
| 156 | |
| 157 | LOperand* destination = move.destination(); |
| 158 | if (destination->IsRegister()) ++destination_uses_[destination->index()]; |
| 159 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 160 | moves_.Add(move, cgen_->zone()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | void LGapResolver::RemoveMove(int index) { |
| 165 | LOperand* source = moves_[index].source(); |
| 166 | if (source->IsRegister()) { |
| 167 | --source_uses_[source->index()]; |
| 168 | ASSERT(source_uses_[source->index()] >= 0); |
| 169 | } |
| 170 | |
| 171 | LOperand* destination = moves_[index].destination(); |
| 172 | if (destination->IsRegister()) { |
| 173 | --destination_uses_[destination->index()]; |
| 174 | ASSERT(destination_uses_[destination->index()] >= 0); |
| 175 | } |
| 176 | |
| 177 | moves_[index].Eliminate(); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | int LGapResolver::CountSourceUses(LOperand* operand) { |
| 182 | int count = 0; |
| 183 | for (int i = 0; i < moves_.length(); ++i) { |
| 184 | if (!moves_[i].IsEliminated() && moves_[i].source()->Equals(operand)) { |
| 185 | ++count; |
| 186 | } |
| 187 | } |
| 188 | return count; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | Register LGapResolver::GetFreeRegisterNot(Register reg) { |
| 193 | int skip_index = reg.is(no_reg) ? -1 : Register::ToAllocationIndex(reg); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 194 | for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 195 | if (source_uses_[i] == 0 && destination_uses_[i] > 0 && i != skip_index) { |
| 196 | return Register::FromAllocationIndex(i); |
| 197 | } |
| 198 | } |
| 199 | return no_reg; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | bool LGapResolver::HasBeenReset() { |
| 204 | if (!moves_.is_empty()) return false; |
| 205 | if (spilled_register_ >= 0) return false; |
| 206 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 207 | for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 208 | if (source_uses_[i] != 0) return false; |
| 209 | if (destination_uses_[i] != 0) return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void LGapResolver::Verify() { |
| 216 | #ifdef ENABLE_SLOW_ASSERTS |
| 217 | // No operand should be the destination for more than one move. |
| 218 | for (int i = 0; i < moves_.length(); ++i) { |
| 219 | LOperand* destination = moves_[i].destination(); |
| 220 | for (int j = i + 1; j < moves_.length(); ++j) { |
| 221 | SLOW_ASSERT(!destination->Equals(moves_[j].destination())); |
| 222 | } |
| 223 | } |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #define __ ACCESS_MASM(cgen_->masm()) |
| 229 | |
| 230 | void LGapResolver::Finish() { |
| 231 | if (spilled_register_ >= 0) { |
| 232 | __ pop(Register::FromAllocationIndex(spilled_register_)); |
| 233 | spilled_register_ = -1; |
| 234 | } |
| 235 | moves_.Rewind(0); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void LGapResolver::EnsureRestored(LOperand* operand) { |
| 240 | if (operand->IsRegister() && operand->index() == spilled_register_) { |
| 241 | __ pop(Register::FromAllocationIndex(spilled_register_)); |
| 242 | spilled_register_ = -1; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | Register LGapResolver::EnsureTempRegister() { |
| 248 | // 1. We may have already spilled to create a temp register. |
| 249 | if (spilled_register_ >= 0) { |
| 250 | return Register::FromAllocationIndex(spilled_register_); |
| 251 | } |
| 252 | |
| 253 | // 2. We may have a free register that we can use without spilling. |
| 254 | Register free = GetFreeRegisterNot(no_reg); |
| 255 | if (!free.is(no_reg)) return free; |
| 256 | |
| 257 | // 3. Prefer to spill a register that is not used in any remaining move |
| 258 | // because it will not need to be restored until the end. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 259 | for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 260 | if (source_uses_[i] == 0 && destination_uses_[i] == 0) { |
| 261 | Register scratch = Register::FromAllocationIndex(i); |
| 262 | __ push(scratch); |
| 263 | spilled_register_ = i; |
| 264 | return scratch; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // 4. Use an arbitrary register. Register 0 is as arbitrary as any other. |
| 269 | Register scratch = Register::FromAllocationIndex(0); |
| 270 | __ push(scratch); |
| 271 | spilled_register_ = 0; |
| 272 | return scratch; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void LGapResolver::EmitMove(int index) { |
| 277 | LOperand* source = moves_[index].source(); |
| 278 | LOperand* destination = moves_[index].destination(); |
| 279 | EnsureRestored(source); |
| 280 | EnsureRestored(destination); |
| 281 | |
| 282 | // Dispatch on the source and destination operand kinds. Not all |
| 283 | // combinations are possible. |
| 284 | if (source->IsRegister()) { |
| 285 | ASSERT(destination->IsRegister() || destination->IsStackSlot()); |
| 286 | Register src = cgen_->ToRegister(source); |
| 287 | Operand dst = cgen_->ToOperand(destination); |
| 288 | __ mov(dst, src); |
| 289 | |
| 290 | } else if (source->IsStackSlot()) { |
| 291 | ASSERT(destination->IsRegister() || destination->IsStackSlot()); |
| 292 | Operand src = cgen_->ToOperand(source); |
| 293 | if (destination->IsRegister()) { |
| 294 | Register dst = cgen_->ToRegister(destination); |
| 295 | __ mov(dst, src); |
| 296 | } else { |
| 297 | // Spill on demand to use a temporary register for memory-to-memory |
| 298 | // moves. |
| 299 | Register tmp = EnsureTempRegister(); |
| 300 | Operand dst = cgen_->ToOperand(destination); |
| 301 | __ mov(tmp, src); |
| 302 | __ mov(dst, tmp); |
| 303 | } |
| 304 | |
| 305 | } else if (source->IsConstantOperand()) { |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 306 | LConstantOperand* constant_source = LConstantOperand::cast(source); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 307 | if (destination->IsRegister()) { |
| 308 | Register dst = cgen_->ToRegister(destination); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 309 | if (cgen_->IsSmi(constant_source)) { |
| 310 | __ Set(dst, cgen_->ToSmiImmediate(constant_source)); |
| 311 | } else if (cgen_->IsInteger32(constant_source)) { |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 312 | __ Set(dst, cgen_->ToInteger32Immediate(constant_source)); |
| 313 | } else { |
| 314 | __ LoadObject(dst, cgen_->ToHandle(constant_source)); |
| 315 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 316 | } else { |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 317 | ASSERT(destination->IsStackSlot()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 318 | Operand dst = cgen_->ToOperand(destination); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 319 | if (cgen_->IsSmi(constant_source)) { |
| 320 | __ Set(dst, cgen_->ToSmiImmediate(constant_source)); |
| 321 | } else if (cgen_->IsInteger32(constant_source)) { |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 322 | __ Set(dst, cgen_->ToInteger32Immediate(constant_source)); |
| 323 | } else { |
| 324 | Register tmp = EnsureTempRegister(); |
| 325 | __ LoadObject(tmp, cgen_->ToHandle(constant_source)); |
| 326 | __ mov(dst, tmp); |
| 327 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 328 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 329 | |
| 330 | } else if (source->IsDoubleRegister()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 331 | if (CpuFeatures::IsSupported(SSE2)) { |
| 332 | CpuFeatureScope scope(cgen_->masm(), SSE2); |
| 333 | XMMRegister src = cgen_->ToDoubleRegister(source); |
| 334 | if (destination->IsDoubleRegister()) { |
| 335 | XMMRegister dst = cgen_->ToDoubleRegister(destination); |
| 336 | __ movaps(dst, src); |
| 337 | } else { |
| 338 | ASSERT(destination->IsDoubleStackSlot()); |
| 339 | Operand dst = cgen_->ToOperand(destination); |
| 340 | __ movdbl(dst, src); |
| 341 | } |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 342 | } else { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 343 | // load from the register onto the stack, store in destination, which must |
| 344 | // be a double stack slot in the non-SSE2 case. |
| 345 | ASSERT(source->index() == 0); // source is on top of the stack |
ulan@chromium.org | 4121f23 | 2012-12-27 15:57:11 +0000 | [diff] [blame] | 346 | ASSERT(destination->IsDoubleStackSlot()); |
| 347 | Operand dst = cgen_->ToOperand(destination); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 348 | cgen_->ReadX87Operand(dst); |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 349 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 350 | } else if (source->IsDoubleStackSlot()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 351 | if (CpuFeatures::IsSupported(SSE2)) { |
| 352 | CpuFeatureScope scope(cgen_->masm(), SSE2); |
| 353 | ASSERT(destination->IsDoubleRegister() || |
| 354 | destination->IsDoubleStackSlot()); |
| 355 | Operand src = cgen_->ToOperand(source); |
| 356 | if (destination->IsDoubleRegister()) { |
| 357 | XMMRegister dst = cgen_->ToDoubleRegister(destination); |
| 358 | __ movdbl(dst, src); |
| 359 | } else { |
| 360 | // We rely on having xmm0 available as a fixed scratch register. |
| 361 | Operand dst = cgen_->ToOperand(destination); |
| 362 | __ movdbl(xmm0, src); |
| 363 | __ movdbl(dst, xmm0); |
| 364 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 365 | } else { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 366 | // load from the stack slot on top of the floating point stack, and then |
| 367 | // store in destination. If destination is a double register, then it |
| 368 | // represents the top of the stack and nothing needs to be done. |
| 369 | if (destination->IsDoubleStackSlot()) { |
| 370 | Register tmp = EnsureTempRegister(); |
| 371 | Operand src0 = cgen_->ToOperand(source); |
| 372 | Operand src1 = cgen_->HighOperand(source); |
| 373 | Operand dst0 = cgen_->ToOperand(destination); |
| 374 | Operand dst1 = cgen_->HighOperand(destination); |
| 375 | __ mov(tmp, src0); // Then use tmp to copy source to destination. |
| 376 | __ mov(dst0, tmp); |
| 377 | __ mov(tmp, src1); |
| 378 | __ mov(dst1, tmp); |
| 379 | } else { |
| 380 | Operand src = cgen_->ToOperand(source); |
| 381 | if (cgen_->X87StackNonEmpty()) { |
| 382 | cgen_->PopX87(); |
| 383 | } |
| 384 | cgen_->PushX87DoubleOperand(src); |
| 385 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 386 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 387 | } else { |
| 388 | UNREACHABLE(); |
| 389 | } |
| 390 | |
| 391 | RemoveMove(index); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | void LGapResolver::EmitSwap(int index) { |
| 396 | LOperand* source = moves_[index].source(); |
| 397 | LOperand* destination = moves_[index].destination(); |
| 398 | EnsureRestored(source); |
| 399 | EnsureRestored(destination); |
| 400 | |
| 401 | // Dispatch on the source and destination operand kinds. Not all |
| 402 | // combinations are possible. |
| 403 | if (source->IsRegister() && destination->IsRegister()) { |
| 404 | // Register-register. |
| 405 | Register src = cgen_->ToRegister(source); |
| 406 | Register dst = cgen_->ToRegister(destination); |
| 407 | __ xchg(dst, src); |
| 408 | |
| 409 | } else if ((source->IsRegister() && destination->IsStackSlot()) || |
| 410 | (source->IsStackSlot() && destination->IsRegister())) { |
| 411 | // Register-memory. Use a free register as a temp if possible. Do not |
| 412 | // spill on demand because the simple spill implementation cannot avoid |
| 413 | // spilling src at this point. |
| 414 | Register tmp = GetFreeRegisterNot(no_reg); |
| 415 | Register reg = |
| 416 | cgen_->ToRegister(source->IsRegister() ? source : destination); |
| 417 | Operand mem = |
| 418 | cgen_->ToOperand(source->IsRegister() ? destination : source); |
| 419 | if (tmp.is(no_reg)) { |
| 420 | __ xor_(reg, mem); |
| 421 | __ xor_(mem, reg); |
| 422 | __ xor_(reg, mem); |
| 423 | } else { |
| 424 | __ mov(tmp, mem); |
| 425 | __ mov(mem, reg); |
| 426 | __ mov(reg, tmp); |
| 427 | } |
| 428 | |
| 429 | } else if (source->IsStackSlot() && destination->IsStackSlot()) { |
| 430 | // Memory-memory. Spill on demand to use a temporary. If there is a |
| 431 | // free register after that, use it as a second temporary. |
| 432 | Register tmp0 = EnsureTempRegister(); |
| 433 | Register tmp1 = GetFreeRegisterNot(tmp0); |
| 434 | Operand src = cgen_->ToOperand(source); |
| 435 | Operand dst = cgen_->ToOperand(destination); |
| 436 | if (tmp1.is(no_reg)) { |
| 437 | // Only one temp register available to us. |
| 438 | __ mov(tmp0, dst); |
| 439 | __ xor_(tmp0, src); |
| 440 | __ xor_(src, tmp0); |
| 441 | __ xor_(tmp0, src); |
| 442 | __ mov(dst, tmp0); |
| 443 | } else { |
| 444 | __ mov(tmp0, dst); |
| 445 | __ mov(tmp1, src); |
| 446 | __ mov(dst, tmp1); |
| 447 | __ mov(src, tmp0); |
| 448 | } |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 449 | } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 450 | CpuFeatureScope scope(cgen_->masm(), SSE2); |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 451 | // XMM register-register swap. We rely on having xmm0 |
| 452 | // available as a fixed scratch register. |
| 453 | XMMRegister src = cgen_->ToDoubleRegister(source); |
| 454 | XMMRegister dst = cgen_->ToDoubleRegister(destination); |
| 455 | __ movaps(xmm0, src); |
| 456 | __ movaps(src, dst); |
| 457 | __ movaps(dst, xmm0); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 458 | } else if (source->IsDoubleRegister() || destination->IsDoubleRegister()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 459 | CpuFeatureScope scope(cgen_->masm(), SSE2); |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 460 | // XMM register-memory swap. We rely on having xmm0 |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 461 | // available as a fixed scratch register. |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 462 | ASSERT(source->IsDoubleStackSlot() || destination->IsDoubleStackSlot()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 463 | XMMRegister reg = cgen_->ToDoubleRegister(source->IsDoubleRegister() |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 464 | ? source |
| 465 | : destination); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 466 | Operand other = |
| 467 | cgen_->ToOperand(source->IsDoubleRegister() ? destination : source); |
| 468 | __ movdbl(xmm0, other); |
| 469 | __ movdbl(other, reg); |
| 470 | __ movdbl(reg, Operand(xmm0)); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 471 | } else if (source->IsDoubleStackSlot() && destination->IsDoubleStackSlot()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 472 | CpuFeatureScope scope(cgen_->masm(), SSE2); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 473 | // Double-width memory-to-memory. Spill on demand to use a general |
| 474 | // purpose temporary register and also rely on having xmm0 available as |
| 475 | // a fixed scratch register. |
| 476 | Register tmp = EnsureTempRegister(); |
| 477 | Operand src0 = cgen_->ToOperand(source); |
| 478 | Operand src1 = cgen_->HighOperand(source); |
| 479 | Operand dst0 = cgen_->ToOperand(destination); |
| 480 | Operand dst1 = cgen_->HighOperand(destination); |
| 481 | __ movdbl(xmm0, dst0); // Save destination in xmm0. |
| 482 | __ mov(tmp, src0); // Then use tmp to copy source to destination. |
| 483 | __ mov(dst0, tmp); |
| 484 | __ mov(tmp, src1); |
| 485 | __ mov(dst1, tmp); |
| 486 | __ movdbl(src0, xmm0); |
| 487 | |
| 488 | } else { |
| 489 | // No other combinations are possible. |
| 490 | UNREACHABLE(); |
| 491 | } |
| 492 | |
| 493 | // The swap of source and destination has executed a move from source to |
| 494 | // destination. |
| 495 | RemoveMove(index); |
| 496 | |
| 497 | // Any unperformed (including pending) move with a source of either |
| 498 | // this move's source or destination needs to have their source |
| 499 | // changed to reflect the state of affairs after the swap. |
| 500 | for (int i = 0; i < moves_.length(); ++i) { |
| 501 | LMoveOperands other_move = moves_[i]; |
| 502 | if (other_move.Blocks(source)) { |
| 503 | moves_[i].set_source(destination); |
| 504 | } else if (other_move.Blocks(destination)) { |
| 505 | moves_[i].set_source(source); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // In addition to swapping the actual uses as sources, we need to update |
| 510 | // the use counts. |
| 511 | if (source->IsRegister() && destination->IsRegister()) { |
| 512 | int temp = source_uses_[source->index()]; |
| 513 | source_uses_[source->index()] = source_uses_[destination->index()]; |
| 514 | source_uses_[destination->index()] = temp; |
| 515 | } else if (source->IsRegister()) { |
| 516 | // We don't have use counts for non-register operands like destination. |
| 517 | // Compute those counts now. |
| 518 | source_uses_[source->index()] = CountSourceUses(source); |
| 519 | } else if (destination->IsRegister()) { |
| 520 | source_uses_[destination->index()] = CountSourceUses(destination); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | #undef __ |
| 525 | |
| 526 | } } // namespace v8::internal |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 527 | |
| 528 | #endif // V8_TARGET_ARCH_IA32 |