17
17
18
18
#include " swift/SILOptimizer/PassManager/Transforms.h"
19
19
20
+ #include " llvm/Support/CommandLine.h"
21
+
20
22
using namespace swift ;
21
23
using namespace swift ::semanticarc;
22
24
25
+ namespace {
26
+
27
+ // / An enum used so that at the command line, we can override
28
+ enum class TransformToPerformKind {
29
+ Peepholes,
30
+ OwnedToGuaranteedPhi,
31
+ };
32
+
33
+ } // anonymous namespace
34
+
35
+ static llvm::cl::list<TransformToPerformKind> TransformsToPerform (
36
+ llvm::cl::values (
37
+ clEnumValN (TransformToPerformKind::Peepholes,
38
+ " sil-semantic-arc-peepholes" ,
39
+ " Perform ARC canonicalizations and peepholes" ),
40
+ clEnumValN(TransformToPerformKind::OwnedToGuaranteedPhi,
41
+ " sil-semantic-arc-owned-to-guaranteed-phi" ,
42
+ " Perform Owned To Guaranteed Phi. NOTE: Seeded by peephole "
43
+ " optimizer for compile time saving purposes, so run this "
44
+ " after running peepholes)" )),
45
+ llvm::cl::desc(
46
+ " For testing purposes only run the specified list of semantic arc "
47
+ " optimization. If the list is empty, we run all transforms" ));
48
+
23
49
// ===----------------------------------------------------------------------===//
24
50
// Top Level Entrypoint
25
51
// ===----------------------------------------------------------------------===//
@@ -35,6 +61,42 @@ struct SemanticARCOpts : SILFunctionTransform {
35
61
SemanticARCOpts (bool guaranteedOptsOnly)
36
62
: guaranteedOptsOnly(guaranteedOptsOnly) {}
37
63
64
+ #ifndef NDEBUG
65
+ void performCommandlineSpecifiedTransforms (SemanticARCOptVisitor &visitor) {
66
+ for (auto transform : TransformsToPerform) {
67
+ switch (transform) {
68
+ case TransformToPerformKind::Peepholes:
69
+ if (performPeepholes (visitor)) {
70
+ invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
71
+ }
72
+ continue ;
73
+ case TransformToPerformKind::OwnedToGuaranteedPhi:
74
+ if (tryConvertOwnedPhisToGuaranteedPhis (visitor.ctx )) {
75
+ invalidateAnalysis (
76
+ SILAnalysis::InvalidationKind::BranchesAndInstructions);
77
+ }
78
+ continue ;
79
+ }
80
+ }
81
+ }
82
+ #endif
83
+
84
+ bool performPeepholes (SemanticARCOptVisitor &visitor) {
85
+ // Add all the results of all instructions that we want to visit to the
86
+ // worklist.
87
+ for (auto &block : *getFunction ()) {
88
+ for (auto &inst : block) {
89
+ if (SemanticARCOptVisitor::shouldVisitInst (&inst)) {
90
+ for (SILValue v : inst.getResults ()) {
91
+ visitor.worklist .insert (v);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ // Then process the worklist, performing peepholes.
97
+ return visitor.optimize ();
98
+ }
99
+
38
100
void run () override {
39
101
SILFunction &f = *getFunction ();
40
102
@@ -49,32 +111,29 @@ struct SemanticARCOpts : SILFunctionTransform {
49
111
50
112
SemanticARCOptVisitor visitor (f, guaranteedOptsOnly);
51
113
52
- // Add all the results of all instructions that we want to visit to the
53
- // worklist.
54
- for (auto &block : f) {
55
- for (auto &inst : block) {
56
- if (SemanticARCOptVisitor::shouldVisitInst (&inst)) {
57
- for (SILValue v : inst.getResults ()) {
58
- visitor.worklist .insert (v);
59
- }
60
- }
61
- }
114
+ #ifndef NDEBUG
115
+ // If we are being asked for testing purposes to run a series of transforms
116
+ // expressed on the command line, run that and return.
117
+ if (!TransformsToPerform.empty ()) {
118
+ return performCommandlineSpecifiedTransforms (visitor);
62
119
}
120
+ #endif
63
121
64
- // Then process the worklist, performing peepholes .
65
- bool eliminatedARCInst = visitor. optimize ( );
122
+ // Otherwise, perform our standard optimizations .
123
+ bool didEliminateARCInsts = performPeepholes (visitor );
66
124
67
125
// Now that we have seeded the map of phis to incoming values that could be
68
126
// converted to guaranteed, ignoring the phi, try convert those phis to be
69
127
// guaranteed.
70
128
if (tryConvertOwnedPhisToGuaranteedPhis (visitor.ctx )) {
71
- invalidateAnalysis (
129
+ // We return here early to save a little compile time so we do not
130
+ // invalidate analyses redundantly.
131
+ return invalidateAnalysis (
72
132
SILAnalysis::InvalidationKind::BranchesAndInstructions);
73
- return ;
74
133
}
75
134
76
135
// Otherwise, we only deleted instructions and did not touch phis.
77
- if (eliminatedARCInst )
136
+ if (didEliminateARCInsts )
78
137
invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
79
138
}
80
139
};
0 commit comments