Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 777479f

Browse files
committed
Merging r259696:
------------------------------------------------------------------------ r259696 | kfischer | 2016-02-03 13:13:33 -0800 (Wed, 03 Feb 2016) | 12 lines [DWARFDebug] Fix another case of overlapping ranges Summary: In r257979, I added code to ensure that we wouldn't merge DebugLocEntries if the pieces they describe overlap. Unfortunately, I failed to cover the case, where there may have multiple active Expressions in the entry, in which case we need to make sure that no two values overlap before we can perform the merge. This fixed PR26148. Reviewers: aprantl Differential Revision: http://reviews.llvm.org/D16742 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_38@260121 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7ecd92d commit 777479f

File tree

2 files changed

+144
-13
lines changed

2 files changed

+144
-13
lines changed

lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -793,16 +793,27 @@ static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
793793
llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
794794
}
795795

796-
/// Determine whether two variable pieces overlap.
797-
static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
798-
if (!P1->isBitPiece() || !P2->isBitPiece())
799-
return true;
796+
// Determine the relative position of the pieces described by P1 and P2.
797+
// Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap,
798+
// 1 if P1 is entirely after P2.
799+
static int pieceCmp(const DIExpression *P1, const DIExpression *P2) {
800800
unsigned l1 = P1->getBitPieceOffset();
801801
unsigned l2 = P2->getBitPieceOffset();
802802
unsigned r1 = l1 + P1->getBitPieceSize();
803803
unsigned r2 = l2 + P2->getBitPieceSize();
804-
// True where [l1,r1[ and [r1,r2[ overlap.
805-
return (l1 < r2) && (l2 < r1);
804+
if (r1 <= l2)
805+
return -1;
806+
else if (r2 <= l1)
807+
return 1;
808+
else
809+
return 0;
810+
}
811+
812+
/// Determine whether two variable pieces overlap.
813+
static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
814+
if (!P1->isBitPiece() || !P2->isBitPiece())
815+
return true;
816+
return pieceCmp(P1, P2) == 0;
806817
}
807818

808819
/// \brief If this and Next are describing different pieces of the same
@@ -811,14 +822,32 @@ static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
811822
/// Return true if the merge was successful.
812823
bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
813824
if (Begin == Next.Begin) {
814-
auto *Expr = cast_or_null<DIExpression>(Values[0].Expression);
815-
auto *NextExpr = cast_or_null<DIExpression>(Next.Values[0].Expression);
816-
if (Expr->isBitPiece() && NextExpr->isBitPiece() &&
817-
!piecesOverlap(Expr, NextExpr)) {
818-
addValues(Next.Values);
819-
End = Next.End;
820-
return true;
825+
auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
826+
auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
827+
if (!FirstExpr->isBitPiece() || !FirstNextExpr->isBitPiece())
828+
return false;
829+
830+
// We can only merge entries if none of the pieces overlap any others.
831+
// In doing so, we can take advantage of the fact that both lists are
832+
// sorted.
833+
for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
834+
for (; j < Next.Values.size(); ++j) {
835+
int res = pieceCmp(cast<DIExpression>(Values[i].Expression),
836+
cast<DIExpression>(Next.Values[j].Expression));
837+
if (res == 0) // The two expressions overlap, we can't merge.
838+
return false;
839+
// Values[i] is entirely before Next.Values[j],
840+
// so go back to the next entry of Values.
841+
else if (res == -1)
842+
break;
843+
// Next.Values[j] is entirely before Values[i], so go on to the
844+
// next entry of Next.Values.
845+
}
821846
}
847+
848+
addValues(Next.Values);
849+
End = Next.End;
850+
return true;
822851
}
823852
return false;
824853
}

