Skip to content

Commit 210449f

Browse files
Merge pull request #3317 from adrian-prantl/80227769
Cherry-pick salvagedebuginfo improvements
2 parents e247502 + 0222dc0 commit 210449f

File tree

6 files changed

+82
-17
lines changed

6 files changed

+82
-17
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,15 +2527,6 @@ void coro::salvageDebugInfo(
25272527
Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
25282528
} else if (auto *StInst = dyn_cast<StoreInst>(Inst)) {
25292529
Storage = StInst->getOperand(0);
2530-
} else if (auto *I2PInst = dyn_cast<llvm::IntToPtrInst>(Inst)) {
2531-
Storage = I2PInst->getOperand(0);
2532-
} else if (auto *P2IInst = dyn_cast<llvm::PtrToIntInst>(Inst)) {
2533-
Storage = P2IInst->getOperand(0);
2534-
} else if (auto *IInst = dyn_cast<llvm::IntrinsicInst>(Inst)) {
2535-
if (IInst->getIntrinsicID() == Intrinsic::ptrauth_auth)
2536-
Storage = IInst->getArgOperand(0);
2537-
else
2538-
break;
25392530
} else if (auto *Phi = dyn_cast<PHINode>(Inst)) {
25402531
if (Phi->getNumIncomingValues() != 1)
25412532
return;

llvm/lib/Transforms/Scalar/ADCE.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() {
538538
// that have no side effects and do not influence the control flow or return
539539
// value of the function, and may therefore be deleted safely.
540540
// NOTE: We reuse the Worklist vector here for memory efficiency.
541-
for (Instruction &I : instructions(F)) {
541+
for (Instruction &I : llvm::reverse(instructions(F))) {
542542
// Check if the instruction is alive.
543543
if (isLive(&I))
544544
continue;
@@ -554,9 +554,11 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() {
554554
// Prepare to delete.
555555
Worklist.push_back(&I);
556556
salvageDebugInfo(I);
557-
I.dropAllReferences();
558557
}
559558

559+
for (Instruction *&I : Worklist)
560+
I->dropAllReferences();
561+
560562
for (Instruction *&I : Worklist) {
561563
++NumRemoved;
562564
I->eraseFromParent();

llvm/lib/Transforms/Scalar/BDCE.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
106106
(I.getType()->isIntOrIntVectorTy() &&
107107
DB.getDemandedBits(&I).isNullValue() &&
108108
wouldInstructionBeTriviallyDead(&I))) {
109-
salvageDebugInfo(I);
110109
Worklist.push_back(&I);
111-
I.dropAllReferences();
112110
Changed = true;
113111
continue;
114112
}
@@ -155,6 +153,11 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
155153
}
156154
}
157155

156+
for (Instruction *&I : llvm::reverse(Worklist)) {
157+
salvageDebugInfo(*I);
158+
I->dropAllReferences();
159+
}
160+
158161
for (Instruction *&I : Worklist) {
159162
++NumRemoved;
160163
I->eraseFromParent();

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,12 +1908,19 @@ Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
19081908
}
19091909

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

1916-
unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits();
1919+
llvm::Type *FromType = FromValue->getType();
1920+
if (FromType->isPointerTy())
1921+
FromType = DL.getIntPtrType(FromType);
1922+
1923+
unsigned FromTypeBitSize = FromType->getScalarSizeInBits();
19171924
unsigned ToTypeBitSize = Type->getScalarSizeInBits();
19181925

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

19251932
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
19261933
return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
1927-
else if (auto *BI = dyn_cast<BinaryOperator>(&I)) {
1934+
if (auto *BI = dyn_cast<BinaryOperator>(&I))
19281935
return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
1929-
}
1936+
19301937
// *Not* to do: we should not attempt to salvage load instructions,
19311938
// because the validity and lifetime of a dbg.value containing
19321939
// DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
1940+
1941+
if (auto *IInst = dyn_cast<llvm::IntrinsicInst>(&I))
1942+
if (IInst->getIntrinsicID() == Intrinsic::ptrauth_auth)
1943+
return IInst->getArgOperand(0);
1944+
19331945
return nullptr;
19341946
}
19351947

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: opt -mtriple arm64e-apple-darwin -adce %s -S -o - | FileCheck %s
2+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
3+
4+
declare i64 @llvm.ptrauth.auth.i64(i64, i32, i64)
5+
6+
define void @f(i64 %arg, i64 %arg1) !dbg !8 {
7+
entry:
8+
%tmp = call i64 @llvm.ptrauth.auth.i64(i64 %arg, i32 0, i64 %arg1)
9+
; CHECK: call void @llvm.dbg.value(metadata i64 %arg,
10+
; CHECK-SAME: !DIExpression())
11+
call void @llvm.dbg.value(metadata i64 %tmp, metadata !11, metadata !DIExpression()), !dbg !13
12+
ret void, !dbg !13
13+
}
14+
declare void @llvm.dbg.value(metadata, metadata, metadata)
15+
16+
!llvm.dbg.cu = !{!0}
17+
!llvm.module.flags = !{!3, !4}
18+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
19+
!1 = !DIFile(filename: "salavage.c", directory: "/")
20+
!3 = !{i32 2, !"Dwarf Version", i32 4}
21+
!4 = !{i32 2, !"Debug Info Version", i32 3}
22+
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0)
23+
!9 = !DISubroutineType(types: !10)
24+
!10 = !{null}
25+
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
26+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
27+
!13 = !DILocation(line: 1, column: 1, scope: !8)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: opt -adce %s -S -o - | FileCheck %s
2+
; RUN: opt -bdce %s -S -o - | FileCheck %s
3+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-apple-macosx"
5+
define void @f(i32) !dbg !8 {
6+
entry:
7+
%p_x = inttoptr i32 %0 to i8*
8+
%i_x = ptrtoint i8* %p_x to i32
9+
; CHECK: call void @llvm.dbg.value(metadata i32 %0,
10+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned,
11+
; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned,
12+
; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned,
13+
; CHECK-SAME: DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value))
14+
call void @llvm.dbg.value(metadata i32 %i_x, metadata !11, metadata !DIExpression()), !dbg !13
15+
ret void, !dbg !13
16+
}
17+
declare void @llvm.dbg.value(metadata, metadata, metadata)
18+
19+
!llvm.dbg.cu = !{!0}
20+
!llvm.module.flags = !{!3, !4}
21+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
22+
!1 = !DIFile(filename: "salavage.c", directory: "/")
23+
!3 = !{i32 2, !"Dwarf Version", i32 4}
24+
!4 = !{i32 2, !"Debug Info Version", i32 3}
25+
!8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0)
26+
!9 = !DISubroutineType(types: !10)
27+
!10 = !{null}
28+
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
29+
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
30+
!13 = !DILocation(line: 1, column: 1, scope: !8)

0 commit comments

Comments
 (0)