Skip to content

Commit 46cf825

Browse files
committed
[NFC] Replace Function handling of attributes with less confusing calls
To avoid magic constants and confusing indexes.
1 parent b41bfb8 commit 46cf825

File tree

11 files changed

+19
-17
lines changed

11 files changed

+19
-17
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
21182118
F->arg_begin()->getType()
21192119
->canLosslesslyBitCastTo(F->getReturnType()) &&
21202120
"unexpected this return");
2121-
F->addAttribute(1, llvm::Attribute::Returned);
2121+
F->addParamAttr(0, llvm::Attribute::Returned);
21222122
}
21232123

21242124
// Only a few attributes are set on declarations; these may later be
@@ -2144,15 +2144,13 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
21442144
assert(HasBody && "Inline builtin declarations should always have an "
21452145
"available body!");
21462146
if (shouldEmitFunction(FDBody))
2147-
F->addAttribute(llvm::AttributeList::FunctionIndex,
2148-
llvm::Attribute::NoBuiltin);
2147+
F->addFnAttr(llvm::Attribute::NoBuiltin);
21492148
}
21502149

21512150
if (FD->isReplaceableGlobalAllocationFunction()) {
21522151
// A replaceable global allocation function does not act like a builtin by
21532152
// default, only if it is invoked by a new-expression or delete-expression.
2154-
F->addAttribute(llvm::AttributeList::FunctionIndex,
2155-
llvm::Attribute::NoBuiltin);
2153+
F->addFnAttr(llvm::Attribute::NoBuiltin);
21562154
}
21572155

21582156
if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))

llvm/include/llvm/IR/Function.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ class Function : public GlobalObject, public ilist_node<Function> {
251251
/// Set the attribute list for this Function.
252252
void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
253253

254+
/// Add return value attributes to this function.
255+
void addRetAttr(Attribute::AttrKind Kind) {
256+
addAttribute(AttributeList::ReturnIndex, Kind);
257+
}
258+
254259
/// Add function attributes to this function.
255260
void addFnAttr(Attribute::AttrKind Kind) {
256261
addAttribute(AttributeList::FunctionIndex, Kind);

llvm/lib/IR/Core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
25022502
const char *V) {
25032503
Function *Func = unwrap<Function>(Fn);
25042504
Attribute Attr = Attribute::get(Func->getContext(), A, V);
2505-
Func->addAttribute(AttributeList::FunctionIndex, Attr);
2505+
Func->addFnAttr(Attr);
25062506
}
25072507

25082508
/*--.. Operations on parameters ............................................--*/

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17744,7 +17744,7 @@ void AArch64TargetLowering::insertSSPDeclarations(Module &M) const {
1774417744
Type::getInt8PtrTy(M.getContext()));
1774517745
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
1774617746
F->setCallingConv(CallingConv::Win64);
17747-
F->addAttribute(1, Attribute::AttrKind::InReg);
17747+
F->addParamAttr(0, Attribute::AttrKind::InReg);
1774817748
}
1774917749
return;
1775017750
}

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20371,7 +20371,7 @@ void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
2037120371
"__security_check_cookie", Type::getVoidTy(M.getContext()),
2037220372
Type::getInt8PtrTy(M.getContext()));
2037320373
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee()))
20374-
F->addAttribute(1, Attribute::AttrKind::InReg);
20374+
F->addParamAttr(0, Attribute::AttrKind::InReg);
2037520375
}
2037620376

2037720377
Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const {

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const {
27142714
Type::getInt8PtrTy(M.getContext()));
27152715
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
27162716
F->setCallingConv(CallingConv::X86_FastCall);
2717-
F->addAttribute(1, Attribute::AttrKind::InReg);
2717+
F->addParamAttr(0, Attribute::AttrKind::InReg);
27182718
}
27192719
return;
27202720
}

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
10761076
// which prevents us from speculating about the entire SCC
10771077
LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
10781078
<< " as nonnull\n");
1079-
F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1079+
F->addRetAttr(Attribute::NonNull);
10801080
++NumNonNullReturn;
10811081
MadeChange = true;
10821082
}
@@ -1094,7 +1094,7 @@ static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
10941094
continue;
10951095

10961096
LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1097-
F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1097+
F->addRetAttr(Attribute::NonNull);
10981098
++NumNonNullReturn;
10991099
MadeChange = true;
11001100
}

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ void DevirtModule::tryICallBranchFunnel(
12881288
M.getDataLayout().getProgramAddressSpace(),
12891289
"branch_funnel", &M);
12901290
}
1291-
JT->addAttribute(1, Attribute::Nest);
1291+
JT->addParamAttr(0, Attribute::Nest);
12921292

12931293
std::vector<Value *> JTArgs;
12941294
JTArgs.push_back(JT->arg_begin());

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,8 +2133,7 @@ Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
21332133
AsanDtorFunction = Function::createWithDefaultAttr(
21342134
FunctionType::get(Type::getVoidTy(*C), false),
21352135
GlobalValue::InternalLinkage, 0, kAsanModuleDtorName, &M);
2136-
AsanDtorFunction->addAttribute(AttributeList::FunctionIndex,
2137-
Attribute::NoUnwind);
2136+
AsanDtorFunction->addFnAttr(Attribute::NoUnwind);
21382137
// Ensure Dtor cannot be discarded, even if in a comdat.
21392138
appendToUsed(M, {AsanDtorFunction});
21402139
BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static bool setDoesNotThrow(Function &F) {
9898
static bool setRetDoesNotAlias(Function &F) {
9999
if (F.hasRetAttribute(Attribute::NoAlias))
100100
return false;
101-
F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
101+
F.addRetAttr(Attribute::NoAlias);
102102
++NumNoAlias;
103103
return true;
104104
}
@@ -146,7 +146,7 @@ static bool setSignExtendedArg(Function &F, unsigned ArgNo) {
146146
static bool setRetNoUndef(Function &F) {
147147
if (!F.getReturnType()->isVoidTy() &&
148148
!F.hasRetAttribute(Attribute::NoUndef)) {
149-
F.addAttribute(AttributeList::ReturnIndex, Attribute::NoUndef);
149+
F.addRetAttr(Attribute::NoUndef);
150150
++NumNoUndef;
151151
return true;
152152
}

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
125125
Function *Ctor = Function::createWithDefaultAttr(
126126
FunctionType::get(Type::getVoidTy(M.getContext()), false),
127127
GlobalValue::InternalLinkage, 0, CtorName, &M);
128-
Ctor->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
128+
Ctor->addFnAttr(Attribute::NoUnwind);
129129
BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
130130
ReturnInst::Create(M.getContext(), CtorBB);
131131
// Ensure Ctor cannot be discarded, even if in a comdat.

0 commit comments

Comments
 (0)