Index: llvm/trunk/include/llvm/CodeGen/MachineInstr.h =================================================================== --- llvm/trunk/include/llvm/CodeGen/MachineInstr.h +++ llvm/trunk/include/llvm/CodeGen/MachineInstr.h @@ -1530,6 +1530,9 @@ /// Add all implicit def and use operands to this instruction. void addImplicitDefUseOperands(MachineFunction &MF); + /// Scan instructions following MI and collect any matching DBG_VALUEs. + void collectDebugValues(SmallVectorImpl &DbgValues); + private: /// If this instruction is embedded into a MachineFunction, return the /// MachineRegisterInfo object for the current function, otherwise Index: llvm/trunk/lib/CodeGen/MachineCSE.cpp =================================================================== --- llvm/trunk/lib/CodeGen/MachineCSE.cpp +++ llvm/trunk/lib/CodeGen/MachineCSE.cpp @@ -180,6 +180,14 @@ continue; LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI); LLVM_DEBUG(dbgs() << "*** to: " << *MI); + + // Collect matching debug values. + SmallVector DbgValues; + DefMI->collectDebugValues(DbgValues); + // Propagate SrcReg to debug value instructions. + for (auto *DBI : DbgValues) + DBI->getOperand(0).setReg(SrcReg); + // Propagate SrcReg of copies to MI. MO.setReg(SrcReg); MRI->clearKillFlags(SrcReg); Index: llvm/trunk/lib/CodeGen/MachineInstr.cpp =================================================================== --- llvm/trunk/lib/CodeGen/MachineInstr.cpp +++ llvm/trunk/lib/CodeGen/MachineInstr.cpp @@ -2031,3 +2031,20 @@ Orig.getOperand(1).ChangeToImmediate(0U); Orig.getOperand(3).setMetadata(Expr); } + +void MachineInstr::collectDebugValues( + SmallVectorImpl &DbgValues) { + MachineInstr &MI = *this; + if (!MI.getOperand(0).isReg()) + return; + + MachineBasicBlock::iterator DI = MI; ++DI; + for (MachineBasicBlock::iterator DE = MI.getParent()->end(); + DI != DE; ++DI) { + if (!DI->isDebugValue()) + return; + if (DI->getOperand(0).isReg() && + DI->getOperand(0).getReg() == MI.getOperand(0).getReg()) + DbgValues.push_back(&*DI); + } +} Index: llvm/trunk/lib/CodeGen/MachineSink.cpp =================================================================== --- llvm/trunk/lib/CodeGen/MachineSink.cpp +++ llvm/trunk/lib/CodeGen/MachineSink.cpp @@ -513,25 +513,6 @@ return true; } -/// collectDebgValues - Scan instructions following MI and collect any -/// matching DBG_VALUEs. -static void collectDebugValues(MachineInstr &MI, - SmallVectorImpl &DbgValues) { - DbgValues.clear(); - if (!MI.getOperand(0).isReg()) - return; - - MachineBasicBlock::iterator DI = MI; ++DI; - for (MachineBasicBlock::iterator DE = MI.getParent()->end(); - DI != DE; ++DI) { - if (!DI->isDebugValue()) - return; - if (DI->getOperand(0).isReg() && - DI->getOperand(0).getReg() == MI.getOperand(0).getReg()) - DbgValues.push_back(&*DI); - } -} - /// isProfitableToSinkTo - Return true if it is profitable to sink MI. bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr &MI, MachineBasicBlock *MBB, @@ -758,7 +739,7 @@ MachineBasicBlock::iterator InsertPos) { // Collect matching debug values. SmallVector DbgValuesToSink; - collectDebugValues(MI, DbgValuesToSink); + MI.collectDebugValues(DbgValuesToSink); // If we cannot find a location to use (merge with), then we erase the debug // location to prevent debug-info driven tools from potentially reporting Index: llvm/trunk/test/CodeGen/X86/debuginfo-locations-dce.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/debuginfo-locations-dce.ll +++ llvm/trunk/test/CodeGen/X86/debuginfo-locations-dce.ll @@ -0,0 +1,72 @@ +; RUN: llc -O2 %s -o %t -filetype=obj +; RUN: llvm-dwarfdump -debug-info %t | FileCheck %s + +; Check that Machine CSE correctly handles during the transformation, the +; debug location information for variables. + +; Generated with clang -c -g -O2 + +; typedef float __attribute__((__vector_size__(16))) f4; +; f4 get(); +; int main() { +; float MyVar = get()[0]; +; if (MyVar) +; return 1; +; } + +; ModuleID = 'test.cpp' +source_filename = "test.cpp" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +define dso_local i32 @main() !dbg !7 { +entry: + %call = tail call <4 x float> @_Z3getv(), !dbg !14 + %vecext = extractelement <4 x float> %call, i32 0, !dbg !14 + call void @llvm.dbg.value(metadata float %vecext, metadata !12, metadata !DIExpression()), !dbg !15 + %tobool = fcmp une float %vecext, 0.000000e+00, !dbg !16 + %. = zext i1 %tobool to i32, !dbg !18 + ret i32 %., !dbg !19 +} + +declare dso_local <4 x float> @_Z3getv() + +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 8.0.0 (trunk 339665)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "test.cpp", directory: ".") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 8.0.0 (trunk 339665)"} +!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 3, type: !8, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{!12} +!12 = !DILocalVariable(name: "MyVar", scope: !7, file: !1, line: 4, type: !13) +!13 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float) +!14 = !DILocation(line: 4, column: 18, scope: !7) +!15 = !DILocation(line: 4, column: 9, scope: !7) +!16 = !DILocation(line: 5, column: 7, scope: !17) +!17 = distinct !DILexicalBlock(scope: !7, file: !1, line: 5, column: 7) +!18 = !DILocation(line: 6, column: 5, scope: !17) +!19 = !DILocation(line: 7, column: 1, scope: !7) + +; Look at the debug location information for variable 'MyVar'. +; Verify that we see a sequence of DI entries, that looks like: +; DW_TAG_variable +; DW_AT_location (0x00000000 +; [0x0000000000000009, 0x0000000000000012): DW_OP_reg17 XMM0) +; DW_AT_name ("MyVar") + +; CHECK-LABEL: DW_TAG_variable +; CHECK-NEXT: DW_AT_location{{.*}} +; CHECK-NEXT: {{.*}}DW_OP_reg17 XMM0 +; CHECK-NEXT: DW_AT_name{{.*}}("MyVar") +