Index: lib/Transforms/IPO/DeadArgumentElimination.cpp =================================================================== --- lib/Transforms/IPO/DeadArgumentElimination.cpp +++ lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -240,8 +240,11 @@ I2->takeName(&*I); } - // Patch the pointer to LLVM function in debug info descriptor. - NF->setSubprogram(Fn.getSubprogram()); + // Clone metadatas from the old function, including debug info descriptor. + SmallVector, 1> MDs; + Fn.getAllMetadata(MDs); + for (auto MD : MDs) + NF->addMetadata(MD.first, *MD.second); // Fix up any BlockAddresses that refer to the function. Fn.replaceAllUsesWith(ConstantExpr::getBitCast(NF, Fn.getType())); @@ -859,9 +862,6 @@ F->getParent()->getFunctionList().insert(F->getIterator(), NF); NF->takeName(F); - // Patch the pointer to LLVM function in debug info descriptor. - NF->setSubprogram(F->getSubprogram()); - // Loop over all of the callers of the function, transforming the call sites // to pass in a smaller number of arguments into the new function. std::vector Args; @@ -1054,6 +1054,12 @@ BB.getInstList().erase(RI); } + // Clone metadatas from the old function, including debug info descriptor. + SmallVector, 1> MDs; + F->getAllMetadata(MDs); + for (auto MD : MDs) + NF->addMetadata(MD.first, *MD.second); + // Now that the old function is dead, delete it. F->eraseFromParent(); Index: test/Transforms/DeadArgElim/func_metadata.ll =================================================================== --- /dev/null +++ test/Transforms/DeadArgElim/func_metadata.ll @@ -0,0 +1,69 @@ +; RUN: opt -deadargelim -S < %s | FileCheck %s + +; Check if function level metadatas are properly cloned. + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@s = common dso_local local_unnamed_addr global i32 0, align 4 + +; Function Attrs: inlinehint norecurse nounwind uwtable +define dso_local i32 @main() local_unnamed_addr #0 !prof !29 { +entry: + tail call fastcc i64 @foo() + ret i32 0 +} + +; Function Attrs: inlinehint noinline norecurse nounwind uwtable +define internal fastcc i64 @foo() unnamed_addr #1 !prof !29 !PGOFuncName !30 { +; CHECK: define internal fastcc void @foo() unnamed_addr #1 !prof ![[ENTRYCOUNT:[0-9]+]] !PGOFuncName ![[PGOFUNCNAME:[0-9]+]] { +entry: + %0 = load i32, i32* @s, align 4, !tbaa !31 + %add = add nsw i32 %0, 8 + store i32 %add, i32* @s, align 4, !tbaa !31 + ret i64 0 +} + +attributes #0 = { inlinehint norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "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 = { inlinehint noinline norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "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" } + +!llvm.module.flags = !{!0, !1} +!llvm.ident = !{!28} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 1, !"ProfileSummary", !2} +!2 = !{!3, !4, !5, !6, !7, !8, !9, !10} +!3 = !{!"ProfileFormat", !"InstrProf"} +!4 = !{!"TotalCount", i64 2} +!5 = !{!"MaxCount", i64 1} +!6 = !{!"MaxInternalCount", i64 0} +!7 = !{!"MaxFunctionCount", i64 1} +!8 = !{!"NumCounts", i64 2} +!9 = !{!"NumFunctions", i64 2} +!10 = !{!"DetailedSummary", !11} +!11 = !{!12, !13, !14, !15, !16, !17, !17, !18, !18, !19, !20, !21, !22, !23, !24, !25, !26, !27} +!12 = !{i32 10000, i64 0, i32 0} +!13 = !{i32 100000, i64 0, i32 0} +!14 = !{i32 200000, i64 0, i32 0} +!15 = !{i32 300000, i64 0, i32 0} +!16 = !{i32 400000, i64 0, i32 0} +!17 = !{i32 500000, i64 1, i32 2} +!18 = !{i32 600000, i64 1, i32 2} +!19 = !{i32 700000, i64 1, i32 2} +!20 = !{i32 800000, i64 1, i32 2} +!21 = !{i32 900000, i64 1, i32 2} +!22 = !{i32 950000, i64 1, i32 2} +!23 = !{i32 990000, i64 1, i32 2} +!24 = !{i32 999000, i64 1, i32 2} +!25 = !{i32 999900, i64 1, i32 2} +!26 = !{i32 999990, i64 1, i32 2} +!27 = !{i32 999999, i64 1, i32 2} +!28 = !{!"clang version 7.0.0 (http://llvm.org/git/clang.git b611017adf557af5d9b193f11ad92e14da240791) (http://llvm.org/git/llvm.git 98824df7ea12c6c7beac8a414b80a03a918a14e8)"} +!29 = !{!"function_entry_count", i64 1} +; CHECK: ![[ENTRYCOUNT]] = !{!"function_entry_count", i64 1} +!30 = !{!"foo.c:foo"} +; CHECK: ![[PGOFUNCNAME]] = !{!"foo.c:foo"} +!31 = !{!32, !32, i64 0} +!32 = !{!"int", !33, i64 0} +!33 = !{!"omnipotent char", !34, i64 0} +!34 = !{!"Simple C/C++ TBAA"}