blob: b4151567308b4227d74442c47114a2e33fdd4ae2 [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002// 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
28#include "v8.h"
29
30#include "mips/lithium-gap-resolver-mips.h"
31#include "mips/lithium-codegen-mips.h"
32
33namespace v8 {
34namespace internal {
35
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000036LGapResolver::LGapResolver(LCodeGen* owner)
37 : cgen_(owner),
mmassi@chromium.org7028c052012-06-13 11:51:58 +000038 moves_(32, owner->zone()),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000039 root_index_(0),
40 in_cycle_(false),
41 saved_destination_(NULL) {}
42
43
44void LGapResolver::Resolve(LParallelMove* parallel_move) {
45 ASSERT(moves_.is_empty());
46 // Build up a worklist of moves.
47 BuildInitialMoveList(parallel_move);
48
49 for (int i = 0; i < moves_.length(); ++i) {
50 LMoveOperands move = moves_[i];
51 // Skip constants to perform them last. They don't block other moves
52 // and skipping such moves with register destinations keeps those
53 // registers free for the whole algorithm.
54 if (!move.IsEliminated() && !move.source()->IsConstantOperand()) {
55 root_index_ = i; // Any cycle is found when by reaching this move again.
56 PerformMove(i);
57 if (in_cycle_) {
58 RestoreValue();
59 }
60 }
61 }
62
63 // Perform the moves with constant sources.
64 for (int i = 0; i < moves_.length(); ++i) {
65 if (!moves_[i].IsEliminated()) {
66 ASSERT(moves_[i].source()->IsConstantOperand());
67 EmitMove(i);
68 }
69 }
70
71 moves_.Rewind(0);
72}
73
74
75void LGapResolver::BuildInitialMoveList(LParallelMove* parallel_move) {
76 // Perform a linear sweep of the moves to add them to the initial list of
77 // moves to perform, ignoring any move that is redundant (the source is
78 // the same as the destination, the destination is ignored and
79 // unallocated, or the move was already eliminated).
80 const ZoneList<LMoveOperands>* moves = parallel_move->move_operands();
81 for (int i = 0; i < moves->length(); ++i) {
82 LMoveOperands move = moves->at(i);
mmassi@chromium.org7028c052012-06-13 11:51:58 +000083 if (!move.IsRedundant()) moves_.Add(move, cgen_->zone());
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000084 }
85 Verify();
86}
87
88
89void LGapResolver::PerformMove(int index) {
90 // Each call to this function performs a move and deletes it from the move
91 // graph. We first recursively perform any move blocking this one. We
92 // mark a move as "pending" on entry to PerformMove in order to detect
93 // cycles in the move graph.
94
95 // We can only find a cycle, when doing a depth-first traversal of moves,
96 // be encountering the starting move again. So by spilling the source of
97 // the starting move, we break the cycle. All moves are then unblocked,
98 // and the starting move is completed by writing the spilled value to
99 // its destination. All other moves from the spilled source have been
100 // completed prior to breaking the cycle.
101 // An additional complication is that moves to MemOperands with large
102 // offsets (more than 1K or 4K) require us to spill this spilled value to
103 // the stack, to free up the register.
104 ASSERT(!moves_[index].IsPending());
105 ASSERT(!moves_[index].IsRedundant());
106
107 // Clear this move's destination to indicate a pending move. The actual
108 // destination is saved in a stack allocated local. Multiple moves can
109 // be pending because this function is recursive.
110 ASSERT(moves_[index].source() != NULL); // Or else it will look eliminated.
111 LOperand* destination = moves_[index].destination();
112 moves_[index].set_destination(NULL);
113
114 // Perform a depth-first traversal of the move graph to resolve
115 // dependencies. Any unperformed, unpending move with a source the same
116 // as this one's destination blocks this one so recursively perform all
117 // such moves.
118 for (int i = 0; i < moves_.length(); ++i) {
119 LMoveOperands other_move = moves_[i];
120 if (other_move.Blocks(destination) && !other_move.IsPending()) {
121 PerformMove(i);
122 // If there is a blocking, pending move it must be moves_[root_index_]
123 // and all other moves with the same source as moves_[root_index_] are
124 // sucessfully executed (because they are cycle-free) by this loop.
125 }
126 }
127
128 // We are about to resolve this move and don't need it marked as
129 // pending, so restore its destination.
130 moves_[index].set_destination(destination);
131
132 // The move may be blocked on a pending move, which must be the starting move.
133 // In this case, we have a cycle, and we save the source of this move to
134 // a scratch register to break it.
135 LMoveOperands other_move = moves_[root_index_];
136 if (other_move.Blocks(destination)) {
137 ASSERT(other_move.IsPending());
138 BreakCycle(index);
139 return;
140 }
141
142 // This move is no longer blocked.
143 EmitMove(index);
144}
145
146
147void LGapResolver::Verify() {
148#ifdef ENABLE_SLOW_ASSERTS
149 // No operand should be the destination for more than one move.
150 for (int i = 0; i < moves_.length(); ++i) {
151 LOperand* destination = moves_[i].destination();
152 for (int j = i + 1; j < moves_.length(); ++j) {
153 SLOW_ASSERT(!destination->Equals(moves_[j].destination()));
154 }
155 }
156#endif
157}
158
159#define __ ACCESS_MASM(cgen_->masm())
160
161void LGapResolver::BreakCycle(int index) {
162 // We save in a register the value that should end up in the source of
163 // moves_[root_index]. After performing all moves in the tree rooted
164 // in that move, we save the value to that source.
165 ASSERT(moves_[index].destination()->Equals(moves_[root_index_].source()));
166 ASSERT(!in_cycle_);
167 in_cycle_ = true;
168 LOperand* source = moves_[index].source();
169 saved_destination_ = moves_[index].destination();
170 if (source->IsRegister()) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000171 __ mov(kLithiumScratchReg, cgen_->ToRegister(source));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000172 } else if (source->IsStackSlot()) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000173 __ lw(kLithiumScratchReg, cgen_->ToMemOperand(source));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000174 } else if (source->IsDoubleRegister()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000175 CpuFeatureScope scope(cgen_->masm(), FPU);
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000176 __ mov_d(kLithiumScratchDouble, cgen_->ToDoubleRegister(source));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000177 } else if (source->IsDoubleStackSlot()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000178 CpuFeatureScope scope(cgen_->masm(), FPU);
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000179 __ ldc1(kLithiumScratchDouble, cgen_->ToMemOperand(source));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000180 } else {
181 UNREACHABLE();
182 }
183 // This move will be done by restoring the saved value to the destination.
184 moves_[index].Eliminate();
185}
186
187
188void LGapResolver::RestoreValue() {
189 ASSERT(in_cycle_);
190 ASSERT(saved_destination_ != NULL);
191
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000192 // Spilled value is in kLithiumScratchReg or kLithiumScratchDouble.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000193 if (saved_destination_->IsRegister()) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000194 __ mov(cgen_->ToRegister(saved_destination_), kLithiumScratchReg);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000195 } else if (saved_destination_->IsStackSlot()) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000196 __ sw(kLithiumScratchReg, cgen_->ToMemOperand(saved_destination_));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000197 } else if (saved_destination_->IsDoubleRegister()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000198 CpuFeatureScope scope(cgen_->masm(), FPU);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000199 __ mov_d(cgen_->ToDoubleRegister(saved_destination_),
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000200 kLithiumScratchDouble);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000201 } else if (saved_destination_->IsDoubleStackSlot()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000202 CpuFeatureScope scope(cgen_->masm(), FPU);
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000203 __ sdc1(kLithiumScratchDouble,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000204 cgen_->ToMemOperand(saved_destination_));
205 } else {
206 UNREACHABLE();
207 }
208
209 in_cycle_ = false;
210 saved_destination_ = NULL;
211}
212
213
214void LGapResolver::EmitMove(int index) {
215 LOperand* source = moves_[index].source();
216 LOperand* destination = moves_[index].destination();
217
218 // Dispatch on the source and destination operand kinds. Not all
219 // combinations are possible.
220
221 if (source->IsRegister()) {
222 Register source_register = cgen_->ToRegister(source);
223 if (destination->IsRegister()) {
224 __ mov(cgen_->ToRegister(destination), source_register);
225 } else {
226 ASSERT(destination->IsStackSlot());
227 __ sw(source_register, cgen_->ToMemOperand(destination));
228 }
229
230 } else if (source->IsStackSlot()) {
231 MemOperand source_operand = cgen_->ToMemOperand(source);
232 if (destination->IsRegister()) {
233 __ lw(cgen_->ToRegister(destination), source_operand);
234 } else {
235 ASSERT(destination->IsStackSlot());
236 MemOperand destination_operand = cgen_->ToMemOperand(destination);
237 if (in_cycle_) {
238 if (!destination_operand.OffsetIsInt16Encodable()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000239 CpuFeatureScope scope(cgen_->masm(), FPU);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000240 // 'at' is overwritten while saving the value to the destination.
241 // Therefore we can't use 'at'. It is OK if the read from the source
242 // destroys 'at', since that happens before the value is read.
243 // This uses only a single reg of the double reg-pair.
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000244 __ lwc1(kLithiumScratchDouble, source_operand);
245 __ swc1(kLithiumScratchDouble, destination_operand);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000246 } else {
247 __ lw(at, source_operand);
248 __ sw(at, destination_operand);
249 }
250 } else {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000251 __ lw(kLithiumScratchReg, source_operand);
252 __ sw(kLithiumScratchReg, destination_operand);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000253 }
254 }
255
256 } else if (source->IsConstantOperand()) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000257 LConstantOperand* constant_source = LConstantOperand::cast(source);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000258 if (destination->IsRegister()) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000259 Register dst = cgen_->ToRegister(destination);
260 if (cgen_->IsInteger32(constant_source)) {
261 __ li(dst, Operand(cgen_->ToInteger32(constant_source)));
262 } else {
263 __ LoadObject(dst, cgen_->ToHandle(constant_source));
264 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000265 } else {
266 ASSERT(destination->IsStackSlot());
267 ASSERT(!in_cycle_); // Constant moves happen after all cycles are gone.
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000268 if (cgen_->IsInteger32(constant_source)) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000269 __ li(kLithiumScratchReg,
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000270 Operand(cgen_->ToInteger32(constant_source)));
271 } else {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000272 __ LoadObject(kLithiumScratchReg,
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000273 cgen_->ToHandle(constant_source));
274 }
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000275 __ sw(kLithiumScratchReg, cgen_->ToMemOperand(destination));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000276 }
277
278 } else if (source->IsDoubleRegister()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000279 CpuFeatureScope scope(cgen_->masm(), FPU);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000280 DoubleRegister source_register = cgen_->ToDoubleRegister(source);
281 if (destination->IsDoubleRegister()) {
282 __ mov_d(cgen_->ToDoubleRegister(destination), source_register);
283 } else {
284 ASSERT(destination->IsDoubleStackSlot());
285 MemOperand destination_operand = cgen_->ToMemOperand(destination);
286 __ sdc1(source_register, destination_operand);
287 }
288
289 } else if (source->IsDoubleStackSlot()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000290 CpuFeatureScope scope(cgen_->masm(), FPU);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000291 MemOperand source_operand = cgen_->ToMemOperand(source);
292 if (destination->IsDoubleRegister()) {
293 __ ldc1(cgen_->ToDoubleRegister(destination), source_operand);
294 } else {
295 ASSERT(destination->IsDoubleStackSlot());
296 MemOperand destination_operand = cgen_->ToMemOperand(destination);
297 if (in_cycle_) {
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000298 // kLithiumScratchDouble was used to break the cycle,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000299 // but kLithiumScratchReg is free.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000300 MemOperand source_high_operand =
301 cgen_->ToHighMemOperand(source);
302 MemOperand destination_high_operand =
303 cgen_->ToHighMemOperand(destination);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000304 __ lw(kLithiumScratchReg, source_operand);
305 __ sw(kLithiumScratchReg, destination_operand);
306 __ lw(kLithiumScratchReg, source_high_operand);
307 __ sw(kLithiumScratchReg, destination_high_operand);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000308 } else {
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000309 __ ldc1(kLithiumScratchDouble, source_operand);
310 __ sdc1(kLithiumScratchDouble, destination_operand);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000311 }
312 }
313 } else {
314 UNREACHABLE();
315 }
316
317 moves_[index].Eliminate();
318}
319
320
321#undef __
322
323} } // namespace v8::internal