llvm.org GIT mirror llvm / 49f6060
Rewrite the addAttr() method. This now uses the AttributeSet object instead of the Attribute / AttributeWithIndex objects. It's fairly simple now. It goes through all of the subsets before the one we're modifying, adds them to the new set. It then adds the modified subset. And then adds the rest of the subsets. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173659 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
2 changed file(s) with 99 addition(s) and 97 deletion(s). Raw diff Collapse all Expand all
218218 /// \brief Add the specified attribute at the specified index to this
219219 /// attribute list. Since attribute lists are immutable, this returns the new
220220 /// list.
221 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
221 AttributeSet addAttr(LLVMContext &C, unsigned Idx, AttributeSet Attrs) const;
222222
223223 /// \brief Remove the specified attribute at the specified index from this
224224 /// attribute list. Since attribute lists are immutable, this returns the new
283283 AttributeSet Attrs) const;
284284
285285 //===--------------------------------------------------------------------===//
286 // Attribute List Accessors
286 // Attribute Set Accessors
287287 //===--------------------------------------------------------------------===//
288288
289289 /// \brief The attributes for the specified index are returned.
295295 /// \brief The function attributes are returned.
296296 AttributeSet getFnAttributes() const;
297297
298 /// \brief Return true if the attribute exists at the given index.
299 bool hasAttribute(uint64_t Index, Attribute::AttrKind Kind) const;
300
301 /// \brief Return true if attribute exists at the given index.
302 bool hasAttributes(uint64_t Index) const;
303
298304 /// \brief Return the alignment for the specified function parameter.
299 unsigned getParamAlignment(unsigned Idx) const;
300
301 /// \brief Return true if the attribute exists at the given index.
302 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
303
304 /// \brief Return true if attribute exists at the given index.
305 bool hasAttributes(unsigned Index) const;
306
307 /// \brief Returns the alignment field of an attribute as a byte alignment
308 /// value.
309 unsigned getAlignment(unsigned Index) const;
305 unsigned getParamAlignment(uint64_t Idx) const;
310306
311307 /// \brief Get the stack alignment.
312 unsigned getStackAlignment(unsigned Index) const;
308 unsigned getStackAlignment(uint64_t Index) const;
313309
314310 /// \brief Return the attributes at the index as a string.
315 std::string getAsString(unsigned Index) const;
311 std::string getAsString(uint64_t Index) const;
316312
317313 uint64_t Raw(unsigned Index) const;
318314
388384 /// \brief Remove an attribute from the builder.
389385 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
390386
391 /// \brief Add the attributes from A to the builder.
392 AttrBuilder &addAttributes(const Attribute &A);
393
394 /// \brief Remove the attributes from A from the builder.
395 AttrBuilder &removeAttributes(const Attribute &A);
387 /// \brief Add the attributes to the builder.
388 AttrBuilder &addAttributes(Attribute A);
389
390 /// \brief Remove the attributes from the builder.
391 AttrBuilder &removeAttributes(Attribute A);
392
393 /// \brief Add the attributes to the builder.
394 AttrBuilder &addAttributes(AttributeSet A);
396395
397396 /// \brief Return true if the builder has the specified attribute.
398397 bool contains(Attribute::AttrKind A) const;
245245 return *this;
246246 }
247247
248 AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
249 uint64_t Mask = Attr.Raw();
250
251 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
252 I = Attribute::AttrKind(I + 1))
253 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
254 Attrs.insert(I);
255
256 if (Attr.getAlignment())
257 Alignment = Attr.getAlignment();
258 if (Attr.getStackAlignment())
259 StackAlignment = Attr.getStackAlignment();
260 return *this;
261 }
262
263 AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
264 uint64_t Mask = A.Raw();
265
266 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
267 I = Attribute::AttrKind(I + 1)) {
268 if (Mask & AttributeImpl::getAttrMask(I)) {
269 Attrs.erase(I);
270
271 if (I == Attribute::Alignment)
272 Alignment = 0;
273 else if (I == Attribute::StackAlignment)
274 StackAlignment = 0;
275 }
276 }
277
278 return *this;
279 }
280
248281 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
249282 if (Align == 0) return *this;
250283
281314 }
282315 }
283316
284 return *this;
285 }
286
287 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
288 uint64_t Mask = Attr.Raw();
289
290 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
291 I = Attribute::AttrKind(I + 1))
292 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
293 Attrs.insert(I);
294
295 if (Attr.getAlignment())
296 Alignment = Attr.getAlignment();
297 if (Attr.getStackAlignment())
298 StackAlignment = Attr.getStackAlignment();
299 return *this;
300 }
301
302 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
303 uint64_t Mask = A.Raw();
304
305 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
306 I = Attribute::AttrKind(I + 1)) {
307 if (Mask & AttributeImpl::getAttrMask(I)) {
308 Attrs.erase(I);
309
310 if (I == Attribute::Alignment)
311 Alignment = 0;
312 else if (I == Attribute::StackAlignment)
313 StackAlignment = 0;
314 }
315 }
316
317317 return *this;
318318 }
319319
709709 return pImpl->getSlotAttributes(Slot);
710710 }
711711
712 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
712 bool AttributeSet::hasAttribute(uint64_t Index, Attribute::AttrKind Kind) const{
713713 return getAttributes(Index).hasAttribute(Kind);
714714 }
715715
716 bool AttributeSet::hasAttributes(unsigned Index) const {
716 bool AttributeSet::hasAttributes(uint64_t Index) const {
717717 return getAttributes(Index).hasAttributes();
718718 }
719719
720 std::string AttributeSet::getAsString(unsigned Index) const {
720 std::string AttributeSet::getAsString(uint64_t Index) const {
721721 return getAttributes(Index).getAsString();
722722 }
723723
724 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
724 unsigned AttributeSet::getParamAlignment(uint64_t Idx) const {
725725 return getAttributes(Idx).getAlignment();
726726 }
727727
728 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
728 unsigned AttributeSet::getStackAlignment(uint64_t Index) const {
729729 return getAttributes(Index).getStackAlignment();
730730 }
731731
770770
771771 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
772772 Attribute::AttrKind Attr) const {
773 return addAttr(C, Idx, Attribute::get(C, Attr));
773 return addAttr(C, Idx, AttributeSet::get(C, Idx, Attr));
774774 }
775775
776776 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
777777 AttributeSet Attrs) const {
778 return addAttr(C, Idx, Attrs.getAttributes(Idx));
778 return addAttr(C, Idx, Attrs);
779779 }
780780
781781 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
782 Attribute Attrs) const {
783 Attribute OldAttrs = getAttributes(Idx);
782 AttributeSet Attrs) const {
783 if (!pImpl) return Attrs;
784 if (!Attrs.pImpl) return *this;
785
784786 #ifndef NDEBUG
785 // FIXME it is not obvious how this should work for alignment.
786 // For now, say we can't change a known alignment.
787 unsigned OldAlign = OldAttrs.getAlignment();
788 unsigned NewAlign = Attrs.getAlignment();
787 // FIXME it is not obvious how this should work for alignment. For now, say
788 // we can't change a known alignment.
789 unsigned OldAlign = getParamAlignment(Idx);
790 unsigned NewAlign = Attrs.getParamAlignment(Idx);
789791 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
790792 "Attempt to change alignment!");
791793 #endif
792794
793 AttrBuilder NewAttrs =
794 AttrBuilder(OldAttrs).addAttributes(Attrs);
795 if (NewAttrs == AttrBuilder(OldAttrs))
796 return *this;
797
798 SmallVector NewAttrList;
799 if (pImpl == 0) {
800 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
801 } else {
802 ArrayRef OldAttrList = pImpl->getAttributes();
803 unsigned i = 0, e = OldAttrList.size();
804
805 // Copy attributes for arguments before this one.
806 for (; i != e && OldAttrList[i].Index < Idx; ++i)
807 NewAttrList.push_back(OldAttrList[i]);
808
809 // If there are attributes already at this index, merge them in.
810 if (i != e && OldAttrList[i].Index == Idx) {
811 Attrs =
812 Attribute::get(C, AttrBuilder(Attrs).
813 addAttributes(OldAttrList[i].Attrs));
814 ++i;
795 // Add the attribute slots before the one we're trying to add.
796 SmallVector AttrSet;
797 uint64_t NumAttrs = pImpl->getNumAttributes();
798 AttributeSet AS;
799 uint64_t LastIndex = 0;
800 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
801 if (getSlotIndex(I) >= Idx) {
802 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
803 break;
815804 }
816
817 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
818
819 // Copy attributes for arguments after this one.
820 NewAttrList.insert(NewAttrList.end(),
821 OldAttrList.begin()+i, OldAttrList.end());
822 }
823
824 return get(C, NewAttrList);
805 LastIndex = I + 1;
806 AttrSet.push_back(getSlotAttributes(I));
807 }
808
809 // Now add the attribute into the correct slot. There may already be an
810 // AttributeSet there.
811 AttrBuilder B(AS, Idx);
812
813 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
814 if (Attrs.getSlotIndex(I) == Idx) {
815 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
816 IE = Attrs.pImpl->end(I); II != IE; ++II)
817 B.addAttributes(*II);
818 break;
819 }
820
821 AttrSet.push_back(AttributeSet::get(C, Idx, B));
822
823 // Add the remaining attribute slots.
824 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
825 AttrSet.push_back(getSlotAttributes(I));
826
827 return get(C, AttrSet);
825828 }
826829
827830 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,