Files
clang-p2996/llvm/test/Transforms/GlobalOpt/atexit-dtor.ll
Max Winkler ce3485a0cd [llvm][GlobalOpt] Remove empty atexit destructors/handlers (#88836)
https://godbolt.org/z/frjhqMKqc for an example.

Removal of allocations due to empty `__cxa_atexit` destructor calls is
done by the following globalopt pass.
This pass currently does not look for `atexit` handlers generated for
platforms that do not use `__cxa_atexit`.
By default Win32 and AIX use `atexit`.

I don't see an easy way to only remove `atexit` calls that the compiler
generated without looking at the generated mangled name of the atexit
handler that is being registered.
However we can easily remove all `atexit` calls that register empty
handlers since it is trivial to ensure the removed call still returns
`0` which is the value for success.
2024-04-29 20:29:34 -04:00

55 lines
1.9 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt < %s -S -passes=globalopt | FileCheck %s
declare dso_local i32 @atexit(ptr)
define dso_local void @empty_atexit_handler() {
; CHECK-LABEL: define dso_local void @empty_atexit_handler() local_unnamed_addr {
; CHECK-NEXT: ret void
;
ret void
}
; Check that `atexit` is removed if the handler is empty.
; Check that a removed `atexit` call returns `0` which is the value that denotes success.
define dso_local noundef i32 @register_atexit_handler() {
; CHECK-LABEL: define dso_local noundef i32 @register_atexit_handler() local_unnamed_addr {
; CHECK-NEXT: ret i32 0
;
%1 = call i32 @atexit(ptr @empty_atexit_handler)
ret i32 %1
}
declare dso_local void @declared_atexit_handler()
; Check that an atexit handler with only a declaration is not removed.
define dso_local noundef i32 @register_declared_atexit_handler() {
; CHECK-LABEL: define dso_local noundef i32 @register_declared_atexit_handler() local_unnamed_addr {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @atexit(ptr @declared_atexit_handler)
; CHECK-NEXT: ret i32 [[TMP1]]
;
%1 = call i32 @atexit(ptr @declared_atexit_handler)
ret i32 %1
}
declare dso_local void @external_exit_func()
define dso_local void @nonempty_atexit_handler() {
; CHECK-LABEL: define dso_local void @nonempty_atexit_handler() {
; CHECK-NEXT: call void @external_exit_func()
; CHECK-NEXT: ret void
;
call void @external_exit_func()
ret void
}
; Check that an atexit handler that consists of any instructions other than `ret` is considered nonempty and not removed.
define dso_local noundef i32 @register_nonempty_atexit_handler() {
; CHECK-LABEL: define dso_local noundef i32 @register_nonempty_atexit_handler() local_unnamed_addr {
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @atexit(ptr @nonempty_atexit_handler)
; CHECK-NEXT: ret i32 [[TMP1]]
;
%1 = call i32 @atexit(ptr @nonempty_atexit_handler)
ret i32 %1
}