[IR] Add additional addParamAttr/removeParamAttr to AttributeList API

Summary:
Fairly straightforward patch to fill in some of the holes in the
attributes API with respect to accessing parameter/argument attributes.
The patch aims to step further towards encapsulating the
idx+FirstArgIndex pattern to access these attributes to within the
AttributeList.

Patch by Daniel Neilson!

Reviewers: rnk, chandlerc, pete, javed.absar, reames

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33355

llvm-svn: 304329
This commit is contained in:
Reid Kleckner 2017-05-31 19:23:09 +00:00
parent 1622520c04
commit 8ae2928164
8 changed files with 322 additions and 96 deletions

View File

@ -228,34 +228,31 @@ public:
bool operator==(const AttributeSet &O) { return SetNode == O.SetNode; } bool operator==(const AttributeSet &O) { return SetNode == O.SetNode; }
bool operator!=(const AttributeSet &O) { return !(*this == O); } bool operator!=(const AttributeSet &O) { return !(*this == O); }
/// Add an argument attribute. Because /// Add an argument attribute. Returns a new set because attribute sets are
/// attribute sets are immutable, this returns a new set. /// immutable.
AttributeSet addAttribute(LLVMContext &C, AttributeSet addAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
Attribute::AttrKind Kind) const;
/// Add a target-dependent attribute. Because /// Add a target-dependent attribute. Returns a new set because attribute sets
/// attribute sets are immutable, this returns a new set. /// are immutable.
AttributeSet addAttribute(LLVMContext &C, StringRef Kind, AttributeSet addAttribute(LLVMContext &C, StringRef Kind,
StringRef Value = StringRef()) const; StringRef Value = StringRef()) const;
/// Add attributes to the attribute set. Because /// Add attributes to the attribute set. Returns a new set because attribute
/// attribute sets are immutable, this returns a new set. /// sets are immutable.
AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const; AttributeSet addAttributes(LLVMContext &C, AttributeSet AS) const;
/// Remove the specified attribute from this set. Because /// Remove the specified attribute from this set. Returns a new set because
/// attribute sets are immutable, this returns a new set. /// attribute sets are immutable.
AttributeSet removeAttribute(LLVMContext &C, AttributeSet removeAttribute(LLVMContext &C, Attribute::AttrKind Kind) const;
Attribute::AttrKind Kind) const;
/// Remove the specified attribute from this set. Because /// Remove the specified attribute from this set. Returns a new set because
/// attribute sets are immutable, this returns a new set. /// attribute sets are immutable.
AttributeSet removeAttribute(LLVMContext &C, AttributeSet removeAttribute(LLVMContext &C, StringRef Kind) const;
StringRef Kind) const;
/// Remove the specified attributes from this set. Because /// Remove the specified attributes from this set. Returns a new set because
/// attribute sets are immutable, this returns a new set. /// attribute sets are immutable.
AttributeSet removeAttributes(LLVMContext &C, AttributeSet removeAttributes(LLVMContext &C,
const AttrBuilder &AttrsToRemove) const; const AttrBuilder &AttrsToRemove) const;
/// Return the number of attributes in this set. /// Return the number of attributes in this set.
unsigned getNumAttributes() const; unsigned getNumAttributes() const;
@ -377,6 +374,25 @@ public:
static AttributeList get(LLVMContext &C, unsigned Index, static AttributeList get(LLVMContext &C, unsigned Index,
const AttrBuilder &B); const AttrBuilder &B);
/// \brief Add an attribute to the attribute set at the given index.
/// Returns a new list because attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const;
/// \brief Add an attribute to the attribute set at the given index.
/// Returns a new list because attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind,
StringRef Value = StringRef()) const;
/// Add an attribute to the attribute set at the given index.
/// Returns a new list because attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute A) const;
/// \brief Add attributes to the attribute set at the given index.
/// Returns a new list because attribute lists are immutable.
AttributeList addAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &B) const;
/// Add an argument attribute to the list. Returns a new list because /// Add an argument attribute to the list. Returns a new list because
/// attribute lists are immutable. /// attribute lists are immutable.
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
@ -384,66 +400,112 @@ public:
return addAttribute(C, ArgNo + FirstArgIndex, Kind); return addAttribute(C, ArgNo + FirstArgIndex, Kind);
} }
/// \brief Add an attribute to the attribute set at the given index. Because /// Add an argument attribute to the list. Returns a new list because
/// attribute sets are immutable, this returns a new set. /// attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, unsigned Index, AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
Attribute::AttrKind Kind) const; StringRef Kind,
StringRef Value = StringRef()) const {
return addAttribute(C, ArgNo + FirstArgIndex, Kind, Value);
}
/// \brief Add an attribute to the attribute set at the given index. Because /// Add an attribute to the attribute list at the given arg indices. Returns a
/// attribute sets are immutable, this returns a new set. /// new list because attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, unsigned Index, StringRef Kind, AttributeList addParamAttribute(LLVMContext &C, ArrayRef<unsigned> ArgNos,
StringRef Value = StringRef()) const; Attribute A) const;
/// Add an attribute to the attribute set at the given indices. Because /// Add an argument attribute to the list. Returns a new list because
/// attribute sets are immutable, this returns a new set. /// attribute lists are immutable.
AttributeList addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices, AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo,
Attribute A) const; const AttrBuilder &B) const {
return addAttributes(C, ArgNo + FirstArgIndex, B);
/// \brief Add attributes to the attribute set at the given index. Because }
/// attribute sets are immutable, this returns a new set.
AttributeList addAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &B) const;
/// \brief Remove the specified attribute at the specified index from this /// \brief Remove the specified attribute at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Returns a new list because attribute lists are immutable.
/// new list.
AttributeList removeAttribute(LLVMContext &C, unsigned Index, AttributeList removeAttribute(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const; Attribute::AttrKind Kind) const;
/// \brief Remove the specified attribute at the specified index from this /// \brief Remove the specified attribute at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Returns a new list because attribute lists are immutable.
/// new list.
AttributeList removeAttribute(LLVMContext &C, unsigned Index, AttributeList removeAttribute(LLVMContext &C, unsigned Index,
StringRef Kind) const; StringRef Kind) const;
/// \brief Remove the specified attributes at the specified index from this /// \brief Remove the specified attributes at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Returns a new list because attribute lists are immutable.
/// new list.
AttributeList removeAttributes(LLVMContext &C, unsigned Index, AttributeList removeAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &AttrsToRemove) const; const AttrBuilder &AttrsToRemove) const;
/// \brief Remove all attributes at the specified index from this /// \brief Remove all attributes at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Returns a new list because attribute lists are immutable.
/// new list.
AttributeList removeAttributes(LLVMContext &C, unsigned Index) const; AttributeList removeAttributes(LLVMContext &C, unsigned Index) const;
/// \brief Add the dereferenceable attribute to the attribute set at the given /// \brief Remove the specified attribute at the specified arg index from this
/// index. Because attribute sets are immutable, this returns a new set. /// attribute list. Returns a new list because attribute lists are immutable.
AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
Attribute::AttrKind Kind) const {
return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
}
/// \brief Remove the specified attribute at the specified arg index from this
/// attribute list. Returns a new list because attribute lists are immutable.
AttributeList removeParamAttribute(LLVMContext &C, unsigned ArgNo,
StringRef Kind) const {
return removeAttribute(C, ArgNo + FirstArgIndex, Kind);
}
/// \brief Remove the specified attribute at the specified arg index from this
/// attribute list. Returns a new list because attribute lists are immutable.
AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo,
const AttrBuilder &AttrsToRemove) const {
return removeAttributes(C, ArgNo + FirstArgIndex, AttrsToRemove);
}
/// \brief Remove all attributes at the specified arg index from this
/// attribute list. Returns a new list because attribute lists are immutable.
AttributeList removeParamAttributes(LLVMContext &C, unsigned ArgNo) const {
return removeAttributes(C, ArgNo + FirstArgIndex);
}
/// \Brief Add the dereferenceable attribute to the attribute set at the given
/// index. Returns a new list because attribute lists are immutable.
AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index, AttributeList addDereferenceableAttr(LLVMContext &C, unsigned Index,
uint64_t Bytes) const; uint64_t Bytes) const;
/// \Brief Add the dereferenceable attribute to the attribute set at the given
/// arg index. Returns a new list because attribute lists are immutable.
AttributeList addDereferenceableParamAttr(LLVMContext &C, unsigned ArgNo,
uint64_t Bytes) const {
return addDereferenceableAttr(C, ArgNo + FirstArgIndex, Bytes);
}
/// \brief Add the dereferenceable_or_null attribute to the attribute set at /// \brief Add the dereferenceable_or_null attribute to the attribute set at
/// the given index. Because attribute sets are immutable, this returns a new /// the given index. Returns a new list because attribute lists are immutable.
/// set.
AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, AttributeList addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
uint64_t Bytes) const; uint64_t Bytes) const;
/// \brief Add the dereferenceable_or_null attribute to the attribute set at
/// the given arg index. Returns a new list because attribute lists are
/// immutable.
AttributeList addDereferenceableOrNullParamAttr(LLVMContext &C,
unsigned ArgNo,
uint64_t Bytes) const {
return addDereferenceableOrNullAttr(C, ArgNo + FirstArgIndex, Bytes);
}
/// Add the allocsize attribute to the attribute set at the given index. /// Add the allocsize attribute to the attribute set at the given index.
/// Because attribute sets are immutable, this returns a new set. /// Returns a new list because attribute lists are immutable.
AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index, AttributeList addAllocSizeAttr(LLVMContext &C, unsigned Index,
unsigned ElemSizeArg, unsigned ElemSizeArg,
const Optional<unsigned> &NumElemsArg); const Optional<unsigned> &NumElemsArg);
/// Add the allocsize attribute to the attribute set at the given arg index.
/// Returns a new list because attribute lists are immutable.
AttributeList addAllocSizeParamAttr(LLVMContext &C, unsigned ArgNo,
unsigned ElemSizeArg,
const Optional<unsigned> &NumElemsArg) {
return addAllocSizeAttr(C, ArgNo + FirstArgIndex, ElemSizeArg, NumElemsArg);
}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// AttributeList Accessors // AttributeList Accessors
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
@ -473,6 +535,21 @@ public:
/// \brief Return true if attribute exists at the given index. /// \brief Return true if attribute exists at the given index.
bool hasAttributes(unsigned Index) const; bool hasAttributes(unsigned Index) const;
/// \brief Return true if the attribute exists for the given argument
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
return hasAttribute(ArgNo + FirstArgIndex, Kind);
}
/// \brief Return true if the attribute exists for the given argument
bool hasParamAttr(unsigned ArgNo, StringRef Kind) const {
return hasAttribute(ArgNo + FirstArgIndex, Kind);
}
/// \brief Return true if attributes exists for the given argument
bool hasParamAttrs(unsigned ArgNo) const {
return hasAttributes(ArgNo + FirstArgIndex);
}
/// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but /// \brief Equivalent to hasAttribute(AttributeList::FunctionIndex, Kind) but
/// may be faster. /// may be faster.
bool hasFnAttribute(Attribute::AttrKind Kind) const; bool hasFnAttribute(Attribute::AttrKind Kind) const;
@ -496,6 +573,16 @@ public:
/// \brief Return the attribute object that exists at the given index. /// \brief Return the attribute object that exists at the given index.
Attribute getAttribute(unsigned Index, StringRef Kind) const; Attribute getAttribute(unsigned Index, StringRef Kind) const;
/// \brief Return the attribute object that exists at the arg index.
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
return getAttribute(ArgNo + FirstArgIndex, Kind);
}
/// \brief Return the attribute object that exists at the given index.
Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
return getAttribute(ArgNo + FirstArgIndex, Kind);
}
/// \brief Return the alignment of the return value. /// \brief Return the alignment of the return value.
unsigned getRetAlignment() const; unsigned getRetAlignment() const;
@ -508,10 +595,22 @@ public:
/// \brief Get the number of dereferenceable bytes (or zero if unknown). /// \brief Get the number of dereferenceable bytes (or zero if unknown).
uint64_t getDereferenceableBytes(unsigned Index) const; uint64_t getDereferenceableBytes(unsigned Index) const;
/// \brief Get the number of dereferenceable bytes (or zero if unknown) of an
/// arg.
uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
return getDereferenceableBytes(ArgNo + FirstArgIndex);
}
/// \brief Get the number of dereferenceable_or_null bytes (or zero if /// \brief Get the number of dereferenceable_or_null bytes (or zero if
/// unknown). /// unknown).
uint64_t getDereferenceableOrNullBytes(unsigned Index) const; uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
/// \brief Get the number of dereferenceable_or_null bytes (or zero if
/// unknown) of an arg.
uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
return getDereferenceableOrNullBytes(ArgNo + FirstArgIndex);
}
/// Get the allocsize argument numbers (or pair(0, 0) if unknown). /// Get the allocsize argument numbers (or pair(0, 0) if unknown).
std::pair<unsigned, Optional<unsigned>> std::pair<unsigned, Optional<unsigned>>
getAllocSizeArgs(unsigned Index) const; getAllocSizeArgs(unsigned Index) const;