test/DebugInfo/X86/PR26148.ll

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
; RUN: llc -filetype=obj -o - < %s | llvm-dwarfdump - | FileCheck %s
2+
;
3+
; Created using clang -g -O3 from:
4+
; struct S0 {
5+
; short f0;
6+
; int f3;
7+
; } a;
8+
; void fn1(short p1) {
9+
; struct S0 b, c = {3};
10+
; b.f3 = p1;
11+
; a = b = c;
12+
; }
13+
;
14+
; int main() { return 0; }
15+
;
16+
; This is similar to the bug in test/DebugInfo/ARM/PR26163.ll, except that there is an
17+
; extra non-overlapping range first. Thus, we make sure that the backend actually looks
18+
; at all expressions when determining whether to merge ranges, not just the first one.
19+
; AS in 26163, we expect two ranges (as opposed to one), the first one being zero sized
20+
;
21+
;
22+
; CHECK: 0x00000000: Beginning address offset: 0x0000000000000004
23+
; CHECK: Ending address offset: 0x0000000000000004
24+
; CHECK: Location description: 10 03 55 93 04
25+
; CHECK: Beginning address offset: 0x0000000000000004
26+
; CHECK: Ending address offset: 0x0000000000000014
27+
; CHECK: Location description: 10 03 10 00
28+
29+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
30+
target triple = "x86_64-apple-macosx10.11.0"
31+
32+
%struct.S0 = type { i16, i32 }
33+
34+
@a = common global %struct.S0 zeroinitializer, align 4
35+
36+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
37+
declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
38+
39+
; The attributes are here to force the zero-sized range not to be at the start of
40+
; the function, which has special interpretation in DWARF. The fact that this happens
41+
; at all is probably an LLVM bug.
42+
attributes #0 = { "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" }
43+
define void @fn1(i16 signext %p1) #0 !dbg !4 {
44+
entry:
45+
tail call void @llvm.dbg.value(metadata i16 %p1, i64 0, metadata !9, metadata !26), !dbg !27
46+
tail call void @llvm.dbg.declare(metadata %struct.S0* undef, metadata !10, metadata !26), !dbg !28
47+
tail call void @llvm.dbg.declare(metadata %struct.S0* undef, metadata !16, metadata !26), !dbg !29
48+
tail call void @llvm.dbg.value(metadata i32 3, i64 0, metadata !16, metadata !30), !dbg !29
49+
tail call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !16, metadata !31), !dbg !29
50+
tail call void @llvm.dbg.value(metadata i16 %p1, i64 0, metadata !10, metadata !32), !dbg !28
51+
tail call void @llvm.dbg.value(metadata i32 3, i64 0, metadata !10, metadata !30), !dbg !28
52+
tail call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !10, metadata !31), !dbg !28
53+
store i32 3, i32* bitcast (%struct.S0* @a to i32*), align 4, !dbg !33
54+
store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @a, i64 0, i32 1), align 4, !dbg !33
55+
ret void, !dbg !34
56+
}
57+
58+
define i32 @main() !dbg !17 {
59+
entry:
60+
ret i32 0, !dbg !35
61+
}
62+
63+
!llvm.dbg.cu = !{!0}
64+
!llvm.module.flags = !{!22, !23, !24}
65+
!llvm.ident = !{!25}
66+
67+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 (https://github.com/llvm-mirror/clang 8f258397c5afd7a708bd95770c718e81d08fb11a) (https://github.com/llvm-mirror/llvm 18481855bdfa1b4a424f81be8525db002671348d)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3, globals: !20)
68+
!1 = !DIFile(filename: "small.c", directory: "/Users/kfischer/Projects/clangbug")
69+
!2 = !{}
70+
!3 = !{!4, !17}
71+
!4 = distinct !DISubprogram(name: "fn1", scope: !1, file: !1, line: 5, type: !5, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, variables: !8)
72+
!5 = !DISubroutineType(types: !6)
73+
!6 = !{null, !7}
74+
!7 = !DIBasicType(name: "short", size: 16, align: 16, encoding: DW_ATE_signed)
75+
!8 = !{!9, !10, !16}
76+
!9 = !DILocalVariable(name: "p1", arg: 1, scope: !4, file: !1, line: 5, type: !7)
77+
!10 = !DILocalVariable(name: "b", scope: !4, file: !1, line: 6, type: !11)
78+
!11 = !DICompositeType(tag: DW_TAG_structure_type, name: "S0", file: !1, line: 1, size: 64, align: 32, elements: !12)
79+
!12 = !{!13, !14}
80+
!13 = !DIDerivedType(tag: DW_TAG_member, name: "f0", scope: !11, file: !1, line: 2, baseType: !7, size: 16, align: 16)
81+
!14 = !DIDerivedType(tag: DW_TAG_member, name: "f3", scope: !11, file: !1, line: 3, baseType: !15, size: 32, align: 32, offset: 32)
82+
!15 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
83+
!16 = !DILocalVariable(name: "c", scope: !4, file: !1, line: 6, type: !11)
84+
!17 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 11, type: !18, isLocal: false, isDefinition: true, scopeLine: 11, isOptimized: true, variables: !2)
85+
!18 = !DISubroutineType(types: !19)
86+
!19 = !{!15}
87+
!20 = !{!21}
88+
!21 = !DIGlobalVariable(name: "a", scope: !0, file: !1, line: 4, type: !11, isLocal: false, isDefinition: true, variable: %struct.S0* @a)
89+
!22 = !{i32 2, !"Dwarf Version", i32 2}
90+
!23 = !{i32 2, !"Debug Info Version", i32 3}
91+
!24 = !{i32 1, !"PIC Level", i32 2}
92+
!25 = !{!"clang version 3.9.0 (https://github.com/llvm-mirror/clang 8f258397c5afd7a708bd95770c718e81d08fb11a) (https://github.com/llvm-mirror/llvm 18481855bdfa1b4a424f81be8525db002671348d)"}
93+
!26 = !DIExpression()
94+
!27 = !DILocation(line: 5, column: 16, scope: !4)
95+
!28 = !DILocation(line: 6, column: 13, scope: !4)
96+
!29 = !DILocation(line: 6, column: 16, scope: !4)
97+
!30 = !DIExpression(DW_OP_bit_piece, 0, 32)
98+
!31 = !DIExpression(DW_OP_bit_piece, 32, 32)
99+
!32 = !DIExpression(DW_OP_bit_piece, 32, 16)
100+
!33 = !DILocation(line: 8, column: 9, scope: !4)
101+
!34 = !DILocation(line: 9, column: 1, scope: !4)
102+
!35 = !DILocation(line: 11, column: 14, scope: !17)

0 commit comments

Comments
 (0)