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.

430 lines
16 KiB

  1. //===--------- llvm/DataLayout.h - Data size & alignment info ---*- 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 defines layout properties related to datatype size/offset/alignment
  11. // information. It uses lazy annotations to cache information about how
  12. // structure types are laid out and used.
  13. //
  14. // This structure should be created once, filled in if the defaults are not
  15. // correct and then passed around by const&. None of the members functions
  16. // require modification to the object.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_DATALAYOUT_H
  20. #define LLVM_DATALAYOUT_H
  21. #include "llvm/Pass.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/Support/DataTypes.h"
  25. namespace llvm {
  26. class Value;
  27. class Type;
  28. class IntegerType;
  29. class StructType;
  30. class StructLayout;
  31. class GlobalVariable;
  32. class LLVMContext;
  33. template<typename T>
  34. class ArrayRef;
  35. /// Enum used to categorize the alignment types stored by LayoutAlignElem
  36. enum AlignTypeEnum {
  37. INTEGER_ALIGN = 'i', ///< Integer type alignment
  38. VECTOR_ALIGN = 'v', ///< Vector type alignment
  39. FLOAT_ALIGN = 'f', ///< Floating point type alignment
  40. AGGREGATE_ALIGN = 'a', ///< Aggregate alignment
  41. STACK_ALIGN = 's' ///< Stack objects alignment
  42. };
  43. /// Layout alignment element.
  44. ///
  45. /// Stores the alignment data associated with a given alignment type (integer,
  46. /// vector, float) and type bit width.
  47. ///
  48. /// @note The unusual order of elements in the structure attempts to reduce
  49. /// padding and make the structure slightly more cache friendly.
  50. struct LayoutAlignElem {
  51. unsigned AlignType : 8; ///< Alignment type (AlignTypeEnum)
  52. unsigned TypeBitWidth : 24; ///< Type bit width
  53. unsigned ABIAlign : 16; ///< ABI alignment for this type/bitw
  54. unsigned PrefAlign : 16; ///< Pref. alignment for this type/bitw
  55. /// Initializer
  56. static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
  57. unsigned pref_align, uint32_t bit_width);
  58. /// Equality predicate
  59. bool operator==(const LayoutAlignElem &rhs) const;
  60. };
  61. /// Layout pointer alignment element.
  62. ///
  63. /// Stores the alignment data associated with a given pointer and address space.
  64. ///
  65. /// @note The unusual order of elements in the structure attempts to reduce
  66. /// padding and make the structure slightly more cache friendly.
  67. struct PointerAlignElem {
  68. unsigned ABIAlign; ///< ABI alignment for this type/bitw
  69. unsigned PrefAlign; ///< Pref. alignment for this type/bitw
  70. uint32_t TypeBitWidth; ///< Type bit width
  71. uint32_t AddressSpace; ///< Address space for the pointer type
  72. /// Initializer
  73. static PointerAlignElem get(uint32_t addr_space, unsigned abi_align,
  74. unsigned pref_align, uint32_t bit_width);
  75. /// Equality predicate
  76. bool operator==(const PointerAlignElem &rhs) const;
  77. };
  78. /// DataLayout - This class holds a parsed version of the target data layout
  79. /// string in a module and provides methods for querying it. The target data
  80. /// layout string is specified *by the target* - a frontend generating LLVM IR
  81. /// is required to generate the right target data for the target being codegen'd
  82. /// to. If some measure of portability is desired, an empty string may be
  83. /// specified in the module.
  84. class DataLayout : public ImmutablePass {
  85. private:
  86. bool LittleEndian; ///< Defaults to false
  87. unsigned StackNaturalAlign; ///< Stack natural alignment
  88. SmallVector<unsigned char, 8> LegalIntWidths; ///< Legal Integers.
  89. /// Alignments- Where the primitive type alignment data is stored.
  90. ///
  91. /// @sa init().
  92. /// @note Could support multiple size pointer alignments, e.g., 32-bit
  93. /// pointers vs. 64-bit pointers by extending LayoutAlignment, but for now,
  94. /// we don't.
  95. SmallVector<LayoutAlignElem, 16> Alignments;
  96. DenseMap<unsigned, PointerAlignElem> Pointers;
  97. /// InvalidAlignmentElem - This member is a signal that a requested alignment
  98. /// type and bit width were not found in the SmallVector.
  99. static const LayoutAlignElem InvalidAlignmentElem;
  100. /// InvalidPointerElem - This member is a signal that a requested pointer
  101. /// type and bit width were not found in the DenseSet.
  102. static const PointerAlignElem InvalidPointerElem;
  103. // The StructType -> StructLayout map.
  104. mutable void *LayoutMap;
  105. //! Set/initialize target alignments
  106. void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
  107. unsigned pref_align, uint32_t bit_width);
  108. unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
  109. bool ABIAlign, Type *Ty) const;
  110. //! Set/initialize pointer alignments
  111. void setPointerAlignment(uint32_t addr_space, unsigned abi_align,
  112. unsigned pref_align, uint32_t bit_width);
  113. //! Internal helper method that returns requested alignment for type.
  114. unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
  115. /// Valid alignment predicate.
  116. ///
  117. /// Predicate that tests a LayoutAlignElem reference returned by get() against
  118. /// InvalidAlignmentElem.
  119. bool validAlignment(const LayoutAlignElem &align) const {
  120. return &align != &InvalidAlignmentElem;
  121. }
  122. /// Valid pointer predicate.
  123. ///
  124. /// Predicate that tests a PointerAlignElem reference returned by get() against
  125. /// InvalidPointerElem.
  126. bool validPointer(const PointerAlignElem &align) const {
  127. return &align != &InvalidPointerElem;
  128. }
  129. /// Initialise a DataLayout object with default values, ensure that the
  130. /// target data pass is registered.
  131. void init();
  132. public:
  133. /// Default ctor.
  134. ///
  135. /// @note This has to exist, because this is a pass, but it should never be
  136. /// used.
  137. DataLayout();
  138. /// Constructs a DataLayout from a specification string. See init().
  139. explicit DataLayout(StringRef LayoutDescription)
  140. : ImmutablePass(ID) {
  141. std::string errMsg = parseSpecifier(LayoutDescription, this);
  142. assert(errMsg == "" && "Invalid target data layout string.");
  143. (void)errMsg;
  144. }
  145. /// Parses a target data specification string. Returns an error message
  146. /// if the string is malformed, or the empty string on success. Optionally
  147. /// initialises a DataLayout object if passed a non-null pointer.
  148. static std::string parseSpecifier(StringRef LayoutDescription,
  149. DataLayout* td = 0);
  150. /// Initialize target data from properties stored in the module.
  151. explicit DataLayout(const Module *M);
  152. DataLayout(const DataLayout &TD) :
  153. ImmutablePass(ID),
  154. LittleEndian(TD.isLittleEndian()),
  155. LegalIntWidths(TD.LegalIntWidths),
  156. Alignments(TD.Alignments),
  157. Pointers(TD.Pointers),
  158. LayoutMap(0)
  159. { }
  160. ~DataLayout(); // Not virtual, do not subclass this class
  161. /// Layout endianness...
  162. bool isLittleEndian() const { return LittleEndian; }
  163. bool isBigEndian() const { return !LittleEndian; }
  164. /// getStringRepresentation - Return the string representation of the
  165. /// DataLayout. This representation is in the same format accepted by the
  166. /// string constructor above.
  167. std::string getStringRepresentation() const;
  168. /// isLegalInteger - This function returns true if the specified type is
  169. /// known to be a native integer type supported by the CPU. For example,
  170. /// i64 is not native on most 32-bit CPUs and i37 is not native on any known
  171. /// one. This returns false if the integer width is not legal.
  172. ///
  173. /// The width is specified in bits.
  174. ///
  175. bool isLegalInteger(unsigned Width) const {
  176. for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
  177. if (LegalIntWidths[i] == Width)
  178. return true;
  179. return false;
  180. }
  181. bool isIllegalInteger(unsigned Width) const {
  182. return !isLegalInteger(Width);
  183. }
  184. /// Returns true if the given alignment exceeds the natural stack alignment.
  185. bool exceedsNaturalStackAlignment(unsigned Align) const {
  186. return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
  187. }
  188. /// fitsInLegalInteger - This function returns true if the specified type fits
  189. /// in a native integer type supported by the CPU. For example, if the CPU
  190. /// only supports i32 as a native integer type, then i27 fits in a legal
  191. // integer type but i45 does not.
  192. bool fitsInLegalInteger(unsigned Width) const {
  193. for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
  194. if (Width <= LegalIntWidths[i])
  195. return true;
  196. return false;
  197. }
  198. /// Layout pointer alignment
  199. /// FIXME: The defaults need to be removed once all of
  200. /// the backends/clients are updated.
  201. unsigned getPointerABIAlignment(unsigned AS = 0) const {
  202. DenseMap<unsigned, PointerAlignElem>::const_iterator val = Pointers.find(AS);
  203. if (val == Pointers.end()) {
  204. val = Pointers.find(0);
  205. }
  206. return val->second.ABIAlign;
  207. }
  208. /// Return target's alignment for stack-based pointers
  209. /// FIXME: The defaults need to be removed once all of
  210. /// the backends/clients are updated.
  211. unsigned getPointerPrefAlignment(unsigned AS = 0) const {
  212. DenseMap<unsigned, PointerAlignElem>::const_iterator val = Pointers.find(AS);
  213. if (val == Pointers.end()) {
  214. val = Pointers.find(0);
  215. }
  216. return val->second.PrefAlign;
  217. }
  218. /// Layout pointer size
  219. /// FIXME: The defaults need to be removed once all of
  220. /// the backends/clients are updated.
  221. unsigned getPointerSize(unsigned AS = 0) const {
  222. DenseMap<unsigned, PointerAlignElem>::const_iterator val = Pointers.find(AS);
  223. if (val == Pointers.end()) {
  224. val = Pointers.find(0);
  225. }
  226. return val->second.TypeBitWidth;
  227. }
  228. /// Layout pointer size, in bits
  229. /// FIXME: The defaults need to be removed once all of
  230. /// the backends/clients are updated.
  231. unsigned getPointerSizeInBits(unsigned AS = 0) const {
  232. DenseMap<unsigned, PointerAlignElem>::const_iterator val = Pointers.find(AS);
  233. if (val == Pointers.end()) {
  234. val = Pointers.find(0);
  235. }
  236. return 8*val->second.TypeBitWidth;
  237. }
  238. /// Size examples:
  239. ///
  240. /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
  241. /// ---- ---------- --------------- ---------------
  242. /// i1 1 8 8
  243. /// i8 8 8 8
  244. /// i19 19 24 32
  245. /// i32 32 32 32
  246. /// i100 100 104 128
  247. /// i128 128 128 128
  248. /// Float 32 32 32
  249. /// Double 64 64 64
  250. /// X86_FP80 80 80 96
  251. ///
  252. /// [*] The alloc size depends on the alignment, and thus on the target.
  253. /// These values are for x86-32 linux.
  254. /// getTypeSizeInBits - Return the number of bits necessary to hold the
  255. /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
  256. uint64_t getTypeSizeInBits(Type* Ty) const;
  257. /// getTypeStoreSize - Return the maximum number of bytes that may be
  258. /// overwritten by storing the specified type. For example, returns 5
  259. /// for i36 and 10 for x86_fp80.
  260. uint64_t getTypeStoreSize(Type *Ty) const {
  261. return (getTypeSizeInBits(Ty)+7)/8;
  262. }
  263. /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
  264. /// overwritten by storing the specified type; always a multiple of 8. For
  265. /// example, returns 40 for i36 and 80 for x86_fp80.
  266. uint64_t getTypeStoreSizeInBits(Type *Ty) const {
  267. return 8*getTypeStoreSize(Ty);
  268. }
  269. /// getTypeAllocSize - Return the offset in bytes between successive objects
  270. /// of the specified type, including alignment padding. This is the amount
  271. /// that alloca reserves for this type. For example, returns 12 or 16 for
  272. /// x86_fp80, depending on alignment.
  273. uint64_t getTypeAllocSize(Type* Ty) const {
  274. // Round up to the next alignment boundary.
  275. return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
  276. }
  277. /// getTypeAllocSizeInBits - Return the offset in bits between successive
  278. /// objects of the specified type, including alignment padding; always a
  279. /// multiple of 8. This is the amount that alloca reserves for this type.
  280. /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
  281. uint64_t getTypeAllocSizeInBits(Type* Ty) const {
  282. return 8*getTypeAllocSize(Ty);
  283. }
  284. /// getABITypeAlignment - Return the minimum ABI-required alignment for the
  285. /// specified type.
  286. unsigned getABITypeAlignment(Type *Ty) const;
  287. /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
  288. /// an integer type of the specified bitwidth.
  289. unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
  290. /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment
  291. /// for the specified type when it is part of a call frame.
  292. unsigned getCallFrameTypeAlignment(Type *Ty) const;
  293. /// getPrefTypeAlignment - Return the preferred stack/global alignment for
  294. /// the specified type. This is always at least as good as the ABI alignment.
  295. unsigned getPrefTypeAlignment(Type *Ty) const;
  296. /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
  297. /// specified type, returned as log2 of the value (a shift amount).
  298. ///
  299. unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
  300. /// getIntPtrType - Return an unsigned integer type that is the same size or
  301. /// greater to the host pointer size.
  302. /// FIXME: Need to remove the default argument when the rest of the LLVM code
  303. /// base has been updated.
  304. IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
  305. /// getIndexedOffset - return the offset from the beginning of the type for
  306. /// the specified indices. This is used to implement getelementptr.
  307. ///
  308. uint64_t getIndexedOffset(Type *Ty, ArrayRef<Value *> Indices) const;
  309. /// getStructLayout - Return a StructLayout object, indicating the alignment
  310. /// of the struct, its size, and the offsets of its fields. Note that this
  311. /// information is lazily cached.
  312. const StructLayout *getStructLayout(StructType *Ty) const;
  313. /// getPreferredAlignment - Return the preferred alignment of the specified
  314. /// global. This includes an explicitly requested alignment (if the global
  315. /// has one).
  316. unsigned getPreferredAlignment(const GlobalVariable *GV) const;
  317. /// getPreferredAlignmentLog - Return the preferred alignment of the
  318. /// specified global, returned in log form. This includes an explicitly
  319. /// requested alignment (if the global has one).
  320. unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
  321. /// RoundUpAlignment - Round the specified value up to the next alignment
  322. /// boundary specified by Alignment. For example, 7 rounded up to an
  323. /// alignment boundary of 4 is 8. 8 rounded up to the alignment boundary of 4
  324. /// is 8 because it is already aligned.
  325. template <typename UIntTy>
  326. static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
  327. assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
  328. return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
  329. }
  330. static char ID; // Pass identification, replacement for typeid
  331. };
  332. /// StructLayout - used to lazily calculate structure layout information for a
  333. /// target machine, based on the DataLayout structure.
  334. ///
  335. class StructLayout {
  336. uint64_t StructSize;
  337. unsigned StructAlignment;
  338. unsigned NumElements;
  339. uint64_t MemberOffsets[1]; // variable sized array!
  340. public:
  341. uint64_t getSizeInBytes() const {
  342. return StructSize;
  343. }
  344. uint64_t getSizeInBits() const {
  345. return 8*StructSize;
  346. }
  347. unsigned getAlignment() const {
  348. return StructAlignment;
  349. }
  350. /// getElementContainingOffset - Given a valid byte offset into the structure,
  351. /// return the structure index that contains it.
  352. ///
  353. unsigned getElementContainingOffset(uint64_t Offset) const;
  354. uint64_t getElementOffset(unsigned Idx) const {
  355. assert(Idx < NumElements && "Invalid element idx!");
  356. return MemberOffsets[Idx];
  357. }
  358. uint64_t getElementOffsetInBits(unsigned Idx) const {
  359. return getElementOffset(Idx)*8;
  360. }
  361. private:
  362. friend class DataLayout; // Only DataLayout can create this class
  363. StructLayout(StructType *ST, const DataLayout &TD);
  364. };
  365. } // End llvm namespace
  366. #endif