@@ -612,8 +612,10 @@ class CopyForwarding {
612612
613613protected:
614614 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);
617619 bool forwardPropagateCopy ();
618620 bool backwardPropagateCopy ();
619621 bool hoistDestroy (SILInstruction *DestroyPoint, SILLocation DestroyLoc);
@@ -681,12 +683,10 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
681683 // Handle copy-of-copy without analyzing uses.
682684 // Assumes that CurrentCopy->getSrc() is dead after CurrentCopy.
683685 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 ;
690690 }
691691
692692 if (forwardPropagateCopy ()) {
@@ -737,7 +737,9 @@ propagateCopy(CopyAddrInst *CopyInst, bool hoistingDestroy) {
737737// / Unlike the forward and backward propagation that finds all use points, this
738738// / handles copies of address projections. By conservatively checking all
739739// / 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) {
741743 auto tmpVal = destCopy->getSrc ();
742744 assert (tmpVal == CurrentDef);
743745 assert (isIdentifiedSourceValue (tmpVal));
@@ -750,8 +752,20 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
750752 if (srcCopy->getDest () == tmpVal)
751753 return srcCopy;
752754 }
755+ // 'SrcUserInsts' consists of all users of the 'temp'
753756 if (SrcUserInsts.count (UserInst))
754757 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+
755769 if (UserInst->mayWriteToMemory ())
756770 return nullptr ;
757771 }
@@ -780,12 +794,16 @@ CopyAddrInst *CopyForwarding::findCopyIntoDeadTemp(CopyAddrInst *destCopy) {
780794// / - %temp is uninitialized following `srcCopy` and subsequent instruction
781795// / attempts to destroy this uninitialized value.
782796bool 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+
784803 LLVM_DEBUG (llvm::dbgs () << " Temp Copy:" << *srcCopy
785804 << " to " << *destCopy);
786805
787806 assert (srcCopy->getDest () == destCopy->getSrc ());
788-
789807 // This pattern can be trivially folded without affecting %temp destroys:
790808 // copy_addr [...] %src, [init] %temp
791809 // copy_addr [take] %temp, [...] %dest
@@ -798,6 +816,11 @@ forwardDeadTempCopy(CopyAddrInst *srcCopy, CopyAddrInst *destCopy) {
798816 .createDestroyAddr (srcCopy->getLoc (), srcCopy->getDest ());
799817 }
800818
819+ // Delete all dead debug_value_addr instructions
820+ for (auto *deadDebugUser : debugInstsToDelete) {
821+ deadDebugUser->eraseFromParent ();
822+ }
823+
801824 // Either `destCopy` is a take, or the caller is hoisting a destroy:
802825 // copy_addr %temp, %dest
803826 // ...
0 commit comments