@@ -612,8 +612,10 @@ class CopyForwarding {
612
612
613
613
protected:
614
614
bool propagateCopy (CopyAddrInst *CopyInst, bool hoistingDestroy);
615
- CopyAddrInst *findCopyIntoDeadTemp (CopyAddrInst *destCopy);
616
- bool forwardDeadTempCopy (CopyAddrInst *srcCopy, CopyAddrInst *destCopy);
615
+ CopyAddrInst *findCopyIntoDeadTemp (
616
+ CopyAddrInst *destCopy,
617
+ SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete);
618
+ bool forwardDeadTempCopy (CopyAddrInst *destCopy);
617
619
bool forwardPropagateCopy ();
618
620
bool backwardPropagateCopy ();
619
621
bool hoistDestroy (SILInstruction *DestroyPoint, SILLocation DestroyLoc);
@@ -681,12 +683,10 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
681
683
// Handle copy-of-copy without analyzing uses.
682
684
// Assumes that CurrentCopy->getSrc() is dead after CurrentCopy.
683
685
assert (CurrentCopy->isTakeOfSrc () || hoistingDestroy);
684
- if (auto *srcCopy = findCopyIntoDeadTemp (CurrentCopy)) {
685
- if (forwardDeadTempCopy (srcCopy, CurrentCopy)) {
686
- HasChanged = true ;
687
- ++NumDeadTemp;
688
- return true ;
689
- }
686
+ if (forwardDeadTempCopy (CurrentCopy)) {
687
+ HasChanged = true ;
688
+ ++NumDeadTemp;
689
+ return true ;
690
690
}
691
691
692
692
if (forwardPropagateCopy ()) {
@@ -737,7 +737,9 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
737
737
// / Unlike the forward and backward propagation that finds all use points, this
738
738
// / handles copies of address projections. By conservatively checking all
739
739
// / intervening instructions, it avoids the need to analyze projection paths.
740
- CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp (CopyAddrInst *destCopy) {
740
+ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp (
741
+ CopyAddrInst *destCopy,
742
+ SmallVectorImpl<DebugValueAddrInst *> &debugInstsToDelete) {
741
743
auto tmpVal = destCopy->getSrc ();
742
744
assert (tmpVal == CurrentDef);
743
745
assert (isIdentifiedSourceValue (tmpVal));
@@ -750,8 +752,20 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
750
752
if (srcCopy->getDest () == tmpVal)
751
753
return srcCopy;
752
754
}
755
+ // 'SrcUserInsts' consists of all users of the 'temp'
753
756
if (SrcUserInsts.count (UserInst))
754
757
return nullptr ;
758
+
759
+ // Collect all debug_value_addr instructions between temp to dest copy and
760
+ // src to temp copy. On success, these debug_value_addr instructions should
761
+ // be deleted.
762
+ if (auto *debugUser = dyn_cast<DebugValueAddrInst>(UserInst)) {
763
+ // 'SrcDebugValueInsts' consists of all the debug users of 'temp'
764
+ if (SrcDebugValueInsts.count (debugUser))
765
+ debugInstsToDelete.push_back (debugUser);
766
+ continue ;
767
+ }
768
+
755
769
if (UserInst->mayWriteToMemory ())
756
770
return nullptr ;
757
771
}
@@ -780,12 +794,16 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
780
794
// / - %temp is uninitialized following `srcCopy` and subsequent instruction
781
795
// / attempts to destroy this uninitialized value.
782
796
bool CopyForwarding::
783
- forwardDeadTempCopy (CopyAddrInst *srcCopy, CopyAddrInst *destCopy) {
797
+ forwardDeadTempCopy (CopyAddrInst *destCopy) {
798
+ SmallVector<DebugValueAddrInst*, 2 > debugInstsToDelete;
799
+ auto *srcCopy = findCopyIntoDeadTemp (CurrentCopy, debugInstsToDelete);
800
+ if (!srcCopy)
801
+ return false ;
802
+
784
803
LLVM_DEBUG (llvm::dbgs () << " Temp Copy:" << *srcCopy
785
804
<< " to " << *destCopy);
786
805
787
806
assert (srcCopy->getDest () == destCopy->getSrc ());
788
-
789
807
// This pattern can be trivially folded without affecting %temp destroys:
790
808
// copy_addr [...] %src, [init] %temp
791
809
// copy_addr [take] %temp, [...] %dest
@@ -798,6 +816,11 @@ forwardDeadTempCopy(CopyAddrInst *srcCopy, CopyAddrInst *destCopy) {
798
816
.createDestroyAddr (srcCopy->getLoc (), srcCopy->getDest ());
799
817
}
800
818
819
+ // Delete all dead debug_value_addr instructions
820
+ for (auto *deadDebugUser : debugInstsToDelete) {
821
+ deadDebugUser->eraseFromParent ();
822
+ }
823
+
801
824
// Either `destCopy` is a take, or the caller is hoisting a destroy:
802
825
// copy_addr %temp, %dest
803
826
// ...
0 commit comments