AttributeImpl.h revision 309124
1//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===// 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/// \file 11/// \brief This file defines various helper methods and classes used by 12/// LLVMContextImpl for creating and managing attributes. 13/// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H 17#define LLVM_LIB_IR_ATTRIBUTEIMPL_H 18 19#include "llvm/ADT/FoldingSet.h" 20#include "llvm/ADT/Optional.h" 21#include "llvm/IR/Attributes.h" 22#include "AttributeSetNode.h" 23#include "llvm/Support/DataTypes.h" 24#include <climits> 25#include <string> 26 27namespace llvm { 28 29class Constant; 30class LLVMContext; 31 32//===----------------------------------------------------------------------===// 33/// \class 34/// \brief This class represents a single, uniqued attribute. That attribute 35/// could be a single enum, a tuple, or a string. 36class AttributeImpl : public FoldingSetNode { 37 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute 38 39 // AttributesImpl is uniqued, these should not be publicly available. 40 void operator=(const AttributeImpl &) = delete; 41 AttributeImpl(const AttributeImpl &) = delete; 42 43protected: 44 enum AttrEntryKind { 45 EnumAttrEntry, 46 IntAttrEntry, 47 StringAttrEntry 48 }; 49 50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} 51 52public: 53 virtual ~AttributeImpl(); 54 55 bool isEnumAttribute() const { return KindID == EnumAttrEntry; } 56 bool isIntAttribute() const { return KindID == IntAttrEntry; } 57 bool isStringAttribute() const { return KindID == StringAttrEntry; } 58 59 bool hasAttribute(Attribute::AttrKind A) const; 60 bool hasAttribute(StringRef Kind) const; 61 62 Attribute::AttrKind getKindAsEnum() const; 63 uint64_t getValueAsInt() const; 64 65 StringRef getKindAsString() const; 66 StringRef getValueAsString() const; 67 68 /// \brief Used when sorting the attributes. 69 bool operator<(const AttributeImpl &AI) const; 70 71 void Profile(FoldingSetNodeID &ID) const { 72 if (isEnumAttribute()) 73 Profile(ID, getKindAsEnum(), 0); 74 else if (isIntAttribute()) 75 Profile(ID, getKindAsEnum(), getValueAsInt()); 76 else 77 Profile(ID, getKindAsString(), getValueAsString()); 78 } 79 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, 80 uint64_t Val) { 81 ID.AddInteger(Kind); 82 if (Val) ID.AddInteger(Val); 83 } 84 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { 85 ID.AddString(Kind); 86 if (!Values.empty()) ID.AddString(Values); 87 } 88 89 // FIXME: Remove this! 90 static uint64_t getAttrMask(Attribute::AttrKind Val); 91}; 92 93//===----------------------------------------------------------------------===// 94/// \class 95/// \brief A set of classes that contain the value of the 96/// attribute object. There are three main categories: enum attribute entries, 97/// represented by Attribute::AttrKind; alignment attribute entries; and string 98/// attribute enties, which are for target-dependent attributes. 99 100class EnumAttributeImpl : public AttributeImpl { 101 virtual void anchor(); 102 Attribute::AttrKind Kind; 103 104protected: 105 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) 106 : AttributeImpl(ID), Kind(Kind) {} 107 108public: 109 EnumAttributeImpl(Attribute::AttrKind Kind) 110 : AttributeImpl(EnumAttrEntry), Kind(Kind) {} 111 112 Attribute::AttrKind getEnumKind() const { return Kind; } 113}; 114 115class IntAttributeImpl : public EnumAttributeImpl { 116 void anchor() override; 117 uint64_t Val; 118 119public: 120 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) 121 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { 122 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment || 123 Kind == Attribute::Dereferenceable || 124 Kind == Attribute::DereferenceableOrNull || 125 Kind == Attribute::AllocSize) && 126 "Wrong kind for int attribute!"); 127 } 128 129 uint64_t getValue() const { return Val; } 130}; 131 132class StringAttributeImpl : public AttributeImpl { 133 virtual void anchor(); 134 std::string Kind; 135 std::string Val; 136 137public: 138 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) 139 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {} 140 141 StringRef getStringKind() const { return Kind; } 142 StringRef getStringValue() const { return Val; } 143}; 144 145typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair; 146 147//===----------------------------------------------------------------------===// 148/// \class 149/// \brief This class represents a set of attributes that apply to the function, 150/// return type, and parameters. 151class AttributeSetImpl final 152 : public FoldingSetNode, 153 private TrailingObjects<AttributeSetImpl, IndexAttrPair> { 154 friend class AttributeSet; 155 friend TrailingObjects; 156 157private: 158 LLVMContext &Context; 159 unsigned NumSlots; ///< Number of entries in this set. 160 /// Bitset with a bit for each available attribute Attribute::AttrKind. 161 uint64_t AvailableFunctionAttrs; 162 163 // Helper fn for TrailingObjects class. 164 size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; } 165 166 /// \brief Return a pointer to the IndexAttrPair for the specified slot. 167 const IndexAttrPair *getNode(unsigned Slot) const { 168 return getTrailingObjects<IndexAttrPair>() + Slot; 169 } 170 171 // AttributesSet is uniqued, these should not be publicly available. 172 void operator=(const AttributeSetImpl &) = delete; 173 AttributeSetImpl(const AttributeSetImpl &) = delete; 174public: 175 AttributeSetImpl(LLVMContext &C, 176 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Slots) 177 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) { 178 static_assert(Attribute::EndAttrKinds <= 179 sizeof(AvailableFunctionAttrs) * CHAR_BIT, 180 "Too many attributes"); 181 182#ifndef NDEBUG 183 if (Slots.size() >= 2) { 184 for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1, 185 *e = Slots.end(); 186 i != e; ++i) { 187 assert((i-1)->first <= i->first && "Attribute set not ordered!"); 188 } 189 } 190#endif 191 // There's memory after the node where we can store the entries in. 192 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>()); 193 194 // Initialize AvailableFunctionAttrs summary bitset. 195 if (NumSlots > 0) { 196 static_assert(AttributeSet::FunctionIndex == ~0u, 197 "FunctionIndex should be biggest possible index"); 198 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back(); 199 if (Last.first == AttributeSet::FunctionIndex) { 200 const AttributeSetNode *Node = Last.second; 201 for (Attribute I : *Node) { 202 if (!I.isStringAttribute()) 203 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum(); 204 } 205 } 206 } 207 } 208 209 void operator delete(void *p) { ::operator delete(p); } 210 211 /// \brief Get the context that created this AttributeSetImpl. 212 LLVMContext &getContext() { return Context; } 213 214 /// \brief Return the number of slots used in this attribute list. This is 215 /// the number of arguments that have an attribute set on them (including the 216 /// function itself). 217 unsigned getNumSlots() const { return NumSlots; } 218 219 /// \brief Get the index of the given "slot" in the AttrNodes list. This index 220 /// is the index of the return, parameter, or function object that the 221 /// attributes are applied to, not the index into the AttrNodes list where the 222 /// attributes reside. 223 unsigned getSlotIndex(unsigned Slot) const { 224 return getNode(Slot)->first; 225 } 226 227 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. 228 /// \p Slot is an index into the AttrNodes list, not the index of the return / 229 /// parameter/ function which the attributes apply to. 230 AttributeSet getSlotAttributes(unsigned Slot) const { 231 return AttributeSet::get(Context, *getNode(Slot)); 232 } 233 234 /// \brief Retrieve the attribute set node for the given "slot" in the 235 /// AttrNode list. 236 AttributeSetNode *getSlotNode(unsigned Slot) const { 237 return getNode(Slot)->second; 238 } 239 240 /// \brief Return true if the AttributeSetNode for the FunctionIndex has an 241 /// enum attribute of the given kind. 242 bool hasFnAttribute(Attribute::AttrKind Kind) const { 243 return AvailableFunctionAttrs & ((uint64_t)1) << Kind; 244 } 245 246 typedef AttributeSetNode::iterator iterator; 247 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); } 248 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); } 249 250 void Profile(FoldingSetNodeID &ID) const { 251 Profile(ID, makeArrayRef(getNode(0), getNumSlots())); 252 } 253 static void Profile(FoldingSetNodeID &ID, 254 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) { 255 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 256 ID.AddInteger(Nodes[i].first); 257 ID.AddPointer(Nodes[i].second); 258 } 259 } 260 261 // FIXME: This atrocity is temporary. 262 uint64_t Raw(unsigned Index) const; 263 264 void dump() const; 265}; 266 267} // end llvm namespace 268 269#endif 270