Skip to content

Commit ea864c9

Browse files
author
Melanie Blower
committed
[clang][patch][NFC] Refactor calculation of FunctionDecl to avoid duplicate code
1 parent cf78995 commit ea864c9

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,9 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
710710

711711
DidCallStackSave = false;
712712
CurCodeDecl = D;
713-
if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D))
714-
if (FD->usesSEHTry())
715-
CurSEHParent = FD;
713+
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
714+
if (FD && FD->usesSEHTry())
715+
CurSEHParent = FD;
716716
CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
717717
FnRetTy = RetTy;
718718
CurFn = Fn;
@@ -803,10 +803,9 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
803803
// are not aware of how to move the extra UBSan instructions across the split
804804
// coroutine boundaries.
805805
if (D && SanOpts.has(SanitizerKind::Null))
806-
if (const auto *FD = dyn_cast<FunctionDecl>(D))
807-
if (FD->getBody() &&
808-
FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
809-
SanOpts.Mask &= ~SanitizerKind::Null;
806+
if (FD && FD->getBody() &&
807+
FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
808+
SanOpts.Mask &= ~SanitizerKind::Null;
810809

811810
// Apply xray attributes to the function (as a string, for now)
812811
bool AlwaysXRayAttr = false;
@@ -896,32 +895,27 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
896895
if (D && D->hasAttr<NoProfileFunctionAttr>())
897896
Fn->addFnAttr(llvm::Attribute::NoProfile);
898897

899-
if (getLangOpts().OpenCL) {
898+
if (FD && getLangOpts().OpenCL) {
900899
// Add metadata for a kernel function.
901-
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
902-
EmitOpenCLKernelMetadata(FD, Fn);
900+
EmitOpenCLKernelMetadata(FD, Fn);
903901
}
904902

905903
// If we are checking function types, emit a function type signature as
906904
// prologue data.
907-
if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) {
908-
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
909-
if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
910-
// Remove any (C++17) exception specifications, to allow calling e.g. a
911-
// noexcept function through a non-noexcept pointer.
912-
auto ProtoTy =
913-
getContext().getFunctionTypeWithExceptionSpec(FD->getType(),
914-
EST_None);
915-
llvm::Constant *FTRTTIConst =
916-
CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true);
917-
llvm::Constant *FTRTTIConstEncoded =
918-
EncodeAddrForUseInPrologue(Fn, FTRTTIConst);
919-
llvm::Constant *PrologueStructElems[] = {PrologueSig,
920-
FTRTTIConstEncoded};
921-
llvm::Constant *PrologueStructConst =
922-
llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true);
923-
Fn->setPrologueData(PrologueStructConst);
924-
}
905+
if (FD && getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) {
906+
if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
907+
// Remove any (C++17) exception specifications, to allow calling e.g. a
908+
// noexcept function through a non-noexcept pointer.
909+
auto ProtoTy = getContext().getFunctionTypeWithExceptionSpec(
910+
FD->getType(), EST_None);
911+
llvm::Constant *FTRTTIConst =
912+
CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true);
913+
llvm::Constant *FTRTTIConstEncoded =
914+
EncodeAddrForUseInPrologue(Fn, FTRTTIConst);
915+
llvm::Constant *PrologueStructElems[] = {PrologueSig, FTRTTIConstEncoded};
916+
llvm::Constant *PrologueStructConst =
917+
llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true);
918+
Fn->setPrologueData(PrologueStructConst);
925919
}
926920
}
927921

@@ -948,25 +942,22 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
948942
// kernels cannot include RTTI information, exception classes,
949943
// recursive code, virtual functions or make use of C++ libraries that
950944
// are not compiled for the device.
951-
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
952-
if ((getLangOpts().CPlusPlus && FD->isMain()) || getLangOpts().OpenCL ||
953-
getLangOpts().SYCLIsDevice ||
954-
(getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>()))
955-
Fn->addFnAttr(llvm::Attribute::NoRecurse);
956-
}
945+
if (FD && ((getLangOpts().CPlusPlus && FD->isMain()) ||
946+
getLangOpts().OpenCL || getLangOpts().SYCLIsDevice ||
947+
(getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>())))
948+
Fn->addFnAttr(llvm::Attribute::NoRecurse);
957949

958-
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
950+
if (FD) {
959951
Builder.setIsFPConstrained(FD->hasAttr<StrictFPAttr>());
960952
if (FD->hasAttr<StrictFPAttr>())
961953
Fn->addFnAttr(llvm::Attribute::StrictFP);
962954
}
963955

964956
// If a custom alignment is used, force realigning to this alignment on
965957
// any main function which certainly will need it.
966-
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
967-
if ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
968-
CGM.getCodeGenOpts().StackAlignment)
969-
Fn->addFnAttr("stackrealign");
958+
if (FD && ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
959+
CGM.getCodeGenOpts().StackAlignment))
960+
Fn->addFnAttr("stackrealign");
970961

971962
llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
972963

@@ -993,7 +984,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
993984
// such as 'this' and 'vtt', show up in the debug info. Preserve the calling
994985
// convention.
995986
CallingConv CC = CallingConv::CC_C;
996-
if (auto *FD = dyn_cast_or_null<FunctionDecl>(D))
987+
if (FD)
997988
if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>())
998989
CC = SrcFnTy->getCallConv();
999990
SmallVector<QualType, 16> ArgTypes;

0 commit comments

Comments
 (0)