1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes
2 ; RUN: opt -S -passes=argpromotion < %s | FileCheck %s
4 ; In the following tests, the call to @callee may invalidate ptr %test_c and so
5 ; prohibit removing loads of %test_c following the call, preventing Argument
6 ; Promotion of %test_c in the general case.
8 ; This is called by @caller_ptr_args, from which we cannot prove anything about
9 ; whether %test_c may alias %p and so we cannot promote %test_c.
11 define internal i32 @test_cannot_promote_1(ptr %p, ptr nocapture readonly %test_c) {
12 ; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_1
13 ; CHECK-SAME: (ptr [[P:%.*]], ptr nocapture readonly [[TEST_C:%.*]]) {
14 ; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 4
15 ; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])
16 ; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 4
17 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]
18 ; CHECK-NEXT: ret i32 [[SUM]]
20 %res = call i32 @callee(ptr %p, ptr %test_c)
22 %ltest_c = load i32, ptr %test_c
24 %sum = add i32 %ltest_c, %res
29 ; This is called by multiple callers, from which we can see that %test_c may
30 ; alias %p and so we cannot promote %test_c.
32 define internal i32 @test_cannot_promote_2(ptr %p, ptr nocapture readonly %test_c) {
33 ; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_2
34 ; CHECK-SAME: (ptr [[P:%.*]], ptr nocapture readonly [[TEST_C:%.*]]) {
35 ; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 4
36 ; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])
37 ; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 4
38 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]
39 ; CHECK-NEXT: ret i32 [[SUM]]
41 %res = call i32 @callee(ptr %p, ptr %test_c)
43 %ltest_c = load i32, ptr %test_c
45 %sum = add i32 %ltest_c, %res
50 ; This is called by @caller_safe_args_1, but also from @caller_aliased_args, so
51 ; we cannot promote %test_c.
53 define internal i32 @test_cannot_promote_3(ptr %p, ptr nocapture readonly %test_c) {
54 ; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_3
55 ; CHECK-SAME: (ptr [[P:%.*]], ptr nocapture readonly [[TEST_C:%.*]]) {
56 ; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 4
57 ; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])
58 ; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 4
59 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]
60 ; CHECK-NEXT: ret i32 [[SUM]]
62 %res = call i32 @callee(ptr %p, ptr %test_c)
64 %ltest_c = load i32, ptr %test_c
66 %sum = add i32 %ltest_c, %res
71 ; This is called only by @caller_safe_args_1, from which we can prove that
72 ; %test_c does not alias %p for any Call to the function, so we can promote it.
74 define internal i32 @test_can_promote_1(ptr %p, ptr nocapture readonly %test_c) {
75 ; CHECK-LABEL: define {{[^@]+}}@test_can_promote_1
76 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[TEST_C_0_VAL:%.*]]) {
77 ; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_0_VAL]])
78 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[TEST_C_0_VAL]], [[RES]]
79 ; CHECK-NEXT: ret i32 [[SUM]]
81 %res = call i32 @callee(ptr %p, ptr %test_c)
83 %ltest_c = load i32, ptr %test_c
85 %sum = add i32 %ltest_c, %res
90 ; This is called by multiple callers, from which we can prove that %test_c does
91 ; not alias %p for any Call to the function, so we can promote it.
93 define internal i32 @test_can_promote_2(ptr %p, ptr nocapture readonly %test_c) {
94 ; CHECK-LABEL: define {{[^@]+}}@test_can_promote_2
95 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[TEST_C_0_VAL:%.*]]) {
96 ; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_0_VAL]])
97 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[TEST_C_0_VAL]], [[RES]]
98 ; CHECK-NEXT: ret i32 [[SUM]]
100 %res = call i32 @callee(ptr %p, ptr %test_c)
102 %ltest_c = load i32, ptr %test_c
104 %sum = add i32 %ltest_c, %res
109 ; Called by @test_XXX
110 define internal i32 @callee(ptr %p, ptr nocapture readonly %callee_c) {
111 ; CHECK-LABEL: define {{[^@]+}}@callee
112 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[CALLEE_C_0_VAL:%.*]]) {
113 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[P]], align 4
114 ; CHECK-NEXT: [[SUM:%.*]] = add i32 [[A]], [[CALLEE_C_0_VAL]]
115 ; CHECK-NEXT: store i32 [[SUM]], ptr [[P]], align 4
116 ; CHECK-NEXT: ret i32 [[SUM]]
118 %a = load i32, ptr %p
120 %lcallee_c = load i32, ptr %callee_c
122 %sum = add i32 %a, %lcallee_c
124 store i32 %sum, ptr %p
129 ; Calls @test_cannot_promote_1
130 define i32 @caller_ptr_args(i64 %n, ptr %p1, ptr %p2) {
131 ; CHECK-LABEL: define {{[^@]+}}@caller_ptr_args
132 ; CHECK-SAME: (i64 [[N:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {
133 ; CHECK-NEXT: call void @memset(ptr [[P1]], i64 0, i64 [[N]])
134 ; CHECK-NEXT: store i32 5, ptr [[P2]], align 4
135 ; CHECK-NEXT: [[RES:%.*]] = call i32 @test_cannot_promote_1(ptr [[P1]], ptr [[P2]])
136 ; CHECK-NEXT: ret i32 [[RES]]
138 call void @memset(ptr %p1, i64 0, i64 %n)
142 %res = call i32 @test_cannot_promote_1(ptr %p1, ptr %p2)
147 ; Calls @test_cannot_promote_2
148 ; Calls @test_cannot_promote_3
149 define i32 @caller_aliased_args() {
150 ; CHECK-LABEL: define {{[^@]+}}@caller_aliased_args() {
151 ; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4
152 ; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4
153 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @test_cannot_promote_2(ptr [[CALLER_C]], ptr [[CALLER_C]])
154 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @test_cannot_promote_3(ptr [[CALLER_C]], ptr [[CALLER_C]])
155 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[RES1]], [[RES2]]
156 ; CHECK-NEXT: ret i32 [[RES]]
158 %caller_c = alloca i32
159 store i32 5, ptr %caller_c
161 %res1 = call i32 @test_cannot_promote_2(ptr %caller_c, ptr %caller_c)
162 %res2 = call i32 @test_cannot_promote_3(ptr %caller_c, ptr %caller_c)
164 %res = add i32 %res1, %res2
169 ; Calls @test_cannot_promote_3
170 ; Calls @test_can_promote_1
171 ; Calls @test_can_promote_2
172 define i32 @caller_safe_args_1(i64 %n) {
173 ; CHECK-LABEL: define {{[^@]+}}@caller_safe_args_1
174 ; CHECK-SAME: (i64 [[N:%.*]]) {
175 ; CHECK-NEXT: [[P:%.*]] = alloca [5 x double], i64 [[N]], align 8
176 ; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])
177 ; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4
178 ; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4
179 ; CHECK-NEXT: [[RES1:%.*]] = call i32 @test_cannot_promote_3(ptr [[P]], ptr [[CALLER_C]])
180 ; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4
181 ; CHECK-NEXT: [[RES2:%.*]] = call i32 @test_can_promote_1(ptr [[P]], i32 [[CALLER_C_VAL]])
182 ; CHECK-NEXT: [[CALLER_C_VAL1:%.*]] = load i32, ptr [[CALLER_C]], align 4
183 ; CHECK-NEXT: [[RES3:%.*]] = call i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL1]])
184 ; CHECK-NEXT: [[RES12:%.*]] = add i32 [[RES1]], [[RES2]]
185 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[RES12]], [[RES3]]
186 ; CHECK-NEXT: ret i32 [[RES]]
188 %p = alloca [5 x double], i64 %n
189 call void @memset(ptr %p, i64 0, i64 %n)
191 %caller_c = alloca i32
192 store i32 5, ptr %caller_c
194 %res1 = call i32 @test_cannot_promote_3(ptr %p, ptr %caller_c)
195 %res2 = call i32 @test_can_promote_1(ptr %p, ptr %caller_c)
196 %res3 = call i32 @test_can_promote_2(ptr %p, ptr %caller_c)
198 %res12 = add i32 %res1, %res2
199 %res = add i32 %res12, %res3
204 ; Calls @test_can_promote_2
205 define i32 @caller_safe_args_2(i64 %n, ptr %p) {
206 ; CHECK-LABEL: define {{[^@]+}}@caller_safe_args_2
207 ; CHECK-SAME: (i64 [[N:%.*]], ptr [[P:%.*]]) {
208 ; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])
209 ; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4
210 ; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4
211 ; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4
212 ; CHECK-NEXT: [[RES:%.*]] = call i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL]])
213 ; CHECK-NEXT: ret i32 [[RES]]
215 call void @memset(ptr %p, i64 0, i64 %n)
217 %caller_c = alloca i32
218 store i32 5, ptr %caller_c
220 %res = call i32 @test_can_promote_2(ptr %p, ptr %caller_c)
225 ; Invokes @test_cannot_promote_2
226 define i32 @caller_invoke_aliased_args() personality ptr @__gxx_personality_v0 {
227 ; CHECK-LABEL: define {{[^@]+}}@caller_invoke_aliased_args() personality ptr @__gxx_personality_v0 {
229 ; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4
230 ; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4
231 ; CHECK-NEXT: [[RES:%.*]] = invoke i32 @test_cannot_promote_2(ptr [[CALLER_C]], ptr [[CALLER_C]])
232 ; CHECK-NEXT: to label [[OUT:%.*]] unwind label [[CPAD:%.*]]
234 ; CHECK-NEXT: ret i32 [[RES]]
236 ; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }
237 ; CHECK-NEXT: catch ptr @_ZTIi
238 ; CHECK-NEXT: ret i32 -1
241 %caller_c = alloca i32
242 store i32 5, ptr %caller_c
244 %res = invoke i32 @test_cannot_promote_2(ptr %caller_c, ptr %caller_c)
245 to label %out unwind label %cpad
251 %exn = landingpad { ptr, i32 }
256 ; Invokes @test_can_promote_2
257 define i32 @caller_invoke_safe_args(i64 %n) personality ptr @__gxx_personality_v0 {
258 ; CHECK-LABEL: define {{[^@]+}}@caller_invoke_safe_args
259 ; CHECK-SAME: (i64 [[N:%.*]]) personality ptr @__gxx_personality_v0 {
261 ; CHECK-NEXT: [[P:%.*]] = alloca [5 x double], i64 [[N]], align 8
262 ; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])
263 ; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4
264 ; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4
265 ; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4
266 ; CHECK-NEXT: [[RES:%.*]] = invoke i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL]])
267 ; CHECK-NEXT: to label [[OUT:%.*]] unwind label [[CPAD:%.*]]
269 ; CHECK-NEXT: ret i32 [[RES]]
271 ; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }
272 ; CHECK-NEXT: catch ptr @_ZTIi
273 ; CHECK-NEXT: ret i32 -1
276 %p = alloca [5 x double], i64 %n
277 call void @memset(ptr %p, i64 0, i64 %n)
279 %caller_c = alloca i32
280 store i32 5, ptr %caller_c
282 %res = invoke i32 @test_can_promote_2(ptr %p, ptr %caller_c)
283 to label %out unwind label %cpad
289 %exn = landingpad { ptr, i32 }
294 declare void @memset(ptr, i64, i64)
295 declare i32 @__gxx_personality_v0(...)
297 @_ZTIi = external constant ptr