Index: lib/Transforms/IPO/Inliner.cpp =================================================================== --- lib/Transforms/IPO/Inliner.cpp +++ lib/Transforms/IPO/Inliner.cpp @@ -228,6 +228,14 @@ DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: " << *AvailableAlloca << '\n'); + // Move affected dbg.declare calls immediately after the new alloca to + // avoid the situation when a dbg.declare preceeds its alloca. + if (auto *L = LocalAsMetadata::getIfExists(AI)) + if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L)) + for (User *U : MDV->users()) + if (DbgDeclareInst *DDI = dyn_cast(U)) + DDI->moveBefore(AvailableAlloca->getNextNode()); + AI->replaceAllUsesWith(AvailableAlloca); if (Align1 != Align2) { Index: lib/Transforms/Utils/Local.cpp =================================================================== --- lib/Transforms/Utils/Local.cpp +++ lib/Transforms/Utils/Local.cpp @@ -1109,10 +1109,10 @@ DIExpr = Builder.createExpression(NewDIExpr); } - // Insert llvm.dbg.declare in the same basic block as the original alloca, - // and remove old llvm.dbg.declare. - BasicBlock *BB = AI->getParent(); - Builder.insertDeclare(NewAllocaAddress, DIVar, DIExpr, Loc, BB); + // Insert llvm.dbg.declare immediately after the original alloca, and remove + // old llvm.dbg.declare. + Builder.insertDeclare(NewAllocaAddress, DIVar, DIExpr, Loc, + AI->getNextNode()); DDI->eraseFromParent(); return true; } Index: test/Transforms/Inline/alloca-dbgdeclare-merge.ll =================================================================== --- /dev/null +++ test/Transforms/Inline/alloca-dbgdeclare-merge.ll @@ -0,0 +1,102 @@ +; Test that alloca merging in the inliner places dbg.declare calls immediately +; after the merged alloca. Not at the end of the entry BB, and definitely not +; before the alloca. +; +; clang -g -S -emit-llvm -Xclang -disable-llvm-optzns +; +;__attribute__((always_inline)) void f() { +; char aaa[100]; +; aaa[10] = 1; +;} +; +;__attribute__((always_inline)) void g() { +; char bbb[100]; +; bbb[20] = 1; +;} +; +;void h() { +; f(); +; g(); +;} +; +; RUN: opt -always-inline -S < %s | FileCheck %s +; +; CHECK: define void @h() +; CHECK-NEXT: entry: +; CHECK-NEXT: %[[AI:.*]] = alloca [100 x i8] +; CHECK-NEXT: call void @llvm.dbg.declare(metadata [100 x i8]* %[[AI]], +; CHECK-NEXT: call void @llvm.dbg.declare(metadata [100 x i8]* %[[AI]], + + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: alwaysinline nounwind uwtable +define void @f() #0 { +entry: + %aaa = alloca [100 x i8], align 16 + call void @llvm.dbg.declare(metadata [100 x i8]* %aaa, metadata !12, metadata !17), !dbg !18 + %arrayidx = getelementptr inbounds [100 x i8], [100 x i8]* %aaa, i64 0, i64 10, !dbg !19 + store i8 1, i8* %arrayidx, align 2, !dbg !20 + ret void, !dbg !21 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: alwaysinline nounwind uwtable +define void @g() #0 { +entry: + %bbb = alloca [100 x i8], align 16 + call void @llvm.dbg.declare(metadata [100 x i8]* %bbb, metadata !22, metadata !17), !dbg !23 + %arrayidx = getelementptr inbounds [100 x i8], [100 x i8]* %bbb, i64 0, i64 20, !dbg !24 + store i8 1, i8* %arrayidx, align 4, !dbg !25 + ret void, !dbg !26 +} + +; Function Attrs: nounwind uwtable +define void @h() #2 { +entry: + call void @f(), !dbg !27 + call void @g(), !dbg !28 + ret void, !dbg !29 +} + +attributes #0 = { alwaysinline nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } +attributes #2 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!9, !10} +!llvm.ident = !{!11} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "../1.c", directory: "/code/llvm-git/build") +!2 = !{} +!3 = !{!4, !7, !8} +!4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, function: void ()* @f, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{null} +!7 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 6, type: !5, isLocal: false, isDefinition: true, scopeLine: 6, isOptimized: false, function: void ()* @g, variables: !2) +!8 = distinct !DISubprogram(name: "h", scope: !1, file: !1, line: 11, type: !5, isLocal: false, isDefinition: true, scopeLine: 11, isOptimized: false, function: void ()* @h, variables: !2) +!9 = !{i32 2, !"Dwarf Version", i32 4} +!10 = !{i32 2, !"Debug Info Version", i32 3} +!11 = !{!"clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)"} +!12 = !DILocalVariable(name: "aaa", scope: !4, file: !1, line: 2, type: !13) +!13 = !DICompositeType(tag: DW_TAG_array_type, baseType: !14, size: 800, align: 8, elements: !15) +!14 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!15 = !{!16} +!16 = !DISubrange(count: 100) +!17 = !DIExpression() +!18 = !DILocation(line: 2, column: 8, scope: !4) +!19 = !DILocation(line: 3, column: 3, scope: !4) +!20 = !DILocation(line: 3, column: 11, scope: !4) +!21 = !DILocation(line: 4, column: 1, scope: !4) +!22 = !DILocalVariable(name: "bbb", scope: !7, file: !1, line: 7, type: !13) +!23 = !DILocation(line: 7, column: 8, scope: !7) +!24 = !DILocation(line: 8, column: 3, scope: !7) +!25 = !DILocation(line: 8, column: 11, scope: !7) +!26 = !DILocation(line: 9, column: 1, scope: !7) +!27 = !DILocation(line: 12, column: 3, scope: !8) +!28 = !DILocation(line: 13, column: 3, scope: !8) +!29 = !DILocation(line: 14, column: 1, scope: !8) Index: test/Transforms/Inline/inline_dbg_declare.ll =================================================================== --- test/Transforms/Inline/inline_dbg_declare.ll +++ test/Transforms/Inline/inline_dbg_declare.ll @@ -42,6 +42,7 @@ entry: ; CHECK: [[x_addr_i:%[a-zA-Z0-9.]+]] = alloca float, align 4 +; CHECK-NEXT: void @llvm.dbg.declare(metadata float* [[x_addr_i]], metadata [[m23:![0-9]+]], metadata !{{[0-9]+}}), !dbg [[m24:![0-9]+]] %dst.addr = alloca float*, align 4 store float* %dst, float** %dst.addr, align 4 @@ -52,7 +53,6 @@ %call = call float @foo(float %1), !dbg !22 ; CHECK-NOT: call float @foo -; CHECK: void @llvm.dbg.declare(metadata float* [[x_addr_i]], metadata [[m23:![0-9]+]], metadata !{{[0-9]+}}), !dbg [[m24:![0-9]+]] %2 = load float*, float** %dst.addr, align 4, !dbg !22 %arrayidx1 = getelementptr inbounds float, float* %2, i32 0, !dbg !22 @@ -95,5 +95,5 @@ ; CHECK: [[FOO:![0-9]+]] = distinct !DISubprogram(name: "foo", ; CHECK: [[BAR:![0-9]+]] = distinct !DISubprogram(name: "bar", ; CHECK: [[m23]] = !DILocalVariable(name: "x", arg: 1, scope: [[FOO]] -; CHECK: [[CALL_SITE:![0-9]+]] = distinct !DILocation(line: 8, column: 14, scope: [[BAR]]) -; CHECK: [[m24]] = !DILocation(line: 1, column: 17, scope: [[FOO]], inlinedAt: [[CALL_SITE]]) +; CHECK: [[m24]] = !DILocation(line: 1, column: 17, scope: [[FOO]], inlinedAt: [[CALL_SITE:![0-9]+]]) +; CHECK: [[CALL_SITE]] = distinct !DILocation(line: 8, column: 14, scope: [[BAR]])