From cc327bd5231126006b4177b8ce0946ce52e2f645 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Mon, 16 Aug 2021 17:35:02 -0700 Subject: [PATCH] [NFC] Cleanup attribute methods in Function --- llvm/include/llvm/IR/Attributes.h | 35 ++++ llvm/include/llvm/IR/Function.h | 259 +++++++++++++----------------- llvm/lib/IR/Function.cpp | 138 ++++++++++------ 3 files changed, 231 insertions(+), 201 deletions(-) diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h index c9eee5b9eaba..5113d79a3b89 100644 --- a/llvm/include/llvm/IR/Attributes.h +++ b/llvm/include/llvm/IR/Attributes.h @@ -475,6 +475,41 @@ public: 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( diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index 69bcd0be8e38..73a3b1328de5 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -245,61 +245,6 @@ public: 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. @@ -367,25 +312,116 @@ public: /// Get the section prefix for this function. Optional getSectionPrefix() const; - /// Return true if the function has the attribute. - bool hasFnAttribute(Attribute::AttrKind Kind) const { - return AttributeSets.hasFnAttr(Kind); + /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm + /// to use during code generation. + bool hasGC() const { + return getSubclassDataFromValue() & (1<<14); } + const std::string &getGC() const; + void setGC(std::string Str); + void clearGC(); + + /// 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; } + + /// 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); + + /// adds the attribute to the list of attributes for the given arg. + void addParamAttr(unsigned ArgNo, Attribute Attr); + + /// adds the attributes to the list of attributes for the given arg. + void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs); + + /// removes the attribute from the list of attributes. + void removeAttribute(unsigned i, Attribute::AttrKind Kind); + + /// 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. + void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind); + + /// removes the attribute from the list of attributes. + void removeParamAttr(unsigned ArgNo, StringRef Kind); + + /// removes the attribute from the list of attributes. + void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs); /// Return true if the function has the attribute. - bool hasFnAttribute(StringRef Kind) const { - return AttributeSets.hasFnAttr(Kind); - } + 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; + + /// 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 AttributeSets.getFnAttr(Kind); - } + Attribute getFnAttribute(Attribute::AttrKind Kind) const; /// Return the attribute for the given attribute kind. - Attribute getFnAttribute(StringRef Kind) const { - return AttributeSets.getFnAttr(Kind); - } + Attribute getFnAttribute(StringRef Kind) const; + + /// gets the specified attribute from the list of attributes. + Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const; + + /// 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 the stack alignment for the function. unsigned getFnStackAlignment() const { @@ -404,88 +440,9 @@ public: return AttributeSets.getStackAlignment(AttributeList::FunctionIndex); } - /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm - /// to use during code generation. - bool hasGC() const { - return getSubclassDataFromValue() & (1<<14); - } - const std::string &getGC() const; - void setGC(std::string Str); - void clearGC(); - /// Returns true if the function has ssp, sspstrong, or sspreq fn attrs. bool hasStackProtectorFnAttr() const; - /// adds the attribute to the list of attributes. - void addAttribute(unsigned i, Attribute::AttrKind Kind); - - /// adds the attribute to the list of attributes. - void addAttribute(unsigned i, Attribute Attr); - - /// adds the attributes to the list of attributes. - void addAttributes(unsigned i, const AttrBuilder &Attrs); - - /// adds the attribute to the list of attributes for the given arg. - void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind); - - /// adds the attribute to the list of attributes for the given arg. - void addParamAttr(unsigned ArgNo, Attribute Attr); - - /// adds the attributes to the list of attributes for the given arg. - void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs); - - /// removes the attribute from the list of attributes. - void removeAttribute(unsigned i, Attribute::AttrKind Kind); - - /// removes the attribute from the list of attributes. - void removeAttribute(unsigned i, StringRef Kind); - - /// 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); - - void removeRetAttrs(const AttrBuilder &Attrs); - - /// removes the attribute from the list of attributes. - void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind); - - /// removes the attribute from the list of attributes. - void removeParamAttr(unsigned ArgNo, StringRef Kind); - - /// 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); - - /// check if an attributes is in the list of attributes. - bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const { - return getAttributes().hasParamAttr(ArgNo, Kind); - } - - /// gets the specified attribute from the list of attributes. - Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const { - return getAttributes().getParamAttr(ArgNo, Kind); - } - - /// check if an attribute is in the list of attributes for the return value. - bool hasRetAttribute(Attribute::AttrKind Kind) const { - return getAttributes().hasRetAttr(Kind); - } - - /// gets the attribute from the list of attributes. - Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { - return AttributeSets.getAttribute(i, Kind); - } - - /// gets the attribute from the list of attributes. - Attribute getAttribute(unsigned i, StringRef Kind) const { - return AttributeSets.getAttribute(i, Kind); - } - /// adds the dereferenceable attribute to the list of attributes for /// the given arg. void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes); @@ -558,6 +515,12 @@ public: 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); @@ -715,9 +678,7 @@ public: 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); } diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index f8558636ab53..9313c4980dfb 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -529,101 +529,135 @@ void Function::dropAllReferences() { clearMetadata(); } -void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.addAttribute(getContext(), i, Kind); - setAttributes(PAL); -} - void Function::addAttribute(unsigned i, Attribute Attr) { - AttributeList PAL = getAttributes(); - PAL = PAL.addAttribute(getContext(), i, Attr); - setAttributes(PAL); + AttributeSets = AttributeSets.addAttribute(getContext(), i, Attr); } -void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) { - AttributeList PAL = getAttributes(); - PAL = PAL.addAttributes(getContext(), i, Attrs); - setAttributes(PAL); +void Function::addFnAttr(Attribute::AttrKind Kind) { + AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind); +} + +void Function::addFnAttr(StringRef Kind, StringRef Val) { + AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val); +} + +void Function::addFnAttr(Attribute Attr) { + AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr); +} + +void Function::addFnAttrs(const AttrBuilder &Attrs) { + AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs); +} + +void Function::addRetAttr(Attribute::AttrKind Kind) { + AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind); } void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind); } void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { - AttributeList PAL = getAttributes(); - PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); - setAttributes(PAL); + AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr); } void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { - AttributeList PAL = getAttributes(); - PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs); - setAttributes(PAL); + AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs); } void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeAttribute(getContext(), i, Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeAttribute(getContext(), i, Kind); } void Function::removeAttribute(unsigned i, StringRef Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeAttribute(getContext(), i, Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeAttribute(getContext(), i, Kind); +} + +void Function::removeFnAttr(Attribute::AttrKind Kind) { + AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); +} + +void Function::removeFnAttr(StringRef Kind) { + AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); +} + +void Function::removeFnAttrs(const AttrBuilder &Attrs) { + AttributeSets = AttributeSets.removeFnAttributes(getContext(), Attrs); } void Function::removeRetAttr(Attribute::AttrKind Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeRetAttribute(getContext(), Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); } void Function::removeRetAttr(StringRef Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeRetAttribute(getContext(), Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); } void Function::removeRetAttrs(const AttrBuilder &Attrs) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeRetAttributes(getContext(), Attrs); - setAttributes(PAL); + AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs); } void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); } void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); - setAttributes(PAL); + AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); } void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { - AttributeList PAL = getAttributes(); - PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs); - setAttributes(PAL); + AttributeSets = + AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs); } void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { - AttributeList PAL = getAttributes(); - PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); - setAttributes(PAL); + AttributeSets = + AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); +} + +bool Function::hasFnAttribute(Attribute::AttrKind Kind) const { + return AttributeSets.hasFnAttr(Kind); +} + +bool Function::hasFnAttribute(StringRef Kind) const { + return AttributeSets.hasFnAttr(Kind); +} + +bool Function::hasRetAttribute(Attribute::AttrKind Kind) const { + return AttributeSets.hasRetAttr(Kind); +} + +bool Function::hasParamAttribute(unsigned ArgNo, + Attribute::AttrKind Kind) const { + return AttributeSets.hasParamAttr(ArgNo, Kind); +} + +Attribute Function::getAttribute(unsigned i, Attribute::AttrKind Kind) const { + return AttributeSets.getAttribute(i, Kind); +} + +Attribute Function::getAttribute(unsigned i, StringRef Kind) const { + return AttributeSets.getAttribute(i, Kind); +} + +Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const { + return AttributeSets.getFnAttr(Kind); +} + +Attribute Function::getFnAttribute(StringRef Kind) const { + return AttributeSets.getFnAttr(Kind); +} + +/// gets the specified attribute from the list of attributes. +Attribute Function::getParamAttribute(unsigned ArgNo, + Attribute::AttrKind Kind) const { + return AttributeSets.getParamAttr(ArgNo, Kind); } void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, uint64_t Bytes) { - AttributeList PAL = getAttributes(); - PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes); - setAttributes(PAL); + AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(), + ArgNo, Bytes); } DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const {