llvm.org GIT mirror llvm / 2079dc3
Improve ConvertDebugDeclareToDebugValue Summary: This is a follow-up to r334830 and r335031. In the valueCoversEntireFragment check we now also handle the situation when there is a variable length array (VLA) involved, and the length of the array has been reduced to a constant. The ConvertDebugDeclareToDebugValue functions that are related to PHI nodes and load instructions now avoid inserting dbg.value intrinsics when the value does not, for certain, cover the variable/fragment that should be described. In r334830 we assumed that the value always covered the entire var/fragment and we had assertions in the code to show that assumption. However, those asserts failed when compiling code with VLAs, so we removed the asserts in r335031. Now when we know that the valueCoversEntireFragment check can fail also for PHI/Load instructions we avoid to insert the faulty dbg.value intrinsic in such situations. Compared to the Store instruction scenario we simply drop the dbg.value here (as the variable does not change its value due to PHI/Load, so an earlier dbg.value describing the variable should still be valid). Reviewers: aprantl, vsk, efriedma Reviewed By: aprantl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D48547 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335580 91177308-0d34-0410-b5e6-96231b3b80d8 Bjorn Pettersson 1 year, 4 months ago
5 changed file(s) with 172 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
9696 PointerType *getType() const {
9797 return cast(Instruction::getType());
9898 }
99
100 /// Get allocation size in bits. Returns None if size can't be determined,
101 /// e.g. in case of a VLA.
102 Optional getAllocationSizeInBits(const DataLayout &DL) const;
99103
100104 /// Return the type that is being allocated by the instruction.
101105 Type *getAllocatedType() const { return AllocatedType; }
4242 #include
4343
4444 using namespace llvm;
45
46 //===----------------------------------------------------------------------===//
47 // AllocaInst Class
48 //===----------------------------------------------------------------------===//
49
50 Optional
51 AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const {
52 uint64_t Size = DL.getTypeAllocSizeInBits(getAllocatedType());
53 if (isArrayAllocation()) {
54 auto C = dyn_cast(getArraySize());
55 if (!C)
56 return None;
57 Size *= C->getZExtValue();
58 }
59 return Size;
60 }
4561
4662 //===----------------------------------------------------------------------===//
4763 // CallSite Class
12411241 uint64_t ValueSize = DL.getTypeAllocSizeInBits(ValTy);
12421242 if (auto FragmentSize = DII->getFragmentSizeInBits())
12431243 return ValueSize >= *FragmentSize;
1244 // We can't always calculate the size of the DI variable (e.g. if it is a
1245 // VLA). Try to use the size of the alloca that the dbg intrinsic describes
1246 // intead.
1247 if (DII->isAddressOfVariable())
1248 if (auto *AI = dyn_cast_or_null(DII->getVariableLocation()))
1249 if (auto FragmentSize = AI->getAllocationSizeInBits(DL))
1250 return ValueSize >= *FragmentSize;
1251 // Could not determine size of variable. Conservatively return false.
12441252 return false;
12451253 }
12461254
13121320 if (LdStHasDebugValue(DIVar, DIExpr, LI))
13131321 return;
13141322
1323 if (!valueCoversEntireFragment(LI->getType(), DII)) {
1324 // FIXME: If only referring to a part of the variable described by the
1325 // dbg.declare, then we want to insert a dbg.value for the corresponding
1326 // fragment.
1327 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1328 << *DII << '\n');
1329 return;
1330 }
1331
13151332 // We are now tracking the loaded value instead of the address. In the
13161333 // future if multi-location support is added to the IR, it might be
13171334 // preferable to keep tracking both the loaded value and the original
13311348
13321349 if (PhiHasDebugValue(DIVar, DIExpr, APN))
13331350 return;
1351
1352 if (!valueCoversEntireFragment(APN->getType(), DII)) {
1353 // FIXME: If only referring to a part of the variable described by the
1354 // dbg.declare, then we want to insert a dbg.value for the corresponding
1355 // fragment.
1356 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
1357 << *DII << '\n');
1358 return;
1359 }
13341360
13351361 BasicBlock *BB = APN->getParent();
13361362 auto InsertionPt = BB->getFirstInsertionPt();
0 ; RUN: opt < %s -mem2reg -S | FileCheck %s
1
2 ; Testing conversion from dbg.declare to dbg.value when the variable is a VLA.
3 ;
4 ; We can't derive the size of the variable simply by looking at the
5 ; metadata. But we can find out the size by examining the alloca, so we should
6 ; know that the load/store instructions are referencing the whole variable,
7 ; and we expect to get dbg.value intrinsics that maps %entryN (aka %[[PHI]])
8 ; and %t0 to the variable allocated as %vla1.
9
10 ; ModuleID = 'debug-alloca-vla.ll'
11 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
12 target triple = "x86_64-apple-macosx10.12.0"
13
14 ; Function Attrs: nounwind ssp uwtable
15 define void @scan() #0 !dbg !4 {
16 entry:
17 %vla1 = alloca i32, i32 1, align 8
18 call void @llvm.dbg.declare(metadata i32* %vla1, metadata !10, metadata !DIExpression()), !dbg !18
19 br label %for.cond, !dbg !18
20
21 for.cond: ; preds = %for.cond, %entry
22 ; CHECK: %[[PHI:.*]] = phi i32 [ undef, %entry ], [ %t0, %for.cond ]
23 %entryN = load i32, i32* %vla1, align 8, !dbg !18
24 ; CHECK: call void @llvm.dbg.value(metadata i32 %[[PHI]],
25 ; CHECK-SAME: metadata !DIExpression())
26 %t0 = add i32 %entryN, 1
27 ; CHECK: %t0 = add i32 %[[PHI]], 1
28 ; CHECK: call void @llvm.dbg.value(metadata i32 %t0,
29 ; CHECK-SAME: metadata !DIExpression())
30 store i32 %t0, i32* %vla1, align 8, !dbg !18
31 br label %for.cond, !dbg !18
32 }
33
34 ; Function Attrs: nounwind readnone speculatable
35 declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
36
37 attributes #0 = { nounwind ssp uwtable }
38 attributes #1 = { nounwind readnone speculatable }
39
40 !llvm.dbg.cu = !{!0}
41 !llvm.module.flags = !{!2, !3}
42
43 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "adrian", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
44 !1 = !DIFile(filename: "", directory: "/")
45 !2 = !{i32 2, !"Debug Info Version", i32 3}
46 !3 = !{i32 7, !"PIC Level", i32 2}
47 !4 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8)
48 !5 = !DISubroutineType(types: !6)
49 !6 = !{null, !7, !7}
50 !7 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
51 !8 = !{!9}
52 !9 = !DILocalVariable(name: "entry", scope: !4, file: !1, line: 6, type: !7)
53 !10 = !DILocalVariable(name: "ptr32", scope: !4, file: !1, line: 240, type: !11)
54 !11 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, elements: !14)
55 !12 = !DIDerivedType(tag: DW_TAG_typedef, name: "__uint32_t", file: !1, line: 41, baseType: !13)
56 !13 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
57 !14 = !{!15}
58 !15 = !DISubrange(count: !16)
59 !16 = !DILocalVariable(name: "__vla_expr", scope: !4, type: !17, flags: DIFlagArtificial)
60 !17 = !DIBasicType(name: "long unsigned int", size: 64, encoding: DW_ATE_unsigned)
61 !18 = !DILocation(line: 6, scope: !4)
0 ; RUN: opt < %s -mem2reg -S | FileCheck %s
1
2 ; Testing conversion from dbg.declare to dbg.value when the variable is a VLA.
3 ;
4 ; We can't derive the size of the variable since it is a VLA with an unknown
5 ; number of element.
6 ;
7 ; Verify that we do not get a dbg.value after the phi node (we can't know if
8 ; the phi nodes result describes the whole array or not). Also verify that we
9 ; get a dbg.value that says that we do not know the value of the VLA in place
10 ; of the store (since we do not know which part of the VLA the store is
11 ; writing to).
12
13 ; ModuleID = 'debug-alloca-vla.ll'
14 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
15 target triple = "x86_64-apple-macosx10.12.0"
16
17 ; Function Attrs: nounwind ssp uwtable
18 define void @scan(i32 %n) #0 !dbg !4 {
19 entry:
20 %vla1 = alloca i32, i32 %n, align 8
21 call void @llvm.dbg.declare(metadata i32* %vla1, metadata !10, metadata !DIExpression()), !dbg !18
22 br label %for.cond, !dbg !18
23
24 for.cond: ; preds = %for.cond, %entry
25 ; CHECK: %[[PHI:.*]] = phi i32 [ undef, %entry ], [ %t0, %for.cond ]
26 %entryN = load i32, i32* %vla1, align 8, !dbg !18
27 ; CHECK-NOT: call void @llvm.dbg.value
28 %t0 = add i32 %entryN, 1
29 ; CHECK: %t0 = add i32 %[[PHI]], 1
30 ; CHECK: call void @llvm.dbg.value(metadata i32 undef,
31 ; CHECK-SAME: metadata !DIExpression())
32 store i32 %t0, i32* %vla1, align 8, !dbg !18
33 br label %for.cond, !dbg !18
34 }
35
36 ; Function Attrs: nounwind readnone speculatable
37 declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
38
39 attributes #0 = { nounwind ssp uwtable }
40 attributes #1 = { nounwind readnone speculatable }
41
42 !llvm.dbg.cu = !{!0}
43 !llvm.module.flags = !{!2, !3}
44
45 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "adrian", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
46 !1 = !DIFile(filename: "", directory: "/")
47 !2 = !{i32 2, !"Debug Info Version", i32 3}
48 !3 = !{i32 7, !"PIC Level", i32 2}
49 !4 = distinct !DISubprogram(name: "scan", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8)
50 !5 = !DISubroutineType(types: !6)
51 !6 = !{null, !7, !7}
52 !7 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
53 !8 = !{!9}
54 !9 = !DILocalVariable(name: "entry", scope: !4, file: !1, line: 6, type: !7)
55 !10 = !DILocalVariable(name: "ptr32", scope: !4, file: !1, line: 240, type: !11)
56 !11 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, elements: !14)
57 !12 = !DIDerivedType(tag: DW_TAG_typedef, name: "__uint32_t", file: !1, line: 41, baseType: !13)
58 !13 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
59 !14 = !{!15}
60 !15 = !DISubrange(count: !16)
61 !16 = !DILocalVariable(name: "__vla_expr", scope: !4, type: !17, flags: DIFlagArtificial)
62 !17 = !DIBasicType(name: "long unsigned int", size: 64, encoding: DW_ATE_unsigned)
63 !18 = !DILocation(line: 6, scope: !4)