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