Skip to content

Commit

Permalink
[NFC] Replace Function handling of attributes with less confusing calls
Browse files Browse the repository at this point in the history
To avoid magic constants and confusing indexes.
  • Loading branch information
aeubanks committed Aug 18, 2021
1 parent b41bfb8 commit 46cf825
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 17 deletions.
8 changes: 3 additions & 5 deletions clang/lib/CodeGen/CodeGenModule.cpp
Expand Up @@ -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
Expand All @@ -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<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/IR/Function.h
Expand Up @@ -251,6 +251,11 @@ class Function : public GlobalObject, public ilist_node<Function> {
/// 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);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Core.cpp
Expand Up @@ -2502,7 +2502,7 @@ void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
const char *V) {
Function *Func = unwrap<Function>(Fn);
Attribute Attr = Attribute::get(Func->getContext(), A, V);
Func->addAttribute(AttributeList::FunctionIndex, Attr);
Func->addFnAttr(Attr);
}

/*--.. Operations on parameters ............................................--*/
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Expand Up @@ -17744,7 +17744,7 @@ void AArch64TargetLowering::insertSSPDeclarations(Module &M) const {
Type::getInt8PtrTy(M.getContext()));
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
F->setCallingConv(CallingConv::Win64);
F->addAttribute(1, Attribute::AttrKind::InReg);
F->addParamAttr(0, Attribute::AttrKind::InReg);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/ARM/ARMISelLowering.cpp
Expand Up @@ -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<Function>(SecurityCheckCookie.getCallee()))
F->addAttribute(1, Attribute::AttrKind::InReg);
F->addParamAttr(0, Attribute::AttrKind::InReg);
}

Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86ISelLowering.cpp
Expand Up @@ -2714,7 +2714,7 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const {
Type::getInt8PtrTy(M.getContext()));
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
F->setCallingConv(CallingConv::X86_FastCall);
F->addAttribute(1, Attribute::AttrKind::InReg);
F->addParamAttr(0, Attribute::AttrKind::InReg);
}
return;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/FunctionAttrs.cpp
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
Expand Up @@ -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<Value *> JTArgs;
JTArgs.push_back(JT->arg_begin());
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/ModuleUtils.cpp
Expand Up @@ -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.
Expand Down

0 comments on commit 46cf825

Please sign in to comment.