OSDN Git Service

[GlobalOpt] Include padding in debug fragments
authorMikael Holmen <mikael.holmen@ericsson.com>
Fri, 2 Feb 2018 10:34:13 +0000 (10:34 +0000)
committerMikael Holmen <mikael.holmen@ericsson.com>
Fri, 2 Feb 2018 10:34:13 +0000 (10:34 +0000)
Summary:
When creating the debug fragments for a SRA'd variable, use the types'
allocation sizes. This fixes issues where the pass would emit too small
fragments, placed at the wrong offset, for padded types.

An example of this is long double on x86. The type is represented using
x86_fp80, which is 10 bytes, but the value is aligned to 12/16 bytes.
The padding is included in the type's DW_AT_byte_size attribute;
therefore, the fragments should also include that. Newer GCC releases
(I tested 7.2.0) emit 12/16-byte pieces for long double. Earlier
releases, e.g. GCC 5.5.0, behaved as LLVM did, i.e. by emitting a
10-byte piece, followed by an empty 2/6-byte piece for the padding.

Failing to cover all `DW_AT_byte_size' bytes of a value with non-empty
pieces results in the value being printed as <optimized out> by GDB.

Patch by: David Stenberg

Reviewers: aprantl, JDevlieghere

Reviewed By: aprantl, JDevlieghere

Subscribers: llvm-commits

Tags: #debug-info

Differential Revision: https://reviews.llvm.org/D42807

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324066 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp
test/DebugInfo/X86/global-sra-fp80-array.ll [new file with mode: 0644]
test/DebugInfo/X86/global-sra-fp80-struct.ll [new file with mode: 0644]

index 91db29a..9dfb61d 100644 (file)
@@ -526,7 +526,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
         NGV->setAlignment(NewAlign);
 
       // Copy over the debug info for the variable.
-      uint64_t Size = DL.getTypeSizeInBits(NGV->getValueType());
+      uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType());
       uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(i);
       transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, NumElements);
     }
@@ -538,7 +538,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
     auto ElTy = STy->getElementType();
     uint64_t EltSize = DL.getTypeAllocSize(ElTy);
     unsigned EltAlign = DL.getABITypeAlignment(ElTy);
-    uint64_t FragmentSizeInBits = DL.getTypeSizeInBits(ElTy);
+    uint64_t FragmentSizeInBits = DL.getTypeAllocSizeInBits(ElTy);
     for (unsigned i = 0, e = NumElements; i != e; ++i) {
       Constant *In = Init->getAggregateElement(i);
       assert(In && "Couldn't get element of initializer?");
diff --git a/test/DebugInfo/X86/global-sra-fp80-array.ll b/test/DebugInfo/X86/global-sra-fp80-array.ll
new file mode 100644 (file)
index 0000000..a643127
--- /dev/null
@@ -0,0 +1,129 @@
+; RUN: opt -S -globalopt < %s | FileCheck %s
+source_filename = "array.c"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%struct.mystruct = type { i32, i64 }
+
+; Generated from:
+;
+; static long double array[2];
+; void __attribute__((nodebug)) foo(int in) { array[0] = in; }
+; void __attribute__((nodebug)) bar(int in) { array[1] = in; }
+; int main(int argc, char **argv)
+; {
+;   foo(argv[0][1]);
+;   bar(argv[0][1]);
+;   return (array[0] + array[1]) > 0;
+; }
+;
+; using clang -O0 -g2 -S -emit-llvm
+
+@array = internal global [2 x x86_fp80] zeroinitializer, align 16, !dbg !0
+
+; CHECK: @array.0 = internal unnamed_addr global x86_fp80 0xK00000000000000000000, align 16, !dbg ![[EL0:.*]]
+; CHECK: @array.1 = internal unnamed_addr global x86_fp80 0xK00000000000000000000, align 16, !dbg ![[EL1:.*]]
+;
+; CHECK: ![[EL0]] = !DIGlobalVariableExpression(var: ![[VAR:.*]], expr: !DIExpression(DW_OP_LLVM_fragment, 0, 128))
+; CHECK: ![[VAR]] = distinct !DIGlobalVariable(name: "array"
+; CHECK: ![[EL1]] = !DIGlobalVariableExpression(var: ![[VAR]], expr: !DIExpression(DW_OP_LLVM_fragment, 128, 128))
+
+; Function Attrs: noinline nounwind optnone uwtable
+define void @foo(i32 %in) #0 {
+entry:
+  %in.addr = alloca i32, align 4
+  store i32 %in, i32* %in.addr, align 4
+  %0 = load i32, i32* %in.addr, align 4
+  %conv = sitofp i32 %0 to x86_fp80
+  store x86_fp80 %conv, x86_fp80* getelementptr inbounds ([2 x x86_fp80], [2 x x86_fp80]* @array, i64 0, i64 0), align 16
+  ret void
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define void @bar(i32 %in) #0 {
+entry:
+  %in.addr = alloca i32, align 4
+  store i32 %in, i32* %in.addr, align 4
+  %0 = load i32, i32* %in.addr, align 4
+  %conv = sitofp i32 %0 to x86_fp80
+  store x86_fp80 %conv, x86_fp80* getelementptr inbounds ([2 x x86_fp80], [2 x x86_fp80]* @array, i64 0, i64 1), align 16
+  ret void
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define i32 @main(i32 %argc, i8** %argv) #0 !dbg !14 {
+entry:
+  %retval = alloca i32, align 4
+  %argc.addr = alloca i32, align 4
+  %argv.addr = alloca i8**, align 8
+  store i32 0, i32* %retval, align 4
+  store i32 %argc, i32* %argc.addr, align 4
+  call void @llvm.dbg.declare(metadata i32* %argc.addr, metadata !21, metadata !DIExpression()), !dbg !22
+  store i8** %argv, i8*** %argv.addr, align 8
+  call void @llvm.dbg.declare(metadata i8*** %argv.addr, metadata !23, metadata !DIExpression()), !dbg !24
+  %0 = load i8**, i8*** %argv.addr, align 8, !dbg !25
+  %arrayidx = getelementptr inbounds i8*, i8** %0, i64 0, !dbg !25
+  %1 = load i8*, i8** %arrayidx, align 8, !dbg !25
+  %arrayidx1 = getelementptr inbounds i8, i8* %1, i64 1, !dbg !25
+  %2 = load i8, i8* %arrayidx1, align 1, !dbg !25
+  %conv = sext i8 %2 to i32, !dbg !25
+  call void @foo(i32 %conv), !dbg !26
+  %3 = load i8**, i8*** %argv.addr, align 8, !dbg !27
+  %arrayidx2 = getelementptr inbounds i8*, i8** %3, i64 0, !dbg !27
+  %4 = load i8*, i8** %arrayidx2, align 8, !dbg !27
+  %arrayidx3 = getelementptr inbounds i8, i8* %4, i64 1, !dbg !27
+  %5 = load i8, i8* %arrayidx3, align 1, !dbg !27
+  %conv4 = sext i8 %5 to i32, !dbg !27
+  call void @bar(i32 %conv4), !dbg !28
+  %6 = load x86_fp80, x86_fp80* getelementptr inbounds ([2 x x86_fp80], [2 x x86_fp80]* @array, i64 0, i64 0), align 16, !dbg !29
+  %7 = load x86_fp80, x86_fp80* getelementptr inbounds ([2 x x86_fp80], [2 x x86_fp80]* @array, i64 0, i64 1), align 16, !dbg !30
+  %add = fadd x86_fp80 %6, %7, !dbg !31
+  %cmp = fcmp ogt x86_fp80 %add, 0xK00000000000000000000, !dbg !32
+  %conv5 = zext i1 %cmp to i32, !dbg !32
+  ret i32 %conv5, !dbg !33
+}
+
+; Function Attrs: nounwind readnone speculatable
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
+
+attributes #0 = { noinline nounwind optnone uwtable }
+attributes #1 = { nounwind readnone speculatable }
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!10, !11, !12}
+!llvm.ident = !{!13}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "array", scope: !2, file: !3, line: 1, type: !6, isLocal: true, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 7.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5)
+!3 = !DIFile(filename: "array.c", directory: "/")
+!4 = !{}
+!5 = !{!0}
+!6 = !DICompositeType(tag: DW_TAG_array_type, baseType: !7, size: 256, elements: !8)
+!7 = !DIBasicType(name: "long double", size: 128, encoding: DW_ATE_float)
+!8 = !{!9}
+!9 = !DISubrange(count: 2)
+!10 = !{i32 2, !"Dwarf Version", i32 4}
+!11 = !{i32 2, !"Debug Info Version", i32 3}
+!12 = !{i32 1, !"wchar_size", i32 4}
+!13 = !{!"clang version 7.0.0"}
+!14 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 4, type: !15, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !4)
+!15 = !DISubroutineType(types: !16)
+!16 = !{!17, !17, !18}
+!17 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64)
+!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
+!20 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
+!21 = !DILocalVariable(name: "argc", arg: 1, scope: !14, file: !3, line: 4, type: !17)
+!22 = !DILocation(line: 4, column: 14, scope: !14)
+!23 = !DILocalVariable(name: "argv", arg: 2, scope: !14, file: !3, line: 4, type: !18)
+!24 = !DILocation(line: 4, column: 27, scope: !14)
+!25 = !DILocation(line: 6, column: 7, scope: !14)
+!26 = !DILocation(line: 6, column: 3, scope: !14)
+!27 = !DILocation(line: 7, column: 7, scope: !14)
+!28 = !DILocation(line: 7, column: 3, scope: !14)
+!29 = !DILocation(line: 8, column: 11, scope: !14)
+!30 = !DILocation(line: 8, column: 22, scope: !14)
+!31 = !DILocation(line: 8, column: 20, scope: !14)
+!32 = !DILocation(line: 8, column: 32, scope: !14)
+!33 = !DILocation(line: 8, column: 3, scope: !14)
diff --git a/test/DebugInfo/X86/global-sra-fp80-struct.ll b/test/DebugInfo/X86/global-sra-fp80-struct.ll
new file mode 100644 (file)
index 0000000..2254396
--- /dev/null
@@ -0,0 +1,134 @@
+; RUN: opt -S -globalopt < %s | FileCheck %s
+source_filename = "struct.c"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+%struct.mystruct = type { x86_fp80, i32, [12 x i8] }
+
+; Generated from:
+;
+; static struct mystruct {
+;     long double a;
+;     int b;
+; } static_struct;
+; void __attribute__((nodebug)) foo(int in) { static_struct.a = in; }
+; void __attribute__((nodebug)) bar(int in) { static_struct.b = in; }
+; int main(int argc, char **argv)
+; {
+;     foo(argv[0][1]);
+;     bar(argv[0][1]);
+;     return (static_struct.a + static_struct.b) > 0;
+; }
+;
+; using clang -O0 -g2 -S -emit-llvm
+
+@static_struct = internal global %struct.mystruct zeroinitializer, align 16, !dbg !0
+
+; CHECK: @static_struct.0 = internal unnamed_addr global x86_fp80 0xK00000000000000000000, align 16, !dbg ![[EL0:.*]]
+; CHECK: @static_struct.1 = internal unnamed_addr global i32 0, align 16, !dbg ![[EL1:.*]]
+
+; CHECK: ![[EL0]] = !DIGlobalVariableExpression(var: ![[VAR:.*]], expr: !DIExpression(DW_OP_LLVM_fragment, 0, 128))
+; CHECK: ![[VAR]] = distinct !DIGlobalVariable(name: "static_struct"
+; CHECK: ![[EL1]] = !DIGlobalVariableExpression(var: ![[VAR]], expr: !DIExpression(DW_OP_LLVM_fragment, 128, 32))
+
+; Function Attrs: noinline nounwind optnone uwtable
+define void @foo(i32 %in) #0 {
+entry:
+  %in.addr = alloca i32, align 4
+  store i32 %in, i32* %in.addr, align 4
+  %0 = load i32, i32* %in.addr, align 4
+  %conv = sitofp i32 %0 to x86_fp80
+  store x86_fp80 %conv, x86_fp80* getelementptr inbounds (%struct.mystruct, %struct.mystruct* @static_struct, i32 0, i32 0), align 16
+  ret void
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define void @bar(i32 %in) #0 {
+entry:
+  %in.addr = alloca i32, align 4
+  store i32 %in, i32* %in.addr, align 4
+  %0 = load i32, i32* %in.addr, align 4
+  store i32 %0, i32* getelementptr inbounds (%struct.mystruct, %struct.mystruct* @static_struct, i32 0, i32 1), align 16
+  ret void
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define i32 @main(i32 %argc, i8** %argv) #0 !dbg !16 {
+entry:
+  %retval = alloca i32, align 4
+  %argc.addr = alloca i32, align 4
+  %argv.addr = alloca i8**, align 8
+  store i32 0, i32* %retval, align 4
+  store i32 %argc, i32* %argc.addr, align 4
+  call void @llvm.dbg.declare(metadata i32* %argc.addr, metadata !22, metadata !DIExpression()), !dbg !23
+  store i8** %argv, i8*** %argv.addr, align 8
+  call void @llvm.dbg.declare(metadata i8*** %argv.addr, metadata !24, metadata !DIExpression()), !dbg !25
+  %0 = load i8**, i8*** %argv.addr, align 8, !dbg !26
+  %arrayidx = getelementptr inbounds i8*, i8** %0, i64 0, !dbg !26
+  %1 = load i8*, i8** %arrayidx, align 8, !dbg !26
+  %arrayidx1 = getelementptr inbounds i8, i8* %1, i64 1, !dbg !26
+  %2 = load i8, i8* %arrayidx1, align 1, !dbg !26
+  %conv = sext i8 %2 to i32, !dbg !26
+  call void @foo(i32 %conv), !dbg !27
+  %3 = load i8**, i8*** %argv.addr, align 8, !dbg !28
+  %arrayidx2 = getelementptr inbounds i8*, i8** %3, i64 0, !dbg !28
+  %4 = load i8*, i8** %arrayidx2, align 8, !dbg !28
+  %arrayidx3 = getelementptr inbounds i8, i8* %4, i64 1, !dbg !28
+  %5 = load i8, i8* %arrayidx3, align 1, !dbg !28
+  %conv4 = sext i8 %5 to i32, !dbg !28
+  call void @bar(i32 %conv4), !dbg !29
+  %6 = load x86_fp80, x86_fp80* getelementptr inbounds (%struct.mystruct, %struct.mystruct* @static_struct, i32 0, i32 0), align 16, !dbg !30
+  %7 = load i32, i32* getelementptr inbounds (%struct.mystruct, %struct.mystruct* @static_struct, i32 0, i32 1), align 16, !dbg !31
+  %conv5 = sitofp i32 %7 to x86_fp80, !dbg !32
+  %add = fadd x86_fp80 %6, %conv5, !dbg !33
+  %cmp = fcmp ogt x86_fp80 %add, 0xK00000000000000000000, !dbg !34
+  %conv6 = zext i1 %cmp to i32, !dbg !34
+  ret i32 %conv6, !dbg !35
+}
+
+; Function Attrs: nounwind readnone speculatable
+declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
+
+attributes #0 = { noinline nounwind optnone uwtable }
+attributes #1 = { nounwind readnone speculatable }
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!12, !13, !14}
+!llvm.ident = !{!15}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "static_struct", scope: !2, file: !3, line: 4, type: !6, isLocal: true, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 7.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5)
+!3 = !DIFile(filename: "struct.c", directory: "/")
+!4 = !{}
+!5 = !{!0}
+!6 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "mystruct", file: !3, line: 1, size: 256, elements: !7)
+!7 = !{!8, !10}
+!8 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !6, file: !3, line: 2, baseType: !9, size: 128)
+!9 = !DIBasicType(name: "long double", size: 128, encoding: DW_ATE_float)
+!10 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !6, file: !3, line: 3, baseType: !11, size: 32, offset: 128)
+!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!12 = !{i32 2, !"Dwarf Version", i32 4}
+!13 = !{i32 2, !"Debug Info Version", i32 3}
+!14 = !{i32 1, !"wchar_size", i32 4}
+!15 = !{!"clang version 7.0.0"}
+!16 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 7, type: !17, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !4)
+!17 = !DISubroutineType(types: !18)
+!18 = !{!11, !11, !19}
+!19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
+!20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !21, size: 64)
+!21 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
+!22 = !DILocalVariable(name: "argc", arg: 1, scope: !16, file: !3, line: 7, type: !11)
+!23 = !DILocation(line: 7, column: 14, scope: !16)
+!24 = !DILocalVariable(name: "argv", arg: 2, scope: !16, file: !3, line: 7, type: !19)
+!25 = !DILocation(line: 7, column: 27, scope: !16)
+!26 = !DILocation(line: 9, column: 9, scope: !16)
+!27 = !DILocation(line: 9, column: 5, scope: !16)
+!28 = !DILocation(line: 10, column: 9, scope: !16)
+!29 = !DILocation(line: 10, column: 5, scope: !16)
+!30 = !DILocation(line: 11, column: 27, scope: !16)
+!31 = !DILocation(line: 11, column: 45, scope: !16)
+!32 = !DILocation(line: 11, column: 31, scope: !16)
+!33 = !DILocation(line: 11, column: 29, scope: !16)
+!34 = !DILocation(line: 11, column: 48, scope: !16)
+!35 = !DILocation(line: 11, column: 5, scope: !16)