|
9 | 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 |
| -// |
13 |
| -// Cleanup SIL to make it suitable for IRGen. Specifically, removes the calls to |
14 |
| -// Builtin.staticReport(), which are not needed post SIL. |
15 |
| -// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// |
| 15 | +/// Cleanup SIL to make it suitable for IRGen. |
| 16 | +/// |
| 17 | +/// We perform the following canonicalizations: |
| 18 | +/// |
| 19 | +/// 1. We remove calls to Builtin.staticReport(), which are not needed post SIL. |
| 20 | +/// |
16 | 21 | //===----------------------------------------------------------------------===//
|
17 | 22 |
|
18 | 23 | #include "swift/SILOptimizer/PassManager/Passes.h"
|
|
24 | 29 |
|
25 | 30 | using namespace swift;
|
26 | 31 |
|
27 |
| -static void cleanFunction(SILFunction &Fn) { |
28 |
| - for (auto &BB : Fn) { |
29 |
| - auto I = BB.begin(), E = BB.end(); |
30 |
| - while (I != E) { |
| 32 | +static bool cleanFunction(SILFunction &fn) { |
| 33 | + bool madeChange = false; |
| 34 | + |
| 35 | + for (auto &bb : fn) { |
| 36 | + for (auto i = bb.begin(), e = bb.end(); i != e;) { |
31 | 37 | // Make sure there is no iterator invalidation if the inspected
|
32 | 38 | // instruction gets removed from the block.
|
33 |
| - SILInstruction *Inst = &*I; |
34 |
| - ++I; |
| 39 | + SILInstruction *inst = &*i; |
| 40 | + ++i; |
35 | 41 |
|
36 | 42 | // Remove calls to Builtin.staticReport().
|
37 |
| - if (auto *BI = dyn_cast<BuiltinInst>(Inst)) { |
38 |
| - const BuiltinInfo &B = BI->getBuiltinInfo(); |
39 |
| - if (B.ID == BuiltinValueKind::StaticReport) { |
40 |
| - // The call to the builtin should get removed before we reach |
41 |
| - // IRGen. |
42 |
| - recursivelyDeleteTriviallyDeadInstructions(BI, /* Force */true); |
43 |
| - } |
| 43 | + auto *bi = dyn_cast<BuiltinInst>(inst); |
| 44 | + if (!bi) { |
| 45 | + continue; |
44 | 46 | }
|
| 47 | + |
| 48 | + const BuiltinInfo &bInfo = bi->getBuiltinInfo(); |
| 49 | + if (bInfo.ID != BuiltinValueKind::StaticReport) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + // The call to the builtin should get removed before we reach |
| 54 | + // IRGen. |
| 55 | + recursivelyDeleteTriviallyDeadInstructions(bi, /* Force */ true); |
| 56 | + madeChange = true; |
45 | 57 | }
|
46 | 58 | }
|
| 59 | + |
| 60 | + return madeChange; |
47 | 61 | }
|
48 | 62 |
|
| 63 | +//===----------------------------------------------------------------------===// |
| 64 | +// Top Level Entrypoint |
| 65 | +//===----------------------------------------------------------------------===// |
| 66 | + |
49 | 67 | namespace {
|
50 |
| -class IRGenPrepare : public swift::SILFunctionTransform { |
51 | 68 |
|
52 |
| - /// The entry point to the transformation. |
| 69 | +class IRGenPrepare : public SILFunctionTransform { |
53 | 70 | void run() override {
|
54 |
| - cleanFunction(*getFunction()); |
55 |
| - invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody); |
| 71 | + bool shouldInvalidate = cleanFunction(*getFunction()); |
| 72 | + if (!shouldInvalidate) |
| 73 | + return; |
| 74 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
56 | 75 | }
|
57 |
| - |
58 | 76 | };
|
| 77 | + |
59 | 78 | } // end anonymous namespace
|
60 | 79 |
|
61 | 80 |
|
|
0 commit comments