1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file defines various helper methods and classes used by 11 /// LLVMContextImpl for creating and managing attributes. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H 16 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/FoldingSet.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/IR/Attributes.h" 23 #include "llvm/Support/TrailingObjects.h" 24 #include <cassert> 25 #include <cstddef> 26 #include <cstdint> 27 #include <string> 28 #include <utility> 29 30 namespace llvm { 31 32 class LLVMContext; 33 class Type; 34 35 //===----------------------------------------------------------------------===// 36 /// \class 37 /// This class represents a single, uniqued attribute. That attribute 38 /// could be a single enum, a tuple, or a string. 39 class AttributeImpl : public FoldingSetNode { 40 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute 41 42 protected: 43 enum AttrEntryKind { 44 EnumAttrEntry, 45 IntAttrEntry, 46 StringAttrEntry, 47 TypeAttrEntry, 48 }; 49 50 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} 51 52 public: 53 // AttributesImpl is uniqued, these should not be available. 54 AttributeImpl(const AttributeImpl &) = delete; 55 AttributeImpl &operator=(const AttributeImpl &) = delete; 56 57 virtual ~AttributeImpl(); 58 59 bool isEnumAttribute() const { return KindID == EnumAttrEntry; } 60 bool isIntAttribute() const { return KindID == IntAttrEntry; } 61 bool isStringAttribute() const { return KindID == StringAttrEntry; } 62 bool isTypeAttribute() const { return KindID == TypeAttrEntry; } 63 64 bool hasAttribute(Attribute::AttrKind A) const; 65 bool hasAttribute(StringRef Kind) const; 66 67 Attribute::AttrKind getKindAsEnum() const; 68 uint64_t getValueAsInt() const; 69 70 StringRef getKindAsString() const; 71 StringRef getValueAsString() const; 72 73 Type *getValueAsType() const; 74 75 /// Used when sorting the attributes. 76 bool operator<(const AttributeImpl &AI) const; 77 78 void Profile(FoldingSetNodeID &ID) const { 79 if (isEnumAttribute()) 80 Profile(ID, getKindAsEnum(), static_cast<uint64_t>(0)); 81 else if (isIntAttribute()) 82 Profile(ID, getKindAsEnum(), getValueAsInt()); 83 else if (isStringAttribute()) 84 Profile(ID, getKindAsString(), getValueAsString()); 85 else 86 Profile(ID, getKindAsEnum(), getValueAsType()); 87 } 88 89 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, 90 uint64_t Val) { 91 ID.AddInteger(Kind); 92 if (Val) ID.AddInteger(Val); 93 } 94 95 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { 96 ID.AddString(Kind); 97 if (!Values.empty()) ID.AddString(Values); 98 } 99 100 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, 101 Type *Ty) { 102 ID.AddInteger(Kind); 103 ID.AddPointer(Ty); 104 } 105 }; 106 107 //===----------------------------------------------------------------------===// 108 /// \class 109 /// A set of classes that contain the value of the 110 /// attribute object. There are three main categories: enum attribute entries, 111 /// represented by Attribute::AttrKind; alignment attribute entries; and string 112 /// attribute enties, which are for target-dependent attributes. 113 114 class EnumAttributeImpl : public AttributeImpl { 115 virtual void anchor(); 116 117 Attribute::AttrKind Kind; 118 119 protected: 120 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) 121 : AttributeImpl(ID), Kind(Kind) {} 122 123 public: 124 EnumAttributeImpl(Attribute::AttrKind Kind) 125 : AttributeImpl(EnumAttrEntry), Kind(Kind) {} 126 127 Attribute::AttrKind getEnumKind() const { return Kind; } 128 }; 129 130 class IntAttributeImpl : public EnumAttributeImpl { 131 uint64_t Val; 132 133 void anchor() override; 134 135 public: 136 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val) 137 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) { 138 assert(Attribute::doesAttrKindHaveArgument(Kind) && 139 "Wrong kind for int attribute!"); 140 } 141 142 uint64_t getValue() const { return Val; } 143 }; 144 145 class StringAttributeImpl : public AttributeImpl { 146 virtual void anchor(); 147 148 std::string Kind; 149 std::string Val; 150 151 public: 152 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) 153 : AttributeImpl(StringAttrEntry), Kind(std::string(Kind)), 154 Val(std::string(Val)) {} 155 156 StringRef getStringKind() const { return Kind; } 157 StringRef getStringValue() const { return Val; } 158 }; 159 160 class TypeAttributeImpl : public EnumAttributeImpl { 161 void anchor() override; 162 163 Type *Ty; 164 165 public: 166 TypeAttributeImpl(Attribute::AttrKind Kind, Type *Ty) 167 : EnumAttributeImpl(TypeAttrEntry, Kind), Ty(Ty) {} 168 169 Type *getTypeValue() const { return Ty; } 170 }; 171 172 //===----------------------------------------------------------------------===// 173 /// \class 174 /// This class represents a group of attributes that apply to one 175 /// element: function, return type, or parameter. 176 class AttributeSetNode final 177 : public FoldingSetNode, 178 private TrailingObjects<AttributeSetNode, Attribute> { 179 friend TrailingObjects; 180 181 unsigned NumAttrs; ///< Number of attributes in this node. 182 /// Bitset with a bit for each available attribute Attribute::AttrKind. 183 uint8_t AvailableAttrs[12] = {}; 184 185 DenseMap<StringRef, Attribute> StringAttrs; 186 187 AttributeSetNode(ArrayRef<Attribute> Attrs); 188 189 static AttributeSetNode *getSorted(LLVMContext &C, 190 ArrayRef<Attribute> SortedAttrs); 191 Optional<Attribute> findEnumAttribute(Attribute::AttrKind Kind) const; 192 193 public: 194 // AttributesSetNode is uniqued, these should not be available. 195 AttributeSetNode(const AttributeSetNode &) = delete; 196 AttributeSetNode &operator=(const AttributeSetNode &) = delete; 197 198 void operator delete(void *p) { ::operator delete(p); } 199 200 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B); 201 202 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); 203 204 /// Return the number of attributes this AttributeList contains. 205 unsigned getNumAttributes() const { return NumAttrs; } 206 207 bool hasAttribute(Attribute::AttrKind Kind) const { 208 return AvailableAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8); 209 } 210 bool hasAttribute(StringRef Kind) const; 211 bool hasAttributes() const { return NumAttrs != 0; } 212 213 Attribute getAttribute(Attribute::AttrKind Kind) const; 214 Attribute getAttribute(StringRef Kind) const; 215 216 MaybeAlign getAlignment() const; 217 MaybeAlign getStackAlignment() const; 218 uint64_t getDereferenceableBytes() const; 219 uint64_t getDereferenceableOrNullBytes() const; 220 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; 221 std::string getAsString(bool InAttrGrp) const; 222 Type *getByValType() const; 223 Type *getPreallocatedType() const; 224 225 using iterator = const Attribute *; 226 227 iterator begin() const { return getTrailingObjects<Attribute>(); } 228 iterator end() const { return begin() + NumAttrs; } 229 230 void Profile(FoldingSetNodeID &ID) const { 231 Profile(ID, makeArrayRef(begin(), end())); 232 } 233 234 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { 235 for (const auto &Attr : AttrList) 236 Attr.Profile(ID); 237 } 238 }; 239 240 using IndexAttrPair = std::pair<unsigned, AttributeSet>; 241 242 //===----------------------------------------------------------------------===// 243 /// \class 244 /// This class represents a set of attributes that apply to the function, 245 /// return type, and parameters. 246 class AttributeListImpl final 247 : public FoldingSetNode, 248 private TrailingObjects<AttributeListImpl, AttributeSet> { 249 friend class AttributeList; 250 friend TrailingObjects; 251 252 private: 253 LLVMContext &Context; 254 unsigned NumAttrSets; ///< Number of entries in this set. 255 /// Bitset with a bit for each available attribute Attribute::AttrKind. 256 uint8_t AvailableFunctionAttrs[12] = {}; 257 258 // Helper fn for TrailingObjects class. 259 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; } 260 261 public: 262 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets); 263 264 // AttributesSetImpt is uniqued, these should not be available. 265 AttributeListImpl(const AttributeListImpl &) = delete; 266 AttributeListImpl &operator=(const AttributeListImpl &) = delete; 267 268 void operator delete(void *p) { ::operator delete(p); } 269 270 /// Get the context that created this AttributeListImpl. 271 LLVMContext &getContext() { return Context; } 272 273 /// Return true if the AttributeSet or the FunctionIndex has an 274 /// enum attribute of the given kind. 275 bool hasFnAttribute(Attribute::AttrKind Kind) const { 276 return AvailableFunctionAttrs[Kind / 8] & ((uint64_t)1) << (Kind % 8); 277 } 278 279 using iterator = const AttributeSet *; 280 281 iterator begin() const { return getTrailingObjects<AttributeSet>(); } 282 iterator end() const { return begin() + NumAttrSets; } 283 284 void Profile(FoldingSetNodeID &ID) const; 285 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes); 286 287 void dump() const; 288 }; 289 290 } // end namespace llvm 291 292 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H 293