Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "jit/MoveResolver.h" |
michael@0 | 8 | |
michael@0 | 9 | using namespace js; |
michael@0 | 10 | using namespace js::jit; |
michael@0 | 11 | |
michael@0 | 12 | MoveResolver::MoveResolver() |
michael@0 | 13 | : hasCycles_(false) |
michael@0 | 14 | { |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | void |
michael@0 | 18 | MoveResolver::resetState() |
michael@0 | 19 | { |
michael@0 | 20 | hasCycles_ = false; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | bool |
michael@0 | 24 | MoveResolver::addMove(const MoveOperand &from, const MoveOperand &to, MoveOp::Type type) |
michael@0 | 25 | { |
michael@0 | 26 | // Assert that we're not doing no-op moves. |
michael@0 | 27 | JS_ASSERT(!(from == to)); |
michael@0 | 28 | PendingMove *pm = movePool_.allocate(); |
michael@0 | 29 | if (!pm) |
michael@0 | 30 | return false; |
michael@0 | 31 | new (pm) PendingMove(from, to, type); |
michael@0 | 32 | pending_.pushBack(pm); |
michael@0 | 33 | return true; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | // Given move (A -> B), this function attempts to find any move (B -> *) in the |
michael@0 | 37 | // pending move list, and returns the first one. |
michael@0 | 38 | MoveResolver::PendingMove * |
michael@0 | 39 | MoveResolver::findBlockingMove(const PendingMove *last) |
michael@0 | 40 | { |
michael@0 | 41 | for (PendingMoveIterator iter = pending_.begin(); iter != pending_.end(); iter++) { |
michael@0 | 42 | PendingMove *other = *iter; |
michael@0 | 43 | |
michael@0 | 44 | if (other->from() == last->to()) { |
michael@0 | 45 | // We now have pairs in the form (A -> X) (X -> y). The second pair |
michael@0 | 46 | // blocks the move in the first pair, so return it. |
michael@0 | 47 | return other; |
michael@0 | 48 | } |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | // No blocking moves found. |
michael@0 | 52 | return nullptr; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool |
michael@0 | 56 | MoveResolver::resolve() |
michael@0 | 57 | { |
michael@0 | 58 | resetState(); |
michael@0 | 59 | orderedMoves_.clear(); |
michael@0 | 60 | |
michael@0 | 61 | InlineList<PendingMove> stack; |
michael@0 | 62 | |
michael@0 | 63 | // This is a depth-first-search without recursion, which tries to find |
michael@0 | 64 | // cycles in a list of moves. The output is not entirely optimal for cases |
michael@0 | 65 | // where a source has multiple destinations, i.e.: |
michael@0 | 66 | // [stack0] -> A |
michael@0 | 67 | // [stack0] -> B |
michael@0 | 68 | // |
michael@0 | 69 | // These moves may not occur next to each other in the list, making it |
michael@0 | 70 | // harder for the emitter to optimize memory to memory traffic. However, we |
michael@0 | 71 | // expect duplicate sources to be rare in greedy allocation, and indicative |
michael@0 | 72 | // of an error in LSRA. |
michael@0 | 73 | // |
michael@0 | 74 | // Algorithm. |
michael@0 | 75 | // |
michael@0 | 76 | // S = Traversal stack. |
michael@0 | 77 | // P = Pending move list. |
michael@0 | 78 | // O = Ordered list of moves. |
michael@0 | 79 | // |
michael@0 | 80 | // As long as there are pending moves in P: |
michael@0 | 81 | // Let |root| be any pending move removed from P |
michael@0 | 82 | // Add |root| to the traversal stack. |
michael@0 | 83 | // As long as S is not empty: |
michael@0 | 84 | // Let |L| be the most recent move added to S. |
michael@0 | 85 | // |
michael@0 | 86 | // Find any pending move M whose source is L's destination, thus |
michael@0 | 87 | // preventing L's move until M has completed. |
michael@0 | 88 | // |
michael@0 | 89 | // If a move M was found, |
michael@0 | 90 | // Remove M from the pending list. |
michael@0 | 91 | // If M's destination is |root|, |
michael@0 | 92 | // Annotate M and |root| as cycles. |
michael@0 | 93 | // Add M to S. |
michael@0 | 94 | // do not Add M to O, since M may have other conflictors in P that have not yet been processed. |
michael@0 | 95 | // Otherwise, |
michael@0 | 96 | // Add M to S. |
michael@0 | 97 | // Otherwise, |
michael@0 | 98 | // Remove L from S. |
michael@0 | 99 | // Add L to O. |
michael@0 | 100 | // |
michael@0 | 101 | while (!pending_.empty()) { |
michael@0 | 102 | PendingMove *pm = pending_.popBack(); |
michael@0 | 103 | |
michael@0 | 104 | // Add this pending move to the cycle detection stack. |
michael@0 | 105 | stack.pushBack(pm); |
michael@0 | 106 | |
michael@0 | 107 | while (!stack.empty()) { |
michael@0 | 108 | PendingMove *blocking = findBlockingMove(stack.peekBack()); |
michael@0 | 109 | |
michael@0 | 110 | if (blocking) { |
michael@0 | 111 | if (blocking->to() == pm->from()) { |
michael@0 | 112 | // We annotate cycles at each move in the cycle, and |
michael@0 | 113 | // assert that we do not find two cycles in one move chain |
michael@0 | 114 | // traversal (which would indicate two moves to the same |
michael@0 | 115 | // destination). |
michael@0 | 116 | pm->setCycleEnd(); |
michael@0 | 117 | blocking->setCycleBegin(pm->type()); |
michael@0 | 118 | hasCycles_ = true; |
michael@0 | 119 | pending_.remove(blocking); |
michael@0 | 120 | stack.pushBack(blocking); |
michael@0 | 121 | } else { |
michael@0 | 122 | // This is a new link in the move chain, so keep |
michael@0 | 123 | // searching for a cycle. |
michael@0 | 124 | pending_.remove(blocking); |
michael@0 | 125 | stack.pushBack(blocking); |
michael@0 | 126 | } |
michael@0 | 127 | } else { |
michael@0 | 128 | // Otherwise, pop the last move on the search stack because it's |
michael@0 | 129 | // complete and not participating in a cycle. The resulting |
michael@0 | 130 | // move can safely be added to the ordered move list. |
michael@0 | 131 | PendingMove *done = stack.popBack(); |
michael@0 | 132 | if (!orderedMoves_.append(*done)) |
michael@0 | 133 | return false; |
michael@0 | 134 | movePool_.free(done); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | return true; |
michael@0 | 140 | } |