From 46cf82532cf1502b6320517a2ef42d8c435e598a Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Mon, 16 Aug 2021 16:29:20 -0700 Subject: [PATCH] [NFC] Replace Function handling of attributes with less confusing calls To avoid magic constants and confusing indexes. --- clang/lib/CodeGen/CodeGenModule.cpp | 8 +++----- llvm/include/llvm/IR/Function.h | 5 +++++ llvm/lib/IR/Core.cpp | 2 +- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 2 +- llvm/lib/Target/ARM/ARMISelLowering.cpp | 2 +- llvm/lib/Target/X86/X86ISelLowering.cpp | 2 +- llvm/lib/Transforms/IPO/FunctionAttrs.cpp | 4 ++-- llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp | 2 +- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp | 3 +-- llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 4 ++-- llvm/lib/Transforms/Utils/ModuleUtils.cpp | 2 +- 11 files changed, 19 insertions(+), 17 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ebd3cc7..0a66024 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2118,7 +2118,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && "unexpected this return"); - F->addAttribute(1, llvm::Attribute::Returned); + F->addParamAttr(0, llvm::Attribute::Returned); } // Only a few attributes are set on declarations; these may later be @@ -2144,15 +2144,13 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, assert(HasBody && "Inline builtin declarations should always have an " "available body!"); if (shouldEmitFunction(FDBody)) - F->addAttribute(llvm::AttributeList::FunctionIndex, - llvm::Attribute::NoBuiltin); + F->addFnAttr(llvm::Attribute::NoBuiltin); } if (FD->isReplaceableGlobalAllocationFunction()) { // A replaceable global allocation function does not act like a builtin by // default, only if it is invoked by a new-expression or delete-expression. - F->addAttribute(llvm::AttributeList::FunctionIndex, - llvm::Attribute::NoBuiltin); + F->addFnAttr(llvm::Attribute::NoBuiltin); } if (isa(FD) || isa(FD)) diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index f07e577..27a1d12 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -251,6 +251,11 @@ public: /// Set the attribute list for this Function. void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; } + /// Add return value attributes to this function. + void addRetAttr(Attribute::AttrKind Kind) { + addAttribute(AttributeList::ReturnIndex, Kind); + } + /// Add function attributes to this function. void addFnAttr(Attribute::AttrKind Kind) { addAttribute(AttributeList::FunctionIndex, Kind); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 8a7060c..f171066 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2502,7 +2502,7 @@ void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, const char *V) { Function *Func = unwrap(Fn); Attribute Attr = Attribute::get(Func->getContext(), A, V); - Func->addAttribute(AttributeList::FunctionIndex, Attr); + Func->addFnAttr(Attr); } /*--.. Operations on parameters ............................................--*/ diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp index 8836219..8de9bd7 100644 --- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -17744,7 +17744,7 @@ void AArch64TargetLowering::insertSSPDeclarations(Module &M) const { Type::getInt8PtrTy(M.getContext())); if (Function *F = dyn_cast(SecurityCheckCookie.getCallee())) { F->setCallingConv(CallingConv::Win64); - F->addAttribute(1, Attribute::AttrKind::InReg); + F->addParamAttr(0, Attribute::AttrKind::InReg); } return; } diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp index cae7170..f914263 100644 --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -20371,7 +20371,7 @@ void ARMTargetLowering::insertSSPDeclarations(Module &M) const { "__security_check_cookie", Type::getVoidTy(M.getContext()), Type::getInt8PtrTy(M.getContext())); if (Function *F = dyn_cast(SecurityCheckCookie.getCallee())) - F->addAttribute(1, Attribute::AttrKind::InReg); + F->addParamAttr(0, Attribute::AttrKind::InReg); } Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const { diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp index 9d735c8..172dc35 100644 --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -2714,7 +2714,7 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const { Type::getInt8PtrTy(M.getContext())); if (Function *F = dyn_cast(SecurityCheckCookie.getCallee())) { F->setCallingConv(CallingConv::X86_FastCall); - F->addAttribute(1, Attribute::AttrKind::InReg); + F->addParamAttr(0, Attribute::AttrKind::InReg); } return; } diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp index eae971d..c6d7d43 100644 --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -1076,7 +1076,7 @@ static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) { // which prevents us from speculating about the entire SCC LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); - F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); + F->addRetAttr(Attribute::NonNull); ++NumNonNullReturn; MadeChange = true; } @@ -1094,7 +1094,7 @@ static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) { continue; LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); - F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); + F->addRetAttr(Attribute::NonNull); ++NumNonNullReturn; MadeChange = true; } diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp index 1cac59b..a7e68a6 100644 --- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp +++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp @@ -1288,7 +1288,7 @@ void DevirtModule::tryICallBranchFunnel( M.getDataLayout().getProgramAddressSpace(), "branch_funnel", &M); } - JT->addAttribute(1, Attribute::Nest); + JT->addParamAttr(0, Attribute::Nest); std::vector JTArgs; JTArgs.push_back(JT->arg_begin()); diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 0d4ca0b..c02955b 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -2133,8 +2133,7 @@ Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) { AsanDtorFunction = Function::createWithDefaultAttr( FunctionType::get(Type::getVoidTy(*C), false), GlobalValue::InternalLinkage, 0, kAsanModuleDtorName, &M); - AsanDtorFunction->addAttribute(AttributeList::FunctionIndex, - Attribute::NoUnwind); + AsanDtorFunction->addFnAttr(Attribute::NoUnwind); // Ensure Dtor cannot be discarded, even if in a comdat. appendToUsed(M, {AsanDtorFunction}); BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index f49abc6..4118ee0 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -98,7 +98,7 @@ static bool setDoesNotThrow(Function &F) { static bool setRetDoesNotAlias(Function &F) { if (F.hasRetAttribute(Attribute::NoAlias)) return false; - F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); + F.addRetAttr(Attribute::NoAlias); ++NumNoAlias; return true; } @@ -146,7 +146,7 @@ static bool setSignExtendedArg(Function &F, unsigned ArgNo) { static bool setRetNoUndef(Function &F) { if (!F.getReturnType()->isVoidTy() && !F.hasRetAttribute(Attribute::NoUndef)) { - F.addAttribute(AttributeList::ReturnIndex, Attribute::NoUndef); + F.addRetAttr(Attribute::NoUndef); ++NumNoUndef; return true; } diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp index 2aef372..aa95204 100644 --- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp +++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp @@ -125,7 +125,7 @@ Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) { Function *Ctor = Function::createWithDefaultAttr( FunctionType::get(Type::getVoidTy(M.getContext()), false), GlobalValue::InternalLinkage, 0, CtorName, &M); - Ctor->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); + Ctor->addFnAttr(Attribute::NoUnwind); BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor); ReturnInst::Create(M.getContext(), CtorBB); // Ensure Ctor cannot be discarded, even if in a comdat. -- 2.7.4