Index: llvm/lib/CodeGen/VirtRegMap.cpp =================================================================== --- llvm/lib/CodeGen/VirtRegMap.cpp +++ llvm/lib/CodeGen/VirtRegMap.cpp @@ -181,6 +181,7 @@ SlotIndexes *Indexes; LiveIntervals *LIS; VirtRegMap *VRM; + LiveDebugVariables *DebugVars; DenseSet RewriteRegs; bool ClearVirtRegs; @@ -238,6 +239,10 @@ AU.addRequired(); AU.addPreserved(); AU.addRequired(); + + if (!ClearVirtRegs) + AU.addPreserved(); + MachineFunctionPass::getAnalysisUsage(AU); } @@ -249,6 +254,7 @@ Indexes = &getAnalysis(); LIS = &getAnalysis(); VRM = &getAnalysis(); + DebugVars = getAnalysisIfAvailable(); LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" << "********** Function: " << MF->getName() << '\n'); LLVM_DEBUG(VRM->dump()); @@ -262,10 +268,13 @@ // Rewrite virtual registers. rewrite(); - // Write out new DBG_VALUE instructions. - getAnalysis().emitDebugValues(VRM); + if (DebugVars && ClearVirtRegs) { + // Write out new DBG_VALUE instructions. + + // We only do this if ClearVirtRegs is specified since this should be the + // final run of the pass and we don't want to emit them multiple times. + DebugVars->emitDebugValues(VRM); - if (ClearVirtRegs) { // All machine operands and other references to virtual registers have been // replaced. Remove the virtual registers and release all the transient data. VRM->clearAllVirt(); Index: llvm/test/CodeGen/AMDGPU/debug-value.ll =================================================================== --- llvm/test/CodeGen/AMDGPU/debug-value.ll +++ llvm/test/CodeGen/AMDGPU/debug-value.ll @@ -71,7 +71,10 @@ %tmp56 = fmul float %tmp55, %tmp50 %tmp57 = fmul float %tmp54, %tmp56 %tmp58 = fdiv float %tmp57, 0.000000e+00 + ; Make sure this isn't double emitted + ; CHECK-NOT: ;DEBUG_VALUE: ; CHECK: ;DEBUG_VALUE: foo:var <- [DW_OP_constu 1, DW_OP_swap, DW_OP_xderef] + ; CHECK-NOT: ;DEBUG_VALUE: call void @llvm.dbg.value(metadata <4 x float> %tmp29, metadata !3, metadata !DIExpression(DW_OP_constu, 1, DW_OP_swap, DW_OP_xderef)) #2, !dbg !5 %tmp59 = bitcast i64 %tmp35 to <2 x float> %tmp60 = insertelement <2 x float> undef, float %tmp58, i32 0