1; RUN: opt < %s -inline -S | FileCheck %s
2; RUN: opt < %s -passes='cgscc(inline)' -S | FileCheck %s
3
4; Inlining a byval struct should cause an explicit copy into an alloca.
5
6	%struct.ss = type { i32, i64 }
7@.str = internal constant [10 x i8] c"%d, %lld\0A\00"		; <[10 x i8]*> [#uses=1]
8
9define internal void @f(%struct.ss* byval  %b) nounwind  {
10entry:
11	%tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0		; <i32*> [#uses=2]
12	%tmp1 = load i32, i32* %tmp, align 4		; <i32> [#uses=1]
13	%tmp2 = add i32 %tmp1, 1		; <i32> [#uses=1]
14	store i32 %tmp2, i32* %tmp, align 4
15	ret void
16}
17
18declare i32 @printf(i8*, ...) nounwind
19
20define i32 @test1() nounwind  {
21entry:
22	%S = alloca %struct.ss		; <%struct.ss*> [#uses=4]
23	%tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0		; <i32*> [#uses=1]
24	store i32 1, i32* %tmp1, align 8
25	%tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1		; <i64*> [#uses=1]
26	store i64 2, i64* %tmp4, align 4
27	call void @f( %struct.ss* byval  %S ) nounwind
28	ret i32 0
29; CHECK: @test1()
30; CHECK: %S1 = alloca %struct.ss
31; CHECK: %S = alloca %struct.ss
32; CHECK: call void @llvm.memcpy
33; CHECK: ret i32 0
34}
35
36; Inlining a byval struct should NOT cause an explicit copy
37; into an alloca if the function is readonly
38
39define internal i32 @f2(%struct.ss* byval  %b) nounwind readonly {
40entry:
41	%tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0		; <i32*> [#uses=2]
42	%tmp1 = load i32, i32* %tmp, align 4		; <i32> [#uses=1]
43	%tmp2 = add i32 %tmp1, 1		; <i32> [#uses=1]
44	ret i32 %tmp2
45}
46
47define i32 @test2() nounwind  {
48entry:
49	%S = alloca %struct.ss		; <%struct.ss*> [#uses=4]
50	%tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0		; <i32*> [#uses=1]
51	store i32 1, i32* %tmp1, align 8
52	%tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1		; <i64*> [#uses=1]
53	store i64 2, i64* %tmp4, align 4
54	%X = call i32 @f2( %struct.ss* byval  %S ) nounwind
55	ret i32 %X
56; CHECK: @test2()
57; CHECK: %S = alloca %struct.ss
58; CHECK-NOT: call void @llvm.memcpy
59; CHECK: ret i32
60}
61
62
63; Inlining a byval with an explicit alignment needs to use *at least* that
64; alignment on the generated alloca.
65; PR8769
66declare void @g3(%struct.ss* %p)
67
68define internal void @f3(%struct.ss* byval align 64 %b) nounwind {
69   call void @g3(%struct.ss* %b)  ;; Could make alignment assumptions!
70   ret void
71}
72
73define void @test3() nounwind  {
74entry:
75	%S = alloca %struct.ss, align 1  ;; May not be aligned.
76	call void @f3( %struct.ss* byval align 64 %S) nounwind
77	ret void
78; CHECK: @test3()
79; CHECK: %S1 = alloca %struct.ss, align 64
80; CHECK: %S = alloca %struct.ss
81; CHECK: call void @llvm.memcpy
82; CHECK: call void @g3(%struct.ss* %S1)
83; CHECK: ret void
84}
85
86
87; Inlining a byval struct should NOT cause an explicit copy
88; into an alloca if the function is readonly, but should increase an alloca's
89; alignment to satisfy an explicit alignment request.
90
91define internal i32 @f4(%struct.ss* byval align 64 %b) nounwind readonly {
92        call void @g3(%struct.ss* %b)
93	ret i32 4
94}
95
96define i32 @test4() nounwind  {
97entry:
98	%S = alloca %struct.ss, align 2		; <%struct.ss*> [#uses=4]
99	%X = call i32 @f4( %struct.ss* byval align 64 %S ) nounwind
100	ret i32 %X
101; CHECK: @test4()
102; CHECK: %S = alloca %struct.ss, align 64
103; CHECK-NOT: call void @llvm.memcpy
104; CHECK: call void @g3
105; CHECK: ret i32 4
106}
107
108%struct.S0 = type { i32 }
109
110@b = global %struct.S0 { i32 1 }, align 4
111@a = common global i32 0, align 4
112
113define internal void @f5(%struct.S0* byval nocapture readonly align 4 %p) {
114entry:
115	store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
116	%f2 = getelementptr inbounds %struct.S0, %struct.S0* %p, i64 0, i32 0
117	%0 = load i32, i32* %f2, align 4
118	store i32 %0, i32* @a, align 4
119	ret void
120}
121
122define i32 @test5() {
123entry:
124	tail call void @f5(%struct.S0* byval align 4 @b)
125	%0 = load i32, i32* @a, align 4
126	ret i32 %0
127; CHECK: @test5()
128; CHECK: store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
129; CHECK-NOT: load i32, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
130}
131