Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6001,8 +6001,16 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();
++it) {
EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);
if (!(Cleanup && Cleanup->getCleanup()->isRedundantBeforeReturn()))
// Fake uses can be safely emitted immediately prior to the tail call, so
// we choose to emit them just before the call here.
if (Cleanup && Cleanup->isFakeUse()) {
CGBuilderTy::InsertPointGuard IPG(Builder);
Builder.SetInsertPoint(CI);
Cleanup->getCleanup()->Emit(*this, EHScopeStack::Cleanup::Flags());
} else if (!(Cleanup &&
Cleanup->getCleanup()->isRedundantBeforeReturn())) {
CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");
}
}
if (CI->getType()->isVoidTy())
Builder.CreateRetVoid();
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CodeGenCXX/fake-use-musttail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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);
}
Loading