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

Commit c8ef2d5

Browse files
committed
SROA: Avoid creating a fragment expression that covers the entire variable.
Fixes PR35416. https://bugs.llvm.org/show_bug.cgi?id=35416 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319126 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e707c0c commit c8ef2d5

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

lib/Transforms/Scalar/SROA.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4101,6 +4101,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
41014101
if (!DbgDeclares.empty()) {
41024102
auto *Var = DbgDeclares.front()->getVariable();
41034103
auto *Expr = DbgDeclares.front()->getExpression();
4104+
auto VarSize = Var->getSizeInBits();
41044105
DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
41054106
uint64_t AllocaSize = DL.getTypeSizeInBits(AI.getAllocatedType());
41064107
for (auto Fragment : Fragments) {
@@ -4128,10 +4129,14 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
41284129
"new fragment is outside of original fragment");
41294130
Start -= OrigFragment->OffsetInBits;
41304131
}
4131-
if (auto E = DIExpression::createFragmentExpression(Expr, Start, Size))
4132-
FragmentExpr = *E;
4133-
else
4134-
continue;
4132+
// Avoid creating a fragment expression that covers the entire variable.
4133+
if (!VarSize || *VarSize != Size) {
4134+
if (auto E =
4135+
DIExpression::createFragmentExpression(Expr, Start, Size))
4136+
FragmentExpr = *E;
4137+
else
4138+
continue;
4139+
}
41354140
}
41364141

