xref: /llvm-project/llvm/test/DebugInfo/COFF/fpo-realign-vframe.ll (revision f8499d5709e37b4e9a6d2a39c385cfd2c00bad6e)
1; RUN: llc < %s | FileCheck %s --check-prefix=ASM
2; RUN: llc -filetype=obj < %s | llvm-readobj --codeview - | FileCheck %s --check-prefix=OBJ
3
4; PR38857
5
6; When stack realignment is required by dynamic allocas are not used, the
7; compiler will address locals with the ESP register. However, if call argument
8; set up uses PUSH instructions, ESP may vary over the course of the function.
9; This means it's not useful as a base register for describing the locations of
10; variables. Instead, our CodeView output prefers to use the VFRAME virtual
11; register, which is defined in the FPO data as $T0. Make sure we define it.
12
13; Original C++ test case, which uses __thiscall to encourage PUSH conversion:
14; struct Foo {
15;   int x = 42;
16;   int __declspec(noinline) foo();
17;   void __declspec(noinline) bar(int *a, int *b, ptr c);
18; };
19; int Foo::foo() {
20;   int a = 1;
21;   int b = 2;
22;   double __declspec(align(8)) force_alignment = 0.42;
23;   bar(&a, &b, &force_alignment);
24;   x += (int)force_alignment;
25;   return x;
26; }
27; void Foo::bar(int *a, int *b, ptr c) {
28;   __debugbreak();
29;   *c += *a + *b;
30; }
31; int main() {
32;   Foo o;
33;   o.foo();
34; }
35; This stops the debugger in bar, and locals in Foo::foo would be corrupt.
36
37; More reduced C code to generate this IR:
38; int getval(void);
39; void usevals(int *, int *, ptr);
40; int realign_with_csrs(int x) {
41;   int a = getval();
42;   double __declspec(align(8)) force_alignment = 0.42;
43;   usevals(&a, &x, &force_alignment);
44;   return x;
45; }
46
47; Match the prologue for the .cv_fpo* directives.
48; ASM-LABEL: _realign_with_csrs:
49; ASM:         .cv_fpo_proc    _realign_with_csrs 4
50; ASM: # %bb.0:                                # %entry
51; ASM:         pushl   %ebp
52; ASM:         .cv_fpo_pushreg %ebp
53; ASM:         movl    %esp, %ebp
54; ASM:         .cv_fpo_setframe        %ebp
55; ASM:         andl    $-8, %esp
56; ASM:         .cv_fpo_stackalign      8
57; ASM:         subl    $16, %esp
58; ASM:         .cv_fpo_stackalloc      16
59; ASM:         .cv_fpo_endprologue
60
61; 'x' should be EBP-relative, 'a' and 'force_alignment' ESP relative.
62; ASM:         calll   _getval
63; ASM-DAG:     leal    8(%esp), %[[LEA_DBL:[^ ]*]]
64; ASM-DAG:     leal    8(%ebp), %[[LEA_X:[^ ]*]]
65; ASM-DAG:     leal    4(%esp), %[[LEA_A:[^ ]*]]
66; ASM:         pushl   %[[LEA_DBL]]
67; ASM:         pushl   %[[LEA_X]]
68; ASM:         pushl   %[[LEA_A]]
69; ASM:         calll   _usevals
70; ASM:         addl    $12, %esp
71
72; OBJ: Subsection [
73; OBJ:   SubSectionType: Symbols (0xF1)
74; OBJ: ]
75; OBJ: Subsection [
76; OBJ:   SubSectionType: FrameData (0xF5)
77;   	Really, the only important FrameFunc is the last one.
78; OBJ:   FrameData {
79; OBJ:   }
80; OBJ:   FrameData {
81; OBJ:   }
82; OBJ:   FrameData {
83; OBJ:   }
84; OBJ:   FrameData {
85; OBJ:     FrameFunc [
86; OBJ-NEXT:   $T1 $ebp 4 + =
87; OBJ-NEXT:   $T0 $T1 4 - 8 @ =
88; OBJ-NEXT:   $eip $T1 ^ =
89; OBJ-NEXT:   $esp $T1 4 + =
90; OBJ-NEXT:   $ebp $T1 4 - ^ =
91; OBJ-NEXT: ]
92; OBJ:   }
93; OBJ: ]
94; OBJ: Subsection [
95; OBJ:   SubSectionType: Symbols (0xF1)
96; OBJ:   GlobalProcIdSym {
97; OBJ:     Kind: S_GPROC32_ID (0x1147)
98; OBJ:     Flags [ (0x81)
99; OBJ:       HasFP (0x1)
100; OBJ:       HasOptimizedDebugInfo (0x80)
101; OBJ:     ]
102; OBJ:     DisplayName: realign_with_csrs
103; OBJ:     LinkageName: _realign_with_csrs
104; OBJ:   }
105; 	The frame register for locals should be VFRAME, and EBP for parameters.
106; OBJ:   FrameProcSym {
107; OBJ:     Kind: S_FRAMEPROC (0x1012)
108; OBJ:     TotalFrameBytes: 0x14
109; OBJ:     LocalFramePtrReg: VFRAME (0x7536)
110; OBJ:     ParamFramePtrReg: EBP (0x16)
111; OBJ:   }
112; 	As seen in ASM, offset of x is 8.
113; OBJ:   LocalSym {
114; OBJ:     Kind: S_LOCAL (0x113E)
115; OBJ:     Type: int (0x74)
116; OBJ:     Flags [ (0x1)
117; OBJ:       IsParameter (0x1)
118; OBJ:     ]
119; OBJ:     VarName: x
120; OBJ:   }
121; OBJ:   DefRangeFramePointerRelSym {
122; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
123; OBJ:     Offset: 8
124; OBJ:   }
125; 	ESP is VFRAME - 16, ESP offset of 'a' is 4, so -12.
126; OBJ:   LocalSym {
127; OBJ:     Kind: S_LOCAL (0x113E)
128; OBJ:     Type: int (0x74)
129; OBJ:     Flags [ (0x0)
130; OBJ:     ]
131; OBJ:     VarName: a
132; OBJ:   }
133; OBJ:   DefRangeFramePointerRelSym {
134; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
135; OBJ:     Offset: -12
136; OBJ:   }
137; 	ESP is VFRAME - 16, ESP offset of 'force_alignment' is 8, so -8.
138; OBJ:   LocalSym {
139; OBJ:     Kind: S_LOCAL (0x113E)
140; OBJ:     Type: double (0x41)
141; OBJ:     Flags [ (0x0)
142; OBJ:     ]
143; OBJ:     VarName: force_alignment
144; OBJ:   }
145; OBJ:   DefRangeFramePointerRelSym {
146; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
147; OBJ:     Offset: -8
148; OBJ:   }
149; OBJ:   ProcEnd {
150; OBJ:     Kind: S_PROC_ID_END (0x114F)
151; OBJ:   }
152; OBJ: ]
153
154; ModuleID = 't.c'
155source_filename = "t.c"
156target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
157target triple = "i386-pc-windows-msvc19.14.26433"
158
159; Function Attrs: nounwind
160define dso_local i32 @realign_with_csrs(i32 %x) local_unnamed_addr #0 !dbg !8 {
161entry:
162  %x.addr = alloca i32, align 4
163  %a = alloca i32, align 4
164  %force_alignment = alloca double, align 8
165  store i32 %x, ptr %x.addr, align 4, !tbaa !17
166  call void @llvm.dbg.declare(metadata ptr %x.addr, metadata !13, metadata !DIExpression()), !dbg !21
167  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %a) #4, !dbg !22
168  call void @llvm.dbg.declare(metadata ptr %a, metadata !14, metadata !DIExpression()), !dbg !22
169  %call = tail call i32 @getval() #4, !dbg !22
170  store i32 %call, ptr %a, align 4, !dbg !22, !tbaa !17
171  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %force_alignment) #4, !dbg !23
172  call void @llvm.dbg.declare(metadata ptr %force_alignment, metadata !15, metadata !DIExpression()), !dbg !23
173  store double 4.200000e-01, ptr %force_alignment, align 8, !dbg !23, !tbaa !24
174  call void @usevals(ptr nonnull %a, ptr nonnull %x.addr, ptr nonnull %force_alignment) #4, !dbg !26
175  %0 = load i32, ptr %x.addr, align 4, !dbg !27, !tbaa !17
176  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %force_alignment) #4, !dbg !28
177  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %a) #4, !dbg !28
178  ret i32 %0, !dbg !27
179}
180
181; Function Attrs: nounwind readnone speculatable
182declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
183
184; Function Attrs: argmemonly nounwind
185declare void @llvm.lifetime.start.p0(i64, ptr nocapture) #2
186
187declare dso_local i32 @getval() local_unnamed_addr #3
188
189declare dso_local void @usevals(ptr, ptr, ptr) local_unnamed_addr #3
190
191; Function Attrs: argmemonly nounwind
192declare void @llvm.lifetime.end.p0(i64, ptr nocapture) #2
193
194attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "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" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
195attributes #1 = { nounwind readnone speculatable }
196attributes #2 = { argmemonly nounwind }
197attributes #3 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "frame-pointer"="none" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
198attributes #4 = { nounwind }
199
200!llvm.dbg.cu = !{!0}
201!llvm.module.flags = !{!3, !4, !5, !6}
202!llvm.ident = !{!7}
203
204!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 8.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
205!1 = !DIFile(filename: "t.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "a646950309d5d01d8087fc10fea33941")
206!2 = !{}
207!3 = !{i32 1, !"NumRegisterParameters", i32 0}
208!4 = !{i32 2, !"CodeView", i32 1}
209!5 = !{i32 2, !"Debug Info Version", i32 3}
210!6 = !{i32 1, !"wchar_size", i32 2}
211!7 = !{!"clang version 8.0.0 "}
212!8 = distinct !DISubprogram(name: "realign_with_csrs", scope: !1, file: !1, line: 3, type: !9, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !12)
213!9 = !DISubroutineType(types: !10)
214!10 = !{!11, !11}
215!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
216!12 = !{!13, !14, !15}
217!13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 3, type: !11)
218!14 = !DILocalVariable(name: "a", scope: !8, file: !1, line: 4, type: !11)
219!15 = !DILocalVariable(name: "force_alignment", scope: !8, file: !1, line: 5, type: !16, align: 64)
220!16 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
221!17 = !{!18, !18, i64 0}
222!18 = !{!"int", !19, i64 0}
223!19 = !{!"omnipotent char", !20, i64 0}
224!20 = !{!"Simple C/C++ TBAA"}
225!21 = !DILocation(line: 3, scope: !8)
226!22 = !DILocation(line: 4, scope: !8)
227!23 = !DILocation(line: 5, scope: !8)
228!24 = !{!25, !25, i64 0}
229!25 = !{!"double", !19, i64 0}
230!26 = !DILocation(line: 6, scope: !8)
231!27 = !DILocation(line: 7, scope: !8)
232!28 = !DILocation(line: 8, scope: !8)
233