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 | } |
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 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 60 | ParameterAttributes ParamAttr::typeIncompatible(const Type *Ty) { |
| 61 | ParameterAttributes Incompatible = None; |
| 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 | //===----------------------------------------------------------------------===// |
| 75 | // ParamAttributeListImpl Definition |
| 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 { |
| 79 | class ParamAttributeListImpl : public FoldingSetNode { |
| 80 | unsigned RefCount; |
| 81 | |
Chris Lattner | 4d650c4 | 2008-03-13 04:33:03 +0000 | [diff] [blame] | 82 | // ParamAttrsList is uniqued, these should not be publicly available. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 83 | void operator=(const ParamAttributeListImpl &); // Do not implement |
| 84 | ParamAttributeListImpl(const ParamAttributeListImpl &); // Do not implement |
| 85 | ~ParamAttributeListImpl(); // Private implementation |
| 86 | public: |
| 87 | SmallVector<ParamAttrsWithIndex, 4> Attrs; |
| 88 | |
| 89 | ParamAttributeListImpl(const ParamAttrsWithIndex *Attr, unsigned NumAttrs) |
| 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 | } |
| 100 | static void Profile(FoldingSetNodeID &ID, const ParamAttrsWithIndex *Attr, |
| 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 | |
| 108 | static ManagedStatic<FoldingSet<ParamAttributeListImpl> > ParamAttrsLists; |
| 109 | |
| 110 | ParamAttributeListImpl::~ParamAttributeListImpl() { |
| 111 | ParamAttrsLists->RemoveNode(this); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | PAListPtr PAListPtr::get(const ParamAttrsWithIndex *Attrs, unsigned NumAttrs) { |
| 116 | // If there are no attributes then return a null ParamAttrsList pointer. |
| 117 | if (NumAttrs == 0) |
| 118 | return PAListPtr(); |
| 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) { |
| 122 | assert(Attrs[i].Attrs != ParamAttr::None && |
| 123 | "Pointless parameter attribute!"); |
| 124 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
| 125 | "Misordered ParamAttrsList!"); |
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; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 131 | ParamAttributeListImpl::Profile(ID, Attrs, NumAttrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 132 | void *InsertPos; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 133 | ParamAttributeListImpl *PAL = |
| 134 | ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos); |
| 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) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 139 | PAL = new ParamAttributeListImpl(Attrs, NumAttrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 140 | ParamAttrsLists->InsertNode(PAL, InsertPos); |
| 141 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 143 | // Return the ParamAttrsList that we found or created. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 144 | return PAListPtr(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 | //===----------------------------------------------------------------------===// |
| 149 | // PAListPtr Method Implementations |
| 150 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 152 | PAListPtr::PAListPtr(ParamAttributeListImpl *LI) : PAList(LI) { |
| 153 | if (LI) LI->AddRef(); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 156 | PAListPtr::PAListPtr(const PAListPtr &P) : PAList(P.PAList) { |
| 157 | if (PAList) PAList->AddRef(); |
| 158 | } |
| 159 | |
| 160 | const PAListPtr &PAListPtr::operator=(const PAListPtr &RHS) { |
| 161 | if (PAList == RHS.PAList) return *this; |
| 162 | if (PAList) PAList->DropRef(); |
| 163 | PAList = RHS.PAList; |
| 164 | if (PAList) PAList->AddRef(); |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | PAListPtr::~PAListPtr() { |
| 169 | if (PAList) PAList->DropRef(); |
| 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). |
| 175 | unsigned PAListPtr::getNumSlots() const { |
| 176 | return PAList ? PAList->Attrs.size() : 0; |
| 177 | } |
| 178 | |
| 179 | /// getSlot - Return the ParamAttrsWithIndex at the specified slot. This |
| 180 | /// holds a parameter number plus a set of attributes. |
| 181 | const ParamAttrsWithIndex &PAListPtr::getSlot(unsigned Slot) const { |
| 182 | assert(PAList && Slot < PAList->Attrs.size() && "Slot # out of range!"); |
| 183 | return PAList->Attrs[Slot]; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /// getParamAttrs - The parameter attributes for the specified parameter are |
| 188 | /// returned. Parameters 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. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 190 | ParameterAttributes PAListPtr::getParamAttrs(unsigned Idx) const { |
| 191 | if (PAList == 0) return ParamAttr::None; |
| 192 | |
| 193 | const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs; |
| 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; |
| 197 | return ParamAttr::None; |
| 198 | } |
| 199 | |
| 200 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 201 | /// least one parameter or for the return value. |
| 202 | bool PAListPtr::hasAttrSomewhere(ParameterAttributes Attr) const { |
| 203 | if (PAList == 0) return false; |
| 204 | |
| 205 | const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs; |
| 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 | |
| 213 | PAListPtr PAListPtr::addAttr(unsigned Idx, ParameterAttributes Attrs) const { |
| 214 | ParameterAttributes OldAttrs = getParamAttrs(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. |
| 218 | ParameterAttributes OldAlign = OldAttrs & ParamAttr::Alignment; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 219 | ParameterAttributes NewAlign = Attrs & ParamAttr::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 | |
| 224 | ParameterAttributes 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 | |
| 228 | SmallVector<ParamAttrsWithIndex, 8> NewAttrList; |
| 229 | if (PAList == 0) |
| 230 | NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs)); |
| 231 | else { |
| 232 | const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs; |
| 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 | |
| 244 | NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs)); |
| 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 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 254 | PAListPtr PAListPtr::removeAttr(unsigned Idx, ParameterAttributes 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. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 258 | assert(!(Attrs & ParamAttr::Alignment) && "Attempt to exclude alignment!"); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 259 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 260 | if (PAList == 0) return PAListPtr(); |
| 261 | |
| 262 | ParameterAttributes OldAttrs = getParamAttrs(Idx); |
| 263 | ParameterAttributes 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 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 267 | SmallVector<ParamAttrsWithIndex, 8> NewAttrList; |
| 268 | const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs; |
| 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. |
| 280 | NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs)); |
| 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 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 289 | void PAListPtr::dump() const { |
| 290 | cerr << "PAL[ "; |
| 291 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
| 292 | const ParamAttrsWithIndex &PAWI = getSlot(i); |
| 293 | cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} "; |
| 294 | } |
| 295 | |
| 296 | cerr << "]\n"; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 297 | } |