41374142
// Remove any existing intrinsics describing the same alloca.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
; RUN: opt -sroa -S -o - %s | FileCheck %s
2+
; Generated from clang -c -O2 -g -target x86_64-pc-windows-msvc
3+
; struct A { double x1[]; };
4+
; struct x2 {
5+
; x2(int) : x3() {}
6+
; int x3;
7+
; };
8+
; int x4();
9+
; x2 x5() {
10+
; x2 a(x4());
11+
; return a;
12+
; }
13+
; void *operator new(size_t, void *);
14+
; struct B {
15+
; B() { new (x8.x1) x2(x5()); }
16+
; A x8;
17+
; };
18+
; void x9() { B(); }
19+
source_filename = "t.cpp"
20+
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
21+
target triple = "x86_64-pc-windows-msvc19.11.0"
22+
23+
%struct.B = type { %struct.A }
24+
%struct.A = type { [0 x double], [8 x i8] }
25+
26+
; Function Attrs: nounwind readnone speculatable
27+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
28+
29+
declare i32 @"\01?x4@@YAHXZ"() local_unnamed_addr
30+
31+
define void @"\01?x9@@YAXXZ"() local_unnamed_addr !dbg !8 {
32+
entry:
33+
%agg.tmp.ensured = alloca %struct.B, align 8
34+
call void @llvm.dbg.declare(metadata %struct.B* %agg.tmp.ensured, metadata !11, metadata !DIExpression()), !dbg !24
35+
%call.i.i = call i32 @"\01?x4@@YAHXZ"(), !dbg !46, !noalias !47
36+
%x3.i.i.i = bitcast %struct.B* %agg.tmp.ensured to i32*, !dbg !50
37+
store i32 0, i32* %x3.i.i.i, align 4, !dbg !50, !tbaa !57, !alias.scope !47
38+
; CHECK: call void @llvm.dbg.value(metadata i32 0, metadata ![[A:.*]], metadata !DIExpression())
39+
; CHECK: ![[A]] = !DILocalVariable(name: "a",
40+
ret void, !dbg !62
41+
}
42+
43+
; Function Attrs: nounwind readnone speculatable
44+
declare void @llvm.dbg.value(metadata, metadata, metadata) #0
45+
46+
attributes #0 = { nounwind readnone speculatable }
47+
48+
!llvm.dbg.cu = !{!0}
49+
!llvm.module.flags = !{!3, !4, !5, !6}
50+
!llvm.ident = !{!7}
51+
52+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 6.0.0 (trunk 319058) (llvm/trunk 319066)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
53+
!1 = !DIFile(filename: "t.cpp", directory: "/")
54+
!2 = !{}
55+
!3 = !{i32 2, !"Dwarf Version", i32 4}
56+
!4 = !{i32 2, !"Debug Info Version", i32 3}
57+
!5 = !{i32 1, !"wchar_size", i32 2}
58+
!6 = !{i32 7, !"PIC Level", i32 2}
59+
!7 = !{!"clang version 6.0.0 (trunk 319058) (llvm/trunk 319066)"}
60+
!8 = distinct !DISubprogram(name: "x9", linkageName: "\01?x9@@YAXXZ", scope: !1, file: !1, line: 16, type: !9, isLocal: false, isDefinition: true, scopeLine: 16, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !2)
61+
!9 = !DISubroutineType(types: !10)
62+
!10 = !{null}
63+
!11 = !DILocalVariable(name: "a", scope: !12, file: !1, line: 8, type: !15)
64+
!12 = distinct !DISubprogram(name: "x5", linkageName: "\01?x5@@YA?AUx2@@XZ", scope: !1, file: !1, line: 7, type: !13, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !23)
65+
!13 = !DISubroutineType(types: !14)
66+
!14 = !{!15}
67+
!15 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "x2", file: !1, line: 2, size: 32, elements: !16, identifier: ".?AUx2@@")
68+
!16 = !{!17, !19}
69+
!17 = !DIDerivedType(tag: DW_TAG_member, name: "x3", scope: !15, file: !1, line: 4, baseType: !18, size: 32)
70+
!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
71+
!19 = !DISubprogram(name: "x2", scope: !15, file: !1, line: 3, type: !20, isLocal: false, isDefinition: false, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true)
72+
!20 = !DISubroutineType(types: !21)
73+
!21 = !{null, !22, !18}
74+
!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !15, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
75+
!23 = !{!11}
76+
!24 = !DILocation(line: 8, column: 6, scope: !12, inlinedAt: !25)
77+
!25 = distinct !DILocation(line: 13, column: 24, scope: !26, inlinedAt: !45)
78+
!26 = distinct !DILexicalBlock(scope: !27, file: !1, line: 13, column: 7)
79+
!27 = distinct !DISubprogram(name: "B", linkageName: "\01??0B@@QEAA@XZ", scope: !28, file: !1, line: 13, type: !39, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true, unit: !0, declaration: !38, variables: !42)
80+
!28 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "B", file: !1, line: 12, size: 64, elements: !29, identifier: ".?AUB@@")
81+
!29 = !{!30, !38}
82+
!30 = !DIDerivedType(tag: DW_TAG_member, name: "x8", scope: !28, file: !1, line: 14, baseType: !31, size: 64)
83+
!31 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "A", file: !1, line: 1, size: 64, elements: !32, identifier: ".?AUA@@")
84+
!32 = !{!33}
85+
!33 = !DIDerivedType(tag: DW_TAG_member, name: "x1", scope: !31, file: !1, line: 1, baseType: !34)
86+
!34 = !DICompositeType(tag: DW_TAG_array_type, baseType: !35, elements: !36)
87+
!35 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
88+
!36 = !{!37}
89+
!37 = !DISubrange(count: -1)
90+
!38 = !DISubprogram(name: "B", scope: !28, file: !1, line: 13, type: !39, isLocal: false, isDefinition: false, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true)
91+
!39 = !DISubroutineType(types: !40)
92+
!40 = !{null, !41}
93+
!41 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !28, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
94+
!42 = !{!43}
95+
!43 = !DILocalVariable(name: "this", arg: 1, scope: !27, type: !44, flags: DIFlagArtificial | DIFlagObjectPointer)
96+
!44 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !28, size: 64)
97+
!45 = distinct !DILocation(line: 16, column: 13, scope: !8)
98+
!46 = !DILocation(line: 8, column: 8, scope: !12, inlinedAt: !25)
99+
!47 = !{!48}
100+
!48 = distinct !{!48, !49, !"\01?x5@@YA?AUx2@@XZ: %agg.result"}
101+
!49 = distinct !{!49, !"\01?x5@@YA?AUx2@@XZ"}
102+
!50 = !DILocation(line: 3, column: 13, scope: !51, inlinedAt: !56)
103+
!51 = distinct !DISubprogram(name: "x2", linkageName: "\01??0x2@@QEAA@H@Z", scope: !15, file: !1, line: 3, type: !20, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, declaration: !19, variables: !52)
104+
!52 = !{!53, !54}
105+
!53 = !DILocalVariable(arg: 2, scope: !51, file: !1, line: 3, type: !18)
106+
!54 = !DILocalVariable(name: "this", arg: 1, scope: !51, type: !55, flags: DIFlagArtificial | DIFlagObjectPointer)
107+
!55 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !15, size: 64)
108+
!56 = distinct !DILocation(line: 8, column: 6, scope: !12, inlinedAt: !25)
109+
!57 = !{!58, !59, i64 0}
110+
!58 = !{!"?AUx2@@", !59, i64 0}
111+
!59 = !{!"int", !60, i64 0}
112+
!60 = !{!"omnipotent char", !61, i64 0}
113+
!61 = !{!"Simple C++ TBAA"}
114+
!62 = !DILocation(line: 16, column: 18, scope: !8)

0 commit comments

Comments
 (0)