• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/Support/DataTypes.h"
23 #include "llvm/Support/TrailingObjects.h"
24 #include <climits>
25 #include <string>
26 
27 namespace llvm {
28 
29 class Constant;
30 class 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.
36 class 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 
43 protected:
44   enum AttrEntryKind {
45     EnumAttrEntry,
46     IntAttrEntry,
47     StringAttrEntry
48   };
49 
AttributeImpl(AttrEntryKind KindID)50   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 
52 public:
53   virtual ~AttributeImpl();
54 
isEnumAttribute()55   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
isIntAttribute()56   bool isIntAttribute() const { return KindID == IntAttrEntry; }
isStringAttribute()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 
Profile(FoldingSetNodeID & ID)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   }
Profile(FoldingSetNodeID & ID,Attribute::AttrKind Kind,uint64_t Val)79   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
80                       uint64_t Val) {
81     ID.AddInteger(Kind);
82     if (Val) ID.AddInteger(Val);
83   }
Profile(FoldingSetNodeID & ID,StringRef Kind,StringRef Values)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 
100 class EnumAttributeImpl : public AttributeImpl {
101   virtual void anchor();
102   Attribute::AttrKind Kind;
103 
104 protected:
EnumAttributeImpl(AttrEntryKind ID,Attribute::AttrKind Kind)105   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
106       : AttributeImpl(ID), Kind(Kind) {}
107 
108 public:
EnumAttributeImpl(Attribute::AttrKind Kind)109   EnumAttributeImpl(Attribute::AttrKind Kind)
110       : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
111 
getEnumKind()112   Attribute::AttrKind getEnumKind() const { return Kind; }
113 };
114 
115 class IntAttributeImpl : public EnumAttributeImpl {
116   void anchor() override;
117   uint64_t Val;
118 
119 public:
IntAttributeImpl(Attribute::AttrKind Kind,uint64_t Val)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 
getValue()129   uint64_t getValue() const { return Val; }
130 };
131 
132 class StringAttributeImpl : public AttributeImpl {
133   virtual void anchor();
134   std::string Kind;
135   std::string Val;
136 
137 public:
138   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
AttributeImpl(StringAttrEntry)139       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
140 
getStringKind()141   StringRef getStringKind() const { return Kind; }
getStringValue()142   StringRef getStringValue() const { return Val; }
143 };
144 
145 //===----------------------------------------------------------------------===//
146 /// \class
147 /// \brief This class represents a group of attributes that apply to one
148 /// element: function, return type, or parameter.
149 class AttributeSetNode final
150     : public FoldingSetNode,
151       private TrailingObjects<AttributeSetNode, Attribute> {
152   friend TrailingObjects;
153 
154   unsigned NumAttrs; ///< Number of attributes in this node.
155   /// Bitset with a bit for each available attribute Attribute::AttrKind.
156   uint64_t AvailableAttrs;
157 
AttributeSetNode(ArrayRef<Attribute> Attrs)158   AttributeSetNode(ArrayRef<Attribute> Attrs)
159     : NumAttrs(Attrs.size()), AvailableAttrs(0) {
160     static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
161                   "Too many attributes for AvailableAttrs");
162     // There's memory after the node where we can store the entries in.
163     std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
164 
165     for (Attribute I : *this) {
166       if (!I.isStringAttribute()) {
167         AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
168       }
169     }
170   }
171 
172   // AttributesSetNode is uniqued, these should not be publicly available.
173   void operator=(const AttributeSetNode &) = delete;
174   AttributeSetNode(const AttributeSetNode &) = delete;
175 public:
delete(void * p)176   void operator delete(void *p) { ::operator delete(p); }
177 
178   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
179 
180   /// \brief Return the number of attributes this AttributeSet contains.
getNumAttributes()181   unsigned getNumAttributes() const { return NumAttrs; }
182 
hasAttribute(Attribute::AttrKind Kind)183   bool hasAttribute(Attribute::AttrKind Kind) const {
184     return AvailableAttrs & ((uint64_t)1) << Kind;
185   }
186   bool hasAttribute(StringRef Kind) const;
hasAttributes()187   bool hasAttributes() const { return NumAttrs != 0; }
188 
189   Attribute getAttribute(Attribute::AttrKind Kind) const;
190   Attribute getAttribute(StringRef Kind) const;
191 
192   unsigned getAlignment() const;
193   unsigned getStackAlignment() const;
194   uint64_t getDereferenceableBytes() const;
195   uint64_t getDereferenceableOrNullBytes() const;
196   std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
197   std::string getAsString(bool InAttrGrp) const;
198 
199   typedef const Attribute *iterator;
begin()200   iterator begin() const { return getTrailingObjects<Attribute>(); }
end()201   iterator end() const { return begin() + NumAttrs; }
202 
Profile(FoldingSetNodeID & ID)203   void Profile(FoldingSetNodeID &ID) const {
204     Profile(ID, makeArrayRef(begin(), end()));
205   }
Profile(FoldingSetNodeID & ID,ArrayRef<Attribute> AttrList)206   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
207     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
208       AttrList[I].Profile(ID);
209   }
210 };
211 
212 typedef std::pair<unsigned, AttributeSetNode *> IndexAttrPair;
213 
214 //===----------------------------------------------------------------------===//
215 /// \class
216 /// \brief This class represents a set of attributes that apply to the function,
217 /// return type, and parameters.
218 class AttributeSetImpl final
219     : public FoldingSetNode,
220       private TrailingObjects<AttributeSetImpl, IndexAttrPair> {
221   friend class AttributeSet;
222   friend TrailingObjects;
223 
224 private:
225   LLVMContext &Context;
226   unsigned NumSlots; ///< Number of entries in this set.
227   /// Bitset with a bit for each available attribute Attribute::AttrKind.
228   uint64_t AvailableFunctionAttrs;
229 
230   // Helper fn for TrailingObjects class.
numTrailingObjects(OverloadToken<IndexAttrPair>)231   size_t numTrailingObjects(OverloadToken<IndexAttrPair>) { return NumSlots; }
232 
233   /// \brief Return a pointer to the IndexAttrPair for the specified slot.
getNode(unsigned Slot)234   const IndexAttrPair *getNode(unsigned Slot) const {
235     return getTrailingObjects<IndexAttrPair>() + Slot;
236   }
237 
238   // AttributesSet is uniqued, these should not be publicly available.
239   void operator=(const AttributeSetImpl &) = delete;
240   AttributeSetImpl(const AttributeSetImpl &) = delete;
241 public:
AttributeSetImpl(LLVMContext & C,ArrayRef<std::pair<unsigned,AttributeSetNode * >> Slots)242   AttributeSetImpl(LLVMContext &C,
243                    ArrayRef<std::pair<unsigned, AttributeSetNode *> > Slots)
244       : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
245     static_assert(Attribute::EndAttrKinds <=
246                       sizeof(AvailableFunctionAttrs) * CHAR_BIT,
247                   "Too many attributes");
248 
249 #ifndef NDEBUG
250     if (Slots.size() >= 2) {
251       for (const std::pair<unsigned, AttributeSetNode *> *i = Slots.begin() + 1,
252                                                          *e = Slots.end();
253            i != e; ++i) {
254         assert((i-1)->first <= i->first && "Attribute set not ordered!");
255       }
256     }
257 #endif
258     // There's memory after the node where we can store the entries in.
259     std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
260 
261     // Initialize AvailableFunctionAttrs summary bitset.
262     if (NumSlots > 0) {
263       static_assert(AttributeSet::FunctionIndex == ~0u,
264                     "FunctionIndex should be biggest possible index");
265       const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
266       if (Last.first == AttributeSet::FunctionIndex) {
267         const AttributeSetNode *Node = Last.second;
268         for (Attribute I : *Node) {
269           if (!I.isStringAttribute())
270             AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
271         }
272       }
273     }
274   }
275 
delete(void * p)276   void operator delete(void *p) { ::operator delete(p); }
277 
278   /// \brief Get the context that created this AttributeSetImpl.
getContext()279   LLVMContext &getContext() { return Context; }
280 
281   /// \brief Return the number of slots used in this attribute list. This is
282   /// the number of arguments that have an attribute set on them (including the
283   /// function itself).
getNumSlots()284   unsigned getNumSlots() const { return NumSlots; }
285 
286   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
287   /// is the index of the return, parameter, or function object that the
288   /// attributes are applied to, not the index into the AttrNodes list where the
289   /// attributes reside.
getSlotIndex(unsigned Slot)290   unsigned getSlotIndex(unsigned Slot) const {
291     return getNode(Slot)->first;
292   }
293 
294   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
295   /// \p Slot is an index into the AttrNodes list, not the index of the return /
296   /// parameter/ function which the attributes apply to.
getSlotAttributes(unsigned Slot)297   AttributeSet getSlotAttributes(unsigned Slot) const {
298     return AttributeSet::get(Context, *getNode(Slot));
299   }
300 
301   /// \brief Retrieve the attribute set node for the given "slot" in the
302   /// AttrNode list.
getSlotNode(unsigned Slot)303   AttributeSetNode *getSlotNode(unsigned Slot) const {
304     return getNode(Slot)->second;
305   }
306 
307   /// \brief Return true if the AttributeSetNode for the FunctionIndex has an
308   /// enum attribute of the given kind.
hasFnAttribute(Attribute::AttrKind Kind)309   bool hasFnAttribute(Attribute::AttrKind Kind) const {
310     return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
311   }
312 
313   typedef AttributeSetNode::iterator iterator;
begin(unsigned Slot)314   iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
end(unsigned Slot)315   iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
316 
Profile(FoldingSetNodeID & ID)317   void Profile(FoldingSetNodeID &ID) const {
318     Profile(ID, makeArrayRef(getNode(0), getNumSlots()));
319   }
Profile(FoldingSetNodeID & ID,ArrayRef<std::pair<unsigned,AttributeSetNode * >> Nodes)320   static void Profile(FoldingSetNodeID &ID,
321                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
322     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
323       ID.AddInteger(Nodes[i].first);
324       ID.AddPointer(Nodes[i].second);
325     }
326   }
327 
328   // FIXME: This atrocity is temporary.
329   uint64_t Raw(unsigned Index) const;
330 
331   void dump() const;
332 };
333 
334 } // end llvm namespace
335 
336 #endif
337