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