OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/ppc/lithium-codegen-ppc.h" | |
6 #include "src/ppc/lithium-gap-resolver-ppc.h" | |
7 | |
8 namespace v8 { | |
9 namespace internal { | |
10 | |
11 static const Register kSavedValueRegister = {11}; | |
12 | |
13 LGapResolver::LGapResolver(LCodeGen* owner) | |
14 : cgen_(owner), | |
15 moves_(32, owner->zone()), | |
16 root_index_(0), | |
17 in_cycle_(false), | |
18 saved_destination_(NULL) {} | |
19 | |
20 | |
21 void LGapResolver::Resolve(LParallelMove* parallel_move) { | |
22 DCHECK(moves_.is_empty()); | |
23 // Build up a worklist of moves. | |
24 BuildInitialMoveList(parallel_move); | |
25 | |
26 for (int i = 0; i < moves_.length(); ++i) { | |
27 LMoveOperands move = moves_[i]; | |
28 // Skip constants to perform them last. They don't block other moves | |
29 // and skipping such moves with register destinations keeps those | |
30 // registers free for the whole algorithm. | |
31 if (!move.IsEliminated() && !move.source()->IsConstantOperand()) { | |
32 root_index_ = i; // Any cycle is found when by reaching this move again. | |
33 PerformMove(i); | |
34 if (in_cycle_) { | |
35 RestoreValue(); | |
36 } | |
37 } | |
38 } | |
39 | |
40 // Perform the moves with constant sources. | |
41 for (int i = 0; i < moves_.length(); ++i) { | |
42 if (!moves_[i].IsEliminated()) { | |
43 DCHECK(moves_[i].source()->IsConstantOperand()); | |
44 EmitMove(i); | |
45 } | |
46 } | |
47 | |
48 moves_.Rewind(0); | |
49 } | |
50 | |
51 | |
52 void LGapResolver::BuildInitialMoveList(LParallelMove* parallel_move) { | |
53 // Perform a linear sweep of the moves to add them to the initial list of | |
54 // moves to perform, ignoring any move that is redundant (the source is | |
55 // the same as the destination, the destination is ignored and | |
56 // unallocated, or the move was already eliminated). | |
57 const ZoneList<LMoveOperands>* moves = parallel_move->move_operands(); | |
58 for (int i = 0; i < moves->length(); ++i) { | |
59 LMoveOperands move = moves->at(i); | |
60 if (!move.IsRedundant()) moves_.Add(move, cgen_->zone()); | |
61 } | |
62 Verify(); | |
63 } | |
64 | |
65 | |
66 void LGapResolver::PerformMove(int index) { | |
67 // Each call to this function performs a move and deletes it from the move | |
68 // graph. We first recursively perform any move blocking this one. We | |
69 // mark a move as "pending" on entry to PerformMove in order to detect | |
70 // cycles in the move graph. | |
71 | |
72 // We can only find a cycle, when doing a depth-first traversal of moves, | |
73 // be encountering the starting move again. So by spilling the source of | |
74 // the starting move, we break the cycle. All moves are then unblocked, | |
75 // and the starting move is completed by writing the spilled value to | |
76 // its destination. All other moves from the spilled source have been | |
77 // completed prior to breaking the cycle. | |
78 // An additional complication is that moves to MemOperands with large | |
79 // offsets (more than 1K or 4K) require us to spill this spilled value to | |
80 // the stack, to free up the register. | |
81 DCHECK(!moves_[index].IsPending()); | |
82 DCHECK(!moves_[index].IsRedundant()); | |
83 | |
84 // Clear this move's destination to indicate a pending move. The actual | |
85 // destination is saved in a stack allocated local. Multiple moves can | |
86 // be pending because this function is recursive. | |
87 DCHECK(moves_[index].source() != NULL); // Or else it will look eliminated. | |
88 LOperand* destination = moves_[index].destination(); | |
89 moves_[index].set_destination(NULL); | |
90 | |
91 // Perform a depth-first traversal of the move graph to resolve | |
92 // dependencies. Any unperformed, unpending move with a source the same | |
93 // as this one's destination blocks this one so recursively perform all | |
94 // such moves. | |
95 for (int i = 0; i < moves_.length(); ++i) { | |
96 LMoveOperands other_move = moves_[i]; | |
97 if (other_move.Blocks(destination) && !other_move.IsPending()) { | |
98 PerformMove(i); | |
99 // If there is a blocking, pending move it must be moves_[root_index_] | |
100 // and all other moves with the same source as moves_[root_index_] are | |
101 // sucessfully executed (because they are cycle-free) by this loop. | |
102 } | |
103 } | |
104 | |
105 // We are about to resolve this move and don't need it marked as | |
106 // pending, so restore its destination. | |
107 moves_[index].set_destination(destination); | |
108 | |
109 // The move may be blocked on a pending move, which must be the starting move. | |
110 // In this case, we have a cycle, and we save the source of this move to | |
111 // a scratch register to break it. | |
112 LMoveOperands other_move = moves_[root_index_]; | |
113 if (other_move.Blocks(destination)) { | |
114 DCHECK(other_move.IsPending()); | |
115 BreakCycle(index); | |
116 return; | |
117 } | |
118 | |
119 // This move is no longer blocked. | |
120 EmitMove(index); | |
121 } | |
122 | |
123 | |
124 void LGapResolver::Verify() { | |
125 #ifdef ENABLE_SLOW_DCHECKS | |
126 // No operand should be the destination for more than one move. | |
127 for (int i = 0; i < moves_.length(); ++i) { | |
128 LOperand* destination = moves_[i].destination(); | |
129 for (int j = i + 1; j < moves_.length(); ++j) { | |
130 SLOW_DCHECK(!destination->Equals(moves_[j].destination())); | |
131 } | |
132 } | |
133 #endif | |
134 } | |
135 | |
136 #define __ ACCESS_MASM(cgen_->masm()) | |
137 | |
138 void LGapResolver::BreakCycle(int index) { | |
139 // We save in a register the value that should end up in the source of | |
140 // moves_[root_index]. After performing all moves in the tree rooted | |
141 // in that move, we save the value to that source. | |
142 DCHECK(moves_[index].destination()->Equals(moves_[root_index_].source())); | |
143 DCHECK(!in_cycle_); | |
144 in_cycle_ = true; | |
145 LOperand* source = moves_[index].source(); | |
146 saved_destination_ = moves_[index].destination(); | |
147 if (source->IsRegister()) { | |
148 __ mr(kSavedValueRegister, cgen_->ToRegister(source)); | |
149 } else if (source->IsStackSlot()) { | |
150 __ LoadP(kSavedValueRegister, cgen_->ToMemOperand(source)); | |
151 } else if (source->IsDoubleRegister()) { | |
152 __ fmr(kScratchDoubleReg, cgen_->ToDoubleRegister(source)); | |
153 } else if (source->IsDoubleStackSlot()) { | |
154 __ lfd(kScratchDoubleReg, cgen_->ToMemOperand(source)); | |
155 } else { | |
156 UNREACHABLE(); | |
157 } | |
158 // This move will be done by restoring the saved value to the destination. | |
159 moves_[index].Eliminate(); | |
160 } | |
161 | |
162 | |
163 void LGapResolver::RestoreValue() { | |
164 DCHECK(in_cycle_); | |
165 DCHECK(saved_destination_ != NULL); | |
166 | |
167 // Spilled value is in kSavedValueRegister or kSavedDoubleValueRegister. | |
168 if (saved_destination_->IsRegister()) { | |
169 __ mr(cgen_->ToRegister(saved_destination_), kSavedValueRegister); | |
170 } else if (saved_destination_->IsStackSlot()) { | |
171 __ StoreP(kSavedValueRegister, cgen_->ToMemOperand(saved_destination_)); | |
172 } else if (saved_destination_->IsDoubleRegister()) { | |
173 __ fmr(cgen_->ToDoubleRegister(saved_destination_), kScratchDoubleReg); | |
174 } else if (saved_destination_->IsDoubleStackSlot()) { | |
175 __ stfd(kScratchDoubleReg, cgen_->ToMemOperand(saved_destination_)); | |
176 } else { | |
177 UNREACHABLE(); | |
178 } | |
179 | |
180 in_cycle_ = false; | |
181 saved_destination_ = NULL; | |
182 } | |
183 | |
184 | |
185 void LGapResolver::EmitMove(int index) { | |
186 LOperand* source = moves_[index].source(); | |
187 LOperand* destination = moves_[index].destination(); | |
188 | |
189 // Dispatch on the source and destination operand kinds. Not all | |
190 // combinations are possible. | |
191 | |
192 if (source->IsRegister()) { | |
193 Register source_register = cgen_->ToRegister(source); | |
194 if (destination->IsRegister()) { | |
195 __ mr(cgen_->ToRegister(destination), source_register); | |
196 } else { | |
197 DCHECK(destination->IsStackSlot()); | |
198 __ StoreP(source_register, cgen_->ToMemOperand(destination)); | |
199 } | |
200 } else if (source->IsStackSlot()) { | |
201 MemOperand source_operand = cgen_->ToMemOperand(source); | |
202 if (destination->IsRegister()) { | |
203 __ LoadP(cgen_->ToRegister(destination), source_operand); | |
204 } else { | |
205 DCHECK(destination->IsStackSlot()); | |
206 MemOperand destination_operand = cgen_->ToMemOperand(destination); | |
207 if (in_cycle_) { | |
208 __ LoadP(ip, source_operand); | |
209 __ StoreP(ip, destination_operand); | |
210 } else { | |
211 __ LoadP(kSavedValueRegister, source_operand); | |
212 __ StoreP(kSavedValueRegister, destination_operand); | |
213 } | |
214 } | |
215 | |
216 } else if (source->IsConstantOperand()) { | |
217 LConstantOperand* constant_source = LConstantOperand::cast(source); | |
218 if (destination->IsRegister()) { | |
219 Register dst = cgen_->ToRegister(destination); | |
220 if (cgen_->IsInteger32(constant_source)) { | |
221 cgen_->EmitLoadIntegerConstant(constant_source, dst); | |
222 } else { | |
223 __ Move(dst, cgen_->ToHandle(constant_source)); | |
224 } | |
225 } else if (destination->IsDoubleRegister()) { | |
226 DoubleRegister result = cgen_->ToDoubleRegister(destination); | |
227 double v = cgen_->ToDouble(constant_source); | |
228 __ LoadDoubleLiteral(result, v, ip); | |
229 } else { | |
230 DCHECK(destination->IsStackSlot()); | |
231 DCHECK(!in_cycle_); // Constant moves happen after all cycles are gone. | |
232 if (cgen_->IsInteger32(constant_source)) { | |
233 cgen_->EmitLoadIntegerConstant(constant_source, kSavedValueRegister); | |
234 } else { | |
235 __ Move(kSavedValueRegister, cgen_->ToHandle(constant_source)); | |
236 } | |
237 __ StoreP(kSavedValueRegister, cgen_->ToMemOperand(destination)); | |
238 } | |
239 | |
240 } else if (source->IsDoubleRegister()) { | |
241 DoubleRegister source_register = cgen_->ToDoubleRegister(source); | |
242 if (destination->IsDoubleRegister()) { | |
243 __ fmr(cgen_->ToDoubleRegister(destination), source_register); | |
244 } else { | |
245 DCHECK(destination->IsDoubleStackSlot()); | |
246 __ stfd(source_register, cgen_->ToMemOperand(destination)); | |
247 } | |
248 | |
249 } else if (source->IsDoubleStackSlot()) { | |
250 MemOperand source_operand = cgen_->ToMemOperand(source); | |
251 if (destination->IsDoubleRegister()) { | |
252 __ lfd(cgen_->ToDoubleRegister(destination), source_operand); | |
253 } else { | |
254 DCHECK(destination->IsDoubleStackSlot()); | |
255 MemOperand destination_operand = cgen_->ToMemOperand(destination); | |
256 if (in_cycle_) { | |
257 // kSavedDoubleValueRegister was used to break the cycle, | |
258 // but kSavedValueRegister is free. | |
259 #if V8_TARGET_ARCH_PPC64 | |
260 __ ld(kSavedValueRegister, source_operand); | |
261 __ std(kSavedValueRegister, destination_operand); | |
262 #else | |
263 MemOperand source_high_operand = cgen_->ToHighMemOperand(source); | |
264 MemOperand destination_high_operand = | |
265 cgen_->ToHighMemOperand(destination); | |
266 __ lwz(kSavedValueRegister, source_operand); | |
267 __ stw(kSavedValueRegister, destination_operand); | |
268 __ lwz(kSavedValueRegister, source_high_operand); | |
269 __ stw(kSavedValueRegister, destination_high_operand); | |
270 #endif | |
271 } else { | |
272 __ lfd(kScratchDoubleReg, source_operand); | |
273 __ stfd(kScratchDoubleReg, destination_operand); | |
274 } | |
275 } | |
276 } else { | |
277 UNREACHABLE(); | |
278 } | |
279 | |
280 moves_[index].Eliminate(); | |
281 } | |
282 | |
283 | |
284 #undef __ | |
285 } // namespace internal | |
286 } // namespace v8 | |
OLD | NEW |