Index: llvm/lib/Transforms/Scalar/SROA.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SROA.cpp +++ llvm/lib/Transforms/Scalar/SROA.cpp @@ -4241,6 +4241,35 @@ return NewAI; } +/// Finds intrinsics declaring local variables as living in the memory +/// that 'V' points to and intrinsics specifing usages of values which +/// living in the memory that 'V' points to. Put only one relevant +/// intrinsic for each variable. Result may include a mix of dbg.declare, +/// dbg.addr and dbg.value intrinsics. + +void findDeclarations(SmallVectorImpl &Declarations, + Value *V) { + // This function is hot. Check whether the value has any metadata to avoid a + // DenseMap lookup. + if (!V->isUsedByMetadata()) + return; + DenseSet VarNames; + + if (auto *L = LocalAsMetadata::getIfExists(V)) + if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L)) + for (User *U : MDV->users()) + if (DbgVariableIntrinsic *DII = dyn_cast(U)) { + + StringRef Name = DII->getVariable()->getName(); + + if (VarNames.count(Name)) + continue; + + VarNames.insert(Name); + Declarations.push_back(DII); + } +} + /// Walks the slices of an alloca and form partitions based on them, /// rewriting each of their uses. bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { @@ -4340,17 +4369,28 @@ // Migrate debug information from the old alloca to the new alloca(s) // and the individual partitions. - TinyPtrVector DbgDeclares = FindDbgAddrUses(&AI); - if (!DbgDeclares.empty()) { - auto *Var = DbgDeclares.front()->getVariable(); - auto *Expr = DbgDeclares.front()->getExpression(); + + // At this stage declarations could already be promoted into the dbg.value. + // Thus we need to transfer debug information for dbg.declare, dbg.addr and + // for dbg.value also. + SmallVector Declarations; + findDeclarations(Declarations, &AI); + + for (auto Decl : Declarations) { + + auto *Var = Decl->getVariable(); + auto *Expr = Decl->getExpression(); + auto VarSize = Var->getSizeInBits(); + DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false); uint64_t AllocaSize = DL.getTypeSizeInBits(AI.getAllocatedType()); for (auto Fragment : Fragments) { + // Create a fragment expression describing the new partition or reuse AI's // expression if there is only one partition. auto *FragmentExpr = Expr; + if (Fragment.Size < AllocaSize || Expr->isFragment()) { // If this alloca is already a scalar replacement of a larger aggregate, // Fragment.Offset describes the offset inside the scalar. @@ -4395,10 +4435,11 @@ for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(Fragment.Alloca)) OldDII->eraseFromParent(); - DIB.insertDeclare(Fragment.Alloca, Var, FragmentExpr, - DbgDeclares.front()->getDebugLoc(), &AI); + DIB.insertDeclare(Fragment.Alloca, Var, FragmentExpr, Decl->getDebugLoc(), + &AI); } } + return Changed; } @@ -4502,12 +4543,16 @@ Instruction *I = DeadInsts.pop_back_val(); LLVM_DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n"); - // If the instruction is an alloca, find the possible dbg.declare connected - // to it, and remove it too. We must do this before calling RAUW or we will - // not be able to find it. + // If the instruction is an alloca, find the possible dbg.declare and + // dbg.value connected to it, and remove it too. We must do this before + // calling RAUW or we will not be able to find it. if (AllocaInst *AI = dyn_cast(I)) { DeletedAllocas.insert(AI); - for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(AI)) + + SmallVector DbgUsers; + findDbgUsers(DbgUsers, AI); + + for (DbgVariableIntrinsic *OldDII : DbgUsers) OldDII->eraseFromParent(); } Index: llvm/test/DebugInfo/X86/sroa-after-inlining.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/sroa-after-inlining.ll @@ -0,0 +1,149 @@ +; RUN: opt %s -sroa -instcombine -inline -instcombine -sroa -verify -S -o - | FileCheck %s +; +; This test checks that SROA pass processes debug info correcly if applied twice. +; Specifically, after SROA works first time, instcombine converts dbg.declare +; intrinsics into dbg.value. Inlining creates new opportunities for SROA, +; so it is called again. This time it does not handle correctly previously +; inserted dbg.value intrinsics. +; +; struct S1 { +; int p1; +; +; bool IsNull ( ) { +; return p1 == 0; +; } +; }; +; +; S1 foo ( void ); +; +; int bar ( ) { +; +; S1 result = foo(); +; +; if ( result.IsNull() ) +; return 0; +; +; return result.p1 + 1; +; } +; +; + +; CHECK: _Z3barv +; CHECK: %[[NAME:.*]] = call i32 @_Z3foov +; CHECK: llvm.dbg.value(metadata i32 %[[NAME]] +; CHECK: ret + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.S1 = type { i32 } + +$_ZN2S16IsNullEv = comdat any + +; Function Attrs: uwtable +define dso_local i32 @_Z3barv() #0 !dbg !7 { +entry: + %retval = alloca i32, align 4 + %result = alloca %struct.S1, align 4 + %cleanup.dest.slot = alloca i32, align 4 + %0 = bitcast %struct.S1* %result to i8*, !dbg !21 + call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #2, !dbg !21 + call void @llvm.dbg.declare(metadata %struct.S1* %result, metadata !12, metadata !DIExpression()), !dbg !22 + %call = call i32 @_Z3foov(), !dbg !23 + %coerce.dive = getelementptr inbounds %struct.S1, %struct.S1* %result, i32 0, i32 0, !dbg !23 + store i32 %call, i32* %coerce.dive, align 4, !dbg !23 + %call1 = call zeroext i1 @_ZN2S16IsNullEv(%struct.S1* %result), !dbg !24 + br i1 %call1, label %if.then, label %if.end, !dbg !26 + +if.then: ; preds = %entry + store i32 0, i32* %retval, align 4, !dbg !27 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup, !dbg !27 + +if.end: ; preds = %entry + %p1 = getelementptr inbounds %struct.S1, %struct.S1* %result, i32 0, i32 0, !dbg !28 + %1 = load i32, i32* %p1, align 4, !dbg !28 + %add = add nsw i32 %1, 1, !dbg !34 + store i32 %add, i32* %retval, align 4, !dbg !35 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup, !dbg !35 + +cleanup: ; preds = %if.end, %if.then + %2 = bitcast %struct.S1* %result to i8*, !dbg !36 + call void @llvm.lifetime.end.p0i8(i64 4, i8* %2) #2, !dbg !36 + %3 = load i32, i32* %retval, align 4, !dbg !36 + ret i32 %3, !dbg !36 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1 + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #2 + +declare dso_local i32 @_Z3foov() #2 + +; Function Attrs: nounwind uwtable +define linkonce_odr dso_local zeroext i1 @_ZN2S16IsNullEv(%struct.S1* %this) #2 comdat align 2 !dbg !37 { +entry: + %this.addr = alloca %struct.S1*, align 8 + store %struct.S1* %this, %struct.S1** %this.addr, align 8 + call void @llvm.dbg.declare(metadata %struct.S1** %this.addr, metadata !39, metadata !DIExpression()), !dbg !43 + %this1 = load %struct.S1*, %struct.S1** %this.addr, align 8 + %p1 = getelementptr inbounds %struct.S1, %struct.S1* %this1, i32 0, i32 0, !dbg !44 + %0 = load i32, i32* %p1, align 4, !dbg !44 + %cmp = icmp eq i32 %0, 0, !dbg !45 + ret i1 %cmp, !dbg !46 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1 + +attributes #0 = { nounwind ssp uwtable } +attributes #1 = { nounwind readnone } +attributes #2 = { 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", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "sroa-after-inlining.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"} +!7 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !1, file: !1, line: 11, type: !8, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) +!8 = !DISubroutineType(types: !9) +!9 = !{!10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !{!12} +!12 = !DILocalVariable(name: "result", scope: !7, file: !1, line: 13, type: !13) +!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S1", file: !1, line: 1, size: 32, flags: DIFlagTypePassByValue, elements: !14, identifier: "_ZTS2S1") +!14 = !{!15, !16} +!15 = !DIDerivedType(tag: DW_TAG_member, name: "p1", scope: !13, file: !1, line: 2, baseType: !10, size: 32) +!16 = !DISubprogram(name: "IsNull", linkageName: "_ZN2S16IsNullEv", scope: !13, file: !1, line: 4, type: !17, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) +!17 = !DISubroutineType(types: !18) +!18 = !{!19, !20} +!19 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean) +!20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!21 = !DILocation(line: 13, column: 5, scope: !7) +!22 = !DILocation(line: 13, column: 8, scope: !7) +!23 = !DILocation(line: 13, column: 17, scope: !7) +!24 = !DILocation(line: 15, column: 17, scope: !25) +!25 = distinct !DILexicalBlock(scope: !7, file: !1, line: 15, column: 10) +!26 = !DILocation(line: 15, column: 10, scope: !7) +!27 = !DILocation(line: 16, column: 9, scope: !25) +!28 = !DILocation(line: 18, column: 19, scope: !7) +!34 = !DILocation(line: 18, column: 22, scope: !7) +!35 = !DILocation(line: 18, column: 5, scope: !7) +!36 = !DILocation(line: 19, column: 1, scope: !7) +!37 = distinct !DISubprogram(name: "IsNull", linkageName: "_ZN2S16IsNullEv", scope: !13, file: !1, line: 4, type: !17, scopeLine: 4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, declaration: !16, retainedNodes: !38) +!38 = !{!39} +!39 = !DILocalVariable(name: "this", arg: 1, scope: !37, type: !40, flags: DIFlagArtificial | DIFlagObjectPointer) +!40 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64) +!43 = !DILocation(line: 0, scope: !37) +!44 = !DILocation(line: 5, column: 16, scope: !37) +!45 = !DILocation(line: 5, column: 19, scope: !37) +!46 = !DILocation(line: 5, column: 9, scope: !37)