|
| 1 | +//===--- SemanticARCOptVisitor.cpp ----------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// |
| 15 | +/// Implementation of the main optimize loop of the ARC visitor peephole |
| 16 | +/// optimizer. |
| 17 | +/// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +#include "SemanticARCOptVisitor.h" |
| 21 | +#include "swift/SIL/DebugUtils.h" |
| 22 | + |
| 23 | +using namespace swift; |
| 24 | +using namespace swift::semanticarc; |
| 25 | + |
| 26 | +bool SemanticARCOptVisitor::optimize() { |
| 27 | + bool madeChange = false; |
| 28 | + |
| 29 | + // First process the worklist until we reach a fixed point. |
| 30 | + madeChange |= processWorklist(); |
| 31 | + |
| 32 | + { |
| 33 | + // If we made a change, set that we assume we are at fixed point and then |
| 34 | + // re-run the worklist so that we can |
| 35 | + // properly seeded the ARC peephole map. |
| 36 | + ctx.assumingAtFixedPoint = true; |
| 37 | + SWIFT_DEFER { ctx.assumingAtFixedPoint = false; }; |
| 38 | + |
| 39 | + // Add everything in visitedSinceLastMutation to the worklist so we |
| 40 | + // recompute our fixed point. |
| 41 | + drainVisitedSinceLastMutationIntoWorklist(); |
| 42 | + |
| 43 | + // Then re-run the worklist. We shouldn't modify anything since we are at a |
| 44 | + // fixed point and are just using this to seed the |
| 45 | + // joinedOwnedIntroducerToConsumedOperands after we have finished changing |
| 46 | + // things. If we did change something, we did something weird, so assert! |
| 47 | + bool madeAdditionalChanges = processWorklist(); |
| 48 | + (void)madeAdditionalChanges; |
| 49 | + assert(!madeAdditionalChanges && "Should be at the fixed point"); |
| 50 | + } |
| 51 | + |
| 52 | + return madeChange; |
| 53 | +} |
| 54 | + |
| 55 | +bool SemanticARCOptVisitor::processWorklist() { |
| 56 | + // NOTE: The madeChange here is not strictly necessary since we only have |
| 57 | + // items added to the worklist today if we have already made /some/ sort of |
| 58 | + // change. That being said, I think there is a low cost to including this here |
| 59 | + // and makes the algorithm more correct, visually and in the face of potential |
| 60 | + // refactoring. |
| 61 | + bool madeChange = false; |
| 62 | + |
| 63 | + while (!worklist.empty()) { |
| 64 | + // Pop the last element off the list. If we were returned None, we blotted |
| 65 | + // this element, so skip it. |
| 66 | + SILValue next = worklist.pop_back_val().getValueOr(SILValue()); |
| 67 | + if (!next) |
| 68 | + continue; |
| 69 | + |
| 70 | + // First check if this is a value that we have visited since the last time |
| 71 | + // we erased an instruction. If we have visited it, skip it. Every time we |
| 72 | + // modify something, we should be deleting an instruction, so we have not |
| 73 | + // found any further information. |
| 74 | + if (!visitedSinceLastMutation.insert(next).second) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // First check if this is an instruction that is trivially dead. This can |
| 79 | + // occur if we eliminate rr traffic resulting in dead projections and the |
| 80 | + // like. |
| 81 | + // |
| 82 | + // If we delete, we first add all of our deleted instructions operands to |
| 83 | + // the worklist and then remove all results (since we are going to delete |
| 84 | + // the instruction). |
| 85 | + if (auto *defInst = next->getDefiningInstruction()) { |
| 86 | + if (isInstructionTriviallyDead(defInst)) { |
| 87 | + assert(!ctx.assumingAtFixedPoint && |
| 88 | + "Assumed was at fixed point and recomputing state?!"); |
| 89 | + deleteAllDebugUses(defInst); |
| 90 | + eraseInstruction(defInst); |
| 91 | + madeChange = true; |
| 92 | + ctx.verify(); |
| 93 | + continue; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Otherwise, if we have a single value instruction (to be expanded later |
| 98 | + // perhaps), try to visit that value recursively. |
| 99 | + if (auto *svi = dyn_cast<SingleValueInstruction>(next)) { |
| 100 | + bool madeSingleChange = visit(svi); |
| 101 | + assert((!madeSingleChange || !ctx.assumingAtFixedPoint) && |
| 102 | + "Assumed was at fixed point and modified state?!"); |
| 103 | + madeChange |= madeSingleChange; |
| 104 | + if (madeSingleChange) { |
| 105 | + ctx.verify(); |
| 106 | + } |
| 107 | + continue; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return madeChange; |
| 112 | +} |
0 commit comments