• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Attributes.h - Container for Attributes ------------*- 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 contains the simple types necessary to represent the
12 /// attributes associated with functions and their calls.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_IR_ATTRIBUTES_H
17 #define LLVM_IR_ATTRIBUTES_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/Support/PointerLikeTypeTraits.h"
22 #include <bitset>
23 #include <cassert>
24 #include <map>
25 #include <string>
26 
27 namespace llvm {
28 
29 class AttrBuilder;
30 class AttributeImpl;
31 class AttributeSetImpl;
32 class AttributeSetNode;
33 class Constant;
34 template<typename T> struct DenseMapInfo;
35 class LLVMContext;
36 class Type;
37 
38 //===----------------------------------------------------------------------===//
39 /// \class
40 /// \brief Functions, function parameters, and return types can have attributes
41 /// to indicate how they should be treated by optimizations and code
42 /// generation. This class represents one of those attributes. It's light-weight
43 /// and should be passed around by-value.
44 class Attribute {
45 public:
46   /// This enumeration lists the attributes that can be associated with
47   /// parameters, function results, or the function itself.
48   ///
49   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
50   /// entry in the unwind table. The `nounwind' attribute is about an exception
51   /// passing by the function.
52   ///
53   /// In a theoretical system that uses tables for profiling and SjLj for
54   /// exceptions, they would be fully independent. In a normal system that uses
55   /// tables for both, the semantics are:
56   ///
57   /// nil                = Needs an entry because an exception might pass by.
58   /// nounwind           = No need for an entry
59   /// uwtable            = Needs an entry because the ABI says so and because
60   ///                      an exception might pass by.
61   /// uwtable + nounwind = Needs an entry because the ABI says so.
62 
63   enum AttrKind {
64     // IR-Level Attributes
65     None,                  ///< No attributes have been set
66     Alignment,             ///< Alignment of parameter (5 bits)
67                            ///< stored as log2 of alignment with +1 bias
68                            ///< 0 means unaligned (different from align(1))
69     AlwaysInline,          ///< inline=always
70     ByVal,                 ///< Pass structure by value
71     InlineHint,            ///< Source said inlining was desirable
72     InReg,                 ///< Force argument to be passed in register
73     MinSize,               ///< Function must be optimized for size first
74     Naked,                 ///< Naked function
75     Nest,                  ///< Nested function static chain
76     NoAlias,               ///< Considered to not alias after call
77     NoBuiltin,             ///< Callee isn't recognized as a builtin
78     NoCapture,             ///< Function creates no aliases of pointer
79     NoDuplicate,           ///< Call cannot be duplicated
80     NoImplicitFloat,       ///< Disable implicit floating point insts
81     NoInline,              ///< inline=never
82     NonLazyBind,           ///< Function is called early and/or
83                            ///< often, so lazy binding isn't worthwhile
84     NoRedZone,             ///< Disable redzone
85     NoReturn,              ///< Mark the function as not returning
86     NoUnwind,              ///< Function doesn't unwind stack
87     OptimizeForSize,       ///< opt_size
88     ReadNone,              ///< Function does not access memory
89     ReadOnly,              ///< Function only reads from memory
90     ReturnsTwice,          ///< Function can return twice
91     SExt,                  ///< Sign extended before/after call
92     StackAlignment,        ///< Alignment of stack for function (3 bits)
93                            ///< stored as log2 of alignment with +1 bias 0
94                            ///< means unaligned (different from
95                            ///< alignstack=(1))
96     StackProtect,          ///< Stack protection.
97     StackProtectReq,       ///< Stack protection required.
98     StackProtectStrong,    ///< Strong Stack protection.
99     StructRet,             ///< Hidden pointer to structure to return
100     SanitizeAddress,       ///< AddressSanitizer is on.
101     SanitizeThread,        ///< ThreadSanitizer is on.
102     SanitizeMemory,        ///< MemorySanitizer is on.
103     UWTable,               ///< Function must be in a unwind table
104     ZExt,                  ///< Zero extended before/after call
105 
106     EndAttrKinds           ///< Sentinal value useful for loops
107   };
108 private:
109   AttributeImpl *pImpl;
Attribute(AttributeImpl * A)110   Attribute(AttributeImpl *A) : pImpl(A) {}
111 public:
Attribute()112   Attribute() : pImpl(0) {}
113 
114   //===--------------------------------------------------------------------===//
115   // Attribute Construction
116   //===--------------------------------------------------------------------===//
117 
118   /// \brief Return a uniquified Attribute object.
119   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
120   static Attribute get(LLVMContext &Context, StringRef Kind,
121                        StringRef Val = StringRef());
122 
123   /// \brief Return a uniquified Attribute object that has the specific
124   /// alignment set.
125   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
126   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
127 
128   //===--------------------------------------------------------------------===//
129   // Attribute Accessors
130   //===--------------------------------------------------------------------===//
131 
132   /// \brief Return true if the attribute is an Attribute::AttrKind type.
133   bool isEnumAttribute() const;
134 
135   /// \brief Return true if the attribute is an alignment attribute.
136   bool isAlignAttribute() const;
137 
138   /// \brief Return true if the attribute is a string (target-dependent)
139   /// attribute.
140   bool isStringAttribute() const;
141 
142   /// \brief Return true if the attribute is present.
143   bool hasAttribute(AttrKind Val) const;
144 
145   /// \brief Return true if the target-dependent attribute is present.
146   bool hasAttribute(StringRef Val) const;
147 
148   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
149   /// requires the attribute to be an enum or alignment attribute.
150   Attribute::AttrKind getKindAsEnum() const;
151 
152   /// \brief Return the attribute's value as an integer. This requires that the
153   /// attribute be an alignment attribute.
154   uint64_t getValueAsInt() const;
155 
156   /// \brief Return the attribute's kind as a string. This requires the
157   /// attribute to be a string attribute.
158   StringRef getKindAsString() const;
159 
160   /// \brief Return the attribute's value as a string. This requires the
161   /// attribute to be a string attribute.
162   StringRef getValueAsString() const;
163 
164   /// \brief Returns the alignment field of an attribute as a byte alignment
165   /// value.
166   unsigned getAlignment() const;
167 
168   /// \brief Returns the stack alignment field of an attribute as a byte
169   /// alignment value.
170   unsigned getStackAlignment() const;
171 
172   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
173   /// is, presumably, for writing out the mnemonics for the assembly writer.
174   std::string getAsString(bool InAttrGrp = false) const;
175 
176   /// \brief Equality and non-equality operators.
177   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
178   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
179 
180   /// \brief Less-than operator. Useful for sorting the attributes list.
181   bool operator<(Attribute A) const;
182 
Profile(FoldingSetNodeID & ID)183   void Profile(FoldingSetNodeID &ID) const {
184     ID.AddPointer(pImpl);
185   }
186 };
187 
188 //===----------------------------------------------------------------------===//
189 /// \class
190 /// \brief This class holds the attributes for a function, its return value, and
191 /// its parameters. You access the attributes for each of them via an index into
192 /// the AttributeSet object. The function attributes are at index
193 /// `AttributeSet::FunctionIndex', the return value is at index
194 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
195 /// index `1'.
196 class AttributeSet {
197 public:
198   enum AttrIndex {
199     ReturnIndex = 0U,
200     FunctionIndex = ~0U
201   };
202 private:
203   friend class AttrBuilder;
204   friend class AttributeSetImpl;
205   template <typename Ty> friend struct DenseMapInfo;
206 
207   /// \brief The attributes that we are managing. This can be null to represent
208   /// the empty attributes list.
209   AttributeSetImpl *pImpl;
210 
211   /// \brief The attributes for the specified index are returned.
212   AttributeSetNode *getAttributes(unsigned Idx) const;
213 
214   /// \brief Create an AttributeSet with the specified parameters in it.
215   static AttributeSet get(LLVMContext &C,
216                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
217   static AttributeSet get(LLVMContext &C,
218                           ArrayRef<std::pair<unsigned,
219                                              AttributeSetNode*> > Attrs);
220 
221   static AttributeSet getImpl(LLVMContext &C,
222                               ArrayRef<std::pair<unsigned,
223                                                  AttributeSetNode*> > Attrs);
224 
225 
AttributeSet(AttributeSetImpl * LI)226   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
227 public:
AttributeSet()228   AttributeSet() : pImpl(0) {}
229 
230   //===--------------------------------------------------------------------===//
231   // AttributeSet Construction and Mutation
232   //===--------------------------------------------------------------------===//
233 
234   /// \brief Return an AttributeSet with the specified parameters in it.
235   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
236   static AttributeSet get(LLVMContext &C, unsigned Idx,
237                           ArrayRef<Attribute::AttrKind> Kind);
238   static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
239 
240   /// \brief Add an attribute to the attribute set at the given index. Since
241   /// attribute sets are immutable, this returns a new set.
242   AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
243                             Attribute::AttrKind Attr) const;
244 
245   /// \brief Add an attribute to the attribute set at the given index. Since
246   /// attribute sets are immutable, this returns a new set.
247   AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
248                             StringRef Kind) const;
249 
250   /// \brief Add attributes to the attribute set at the given index. Since
251   /// attribute sets are immutable, this returns a new set.
252   AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
253                              AttributeSet Attrs) const;
254 
255   /// \brief Remove the specified attribute at the specified index from this
256   /// attribute list. Since attribute lists are immutable, this returns the new
257   /// list.
258   AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
259                                Attribute::AttrKind Attr) const;
260 
261   /// \brief Remove the specified attributes at the specified index from this
262   /// attribute list. Since attribute lists are immutable, this returns the new
263   /// list.
264   AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
265                                 AttributeSet Attrs) const;
266 
267   //===--------------------------------------------------------------------===//
268   // AttributeSet Accessors
269   //===--------------------------------------------------------------------===//
270 
271   /// \brief Retrieve the LLVM context.
272   LLVMContext &getContext() const;
273 
274   /// \brief The attributes for the specified index are returned.
275   AttributeSet getParamAttributes(unsigned Idx) const;
276 
277   /// \brief The attributes for the ret value are returned.
278   AttributeSet getRetAttributes() const;
279 
280   /// \brief The function attributes are returned.
281   AttributeSet getFnAttributes() const;
282 
283   /// \brief Return true if the attribute exists at the given index.
284   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
285 
286   /// \brief Return true if the attribute exists at the given index.
287   bool hasAttribute(unsigned Index, StringRef Kind) const;
288 
289   /// \brief Return true if attribute exists at the given index.
290   bool hasAttributes(unsigned Index) const;
291 
292   /// \brief Return true if the specified attribute is set for at least one
293   /// parameter or for the return value.
294   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
295 
296   /// \brief Return the attribute object that exists at the given index.
297   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
298 
299   /// \brief Return the attribute object that exists at the given index.
300   Attribute getAttribute(unsigned Index, StringRef Kind) const;
301 
302   /// \brief Return the alignment for the specified function parameter.
303   unsigned getParamAlignment(unsigned Idx) const;
304 
305   /// \brief Get the stack alignment.
306   unsigned getStackAlignment(unsigned Index) const;
307 
308   /// \brief Return the attributes at the index as a string.
309   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
310 
311   typedef ArrayRef<Attribute>::iterator iterator;
312 
313   iterator begin(unsigned Idx) const;
314   iterator end(unsigned Idx) const;
315 
316   /// operator==/!= - Provide equality predicates.
317   bool operator==(const AttributeSet &RHS) const {
318     return pImpl == RHS.pImpl;
319   }
320   bool operator!=(const AttributeSet &RHS) const {
321     return pImpl != RHS.pImpl;
322   }
323 
324   //===--------------------------------------------------------------------===//
325   // AttributeSet Introspection
326   //===--------------------------------------------------------------------===//
327 
328   // FIXME: Remove this.
329   uint64_t Raw(unsigned Index) const;
330 
331   /// \brief Return a raw pointer that uniquely identifies this attribute list.
getRawPointer()332   void *getRawPointer() const {
333     return pImpl;
334   }
335 
336   /// \brief Return true if there are no attributes.
isEmpty()337   bool isEmpty() const {
338     return getNumSlots() == 0;
339   }
340 
341   /// \brief Return the number of slots used in this attribute list.  This is
342   /// the number of arguments that have an attribute set on them (including the
343   /// function itself).
344   unsigned getNumSlots() const;
345 
346   /// \brief Return the index for the given slot.
347   uint64_t getSlotIndex(unsigned Slot) const;
348 
349   /// \brief Return the attributes at the given slot.
350   AttributeSet getSlotAttributes(unsigned Slot) const;
351 
352   void dump() const;
353 };
354 
355 //===----------------------------------------------------------------------===//
356 /// \class
357 /// \brief Provide DenseMapInfo for AttributeSet.
358 template<> struct DenseMapInfo<AttributeSet> {
359   static inline AttributeSet getEmptyKey() {
360     uintptr_t Val = static_cast<uintptr_t>(-1);
361     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
362     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
363   }
364   static inline AttributeSet getTombstoneKey() {
365     uintptr_t Val = static_cast<uintptr_t>(-2);
366     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
367     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
368   }
369   static unsigned getHashValue(AttributeSet AS) {
370     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
371            (unsigned((uintptr_t)AS.pImpl) >> 9);
372   }
373   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
374 };
375 
376 //===----------------------------------------------------------------------===//
377 /// \class
378 /// \brief This class is used in conjunction with the Attribute::get method to
379 /// create an Attribute object. The object itself is uniquified. The Builder's
380 /// value, however, is not. So this can be used as a quick way to test for
381 /// equality, presence of attributes, etc.
382 class AttrBuilder {
383   std::bitset<Attribute::EndAttrKinds> Attrs;
384   std::map<std::string, std::string> TargetDepAttrs;
385   uint64_t Alignment;
386   uint64_t StackAlignment;
387 public:
388   AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
389   explicit AttrBuilder(uint64_t Val)
390     : Attrs(0), Alignment(0), StackAlignment(0) {
391     addRawValue(Val);
392   }
393   AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
394     addAttribute(A);
395   }
396   AttrBuilder(AttributeSet AS, unsigned Idx);
397   AttrBuilder(const AttrBuilder &B)
398     : Attrs(B.Attrs),
399       TargetDepAttrs(B.TargetDepAttrs.begin(), B.TargetDepAttrs.end()),
400       Alignment(B.Alignment), StackAlignment(B.StackAlignment) {}
401 
402   void clear();
403 
404   /// \brief Add an attribute to the builder.
405   AttrBuilder &addAttribute(Attribute::AttrKind Val);
406 
407   /// \brief Add the Attribute object to the builder.
408   AttrBuilder &addAttribute(Attribute A);
409 
410   /// \brief Add the target-dependent attribute to the builder.
411   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
412 
413   /// \brief Remove an attribute from the builder.
414   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
415 
416   /// \brief Remove the attributes from the builder.
417   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
418 
419   /// \brief Remove the target-dependent attribute to the builder.
420   AttrBuilder &removeAttribute(StringRef A);
421 
422   /// \brief Add the attributes from the builder.
423   AttrBuilder &merge(const AttrBuilder &B);
424 
425   /// \brief Return true if the builder has the specified attribute.
426   bool contains(Attribute::AttrKind A) const {
427     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
428     return Attrs[A];
429   }
430 
431   /// \brief Return true if the builder has the specified target-dependent
432   /// attribute.
433   bool contains(StringRef A) const;
434 
435   /// \brief Return true if the builder has IR-level attributes.
436   bool hasAttributes() const;
437 
438   /// \brief Return true if the builder has any attribute that's in the
439   /// specified attribute.
440   bool hasAttributes(AttributeSet A, uint64_t Index) const;
441 
442   /// \brief Return true if the builder has an alignment attribute.
443   bool hasAlignmentAttr() const;
444 
445   /// \brief Retrieve the alignment attribute, if it exists.
446   uint64_t getAlignment() const { return Alignment; }
447 
448   /// \brief Retrieve the stack alignment attribute, if it exists.
449   uint64_t getStackAlignment() const { return StackAlignment; }
450 
451   /// \brief This turns an int alignment (which must be a power of 2) into the
452   /// form used internally in Attribute.
453   AttrBuilder &addAlignmentAttr(unsigned Align);
454 
455   /// \brief This turns an int stack alignment (which must be a power of 2) into
456   /// the form used internally in Attribute.
457   AttrBuilder &addStackAlignmentAttr(unsigned Align);
458 
459   /// \brief Return true if the builder contains no target-independent
460   /// attributes.
461   bool empty() const { return Attrs.none(); }
462 
463   // Iterators for target-dependent attributes.
464   typedef std::pair<std::string, std::string>                td_type;
465   typedef std::map<std::string, std::string>::iterator       td_iterator;
466   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
467 
468   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
469   td_iterator td_end()               { return TargetDepAttrs.end(); }
470 
471   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
472   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
473 
474   bool td_empty() const              { return TargetDepAttrs.empty(); }
475 
476   /// \brief Remove attributes that are used on functions only.
477   void removeFunctionOnlyAttrs();
478 
479   bool operator==(const AttrBuilder &B);
480   bool operator!=(const AttrBuilder &B) {
481     return !(*this == B);
482   }
483 
484   // FIXME: Remove this in 4.0.
485 
486   /// \brief Add the raw value to the internal representation.
487   AttrBuilder &addRawValue(uint64_t Val);
488 };
489 
490 namespace AttributeFuncs {
491 
492 /// \brief Which attributes cannot be applied to a type.
493 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
494 
495 } // end AttributeFuncs namespace
496 
497 } // end llvm namespace
498 
499 #endif
500