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