Index: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2897,6 +2897,7 @@ /// block. static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { assert(I->hasOneUse() && "Invariants didn't hold!"); + BasicBlock *SrcBlock = I->getParent(); // Cannot move control-flow-involving, volatile loads, vaarg, etc. if (isa(I) || I->isEHPad() || I->mayHaveSideEffects() || @@ -2926,10 +2927,20 @@ if (Scan->mayWriteToMemory()) return false; } - BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt(); I->moveBefore(&*InsertPos); ++NumSunkInst; + + // Also sink all related debug uses from the source basic block. Otherwise we + // get debug use before the def. + SmallVector DbgUsers; + findDbgUsers(DbgUsers, I); + for (auto *DII : DbgUsers) { + if (DII->getParent() == SrcBlock) { + DII->moveBefore(&*InsertPos); + DEBUG(dbgs() << "SINK: " << *DII << '\n'); + } + } return true; } Index: llvm/trunk/test/DebugInfo/X86/sdagsplit-1.ll =================================================================== --- llvm/trunk/test/DebugInfo/X86/sdagsplit-1.ll +++ llvm/trunk/test/DebugInfo/X86/sdagsplit-1.ll @@ -25,9 +25,9 @@ entry: tail call void @llvm.dbg.value(metadata i64 %a, metadata !13, metadata !16), !dbg !17 tail call void @llvm.dbg.value(metadata i64 %b, metadata !14, metadata !16), !dbg !18 - tail call void @llvm.dbg.value(metadata i64 %add, metadata !15, metadata !16), !dbg !19 %cmp = icmp eq i64 %a, %b, !dbg !20 %add = add nsw i64 %b, 1, !dbg !22 + tail call void @llvm.dbg.value(metadata i64 %add, metadata !15, metadata !16), !dbg !19 %retval.0 = select i1 %cmp, i64 %add, i64 0, !dbg !23 ret i64 %retval.0, !dbg !24 } Index: llvm/trunk/test/Transforms/InstCombine/debuginfo_add.ll =================================================================== --- llvm/trunk/test/Transforms/InstCombine/debuginfo_add.ll +++ llvm/trunk/test/Transforms/InstCombine/debuginfo_add.ll @@ -28,14 +28,20 @@ tail call void @llvm.dbg.value(metadata i64 %0, metadata !25, metadata !DIExpression()), !dbg !30 %offset.08 = add i64 %0, -4096 tail call void @llvm.dbg.value(metadata i64 %offset.08, metadata !26, metadata !DIExpression()), !dbg !31 - ; CHECK: call void @llvm.dbg.value(metadata i64 %0, metadata !26, metadata !DIExpression(DW_OP_constu, 4096, DW_OP_minus, DW_OP_stack_value)), !dbg !30 tail call void @llvm.dbg.value(metadata i32 undef, metadata !23, metadata !DIExpression()), !dbg !32 br i1 undef, label %for.end, label %for.body.lr.ph, !dbg !32 for.body.lr.ph: ; preds = %entry + ; The 'load' and the 'add' are sunken to this basic block. So let's verify that the related dbg.values are sunken as well. + ; The add is later eliminated, so we verify that the dbg.value is salvaged by using DW_OP_minus. + ; CHECK-LABEL: for.body.lr.ph: + ; CHECK-NEXT: %0 = load + ; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 %0, metadata !25, metadata !DIExpression()), !dbg ! + ; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 %0, metadata !26, metadata !DIExpression(DW_OP_constu, 4096, DW_OP_minus, DW_OP_stack_value)), !dbg ! br label %for.body, !dbg !32 for.body: ; preds = %for.body.lr.ph, %for.body + ; CHECK-LABEL: for.body: %offset.010 = phi i64 [ %offset.08, %for.body.lr.ph ], [ %offset.0, %for.body ] %head_size.09 = phi i32 [ undef, %for.body.lr.ph ], [ %sub2, %for.body ] tail call void @llvm.dbg.value(metadata i32 %head_size.09, metadata !23, metadata !DIExpression()), !dbg !31 @@ -43,7 +49,7 @@ %sub2 = add i32 %head_size.09, -4096, !dbg !37 %offset.0 = add i64 %offset.010, -4096 tail call void @llvm.dbg.value(metadata i64 %offset.0, metadata !26, metadata !DIExpression()), !dbg !30 - ; CHECK: call void @llvm.dbg.value(metadata i64 %offset.010, metadata !26, metadata !DIExpression(DW_OP_constu, 4096, DW_OP_minus, DW_OP_stack_value)), !dbg !29 + ; CHECK: call void @llvm.dbg.value(metadata i64 %offset.010, metadata !26, metadata !DIExpression(DW_OP_constu, 4096, DW_OP_minus, DW_OP_stack_value)), !dbg ! tail call void @llvm.dbg.value(metadata i32 %sub2, metadata !23, metadata !DIExpression()), !dbg !31 %tobool = icmp eq i32 %sub2, 0, !dbg !32 br i1 %tobool, label %for.end, label %for.body, !dbg !32, !llvm.loop !38