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.

507 lines
20 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. // This file contains the simple types necessary to represent the
  11. // attributes associated with functions and their calls.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ATTRIBUTES_H
  15. #define LLVM_ATTRIBUTES_H
  16. #include "llvm/AttributesImpl.h"
  17. #include "llvm/Support/MathExtras.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include <cassert>
  20. #include <string>
  21. namespace llvm {
  22. class LLVMContext;
  23. class Type;
  24. namespace Attribute {
  25. /// AttrConst - We use this proxy POD type to allow constructing Attributes
  26. /// constants using initializer lists. Do not use this class directly.
  27. struct AttrConst {
  28. uint64_t v;
  29. AttrConst operator | (const AttrConst Attrs) const {
  30. AttrConst Res = {v | Attrs.v};
  31. return Res;
  32. }
  33. AttrConst operator ~ () const {
  34. AttrConst Res = {~v};
  35. return Res;
  36. }
  37. };
  38. /// Function parameters and results can have attributes to indicate how they
  39. /// should be treated by optimizations and code generation. This enumeration
  40. /// lists the attributes that can be associated with parameters, function
  41. /// results or the function itself.
  42. /// @brief Function attributes.
  43. /// We declare AttrConst objects that will be used throughout the code and also
  44. /// raw uint64_t objects with _i suffix to be used below for other constant
  45. /// declarations. This is done to avoid static CTORs and at the same time to
  46. /// keep type-safety of Attributes.
  47. #define DECLARE_LLVM_ATTRIBUTE(name, value) \
  48. const AttrConst name = {value};
  49. DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
  50. DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
  51. DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
  52. DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
  53. DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
  54. DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
  55. DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
  56. DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
  57. DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
  58. DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
  59. DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
  60. DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
  61. DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
  62. DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
  63. DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
  64. DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
  65. DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
  66. DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
  67. // stored as log2 of alignment with +1 bias
  68. // 0 means unaligned different from align 1
  69. DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
  70. DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
  71. DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
  72. /// instructions.
  73. DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
  74. DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
  75. ///desirable
  76. DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
  77. ///function (3 bits) stored as log2
  78. ///of alignment with +1 bias
  79. ///0 means unaligned (different from
  80. ///alignstack= {1))
  81. DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
  82. DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
  83. ///table
  84. DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
  85. /// often, so lazy binding isn't
  86. /// worthwhile.
  87. DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
  88. #undef DECLARE_LLVM_ATTRIBUTE
  89. /// Note that uwtable is about the ABI or the user mandating an entry in the
  90. /// unwind table. The nounwind attribute is about an exception passing by the
  91. /// function.
  92. /// In a theoretical system that uses tables for profiling and sjlj for
  93. /// exceptions, they would be fully independent. In a normal system that
  94. /// uses tables for both, the semantics are:
  95. /// nil = Needs an entry because an exception might pass by.
  96. /// nounwind = No need for an entry
  97. /// uwtable = Needs an entry because the ABI says so and because
  98. /// an exception might pass by.
  99. /// uwtable + nounwind = Needs an entry because the ABI says so.
  100. } // namespace Attribute
  101. /// AttributeImpl - The internal representation of the Attributes class. This is
  102. /// uniquified.
  103. class AttributesImpl;
  104. /// Attributes - A bitset of attributes.
  105. class Attributes {
  106. public:
  107. enum AttrVal {
  108. None = 0, ///< No attributes have been set
  109. AddressSafety = 1, ///< Address safety checking is on.
  110. Alignment = 2, ///< Alignment of parameter (5 bits)
  111. ///< stored as log2 of alignment with +1 bias
  112. ///< 0 means unaligned different from align 1
  113. AlwaysInline = 3, ///< inline=always
  114. ByVal = 4, ///< Pass structure by value
  115. InlineHint = 5, ///< Source said inlining was desirable
  116. InReg = 6, ///< Force argument to be passed in register
  117. Naked = 7, ///< Naked function
  118. Nest = 8, ///< Nested function static chain
  119. NoAlias = 9, ///< Considered to not alias after call
  120. NoCapture = 10, ///< Function creates no aliases of pointer
  121. NoImplicitFloat = 11, ///< Disable implicit floating point insts
  122. NoInline = 12, ///< inline=never
  123. NonLazyBind = 13, ///< Function is called early and/or
  124. ///< often, so lazy binding isn't worthwhile
  125. NoRedZone = 14, ///< Disable redzone
  126. NoReturn = 15, ///< Mark the function as not returning
  127. NoUnwind = 16, ///< Function doesn't unwind stack
  128. OptimizeForSize = 17, ///< opt_size
  129. ReadNone = 18, ///< Function does not access memory
  130. ReadOnly = 19, ///< Function only reads from memory
  131. ReturnsTwice = 20, ///< Function can return twice
  132. SExt = 21, ///< Sign extended before/after call
  133. StackAlignment = 22, ///< Alignment of stack for function (3 bits)
  134. ///< stored as log2 of alignment with +1 bias 0
  135. ///< means unaligned (different from
  136. ///< alignstack={1))
  137. StackProtect = 23, ///< Stack protection.
  138. StackProtectReq = 24, ///< Stack protection required.
  139. StructRet = 25, ///< Hidden pointer to structure to return
  140. UWTable = 26, ///< Function must be in a unwind table
  141. ZExt = 27 ///< Zero extended before/after call
  142. };
  143. private:
  144. AttributesImpl Attrs;
  145. explicit Attributes(AttributesImpl *A);
  146. public:
  147. Attributes() : Attrs(0) {}
  148. explicit Attributes(uint64_t Val);
  149. /*implicit*/ Attributes(Attribute::AttrConst Val);
  150. Attributes(const Attributes &A);
  151. class Builder {
  152. friend class Attributes;
  153. uint64_t Bits;
  154. public:
  155. Builder() : Bits(0) {}
  156. Builder(const Attributes &A) : Bits(A.Raw()) {}
  157. void clear() { Bits = 0; }
  158. bool hasAttributes() const;
  159. bool hasAttributes(const Attributes &A) const;
  160. bool hasAlignmentAttr() const;
  161. uint64_t getAlignment() const;
  162. Builder &addAttribute(Attributes::AttrVal Val);
  163. Builder &removeAttribute(Attributes::AttrVal Val);
  164. void addAlignmentAttr(unsigned Align);
  165. void addStackAlignmentAttr(unsigned Align);
  166. void removeAttributes(const Attributes &A);
  167. /// @brief Remove attributes that are used on functions only.
  168. void removeFunctionOnlyAttrs() {
  169. removeAttribute(Attributes::NoReturn)
  170. .removeAttribute(Attributes::NoUnwind)
  171. .removeAttribute(Attributes::ReadNone)
  172. .removeAttribute(Attributes::ReadOnly)
  173. .removeAttribute(Attributes::NoInline)
  174. .removeAttribute(Attributes::AlwaysInline)
  175. .removeAttribute(Attributes::OptimizeForSize)
  176. .removeAttribute(Attributes::StackProtect)
  177. .removeAttribute(Attributes::StackProtectReq)
  178. .removeAttribute(Attributes::NoRedZone)
  179. .removeAttribute(Attributes::NoImplicitFloat)
  180. .removeAttribute(Attributes::Naked)
  181. .removeAttribute(Attributes::InlineHint)
  182. .removeAttribute(Attributes::StackAlignment)
  183. .removeAttribute(Attributes::UWTable)
  184. .removeAttribute(Attributes::NonLazyBind)
  185. .removeAttribute(Attributes::ReturnsTwice)
  186. .removeAttribute(Attributes::AddressSafety);
  187. }
  188. };
  189. /// get - Return a uniquified Attributes object. This takes the uniquified
  190. /// value from the Builder and wraps it in the Attributes class.
  191. static Attributes get(Builder &B);
  192. static Attributes get(LLVMContext &Context, Builder &B);
  193. /// @brief Return true if the attribute is present.
  194. bool hasAttribute(AttrVal Val) const;
  195. /// @brief Return true if attributes exist
  196. bool hasAttributes() const {
  197. return Attrs.hasAttributes();
  198. }
  199. /// @brief Return true if the attributes are a non-null intersection.
  200. bool hasAttributes(const Attributes &A) const;
  201. /// @brief Returns the alignment field of an attribute as a byte alignment
  202. /// value.
  203. unsigned getAlignment() const;
  204. /// @brief Returns the stack alignment field of an attribute as a byte
  205. /// alignment value.
  206. unsigned getStackAlignment() const;
  207. /// @brief Parameter attributes that do not apply to vararg call arguments.
  208. bool hasIncompatibleWithVarArgsAttrs() const {
  209. return hasAttribute(Attributes::StructRet);
  210. }
  211. /// @brief Attributes that only apply to function parameters.
  212. bool hasParameterOnlyAttrs() const {
  213. return hasAttribute(Attributes::ByVal) ||
  214. hasAttribute(Attributes::Nest) ||
  215. hasAttribute(Attributes::StructRet) ||
  216. hasAttribute(Attributes::NoCapture);
  217. }
  218. /// @brief Attributes that may be applied to the function itself. These cannot
  219. /// be used on return values or function parameters.
  220. bool hasFunctionOnlyAttrs() const {
  221. return hasAttribute(Attributes::NoReturn) ||
  222. hasAttribute(Attributes::NoUnwind) ||
  223. hasAttribute(Attributes::ReadNone) ||
  224. hasAttribute(Attributes::ReadOnly) ||
  225. hasAttribute(Attributes::NoInline) ||
  226. hasAttribute(Attributes::AlwaysInline) ||
  227. hasAttribute(Attributes::OptimizeForSize) ||
  228. hasAttribute(Attributes::StackProtect) ||
  229. hasAttribute(Attributes::StackProtectReq) ||
  230. hasAttribute(Attributes::NoRedZone) ||
  231. hasAttribute(Attributes::NoImplicitFloat) ||
  232. hasAttribute(Attributes::Naked) ||
  233. hasAttribute(Attributes::InlineHint) ||
  234. hasAttribute(Attributes::StackAlignment) ||
  235. hasAttribute(Attributes::UWTable) ||
  236. hasAttribute(Attributes::NonLazyBind) ||
  237. hasAttribute(Attributes::ReturnsTwice) ||
  238. hasAttribute(Attributes::AddressSafety);
  239. }
  240. bool isEmptyOrSingleton() const;
  241. // This is a "safe bool() operator".
  242. operator const void *() const { return Attrs.Bits ? this : 0; }
  243. bool operator == (const Attributes &A) const {
  244. return Attrs.Bits == A.Attrs.Bits;
  245. }
  246. bool operator != (const Attributes &A) const {
  247. return Attrs.Bits != A.Attrs.Bits;
  248. }
  249. Attributes operator | (const Attributes &A) const;
  250. Attributes operator & (const Attributes &A) const;
  251. Attributes operator ^ (const Attributes &A) const;
  252. Attributes &operator |= (const Attributes &A);
  253. Attributes &operator &= (const Attributes &A);
  254. Attributes operator ~ () const;
  255. uint64_t Raw() const;
  256. /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
  257. /// normally) into the form used internally in Attributes.
  258. static Attributes constructAlignmentFromInt(unsigned i) {
  259. // Default alignment, allow the target to define how to align it.
  260. if (i == 0)
  261. return Attributes();
  262. assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
  263. assert(i <= 0x40000000 && "Alignment too large.");
  264. return Attributes((Log2_32(i)+1) << 16);
  265. }
  266. /// constructStackAlignmentFromInt - This turns an int stack alignment (which
  267. /// must be a power of 2) into the form used internally in Attributes.
  268. static Attributes constructStackAlignmentFromInt(unsigned i) {
  269. // Default alignment, allow the target to define how to align it.
  270. if (i == 0)
  271. return Attributes();
  272. assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
  273. assert(i <= 0x100 && "Alignment too large.");
  274. return Attributes((Log2_32(i)+1) << 26);
  275. }
  276. /// @brief Which attributes cannot be applied to a type.
  277. static Attributes typeIncompatible(Type *Ty);
  278. /// encodeLLVMAttributesForBitcode - This returns an integer containing an
  279. /// encoding of all the LLVM attributes found in the given attribute bitset.
  280. /// Any change to this encoding is a breaking change to bitcode compatibility.
  281. static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
  282. // FIXME: It doesn't make sense to store the alignment information as an
  283. // expanded out value, we should store it as a log2 value. However, we
  284. // can't just change that here without breaking bitcode compatibility. If
  285. // this ever becomes a problem in practice, we should introduce new tag
  286. // numbers in the bitcode file and have those tags use a more efficiently
  287. // encoded alignment field.
  288. // Store the alignment in the bitcode as a 16-bit raw value instead of a
  289. // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
  290. // bits.
  291. uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
  292. if (Attrs.hasAttribute(Attributes::Alignment))
  293. EncodedAttrs |= Attrs.getAlignment() << 16;
  294. EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
  295. return EncodedAttrs;
  296. }
  297. /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
  298. /// containing the LLVM attributes that have been decoded from the given
  299. /// integer. This function must stay in sync with
  300. /// 'encodeLLVMAttributesForBitcode'.
  301. static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
  302. // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
  303. // the bits above 31 down by 11 bits.
  304. unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
  305. assert((!Alignment || isPowerOf2_32(Alignment)) &&
  306. "Alignment must be a power of two.");
  307. Attributes Attrs(EncodedAttrs & 0xffff);
  308. if (Alignment)
  309. Attrs |= Attributes::constructAlignmentFromInt(Alignment);
  310. Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
  311. return Attrs;
  312. }
  313. /// getAsString - The set of Attributes set in Attributes is converted to a
  314. /// string of equivalent mnemonics. This is, presumably, for writing out the
  315. /// mnemonics for the assembly writer.
  316. /// @brief Convert attribute bits to text
  317. std::string getAsString() const;
  318. };
  319. //===----------------------------------------------------------------------===//
  320. // AttributeWithIndex
  321. //===----------------------------------------------------------------------===//
  322. /// AttributeWithIndex - This is just a pair of values to associate a set of
  323. /// attributes with an index.
  324. struct AttributeWithIndex {
  325. Attributes Attrs; ///< The attributes that are set, or'd together.
  326. unsigned Index; ///< Index of the parameter for which the attributes apply.
  327. ///< Index 0 is used for return value attributes.
  328. ///< Index ~0U is used for function attributes.
  329. static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
  330. AttributeWithIndex P;
  331. P.Index = Idx;
  332. P.Attrs = Attrs;
  333. return P;
  334. }
  335. };
  336. //===----------------------------------------------------------------------===//
  337. // AttrListPtr Smart Pointer
  338. //===----------------------------------------------------------------------===//
  339. class AttributeListImpl;
  340. /// AttrListPtr - This class manages the ref count for the opaque
  341. /// AttributeListImpl object and provides accessors for it.
  342. class AttrListPtr {
  343. /// AttrList - The attributes that we are managing. This can be null
  344. /// to represent the empty attributes list.
  345. AttributeListImpl *AttrList;
  346. public:
  347. AttrListPtr() : AttrList(0) {}
  348. AttrListPtr(const AttrListPtr &P);
  349. const AttrListPtr &operator=(const AttrListPtr &RHS);
  350. ~AttrListPtr();
  351. //===--------------------------------------------------------------------===//
  352. // Attribute List Construction and Mutation
  353. //===--------------------------------------------------------------------===//
  354. /// get - Return a Attributes list with the specified parameters in it.
  355. static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
  356. /// addAttr - Add the specified attribute at the specified index to this
  357. /// attribute list. Since attribute lists are immutable, this
  358. /// returns the new list.
  359. AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
  360. /// removeAttr - Remove the specified attribute at the specified index from
  361. /// this attribute list. Since attribute lists are immutable, this
  362. /// returns the new list.
  363. AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
  364. //===--------------------------------------------------------------------===//
  365. // Attribute List Accessors
  366. //===--------------------------------------------------------------------===//
  367. /// getParamAttributes - The attributes for the specified index are
  368. /// returned.
  369. Attributes getParamAttributes(unsigned Idx) const {
  370. return getAttributes(Idx);
  371. }
  372. /// getRetAttributes - The attributes for the ret value are
  373. /// returned.
  374. Attributes getRetAttributes() const {
  375. return getAttributes(0);
  376. }
  377. /// getFnAttributes - The function attributes are returned.
  378. Attributes getFnAttributes() const {
  379. return getAttributes(~0U);
  380. }
  381. /// paramHasAttr - Return true if the specified parameter index has the
  382. /// specified attribute set.
  383. bool paramHasAttr(unsigned Idx, Attributes Attr) const {
  384. return getAttributes(Idx).hasAttributes(Attr);
  385. }
  386. /// getParamAlignment - Return the alignment for the specified function
  387. /// parameter.
  388. unsigned getParamAlignment(unsigned Idx) const {
  389. return getAttributes(Idx).getAlignment();
  390. }
  391. /// hasAttrSomewhere - Return true if the specified attribute is set for at
  392. /// least one parameter or for the return value.
  393. bool hasAttrSomewhere(Attributes Attr) const;
  394. unsigned getNumAttrs() const;
  395. Attributes &getAttributesAtIndex(unsigned i) const;
  396. /// operator==/!= - Provide equality predicates.
  397. bool operator==(const AttrListPtr &RHS) const
  398. { return AttrList == RHS.AttrList; }
  399. bool operator!=(const AttrListPtr &RHS) const
  400. { return AttrList != RHS.AttrList; }
  401. void dump() const;
  402. //===--------------------------------------------------------------------===//
  403. // Attribute List Introspection
  404. //===--------------------------------------------------------------------===//
  405. /// getRawPointer - Return a raw pointer that uniquely identifies this
  406. /// attribute list.
  407. void *getRawPointer() const {
  408. return AttrList;
  409. }
  410. // Attributes are stored as a dense set of slots, where there is one
  411. // slot for each argument that has an attribute. This allows walking over the
  412. // dense set instead of walking the sparse list of attributes.
  413. /// isEmpty - Return true if there are no attributes.
  414. ///
  415. bool isEmpty() const {
  416. return AttrList == 0;
  417. }
  418. /// getNumSlots - Return the number of slots used in this attribute list.
  419. /// This is the number of arguments that have an attribute set on them
  420. /// (including the function itself).
  421. unsigned getNumSlots() const;
  422. /// getSlot - Return the AttributeWithIndex at the specified slot. This
  423. /// holds a index number plus a set of attributes.
  424. const AttributeWithIndex &getSlot(unsigned Slot) const;
  425. private:
  426. explicit AttrListPtr(AttributeListImpl *L);
  427. /// getAttributes - The attributes for the specified index are
  428. /// returned. Attributes for the result are denoted with Idx = 0.
  429. Attributes getAttributes(unsigned Idx) const;
  430. };
  431. } // End llvm namespace
  432. #endif