Index: include/llvm/CodeGen/SlotIndexes.h
===================================================================
--- include/llvm/CodeGen/SlotIndexes.h
+++ include/llvm/CodeGen/SlotIndexes.h
@@ -414,7 +414,12 @@
     SlotIndex getInstructionIndex(const MachineInstr &MI) const {
       // Instructions inside a bundle have the same number as the bundle itself.
       const MachineInstr &BundleStart = *getBundleStart(MI.getIterator());
-      Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleStart);
+      // Skip debug instructions.
+      const MachineInstr &BundleNonDebug = *skipDebugInstructionsForward(
+                                  BundleStart.getIterator(), MI.getIterator());
+      assert(!BundleNonDebug.isDebugInstr() &&
+             "There is no non-debug instruction in the bundle.");
+      Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleNonDebug);
       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
       return itr->second;
     }
Index: lib/CodeGen/LiveDebugVariables.cpp
===================================================================
--- lib/CodeGen/LiveDebugVariables.cpp
+++ lib/CodeGen/LiveDebugVariables.cpp
@@ -583,10 +583,20 @@
         continue;
       }
       // DBG_VALUE has no slot index, use the previous instruction instead.
-      SlotIndex Idx =
-          MBBI == MBB->begin()
-              ? LIS->getMBBStartIdx(MBB)
-              : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
+      SlotIndex Idx;
+      if (MBBI == MBB->begin()) {
+        Idx = LIS->getMBBStartIdx(MBB);
+      } else {
+        const MachineInstr &PrevMI = *skipDebugInstructionsBackward(
+                                                       MBBI, MBB->begin());
+        // If PrevMI is still debug instruction, it means all previous
+        // instructions are debug instructions. Use MBB's SlotIndex as the
+        // instruction's SlotIndex.
+        if (PrevMI.isDebugInstr())
+          Idx = LIS->getMBBStartIdx(MBB);
+        else
+          Idx = LIS->getInstructionIndex(PrevMI).getRegSlot();
+      }
       // Handle consecutive DBG_VALUE instructions with the same slot index.
       do {
         if (handleDebugValue(*MBBI, Idx)) {
Index: test/DebugInfo/Generic/debug-var-slot.ll
===================================================================
--- /dev/null
+++ test/DebugInfo/Generic/debug-var-slot.ll
@@ -0,0 +1,59 @@
+; RUN: llc -filetype=obj -o - %s | llvm-dwarfdump -v - | FileCheck %s
+;
+; CHECK: .debug_info contents:
+; CHECK: DW_TAG_variable
+; CHECK: DW_AT_name [DW_FORM_strp] {{.*}}"local_var"
+; CHECK-NEXT: DW_AT_decl_file [DW_FORM_data1] {{.*}}debug-var-slot.c
+; CHECK-NEXT: DW_AT_decl_line [DW_FORM_data1] {{.*}}7
+; CHECK: DW_TAG_label
+; CHECK-NEXT: DW_AT_name [DW_FORM_strp] {{.*}}"top"
+; CHECK-NEXT: DW_AT_decl_file [DW_FORM_data1] {{.*}}debug-var-slot.c
+; CHECK-NEXT: DW_AT_decl_line [DW_FORM_data1] {{.*}}4
+; CHECK-NEXT: DW_AT_low_pc [DW_FORM_addr] {{.*}}{{0x[0-9a-f]+}}
+
+source_filename = "debug-var-slot.c"
+
+define dso_local i32 @foo(i32 %a, i32 %b) !dbg !6 {
+entry:
+  %a.addr = alloca i32, align 4
+  %b.addr = alloca i32, align 4
+  %sum = alloca i32, align 4
+  store i32 %a, i32* %a.addr, align 4
+  store i32 %b, i32* %b.addr, align 4
+  br label %top
+
+top:
+  call void @llvm.dbg.label(metadata !10), !dbg !13
+  call void @llvm.dbg.value(metadata i32 %0, metadata !12, metadata !DIExpression()), !dbg !14
+  %0 = load i32, i32* %a.addr, align 4
+  %1 = load i32, i32* %a.addr, align 4
+  %2 = load i32, i32* %b.addr, align 4
+  %add = add nsw i32 %1, %2
+  store i32 %add, i32* %sum, align 4
+  br label %done
+
+done:
+  %3 = load i32, i32* %sum, align 4
+  ret i32 %3, !dbg !15
+}
+
+declare void @llvm.dbg.label(metadata)
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: true, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "debug-var-slot.c", directory: "./")
+!2 = !{}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 2, isOptimized: true, unit: !0, retainedNodes: !2)
+!7 = !DISubroutineType(types: !8)
+!8 = !{!9, !9, !9}
+!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!10 = !DILabel(scope: !6, name: "top", file: !1, line: 4)
+!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!12 = !DILocalVariable(name: "local_var", scope: !6, file: !1, line: 7, type: !11)
+!13 = !DILocation(line: 4, column: 1, scope: !6)
+!14 = !DILocation(line: 7, column: 1, scope: !6)
+!15 = !DILocation(line: 8, column: 3, scope: !6)