@@ -114,6 +114,21 @@ static inline bool isPerformingDSE(DSEKind Kind) {
114
114
return Kind == DSEKind::PerformDSE;
115
115
}
116
116
117
+ // / Return true if all basic blocks have their successors processed if
118
+ // / they are iterated in post order.
119
+ static bool isOneIterationFunction (PostOrderFunctionInfo *PO) {
120
+ bool OneIterationFunction = true ;
121
+ llvm::DenseSet<SILBasicBlock *> HandledBBs;
122
+
123
+ for (SILBasicBlock *B : PO->getPostOrder ()) {
124
+ for (auto &X : B->getSuccessors ()) {
125
+ OneIterationFunction &= (HandledBBs.find (X) != HandledBBs.end ());
126
+ }
127
+ HandledBBs.insert (B);
128
+ }
129
+ return OneIterationFunction;
130
+ }
131
+
117
132
// / Returns true if this is an instruction that may have side effects in a
118
133
// / general sense but are inert from a load store perspective.
119
134
static bool isDeadStoreInertInstruction (SILInstruction *Inst) {
@@ -227,7 +242,7 @@ class BlockState {
227
242
void initReturnBlock (DSEContext &Ctx);
228
243
229
244
// / Initialize the bitvectors for the basic block.
230
- void init (DSEContext &Ctx);
245
+ void init (DSEContext &Ctx, bool OneIterationFunction );
231
246
232
247
// / Check whether the BBWriteSetIn has changed. If it does, we need to rerun
233
248
// / the data flow on this block's predecessors to reach fixed point.
@@ -387,6 +402,9 @@ class DSEContext {
387
402
// / Entry point for dead store elimination.
388
403
bool run ();
389
404
405
+ // / Run the iterative DF to converge the BBWriteSetIn.
406
+ void runIterativeDF ();
407
+
390
408
// / Returns the escape analysis we use.
391
409
EscapeAnalysis *getEA () { return EA; }
392
410
@@ -430,7 +448,7 @@ void BlockState::initReturnBlock(DSEContext &Ctx) {
430
448
}
431
449
}
432
450
433
- void BlockState::init (DSEContext &Ctx) {
451
+ void BlockState::init (DSEContext &Ctx, bool OneIterationFunction ) {
434
452
std::vector<LSLocation> &LV = Ctx.getLocationVault ();
435
453
LocationNum = LV.size ();
436
454
// The initial state of BBWriteSetIn should be all 1's. Otherwise the
@@ -446,7 +464,7 @@ void BlockState::init(DSEContext &Ctx) {
446
464
// However, by doing so, we can only eliminate the dead stores after the
447
465
// data flow stabilizes.
448
466
//
449
- BBWriteSetIn.resize (LocationNum, true );
467
+ BBWriteSetIn.resize (LocationNum, !OneIterationFunction );
450
468
BBWriteSetOut.resize (LocationNum, false );
451
469
BBWriteSetMid.resize (LocationNum, false );
452
470
@@ -536,6 +554,8 @@ void DSEContext::processBasicBlockForDSE(SILBasicBlock *BB) {
536
554
for (auto I = BB->rbegin (), E = BB->rend (); I != E; ++I) {
537
555
processInstruction (&(*I), DSEKind::PerformDSE);
538
556
}
557
+
558
+ S->BBWriteSetIn = S->BBWriteSetMid ;
539
559
}
540
560
541
561
void DSEContext::mergeSuccessorStates (SILBasicBlock *BB) {
@@ -948,28 +968,7 @@ void DSEContext::processInstruction(SILInstruction *I, DSEKind Kind) {
948
968
invalidateLSLocationBase (I, Kind);
949
969
}
950
970
951
- bool DSEContext::run () {
952
- // Walk over the function and find all the locations accessed by
953
- // this function.
954
- LSLocation::enumerateLSLocations (*F, LocationVault, LocToBitIndex, TE);
955
-
956
- // For all basic blocks in the function, initialize a BB state.
957
- //
958
- // DenseMap has a minimum size of 64, while many functions do not have more
959
- // than 64 basic blocks. Therefore, allocate the BlockState in a vector and
960
- // use pointer in BBToLocState to access them.
961
- for (auto &B : *F) {
962
- BlockStates.push_back (BlockState (&B));
963
- // Since we know all the locations accessed in this function, we can resize
964
- // the bit vector to the appropriate size.
965
- BlockStates.back ().init (*this );
966
- }
967
-
968
- // Initialize the BBToLocState mapping.
969
- for (auto &S : BlockStates) {
970
- BBToLocState[S.getBB ()] = &S;
971
- }
972
-
971
+ void DSEContext::runIterativeDF () {
973
972
// We perform dead store elimination in the following phases.
974
973
//
975
974
// Phase 1. we compute the max store set at the beginning of the basic block.
@@ -983,7 +982,6 @@ bool DSEContext::run() {
983
982
//
984
983
// Phase 5. we remove the dead stores.
985
984
986
-
987
985
// Generate the genset and killset for each basic block. We can process the
988
986
// basic blocks in any order.
989
987
//
@@ -1018,6 +1016,40 @@ bool DSEContext::run() {
1018
1016
}
1019
1017
}
1020
1018
}
1019
+ }
1020
+
1021
+ bool DSEContext::run () {
1022
+ // Is this a one iteration function.
1023
+ auto *PO = PM->getAnalysis <PostOrderAnalysis>()->get (F);
1024
+
1025
+ // Do we really need to run the iterative data flow on the function.
1026
+ bool OneIterationFunction = isOneIterationFunction (PO);
1027
+
1028
+ // Walk over the function and find all the locations accessed by
1029
+ // this function.
1030
+ LSLocation::enumerateLSLocations (*F, LocationVault, LocToBitIndex, TE);
1031
+
1032
+ // For all basic blocks in the function, initialize a BB state.
1033
+ //
1034
+ // DenseMap has a minimum size of 64, while many functions do not have more
1035
+ // than 64 basic blocks. Therefore, allocate the BlockState in a vector and
1036
+ // use pointer in BBToLocState to access them.
1037
+ for (auto &B : *F) {
1038
+ BlockStates.push_back (BlockState (&B));
1039
+ // Since we know all the locations accessed in this function, we can resize
1040
+ // the bit vector to the appropriate size.
1041
+ BlockStates.back ().init (*this , OneIterationFunction);
1042
+ }
1043
+
1044
+ // Initialize the BBToLocState mapping.
1045
+ for (auto &S : BlockStates) {
1046
+ BBToLocState[S.getBB ()] = &S;
1047
+ }
1048
+
1049
+ // We need to run the iterative data flow on the function.
1050
+ if (!OneIterationFunction) {
1051
+ runIterativeDF ();
1052
+ }
1021
1053
1022
1054
// The data flow has stabilized, run one last iteration over all the basic
1023
1055
// blocks and try to remove dead stores.
@@ -1043,6 +1075,7 @@ bool DSEContext::run() {
1043
1075
recursivelyDeleteTriviallyDeadInstructions (I, true );
1044
1076
}
1045
1077
}
1078
+
1046
1079
return Changed;
1047
1080
}
1048
1081
0 commit comments