diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
--- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp
@@ -1324,13 +1324,11 @@
   /// Step through the function, recording register definitions and movements
   /// in an MLocTracker. Convert the observations into a per-block transfer
   /// function in \p MLocTransfer, suitable for using with the machine value
-  /// location dataflow problem. Do the same with VLoc trackers in \p VLocs,
-  /// although the precise machine value numbers can't be known until after
-  /// the machine value number problem is solved.
-  void produceTransferFunctions(MachineFunction &MF,
-                                SmallVectorImpl<MLocTransferMap> &MLocTransfer,
-                                unsigned MaxNumBlocks,
-                                SmallVectorImpl<VLocTracker> &VLocs);
+  /// location dataflow problem.
+  void
+  produceMLocTransferFunction(MachineFunction &MF,
+                              SmallVectorImpl<MLocTransferMap> &MLocTransfer,
+                              unsigned MaxNumBlocks);
 
   /// Solve the machine value location dataflow problem. Takes as input the
   /// transfer functions in \p MLocTransfer. Writes the output live-in and
@@ -1925,9 +1923,9 @@
   transferRegisterDef(MI);
 }
 
-void InstrRefBasedLDV::produceTransferFunctions(
+void InstrRefBasedLDV::produceMLocTransferFunction(
     MachineFunction &MF, SmallVectorImpl<MLocTransferMap> &MLocTransfer,
-    unsigned MaxNumBlocks, SmallVectorImpl<VLocTracker> &VLocs) {
+    unsigned MaxNumBlocks) {
   // Because we try to optimize around register mask operands by ignoring regs
   // that aren't currently tracked, we set up something ugly for later: RegMask
   // operands that are seen earlier than the first use of a register, still need
@@ -1956,9 +1954,6 @@
     MTracker->reset();
     MTracker->setMPhis(CurBB);
 
-    VTracker = &VLocs[CurBB];
-    VTracker->MBB = &MBB;
-
     // Step through each instruction in this block.
     for (auto &MI : MBB) {
       process(MI);
@@ -2809,7 +2804,6 @@
           CurBB, VarsWeCareAbout, MOutLocs, MInLocs, InScopeBlocks,
           BlocksToExplore, JoinedInLocs);
 
-      auto &VTracker = AllTheVLocs[MBB->getNumber()];
       bool FirstVisit = VLOCVisited.insert(MBB).second;
 
       // Always explore transfer function if inlocs changed, or if we've not
@@ -2821,23 +2815,11 @@
       if (DowngradeOccurred && OnPending.insert(MBB).second)
         Pending.push(BBToOrder[MBB]);
 
-      // Patch up the variable value transfer function to use the live-in
-      // machine values, now that that problem is solved.
-      if (FirstVisit) {
-        for (auto &Transfer : VTracker.Vars) {
-          if (Transfer.second.Kind == DbgValue::Def &&
-              Transfer.second.ID.getBlock() == CurBB &&
-              Transfer.second.ID.isPHI()) {
-            LocIdx Loc = Transfer.second.ID.getLoc();
-            Transfer.second.ID = MInLocs[CurBB][Loc.asU64()];
-          }
-        }
-      }
-
       if (!InLocsChanged)
         continue;
 
       // Do transfer function.
+      auto &VTracker = AllTheVLocs[MBB->getNumber()];
       for (auto &Transfer : VTracker.Vars) {
         // Is this var we're mangling in this scope?
         if (VarsWeCareAbout.count(Transfer.first)) {
@@ -3035,7 +3017,7 @@
 
   initialSetup(MF);
 
-  produceTransferFunctions(MF, MLocTransfer, MaxNumBlocks, vlocs);
+  produceMLocTransferFunction(MF, MLocTransfer, MaxNumBlocks);
 
   // Allocate and initialize two array-of-arrays for the live-in and live-out
   // machine values. The outer dimension is the block number; while the inner
@@ -3054,6 +3036,22 @@
   // dataflow problem.
   mlocDataflow(MInLocs, MOutLocs, MLocTransfer);
 
+  // Walk back through each block / instruction, collecting DBG_VALUE
+  // instructions and recording what machine value their operands refer to.
+  for (auto &OrderPair : OrderToBB) {
+    MachineBasicBlock &MBB = *OrderPair.second;
+    CurBB = MBB.getNumber();
+    VTracker = &vlocs[CurBB];
+    VTracker->MBB = &MBB;
+    MTracker->loadFromArray(MInLocs[CurBB], CurBB);
+    CurInst = 1;
+    for (auto &MI : MBB) {
+      process(MI);
+      ++CurInst;
+    }
+    MTracker->reset();
+  }
+
   // Number all variables in the order that they appear, to be used as a stable
   // insertion order later.
   DenseMap<DebugVariable, unsigned> AllVarsNumbering;