diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2898,9 +2898,14 @@ // could replace PBI's branch probabilities with BI's. // Copy any debug value intrinsics into the end of PredBlock. - for (Instruction &I : *BB) - if (isa(I)) - I.clone()->insertBefore(PBI); + for (Instruction &I : *BB) { + if (isa(I)) { + Instruction *NewI = I.clone(); + RemapInstruction(NewI, VMap, + RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); + NewI->insertBefore(PBI); + } + } return true; } diff --git a/llvm/test/DebugInfo/simplify-cfg-preserve-dbg-values.ll b/llvm/test/DebugInfo/simplify-cfg-preserve-dbg-values.ll new file mode 100644 --- /dev/null +++ b/llvm/test/DebugInfo/simplify-cfg-preserve-dbg-values.ll @@ -0,0 +1,182 @@ +; RUN: opt %s -simplifycfg -verify -S -o - | FileCheck %s +; +; Verify that SimplifyCFG does not invalidate operands for +; llvm.dbg.value intrinsics. +; +; Based on the following example, compiled +; -O1 -g -fno-exceptions -fno-discard-value-names: +; +; void test() { +; bool a = false; +; bool b = false; +; bool c = false; +; [&a, &b, &c] { b = true; }(); +; +; if (a || b || c) { +; __builtin_debugtrap(); +; } +; } + +; CHECK: _Z4testv +; CHECK-NOT: llvm.dbg.value(metadata ! +; CHECK: ret void + +%class.anon = type { i8*, i8*, i8* } + +; Function Attrs: nounwind uwtable +define dso_local void @_Z4testv() local_unnamed_addr #0 !dbg !7 { +entry: + %a = alloca i8, align 1 + %b = alloca i8, align 1 + %c = alloca i8, align 1 + %ref.tmp = alloca %class.anon, align 8 + call void @llvm.dbg.value(metadata i8* %a, metadata !12, metadata !DIExpression(DW_OP_deref)), !dbg !16 + call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %a) #4, !dbg !17 + call void @llvm.dbg.value(metadata i8 0, metadata !12, metadata !DIExpression()), !dbg !16 + store i8 0, i8* %a, align 1, !dbg !16, !tbaa !18 + call void @llvm.dbg.value(metadata i8* %b, metadata !14, metadata !DIExpression(DW_OP_deref)), !dbg !22 + call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %b) #4, !dbg !23 + call void @llvm.dbg.value(metadata i8 0, metadata !14, metadata !DIExpression()), !dbg !22 + store i8 0, i8* %b, align 1, !dbg !22, !tbaa !18 + call void @llvm.dbg.value(metadata i8* %c, metadata !15, metadata !DIExpression(DW_OP_deref)), !dbg !24 + call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %c) #4, !dbg !25 + call void @llvm.dbg.value(metadata i8 0, metadata !15, metadata !DIExpression()), !dbg !24 + store i8 0, i8* %c, align 1, !dbg !24, !tbaa !18 + %0 = bitcast %class.anon* %ref.tmp to i8*, !dbg !26 + call void @llvm.lifetime.start.p0i8(i64 24, i8* %0) #4, !dbg !26 + %1 = getelementptr inbounds %class.anon, %class.anon* %ref.tmp, i64 0, i32 0, !dbg !26 + store i8* %a, i8** %1, align 8, !dbg !26, !tbaa !27 + %2 = getelementptr inbounds %class.anon, %class.anon* %ref.tmp, i64 0, i32 1, !dbg !26 + store i8* %b, i8** %2, align 8, !dbg !26, !tbaa !27 + %3 = getelementptr inbounds %class.anon, %class.anon* %ref.tmp, i64 0, i32 2, !dbg !26 + store i8* %c, i8** %3, align 8, !dbg !26, !tbaa !27 + call fastcc void @"_ZZ4testvENK3$_0clEv"(%class.anon* nonnull %ref.tmp), !dbg !26 + call void @llvm.lifetime.end.p0i8(i64 24, i8* %0) #4, !dbg !26 + %4 = load i8, i8* %a, align 1, !dbg !29, !tbaa !18, !range !31 + call void @llvm.dbg.value(metadata i8 %4, metadata !12, metadata !DIExpression()), !dbg !16 + %tobool = icmp eq i8 %4, 0, !dbg !29 + br i1 %tobool, label %lor.lhs.false, label %if.then, !dbg !32 + +lor.lhs.false: ; preds = %entry + %5 = load i8, i8* %b, align 1, !dbg !33, !tbaa !18, !range !31 + call void @llvm.dbg.value(metadata i8 %5, metadata !14, metadata !DIExpression()), !dbg !22 + %tobool1 = icmp eq i8 %5, 0, !dbg !33 + br i1 %tobool1, label %lor.lhs.false2, label %if.then, !dbg !34 + +lor.lhs.false2: ; preds = %lor.lhs.false + %6 = load i8, i8* %c, align 1, !dbg !35, !tbaa !18, !range !31 + call void @llvm.dbg.value(metadata i8 %6, metadata !15, metadata !DIExpression()), !dbg !24 + %tobool3 = icmp eq i8 %6, 0, !dbg !35 + br i1 %tobool3, label %if.end, label %if.then, !dbg !36 + +if.then: ; preds = %lor.lhs.false2, %lor.lhs.false, %entry + call void @llvm.debugtrap(), !dbg !37 + br label %if.end, !dbg !39 + +if.end: ; preds = %lor.lhs.false2, %if.then + call void @llvm.dbg.value(metadata i8* %c, metadata !15, metadata !DIExpression(DW_OP_deref)), !dbg !24 + call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %c) #4, !dbg !40 + call void @llvm.dbg.value(metadata i8* %b, metadata !14, metadata !DIExpression(DW_OP_deref)), !dbg !22 + call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %b) #4, !dbg !40 + call void @llvm.dbg.value(metadata i8* %a, metadata !12, metadata !DIExpression(DW_OP_deref)), !dbg !16 + call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %a) #4, !dbg !40 + ret void, !dbg !40 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +; Function Attrs: inlinehint nounwind uwtable +define internal fastcc void @"_ZZ4testvENK3$_0clEv"(%class.anon* %this) unnamed_addr #3 align 2 !dbg !41 { +entry: + call void @llvm.dbg.value(metadata %class.anon* %this, metadata !54, metadata !DIExpression()), !dbg !56 + %0 = getelementptr inbounds %class.anon, %class.anon* %this, i32 0, i32 1, !dbg !57 + %1 = load i8*, i8** %0, align 8, !dbg !57, !tbaa !58 + store i8 1, i8* %1, align 1, !dbg !60, !tbaa !18 + ret void, !dbg !61 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1 + +; Function Attrs: nounwind +declare void @llvm.debugtrap() #4 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + +attributes #0 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { argmemonly nounwind } +attributes #2 = { nounwind readnone speculatable } +attributes #3 = { inlinehint nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #4 = { nounwind } + +!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.1-4 (tags/RELEASE_801/final)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "/usr/local/google/home/rickyz/test.cc", directory: "/usr/local/google/home/rickyz") +!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.1-4 (tags/RELEASE_801/final)"} +!7 = distinct !DISubprogram(name: "test", linkageName: "_Z4testv", scope: !8, file: !8, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) +!8 = !DIFile(filename: "test.cc", directory: "/usr/local/google/home/rickyz") +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !{!12, !14, !15} +!12 = !DILocalVariable(name: "a", scope: !7, file: !8, line: 2, type: !13) +!13 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean) +!14 = !DILocalVariable(name: "b", scope: !7, file: !8, line: 3, type: !13) +!15 = !DILocalVariable(name: "c", scope: !7, file: !8, line: 4, type: !13) +!16 = !DILocation(line: 2, column: 3, scope: !7) +!17 = !DILocation(line: 2, column: 8, scope: !7) +!18 = !{!19, !19, i64 0} +!19 = !{!"bool", !20, i64 0} +!20 = !{!"omnipotent char", !21, i64 0} +!21 = !{!"Simple C++ TBAA"} +!22 = !DILocation(line: 3, column: 3, scope: !7) +!23 = !DILocation(line: 3, column: 8, scope: !7) +!24 = !DILocation(line: 4, column: 3, scope: !7) +!25 = !DILocation(line: 4, column: 8, scope: !7) +!26 = !DILocation(line: 5, column: 3, scope: !7) +!27 = !{!28, !28, i64 0} +!28 = !{!"any pointer", !20, i64 0} +!29 = !DILocation(line: 7, column: 7, scope: !30) +!30 = distinct !DILexicalBlock(scope: !7, file: !8, line: 7, column: 7) +!31 = !{i8 0, i8 2} +!32 = !DILocation(line: 7, column: 9, scope: !30) +!33 = !DILocation(line: 7, column: 12, scope: !30) +!34 = !DILocation(line: 7, column: 14, scope: !30) +!35 = !DILocation(line: 7, column: 17, scope: !30) +!36 = !DILocation(line: 7, column: 7, scope: !7) +!37 = !DILocation(line: 8, column: 5, scope: !38) +!38 = distinct !DILexicalBlock(scope: !30, file: !8, line: 7, column: 20) +!39 = !DILocation(line: 9, column: 3, scope: !38) +!40 = !DILocation(line: 10, column: 1, scope: !7) +!41 = distinct !DISubprogram(name: "operator()", linkageName: "_ZZ4testvENK3$_0clEv", scope: !42, file: !8, line: 5, type: !48, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: !52, retainedNodes: !53) +!42 = distinct !DICompositeType(tag: DW_TAG_class_type, scope: !7, file: !8, line: 5, size: 192, flags: DIFlagTypePassByValue, elements: !43) +!43 = !{!44, !46, !47} +!44 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !42, file: !8, line: 5, baseType: !45, size: 64) +!45 = !DIDerivedType(tag: DW_TAG_reference_type, baseType: !13, size: 64) +!46 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !42, file: !8, line: 5, baseType: !45, size: 64, offset: 64) +!47 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !42, file: !8, line: 5, baseType: !45, size: 64, offset: 128) +!48 = !DISubroutineType(types: !49) +!49 = !{null, !50} +!50 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !51, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!51 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !42) +!52 = !DISubprogram(name: "operator()", scope: !42, file: !8, line: 5, type: !48, scopeLine: 5, flags: DIFlagPublic | DIFlagPrototyped, spFlags: DISPFlagOptimized) +!53 = !{!54} +!54 = !DILocalVariable(name: "this", arg: 1, scope: !41, type: !55, flags: DIFlagArtificial | DIFlagObjectPointer) +!55 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !51, size: 64) +!56 = !DILocation(line: 0, scope: !41) +!57 = !DILocation(line: 5, column: 18, scope: !41) +!58 = !{!59, !28, i64 8} +!59 = !{!"_ZTSZ4testvE3$_0", !28, i64 0, !28, i64 8, !28, i64 16} +!60 = !DILocation(line: 5, column: 20, scope: !41) +!61 = !DILocation(line: 5, column: 28, scope: !41)