Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
19 KiB

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