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, double *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, double *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 *, double *);
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:     DisplayName: realign_with_csrs
99; OBJ:     LinkageName: _realign_with_csrs
100; OBJ:   }
101; 	The frame register for locals should be VFRAME, and EBP for parameters.
102; OBJ:   FrameProcSym {
103; OBJ:     Kind: S_FRAMEPROC (0x1012)
104; OBJ:     TotalFrameBytes: 0x14
105; OBJ:     LocalFramePtrReg: VFRAME (0x7536)
106; OBJ:     ParamFramePtrReg: EBP (0x16)
107; OBJ:   }
108; 	As seen in ASM, offset of x is 8.
109; OBJ:   LocalSym {
110; OBJ:     Kind: S_LOCAL (0x113E)
111; OBJ:     Type: int (0x74)
112; OBJ:     Flags [ (0x1)
113; OBJ:       IsParameter (0x1)
114; OBJ:     ]
115; OBJ:     VarName: x
116; OBJ:   }
117; OBJ:   DefRangeFramePointerRelSym {
118; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
119; OBJ:     Offset: 8
120; OBJ:   }
121; 	ESP is VFRAME - 16, ESP offset of 'a' is 4, so -12.
122; OBJ:   LocalSym {
123; OBJ:     Kind: S_LOCAL (0x113E)
124; OBJ:     Type: int (0x74)
125; OBJ:     Flags [ (0x0)
126; OBJ:     ]
127; OBJ:     VarName: a
128; OBJ:   }
129; OBJ:   DefRangeFramePointerRelSym {
130; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
131; OBJ:     Offset: -12
132; OBJ:   }
133; 	ESP is VFRAME - 16, ESP offset of 'force_alignment' is 8, so -8.
134; OBJ:   LocalSym {
135; OBJ:     Kind: S_LOCAL (0x113E)
136; OBJ:     Type: double (0x41)
137; OBJ:     Flags [ (0x0)
138; OBJ:     ]
139; OBJ:     VarName: force_alignment
140; OBJ:   }
141; OBJ:   DefRangeFramePointerRelSym {
142; OBJ:     Kind: S_DEFRANGE_FRAMEPOINTER_REL (0x1142)
143; OBJ:     Offset: -8
144; OBJ:   }
145; OBJ:   ProcEnd {
146; OBJ:     Kind: S_PROC_ID_END (0x114F)
147; OBJ:   }
148; OBJ: ]
149
150; ModuleID = 't.c'
151source_filename = "t.c"
152target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
153target triple = "i386-pc-windows-msvc19.14.26433"
154
155; Function Attrs: nounwind
156define dso_local i32 @realign_with_csrs(i32 %x) local_unnamed_addr #0 !dbg !8 {
157entry:
158  %x.addr = alloca i32, align 4
159  %a = alloca i32, align 4
160  %force_alignment = alloca double, align 8
161  store i32 %x, i32* %x.addr, align 4, !tbaa !17
162  call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !13, metadata !DIExpression()), !dbg !21
163  %0 = bitcast i32* %a to i8*, !dbg !22
164  call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0) #4, !dbg !22
165  call void @llvm.dbg.declare(metadata i32* %a, metadata !14, metadata !DIExpression()), !dbg !22
166  %call = tail call i32 @getval() #4, !dbg !22
167  store i32 %call, i32* %a, align 4, !dbg !22, !tbaa !17
168  %1 = bitcast double* %force_alignment to i8*, !dbg !23
169  call void @llvm.lifetime.start.p0i8(i64 8, i8* nonnull %1) #4, !dbg !23
170  call void @llvm.dbg.declare(metadata double* %force_alignment, metadata !15, metadata !DIExpression()), !dbg !23
171  store double 4.200000e-01, double* %force_alignment, align 8, !dbg !23, !tbaa !24
172  call void @usevals(i32* nonnull %a, i32* nonnull %x.addr, double* nonnull %force_alignment) #4, !dbg !26
173  %2 = load i32, i32* %x.addr, align 4, !dbg !27, !tbaa !17
174  call void @llvm.lifetime.end.p0i8(i64 8, i8* nonnull %1) #4, !dbg !28
175  call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0) #4, !dbg !28
176  ret i32 %2, !dbg !27
177}
178
179; Function Attrs: nounwind readnone speculatable
180declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
181
182; Function Attrs: argmemonly nounwind
183declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) #2
184
185declare dso_local i32 @getval() local_unnamed_addr #3
186
187declare dso_local void @usevals(i32*, i32*, double*) local_unnamed_addr #3
188
189; Function Attrs: argmemonly nounwind
190declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) #2
191
192attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "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" }
193attributes #1 = { nounwind readnone speculatable }
194attributes #2 = { argmemonly nounwind }
195attributes #3 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "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" }
196attributes #4 = { nounwind }
197
198!llvm.dbg.cu = !{!0}
199!llvm.module.flags = !{!3, !4, !5, !6}
200!llvm.ident = !{!7}
201
202!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)
203!1 = !DIFile(filename: "t.c", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "a646950309d5d01d8087fc10fea33941")
204!2 = !{}
205!3 = !{i32 1, !"NumRegisterParameters", i32 0}
206!4 = !{i32 2, !"CodeView", i32 1}
207!5 = !{i32 2, !"Debug Info Version", i32 3}
208!6 = !{i32 1, !"wchar_size", i32 2}
209!7 = !{!"clang version 8.0.0 "}
210!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)
211!9 = !DISubroutineType(types: !10)
212!10 = !{!11, !11}
213!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
214!12 = !{!13, !14, !15}
215!13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 3, type: !11)
216!14 = !DILocalVariable(name: "a", scope: !8, file: !1, line: 4, type: !11)
217!15 = !DILocalVariable(name: "force_alignment", scope: !8, file: !1, line: 5, type: !16, align: 64)
218!16 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
219!17 = !{!18, !18, i64 0}
220!18 = !{!"int", !19, i64 0}
221!19 = !{!"omnipotent char", !20, i64 0}
222!20 = !{!"Simple C/C++ TBAA"}
223!21 = !DILocation(line: 3, scope: !8)
224!22 = !DILocation(line: 4, scope: !8)
225!23 = !DILocation(line: 5, scope: !8)
226!24 = !{!25, !25, i64 0}
227!25 = !{!"double", !19, i64 0}
228!26 = !DILocation(line: 6, scope: !8)
229!27 = !DILocation(line: 7, scope: !8)
230!28 = !DILocation(line: 8, scope: !8)
231