Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -1372,6 +1372,13 @@ } } + // As the parent basic block terminator is a branch instruction which is + // removed at the end of the current transformation, use its previous + // instruction, as the reference insertion point. + Instruction *InsertPt = BIParent->size() > 1 + ? BIParent->getTerminator()->getPrevNode() + : BIParent->getTerminator(); + // Okay, it is safe to hoist the terminator. Instruction *NT = I1->clone(); BIParent->getInstList().insert(BI->getIterator(), NT); @@ -1381,6 +1388,11 @@ NT->takeName(I1); } + // For the instruction being hoisted, we do not retain its original debug + // location (DILocation); instead use the value from the reference insertion + // point. + NT->setDebugLoc(InsertPt->getDebugLoc()); + IRBuilder Builder(NT); // Hoisting one of the terminators from our successor is a great thing. // Unfortunately, the successors of the if/else blocks may have PHI nodes in Index: test/CodeGen/X86/pr39187.ll =================================================================== --- test/CodeGen/X86/pr39187.ll +++ test/CodeGen/X86/pr39187.ll @@ -0,0 +1,93 @@ +; RUN: opt < %s -S -simplifycfg | FileCheck %s + +; SimplifyCFG can hoist any common code in the 'then' and 'else' blocks to +; the 'if' basic block. +; +; For the special case, when hoisting the terminator instruction, its debug +; location keep references to its basic block, causing the debug information +; to become ambiguous. It causes the debugger to display unreached lines. + +; Change the debug location associated with the hoisted instruction, to +; the debug location from the insertion point in the 'if' block. + +; IR generated with: +; clang -S -g -gno-column-info -O2 -emit-llvm pr39187.cpp -o pr39187.ll -mllvm -opt-bisect-limit=10 + +; // pr39187.cpp +; int main() { +; volatile int foo = 0; +; +; int beards = 0; +; if (foo == 4) +; beards = 8; +; else +; beards = 4; +; +; return beards; +; } + +; CHECK-LABEL: entry +; CHECK: %foo = alloca i32, align 4 +; CHECK: %foo.0..sroa_cast = bitcast i32* %foo to i8* +; CHECK: store volatile i32 0, i32* %foo, align 4 +; CHECK: %foo.0. = load volatile i32, i32* %foo, align 4 +; CHECK: %cmp = icmp eq i32 %foo.0., 4, !dbg !13 +; CHECK: %. = select i1 %cmp, i32 8, i32 4, !dbg !13 + +; ModuleID = 'pr39187.cpp' +source_filename = "pr39187.cpp" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +; Function Attrs: norecurse nounwind uwtable +define dso_local i32 @main() local_unnamed_addr #0 !dbg !7 { +entry: + %foo = alloca i32, align 4 + %foo.0..sroa_cast = bitcast i32* %foo to i8* + store volatile i32 0, i32* %foo, align 4 + %foo.0. = load volatile i32, i32* %foo, align 4 + %cmp = icmp eq i32 %foo.0., 4, !dbg !21 + br i1 %cmp, label %if.then, label %if.else, !dbg !23 + +if.then: ; preds = %entry + call void @llvm.dbg.value(metadata i32 8, metadata !14, metadata !DIExpression()), !dbg !20 + br label %if.end, !dbg !24 + +if.else: ; preds = %entry + call void @llvm.dbg.value(metadata i32 4, metadata !14, metadata !DIExpression()), !dbg !20 + br label %if.end + +if.end: ; preds = %if.else, %if.then!19 + %beards.0 = phi i32 [ 8, %if.then ], [ 4, %if.else ], !dbg !25 + call void @llvm.dbg.value(metadata i32 %beards.0, metadata !14, metadata !DIExpression()), !dbg !20 + ret i32 %beards.0 +} + +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 +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 344693)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "pr39187.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 344693)"} +!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, 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 = !{!14} +!13 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !10) +!14 = !DILocalVariable(name: "beards", scope: !7, file: !1, line: 4, type: !10) +!20 = !DILocation(line: 4, scope: !7) +!21 = !DILocation(line: 5, scope: !22) +!22 = distinct !DILexicalBlock(scope: !7, file: !1, line: 5) +!23 = !DILocation(line: 5, scope: !7) +!24 = !DILocation(line: 6, scope: !22) +!25 = !DILocation(line: 0, scope: !22) +!26 = !DILocation(line: 11, scope: !7)