Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame^] | 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 "lithium.h" |
| 29 | |
| 30 | namespace v8 { |
| 31 | namespace internal { |
| 32 | |
| 33 | |
| 34 | class LGapNode: public ZoneObject { |
| 35 | public: |
| 36 | explicit LGapNode(LOperand* operand) |
| 37 | : operand_(operand), resolved_(false), visited_id_(-1) { } |
| 38 | |
| 39 | LOperand* operand() const { return operand_; } |
| 40 | bool IsResolved() const { return !IsAssigned() || resolved_; } |
| 41 | void MarkResolved() { |
| 42 | ASSERT(!IsResolved()); |
| 43 | resolved_ = true; |
| 44 | } |
| 45 | int visited_id() const { return visited_id_; } |
| 46 | void set_visited_id(int id) { |
| 47 | ASSERT(id > visited_id_); |
| 48 | visited_id_ = id; |
| 49 | } |
| 50 | |
| 51 | bool IsAssigned() const { return assigned_from_.is_set(); } |
| 52 | LGapNode* assigned_from() const { return assigned_from_.get(); } |
| 53 | void set_assigned_from(LGapNode* n) { assigned_from_.set(n); } |
| 54 | |
| 55 | private: |
| 56 | LOperand* operand_; |
| 57 | SetOncePointer<LGapNode> assigned_from_; |
| 58 | bool resolved_; |
| 59 | int visited_id_; |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | LGapResolver::LGapResolver(const ZoneList<LMoveOperands>* moves, |
| 64 | LOperand* marker_operand) |
| 65 | : nodes_(4), |
| 66 | identified_cycles_(4), |
| 67 | result_(4), |
| 68 | marker_operand_(marker_operand), |
| 69 | next_visited_id_(0) { |
| 70 | for (int i = 0; i < moves->length(); ++i) { |
| 71 | LMoveOperands move = moves->at(i); |
| 72 | if (!move.IsRedundant()) RegisterMove(move); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | const ZoneList<LMoveOperands>* LGapResolver::ResolveInReverseOrder() { |
| 78 | for (int i = 0; i < identified_cycles_.length(); ++i) { |
| 79 | ResolveCycle(identified_cycles_[i]); |
| 80 | } |
| 81 | |
| 82 | int unresolved_nodes; |
| 83 | do { |
| 84 | unresolved_nodes = 0; |
| 85 | for (int j = 0; j < nodes_.length(); j++) { |
| 86 | LGapNode* node = nodes_[j]; |
| 87 | if (!node->IsResolved() && node->assigned_from()->IsResolved()) { |
| 88 | AddResultMove(node->assigned_from(), node); |
| 89 | node->MarkResolved(); |
| 90 | } |
| 91 | if (!node->IsResolved()) ++unresolved_nodes; |
| 92 | } |
| 93 | } while (unresolved_nodes > 0); |
| 94 | return &result_; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void LGapResolver::AddResultMove(LGapNode* from, LGapNode* to) { |
| 99 | AddResultMove(from->operand(), to->operand()); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void LGapResolver::AddResultMove(LOperand* from, LOperand* to) { |
| 104 | result_.Add(LMoveOperands(from, to)); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void LGapResolver::ResolveCycle(LGapNode* start) { |
| 109 | ZoneList<LOperand*> circle_operands(8); |
| 110 | circle_operands.Add(marker_operand_); |
| 111 | LGapNode* cur = start; |
| 112 | do { |
| 113 | cur->MarkResolved(); |
| 114 | circle_operands.Add(cur->operand()); |
| 115 | cur = cur->assigned_from(); |
| 116 | } while (cur != start); |
| 117 | circle_operands.Add(marker_operand_); |
| 118 | |
| 119 | for (int i = circle_operands.length() - 1; i > 0; --i) { |
| 120 | LOperand* from = circle_operands[i]; |
| 121 | LOperand* to = circle_operands[i - 1]; |
| 122 | AddResultMove(from, to); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | bool LGapResolver::CanReach(LGapNode* a, LGapNode* b, int visited_id) { |
| 128 | ASSERT(a != b); |
| 129 | LGapNode* cur = a; |
| 130 | while (cur != b && cur->visited_id() != visited_id && cur->IsAssigned()) { |
| 131 | cur->set_visited_id(visited_id); |
| 132 | cur = cur->assigned_from(); |
| 133 | } |
| 134 | |
| 135 | return cur == b; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | bool LGapResolver::CanReach(LGapNode* a, LGapNode* b) { |
| 140 | ASSERT(a != b); |
| 141 | return CanReach(a, b, next_visited_id_++); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void LGapResolver::RegisterMove(LMoveOperands move) { |
| 146 | if (move.from()->IsConstantOperand()) { |
| 147 | // Constant moves should be last in the machine code. Therefore add them |
| 148 | // first to the result set. |
| 149 | AddResultMove(move.from(), move.to()); |
| 150 | } else { |
| 151 | LGapNode* from = LookupNode(move.from()); |
| 152 | LGapNode* to = LookupNode(move.to()); |
| 153 | if (to->IsAssigned() && to->assigned_from() == from) { |
| 154 | move.Eliminate(); |
| 155 | return; |
| 156 | } |
| 157 | ASSERT(!to->IsAssigned()); |
| 158 | if (CanReach(from, to)) { |
| 159 | // This introduces a circle. Save. |
| 160 | identified_cycles_.Add(from); |
| 161 | } |
| 162 | to->set_assigned_from(from); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | LGapNode* LGapResolver::LookupNode(LOperand* operand) { |
| 168 | for (int i = 0; i < nodes_.length(); ++i) { |
| 169 | if (nodes_[i]->operand()->Equals(operand)) return nodes_[i]; |
| 170 | } |
| 171 | |
| 172 | // No node found => create a new one. |
| 173 | LGapNode* result = new LGapNode(operand); |
| 174 | nodes_.Add(result); |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | } } // namespace v8::internal |