diff --git a/include/llvm/IR/Attributes.h b/include/llvm/IR/Attributes.h index fbfe5854594..b4056540663 100644 --- a/include/llvm/IR/Attributes.h +++ b/include/llvm/IR/Attributes.h @@ -399,6 +399,9 @@ private: static AttributeList getImpl(LLVMContext &C, ArrayRef AttrSets); + AttributeList setAttributes(LLVMContext &C, unsigned Index, + AttributeSet Attrs) const; + public: AttributeList() = default; diff --git a/lib/IR/Attributes.cpp b/lib/IR/Attributes.cpp index ec2b83f614d..b17673f14c9 100644 --- a/lib/IR/Attributes.cpp +++ b/lib/IR/Attributes.cpp @@ -1272,9 +1272,11 @@ AttributeList AttributeList::get(LLVMContext &C, AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const { if (hasAttribute(Index, Kind)) return *this; - AttrBuilder B; - B.addAttribute(Kind); - return addAttributes(C, Index, B); + AttributeSet Attrs = getAttributes(Index); + // TODO: Insert at correct position and avoid sort. + SmallVector NewAttrs(Attrs.begin(), Attrs.end()); + NewAttrs.push_back(Attribute::get(C, Kind)); + return setAttributes(C, Index, AttributeSet::get(C, NewAttrs)); } AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, @@ -1292,6 +1294,16 @@ AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, return addAttributes(C, Index, B); } +AttributeList AttributeList::setAttributes(LLVMContext &C, unsigned Index, + AttributeSet Attrs) const { + Index = attrIdxToArrayIdx(Index); + SmallVector AttrSets(this->begin(), this->end()); + if (Index >= AttrSets.size()) + AttrSets.resize(Index + 1); + AttrSets[Index] = Attrs; + return AttributeList::getImpl(C, AttrSets); +} + AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, const AttrBuilder &B) const { if (!B.hasAttributes()) @@ -1309,16 +1321,9 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, "Attempt to change alignment!"); #endif - Index = attrIdxToArrayIdx(Index); - SmallVector AttrSets(this->begin(), this->end()); - if (Index >= AttrSets.size()) - AttrSets.resize(Index + 1); - - AttrBuilder Merged(AttrSets[Index]); + AttrBuilder Merged(getAttributes(Index)); Merged.merge(B); - AttrSets[Index] = AttributeSet::get(C, Merged); - - return getImpl(C, AttrSets); + return setAttributes(C, Index, AttributeSet::get(C, Merged)); } AttributeList AttributeList::addParamAttribute(LLVMContext &C,