|
| 1 | +//===--- LexicalLifetimeEliminator.cpp ------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 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 | +#define DEBUG_TYPE "sil-lexical-lifetime-eliminator" |
| 14 | + |
| 15 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +class LexicalLifetimeEliminatorPass : public SILFunctionTransform { |
| 22 | + void run() override { |
| 23 | + auto *fn = getFunction(); |
| 24 | + |
| 25 | + // If we are already canonical, we do not have any diagnostics to emit. |
| 26 | + if (fn->wasDeserializedCanonical()) |
| 27 | + return; |
| 28 | + |
| 29 | + // If we have experimental lexical lifetimes enabled, we do not want to run |
| 30 | + // this pass since we want lexical lifetimes to exist later in the pipeline. |
| 31 | + if (fn->getModule().getOptions().EnableExperimentalLexicalLifetimes) |
| 32 | + return; |
| 33 | + |
| 34 | + bool madeChange = false; |
| 35 | + for (auto &block : *fn) { |
| 36 | + for (auto &inst : block) { |
| 37 | + if (auto *bbi = dyn_cast<BeginBorrowInst>(&inst)) { |
| 38 | + if (bbi->isLexical()) { |
| 39 | + bbi->removeIsLexical(); |
| 40 | + madeChange = true; |
| 41 | + } |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (auto *asi = dyn_cast<AllocStackInst>(&inst)) { |
| 46 | + if (asi->isLexical()) { |
| 47 | + asi->removeIsLexical(); |
| 48 | + madeChange = true; |
| 49 | + } |
| 50 | + continue; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (madeChange) { |
| 56 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 57 | + } |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +} // anonymous namespace |
| 62 | + |
| 63 | +SILTransform *swift::createLexicalLifetimeEliminator() { |
| 64 | + return new LexicalLifetimeEliminatorPass(); |
| 65 | +} |
0 commit comments