| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "lithium.h" | 28 #include "lithium.h" |
| 29 | 29 |
| 30 namespace v8 { | 30 namespace v8 { |
| 31 namespace internal { | 31 namespace internal { |
| 32 | 32 |
| 33 | 33 |
| 34 class LGapNode: public ZoneObject { | 34 void LOperand::PrintTo(StringStream* stream) { |
| 35 public: | 35 LUnallocated* unalloc = NULL; |
| 36 explicit LGapNode(LOperand* operand) | 36 switch (kind()) { |
| 37 : operand_(operand), resolved_(false), visited_id_(-1) { } | 37 case INVALID: |
| 38 | 38 break; |
| 39 LOperand* operand() const { return operand_; } | 39 case UNALLOCATED: |
| 40 bool IsResolved() const { return !IsAssigned() || resolved_; } | 40 unalloc = LUnallocated::cast(this); |
| 41 void MarkResolved() { | 41 stream->Add("v%d", unalloc->virtual_register()); |
| 42 ASSERT(!IsResolved()); | 42 switch (unalloc->policy()) { |
| 43 resolved_ = true; | 43 case LUnallocated::NONE: |
| 44 } | 44 break; |
| 45 int visited_id() const { return visited_id_; } | 45 case LUnallocated::FIXED_REGISTER: { |
| 46 void set_visited_id(int id) { | 46 const char* register_name = |
| 47 ASSERT(id > visited_id_); | 47 Register::AllocationIndexToString(unalloc->fixed_index()); |
| 48 visited_id_ = id; | 48 stream->Add("(=%s)", register_name); |
| 49 } | 49 break; |
| 50 | 50 } |
| 51 bool IsAssigned() const { return assigned_from_.is_set(); } | 51 case LUnallocated::FIXED_DOUBLE_REGISTER: { |
| 52 LGapNode* assigned_from() const { return assigned_from_.get(); } | 52 const char* double_register_name = |
| 53 void set_assigned_from(LGapNode* n) { assigned_from_.set(n); } | 53 DoubleRegister::AllocationIndexToString(unalloc->fixed_index()); |
| 54 | 54 stream->Add("(=%s)", double_register_name); |
| 55 private: | 55 break; |
| 56 LOperand* operand_; | 56 } |
| 57 SetOncePointer<LGapNode> assigned_from_; | 57 case LUnallocated::FIXED_SLOT: |
| 58 bool resolved_; | 58 stream->Add("(=%dS)", unalloc->fixed_index()); |
| 59 int visited_id_; | 59 break; |
| 60 }; | 60 case LUnallocated::MUST_HAVE_REGISTER: |
| 61 | 61 stream->Add("(R)"); |
| 62 | 62 break; |
| 63 LGapResolver::LGapResolver() | 63 case LUnallocated::WRITABLE_REGISTER: |
| 64 : nodes_(32), | 64 stream->Add("(WR)"); |
| 65 identified_cycles_(4), | 65 break; |
| 66 result_(16), | 66 case LUnallocated::SAME_AS_FIRST_INPUT: |
| 67 next_visited_id_(0) { | 67 stream->Add("(1)"); |
| 68 } | 68 break; |
| 69 | 69 case LUnallocated::ANY: |
| 70 | 70 stream->Add("(-)"); |
| 71 const ZoneList<LMoveOperands>* LGapResolver::Resolve( | 71 break; |
| 72 const ZoneList<LMoveOperands>* moves, | 72 case LUnallocated::IGNORE: |
| 73 LOperand* marker_operand) { | 73 stream->Add("(0)"); |
| 74 nodes_.Rewind(0); | 74 break; |
| 75 identified_cycles_.Rewind(0); | |
| 76 result_.Rewind(0); | |
| 77 next_visited_id_ = 0; | |
| 78 | |
| 79 for (int i = 0; i < moves->length(); ++i) { | |
| 80 LMoveOperands move = moves->at(i); | |
| 81 if (!move.IsRedundant()) RegisterMove(move); | |
| 82 } | |
| 83 | |
| 84 for (int i = 0; i < identified_cycles_.length(); ++i) { | |
| 85 ResolveCycle(identified_cycles_[i], marker_operand); | |
| 86 } | |
| 87 | |
| 88 int unresolved_nodes; | |
| 89 do { | |
| 90 unresolved_nodes = 0; | |
| 91 for (int j = 0; j < nodes_.length(); j++) { | |
| 92 LGapNode* node = nodes_[j]; | |
| 93 if (!node->IsResolved() && node->assigned_from()->IsResolved()) { | |
| 94 AddResultMove(node->assigned_from(), node); | |
| 95 node->MarkResolved(); | |
| 96 } | 75 } |
| 97 if (!node->IsResolved()) ++unresolved_nodes; | 76 break; |
| 98 } | 77 case CONSTANT_OPERAND: |
| 99 } while (unresolved_nodes > 0); | 78 stream->Add("[constant:%d]", index()); |
| 100 return &result_; | 79 break; |
| 101 } | 80 case STACK_SLOT: |
| 102 | 81 stream->Add("[stack:%d]", index()); |
| 103 | 82 break; |
| 104 void LGapResolver::AddResultMove(LGapNode* from, LGapNode* to) { | 83 case DOUBLE_STACK_SLOT: |
| 105 AddResultMove(from->operand(), to->operand()); | 84 stream->Add("[double_stack:%d]", index()); |
| 106 } | 85 break; |
| 107 | 86 case REGISTER: |
| 108 | 87 stream->Add("[%s|R]", Register::AllocationIndexToString(index())); |
| 109 void LGapResolver::AddResultMove(LOperand* from, LOperand* to) { | 88 break; |
| 110 result_.Add(LMoveOperands(from, to)); | 89 case DOUBLE_REGISTER: |
| 111 } | 90 stream->Add("[%s|R]", DoubleRegister::AllocationIndexToString(index())); |
| 112 | 91 break; |
| 113 | 92 case ARGUMENT: |
| 114 void LGapResolver::ResolveCycle(LGapNode* start, LOperand* marker_operand) { | 93 stream->Add("[arg:%d]", index()); |
| 115 ZoneList<LOperand*> cycle_operands(8); | 94 break; |
| 116 cycle_operands.Add(marker_operand); | |
| 117 LGapNode* cur = start; | |
| 118 do { | |
| 119 cur->MarkResolved(); | |
| 120 cycle_operands.Add(cur->operand()); | |
| 121 cur = cur->assigned_from(); | |
| 122 } while (cur != start); | |
| 123 cycle_operands.Add(marker_operand); | |
| 124 | |
| 125 for (int i = cycle_operands.length() - 1; i > 0; --i) { | |
| 126 LOperand* from = cycle_operands[i]; | |
| 127 LOperand* to = cycle_operands[i - 1]; | |
| 128 AddResultMove(from, to); | |
| 129 } | 95 } |
| 130 } | 96 } |
| 131 | 97 |
| 132 | 98 |
| 133 bool LGapResolver::CanReach(LGapNode* a, LGapNode* b, int visited_id) { | 99 int LOperand::VirtualRegister() { |
| 134 ASSERT(a != b); | 100 LUnallocated* unalloc = LUnallocated::cast(this); |
| 135 LGapNode* cur = a; | 101 return unalloc->virtual_register(); |
| 136 while (cur != b && cur->visited_id() != visited_id && cur->IsAssigned()) { | |
| 137 cur->set_visited_id(visited_id); | |
| 138 cur = cur->assigned_from(); | |
| 139 } | |
| 140 | |
| 141 return cur == b; | |
| 142 } | |
| 143 | |
| 144 | |
| 145 bool LGapResolver::CanReach(LGapNode* a, LGapNode* b) { | |
| 146 ASSERT(a != b); | |
| 147 return CanReach(a, b, next_visited_id_++); | |
| 148 } | |
| 149 | |
| 150 | |
| 151 void LGapResolver::RegisterMove(LMoveOperands move) { | |
| 152 if (move.from()->IsConstantOperand()) { | |
| 153 // Constant moves should be last in the machine code. Therefore add them | |
| 154 // first to the result set. | |
| 155 AddResultMove(move.from(), move.to()); | |
| 156 } else { | |
| 157 LGapNode* from = LookupNode(move.from()); | |
| 158 LGapNode* to = LookupNode(move.to()); | |
| 159 if (to->IsAssigned() && to->assigned_from() == from) { | |
| 160 move.Eliminate(); | |
| 161 return; | |
| 162 } | |
| 163 ASSERT(!to->IsAssigned()); | |
| 164 if (CanReach(from, to)) { | |
| 165 // This introduces a cycle. Save. | |
| 166 identified_cycles_.Add(from); | |
| 167 } | |
| 168 to->set_assigned_from(from); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 | |
| 173 LGapNode* LGapResolver::LookupNode(LOperand* operand) { | |
| 174 for (int i = 0; i < nodes_.length(); ++i) { | |
| 175 if (nodes_[i]->operand()->Equals(operand)) return nodes_[i]; | |
| 176 } | |
| 177 | |
| 178 // No node found => create a new one. | |
| 179 LGapNode* result = new LGapNode(operand); | |
| 180 nodes_.Add(result); | |
| 181 return result; | |
| 182 } | 102 } |
| 183 | 103 |
| 184 | 104 |
| 185 bool LParallelMove::IsRedundant() const { | 105 bool LParallelMove::IsRedundant() const { |
| 186 for (int i = 0; i < move_operands_.length(); ++i) { | 106 for (int i = 0; i < move_operands_.length(); ++i) { |
| 187 if (!move_operands_[i].IsRedundant()) return false; | 107 if (!move_operands_[i].IsRedundant()) return false; |
| 188 } | 108 } |
| 189 return true; | 109 return true; |
| 190 } | 110 } |
| 191 | 111 |
| 192 | 112 |
| 193 void LParallelMove::PrintDataTo(StringStream* stream) const { | 113 void LParallelMove::PrintDataTo(StringStream* stream) const { |
| 194 for (int i = move_operands_.length() - 1; i >= 0; --i) { | 114 bool first = true; |
| 115 for (int i = 0; i < move_operands_.length(); ++i) { |
| 195 if (!move_operands_[i].IsEliminated()) { | 116 if (!move_operands_[i].IsEliminated()) { |
| 196 LOperand* from = move_operands_[i].from(); | 117 LOperand* source = move_operands_[i].source(); |
| 197 LOperand* to = move_operands_[i].to(); | 118 LOperand* destination = move_operands_[i].destination(); |
| 198 if (from->Equals(to)) { | 119 if (!first) stream->Add(" "); |
| 199 to->PrintTo(stream); | 120 first = false; |
| 121 if (source->Equals(destination)) { |
| 122 destination->PrintTo(stream); |
| 200 } else { | 123 } else { |
| 201 to->PrintTo(stream); | 124 destination->PrintTo(stream); |
| 202 stream->Add(" = "); | 125 stream->Add(" = "); |
| 203 from->PrintTo(stream); | 126 source->PrintTo(stream); |
| 204 } | 127 } |
| 205 stream->Add("; "); | 128 stream->Add(";"); |
| 206 } | 129 } |
| 207 } | 130 } |
| 208 } | 131 } |
| 209 | 132 |
| 210 | 133 |
| 211 void LEnvironment::PrintTo(StringStream* stream) { | 134 void LEnvironment::PrintTo(StringStream* stream) { |
| 212 stream->Add("[id=%d|", ast_id()); | 135 stream->Add("[id=%d|", ast_id()); |
| 213 stream->Add("[parameters=%d|", parameter_count()); | 136 stream->Add("[parameters=%d|", parameter_count()); |
| 214 stream->Add("[arguments_stack_height=%d|", arguments_stack_height()); | 137 stream->Add("[arguments_stack_height=%d|", arguments_stack_height()); |
| 215 for (int i = 0; i < values_.length(); ++i) { | 138 for (int i = 0; i < values_.length(); ++i) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 236 stream->Add("{"); | 159 stream->Add("{"); |
| 237 for (int i = 0; i < pointer_operands_.length(); ++i) { | 160 for (int i = 0; i < pointer_operands_.length(); ++i) { |
| 238 if (i != 0) stream->Add(";"); | 161 if (i != 0) stream->Add(";"); |
| 239 pointer_operands_[i]->PrintTo(stream); | 162 pointer_operands_[i]->PrintTo(stream); |
| 240 } | 163 } |
| 241 stream->Add("} @%d", position()); | 164 stream->Add("} @%d", position()); |
| 242 } | 165 } |
| 243 | 166 |
| 244 | 167 |
| 245 } } // namespace v8::internal | 168 } } // namespace v8::internal |
| OLD | NEW |