llvm-project/clang/test/CodeGenCXX/fake-use-musttail.cpp
Stephen Tozer 2d00c73003
[Clang][CodeGen] Emit fake uses before musttail calls (#136867)
Fixes the issue reported following the merge of #118026.

When a valid `musttail` call is made, the function it is made from must
return immediately after the call; if there are any cleanups left in the
function, then an error is triggered. This is not necessary for fake
uses however - it is perfectly valid to simply emit the fake use
"cleanup" code before the tail call, and indeed LLVM will automatically
move any fake uses following a tail call to come before the tail call.

Therefore, this patch specifically choose to handle fake use cleanups
when a musttail call is present by simply emitting them immediately
before the call.
2025-04-25 11:47:38 +01:00

28 lines
1.1 KiB
C++

// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -fextend-variable-liveness -o - %s | FileCheck %s
/// Tests that when we have fake uses in a function ending in a musttail call,
/// we emit the fake uses and their corresponding loads immediately prior to the
/// tail call.
extern "C" char *bar(int *);
// CHECK-LABEL: define dso_local ptr @foo(
// CHECK-SAME: ptr noundef [[E:%.*]])
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[E_ADDR:%.*]] = alloca ptr, align 8
// CHECK-NEXT: store ptr [[E]], ptr [[E_ADDR]], align 8
// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[E_ADDR]], align 8
// CHECK-NEXT: [[FAKE_USE:%.*]] = load ptr, ptr [[E_ADDR]]
// CHECK-NEXT: notail call void (...) @llvm.fake.use(ptr [[FAKE_USE]])
// CHECK-NEXT: [[CALL:%.*]] = musttail call ptr @bar(ptr noundef [[TMP0]])
// CHECK-NEXT: ret ptr [[CALL]]
// CHECK: [[BB1:.*:]]
// CHECK-NEXT: [[FAKE_USE1:%.*]] = load ptr, ptr [[E_ADDR]]
// CHECK-NEXT: notail call void (...) @llvm.fake.use(ptr [[FAKE_USE1]])
// CHECK-NEXT: ret ptr undef
//
extern "C" const char *foo(int *e) {
[[clang::musttail]] return bar(e);
}