1; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
2
3; Test llvm.dbg.value for the local variables moved onto the unsafe stack.
4; SafeStack rewrites them relative to the unsafe stack pointer (base address of
5; the unsafe stack frame).
6
7target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8target triple = "x86_64-unknown-linux-gnu"
9
10; Function Attrs: noinline safestack uwtable
11define void @f() #0 !dbg !6 {
12entry:
13; CHECK:   %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
14  %x1 = alloca i32, align 4
15  %x2 = alloca i32, align 4
16  %0 = bitcast i32* %x1 to i8*, !dbg !13
17  %1 = bitcast i32* %x2 to i8*, !dbg !14
18
19; Unhandled dbg.value: expression does not start with OP_DW_deref
20; CHECK: call void @llvm.dbg.value(metadata i32* undef, metadata !{{.*}}, metadata !{{.*}})
21  tail call void @llvm.dbg.value(metadata i32* %x1, metadata !10, metadata !23), !dbg !16
22
23; Unhandled dbg.value: expression does not start with OP_DW_deref
24; CHECK: call void @llvm.dbg.value(metadata i32* undef, metadata !{{.*}}, metadata !{{.*}})
25  tail call void @llvm.dbg.value(metadata i32* %x1, metadata !10, metadata !24), !dbg !16
26
27; Supported dbg.value: rewritted based on the [[USP]] value.
28; CHECK: call void @llvm.dbg.value(metadata i8* %[[USP]], metadata ![[X1:.*]], metadata !DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref, DW_OP_LLVM_fragment, 0, 4))
29  tail call void @llvm.dbg.value(metadata i32* %x1, metadata !10, metadata !25), !dbg !16
30
31; Supported dbg.value: rewritted based on the [[USP]] value.
32; CHECK: call void @llvm.dbg.value(metadata i8* %[[USP]], metadata ![[X1:.*]], metadata !DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref))
33  tail call void @llvm.dbg.value(metadata i32* %x1, metadata !10, metadata !15), !dbg !16
34  call void @capture(i32* nonnull %x1), !dbg !17
35
36; An extra non-dbg.value metadata use of %x2. Replaced with undef.
37; CHECK: call void @llvm.random.metadata.use(metadata i32* undef
38  call void @llvm.random.metadata.use(metadata i32* %x2)
39
40; CHECK: call void @llvm.dbg.value(metadata i8* %[[USP]], metadata ![[X2:.*]], metadata !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_deref))
41  call void @llvm.dbg.value(metadata i32* %x2, metadata !12, metadata !15), !dbg !18
42  call void @capture(i32* nonnull %x2), !dbg !19
43  ret void, !dbg !20
44}
45
46; Function Attrs: argmemonly nounwind
47declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #1
48
49declare void @capture(i32*) #2
50
51; Function Attrs: argmemonly nounwind
52declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #1
53
54; Function Attrs: nounwind readnone
55declare void @llvm.dbg.value(metadata, metadata, metadata) #3
56
57declare void @llvm.random.metadata.use(metadata)
58
59attributes #0 = { noinline safestack uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
60attributes #1 = { argmemonly nounwind }
61attributes #2 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
62attributes #3 = { nounwind readnone }
63attributes #4 = { nounwind }
64
65!llvm.dbg.cu = !{!0}
66!llvm.module.flags = !{!3, !4}
67!llvm.ident = !{!5}
68
69!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.9.0 (trunk 271022) (llvm/trunk 271027)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
70!1 = !DIFile(filename: "../llvm/2.cc", directory: "/code/build-llvm")
71
72!2 = !{}
73!3 = !{i32 2, !"Dwarf Version", i32 4}
74!4 = !{i32 2, !"Debug Info Version", i32 3}
75!5 = !{!"clang version 3.9.0 (trunk 271022) (llvm/trunk 271027)"}
76!6 = distinct !DISubprogram(name: "f", linkageName: "_Z1fv", scope: !1, file: !1, line: 4, type: !7, isLocal: false, isDefinition: true, scopeLine: 4, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !9)
77!7 = !DISubroutineType(types: !8)
78!8 = !{null}
79!9 = !{!10, !12}
80
81; CHECK-DAG: ![[X1]] = !DILocalVariable(name: "x1",
82!10 = !DILocalVariable(name: "x1", scope: !6, file: !1, line: 5, type: !11)
83!11 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
84
85; CHECK-DAG: ![[X2]] = !DILocalVariable(name: "x2",
86!12 = !DILocalVariable(name: "x2", scope: !6, file: !1, line: 6, type: !11)
87!13 = !DILocation(line: 5, column: 3, scope: !6)
88!14 = !DILocation(line: 6, column: 3, scope: !6)
89
90!15 = !DIExpression(DW_OP_deref)
91!16 = !DILocation(line: 5, column: 7, scope: !6)
92!17 = !DILocation(line: 8, column: 3, scope: !6)
93!18 = !DILocation(line: 6, column: 7, scope: !6)
94!19 = !DILocation(line: 9, column: 3, scope: !6)
95!20 = !DILocation(line: 10, column: 1, scope: !6)
96!21 = !DILocation(line: 10, column: 1, scope: !22)
97!22 = !DILexicalBlockFile(scope: !6, file: !1, discriminator: 1)
98!23 = !DIExpression()
99!24 = !DIExpression(DW_OP_constu, 42, DW_OP_minus)
100!25 = !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 4)
101