|
| 1 | +//===--- RedundantMoveValueElimination.cpp - Delete spurious move_values --===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | +// A move_value ends an owned lifetime and begins an owned lifetime. |
| 13 | +// |
| 14 | +// The new lifetime may have the same characteristics as the original lifetime |
| 15 | +// with regards to |
| 16 | +// - lexicality |
| 17 | +// - escaping |
| 18 | +// |
| 19 | +// If it has the same characteristics, there is no reason to have two separate |
| 20 | +// lifetimes--they are redundant. This optimization deletes such redundant |
| 21 | +// move_values. |
| 22 | +//===----------------------------------------------------------------------===// |
| 23 | + |
| 24 | +#include "SemanticARC/SemanticARCOpts.h" |
| 25 | +#include "SemanticARCOptVisitor.h" |
| 26 | +#include "swift/SIL/LinearLifetimeChecker.h" |
| 27 | + |
| 28 | +using namespace swift; |
| 29 | +using namespace semanticarc; |
| 30 | + |
| 31 | +//===----------------------------------------------------------------------===// |
| 32 | +// Top Level Entrypoint |
| 33 | +//===----------------------------------------------------------------------===// |
| 34 | + |
| 35 | +bool SemanticARCOptVisitor::visitMoveValueInst(MoveValueInst *mvi) { |
| 36 | + if (ctx.onlyMandatoryOpts) |
| 37 | + return false; |
| 38 | + |
| 39 | + if (!ctx.shouldPerform(ARCTransformKind::RedundantMoveValueElim)) |
| 40 | + return false; |
| 41 | + |
| 42 | + auto original = mvi->getOperand(); |
| 43 | + |
| 44 | + // If the moved-from value has none ownership, hasPointerEscape can't handle |
| 45 | + // it, so it can't be used to determine whether escaping matches. |
| 46 | + if (original->getOwnershipKind() != OwnershipKind::Owned) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // First, check whether lexicality matches, the cheaper check. |
| 51 | + if (mvi->isLexical() != original->isLexical()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + // Then, check whether escaping matches, the more expensive check. |
| 56 | + if (hasPointerEscape(mvi) != hasPointerEscape(original)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // Both characteristics match. |
| 61 | + eraseAndRAUWSingleValueInstruction(mvi, original); |
| 62 | + return true; |
| 63 | +} |
0 commit comments