Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 10 | // This file implements the AttributesList class and Attribute utilities. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 14 | #include "llvm/Attributes.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 15 | #include "llvm/Type.h" |
Dan Gohman | fc42961 | 2008-03-10 23:55:07 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/FoldingSet.h" |
| 18 | #include "llvm/Support/Streams.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
| 20 | using namespace llvm; |
| 21 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 23 | // Attribute Function Definitions |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 25 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 26 | std::string Attribute::getAsString(Attributes Attrs) { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 27 | std::string Result; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 28 | if (Attrs & Attribute::ZExt) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | Result += "zeroext "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 30 | if (Attrs & Attribute::SExt) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 31 | Result += "signext "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 32 | if (Attrs & Attribute::NoReturn) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 33 | Result += "noreturn "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 34 | if (Attrs & Attribute::NoUnwind) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 35 | Result += "nounwind "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 36 | if (Attrs & Attribute::InReg) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 37 | Result += "inreg "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 38 | if (Attrs & Attribute::NoAlias) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 39 | Result += "noalias "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 40 | if (Attrs & Attribute::StructRet) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 41 | Result += "sret "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 42 | if (Attrs & Attribute::ByVal) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 43 | Result += "byval "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 44 | if (Attrs & Attribute::Nest) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 45 | Result += "nest "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 46 | if (Attrs & Attribute::ReadNone) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 47 | Result += "readnone "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 48 | if (Attrs & Attribute::ReadOnly) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 49 | Result += "readonly "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 50 | if (Attrs & Attribute::Alignment) { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 51 | Result += "align "; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 52 | Result += utostr((Attrs & Attribute::Alignment) >> 16); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 53 | Result += " "; |
| 54 | } |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 55 | // Trim the trailing space. |
| 56 | Result.erase(Result.end()-1); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 57 | return Result; |
| 58 | } |
| 59 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 60 | Attributes Attribute::typeIncompatible(const Type *Ty) { |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 61 | Attributes Incompatible = None; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 62 | |
| 63 | if (!Ty->isInteger()) |
| 64 | // Attributes that only apply to integers. |
| 65 | Incompatible |= SExt | ZExt; |
| 66 | |
| 67 | if (!isa<PointerType>(Ty)) |
| 68 | // Attributes that only apply to pointers. |
| 69 | Incompatible |= ByVal | Nest | NoAlias | StructRet; |
| 70 | |
| 71 | return Incompatible; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 75 | // AttributeListImpl Definition |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 78 | namespace llvm { |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 79 | class AttributeListImpl : public FoldingSetNode { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 80 | unsigned RefCount; |
| 81 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 82 | // AttributesList is uniqued, these should not be publicly available. |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 83 | void operator=(const AttributeListImpl &); // Do not implement |
| 84 | AttributeListImpl(const AttributeListImpl &); // Do not implement |
| 85 | ~AttributeListImpl(); // Private implementation |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 86 | public: |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 87 | SmallVector<AttributeWithIndex, 4> Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 88 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 89 | AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 90 | : Attrs(Attr, Attr+NumAttrs) { |
| 91 | RefCount = 0; |
| 92 | } |
| 93 | |
| 94 | void AddRef() { ++RefCount; } |
| 95 | void DropRef() { if (--RefCount == 0) delete this; } |
| 96 | |
| 97 | void Profile(FoldingSetNodeID &ID) const { |
| 98 | Profile(ID, &Attrs[0], Attrs.size()); |
| 99 | } |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 100 | static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr, |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 101 | unsigned NumAttrs) { |
| 102 | for (unsigned i = 0; i != NumAttrs; ++i) |
| 103 | ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index)); |
| 104 | } |
| 105 | }; |
| 106 | } |
| 107 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 108 | static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 109 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 110 | AttributeListImpl::~AttributeListImpl() { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 111 | AttributesLists->RemoveNode(this); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 115 | AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) { |
| 116 | // If there are no attributes then return a null AttributesList pointer. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 117 | if (NumAttrs == 0) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 118 | return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 120 | #ifndef NDEBUG |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i != NumAttrs; ++i) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 122 | assert(Attrs[i].Attrs != Attribute::None && |
| 123 | "Pointless attribute!"); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 124 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 125 | "Misordered AttributesList!"); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 126 | } |
| 127 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 129 | // Otherwise, build a key to look up the existing attributes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 130 | FoldingSetNodeID ID; |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 131 | AttributeListImpl::Profile(ID, Attrs, NumAttrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 132 | void *InsertPos; |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 133 | AttributeListImpl *PAL = |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 134 | AttributesLists->FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 136 | // If we didn't find any existing attributes of the same shape then |
| 137 | // create a new one and insert it. |
| 138 | if (!PAL) { |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 139 | PAL = new AttributeListImpl(Attrs, NumAttrs); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 140 | AttributesLists->InsertNode(PAL, InsertPos); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 142 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 143 | // Return the AttributesList that we found or created. |
| 144 | return AttrListPtr(PAL); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 148 | //===----------------------------------------------------------------------===// |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 149 | // AttrListPtr Method Implementations |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 150 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 151 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 152 | AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 153 | if (LI) LI->AddRef(); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 156 | AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) { |
| 157 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 160 | const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) { |
| 161 | if (AttrList == RHS.AttrList) return *this; |
| 162 | if (AttrList) AttrList->DropRef(); |
| 163 | AttrList = RHS.AttrList; |
| 164 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 165 | return *this; |
| 166 | } |
| 167 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 168 | AttrListPtr::~AttrListPtr() { |
| 169 | if (AttrList) AttrList->DropRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /// getNumSlots - Return the number of slots used in this attribute list. |
| 173 | /// This is the number of arguments that have an attribute set on them |
| 174 | /// (including the function itself). |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 175 | unsigned AttrListPtr::getNumSlots() const { |
| 176 | return AttrList ? AttrList->Attrs.size() : 0; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 179 | /// getSlot - Return the AttributeWithIndex at the specified slot. This |
| 180 | /// holds a number plus a set of attributes. |
| 181 | const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const { |
| 182 | assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!"); |
| 183 | return AttrList->Attrs[Slot]; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 187 | /// getAttributes - The attributes for the specified index are |
| 188 | /// returned. Attributes for the result are denoted with Idx = 0. |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 189 | /// Function notes are denoted with idx = ~0. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 190 | Attributes AttrListPtr::getAttributes(unsigned Idx) const { |
| 191 | if (AttrList == 0) return Attribute::None; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 192 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 193 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i) |
| 195 | if (Attrs[i].Index == Idx) |
| 196 | return Attrs[i].Attrs; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 197 | return Attribute::None; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 201 | /// least one parameter or for the return value. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 202 | bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const { |
| 203 | if (AttrList == 0) return false; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 204 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 205 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 206 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) |
| 207 | if (Attrs[i].Attrs & Attr) |
| 208 | return true; |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 213 | AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const { |
| 214 | Attributes OldAttrs = getAttributes(Idx); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 215 | #ifndef NDEBUG |
| 216 | // FIXME it is not obvious how this should work for alignment. |
| 217 | // For now, say we can't change a known alignment. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 218 | Attributes OldAlign = OldAttrs & Attribute::Alignment; |
| 219 | Attributes NewAlign = Attrs & Attribute::Alignment; |
Anton Korobeynikov | 18991d7 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 220 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 221 | "Attempt to change alignment!"); |
| 222 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 223 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 224 | Attributes NewAttrs = OldAttrs | Attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 225 | if (NewAttrs == OldAttrs) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 226 | return *this; |
| 227 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 228 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 229 | if (AttrList == 0) |
| 230 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 231 | else { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 232 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 233 | unsigned i = 0, e = OldAttrList.size(); |
| 234 | // Copy attributes for arguments before this one. |
| 235 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 236 | NewAttrList.push_back(OldAttrList[i]); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 238 | // If there are attributes already at this index, merge them in. |
| 239 | if (i != e && OldAttrList[i].Index == Idx) { |
| 240 | Attrs |= OldAttrList[i].Attrs; |
| 241 | ++i; |
| 242 | } |
| 243 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 244 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 245 | |
| 246 | // Copy attributes for arguments after this one. |
| 247 | NewAttrList.insert(NewAttrList.end(), |
| 248 | OldAttrList.begin()+i, OldAttrList.end()); |
| 249 | } |
| 250 | |
| 251 | return get(&NewAttrList[0], NewAttrList.size()); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 254 | AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 255 | #ifndef NDEBUG |
| 256 | // FIXME it is not obvious how this should work for alignment. |
| 257 | // For now, say we can't pass in alignment, which no current use does. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 258 | assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!"); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 259 | #endif |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 260 | if (AttrList == 0) return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 261 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 262 | Attributes OldAttrs = getAttributes(Idx); |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 263 | Attributes NewAttrs = OldAttrs & ~Attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 264 | if (NewAttrs == OldAttrs) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 265 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 266 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 267 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 268 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 269 | unsigned i = 0, e = OldAttrList.size(); |
| 270 | |
| 271 | // Copy attributes for arguments before this one. |
| 272 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 273 | NewAttrList.push_back(OldAttrList[i]); |
| 274 | |
| 275 | // If there are attributes already at this index, merge them in. |
| 276 | assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); |
| 277 | Attrs = OldAttrList[i].Attrs & ~Attrs; |
| 278 | ++i; |
| 279 | if (Attrs) // If any attributes left for this parameter, add them. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 280 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 281 | |
| 282 | // Copy attributes for arguments after this one. |
| 283 | NewAttrList.insert(NewAttrList.end(), |
| 284 | OldAttrList.begin()+i, OldAttrList.end()); |
| 285 | |
| 286 | return get(&NewAttrList[0], NewAttrList.size()); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 289 | void AttrListPtr::dump() const { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 290 | cerr << "PAL[ "; |
| 291 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame^] | 292 | const AttributeWithIndex &PAWI = getSlot(i); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 293 | cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} "; |
| 294 | } |
| 295 | |
| 296 | cerr << "]\n"; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 297 | } |