Skip to content

Commit cc327bd

Browse files
committed
[NFC] Cleanup attribute methods in Function
1 parent ad727ab commit cc327bd

File tree

3 files changed

+199
-169
lines changed

3 files changed

+199
-169
lines changed

llvm/include/llvm/IR/Attributes.h

+35
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,41 @@ class AttributeList {
475475
LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
476476
const AttrBuilder &B) const;
477477

478+
/// Add a function attribute to the list. Returns a new list because
479+
/// attribute lists are immutable.
480+
LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
481+
Attribute::AttrKind Kind) const {
482+
return addAttribute(C, FunctionIndex, Kind);
483+
}
484+
485+
/// Add a function attribute to the list. Returns a new list because
486+
/// attribute lists are immutable.
487+
LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
488+
Attribute Attr) const {
489+
return addAttribute(C, FunctionIndex, Attr);
490+
}
491+
492+
/// Add a function attribute to the list. Returns a new list because
493+
/// attribute lists are immutable.
494+
LLVM_NODISCARD AttributeList addFnAttribute(
495+
LLVMContext &C, StringRef Kind, StringRef Value = StringRef()) const {
496+
return addAttribute(C, FunctionIndex, Kind, Value);
497+
}
498+
499+
/// Add function attribute to the list. Returns a new list because
500+
/// attribute lists are immutable.
501+
LLVM_NODISCARD AttributeList addFnAttributes(LLVMContext &C,
502+
const AttrBuilder &B) const {
503+
return addAttributes(C, FunctionIndex, B);
504+
}
505+
506+
/// Add a return value attribute to the list. Returns a new list because
507+
/// attribute lists are immutable.
508+
LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
509+
Attribute::AttrKind Kind) const {
510+
return addAttribute(C, ReturnIndex, Kind);
511+
}
512+
478513
/// Add an argument attribute to the list. Returns a new list because
479514
/// attribute lists are immutable.
480515
LLVM_NODISCARD AttributeList addParamAttribute(

llvm/include/llvm/IR/Function.h

+79-118
Original file line numberDiff line numberDiff line change
@@ -245,61 +245,6 @@ class Function : public GlobalObject, public ilist_node<Function> {
245245
setValueSubclassData((getSubclassDataFromValue() & 0xc00f) | (ID << 4));
246246
}
247247

248-
/// Return the attribute list for this Function.
249-
AttributeList getAttributes() const { return AttributeSets; }
250-
251-
/// Set the attribute list for this Function.
252-
void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
253-
254-
/// Add return value attributes to this function.
255-
void addRetAttr(Attribute::AttrKind Kind) {
256-
addAttribute(AttributeList::ReturnIndex, Kind);
257-
}
258-
259-
/// Add function attributes to this function.
260-
void addFnAttr(Attribute::AttrKind Kind) {
261-
addAttribute(AttributeList::FunctionIndex, Kind);
262-
}
263-
264-
/// Add function attributes to this function.
265-
void addFnAttr(StringRef Kind, StringRef Val = StringRef()) {
266-
addAttribute(AttributeList::FunctionIndex,
267-
Attribute::get(getContext(), Kind, Val));
268-
}
269-
270-
/// Add function attributes to this function.
271-
void addFnAttr(Attribute Attr) {
272-
addAttribute(AttributeList::FunctionIndex, Attr);
273-
}
274-
275-
/// Add function attributes to this function.
276-
void addFnAttrs(const AttrBuilder &Attrs) {
277-
addAttributes(AttributeList::FunctionIndex, Attrs);
278-
}
279-
280-
/// removes the attributes from the list of attributes.
281-
void removeAttributes(unsigned i, const AttrBuilder &Attrs);
282-
283-
/// Remove function attributes from this function.
284-
void removeFnAttr(Attribute::AttrKind Kind) {
285-
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
286-
}
287-
288-
/// Remove function attribute from this function.
289-
void removeFnAttr(StringRef Kind) {
290-
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
291-
}
292-
293-
void removeFnAttrs(const AttrBuilder &Attrs) {
294-
setAttributes(getAttributes().removeFnAttributes(getContext(), Attrs));
295-
}
296-
297-
/// A function will have the "coroutine.presplit" attribute if it's
298-
/// a coroutine and has not gone through full CoroSplit pass.
299-
bool isPresplitCoroutine() const {
300-
return hasFnAttribute("coroutine.presplit");
301-
}
302-
303248
enum ProfileCountType { PCT_Invalid, PCT_Real, PCT_Synthetic };
304249

305250
/// Class to represent profile counts.
@@ -367,43 +312,6 @@ class Function : public GlobalObject, public ilist_node<Function> {
367312
/// Get the section prefix for this function.
368313
Optional<StringRef> getSectionPrefix() const;
369314

370-
/// Return true if the function has the attribute.
371-
bool hasFnAttribute(Attribute::AttrKind Kind) const {
372-
return AttributeSets.hasFnAttr(Kind);
373-
}
374-
375-
/// Return true if the function has the attribute.
376-
bool hasFnAttribute(StringRef Kind) const {
377-
return AttributeSets.hasFnAttr(Kind);
378-
}
379-
380-
/// Return the attribute for the given attribute kind.
381-
Attribute getFnAttribute(Attribute::AttrKind Kind) const {
382-
return AttributeSets.getFnAttr(Kind);
383-
}
384-
385-
/// Return the attribute for the given attribute kind.
386-
Attribute getFnAttribute(StringRef Kind) const {
387-
return AttributeSets.getFnAttr(Kind);
388-
}
389-
390-
/// Return the stack alignment for the function.
391-
unsigned getFnStackAlignment() const {
392-
if (!hasFnAttribute(Attribute::StackAlignment))
393-
return 0;
394-
if (const auto MA =
395-
AttributeSets.getStackAlignment(AttributeList::FunctionIndex))
396-
return MA->value();
397-
return 0;
398-
}
399-
400-
/// Return the stack alignment for the function.
401-
MaybeAlign getFnStackAlign() const {
402-
if (!hasFnAttribute(Attribute::StackAlignment))
403-
return None;
404-
return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
405-
}
406-
407315
/// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
408316
/// to use during code generation.
409317
bool hasGC() const {
@@ -413,18 +321,36 @@ class Function : public GlobalObject, public ilist_node<Function> {
413321
void setGC(std::string Str);
414322
void clearGC();
415323

416-
/// Returns true if the function has ssp, sspstrong, or sspreq fn attrs.
417-
bool hasStackProtectorFnAttr() const;
324+
/// Return the attribute list for this Function.
325+
AttributeList getAttributes() const { return AttributeSets; }
418326

419-
/// adds the attribute to the list of attributes.
420-
void addAttribute(unsigned i, Attribute::AttrKind Kind);
327+
/// Set the attribute list for this Function.
328+
void setAttributes(AttributeList Attrs) { AttributeSets = Attrs; }
421329

422330
/// adds the attribute to the list of attributes.
423331
void addAttribute(unsigned i, Attribute Attr);
424332

333+
/// adds the attribute to the list of attributes.
334+
void addAttribute(unsigned i, Attribute::AttrKind Kind);
335+
425336
/// adds the attributes to the list of attributes.
426337
void addAttributes(unsigned i, const AttrBuilder &Attrs);
427338

339+
/// Add function attributes to this function.
340+
void addFnAttr(Attribute::AttrKind Kind);
341+
342+
/// Add function attributes to this function.
343+
void addFnAttr(StringRef Kind, StringRef Val = StringRef());
344+
345+
/// Add function attributes to this function.
346+
void addFnAttr(Attribute Attr);
347+
348+
/// Add function attributes to this function.
349+
void addFnAttrs(const AttrBuilder &Attrs);
350+
351+
/// Add return value attributes to this function.
352+
void addRetAttr(Attribute::AttrKind Kind);
353+
428354
/// adds the attribute to the list of attributes for the given arg.
429355
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
430356

@@ -440,12 +366,21 @@ class Function : public GlobalObject, public ilist_node<Function> {
440366
/// removes the attribute from the list of attributes.
441367
void removeAttribute(unsigned i, StringRef Kind);
442368

369+
/// Remove function attributes from this function.
370+
void removeFnAttr(Attribute::AttrKind Kind);
371+
372+
/// Remove function attribute from this function.
373+
void removeFnAttr(StringRef Kind);
374+
375+
void removeFnAttrs(const AttrBuilder &Attrs);
376+
443377
/// removes the attribute from the return value list of attributes.
444378
void removeRetAttr(Attribute::AttrKind Kind);
445379

446380
/// removes the attribute from the return value list of attributes.
447381
void removeRetAttr(StringRef Kind);
448382

383+
/// removes the attributes from the return value list of attributes.
449384
void removeRetAttrs(const AttrBuilder &Attrs);
450385

451386
/// removes the attribute from the list of attributes.
@@ -457,35 +392,57 @@ class Function : public GlobalObject, public ilist_node<Function> {
457392
/// removes the attribute from the list of attributes.
458393
void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
459394

460-
/// removes noundef and other attributes that imply undefined behavior if a
461-
/// `undef` or `poison` value is passed from the list of attributes.
462-
void removeParamUndefImplyingAttrs(unsigned ArgNo);
395+
/// Return true if the function has the attribute.
396+
bool hasFnAttribute(Attribute::AttrKind Kind) const;
397+
398+
/// Return true if the function has the attribute.
399+
bool hasFnAttribute(StringRef Kind) const;
400+
401+
/// check if an attribute is in the list of attributes for the return value.
402+
bool hasRetAttribute(Attribute::AttrKind Kind) const;
463403

464404
/// check if an attributes is in the list of attributes.
465-
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
466-
return getAttributes().hasParamAttr(ArgNo, Kind);
467-
}
405+
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
406+
407+
/// gets the attribute from the list of attributes.
408+
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const;
409+
410+
/// gets the attribute from the list of attributes.
411+
Attribute getAttribute(unsigned i, StringRef Kind) const;
412+
413+
/// Return the attribute for the given attribute kind.
414+
Attribute getFnAttribute(Attribute::AttrKind Kind) const;
415+
416+
/// Return the attribute for the given attribute kind.
417+
Attribute getFnAttribute(StringRef Kind) const;
468418

469419
/// gets the specified attribute from the list of attributes.
470-
Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
471-
return getAttributes().getParamAttr(ArgNo, Kind);
472-
}
420+
Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
473421

474-
/// check if an attribute is in the list of attributes for the return value.
475-
bool hasRetAttribute(Attribute::AttrKind Kind) const {
476-
return getAttributes().hasRetAttr(Kind);
477-
}
422+
/// removes noundef and other attributes that imply undefined behavior if a
423+
/// `undef` or `poison` value is passed from the list of attributes.
424+
void removeParamUndefImplyingAttrs(unsigned ArgNo);
478425

479-
/// gets the attribute from the list of attributes.
480-
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
481-
return AttributeSets.getAttribute(i, Kind);
426+
/// Return the stack alignment for the function.
427+
unsigned getFnStackAlignment() const {
428+
if (!hasFnAttribute(Attribute::StackAlignment))
429+
return 0;
430+
if (const auto MA =
431+
AttributeSets.getStackAlignment(AttributeList::FunctionIndex))
432+
return MA->value();
433+
return 0;
482434
}
483435

484-
/// gets the attribute from the list of attributes.
485-
Attribute getAttribute(unsigned i, StringRef Kind) const {
486-
return AttributeSets.getAttribute(i, Kind);
436+
/// Return the stack alignment for the function.
437+
MaybeAlign getFnStackAlign() const {
438+
if (!hasFnAttribute(Attribute::StackAlignment))
439+
return None;
440+
return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
487441
}
488442

443+
/// Returns true if the function has ssp, sspstrong, or sspreq fn attrs.
444+
bool hasStackProtectorFnAttr() const;
445+
489446
/// adds the dereferenceable attribute to the list of attributes for
490447
/// the given arg.
491448
void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
@@ -558,6 +515,12 @@ class Function : public GlobalObject, public ilist_node<Function> {
558515
return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
559516
}
560517

518+
/// A function will have the "coroutine.presplit" attribute if it's
519+
/// a coroutine and has not gone through full CoroSplit pass.
520+
bool isPresplitCoroutine() const {
521+
return hasFnAttribute("coroutine.presplit");
522+
}
523+
561524
/// Determine if the function does not access memory.
562525
bool doesNotAccessMemory() const {
563526
return hasFnAttribute(Attribute::ReadNone);
@@ -715,9 +678,7 @@ class Function : public GlobalObject, public ilist_node<Function> {
715678
bool returnDoesNotAlias() const {
716679
return AttributeSets.hasRetAttr(Attribute::NoAlias);
717680
}
718-
void setReturnDoesNotAlias() {
719-
addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
720-
}
681+
void setReturnDoesNotAlias() { addRetAttr(Attribute::NoAlias); }
721682

722683
/// Do not optimize this function (-O0).
723684
bool hasOptNone() const { return hasFnAttribute(Attribute::OptimizeNone); }

0 commit comments

Comments
 (0)