1717
1818#include " swift/SILOptimizer/PassManager/Transforms.h"
1919
20+ #include " llvm/Support/CommandLine.h"
21+
2022using namespace swift ;
2123using namespace swift ::semanticarc;
2224
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+
2349// ===----------------------------------------------------------------------===//
2450// Top Level Entrypoint
2551// ===----------------------------------------------------------------------===//
@@ -35,6 +61,42 @@ struct SemanticARCOpts : SILFunctionTransform {
3561 SemanticARCOpts (bool guaranteedOptsOnly)
3662 : guaranteedOptsOnly(guaranteedOptsOnly) {}
3763
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+
38100 void run () override {
39101 SILFunction &f = *getFunction ();
40102
@@ -49,32 +111,29 @@ struct SemanticARCOpts : SILFunctionTransform {
49111
50112 SemanticARCOptVisitor visitor (f, guaranteedOptsOnly);
51113
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);
62119 }
120+ #endif
63121
64- // Then process the worklist, performing peepholes .
65- bool eliminatedARCInst = visitor. optimize ( );
122+ // Otherwise, perform our standard optimizations .
123+ bool didEliminateARCInsts = performPeepholes (visitor );
66124
67125 // Now that we have seeded the map of phis to incoming values that could be
68126 // converted to guaranteed, ignoring the phi, try convert those phis to be
69127 // guaranteed.
70128 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 (
72132 SILAnalysis::InvalidationKind::BranchesAndInstructions);
73- return ;
74133 }
75134
76135 // Otherwise, we only deleted instructions and did not touch phis.
77- if (eliminatedARCInst )
136+ if (didEliminateARCInsts )
78137 invalidateAnalysis (SILAnalysis::InvalidationKind::Instructions);
79138 }
80139};
0 commit comments