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 | |
Dale Johannesen | 09f410b | 2008-02-22 22:17:59 +0000 | [diff] [blame^] | 14 | #include "llvm/ParamAttrsList.h" |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ManagedStatic.h" |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 17 | #include <sstream> |
Gordon Henriksen | 8624d7b | 2008-01-03 03:21:18 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 21 | static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists; |
| 22 | |
| 23 | ParamAttrsList::ParamAttrsList(const ParamAttrsVector &attrVec) |
| 24 | : attrs(attrVec), refCount(0) { |
| 25 | } |
| 26 | |
| 27 | ParamAttrsList::~ParamAttrsList() { |
| 28 | ParamAttrsLists->RemoveNode(this); |
| 29 | } |
| 30 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 31 | ParameterAttributes |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 32 | ParamAttrsList::getParamAttrs(uint16_t Index) const { |
| 33 | unsigned limit = attrs.size(); |
| 34 | for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i) |
| 35 | if (attrs[i].index == Index) |
| 36 | return attrs[i].attrs; |
| 37 | return ParamAttr::None; |
| 38 | } |
| 39 | |
Duncan Sands | b5ca2e9 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 40 | bool ParamAttrsList::hasAttrSomewhere(ParameterAttributes attr) const { |
| 41 | for (unsigned i = 0, e = attrs.size(); i < e; ++i) |
| 42 | if (attrs[i].attrs & attr) |
| 43 | return true; |
| 44 | return false; |
| 45 | } |
| 46 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 47 | std::string |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 48 | ParamAttrsList::getParamAttrsText(ParameterAttributes Attrs) { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 49 | std::string Result; |
| 50 | if (Attrs & ParamAttr::ZExt) |
| 51 | Result += "zeroext "; |
| 52 | if (Attrs & ParamAttr::SExt) |
| 53 | Result += "signext "; |
| 54 | if (Attrs & ParamAttr::NoReturn) |
| 55 | Result += "noreturn "; |
| 56 | if (Attrs & ParamAttr::NoUnwind) |
| 57 | Result += "nounwind "; |
| 58 | if (Attrs & ParamAttr::InReg) |
| 59 | Result += "inreg "; |
| 60 | if (Attrs & ParamAttr::NoAlias) |
| 61 | Result += "noalias "; |
| 62 | if (Attrs & ParamAttr::StructRet) |
| 63 | Result += "sret "; |
| 64 | if (Attrs & ParamAttr::ByVal) |
| 65 | Result += "byval "; |
| 66 | if (Attrs & ParamAttr::Nest) |
| 67 | Result += "nest "; |
| 68 | if (Attrs & ParamAttr::ReadNone) |
| 69 | Result += "readnone "; |
| 70 | if (Attrs & ParamAttr::ReadOnly) |
| 71 | Result += "readonly "; |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 72 | if (Attrs & ParamAttr::Alignment) { |
| 73 | std::stringstream s; |
| 74 | s << ((Attrs & ParamAttr::Alignment) >> 16); |
| 75 | Result += "align "; |
| 76 | Result += s.str(); |
| 77 | Result += " "; |
| 78 | } |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 79 | return Result; |
| 80 | } |
| 81 | |
Chris Lattner | b18b0b9 | 2008-01-03 00:29:27 +0000 | [diff] [blame] | 82 | void ParamAttrsList::Profile(FoldingSetNodeID &ID, |
| 83 | const ParamAttrsVector &Attrs) { |
| 84 | for (unsigned i = 0; i < Attrs.size(); ++i) |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 85 | ID.AddInteger(uint64_t(Attrs[i].attrs) << 16 | unsigned(Attrs[i].index)); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 88 | const ParamAttrsList * |
| 89 | ParamAttrsList::get(const ParamAttrsVector &attrVec) { |
| 90 | // If there are no attributes then return a null ParamAttrsList pointer. |
| 91 | if (attrVec.empty()) |
| 92 | return 0; |
| 93 | |
| 94 | #ifndef NDEBUG |
| 95 | for (unsigned i = 0, e = attrVec.size(); i < e; ++i) { |
| 96 | assert(attrVec[i].attrs != ParamAttr::None |
| 97 | && "Pointless parameter attribute!"); |
| 98 | assert((!i || attrVec[i-1].index < attrVec[i].index) |
| 99 | && "Misordered ParamAttrsList!"); |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | // Otherwise, build a key to look up the existing attributes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 104 | FoldingSetNodeID ID; |
Chris Lattner | b18b0b9 | 2008-01-03 00:29:27 +0000 | [diff] [blame] | 105 | ParamAttrsList::Profile(ID, attrVec); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 106 | void *InsertPos; |
Chris Lattner | b18b0b9 | 2008-01-03 00:29:27 +0000 | [diff] [blame] | 107 | ParamAttrsList *PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 108 | |
| 109 | // If we didn't find any existing attributes of the same shape then |
| 110 | // create a new one and insert it. |
| 111 | if (!PAL) { |
| 112 | PAL = new ParamAttrsList(attrVec); |
| 113 | ParamAttrsLists->InsertNode(PAL, InsertPos); |
| 114 | } |
| 115 | |
| 116 | // Return the ParamAttrsList that we found or created. |
| 117 | return PAL; |
| 118 | } |
| 119 | |
| 120 | const ParamAttrsList * |
| 121 | ParamAttrsList::getModified(const ParamAttrsList *PAL, |
| 122 | const ParamAttrsVector &modVec) { |
| 123 | if (modVec.empty()) |
| 124 | return PAL; |
| 125 | |
| 126 | #ifndef NDEBUG |
| 127 | for (unsigned i = 0, e = modVec.size(); i < e; ++i) |
| 128 | assert((!i || modVec[i-1].index < modVec[i].index) |
| 129 | && "Misordered ParamAttrsList!"); |
| 130 | #endif |
| 131 | |
| 132 | if (!PAL) { |
| 133 | // Strip any instances of ParamAttr::None from modVec before calling 'get'. |
| 134 | ParamAttrsVector newVec; |
Duncan Sands | 2efbc7f | 2008-02-16 20:53:06 +0000 | [diff] [blame] | 135 | newVec.reserve(modVec.size()); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 136 | for (unsigned i = 0, e = modVec.size(); i < e; ++i) |
| 137 | if (modVec[i].attrs != ParamAttr::None) |
| 138 | newVec.push_back(modVec[i]); |
| 139 | return get(newVec); |
| 140 | } |
| 141 | |
| 142 | const ParamAttrsVector &oldVec = PAL->attrs; |
| 143 | |
| 144 | ParamAttrsVector newVec; |
| 145 | unsigned oldI = 0; |
| 146 | unsigned modI = 0; |
| 147 | unsigned oldE = oldVec.size(); |
| 148 | unsigned modE = modVec.size(); |
| 149 | |
| 150 | while (oldI < oldE && modI < modE) { |
| 151 | uint16_t oldIndex = oldVec[oldI].index; |
| 152 | uint16_t modIndex = modVec[modI].index; |
| 153 | |
| 154 | if (oldIndex < modIndex) { |
| 155 | newVec.push_back(oldVec[oldI]); |
| 156 | ++oldI; |
| 157 | } else if (modIndex < oldIndex) { |
| 158 | if (modVec[modI].attrs != ParamAttr::None) |
| 159 | newVec.push_back(modVec[modI]); |
| 160 | ++modI; |
| 161 | } else { |
| 162 | // Same index - overwrite or delete existing attributes. |
| 163 | if (modVec[modI].attrs != ParamAttr::None) |
| 164 | newVec.push_back(modVec[modI]); |
| 165 | ++oldI; |
| 166 | ++modI; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | for (; oldI < oldE; ++oldI) |
| 171 | newVec.push_back(oldVec[oldI]); |
| 172 | for (; modI < modE; ++modI) |
| 173 | if (modVec[modI].attrs != ParamAttr::None) |
| 174 | newVec.push_back(modVec[modI]); |
| 175 | |
| 176 | return get(newVec); |
| 177 | } |
| 178 | |
| 179 | const ParamAttrsList * |
| 180 | ParamAttrsList::includeAttrs(const ParamAttrsList *PAL, |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 181 | uint16_t idx, ParameterAttributes attrs) { |
| 182 | ParameterAttributes OldAttrs = PAL ? PAL->getParamAttrs(idx) : |
| 183 | ParamAttr::None; |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 184 | #ifndef NDEBUG |
| 185 | // FIXME it is not obvious how this should work for alignment. |
| 186 | // For now, say we can't change a known alignment. |
| 187 | ParameterAttributes OldAlign = OldAttrs & ParamAttr::Alignment; |
| 188 | ParameterAttributes NewAlign = attrs & ParamAttr::Alignment; |
Anton Korobeynikov | 18991d7 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 189 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 190 | "Attempt to change alignment!"); |
| 191 | #endif |
| 192 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 193 | ParameterAttributes NewAttrs = OldAttrs | attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 194 | if (NewAttrs == OldAttrs) |
| 195 | return PAL; |
| 196 | |
Duncan Sands | 2efbc7f | 2008-02-16 20:53:06 +0000 | [diff] [blame] | 197 | ParamAttrsVector modVec(1); |
| 198 | modVec[0] = ParamAttrsWithIndex::get(idx, NewAttrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 199 | return getModified(PAL, modVec); |
| 200 | } |
| 201 | |
| 202 | const ParamAttrsList * |
| 203 | ParamAttrsList::excludeAttrs(const ParamAttrsList *PAL, |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 204 | uint16_t idx, ParameterAttributes attrs) { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 205 | #ifndef NDEBUG |
| 206 | // FIXME it is not obvious how this should work for alignment. |
| 207 | // For now, say we can't pass in alignment, which no current use does. |
| 208 | assert(!(attrs & ParamAttr::Alignment) && "Attempt to exclude alignment!"); |
| 209 | #endif |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 210 | ParameterAttributes OldAttrs = PAL ? PAL->getParamAttrs(idx) : |
| 211 | ParamAttr::None; |
| 212 | ParameterAttributes NewAttrs = OldAttrs & ~attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 213 | if (NewAttrs == OldAttrs) |
| 214 | return PAL; |
| 215 | |
Duncan Sands | 2efbc7f | 2008-02-16 20:53:06 +0000 | [diff] [blame] | 216 | ParamAttrsVector modVec(1); |
| 217 | modVec[0] = ParamAttrsWithIndex::get(idx, NewAttrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 218 | return getModified(PAL, modVec); |
| 219 | } |
| 220 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 221 | ParameterAttributes ParamAttr::typeIncompatible (const Type *Ty) { |
| 222 | ParameterAttributes Incompatible = None; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 223 | |
| 224 | if (!Ty->isInteger()) |
Duncan Sands | b18c30a | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 225 | // Attributes that only apply to integers. |
| 226 | Incompatible |= SExt | ZExt; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 227 | |
Duncan Sands | 92a56b5 | 2008-01-21 21:37:41 +0000 | [diff] [blame] | 228 | if (!isa<PointerType>(Ty)) |
Duncan Sands | b18c30a | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 229 | // Attributes that only apply to pointers. |
| 230 | Incompatible |= ByVal | Nest | NoAlias | StructRet; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 231 | |
Duncan Sands | b18c30a | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 232 | return Incompatible; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 233 | } |