Skip to content

Commit 4da4fac

Browse files
authored
[ArgPromotion] Consider InvokeInst in Caller alias analysis (#110335)
Check that all users of a Function are CallBase rather than CallInst when performing alias analysis using actual arguments in the calling function, as this check is also valid for Invoke instructions. This allows replacing the existing check with an assert, as the Function only being used by CallBase derived instructions is a precondition of the transform. This addresses post-commit review on #106216.
1 parent 47c8b95 commit 4da4fac

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

llvm/lib/Transforms/IPO/ArgumentPromotion.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,7 @@ static bool isArgUnmodifiedByAllCalls(Argument *Arg,
491491
FunctionAnalysisManager &FAM) {
492492
for (User *U : Arg->getParent()->users()) {
493493

494-
// Bail if we find an unexpected (non CallInst) use of the function.
495-
auto *Call = dyn_cast<CallInst>(U);
496-
if (!Call)
497-
return false;
494+
auto *Call = cast<CallBase>(U);
498495

499496
MemoryLocation Loc =
500497
MemoryLocation::getForArgument(Call, Arg->getArgNo(), nullptr);

llvm/test/Transforms/ArgumentPromotion/actual-arguments.ll

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ define internal i32 @test_cannot_promote_1(ptr %p, ptr nocapture readonly %test_
2626
ret i32 %sum
2727
}
2828

29-
; This is called by @caller_aliased_args, from which we can see that %test_c may
29+
; This is called by multiple callers, from which we can see that %test_c may
3030
; alias %p and so we cannot promote %test_c.
3131
;
3232
define internal i32 @test_cannot_promote_2(ptr %p, ptr nocapture readonly %test_c) {
@@ -87,9 +87,8 @@ define internal i32 @test_can_promote_1(ptr %p, ptr nocapture readonly %test_c)
8787
ret i32 %sum
8888
}
8989

90-
; This is called by multiple callers (@caller_safe_args_1, @caller_safe_args_2),
91-
; from which we can prove that %test_c does not alias %p for any Call to the
92-
; function, so we can promote it.
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.
9392
;
9493
define internal i32 @test_can_promote_2(ptr %p, ptr nocapture readonly %test_c) {
9594
; CHECK-LABEL: define {{[^@]+}}@test_can_promote_2
@@ -223,4 +222,76 @@ define i32 @caller_safe_args_2(i64 %n, ptr %p) {
223222
ret i32 %res
224223
}
225224

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 {
228+
; CHECK-NEXT: entry:
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:%.*]]
233+
; CHECK: out:
234+
; CHECK-NEXT: ret i32 [[RES]]
235+
; CHECK: cpad:
236+
; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }
237+
; CHECK-NEXT: catch ptr @_ZTIi
238+
; CHECK-NEXT: ret i32 -1
239+
;
240+
entry:
241+
%caller_c = alloca i32
242+
store i32 5, ptr %caller_c
243+
244+
%res = invoke i32 @test_cannot_promote_2(ptr %caller_c, ptr %caller_c)
245+
to label %out unwind label %cpad
246+
247+
out:
248+
ret i32 %res
249+
250+
cpad:
251+
%exn = landingpad { ptr, i32 }
252+
catch ptr @_ZTIi
253+
ret i32 -1
254+
}
255+
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 {
260+
; CHECK-NEXT: entry:
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:%.*]]
268+
; CHECK: out:
269+
; CHECK-NEXT: ret i32 [[RES]]
270+
; CHECK: cpad:
271+
; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }
272+
; CHECK-NEXT: catch ptr @_ZTIi
273+
; CHECK-NEXT: ret i32 -1
274+
;
275+
entry:
276+
%p = alloca [5 x double], i64 %n
277+
call void @memset(ptr %p, i64 0, i64 %n)
278+
279+
%caller_c = alloca i32
280+
store i32 5, ptr %caller_c
281+
282+
%res = invoke i32 @test_can_promote_2(ptr %p, ptr %caller_c)
283+
to label %out unwind label %cpad
284+
285+
out:
286+
ret i32 %res
287+
288+
cpad:
289+
%exn = landingpad { ptr, i32 }
290+
catch ptr @_ZTIi
291+
ret i32 -1
292+
}
293+
226294
declare void @memset(ptr, i64, i64)
295+
declare i32 @__gxx_personality_v0(...)
296+
297+
@_ZTIi = external constant ptr

0 commit comments

Comments
 (0)