Skip to content

Commit 245325c

Browse files
committed
[irgen-prepare] Clean up the pass and only invalidate analyzes if we actually change something.
rdar://39335800
1 parent 5baa90c commit 245325c

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

lib/SILOptimizer/Mandatory/IRGenPrepare.cpp

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
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+
///
1621
//===----------------------------------------------------------------------===//
1722

1823
#include "swift/SILOptimizer/PassManager/Passes.h"
@@ -24,38 +29,52 @@
2429

2530
using namespace swift;
2631

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;) {
3137
// Make sure there is no iterator invalidation if the inspected
3238
// instruction gets removed from the block.
33-
SILInstruction *Inst = &*I;
34-
++I;
39+
SILInstruction *inst = &*i;
40+
++i;
3541

3642
// 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;
4446
}
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;
4557
}
4658
}
59+
60+
return madeChange;
4761
}
4862

63+
//===----------------------------------------------------------------------===//
64+
// Top Level Entrypoint
65+
//===----------------------------------------------------------------------===//
66+
4967
namespace {
50-
class IRGenPrepare : public swift::SILFunctionTransform {
5168

52-
/// The entry point to the transformation.
69+
class IRGenPrepare : public SILFunctionTransform {
5370
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);
5675
}
57-
5876
};
77+
5978
} // end anonymous namespace
6079

6180

0 commit comments

Comments
 (0)