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