Skip to content

Commit

Permalink
[NFC] Cleanup attribute methods in Function
Browse files Browse the repository at this point in the history
  • Loading branch information
aeubanks committed Aug 18, 2021
1 parent ad727ab commit cc327bd
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 169 deletions.
35 changes: 35 additions & 0 deletions llvm/include/llvm/IR/Attributes.h
Expand Up @@ -475,6 +475,41 @@ class AttributeList {
LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &B) const;

/// Add a function attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
Attribute::AttrKind Kind) const {
return addAttribute(C, FunctionIndex, Kind);
}

/// Add a function attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addFnAttribute(LLVMContext &C,
Attribute Attr) const {
return addAttribute(C, FunctionIndex, Attr);
}

/// Add a function attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addFnAttribute(
LLVMContext &C, StringRef Kind, StringRef Value = StringRef()) const {
return addAttribute(C, FunctionIndex, Kind, Value);
}

/// Add function attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addFnAttributes(LLVMContext &C,
const AttrBuilder &B) const {
return addAttributes(C, FunctionIndex, B);
}

/// Add a return value attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addRetAttribute(LLVMContext &C,
Attribute::AttrKind Kind) const {
return addAttribute(C, ReturnIndex, Kind);
}

/// Add an argument attribute to the list. Returns a new list because
/// attribute lists are immutable.
LLVM_NODISCARD AttributeList addParamAttribute(
Expand Down
197 changes: 79 additions & 118 deletions llvm/include/llvm/IR/Function.h
Expand Up @@ -245,61 +245,6 @@ class Function : public GlobalObject, public ilist_node<Function> {
setValueSubclassData((getSubclassDataFromValue() & 0xc00f) | (ID << 4));
}

/// Return the attribute list for this Function.
AttributeList getAttributes() const { return AttributeSets; }

/// 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);
}

/// Add function attributes to this function.
void addFnAttr(StringRef Kind, StringRef Val = StringRef()) {
addAttribute(AttributeList::FunctionIndex,
Attribute::get(getContext(), Kind, Val));
}

/// Add function attributes to this function.
void addFnAttr(Attribute Attr) {
addAttribute(AttributeList::FunctionIndex, Attr);
}

/// Add function attributes to this function.
void addFnAttrs(const AttrBuilder &Attrs) {
addAttributes(AttributeList::FunctionIndex, Attrs);
}

/// removes the attributes from the list of attributes.
void removeAttributes(unsigned i, const AttrBuilder &Attrs);

/// Remove function attributes from this function.
void removeFnAttr(Attribute::AttrKind Kind) {
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
}

/// Remove function attribute from this function.
void removeFnAttr(StringRef Kind) {
setAttributes(getAttributes().removeFnAttribute(getContext(), Kind));
}

void removeFnAttrs(const AttrBuilder &Attrs) {
setAttributes(getAttributes().removeFnAttributes(getContext(), Attrs));
}

/// A function will have the "coroutine.presplit" attribute if it's
/// a coroutine and has not gone through full CoroSplit pass.
bool isPresplitCoroutine() const {
return hasFnAttribute("coroutine.presplit");
}

enum ProfileCountType { PCT_Invalid, PCT_Real, PCT_Synthetic };

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

/// Return true if the function has the attribute.
bool hasFnAttribute(Attribute::AttrKind Kind) const {
return AttributeSets.hasFnAttr(Kind);
}

/// Return true if the function has the attribute.
bool hasFnAttribute(StringRef Kind) const {
return AttributeSets.hasFnAttr(Kind);
}

/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(Attribute::AttrKind Kind) const {
return AttributeSets.getFnAttr(Kind);
}

/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(StringRef Kind) const {
return AttributeSets.getFnAttr(Kind);
}

/// Return the stack alignment for the function.
unsigned getFnStackAlignment() const {
if (!hasFnAttribute(Attribute::StackAlignment))
return 0;
if (const auto MA =
AttributeSets.getStackAlignment(AttributeList::FunctionIndex))
return MA->value();
return 0;
}

/// Return the stack alignment for the function.
MaybeAlign getFnStackAlign() const {
if (!hasFnAttribute(Attribute::StackAlignment))
return None;
return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
}

/// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
/// to use during code generation.
bool hasGC() const {
Expand All @@ -413,18 +321,36 @@ class Function : public GlobalObject, public ilist_node<Function> {
void setGC(std::string Str);
void clearGC();

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

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

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

/// adds the attribute to the list of attributes.
void addAttribute(unsigned i, Attribute::AttrKind Kind);

/// adds the attributes to the list of attributes.
void addAttributes(unsigned i, const AttrBuilder &Attrs);

/// Add function attributes to this function.
void addFnAttr(Attribute::AttrKind Kind);

/// Add function attributes to this function.
void addFnAttr(StringRef Kind, StringRef Val = StringRef());

/// Add function attributes to this function.
void addFnAttr(Attribute Attr);

/// Add function attributes to this function.
void addFnAttrs(const AttrBuilder &Attrs);

/// Add return value attributes to this function.
void addRetAttr(Attribute::AttrKind Kind);

/// adds the attribute to the list of attributes for the given arg.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);

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

/// Remove function attributes from this function.
void removeFnAttr(Attribute::AttrKind Kind);

/// Remove function attribute from this function.
void removeFnAttr(StringRef Kind);

void removeFnAttrs(const AttrBuilder &Attrs);

/// removes the attribute from the return value list of attributes.
void removeRetAttr(Attribute::AttrKind Kind);

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

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

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

/// removes noundef and other attributes that imply undefined behavior if a
/// `undef` or `poison` value is passed from the list of attributes.
void removeParamUndefImplyingAttrs(unsigned ArgNo);
/// Return true if the function has the attribute.
bool hasFnAttribute(Attribute::AttrKind Kind) const;

/// Return true if the function has the attribute.
bool hasFnAttribute(StringRef Kind) const;

/// check if an attribute is in the list of attributes for the return value.
bool hasRetAttribute(Attribute::AttrKind Kind) const;

/// check if an attributes is in the list of attributes.
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
return getAttributes().hasParamAttr(ArgNo, Kind);
}
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;

/// gets the attribute from the list of attributes.
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const;

/// gets the attribute from the list of attributes.
Attribute getAttribute(unsigned i, StringRef Kind) const;

/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(Attribute::AttrKind Kind) const;

/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(StringRef Kind) const;

/// gets the specified attribute from the list of attributes.
Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
return getAttributes().getParamAttr(ArgNo, Kind);
}
Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;

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

/// gets the attribute from the list of attributes.
Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
return AttributeSets.getAttribute(i, Kind);
/// Return the stack alignment for the function.
unsigned getFnStackAlignment() const {
if (!hasFnAttribute(Attribute::StackAlignment))
return 0;
if (const auto MA =
AttributeSets.getStackAlignment(AttributeList::FunctionIndex))
return MA->value();
return 0;
}

/// gets the attribute from the list of attributes.
Attribute getAttribute(unsigned i, StringRef Kind) const {
return AttributeSets.getAttribute(i, Kind);
/// Return the stack alignment for the function.
MaybeAlign getFnStackAlign() const {
if (!hasFnAttribute(Attribute::StackAlignment))
return None;
return AttributeSets.getStackAlignment(AttributeList::FunctionIndex);
}

/// Returns true if the function has ssp, sspstrong, or sspreq fn attrs.
bool hasStackProtectorFnAttr() const;

/// adds the dereferenceable attribute to the list of attributes for
/// the given arg.
void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
Expand Down Expand Up @@ -558,6 +515,12 @@ class Function : public GlobalObject, public ilist_node<Function> {
return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
}

/// A function will have the "coroutine.presplit" attribute if it's
/// a coroutine and has not gone through full CoroSplit pass.
bool isPresplitCoroutine() const {
return hasFnAttribute("coroutine.presplit");
}

/// Determine if the function does not access memory.
bool doesNotAccessMemory() const {
return hasFnAttribute(Attribute::ReadNone);
Expand Down Expand Up @@ -715,9 +678,7 @@ class Function : public GlobalObject, public ilist_node<Function> {
bool returnDoesNotAlias() const {
return AttributeSets.hasRetAttr(Attribute::NoAlias);
}
void setReturnDoesNotAlias() {
addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
}
void setReturnDoesNotAlias() { addRetAttr(Attribute::NoAlias); }

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

0 comments on commit cc327bd

Please sign in to comment.