View File

@ -214,10 +214,6 @@ public:
addAttribute(AttributeList::FunctionIndex, Attr); addAttribute(AttributeList::FunctionIndex, Attr);
} }
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
}
/// @brief Remove function attributes from this function. /// @brief Remove function attributes from this function.
void removeFnAttr(Attribute::AttrKind Kind) { void removeFnAttr(Attribute::AttrKind Kind) {
removeAttribute(AttributeList::FunctionIndex, Kind); removeAttribute(AttributeList::FunctionIndex, Kind);
@ -229,10 +225,6 @@ public:
getContext(), AttributeList::FunctionIndex, Kind)); getContext(), AttributeList::FunctionIndex, Kind));
} }
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
}
/// \brief Set the entry count for this function. /// \brief Set the entry count for this function.
/// ///
/// Entry count is the number of times this function was executed based on /// Entry count is the number of times this function was executed based on
@ -299,6 +291,15 @@ public:
/// @brief adds the attributes to the list of attributes. /// @brief adds the attributes to the list of attributes.
void addAttributes(unsigned i, const AttrBuilder &Attrs); void addAttributes(unsigned i, const AttrBuilder &Attrs);
/// @brief adds the attribute to the list of attributes for the given arg.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
/// @brief adds the attribute to the list of attributes for the given arg.
void addParamAttr(unsigned ArgNo, Attribute Attr);
/// @brief adds the attributes to the list of attributes for the given arg.
void addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
/// @brief removes the attribute from the list of attributes. /// @brief removes the attribute from the list of attributes.
void removeAttribute(unsigned i, Attribute::AttrKind Kind); void removeAttribute(unsigned i, Attribute::AttrKind Kind);
@ -308,6 +309,15 @@ public:
/// @brief removes the attributes from the list of attributes. /// @brief removes the attributes from the list of attributes.
void removeAttributes(unsigned i, const AttrBuilder &Attrs); void removeAttributes(unsigned i, const AttrBuilder &Attrs);
/// @brief removes the attribute from the list of attributes.
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
/// @brief removes the attribute from the list of attributes.
void removeParamAttr(unsigned ArgNo, StringRef Kind);
/// @brief removes the attribute from the list of attributes.
void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
/// @brief check if an attributes is in the list of attributes. /// @brief check if an attributes is in the list of attributes.
bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const { bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {
return getAttributes().hasAttribute(i, Kind); return getAttributes().hasAttribute(i, Kind);
@ -329,10 +339,18 @@ public:
/// @brief adds the dereferenceable attribute to the list of attributes. /// @brief adds the dereferenceable attribute to the list of attributes.
void addDereferenceableAttr(unsigned i, uint64_t Bytes); void addDereferenceableAttr(unsigned i, uint64_t Bytes);
/// @brief adds the dereferenceable attribute to the list of attributes for
/// the given arg.
void addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes);
/// @brief adds the dereferenceable_or_null attribute to the list of /// @brief adds the dereferenceable_or_null attribute to the list of
/// attributes. /// attributes.
void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes); void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
/// @brief adds the dereferenceable_or_null attribute to the list of
/// attributes for the given arg.
void addDereferenceableOrNullParamAttr(unsigned ArgNo, uint64_t Bytes);
/// @brief Extract the alignment for a call or parameter (0=unknown). /// @brief Extract the alignment for a call or parameter (0=unknown).
unsigned getParamAlignment(unsigned ArgNo) const { unsigned getParamAlignment(unsigned ArgNo) const {
return AttributeSets.getParamAlignment(ArgNo); return AttributeSets.getParamAlignment(ArgNo);
@ -345,6 +363,12 @@ public:
return AttributeSets.getDereferenceableBytes(i); return AttributeSets.getDereferenceableBytes(i);
} }
/// @brief Extract the number of dereferenceable bytes for a parameter.
/// @param ArgNo Index of an argument, with 0 being the first function arg.
uint64_t getParamDereferenceableBytes(unsigned ArgNo) const {
return AttributeSets.getParamDereferenceableBytes(ArgNo);
}
/// @brief Extract the number of dereferenceable_or_null bytes for a call or /// @brief Extract the number of dereferenceable_or_null bytes for a call or
/// parameter (0=unknown). /// parameter (0=unknown).
/// @param i AttributeList index, referring to a return value or argument. /// @param i AttributeList index, referring to a return value or argument.
@ -352,6 +376,13 @@ public:
return AttributeSets.getDereferenceableOrNullBytes(i); return AttributeSets.getDereferenceableOrNullBytes(i);
} }
/// @brief Extract the number of dereferenceable_or_null bytes for a
/// parameter.
/// @param ArgNo AttributeList ArgNo, referring to an argument.
uint64_t getParamDereferenceableOrNullBytes(unsigned ArgNo) const {
return AttributeSets.getParamDereferenceableOrNullBytes(ArgNo);
}
/// @brief Determine if the function does not access memory. /// @brief Determine if the function does not access memory.
bool doesNotAccessMemory() const { bool doesNotAccessMemory() const {
return hasFnAttribute(Attribute::ReadNone); return hasFnAttribute(Attribute::ReadNone);

View File

@ -1660,6 +1660,9 @@ public:
/// Adds the attribute to the indicated argument /// Adds the attribute to the indicated argument
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind); void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
/// Adds the attribute to the indicated argument
void addParamAttr(unsigned ArgNo, Attribute Attr);
/// removes the attribute from the list of attributes. /// removes the attribute from the list of attributes.
void removeAttribute(unsigned i, Attribute::AttrKind Kind); void removeAttribute(unsigned i, Attribute::AttrKind Kind);
@ -1669,6 +1672,9 @@ public:
/// Removes the attribute from the given argument /// Removes the attribute from the given argument
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind); void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
/// Removes the attribute from the given argument
void removeParamAttr(unsigned ArgNo, StringRef Kind);
/// adds the dereferenceable attribute to the list of attributes. /// adds the dereferenceable attribute to the list of attributes.
void addDereferenceableAttr(unsigned i, uint64_t Bytes); void addDereferenceableAttr(unsigned i, uint64_t Bytes);
@ -1704,6 +1710,18 @@ public:
return getAttributes().getAttribute(i, Kind); return getAttributes().getAttribute(i, Kind);
} }
/// Get the attribute of a given kind from a given arg
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
assert(ArgNo < getNumArgOperands() && "Out of bounds");
return getAttributes().getParamAttr(ArgNo, Kind);
}
/// Get the attribute of a given kind from a given arg
Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
assert(ArgNo < getNumArgOperands() && "Out of bounds");
return getAttributes().getParamAttr(ArgNo, Kind);
}
/// Return true if the data operand at index \p i has the attribute \p /// Return true if the data operand at index \p i has the attribute \p
/// A. /// A.
/// ///

