@@ -941,10 +941,12 @@ void CanonicalizeOSSALifetime::findExtendedBoundary(
941
941
942
942
// / Create a new destroy_value instruction before the specified instruction and
943
943
// / record it as a final consume.
944
- static void insertDestroyBeforeInstruction (SILInstruction *nextInstruction,
945
- SILValue currentDef,
946
- CanonicalOSSAConsumeInfo &consumes,
947
- InstModCallbacks &callbacks) {
944
+ static void
945
+ insertDestroyBeforeInstruction (SILInstruction *nextInstruction,
946
+ SILValue currentDef,
947
+ CanonicalOSSAConsumeInfo &consumes,
948
+ SmallVectorImpl<DestroyValueInst *> &destroys,
949
+ InstModCallbacks &callbacks) {
948
950
// OSSALifetimeCompletion: This conditional clause can be deleted with
949
951
// complete lifetimes.
950
952
if (consumes.isUnreachableLifetimeEnd (nextInstruction)) {
@@ -976,6 +978,7 @@ static void insertDestroyBeforeInstruction(SILInstruction *nextInstruction,
976
978
callbacks.createdNewInst (dvi);
977
979
consumes.recordFinalConsume (dvi);
978
980
++NumDestroysGenerated;
981
+ destroys.push_back (dvi);
979
982
}
980
983
981
984
// / Inserts destroys along the boundary where needed and records all final
@@ -987,7 +990,8 @@ static void insertDestroyBeforeInstruction(SILInstruction *nextInstruction,
987
990
// / - The postdominating consumes cannot be within nested loops.
988
991
// / - Any blocks in nested loops are now marked LiveOut.
989
992
void CanonicalizeOSSALifetime::insertDestroysOnBoundary (
990
- PrunedLivenessBoundary const &boundary) {
993
+ PrunedLivenessBoundary const &boundary,
994
+ SmallVectorImpl<DestroyValueInst *> &newDestroys) {
991
995
BasicBlockSet seenMergePoints (getCurrentDef ()->getFunction ());
992
996
for (auto *instruction : boundary.lastUsers ) {
993
997
if (destroys.contains (instruction)) {
@@ -1009,7 +1013,7 @@ void CanonicalizeOSSALifetime::insertDestroysOnBoundary(
1009
1013
}
1010
1014
auto *insertionPoint = &*successor->begin ();
1011
1015
insertDestroyBeforeInstruction (insertionPoint, getCurrentDef (),
1012
- consumes, getCallbacks ());
1016
+ consumes, newDestroys, getCallbacks ());
1013
1017
LLVM_DEBUG (llvm::dbgs () << " Destroy after terminator "
1014
1018
<< *instruction << " at beginning of " ;
1015
1019
successor->printID (llvm::dbgs (), false );
@@ -1019,7 +1023,7 @@ void CanonicalizeOSSALifetime::insertDestroysOnBoundary(
1019
1023
}
1020
1024
auto *insertionPoint = instruction->getNextInstruction ();
1021
1025
insertDestroyBeforeInstruction (insertionPoint, getCurrentDef (), consumes,
1022
- getCallbacks ());
1026
+ newDestroys, getCallbacks ());
1023
1027
LLVM_DEBUG (llvm::dbgs ()
1024
1028
<< " Destroy at last use " << insertionPoint << " \n " );
1025
1029
continue ;
@@ -1028,22 +1032,22 @@ void CanonicalizeOSSALifetime::insertDestroysOnBoundary(
1028
1032
for (auto *edgeDestination : boundary.boundaryEdges ) {
1029
1033
auto *insertionPoint = &*edgeDestination->begin ();
1030
1034
insertDestroyBeforeInstruction (insertionPoint, getCurrentDef (), consumes,
1031
- getCallbacks ());
1035
+ newDestroys, getCallbacks ());
1032
1036
LLVM_DEBUG (llvm::dbgs () << " Destroy on edge " << edgeDestination << " \n " );
1033
1037
}
1034
1038
for (auto *def : boundary.deadDefs ) {
1035
1039
if (auto *arg = dyn_cast<SILArgument>(def)) {
1036
1040
auto *insertionPoint = &*arg->getParent ()->begin ();
1037
1041
insertDestroyBeforeInstruction (insertionPoint, getCurrentDef (), consumes,
1038
- getCallbacks ());
1042
+ newDestroys, getCallbacks ());
1039
1043
LLVM_DEBUG (llvm::dbgs ()
1040
1044
<< " Destroy after dead def arg " << arg << " \n " );
1041
1045
} else {
1042
1046
auto *instruction = cast<SILInstruction>(def);
1043
1047
auto *insertionPoint = instruction->getNextInstruction ();
1044
1048
assert (insertionPoint && " def instruction was a terminator?!" );
1045
1049
insertDestroyBeforeInstruction (insertionPoint, getCurrentDef (), consumes,
1046
- getCallbacks ());
1050
+ newDestroys, getCallbacks ());
1047
1051
LLVM_DEBUG (llvm::dbgs ()
1048
1052
<< " Destroy after dead def inst " << instruction << " \n " );
1049
1053
}
@@ -1235,8 +1239,9 @@ void CanonicalizeOSSALifetime::rewriteLifetimes() {
1235
1239
findExtendedBoundary (originalBoundary, extendedBoundary);
1236
1240
}
1237
1241
1242
+ SmallVector<DestroyValueInst *> newDestroys;
1238
1243
// Step 5: insert destroys and record consumes
1239
- insertDestroysOnBoundary (extendedBoundary);
1244
+ insertDestroysOnBoundary (extendedBoundary, newDestroys );
1240
1245
// Step 6: rewrite copies and delete extra destroys
1241
1246
rewriteCopies ();
1242
1247
0 commit comments