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