Skip to content

Cherry-pick salvagedebuginfo improvements #3317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,15 +2527,6 @@ void coro::salvageDebugInfo(
Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
} else if (auto *StInst = dyn_cast<StoreInst>(Inst)) {
Storage = StInst->getOperand(0);
} else if (auto *I2PInst = dyn_cast<llvm::IntToPtrInst>(Inst)) {
Storage = I2PInst->getOperand(0);
} else if (auto *P2IInst = dyn_cast<llvm::PtrToIntInst>(Inst)) {
Storage = P2IInst->getOperand(0);
} else if (auto *IInst = dyn_cast<llvm::IntrinsicInst>(Inst)) {
if (IInst->getIntrinsicID() == Intrinsic::ptrauth_auth)
Storage = IInst->getArgOperand(0);
else
break;
} else if (auto *Phi = dyn_cast<PHINode>(Inst)) {
if (Phi->getNumIncomingValues() != 1)
return;
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Scalar/ADCE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() {
// that have no side effects and do not influence the control flow or return
// value of the function, and may therefore be deleted safely.
// NOTE: We reuse the Worklist vector here for memory efficiency.
for (Instruction &I : instructions(F)) {
for (Instruction &I : llvm::reverse(instructions(F))) {
// Check if the instruction is alive.
if (isLive(&I))
continue;
Expand All @@ -554,9 +554,11 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() {
// Prepare to delete.
Worklist.push_back(&I);
salvageDebugInfo(I);
I.dropAllReferences();
}

for (Instruction *&I : Worklist)
I->dropAllReferences();

for (Instruction *&I : Worklist) {
++NumRemoved;
I->eraseFromParent();
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/Scalar/BDCE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
(I.getType()->isIntOrIntVectorTy() &&
DB.getDemandedBits(&I).isNullValue() &&
wouldInstructionBeTriviallyDead(&I))) {
salvageDebugInfo(I);
Worklist.push_back(&I);
I.dropAllReferences();
Changed = true;
continue;
}
Expand Down Expand Up @@ -155,6 +153,11 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
}
}

for (Instruction *&I : llvm::reverse(Worklist)) {
salvageDebugInfo(*I);
I->dropAllReferences();
}

for (Instruction *&I : Worklist) {
++NumRemoved;
I->eraseFromParent();
Expand Down
20 changes: 16 additions & 4 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1908,12 +1908,19 @@ Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
}

Type *Type = CI->getType();
if (Type->isPointerTy())
Type = DL.getIntPtrType(Type);
// Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
if (Type->isVectorTy() ||
!(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I)))
!(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I) ||
isa<IntToPtrInst>(&I) || isa<PtrToIntInst>(&I)))
return nullptr;

unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits();
llvm::Type *FromType = FromValue->getType();
if (FromType->isPointerTy())
FromType = DL.getIntPtrType(FromType);

unsigned FromTypeBitSize = FromType->getScalarSizeInBits();
unsigned ToTypeBitSize = Type->getScalarSizeInBits();

auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
Expand All @@ -1924,12 +1931,17 @@ Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,

if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
else if (auto *BI = dyn_cast<BinaryOperator>(&I)) {
if (auto *BI = dyn_cast<BinaryOperator>(&I))
return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
}

// *Not* to do: we should not attempt to salvage load instructions,
// because the validity and lifetime of a dbg.value containing
// DW_OP_deref becomes difficult to analyze. See PR40628 for examples.

if (auto *IInst = dyn_cast<llvm::IntrinsicInst>(&I))
if (IInst->getIntrinsicID() == Intrinsic::ptrauth_auth)
return IInst->getArgOperand(0);

return nullptr;
}

Expand Down
27 changes: 27 additions & 0 deletions llvm/test/DebugInfo/AArch64/salvage-ptrauth.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: opt -mtriple arm64e-apple-darwin -adce %s -S -o - | FileCheck %s
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"

declare i64 @llvm.ptrauth.auth.i64(i64, i32, i64)

define void @f(i64 %arg, i64 %arg1) !dbg !8 {
entry:
%tmp = call i64 @llvm.ptrauth.auth.i64(i64 %arg, i32 0, i64 %arg1)
; CHECK: call void @llvm.dbg.value(metadata i64 %arg,
; CHECK-SAME: !DIExpression())
call void @llvm.dbg.value(metadata i64 %tmp, metadata !11, metadata !DIExpression()), !dbg !13
ret void, !dbg !13
}
declare void @llvm.dbg.value(metadata, metadata, metadata)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
!1 = !DIFile(filename: "salavage.c", directory: "/")
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0)
!9 = !DISubroutineType(types: !10)
!10 = !{null}
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!13 = !DILocation(line: 1, column: 1, scope: !8)
30 changes: 30 additions & 0 deletions llvm/test/Transforms/Util/salvage-debuginfo.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; RUN: opt -adce %s -S -o - | FileCheck %s
; RUN: opt -bdce %s -S -o - | FileCheck %s
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx"
define void @f(i32) !dbg !8 {
entry:
%p_x = inttoptr i32 %0 to i8*
%i_x = ptrtoint i8* %p_x to i32
; CHECK: call void @llvm.dbg.value(metadata i32 %0,
; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned,
; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned,
; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned,
; CHECK-SAME: DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value))
call void @llvm.dbg.value(metadata i32 %i_x, metadata !11, metadata !DIExpression()), !dbg !13
ret void, !dbg !13
}
declare void @llvm.dbg.value(metadata, metadata, metadata)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
!1 = !DIFile(filename: "salavage.c", directory: "/")
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0)
!9 = !DISubroutineType(types: !10)
!10 = !{null}
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!13 = !DILocation(line: 1, column: 1, scope: !8)