43
43
#include " llvm/Support/Debug.h"
44
44
using namespace swift ;
45
45
46
+ // This is temporarily used for testing until Swift 5.5 branches to reduce risk.
47
+ llvm::cl::opt<bool > EnableOSSASimplifyCFG (
48
+ " enable-ossa-simplify-cfg" ,
49
+ llvm::cl::desc (
50
+ " Enable non-trivial OSSA simplify-cfg and simple jump threading "
51
+ " (staging)." ));
52
+
53
+ // This requires new OwnershipOptUtilities which aren't well tested yet.
54
+ llvm::cl::opt<bool > EnableOSSARewriteTerminator (
55
+ " enable-ossa-rewriteterminator" ,
56
+ llvm::cl::desc (
57
+ " Enable OSSA simplify-cfg with non-trivial terminator rewriting "
58
+ " (staging)." ));
59
+
46
60
STATISTIC (NumBlocksDeleted, " Number of unreachable blocks removed" );
47
61
STATISTIC (NumBlocksMerged, " Number of blocks merged together" );
48
62
STATISTIC (NumJumpThreads, " Number of jumps threaded" );
@@ -661,23 +675,16 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
661
675
// Get the dominator tree.
662
676
DT = DA->get (&Fn);
663
677
678
+ if (!EnableOSSASimplifyCFG && Fn.hasOwnership ())
679
+ return false ;
680
+
664
681
// Split all critical edges such that we can move code onto edges. This is
665
682
// also required for SSA construction in dominatorBasedSimplifications' jump
666
683
// threading. It only splits new critical edges it creates by jump threading.
667
684
bool Changed = false ;
668
685
if (!Fn.hasOwnership () && EnableJumpThread) {
669
686
Changed = splitAllCriticalEdges (Fn, DT, nullptr );
670
687
}
671
-
672
- // TODO: OSSA phi support. Even if all block arguments are trivial,
673
- // jump-threading may require creation of guaranteed phis, which may require
674
- // creation of nested borrow scopes.
675
- if (Fn.hasOwnership ()) {
676
- for (auto &BB : Fn)
677
- Changed |= simplifyArgs (&BB);
678
- return Changed;
679
- }
680
-
681
688
unsigned MaxIter = MaxIterationsOfDominatorBasedSimplify;
682
689
SmallVector<SILBasicBlock *, 16 > BlocksForWorklist;
683
690
@@ -688,7 +695,8 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
688
695
// Do dominator based simplification of terminator condition. This does not
689
696
// and MUST NOT change the CFG without updating the dominator tree to
690
697
// reflect such change.
691
- if (tryCheckedCastBrJumpThreading (&Fn, DT, BlocksForWorklist)) {
698
+ if (tryCheckedCastBrJumpThreading (&Fn, DT, BlocksForWorklist,
699
+ EnableOSSARewriteTerminator)) {
692
700
for (auto BB: BlocksForWorklist)
693
701
addToWorklist (BB);
694
702
@@ -1029,10 +1037,13 @@ static bool hasInjectedEnumAtEndOfBlock(SILBasicBlock *block, SILValue enumAddr)
1029
1037
// / tryJumpThreading - Check to see if it looks profitable to duplicate the
1030
1038
// / destination of an unconditional jump into the bottom of this block.
1031
1039
bool SimplifyCFG::tryJumpThreading (BranchInst *BI) {
1040
+ if (!EnableOSSASimplifyCFG && Fn.hasOwnership ())
1041
+ return false ;
1042
+
1032
1043
auto *DestBB = BI->getDestBB ();
1033
1044
auto *SrcBB = BI->getParent ();
1034
1045
TermInst *destTerminator = DestBB->getTerminator ();
1035
- if (Fn.hasOwnership ()) {
1046
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
1036
1047
if (llvm::any_of (DestBB->getArguments (), [this ](SILValue op) {
1037
1048
return !op->getType ().isTrivial (Fn);
1038
1049
})) {
@@ -1821,8 +1832,7 @@ static bool isOnlyUnreachable(SILBasicBlock *BB) {
1821
1832
// / switch_enum where all but one block consists of just an
1822
1833
// / "unreachable" with an unchecked_enum_data and branch.
1823
1834
bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks (SwitchEnumInst *SEI) {
1824
- if (Fn.hasOwnership ()) {
1825
- // TODO: OSSA; cleanup terminator results.
1835
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
1826
1836
if (!SEI->getOperand ()->getType ().isTrivial (Fn))
1827
1837
return false ;
1828
1838
}
@@ -2119,7 +2129,7 @@ static bool hasSameUltimateSuccessor(SILBasicBlock *noneBB, SILBasicBlock *someB
2119
2129
bool SimplifyCFG::simplifySwitchEnumOnObjcClassOptional (SwitchEnumInst *SEI) {
2120
2130
// TODO: OSSA; handle non-trivial enum case cleanup
2121
2131
// (simplify_switch_enum_objc.sil).
2122
- if (Fn.hasOwnership ()) {
2132
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
2123
2133
return false ;
2124
2134
}
2125
2135
@@ -2183,7 +2193,7 @@ bool SimplifyCFG::simplifySwitchEnumBlock(SwitchEnumInst *SEI) {
2183
2193
auto *LiveBlock = SEI->getCaseDestination (EnumCase.get ());
2184
2194
auto *ThisBB = SEI->getParent ();
2185
2195
2186
- if (Fn.hasOwnership ()) {
2196
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
2187
2197
// TODO: OSSA; cleanup terminator results.
2188
2198
if (!SEI->getOperand ()->getType ().isTrivial (Fn))
2189
2199
return false ;
@@ -2423,7 +2433,7 @@ bool SimplifyCFG::simplifyCheckedCastBranchBlock(CheckedCastBranchInst *CCBI) {
2423
2433
bool SimplifyCFG::simplifyCheckedCastValueBranchBlock (
2424
2434
CheckedCastValueBranchInst *CCBI) {
2425
2435
// TODO: OSSA; handle cleanups for opaque cases (simplify_cfg_opaque.sil).
2426
- if (Fn.hasOwnership ()) {
2436
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
2427
2437
return false ;
2428
2438
}
2429
2439
@@ -2701,7 +2711,7 @@ bool SimplifyCFG::simplifyTermWithIdenticalDestBlocks(SILBasicBlock *BB) {
2701
2711
TermInst *Term = BB->getTerminator ();
2702
2712
// TODO: OSSA; cleanup nontrivial terminator operands (if this ever actually
2703
2713
// happens)
2704
- if (Fn.hasOwnership ()) {
2714
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
2705
2715
if (llvm::any_of (Term->getOperandValues (), [this ](SILValue op) {
2706
2716
return !op->getType ().isTrivial (Fn);
2707
2717
})) {
@@ -2896,7 +2906,7 @@ bool SimplifyCFG::simplifyBlocks() {
2896
2906
// / Canonicalize all switch_enum and switch_enum_addr instructions.
2897
2907
// / If possible, replace the default with the corresponding unique case.
2898
2908
bool SimplifyCFG::canonicalizeSwitchEnums () {
2899
- if (Fn.hasOwnership ()) {
2909
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
2900
2910
// TODO: OSSA. In OSSA, the default switch_enum case passes the
2901
2911
// original enum as a block argument. This needs to check that the block
2902
2912
// argument is dead, then replace it with the a new argument for the default
@@ -3004,7 +3014,7 @@ bool SimplifyCFG::tailDuplicateObjCMethodCallSuccessorBlocks() {
3004
3014
// TODO: OSSA phi support. Even if all block arguments are trivial,
3005
3015
// jump-threading may require creation of guaranteed phis, which may require
3006
3016
// creation of nested borrow scopes.
3007
- if (Fn.hasOwnership ()) {
3017
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
3008
3018
return false ;
3009
3019
}
3010
3020
// Collect blocks to tail duplicate.
@@ -3213,7 +3223,7 @@ RemoveDeadArgsWhenSplitting("sroa-args-remove-dead-args-after",
3213
3223
llvm::cl::init (true ));
3214
3224
3215
3225
bool ArgumentSplitter::split () {
3216
- if (Arg->getFunction ()->hasOwnership ()) {
3226
+ if (!EnableOSSARewriteTerminator && Arg->getFunction ()->hasOwnership ()) {
3217
3227
// TODO: OSSA phi support
3218
3228
if (!Arg->getType ().isTrivial (*Arg->getFunction ()))
3219
3229
return false ;
@@ -4005,7 +4015,7 @@ bool SimplifyCFG::simplifyArgs(SILBasicBlock *BB) {
4005
4015
if (BB->pred_empty ())
4006
4016
return false ;
4007
4017
4008
- if (Fn.hasOwnership ()) {
4018
+ if (!EnableOSSARewriteTerminator && Fn.hasOwnership ()) {
4009
4019
// TODO: OSSA phi support
4010
4020
if (llvm::any_of (BB->getArguments (), [this ](SILArgument *arg) {
4011
4021
return !arg->getType ().isTrivial (Fn);
0 commit comments