blob: fcf1f913785b83ebae7244412683940bdbe58cfa [file] [log] [blame]
Ben Murdochb8e0da22011-05-16 14:20:40 +01001// 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
Steve Block44f0eee2011-05-26 01:26:41 +010028#include "v8.h"
29
Ben Murdoch8b112d22011-06-08 16:22:53 +010030#if defined(V8_TARGET_ARCH_IA32)
31
Ben Murdochb8e0da22011-05-16 14:20:40 +010032#include "ia32/lithium-gap-resolver-ia32.h"
33#include "ia32/lithium-codegen-ia32.h"
34
35namespace v8 {
36namespace internal {
37
38LGapResolver::LGapResolver(LCodeGen* owner)
Steve Block1e0659c2011-05-24 12:43:12 +010039 : cgen_(owner),
40 moves_(32),
41 source_uses_(),
42 destination_uses_(),
43 spilled_register_(-1) {}
Ben Murdochb8e0da22011-05-16 14:20:40 +010044
45
46void 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
74void 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
88void 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
153void 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
160 moves_.Add(move);
161}
162
163
164void 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
181int 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
192Register LGapResolver::GetFreeRegisterNot(Register reg) {
193 int skip_index = reg.is(no_reg) ? -1 : Register::ToAllocationIndex(reg);
194 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
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
203bool LGapResolver::HasBeenReset() {
204 if (!moves_.is_empty()) return false;
205 if (spilled_register_ >= 0) return false;
206
207 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
208 if (source_uses_[i] != 0) return false;
209 if (destination_uses_[i] != 0) return false;
210 }
211 return true;
212}
213
214
215void 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
230void 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
239void 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
247Register 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.
259 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
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
276void 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()) {
306 ASSERT(destination->IsRegister() || destination->IsStackSlot());
307 Immediate src = cgen_->ToImmediate(source);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000308 if (destination->IsRegister()) {
309 Register dst = cgen_->ToRegister(destination);
310 __ Set(dst, src);
311 } else {
312 Operand dst = cgen_->ToOperand(destination);
313 __ Set(dst, src);
314 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100315
316 } else if (source->IsDoubleRegister()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100317 XMMRegister src = cgen_->ToDoubleRegister(source);
Ben Murdoch257744e2011-11-30 15:57:28 +0000318 if (destination->IsDoubleRegister()) {
319 XMMRegister dst = cgen_->ToDoubleRegister(destination);
320 __ movaps(dst, src);
321 } else {
322 ASSERT(destination->IsDoubleStackSlot());
323 Operand dst = cgen_->ToOperand(destination);
324 __ movdbl(dst, src);
325 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100326 } else if (source->IsDoubleStackSlot()) {
327 ASSERT(destination->IsDoubleRegister() ||
328 destination->IsDoubleStackSlot());
329 Operand src = cgen_->ToOperand(source);
330 if (destination->IsDoubleRegister()) {
331 XMMRegister dst = cgen_->ToDoubleRegister(destination);
332 __ movdbl(dst, src);
333 } else {
334 // We rely on having xmm0 available as a fixed scratch register.
335 Operand dst = cgen_->ToOperand(destination);
336 __ movdbl(xmm0, src);
337 __ movdbl(dst, xmm0);
338 }
339
340 } else {
341 UNREACHABLE();
342 }
343
344 RemoveMove(index);
345}
346
347
348void LGapResolver::EmitSwap(int index) {
349 LOperand* source = moves_[index].source();
350 LOperand* destination = moves_[index].destination();
351 EnsureRestored(source);
352 EnsureRestored(destination);
353
354 // Dispatch on the source and destination operand kinds. Not all
355 // combinations are possible.
356 if (source->IsRegister() && destination->IsRegister()) {
357 // Register-register.
358 Register src = cgen_->ToRegister(source);
359 Register dst = cgen_->ToRegister(destination);
360 __ xchg(dst, src);
361
362 } else if ((source->IsRegister() && destination->IsStackSlot()) ||
363 (source->IsStackSlot() && destination->IsRegister())) {
364 // Register-memory. Use a free register as a temp if possible. Do not
365 // spill on demand because the simple spill implementation cannot avoid
366 // spilling src at this point.
367 Register tmp = GetFreeRegisterNot(no_reg);
368 Register reg =
369 cgen_->ToRegister(source->IsRegister() ? source : destination);
370 Operand mem =
371 cgen_->ToOperand(source->IsRegister() ? destination : source);
372 if (tmp.is(no_reg)) {
373 __ xor_(reg, mem);
374 __ xor_(mem, reg);
375 __ xor_(reg, mem);
376 } else {
377 __ mov(tmp, mem);
378 __ mov(mem, reg);
379 __ mov(reg, tmp);
380 }
381
382 } else if (source->IsStackSlot() && destination->IsStackSlot()) {
383 // Memory-memory. Spill on demand to use a temporary. If there is a
384 // free register after that, use it as a second temporary.
385 Register tmp0 = EnsureTempRegister();
386 Register tmp1 = GetFreeRegisterNot(tmp0);
387 Operand src = cgen_->ToOperand(source);
388 Operand dst = cgen_->ToOperand(destination);
389 if (tmp1.is(no_reg)) {
390 // Only one temp register available to us.
391 __ mov(tmp0, dst);
392 __ xor_(tmp0, src);
393 __ xor_(src, tmp0);
394 __ xor_(tmp0, src);
395 __ mov(dst, tmp0);
396 } else {
397 __ mov(tmp0, dst);
398 __ mov(tmp1, src);
399 __ mov(dst, tmp1);
400 __ mov(src, tmp0);
401 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000402 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
403 // XMM register-register swap. We rely on having xmm0
404 // available as a fixed scratch register.
405 XMMRegister src = cgen_->ToDoubleRegister(source);
406 XMMRegister dst = cgen_->ToDoubleRegister(destination);
407 __ movaps(xmm0, src);
408 __ movaps(src, dst);
409 __ movaps(dst, xmm0);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100410
411 } else if (source->IsDoubleRegister() || destination->IsDoubleRegister()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000412 // XMM register-memory swap. We rely on having xmm0
Ben Murdochb8e0da22011-05-16 14:20:40 +0100413 // available as a fixed scratch register.
Ben Murdoch257744e2011-11-30 15:57:28 +0000414 ASSERT(source->IsDoubleStackSlot() || destination->IsDoubleStackSlot());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100415 XMMRegister reg = cgen_->ToDoubleRegister(source->IsDoubleRegister()
416 ? source
417 : destination);
418 Operand other =
419 cgen_->ToOperand(source->IsDoubleRegister() ? destination : source);
420 __ movdbl(xmm0, other);
421 __ movdbl(other, reg);
422 __ movdbl(reg, Operand(xmm0));
423
424 } else if (source->IsDoubleStackSlot() && destination->IsDoubleStackSlot()) {
425 // Double-width memory-to-memory. Spill on demand to use a general
426 // purpose temporary register and also rely on having xmm0 available as
427 // a fixed scratch register.
428 Register tmp = EnsureTempRegister();
429 Operand src0 = cgen_->ToOperand(source);
430 Operand src1 = cgen_->HighOperand(source);
431 Operand dst0 = cgen_->ToOperand(destination);
432 Operand dst1 = cgen_->HighOperand(destination);
433 __ movdbl(xmm0, dst0); // Save destination in xmm0.
434 __ mov(tmp, src0); // Then use tmp to copy source to destination.
435 __ mov(dst0, tmp);
436 __ mov(tmp, src1);
437 __ mov(dst1, tmp);
438 __ movdbl(src0, xmm0);
439
440 } else {
441 // No other combinations are possible.
442 UNREACHABLE();
443 }
444
445 // The swap of source and destination has executed a move from source to
446 // destination.
447 RemoveMove(index);
448
449 // Any unperformed (including pending) move with a source of either
450 // this move's source or destination needs to have their source
451 // changed to reflect the state of affairs after the swap.
452 for (int i = 0; i < moves_.length(); ++i) {
453 LMoveOperands other_move = moves_[i];
454 if (other_move.Blocks(source)) {
455 moves_[i].set_source(destination);
456 } else if (other_move.Blocks(destination)) {
457 moves_[i].set_source(source);
458 }
459 }
460
461 // In addition to swapping the actual uses as sources, we need to update
462 // the use counts.
463 if (source->IsRegister() && destination->IsRegister()) {
464 int temp = source_uses_[source->index()];
465 source_uses_[source->index()] = source_uses_[destination->index()];
466 source_uses_[destination->index()] = temp;
467 } else if (source->IsRegister()) {
468 // We don't have use counts for non-register operands like destination.
469 // Compute those counts now.
470 source_uses_[source->index()] = CountSourceUses(source);
471 } else if (destination->IsRegister()) {
472 source_uses_[destination->index()] = CountSourceUses(destination);
473 }
474}
475
476#undef __
477
478} } // namespace v8::internal
Ben Murdoch8b112d22011-06-08 16:22:53 +0100479
480#endif // V8_TARGET_ARCH_IA32