blob: aeb9964b2171e8e0a7d7bb80f2914532e9370457 [file] [log] [blame]
Chris Lattner6f3ddbd2010-12-20 07:38:24 +00001; RUN: opt < %s -inline -S | FileCheck %s
Chandler Carruth625038d2016-12-27 07:18:43 +00002; RUN: opt < %s -passes='cgscc(inline)' -S | FileCheck %s
Chris Lattner908117b2008-01-11 06:09:30 +00003
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:
David Blaikie79e6c742015-02-27 19:29:02 +000011 %tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0 ; <i32*> [#uses=2]
David Blaikiea79ac142015-02-27 21:17:42 +000012 %tmp1 = load i32, i32* %tmp, align 4 ; <i32> [#uses=1]
Chris Lattner908117b2008-01-11 06:09:30 +000013 %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
Chris Lattnera9a5c592010-12-20 07:39:57 +000020define i32 @test1() nounwind {
Chris Lattner908117b2008-01-11 06:09:30 +000021entry:
22 %S = alloca %struct.ss ; <%struct.ss*> [#uses=4]
David Blaikie79e6c742015-02-27 19:29:02 +000023 %tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0 ; <i32*> [#uses=1]
Chris Lattner908117b2008-01-11 06:09:30 +000024 store i32 1, i32* %tmp1, align 8
David Blaikie79e6c742015-02-27 19:29:02 +000025 %tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1 ; <i64*> [#uses=1]
Chris Lattner908117b2008-01-11 06:09:30 +000026 store i64 2, i64* %tmp4, align 4
27 call void @f( %struct.ss* byval %S ) nounwind
28 ret i32 0
David Majnemer120f4a02013-11-03 12:22:13 +000029; CHECK: @test1()
Chris Lattner00997442010-12-20 07:57:41 +000030; CHECK: %S1 = alloca %struct.ss
31; CHECK: %S = alloca %struct.ss
Chris Lattner6f3ddbd2010-12-20 07:38:24 +000032; CHECK: call void @llvm.memcpy
Chris Lattnera9a5c592010-12-20 07:39:57 +000033; 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:
David Blaikie79e6c742015-02-27 19:29:02 +000041 %tmp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0 ; <i32*> [#uses=2]
David Blaikiea79ac142015-02-27 21:17:42 +000042 %tmp1 = load i32, i32* %tmp, align 4 ; <i32> [#uses=1]
Chris Lattnera9a5c592010-12-20 07:39:57 +000043 %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]
David Blaikie79e6c742015-02-27 19:29:02 +000050 %tmp1 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 0 ; <i32*> [#uses=1]
Chris Lattnera9a5c592010-12-20 07:39:57 +000051 store i32 1, i32* %tmp1, align 8
David Blaikie79e6c742015-02-27 19:29:02 +000052 %tmp4 = getelementptr %struct.ss, %struct.ss* %S, i32 0, i32 1 ; <i64*> [#uses=1]
Chris Lattnera9a5c592010-12-20 07:39:57 +000053 store i64 2, i64* %tmp4, align 4
54 %X = call i32 @f2( %struct.ss* byval %S ) nounwind
55 ret i32 %X
David Majnemer120f4a02013-11-03 12:22:13 +000056; CHECK: @test2()
Chris Lattnera9a5c592010-12-20 07:39:57 +000057; CHECK: %S = alloca %struct.ss
58; CHECK-NOT: call void @llvm.memcpy
59; CHECK: ret i32
Chris Lattner908117b2008-01-11 06:09:30 +000060}
Chris Lattner73946802010-12-20 07:45:28 +000061
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
David Majnemer120f4a02013-11-03 12:22:13 +000078; CHECK: @test3()
Chris Lattner00997442010-12-20 07:57:41 +000079; CHECK: %S1 = alloca %struct.ss, align 64
Chris Lattner73946802010-12-20 07:45:28 +000080; CHECK: %S = alloca %struct.ss
81; CHECK: call void @llvm.memcpy
Chris Lattner00997442010-12-20 07:57:41 +000082; CHECK: call void @g3(%struct.ss* %S1)
Chris Lattner73946802010-12-20 07:45:28 +000083; CHECK: ret void
84}
Chris Lattner0f114952010-12-20 08:10:40 +000085
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
David Majnemer120f4a02013-11-03 12:22:13 +0000101; CHECK: @test4()
Chris Lattner0f114952010-12-20 08:10:40 +0000102; 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
David Majnemer120f4a02013-11-03 12:22:13 +0000108%struct.S0 = type { i32 }
Tom Stellardbc7d87f2013-10-24 16:38:33 +0000109
David Majnemer120f4a02013-11-03 12:22:13 +0000110@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) {
Tom Stellardbc7d87f2013-10-24 16:38:33 +0000114entry:
David Blaikief72d05b2015-03-13 18:20:45 +0000115 store i32 0, i32* getelementptr inbounds (%struct.S0, %struct.S0* @b, i64 0, i32 0), align 4
David Blaikie79e6c742015-02-27 19:29:02 +0000116 %f2 = getelementptr inbounds %struct.S0, %struct.S0* %p, i64 0, i32 0
David Blaikiea79ac142015-02-27 21:17:42 +0000117 %0 = load i32, i32* %f2, align 4
David Majnemer120f4a02013-11-03 12:22:13 +0000118 store i32 %0, i32* @a, align 4
119 ret void
Tom Stellardbc7d87f2013-10-24 16:38:33 +0000120}
121
David Majnemer120f4a02013-11-03 12:22:13 +0000122define i32 @test5() {
Tom Stellardbc7d87f2013-10-24 16:38:33 +0000123entry:
David Majnemer120f4a02013-11-03 12:22:13 +0000124 tail call void @f5(%struct.S0* byval align 4 @b)
David Blaikiea79ac142015-02-27 21:17:42 +0000125 %0 = load i32, i32* @a, align 4
David Majnemer120f4a02013-11-03 12:22:13 +0000126 ret i32 %0
127; CHECK: @test5()
David Blaikief72d05b2015-03-13 18:20:45 +0000128; 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
Tom Stellardbc7d87f2013-10-24 16:38:33 +0000130}