Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp +++ lib/Analysis/ScalarEvolutionExpander.cpp @@ -25,6 +25,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/PatternMatch.h" +#include "llvm/Transforms/Utils/Local.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" @@ -1210,6 +1211,9 @@ // Save the original insertion point so we can restore it when we're done. SCEVInsertPointGuard Guard(Builder, this); + // Save the original PHI node so we can copy over its debug info. + PHINode *OldPN = L->getCanonicalInductionVariable(); + // Another AddRec may need to be recursively expanded below. For example, if // this AddRec is quadratic, the StepV may itself be an AddRec in this // loop. Remove this loop from the PostIncLoops set before expanding such @@ -1291,6 +1295,12 @@ // Remember this PHI, even in post-inc mode. InsertedValues.insert(PN); + // Attach any debug information to the new PHI. + SmallVector DbgValues; + llvm::findDbgValues(DbgValues, OldPN); + for (auto &DbgValue : DbgValues) + DbgValue->setOperand( + 0, MetadataAsValue::get(PN->getContext(), ValueAsMetadata::get(PN))); return PN; } Index: test/Transforms/IndVarSimplify/scev-phi-debug-info.ll =================================================================== --- /dev/null +++ test/Transforms/IndVarSimplify/scev-phi-debug-info.ll @@ -0,0 +1,90 @@ +; RUN: opt %s -indvars -o - | FileCheck %s +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.status = type { i32, i8* } + +@status = internal unnamed_addr global [32 x %struct.status] zeroinitializer, align 16, !dbg !0 + +define i32 @init() local_unnamed_addr !dbg !20 { +entry: + tail call void @llvm.dbg.value(metadata i32 0, metadata !24, metadata !DIExpression()), !dbg !25 + br label %for.cond, !dbg !26 + +for.cond: + %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + ; CHECK: %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ] + ; CHECK: call void @llvm.dbg.value(metadata i64 %indvars.iv, metadata !24, metadata !DIExpression()), !dbg !25 + tail call void @llvm.dbg.value(metadata i32 %i.0, metadata !24, metadata !DIExpression()), !dbg !25 + %cmp = icmp slt i32 %i.0, 32, !dbg !28 + br i1 %cmp, label %for.body, label %for.end, !dbg !30 + +for.body: + %idxprom = sext i32 %i.0 to i64, !dbg !31 + %value = getelementptr inbounds [32 x %struct.status], [32 x %struct.status]* @status, i64 0, i64 %idxprom, i32 0, !dbg !33 + store i32 42, i32* %value, align 16, !dbg !34, !tbaa !35 + tail call void @use(i32 %i.0), !dbg !41 + %inc = add nsw i32 %i.0, 1, !dbg !42 + tail call void @llvm.dbg.value(metadata i32 %inc, metadata !24, metadata !DIExpression()), !dbg !25 + br label %for.cond, !dbg !43, !llvm.loop !44 + +for.end: + ret i32 0, !dbg !46 +} + +declare void @use(i32) + +declare void @llvm.dbg.value(metadata, metadata, metadata) #2 +attributes #2 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!16, !17, !18} +!llvm.ident = !{!19} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "status", scope: !2, file: !3, line: 5, type: !6, isLocal: true, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 6.0.0 (trunk 316001) (llvm/trunk 316171)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) +!3 = !DIFile(filename: "x.c", directory: "/home/davide/work/llvm/build-release/bin") +!4 = !{} +!5 = !{!0} +!6 = !DICompositeType(tag: DW_TAG_array_type, baseType: !7, size: 4096, elements: !14) +!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "status", file: !3, line: 2, size: 128, elements: !8) +!8 = !{!9, !11} +!9 = !DIDerivedType(tag: DW_TAG_member, name: "value", scope: !7, file: !3, line: 3, baseType: !10, size: 32) +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DIDerivedType(tag: DW_TAG_member, name: "p", scope: !7, file: !3, line: 4, baseType: !12, size: 64, offset: 64) +!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64) +!13 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char) +!14 = !{!15} +!15 = !DISubrange(count: 32) +!16 = !{i32 2, !"Dwarf Version", i32 4} +!17 = !{i32 2, !"Debug Info Version", i32 3} +!18 = !{i32 1, !"wchar_size", i32 4} +!19 = !{!"clang version 6.0.0 (trunk 316001) (llvm/trunk 316171)"} +!20 = distinct !DISubprogram(name: "init", scope: !3, file: !3, line: 6, type: !21, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: true, unit: !2, variables: !23) +!21 = !DISubroutineType(types: !22) +!22 = !{!10} +!23 = !{!24} +!24 = !DILocalVariable(name: "i", scope: !20, file: !3, line: 8, type: !10) +!25 = !DILocation(line: 8, column: 7, scope: !20) +!26 = !DILocation(line: 9, column: 8, scope: !27) +!27 = distinct !DILexicalBlock(scope: !20, file: !3, line: 9, column: 3) +!28 = !DILocation(line: 9, column: 17, scope: !29) +!29 = distinct !DILexicalBlock(scope: !27, file: !3, line: 9, column: 3) +!30 = !DILocation(line: 9, column: 3, scope: !27) +!31 = !DILocation(line: 10, column: 5, scope: !32) +!32 = distinct !DILexicalBlock(scope: !29, file: !3, line: 9, column: 28) +!33 = !DILocation(line: 10, column: 15, scope: !32) +!34 = !DILocation(line: 10, column: 21, scope: !32) +!35 = !{!36, !37, i64 0} +!36 = !{!"status", !37, i64 0, !40, i64 8} +!37 = !{!"int", !38, i64 0} +!38 = !{!"omnipotent char", !39, i64 0} +!39 = !{!"Simple C/C++ TBAA"} +!40 = !{!"any pointer", !38, i64 0} +!41 = !DILocation(line: 11, column: 5, scope: !32) +!42 = !DILocation(line: 9, column: 24, scope: !29) +!43 = !DILocation(line: 9, column: 3, scope: !29) +!44 = distinct !{!44, !30, !45} +!45 = !DILocation(line: 12, column: 3, scope: !27) +!46 = !DILocation(line: 13, column: 3, scope: !20)