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