View File

@ -1037,24 +1037,11 @@ AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
return addAttributes(C, Index, B); return addAttributes(C, Index, B);
} }
AttributeList AttributeList::addAttribute(LLVMContext &C, AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
ArrayRef<unsigned> Indices,
Attribute A) const { Attribute A) const {
assert(std::is_sorted(Indices.begin(), Indices.end())); AttrBuilder B;
B.addAttribute(A);
SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end()); return addAttributes(C, Index, B);
unsigned MaxIndex = attrIdxToArrayIdx(Indices.back());
if (MaxIndex >= AttrSets.size())
AttrSets.resize(MaxIndex + 1);
for (unsigned Index : Indices) {
Index = attrIdxToArrayIdx(Index);
AttrBuilder B(AttrSets[Index]);
B.addAttribute(A);
AttrSets[Index] = AttributeSet::get(C, B);
}
return getImpl(C, AttrSets);
} }
AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
@ -1086,6 +1073,26 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
return getImpl(C, AttrSets); return getImpl(C, AttrSets);
} }
AttributeList AttributeList::addParamAttribute(LLVMContext &C,
ArrayRef<unsigned> ArgNos,
Attribute A) const {
assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
if (MaxIndex >= AttrSets.size())
AttrSets.resize(MaxIndex + 1);
for (unsigned ArgNo : ArgNos) {
unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
AttrBuilder B(AttrSets[Index]);
B.addAttribute(A);
AttrSets[Index] = AttributeSet::get(C, B);
}
return getImpl(C, AttrSets);
}
AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const { Attribute::AttrKind Kind) const {
if (!hasAttribute(Index, Kind)) return *this; if (!hasAttribute(Index, Kind)) return *this;

View File

@ -118,15 +118,13 @@ unsigned Argument::getParamAlignment() const {
uint64_t Argument::getDereferenceableBytes() const { uint64_t Argument::getDereferenceableBytes() const {
assert(getType()->isPointerTy() && assert(getType()->isPointerTy() &&
"Only pointers have dereferenceable bytes"); "Only pointers have dereferenceable bytes");
return getParent()->getDereferenceableBytes(getArgNo() + return getParent()->getParamDereferenceableBytes(getArgNo());
AttributeList::FirstArgIndex);
} }
uint64_t Argument::getDereferenceableOrNullBytes() const { uint64_t Argument::getDereferenceableOrNullBytes() const {
assert(getType()->isPointerTy() && assert(getType()->isPointerTy() &&
"Only pointers have dereferenceable bytes"); "Only pointers have dereferenceable bytes");
return getParent()->getDereferenceableOrNullBytes( return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
getArgNo() + AttributeList::FirstArgIndex);
} }
bool Argument::hasNestAttr() const { bool Argument::hasNestAttr() const {
@ -169,21 +167,20 @@ bool Argument::onlyReadsMemory() const {
void Argument::addAttrs(AttrBuilder &B) { void Argument::addAttrs(AttrBuilder &B) {
AttributeList AL = getParent()->getAttributes(); AttributeList AL = getParent()->getAttributes();
AL = AL.addAttributes(Parent->getContext(), AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
getArgNo() + AttributeList::FirstArgIndex, B);
getParent()->setAttributes(AL); getParent()->setAttributes(AL);
} }
void Argument::addAttr(Attribute::AttrKind Kind) { void Argument::addAttr(Attribute::AttrKind Kind) {
getParent()->addAttribute(getArgNo() + AttributeList::FirstArgIndex, Kind); getParent()->addParamAttr(getArgNo(), Kind);
} }
void Argument::addAttr(Attribute Attr) { void Argument::addAttr(Attribute Attr) {
getParent()->addAttribute(getArgNo() + AttributeList::FirstArgIndex, Attr); getParent()->addParamAttr(getArgNo(), Attr);
} }
void Argument::removeAttr(Attribute::AttrKind Kind) { void Argument::removeAttr(Attribute::AttrKind Kind) {
getParent()->removeAttribute(getArgNo() + AttributeList::FirstArgIndex, Kind); getParent()->removeParamAttr(getArgNo(), Kind);
} }
bool Argument::hasAttribute(Attribute::AttrKind Kind) const { bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
@ -365,6 +362,24 @@ void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
setAttributes(PAL); setAttributes(PAL);
} }
void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
AttributeList PAL = getAttributes();
PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
}
void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
AttributeList PAL = getAttributes();
PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
setAttributes(PAL);
}
void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
AttributeList PAL = getAttributes();
PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs);
setAttributes(PAL);
}
void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) { void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
AttributeList PAL = getAttributes(); AttributeList PAL = getAttributes();
PAL = PAL.removeAttribute(getContext(), i, Kind); PAL = PAL.removeAttribute(getContext(), i, Kind);
@ -383,18 +398,49 @@ void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
setAttributes(PAL); setAttributes(PAL);
} }
void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
}
void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
}
void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs);
setAttributes(PAL);
}
void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
AttributeList PAL = getAttributes(); AttributeList PAL = getAttributes();
PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
setAttributes(PAL); setAttributes(PAL);
} }
void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
AttributeList PAL = getAttributes();
PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
setAttributes(PAL);
}
void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
AttributeList PAL = getAttributes(); AttributeList PAL = getAttributes();
PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
setAttributes(PAL); setAttributes(PAL);
} }
void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
uint64_t Bytes) {
AttributeList PAL = getAttributes();
PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes);
setAttributes(PAL);
}
const std::string &Function::getGC() const { const std::string &Function::getGC() const {
assert(hasGC() && "Function has no collector"); assert(hasGC() && "Function has no collector");
return getContext().getGC(*this); return getContext().getGC(*this);

View File

@ -393,7 +393,17 @@ void CallInst::addAttribute(unsigned i, Attribute Attr) {
} }
void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); assert(ArgNo < getNumArgOperands() && "Out of bounds");
AttributeList PAL = getAttributes();
PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
}
void CallInst::addParamAttr(unsigned ArgNo, Attribute Attr) {
assert(ArgNo < getNumArgOperands() && "Out of bounds");
AttributeList PAL = getAttributes();
PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
setAttributes(PAL);
} }
void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) { void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
@ -409,7 +419,17 @@ void CallInst::removeAttribute(unsigned i, StringRef Kind) {
} }
void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); assert(ArgNo < getNumArgOperands() && "Out of bounds");
AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
}
void CallInst::removeParamAttr(unsigned ArgNo, StringRef Kind) {
assert(ArgNo < getNumArgOperands() && "Out of bounds");
AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
} }
void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
@ -808,7 +828,9 @@ void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
} }
void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); AttributeList PAL = getAttributes();
PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
} }
void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) { void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
@ -824,7 +846,9 @@ void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
} }
void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind); AttributeList PAL = getAttributes();
PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
setAttributes(PAL);
} }
void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {

View File

@ -3838,24 +3838,24 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) {
// Mark any parameters that are known to be non-null with the nonnull // Mark any parameters that are known to be non-null with the nonnull
// attribute. This is helpful for inlining calls to functions with null // attribute. This is helpful for inlining calls to functions with null
// checks on their arguments. // checks on their arguments.
SmallVector<unsigned, 4> Indices; SmallVector<unsigned, 4> ArgNos;
unsigned ArgNo = 0; unsigned ArgNo = 0;
for (Value *V : CS.args()) { for (Value *V : CS.args()) {
if (V->getType()->isPointerTy() && if (V->getType()->isPointerTy() &&
!CS.paramHasAttr(ArgNo, Attribute::NonNull) && !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
isKnownNonNullAt(V, CS.getInstruction(), &DT)) isKnownNonNullAt(V, CS.getInstruction(), &DT))
Indices.push_back(ArgNo + AttributeList::FirstArgIndex); ArgNos.push_back(ArgNo);
ArgNo++; ArgNo++;
} }
assert(ArgNo == CS.arg_size() && "sanity check"); assert(ArgNo == CS.arg_size() && "sanity check");
if (!Indices.empty()) { if (!ArgNos.empty()) {
AttributeList AS = CS.getAttributes(); AttributeList AS = CS.getAttributes();
LLVMContext &Ctx = CS.getInstruction()->getContext(); LLVMContext &Ctx = CS.getInstruction()->getContext();
AS = AS.addAttribute(Ctx, Indices, AS = AS.addParamAttribute(Ctx, ArgNos,
Attribute::get(Ctx, Attribute::NonNull)); Attribute::get(Ctx, Attribute::NonNull));
CS.setAttributes(AS); CS.setAttributes(AS);
Changed = true; Changed = true;
} }

View File

@ -305,7 +305,7 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {
/// Infer nonnull attributes for the arguments at the specified callsite. /// Infer nonnull attributes for the arguments at the specified callsite.
static bool processCallSite(CallSite CS, LazyValueInfo *LVI) { static bool processCallSite(CallSite CS, LazyValueInfo *LVI) {
SmallVector<unsigned, 4> Indices; SmallVector<unsigned, 4> ArgNos;
unsigned ArgNo = 0; unsigned ArgNo = 0;
for (Value *V : CS.args()) { for (Value *V : CS.args()) {
@ -318,18 +318,19 @@ static bool processCallSite(CallSite CS, LazyValueInfo *LVI) {
LVI->getPredicateAt(ICmpInst::ICMP_EQ, V, LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
ConstantPointerNull::get(Type), ConstantPointerNull::get(Type),
CS.getInstruction()) == LazyValueInfo::False) CS.getInstruction()) == LazyValueInfo::False)
Indices.push_back(ArgNo + AttributeList::FirstArgIndex); ArgNos.push_back(ArgNo);
ArgNo++; ArgNo++;
} }
assert(ArgNo == CS.arg_size() && "sanity check"); assert(ArgNo == CS.arg_size() && "sanity check");
if (Indices.empty()) if (ArgNos.empty())
return false; return false;
AttributeList AS = CS.getAttributes(); AttributeList AS = CS.getAttributes();
LLVMContext &Ctx = CS.getInstruction()->getContext(); LLVMContext &Ctx = CS.getInstruction()->getContext();
AS = AS.addAttribute(Ctx, Indices, Attribute::get(Ctx, Attribute::NonNull)); AS = AS.addParamAttribute(Ctx, ArgNos,
Attribute::get(Ctx, Attribute::NonNull));
CS.setAttributes(AS); CS.setAttributes(AS);
return true; return true;