|
| 1 | +//===--- AccessMarkerElimination.cpp - Eliminate access markers. ----------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +/// This pass eliminates the instructions that demarcate memory access regions. |
| 14 | +/// If no memory access markers exist, then the pass does nothing. Otherwise, it |
| 15 | +/// unconditionally eliminates the markers. |
| 16 | +/// |
| 17 | +/// This is an always-on pass for temporary bootstrapping. It allows running |
| 18 | +/// test cases through the pipeline and exercising SIL verification before all |
| 19 | +/// passes support access markers. |
| 20 | +/// |
| 21 | +/// TODO: DefiniteInitialization needs to be fixed to handle begin/end_access. |
| 22 | +/// The immediate goal is to run this pass only at -O. Access markers should not |
| 23 | +/// interfere with any -Onone passes. |
| 24 | +/// |
| 25 | +//===----------------------------------------------------------------------===// |
| 26 | + |
| 27 | +#define DEBUG_TYPE "access-marker-elim" |
| 28 | +#include "swift/Basic/Range.h" |
| 29 | +#include "swift/SIL/SILFunction.h" |
| 30 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 31 | + |
| 32 | +using namespace swift; |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +struct AccessMarkerElimination : SILModuleTransform { |
| 37 | + void run() override { |
| 38 | + for (auto &F : *getModule()) { |
| 39 | + // If the function has no access markers, don't invalidate any analyses. |
| 40 | + if (!F.hasAccessMarkers()) |
| 41 | + continue; |
| 42 | + |
| 43 | + F.disableAccessMarkers(); |
| 44 | + |
| 45 | + // iterating in reverse eliminates more begin_access users before they |
| 46 | + // need to be replaced. |
| 47 | + for (auto &BB : reversed(F)) { |
| 48 | + for (auto II = BB.rbegin(), IE = BB.rend(); II != IE;) { |
| 49 | + SILInstruction *inst = &*II; |
| 50 | + ++II; |
| 51 | + |
| 52 | + if (auto beginAccess = dyn_cast<BeginAccessInst>(inst)) { |
| 53 | + beginAccess->replaceAllUsesWith(beginAccess->getSource()); |
| 54 | + beginAccess->eraseFromParent(); |
| 55 | + } |
| 56 | + if (auto endAccess = dyn_cast<EndAccessInst>(inst)) { |
| 57 | + assert(endAccess->use_empty()); |
| 58 | + endAccess->eraseFromParent(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + auto InvalidKind = SILAnalysis::InvalidationKind::Instructions; |
| 64 | + invalidateAnalysis(&F, InvalidKind); |
| 65 | + } |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +} // end anonymous namespace |
| 70 | + |
| 71 | +SILTransform *swift::createAccessMarkerElimination() { |
| 72 | + return new AccessMarkerElimination(); |
| 73 | +} |
0 commit comments