llvm.org GIT mirror llvm / 1eca697
Fix debug info with SafeStack. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@248933 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 5 years ago
8 changed file(s) with 198 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
271271 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
272272
273273 /// \brief Replaces llvm.dbg.declare instruction when an alloca is replaced with
274 /// a new value. If Deref is true, tan additional DW_OP_deref is prepended to
275 /// the expression.
274 /// a new value. If Deref is true, an additional DW_OP_deref is prepended to the
275 /// expression. If Offset is non-zero, a constant displacement is added to the
276 /// expression (after the optional Deref). Offset can be negative.
276277 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
277 DIBuilder &Builder, bool Deref);
278 DIBuilder &Builder, bool Deref, int Offset = 0);
278279
279280 /// Replace 'BB's terminator with one that does not have an unwind successor
280281 /// block. Rewrites `invoke` to `call`, `catchendpad unwind label %foo` to
210210 return AddMachineRegPiece(MachineReg, SizeInBits,
211211 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
212212 }
213 case dwarf::DW_OP_plus: {
214 // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
213 case dwarf::DW_OP_plus:
214 case dwarf::DW_OP_minus: {
215 // [DW_OP_reg,Offset,DW_OP_plus, DW_OP_deref] --> [DW_OP_breg, Offset].
216 // [DW_OP_reg,Offset,DW_OP_minus,DW_OP_deref] --> [DW_OP_breg,-Offset].
215217 auto N = I.getNext();
216218 if (N != E && N->getOp() == dwarf::DW_OP_deref) {
217219 unsigned Offset = I->getArg(0);
218 ValidReg = AddMachineRegIndirect(MachineReg, Offset);
220 ValidReg = AddMachineRegIndirect(
221 MachineReg, I->getOp() == dwarf::DW_OP_plus ? Offset : -Offset);
219222 std::advance(I, 2);
220223 break;
221224 } else
254257 EmitOp(dwarf::DW_OP_plus_uconst);
255258 EmitUnsigned(I->getArg(0));
256259 break;
260 case dwarf::DW_OP_minus:
261 // There is no OP_minus_uconst.
262 EmitOp(dwarf::DW_OP_constu);
263 EmitUnsigned(I->getArg(0));
264 EmitOp(dwarf::DW_OP_minus);
265 break;
257266 case dwarf::DW_OP_deref:
258267 EmitOp(dwarf::DW_OP_deref);
259268 break;
43934393 N);
43944394 return nullptr;
43954395 }
4396 } else if (AI)
4396 } else {
43974397 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
43984398 true, 0, dl, SDNodeOrder);
4399 else {
4400 // Can't do anything with other non-AI cases yet.
4401 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
4402 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4403 DEBUG(Address->dump());
4404 return nullptr;
44054399 }
44064400 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
44074401 } else {
495495 case dwarf::DW_OP_bit_piece:
496496 return 3;
497497 case dwarf::DW_OP_plus:
498 case dwarf::DW_OP_minus:
498499 return 2;
499500 default:
500501 return 1;
515516 // Piece expressions must be at the end.
516517 return I->get() + I->getSize() == E->get();
517518 case dwarf::DW_OP_plus:
519 case dwarf::DW_OP_minus:
518520 case dwarf::DW_OP_deref:
519521 break;
520522 }
442442 cast(NewAI)->takeName(AI);
443443
444444 // Replace alloc with the new location.
445 replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
445 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/true, -StaticOffset);
446446 AI->replaceAllUsesWith(NewAI);
447447 AI->eraseFromParent();
448448 }
11351135 }
11361136
11371137 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
1138 DIBuilder &Builder, bool Deref) {
1138 DIBuilder &Builder, bool Deref, int Offset) {
11391139 DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI);
11401140 if (!DDI)
11411141 return false;
11441144 auto *DIExpr = DDI->getExpression();
11451145 assert(DIVar && "Missing variable");
11461146
1147 if (Deref) {
1147 if (Deref || Offset) {
11481148 // Create a copy of the original DIDescriptor for user variable, prepending
11491149 // "deref" operation to a list of address elements, as new llvm.dbg.declare
11501150 // will take a value storing address of the memory for variable, not
11511151 // alloca itself.
11521152 SmallVector NewDIExpr;
1153 NewDIExpr.push_back(dwarf::DW_OP_deref);
1153 if (Deref)
1154 NewDIExpr.push_back(dwarf::DW_OP_deref);
1155 if (Offset > 0) {
1156 NewDIExpr.push_back(dwarf::DW_OP_plus);
1157 NewDIExpr.push_back(Offset);
1158 } else if (Offset < 0) {
1159 NewDIExpr.push_back(dwarf::DW_OP_minus);
1160 NewDIExpr.push_back(-Offset);
1161 }
11541162 if (DIExpr)
11551163 NewDIExpr.append(DIExpr->elements_begin(), DIExpr->elements_end());
11561164 DIExpr = Builder.createExpression(NewDIExpr);
0 ; Test dwarf codegen of DW_OP_minus.
1 ; RUN: llc -O0 -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
2
3 ; This was built by compiling the following source with SafeStack and
4 ; simplifying the result a little.
5 ; extern "C" {
6 ; void Capture(int *);
7 ; void f() {
8 ; int buf[100];
9 ; Capture(buf);
10 ; }
11 ; }
12 ; The interesting part is !DIExpression(DW_OP_deref, DW_OP_minus, 400)
13
14 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
15 target triple = "x86_64-unknown-linux-gnu"
16
17 @__safestack_unsafe_stack_ptr = external thread_local(initialexec) global i8*
18
19 define void @f() {
20 entry:
21 %unsafe_stack_ptr = load i8*, i8** @__safestack_unsafe_stack_ptr
22 %unsafe_stack_static_top = getelementptr i8, i8* %unsafe_stack_ptr, i32 -400
23 store i8* %unsafe_stack_static_top, i8** @__safestack_unsafe_stack_ptr
24 %0 = getelementptr i8, i8* %unsafe_stack_ptr, i32 -400
25 %buf = bitcast i8* %0 to [100 x i32]*
26 %1 = bitcast [100 x i32]* %buf to i8*, !dbg !16
27 call void @llvm.dbg.declare(metadata i8* %unsafe_stack_ptr, metadata !8, metadata !17), !dbg !18
28 %arraydecay = getelementptr inbounds [100 x i32], [100 x i32]* %buf, i64 0, i64 0, !dbg !19
29 call void @Capture(i32* %arraydecay), !dbg !20
30 store i8* %unsafe_stack_ptr, i8** @__safestack_unsafe_stack_ptr, !dbg !21
31 ret void, !dbg !21
32 }
33
34 ; Function Attrs: nounwind readnone
35 declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone
36
37 declare void @Capture(i32*)
38
39 !llvm.dbg.cu = !{!0}
40 !llvm.module.flags = !{!13, !14}
41
42 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3)
43 !1 = !DIFile(filename: "1.cc", directory: "/tmp")
44 !2 = !{}
45 !3 = !{!4}
46 !4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, function: void ()* @f, variables: !7)
47 !5 = !DISubroutineType(types: !6)
48 !6 = !{null}
49 !7 = !{!8}
50 !8 = !DILocalVariable(name: "buf", scope: !4, file: !1, line: 5, type: !9)
51 !9 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 3200, align: 32, elements: !11)
52 !10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
53 !11 = !{!12}
54 !12 = !DISubrange(count: 100)
55 !13 = !{i32 2, !"Dwarf Version", i32 4}
56 !14 = !{i32 2, !"Debug Info Version", i32 3}
57 !15 = !{!"clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)"}
58 !16 = !DILocation(line: 5, column: 3, scope: !4)
59 !17 = !DIExpression(DW_OP_deref, DW_OP_minus, 400)
60 !18 = !DILocation(line: 5, column: 7, scope: !4)
61 !19 = !DILocation(line: 6, column: 11, scope: !4)
62 !20 = !DILocation(line: 6, column: 3, scope: !4)
63 !21 = !DILocation(line: 7, column: 1, scope: !4)
64
65 ; RCX - 400
66 ; CHECK: .short 6 # Loc expr size
67 ; CHECK-NEXT: .byte 114 # DW_OP_breg2
68 ; CHECK-NEXT: .byte 0 # 0
69 ; CHECK-NEXT: .byte 16 # DW_OP_constu
70 ; CHECK-NEXT: .byte 144 # 400
71 ; CHECK-NEXT: .byte 3 # DW_OP_minus
72 ; CHECK-NEXT: .byte 28
73
74 ; RCX is clobbered in call @Capture, but there is a spilled copy.
75 ; *(RSP + 8) - 400
76 ; CHECK: .short 7 # Loc expr size
77 ; CHECK-NEXT: .byte 119 # DW_OP_breg7
78 ; CHECK-NEXT: .byte 8 # 8
79 ; CHECK-NEXT: .byte 6 # DW_OP_deref
80 ; CHECK-NEXT: .byte 16 # DW_OP_constu
81 ; CHECK-NEXT: .byte 144 # 400
82 ; CHECK-NEXT: .byte 3 # DW_OP_minus
83 ; CHECK-NEXT: .byte 28
0 ; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
1
2 ; Test debug location for the local variables moved onto the unsafe stack.
3 ; CHECK: define void @f
4 ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
5
6 ; dbg.declare for %buf is gone; replaced with dbg.declare based off the unsafe stack pointer
7 ; CHECK-NOT: @llvm.dbg.declare.*%buf
8 ; CHECK: call void @llvm.dbg.declare(metadata i8* %[[USP]], metadata ![[VAR:.*]], metadata ![[EXPR:.*]])
9
10 ; dbg.declare appears before the first use of %buf
11 ; CHECK: getelementptr{{.*}}%buf
12 ; CHECK: call{{.*}}@Capture
13 ; CHECK: ret void
14
15 ; dbg.declare describes "buf"...
16 ; CHECK: ![[VAR]] = !DILocalVariable(name: "buf"
17
18 ; ... as an offset from the unsafe stack pointer
19 ; CHECK: ![[EXPR]] = !DIExpression(DW_OP_deref, DW_OP_minus, 400)
20
21
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
23 target triple = "x86_64-unknown-linux-gnu"
24
25 ; Function Attrs: safestack uwtable
26 define void @f() #0 {
27 entry:
28 %buf = alloca [100 x i32], align 16
29 %0 = bitcast [100 x i32]* %buf to i8*, !dbg !16
30 call void @llvm.lifetime.start(i64 400, i8* %0) #4, !dbg !16
31 tail call void @llvm.dbg.declare(metadata [100 x i32]* %buf, metadata !8, metadata !17), !dbg !18
32
33
34 %arraydecay = getelementptr inbounds [100 x i32], [100 x i32]* %buf, i64 0, i64 0, !dbg !19
35 call void @Capture(i32* %arraydecay), !dbg !20
36 call void @llvm.lifetime.end(i64 400, i8* %0) #4, !dbg !21
37 ret void, !dbg !21
38 }
39
40 ; Function Attrs: nounwind argmemonly
41 declare void @llvm.lifetime.start(i64, i8* nocapture) #1
42
43 ; Function Attrs: nounwind readnone
44 declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
45
46 declare void @Capture(i32*) #3
47
48 ; Function Attrs: nounwind argmemonly
49 declare void @llvm.lifetime.end(i64, i8* nocapture) #1
50
51 attributes #0 = { safestack uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
52 attributes #1 = { nounwind argmemonly }
53 attributes #2 = { nounwind readnone }
54 attributes #3 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
55 attributes #4 = { nounwind }
56
57 !llvm.dbg.cu = !{!0}
58 !llvm.module.flags = !{!13, !14}
59 !llvm.ident = !{!15}
60
61 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)", isOptimized: true, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3)
62 !1 = !DIFile(filename: "1.cc", directory: "/tmp")
63 !2 = !{}
64 !3 = !{!4}
65 !4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 4, type: !5, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, function: void ()* @f, variables: !7)
66 !5 = !DISubroutineType(types: !6)
67 !6 = !{null}
68 !7 = !{!8}
69 !8 = !DILocalVariable(name: "buf", scope: !4, file: !1, line: 5, type: !9)
70 !9 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 3200, align: 32, elements: !11)
71 !10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
72 !11 = !{!12}
73 !12 = !DISubrange(count: 100)
74 !13 = !{i32 2, !"Dwarf Version", i32 4}
75 !14 = !{i32 2, !"Debug Info Version", i32 3}
76 !15 = !{!"clang version 3.8.0 (trunk 248518) (llvm/trunk 248512)"}
77 !16 = !DILocation(line: 5, column: 3, scope: !4)
78 !17 = !DIExpression()
79 !18 = !DILocation(line: 5, column: 7, scope: !4)
80 !19 = !DILocation(line: 6, column: 11, scope: !4)
81 !20 = !DILocation(line: 6, column: 3, scope: !4)
82 !21 = !DILocation(line: 7, column: 1, scope: !4)