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