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