llvm.org GIT mirror llvm / 8fad356
Expose AttributeSetNode, use it to provide aggregate getter for attribute in the C API. Summary: See D19181 for context. Reviewers: whitequark, Wallbraker, jyknight, echristo, bkramer, void Subscribers: mehdi_amini Differential Revision: http://reviews.llvm.org/D21265 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@276236 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 3 years ago
5 changed file(s) with 133 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
209209 private:
210210 friend class AttrBuilder;
211211 friend class AttributeSetImpl;
212 friend class AttributeSetNode;
212213 template friend struct DenseMapInfo;
213214
214215 /// \brief The attributes that we are managing. This can be null to represent
20132013
20142014 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
20152015 LLVMAttributeRef A);
2016 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx);
2017 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
2018 LLVMAttributeRef *Attrs);
20162019 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
20172020 LLVMAttributeIndex Idx,
20182021 unsigned KindID);
25992602
26002603 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
26012604 LLVMAttributeRef A);
2605 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, LLVMAttributeIndex Idx);
2606 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2607 LLVMAttributeRef *Attrs);
26022608 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,
26032609 LLVMAttributeIndex Idx,
26042610 unsigned KindID);
1818 #include "llvm/ADT/FoldingSet.h"
1919 #include "llvm/ADT/Optional.h"
2020 #include "llvm/IR/Attributes.h"
21 #include "AttributeSetNode.h"
2122 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/TrailingObjects.h"
2323 #include
2424 #include
2525
139139
140140 StringRef getStringKind() const { return Kind; }
141141 StringRef getStringValue() const { return Val; }
142 };
143
144 //===----------------------------------------------------------------------===//
145 /// \class
146 /// \brief This class represents a group of attributes that apply to one
147 /// element: function, return type, or parameter.
148 class AttributeSetNode final
149 : public FoldingSetNode,
150 private TrailingObjects {
151 friend TrailingObjects;
152
153 unsigned NumAttrs; ///< Number of attributes in this node.
154 /// Bitset with a bit for each available attribute Attribute::AttrKind.
155 uint64_t AvailableAttrs;
156
157 AttributeSetNode(ArrayRef Attrs)
158 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
159 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
160 "Too many attributes for AvailableAttrs");
161 // There's memory after the node where we can store the entries in.
162 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects());
163
164 for (Attribute I : *this) {
165 if (!I.isStringAttribute()) {
166 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
167 }
168 }
169 }
170
171 // AttributesSetNode is uniqued, these should not be publicly available.
172 void operator=(const AttributeSetNode &) = delete;
173 AttributeSetNode(const AttributeSetNode &) = delete;
174 public:
175 void operator delete(void *p) { ::operator delete(p); }
176
177 static AttributeSetNode *get(LLVMContext &C, ArrayRef Attrs);
178
179 /// \brief Return the number of attributes this AttributeSet contains.
180 unsigned getNumAttributes() const { return NumAttrs; }
181
182 bool hasAttribute(Attribute::AttrKind Kind) const {
183 return AvailableAttrs & ((uint64_t)1) << Kind;
184 }
185 bool hasAttribute(StringRef Kind) const;
186 bool hasAttributes() const { return NumAttrs != 0; }
187
188 Attribute getAttribute(Attribute::AttrKind Kind) const;
189 Attribute getAttribute(StringRef Kind) const;
190
191 unsigned getAlignment() const;
192 unsigned getStackAlignment() const;
193 uint64_t getDereferenceableBytes() const;
194 uint64_t getDereferenceableOrNullBytes() const;
195 std::pair> getAllocSizeArgs() const;
196 std::string getAsString(bool InAttrGrp) const;
197
198 typedef const Attribute *iterator;
199 iterator begin() const { return getTrailingObjects(); }
200 iterator end() const { return begin() + NumAttrs; }
201
202 void Profile(FoldingSetNodeID &ID) const {
203 Profile(ID, makeArrayRef(begin(), end()));
204 }
205 static void Profile(FoldingSetNodeID &ID, ArrayRef AttrList) {
206 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
207 AttrList[I].Profile(ID);
208 }
209142 };
210143
211144 typedef std::pair IndexAttrPair;
0 //===-- AttributeSetNode.h - AttributeSet Internal Node ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief This file defines the node class used internally by AttributeSet.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_ATTRIBUTESETNODE_H
15 #define LLVM_IR_ATTRIBUTESETNODE_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/IR/Attributes.h"
19 #include "llvm/Support/TrailingObjects.h"
20
21 namespace llvm {
22
23 //===----------------------------------------------------------------------===//
24 /// \class
25 /// \brief This class represents a group of attributes that apply to one
26 /// element: function, return type, or parameter.
27 class AttributeSetNode final
28 : public FoldingSetNode,
29 private TrailingObjects {
30 friend TrailingObjects;
31
32 unsigned NumAttrs; ///< Number of attributes in this node.
33 /// Bitset with a bit for each available attribute Attribute::AttrKind.
34 uint64_t AvailableAttrs;
35
36 AttributeSetNode(ArrayRef Attrs)
37 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
38 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
39 "Too many attributes for AvailableAttrs");
40 // There's memory after the node where we can store the entries in.
41 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects());
42
43 for (Attribute I : *this) {
44 if (!I.isStringAttribute()) {
45 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
46 }
47 }
48 }
49
50 // AttributesSetNode is uniqued, these should not be publicly available.
51 void operator=(const AttributeSetNode &) = delete;
52 AttributeSetNode(const AttributeSetNode &) = delete;
53 public:
54 void operator delete(void *p) { ::operator delete(p); }
55
56 static AttributeSetNode *get(LLVMContext &C, ArrayRef Attrs);
57
58 static AttributeSetNode *get(AttributeSet AS, unsigned Index) {
59 return AS.getAttributes(Index);
60 }
61
62 /// \brief Return the number of attributes this AttributeSet contains.
63 unsigned getNumAttributes() const { return NumAttrs; }
64
65 bool hasAttribute(Attribute::AttrKind Kind) const {
66 return AvailableAttrs & ((uint64_t)1) << Kind;
67 }
68 bool hasAttribute(StringRef Kind) const;
69 bool hasAttributes() const { return NumAttrs != 0; }
70
71 Attribute getAttribute(Attribute::AttrKind Kind) const;
72 Attribute getAttribute(StringRef Kind) const;
73
74 unsigned getAlignment() const;
75 unsigned getStackAlignment() const;
76 uint64_t getDereferenceableBytes() const;
77 uint64_t getDereferenceableOrNullBytes() const;
78 std::pair> getAllocSizeArgs() const;
79 std::string getAsString(bool InAttrGrp) const;
80
81 typedef const Attribute *iterator;
82 iterator begin() const { return getTrailingObjects(); }
83 iterator end() const { return begin() + NumAttrs; }
84
85 void Profile(FoldingSetNodeID &ID) const {
86 Profile(ID, makeArrayRef(begin(), end()));
87 }
88 static void Profile(FoldingSetNodeID &ID, ArrayRef AttrList) {
89 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
90 AttrList[I].Profile(ID);
91 }
92 };
93
94 } // end llvm namespace
95
96 #endif
1515 #include "llvm/ADT/StringSwitch.h"
1616 #include "llvm/Bitcode/ReaderWriter.h"
1717 #include "llvm/IR/Attributes.h"
18 #include "AttributeSetNode.h"
1819 #include "llvm/IR/CallSite.h"
1920 #include "llvm/IR/Constants.h"
2021 #include "llvm/IR/DerivedTypes.h"
18431844 unwrap(F)->addAttribute(Idx, unwrap(A));
18441845 }
18451846
1847 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) {
1848 auto *ASN = AttributeSetNode::get(unwrap(F)->getAttributes(), Idx);
1849 return ASN->getNumAttributes();
1850 }
1851
1852 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
1853 LLVMAttributeRef *Attrs) {
1854 auto *ASN = AttributeSetNode::get(unwrap(F)->getAttributes(), Idx);
1855 for (auto A: make_range(ASN->begin(), ASN->end()))
1856 *Attrs++ = wrap(A);
1857 }
1858
18461859 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F,
18471860 LLVMAttributeIndex Idx,
18481861 unsigned KindID) {
22132226 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,
22142227 LLVMAttributeRef A) {
22152228 CallSite(unwrap(C)).addAttribute(Idx, unwrap(A));
2229 }
2230
2231 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C,
2232 LLVMAttributeIndex Idx) {
2233 auto CS = CallSite(unwrap(C));
2234 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2235 return ASN->getNumAttributes();
2236 }
2237
2238 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx,
2239 LLVMAttributeRef *Attrs) {
2240 auto CS = CallSite(unwrap(C));
2241 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx);
2242 for (auto A: make_range(ASN->begin(), ASN->end()))
2243 *Attrs++ = wrap(A);
22162244 }
22172245
22182246 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C,