Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / ia32 / lithium-gap-resolver-ia32.cc
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_IA32
8
9 #include "src/ia32/lithium-codegen-ia32.h"
10 #include "src/ia32/lithium-gap-resolver-ia32.h"
11
12 namespace v8 {
13 namespace internal {
14
15 LGapResolver::LGapResolver(LCodeGen* owner)
16     : cgen_(owner),
17       moves_(32, owner->zone()),
18       source_uses_(),
19       destination_uses_(),
20       spilled_register_(-1) {}
21
22
23 void LGapResolver::Resolve(LParallelMove* parallel_move) {
24   DCHECK(HasBeenReset());
25   // Build up a worklist of moves.
26   BuildInitialMoveList(parallel_move);
27
28   for (int i = 0; i < moves_.length(); ++i) {
29     LMoveOperands move = moves_[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.source()->IsConstantOperand()) {
34       PerformMove(i);
35     }
36   }
37
38   // Perform the moves with constant sources.
39   for (int i = 0; i < moves_.length(); ++i) {
40     if (!moves_[i].IsEliminated()) {
41       DCHECK(moves_[i].source()->IsConstantOperand());
42       EmitMove(i);
43     }
44   }
45
46   Finish();
47   DCHECK(HasBeenReset());
48 }
49
50
51 void LGapResolver::BuildInitialMoveList(LParallelMove* parallel_move) {
52   // Perform a linear sweep of the moves to add them to the initial list of
53   // moves to perform, ignoring any move that is redundant (the source is
54   // the same as the destination, the destination is ignored and
55   // unallocated, or the move was already eliminated).
56   const ZoneList<LMoveOperands>* moves = parallel_move->move_operands();
57   for (int i = 0; i < moves->length(); ++i) {
58     LMoveOperands move = moves->at(i);
59     if (!move.IsRedundant()) AddMove(move);
60   }
61   Verify();
62 }
63
64
65 void LGapResolver::PerformMove(int index) {
66   // Each call to this function performs a move and deletes it from the move
67   // graph.  We first recursively perform any move blocking this one.  We
68   // mark a move as "pending" on entry to PerformMove in order to detect
69   // cycles in the move graph.  We use operand swaps to resolve cycles,
70   // which means that a call to PerformMove could change any source operand
71   // in the move graph.
72
73   DCHECK(!moves_[index].IsPending());
74   DCHECK(!moves_[index].IsRedundant());
75
76   // Clear this move's destination to indicate a pending move.  The actual
77   // destination is saved on the side.
78   DCHECK(moves_[index].source() != NULL);  // Or else it will look eliminated.
79   LOperand* destination = moves_[index].destination();
80   moves_[index].set_destination(NULL);
81
82   // Perform a depth-first traversal of the move graph to resolve
83   // dependencies.  Any unperformed, unpending move with a source the same
84   // as this one's destination blocks this one so recursively perform all
85   // such moves.
86   for (int i = 0; i < moves_.length(); ++i) {
87     LMoveOperands other_move = moves_[i];
88     if (other_move.Blocks(destination) && !other_move.IsPending()) {
89       // Though PerformMove can change any source operand in the move graph,
90       // this call cannot create a blocking move via a swap (this loop does
91       // not miss any).  Assume there is a non-blocking move with source A
92       // and this move is blocked on source B and there is a swap of A and
93       // B.  Then A and B must be involved in the same cycle (or they would
94       // not be swapped).  Since this move's destination is B and there is
95       // only a single incoming edge to an operand, this move must also be
96       // involved in the same cycle.  In that case, the blocking move will
97       // be created but will be "pending" when we return from PerformMove.
98       PerformMove(i);
99     }
100   }
101
102   // We are about to resolve this move and don't need it marked as
103   // pending, so restore its destination.
104   moves_[index].set_destination(destination);
105
106   // This move's source may have changed due to swaps to resolve cycles and
107   // so it may now be the last move in the cycle.  If so remove it.
108   if (moves_[index].source()->Equals(destination)) {
109     RemoveMove(index);
110     return;
111   }
112
113   // The move may be blocked on a (at most one) pending move, in which case
114   // we have a cycle.  Search for such a blocking move and perform a swap to
115   // resolve it.
116   for (int i = 0; i < moves_.length(); ++i) {
117     LMoveOperands other_move = moves_[i];
118     if (other_move.Blocks(destination)) {
119       DCHECK(other_move.IsPending());
120       EmitSwap(index);
121       return;
122     }
123   }
124
125   // This move is not blocked.
126   EmitMove(index);
127 }
128
129
130 void LGapResolver::AddMove(LMoveOperands move) {
131   LOperand* source = move.source();
132   if (source->IsRegister()) ++source_uses_[source->index()];
133
134   LOperand* destination = move.destination();
135   if (destination->IsRegister()) ++destination_uses_[destination->index()];
136
137   moves_.Add(move, cgen_->zone());
138 }
139
140
141 void LGapResolver::RemoveMove(int index) {
142   LOperand* source = moves_[index].source();
143   if (source->IsRegister()) {
144     --source_uses_[source->index()];
145     DCHECK(source_uses_[source->index()] >= 0);
146   }
147
148   LOperand* destination = moves_[index].destination();
149   if (destination->IsRegister()) {
150     --destination_uses_[destination->index()];
151     DCHECK(destination_uses_[destination->index()] >= 0);
152   }
153
154   moves_[index].Eliminate();
155 }
156
157
158 int LGapResolver::CountSourceUses(LOperand* operand) {
159   int count = 0;
160   for (int i = 0; i < moves_.length(); ++i) {
161     if (!moves_[i].IsEliminated() && moves_[i].source()->Equals(operand)) {
162       ++count;
163     }
164   }
165   return count;
166 }
167
168
169 Register LGapResolver::GetFreeRegisterNot(Register reg) {
170   int skip_index = reg.is(no_reg) ? -1 : Register::ToAllocationIndex(reg);
171   for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) {
172     if (source_uses_[i] == 0 && destination_uses_[i] > 0 && i != skip_index) {
173       return Register::FromAllocationIndex(i);
174     }
175   }
176   return no_reg;
177 }
178
179
180 bool LGapResolver::HasBeenReset() {
181   if (!moves_.is_empty()) return false;
182   if (spilled_register_ >= 0) return false;
183
184   for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) {
185     if (source_uses_[i] != 0) return false;
186     if (destination_uses_[i] != 0) return false;
187   }
188   return true;
189 }
190
191
192 void LGapResolver::Verify() {
193 #ifdef ENABLE_SLOW_DCHECKS
194   // No operand should be the destination for more than one move.
195   for (int i = 0; i < moves_.length(); ++i) {
196     LOperand* destination = moves_[i].destination();
197     for (int j = i + 1; j < moves_.length(); ++j) {
198       SLOW_DCHECK(!destination->Equals(moves_[j].destination()));
199     }
200   }
201 #endif
202 }
203
204
205 #define __ ACCESS_MASM(cgen_->masm())
206
207 void LGapResolver::Finish() {
208   if (spilled_register_ >= 0) {
209     __ pop(Register::FromAllocationIndex(spilled_register_));
210     spilled_register_ = -1;
211   }
212   moves_.Rewind(0);
213 }
214
215
216 void LGapResolver::EnsureRestored(LOperand* operand) {
217   if (operand->IsRegister() && operand->index() == spilled_register_) {
218     __ pop(Register::FromAllocationIndex(spilled_register_));
219     spilled_register_ = -1;
220   }
221 }
222
223
224 Register LGapResolver::EnsureTempRegister() {
225   // 1. We may have already spilled to create a temp register.
226   if (spilled_register_ >= 0) {
227     return Register::FromAllocationIndex(spilled_register_);
228   }
229
230   // 2. We may have a free register that we can use without spilling.
231   Register free = GetFreeRegisterNot(no_reg);
232   if (!free.is(no_reg)) return free;
233
234   // 3. Prefer to spill a register that is not used in any remaining move
235   // because it will not need to be restored until the end.
236   for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) {
237     if (source_uses_[i] == 0 && destination_uses_[i] == 0) {
238       Register scratch = Register::FromAllocationIndex(i);
239       __ push(scratch);
240       spilled_register_ = i;
241       return scratch;
242     }
243   }
244
245   // 4. Use an arbitrary register.  Register 0 is as arbitrary as any other.
246   Register scratch = Register::FromAllocationIndex(0);
247   __ push(scratch);
248   spilled_register_ = 0;
249   return scratch;
250 }
251
252
253 void LGapResolver::EmitMove(int index) {
254   LOperand* source = moves_[index].source();
255   LOperand* destination = moves_[index].destination();
256   EnsureRestored(source);
257   EnsureRestored(destination);
258
259   // Dispatch on the source and destination operand kinds.  Not all
260   // combinations are possible.
261   if (source->IsRegister()) {
262     DCHECK(destination->IsRegister() || destination->IsStackSlot());
263     Register src = cgen_->ToRegister(source);
264     Operand dst = cgen_->ToOperand(destination);
265     __ mov(dst, src);
266
267   } else if (source->IsStackSlot()) {
268     DCHECK(destination->IsRegister() || destination->IsStackSlot());
269     Operand src = cgen_->ToOperand(source);
270     if (destination->IsRegister()) {
271       Register dst = cgen_->ToRegister(destination);
272       __ mov(dst, src);
273     } else {
274       // Spill on demand to use a temporary register for memory-to-memory
275       // moves.
276       Register tmp = EnsureTempRegister();
277       Operand dst = cgen_->ToOperand(destination);
278       __ mov(tmp, src);
279       __ mov(dst, tmp);
280     }
281
282   } else if (source->IsConstantOperand()) {
283     LConstantOperand* constant_source = LConstantOperand::cast(source);
284     if (destination->IsRegister()) {
285       Register dst = cgen_->ToRegister(destination);
286       Representation r = cgen_->IsSmi(constant_source)
287           ? Representation::Smi() : Representation::Integer32();
288       if (cgen_->IsInteger32(constant_source)) {
289         __ Move(dst, cgen_->ToImmediate(constant_source, r));
290       } else {
291         __ LoadObject(dst, cgen_->ToHandle(constant_source));
292       }
293     } else if (destination->IsDoubleRegister()) {
294       double v = cgen_->ToDouble(constant_source);
295       uint64_t int_val = BitCast<uint64_t, double>(v);
296       int32_t lower = static_cast<int32_t>(int_val);
297       int32_t upper = static_cast<int32_t>(int_val >> kBitsPerInt);
298       XMMRegister dst = cgen_->ToDoubleRegister(destination);
299       if (int_val == 0) {
300         __ xorps(dst, dst);
301       } else {
302         __ push(Immediate(upper));
303         __ push(Immediate(lower));
304         __ movsd(dst, Operand(esp, 0));
305         __ add(esp, Immediate(kDoubleSize));
306       }
307     } else {
308       DCHECK(destination->IsStackSlot());
309       Operand dst = cgen_->ToOperand(destination);
310       Representation r = cgen_->IsSmi(constant_source)
311           ? Representation::Smi() : Representation::Integer32();
312       if (cgen_->IsInteger32(constant_source)) {
313         __ Move(dst, cgen_->ToImmediate(constant_source, r));
314       } else {
315         Register tmp = EnsureTempRegister();
316         __ LoadObject(tmp, cgen_->ToHandle(constant_source));
317         __ mov(dst, tmp);
318       }
319     }
320
321   } else if (source->IsDoubleRegister()) {
322     XMMRegister src = cgen_->ToDoubleRegister(source);
323     if (destination->IsDoubleRegister()) {
324       XMMRegister dst = cgen_->ToDoubleRegister(destination);
325       __ movaps(dst, src);
326     } else {
327       DCHECK(destination->IsDoubleStackSlot());
328       Operand dst = cgen_->ToOperand(destination);
329       __ movsd(dst, src);
330     }
331   } else if (source->IsDoubleStackSlot()) {
332     DCHECK(destination->IsDoubleRegister() ||
333            destination->IsDoubleStackSlot());
334     Operand src = cgen_->ToOperand(source);
335     if (destination->IsDoubleRegister()) {
336       XMMRegister dst = cgen_->ToDoubleRegister(destination);
337       __ movsd(dst, src);
338     } else {
339       // We rely on having xmm0 available as a fixed scratch register.
340       Operand dst = cgen_->ToOperand(destination);
341       __ movsd(xmm0, src);
342       __ movsd(dst, xmm0);
343     }
344   } else if (source->IsSIMD128Register()) {
345     XMMRegister src = cgen_->ToSIMD128Register(source);
346     if (destination->IsSIMD128Register()) {
347       __ movaps(cgen_->ToSIMD128Register(destination), src);
348     } else {
349       DCHECK(destination->IsSIMD128StackSlot());
350       __ movups(cgen_->ToOperand(destination), src);
351     }
352   } else if (source->IsSIMD128StackSlot()) {
353     Operand src = cgen_->ToOperand(source);
354     if (destination->IsSIMD128Register()) {
355       __ movups(cgen_->ToSIMD128Register(destination), src);
356     } else {
357       DCHECK(destination->IsSIMD128StackSlot());
358       __ movups(xmm0, src);
359       __ movups(cgen_->ToOperand(destination), xmm0);
360     }
361   } else {
362     UNREACHABLE();
363   }
364
365   RemoveMove(index);
366 }
367
368
369 void LGapResolver::EmitSwap(int index) {
370   LOperand* source = moves_[index].source();
371   LOperand* destination = moves_[index].destination();
372   EnsureRestored(source);
373   EnsureRestored(destination);
374
375   // Dispatch on the source and destination operand kinds.  Not all
376   // combinations are possible.
377   if (source->IsRegister() && destination->IsRegister()) {
378     // Register-register.
379     Register src = cgen_->ToRegister(source);
380     Register dst = cgen_->ToRegister(destination);
381     __ xchg(dst, src);
382
383   } else if ((source->IsRegister() && destination->IsStackSlot()) ||
384              (source->IsStackSlot() && destination->IsRegister())) {
385     // Register-memory.  Use a free register as a temp if possible.  Do not
386     // spill on demand because the simple spill implementation cannot avoid
387     // spilling src at this point.
388     Register tmp = GetFreeRegisterNot(no_reg);
389     Register reg =
390         cgen_->ToRegister(source->IsRegister() ? source : destination);
391     Operand mem =
392         cgen_->ToOperand(source->IsRegister() ? destination : source);
393     if (tmp.is(no_reg)) {
394       __ xor_(reg, mem);
395       __ xor_(mem, reg);
396       __ xor_(reg, mem);
397     } else {
398       __ mov(tmp, mem);
399       __ mov(mem, reg);
400       __ mov(reg, tmp);
401     }
402
403   } else if (source->IsStackSlot() && destination->IsStackSlot()) {
404     // Memory-memory.  Spill on demand to use a temporary.  If there is a
405     // free register after that, use it as a second temporary.
406     Register tmp0 = EnsureTempRegister();
407     Register tmp1 = GetFreeRegisterNot(tmp0);
408     Operand src = cgen_->ToOperand(source);
409     Operand dst = cgen_->ToOperand(destination);
410     if (tmp1.is(no_reg)) {
411       // Only one temp register available to us.
412       __ mov(tmp0, dst);
413       __ xor_(tmp0, src);
414       __ xor_(src, tmp0);
415       __ xor_(tmp0, src);
416       __ mov(dst, tmp0);
417     } else {
418       __ mov(tmp0, dst);
419       __ mov(tmp1, src);
420       __ mov(dst, tmp1);
421       __ mov(src, tmp0);
422     }
423   } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
424     // XMM register-register swap. We rely on having xmm0
425     // available as a fixed scratch register.
426     XMMRegister src = cgen_->ToDoubleRegister(source);
427     XMMRegister dst = cgen_->ToDoubleRegister(destination);
428     __ movaps(xmm0, src);
429     __ movaps(src, dst);
430     __ movaps(dst, xmm0);
431   } else if (source->IsDoubleRegister() || destination->IsDoubleRegister()) {
432     // XMM register-memory swap.  We rely on having xmm0
433     // available as a fixed scratch register.
434     DCHECK(source->IsDoubleStackSlot() || destination->IsDoubleStackSlot());
435     XMMRegister reg = cgen_->ToDoubleRegister(source->IsDoubleRegister()
436                                               ? source
437                                               : destination);
438     Operand other =
439         cgen_->ToOperand(source->IsDoubleRegister() ? destination : source);
440     __ movsd(xmm0, other);
441     __ movsd(other, reg);
442     __ movaps(reg, xmm0);
443   } else if (source->IsDoubleStackSlot() && destination->IsDoubleStackSlot()) {
444     // Double-width memory-to-memory.  Spill on demand to use a general
445     // purpose temporary register and also rely on having xmm0 available as
446     // a fixed scratch register.
447     Register tmp = EnsureTempRegister();
448     Operand src0 = cgen_->ToOperand(source);
449     Operand src1 = cgen_->HighOperand(source);
450     Operand dst0 = cgen_->ToOperand(destination);
451     Operand dst1 = cgen_->HighOperand(destination);
452     __ movsd(xmm0, dst0);  // Save destination in xmm0.
453     __ mov(tmp, src0);  // Then use tmp to copy source to destination.
454     __ mov(dst0, tmp);
455     __ mov(tmp, src1);
456     __ mov(dst1, tmp);
457     __ movsd(src0, xmm0);
458
459   } else if ((source->IsSIMD128StackSlot() &&
460               destination->IsSIMD128StackSlot())) {
461     // Swap two XMM stack slots.
462     Operand src = cgen_->ToOperand(source);
463     Operand dst = cgen_->ToOperand(destination);
464     Register tmp = EnsureTempRegister();
465     __ movups(xmm0, src);
466     for (int offset = 0; offset < kSIMD128Size; offset += kPointerSize) {
467       __ mov(tmp, Operand(dst, offset));
468       __ mov(Operand(src, offset), tmp);
469     }
470     __ movups(dst, xmm0);
471
472   } else if (source->IsSIMD128Register() && destination->IsSIMD128Register()) {
473     // Swap two XMM registers.
474     XMMRegister source_reg = cgen_->ToSIMD128Register(source);
475     XMMRegister destination_reg = cgen_->ToSIMD128Register(destination);
476     __ movaps(xmm0, source_reg);
477     __ movaps(source_reg, destination_reg);
478     __ movaps(destination_reg, xmm0);
479
480   } else if (source->IsSIMD128Register() || destination->IsSIMD128Register()) {
481     // Swap a xmm register and a xmm stack slot.
482     DCHECK((source->IsSIMD128Register() &&
483             destination->IsSIMD128StackSlot()) ||
484            (source->IsSIMD128StackSlot() &&
485             destination->IsSIMD128Register()));
486     XMMRegister reg = cgen_->ToSIMD128Register(source->IsSIMD128Register()
487                                                    ? source
488                                                    : destination);
489     LOperand* other = source->IsSIMD128Register() ? destination : source;
490     DCHECK(other->IsSIMD128StackSlot());
491     Operand other_operand = cgen_->ToOperand(other);
492     __ movups(xmm0, other_operand);
493     __ movups(other_operand, reg);
494     __ movaps(reg, xmm0);
495
496   } else {
497     // No other combinations are possible.
498     UNREACHABLE();
499   }
500
501   // The swap of source and destination has executed a move from source to
502   // destination.
503   RemoveMove(index);
504
505   // Any unperformed (including pending) move with a source of either
506   // this move's source or destination needs to have their source
507   // changed to reflect the state of affairs after the swap.
508   for (int i = 0; i < moves_.length(); ++i) {
509     LMoveOperands other_move = moves_[i];
510     if (other_move.Blocks(source)) {
511       moves_[i].set_source(destination);
512     } else if (other_move.Blocks(destination)) {
513       moves_[i].set_source(source);
514     }
515   }
516
517   // In addition to swapping the actual uses as sources, we need to update
518   // the use counts.
519   if (source->IsRegister() && destination->IsRegister()) {
520     int temp = source_uses_[source->index()];
521     source_uses_[source->index()] = source_uses_[destination->index()];
522     source_uses_[destination->index()] = temp;
523   } else if (source->IsRegister()) {
524     // We don't have use counts for non-register operands like destination.
525     // Compute those counts now.
526     source_uses_[source->index()] = CountSourceUses(source);
527   } else if (destination->IsRegister()) {
528     source_uses_[destination->index()] = CountSourceUses(destination);
529   }
530 }
531
532 #undef __
533
534 } }  // namespace v8::internal
535
536 #endif  // V8_TARGET_ARCH_IA32