199b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow//===-- DataLayout.cpp - Data size & alignment routines --------------------==//
2e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
3e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//                     The LLVM Compiler Infrastructure
4e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
5e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// This file is distributed under the University of Illinois Open Source
6e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// License. See LICENSE.TXT for details.
7e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
8e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
9e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
1099b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow// This file defines layout properties related to datatype size/offset/alignment
11e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// information.
12e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
13e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// This structure should be created once, filled in if the defaults are not
14e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// correct and then passed around by const&.  None of the members functions
15e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// require modification to the object.
16e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//
17e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
18e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/DenseMap.h"
210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/ErrorHandling.h"
25e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow#include "llvm/Support/GetElementPtrTypeIterator.h"
26e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow#include "llvm/Support/ManagedStatic.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/MathExtras.h"
28e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow#include "llvm/Support/Mutex.h"
29d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/raw_ostream.h"
30e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow#include <algorithm>
31e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow#include <cstdlib>
32e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowusing namespace llvm;
33e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
3499b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow// Handle the Pass registration stuff necessary to use DataLayout's.
35e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
36e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// Register the default SparcV9 implementation...
3799b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowINITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true)
3899b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowchar DataLayout::ID = 0;
39e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
40e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
41e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow// Support for StructLayout
42e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
43e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
446b51f75cc58b5a431cfb9e808f27f1fc4432913eEli BenderskyStructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
45e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
46e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructAlignment = 0;
47e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructSize = 0;
48e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  NumElements = ST->getNumElements();
49e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
50e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Loop over each of the elements, placing them in memory.
51e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned i = 0, e = NumElements; i != e; ++i) {
52e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    Type *Ty = ST->getElementType(i);
536b51f75cc58b5a431cfb9e808f27f1fc4432913eEli Bendersky    unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
54e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
55e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // Add padding if necessary to align the data element properly.
56e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if ((StructSize & (TyAlign-1)) != 0)
5799b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow      StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign);
58e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
59e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // Keep track of maximum alignment constraint.
60e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    StructAlignment = std::max(TyAlign, StructAlignment);
61e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
62e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    MemberOffsets[i] = StructSize;
636b51f75cc58b5a431cfb9e808f27f1fc4432913eEli Bendersky    StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
64e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
65e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
66e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Empty structures have alignment of 1 byte.
67e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (StructAlignment == 0) StructAlignment = 1;
68e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
69e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Add padding to the end of the struct so that it could be put in an array
70e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // and all array elements would be aligned correctly.
71e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if ((StructSize & (StructAlignment-1)) != 0)
7299b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow    StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment);
73e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
74e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
75e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
76e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// getElementContainingOffset - Given a valid offset into the structure,
77e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// return the structure index that contains it.
78e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowunsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
79e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  const uint64_t *SI =
80e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
81e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
82e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  --SI;
83e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(*SI <= Offset && "upper_bound didn't work");
84e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
85e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow         (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
86e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow         "Upper bound didn't work!");
87e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
88e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Multiple fields can have the same offset if any of them are zero sized.
89e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
90e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // at the i32 element, because it is the last element at that offset.  This is
91e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // the right one to return, because anything after it will have a higher
92e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // offset, implying that this element is non-empty.
93e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return SI-&MemberOffsets[0];
94e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
95e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
96e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
9799b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow// LayoutAlignElem, LayoutAlign support
98e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
99e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
10099b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowLayoutAlignElem
10199b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowLayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
102e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                     unsigned pref_align, uint32_t bit_width) {
103e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
10499b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow  LayoutAlignElem retval;
105e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  retval.AlignType = align_type;
106e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  retval.ABIAlign = abi_align;
107e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  retval.PrefAlign = pref_align;
108e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  retval.TypeBitWidth = bit_width;
109e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return retval;
110e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
111e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
112e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowbool
11399b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowLayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
114e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return (AlignType == rhs.AlignType
115e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow          && ABIAlign == rhs.ABIAlign
116e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow          && PrefAlign == rhs.PrefAlign
117e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow          && TypeBitWidth == rhs.TypeBitWidth);
118e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
119e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
12099b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowconst LayoutAlignElem
121fe4ef2c2aec3a9fbd913957411b9e7faa4246b95Richard SmithDataLayout::InvalidAlignmentElem = LayoutAlignElem::get(INVALID_ALIGN, 0, 0, 0);
1227d661468682c333739a6f6ab7dc337463573c354Micah Villmow
1237d661468682c333739a6f6ab7dc337463573c354Micah Villmow//===----------------------------------------------------------------------===//
1247d661468682c333739a6f6ab7dc337463573c354Micah Villmow// PointerAlignElem, PointerAlign support
1257d661468682c333739a6f6ab7dc337463573c354Micah Villmow//===----------------------------------------------------------------------===//
1267d661468682c333739a6f6ab7dc337463573c354Micah Villmow
1277d661468682c333739a6f6ab7dc337463573c354Micah VillmowPointerAlignElem
1287d661468682c333739a6f6ab7dc337463573c354Micah VillmowPointerAlignElem::get(uint32_t addr_space, unsigned abi_align,
129f578c89dc6ca3e79667c2aa9d0ac4fe409da7773Eli Bendersky                      unsigned pref_align, uint32_t bit_width) {
1307d661468682c333739a6f6ab7dc337463573c354Micah Villmow  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
1317d661468682c333739a6f6ab7dc337463573c354Micah Villmow  PointerAlignElem retval;
1327d661468682c333739a6f6ab7dc337463573c354Micah Villmow  retval.AddressSpace = addr_space;
1337d661468682c333739a6f6ab7dc337463573c354Micah Villmow  retval.ABIAlign = abi_align;
1347d661468682c333739a6f6ab7dc337463573c354Micah Villmow  retval.PrefAlign = pref_align;
1357d661468682c333739a6f6ab7dc337463573c354Micah Villmow  retval.TypeBitWidth = bit_width;
1367d661468682c333739a6f6ab7dc337463573c354Micah Villmow  return retval;
1377d661468682c333739a6f6ab7dc337463573c354Micah Villmow}
1387d661468682c333739a6f6ab7dc337463573c354Micah Villmow
1397d661468682c333739a6f6ab7dc337463573c354Micah Villmowbool
1407d661468682c333739a6f6ab7dc337463573c354Micah VillmowPointerAlignElem::operator==(const PointerAlignElem &rhs) const {
1417d661468682c333739a6f6ab7dc337463573c354Micah Villmow  return (ABIAlign == rhs.ABIAlign
1427d661468682c333739a6f6ab7dc337463573c354Micah Villmow          && AddressSpace == rhs.AddressSpace
1437d661468682c333739a6f6ab7dc337463573c354Micah Villmow          && PrefAlign == rhs.PrefAlign
1447d661468682c333739a6f6ab7dc337463573c354Micah Villmow          && TypeBitWidth == rhs.TypeBitWidth);
1457d661468682c333739a6f6ab7dc337463573c354Micah Villmow}
1467d661468682c333739a6f6ab7dc337463573c354Micah Villmow
1477d661468682c333739a6f6ab7dc337463573c354Micah Villmowconst PointerAlignElem
1487d661468682c333739a6f6ab7dc337463573c354Micah VillmowDataLayout::InvalidPointerElem = PointerAlignElem::get(~0U, 0U, 0U, 0U);
149e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
150e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
15199b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow//                       DataLayout Class Implementation
152e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow//===----------------------------------------------------------------------===//
153e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
1541e65676a12c61815acb65990d9269611ad544c78Patrik Hägglundvoid DataLayout::init(StringRef Desc) {
15599b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow  initializeDataLayoutPass(*PassRegistry::getPassRegistry());
156e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
157e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  LayoutMap = 0;
158e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  LittleEndian = false;
159e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StackNaturalAlign = 0;
160e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
161e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Default alignments
162e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(INTEGER_ALIGN,   1,  1, 1);   // i1
163e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(INTEGER_ALIGN,   1,  1, 8);   // i8
164e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(INTEGER_ALIGN,   2,  2, 16);  // i16
165e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(INTEGER_ALIGN,   4,  4, 32);  // i32
166e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(INTEGER_ALIGN,   4,  8, 64);  // i64
167e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(FLOAT_ALIGN,     2,  2, 16);  // half
168e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(FLOAT_ALIGN,     4,  4, 32);  // float
169e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(FLOAT_ALIGN,     8,  8, 64);  // double
170e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(FLOAT_ALIGN,    16, 16, 128); // ppcf128, quad, ...
171e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(VECTOR_ALIGN,    8,  8, 64);  // v2i32, v1i64, ...
172e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(VECTOR_ALIGN,   16, 16, 128); // v16i8, v8i16, v4i32, ...
173e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  setAlignment(AGGREGATE_ALIGN, 0,  8,  0);  // struct
1747d661468682c333739a6f6ab7dc337463573c354Micah Villmow  setPointerAlignment(0, 8, 8, 8);
175e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
17658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  parseSpecifier(Desc);
17758b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund}
17858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
17958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund/// Checked version of split, to ensure mandatory subparts.
18058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglundstatic std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
18158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  assert(!Str.empty() && "parse error, string can't be empty here");
18258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  std::pair<StringRef, StringRef> Split = Str.split(Separator);
18358b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  assert((!Split.second.empty() || Split.first == Str) &&
18458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund         "a trailing separator is not allowed");
18558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  return Split;
1861e65676a12c61815acb65990d9269611ad544c78Patrik Hägglund}
187e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
18837d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund/// Get an unsinged integer, including error checks.
18937d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglundstatic unsigned getInt(StringRef R) {
19037d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund  unsigned Result;
191dacb9a51bddbad9535c1b9339532827e73013388Patrik Hägglund  bool error = R.getAsInteger(10, Result); (void)error;
19237d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund  assert(!error && "not a number, or does not fit in an unsigned int");
19337d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund  return Result;
19437d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund}
19537d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund
19658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund/// Convert bits into bytes. Assert if not a byte width multiple.
19758b45535495d20639849abf7a5605be5c8304b66Patrik Hagglundstatic unsigned inBytes(unsigned Bits) {
19858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  assert(Bits % 8 == 0 && "number of bits must be a byte width multiple");
19958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund  return Bits / 8;
20058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund}
20158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
20258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglundvoid DataLayout::parseSpecifier(StringRef Desc) {
203e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  while (!Desc.empty()) {
20458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    // Split at '-'.
20558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    std::pair<StringRef, StringRef> Split = split(Desc, '-');
206e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    Desc = Split.second;
207e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
20858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    // Split at ':'.
20958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    Split = split(Split.first, ':');
21058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
21158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    // Aliases used below.
21258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    StringRef &Tok  = Split.first;  // Current token.
21358b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    StringRef &Rest = Split.second; // The rest of the string.
214e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
21558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    char Specifier = Tok.front();
21658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    Tok = Tok.substr(1);
217e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
21858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund    switch (Specifier) {
219e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'E':
2201e65676a12c61815acb65990d9269611ad544c78Patrik Hägglund      LittleEndian = false;
221e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
222e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'e':
2231e65676a12c61815acb65990d9269611ad544c78Patrik Hägglund      LittleEndian = true;
224e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
225e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'p': {
22658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // Address space.
22758b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
22858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      assert(AddrSpace < 1 << 24 &&
22958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund             "Invalid address space, must be a 24bit integer");
23058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
23158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // Size.
23258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      Split = split(Rest, ':');
23358b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned PointerMemSize = inBytes(getInt(Tok));
23458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
23558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // ABI alignment.
23658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      Split = split(Rest, ':');
23758b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned PointerABIAlign = inBytes(getInt(Tok));
23858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
23958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // Preferred alignment.
24058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned PointerPrefAlign = PointerABIAlign;
24158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      if (!Rest.empty()) {
24258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        Split = split(Rest, ':');
24358b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        PointerPrefAlign = inBytes(getInt(Tok));
244e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      }
245e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
24658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
24758b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund                          PointerMemSize);
248e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
249e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
250e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'i':
251e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'v':
252e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'f':
253e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'a':
254e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 's': {
255e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      AlignTypeEnum AlignType;
25658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      switch (Specifier) {
257e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      default:
258e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      case 'i': AlignType = INTEGER_ALIGN; break;
259e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      case 'v': AlignType = VECTOR_ALIGN; break;
260e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      case 'f': AlignType = FLOAT_ALIGN; break;
261e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      case 'a': AlignType = AGGREGATE_ALIGN; break;
262e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      case 's': AlignType = STACK_ALIGN; break;
263e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      }
264e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
26558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // Bit size.
26658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned Size = Tok.empty() ? 0 : getInt(Tok);
267e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
26858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // ABI alignment.
26958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      Split = split(Rest, ':');
27058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned ABIAlign = inBytes(getInt(Tok));
271e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
27258b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      // Preferred alignment.
27358b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      unsigned PrefAlign = ABIAlign;
27458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      if (!Rest.empty()) {
27558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        Split = split(Rest, ':');
27658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        PrefAlign = inBytes(getInt(Tok));
277e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      }
27858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund
2791e65676a12c61815acb65990d9269611ad544c78Patrik Hägglund      setAlignment(AlignType, ABIAlign, PrefAlign, Size);
28099b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow
281e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
282e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
283e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'n':  // Native integer types.
28458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      for (;;) {
28558b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        unsigned Width = getInt(Tok);
28658b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        assert(Width != 0 && "width must be non-zero");
28737d5be777c81393c54cae8b26241ebaca2ed9e27Patrik Hägglund        LegalIntWidths.push_back(Width);
28858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        if (Rest.empty())
28958b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund          break;
29058b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund        Split = split(Rest, ':');
29158b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      }
292e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
293e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    case 'S': { // Stack natural alignment.
29458b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      StackNaturalAlign = inBytes(getInt(Tok));
295e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
296e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
297e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    default:
29858b45535495d20639849abf7a5605be5c8304b66Patrik Hagglund      llvm_unreachable("Unknown specifier in datalayout string");
299e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      break;
300e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
301e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
302e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
303e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
304e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// Default ctor.
305e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow///
306e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// @note This has to exist, because this is a pass, but it should never be
307e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// used.
30899b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowDataLayout::DataLayout() : ImmutablePass(ID) {
30999b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow  report_fatal_error("Bad DataLayout ctor used.  "
310f578c89dc6ca3e79667c2aa9d0ac4fe409da7773Eli Bendersky                     "Tool did not specify a DataLayout to use?");
311e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
312e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
31399b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowDataLayout::DataLayout(const Module *M)
314e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  : ImmutablePass(ID) {
3151e65676a12c61815acb65990d9269611ad544c78Patrik Hägglund  init(M->getDataLayout());
316e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
317e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
318e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowvoid
31999b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowDataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
320e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                         unsigned pref_align, uint32_t bit_width) {
321e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
322e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
323e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
324e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
325bf07a512f2fd6bbcd0b217060656e9d12b9da5b0Micah Villmow    if (Alignments[i].AlignType == (unsigned)align_type &&
326e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        Alignments[i].TypeBitWidth == bit_width) {
327e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Update the abi, preferred alignments.
328e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Alignments[i].ABIAlign = abi_align;
329e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Alignments[i].PrefAlign = pref_align;
330e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      return;
331e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
332e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
333e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
33499b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow  Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
335e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                                            pref_align, bit_width));
336e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
337e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
3387d661468682c333739a6f6ab7dc337463573c354Micah Villmowvoid
3397d661468682c333739a6f6ab7dc337463573c354Micah VillmowDataLayout::setPointerAlignment(uint32_t addr_space, unsigned abi_align,
3407d661468682c333739a6f6ab7dc337463573c354Micah Villmow                         unsigned pref_align, uint32_t bit_width) {
3417d661468682c333739a6f6ab7dc337463573c354Micah Villmow  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
3427d661468682c333739a6f6ab7dc337463573c354Micah Villmow  DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(addr_space);
3437d661468682c333739a6f6ab7dc337463573c354Micah Villmow  if (val == Pointers.end()) {
3447d661468682c333739a6f6ab7dc337463573c354Micah Villmow    Pointers[addr_space] = PointerAlignElem::get(addr_space,
3457d661468682c333739a6f6ab7dc337463573c354Micah Villmow          abi_align, pref_align, bit_width);
3467d661468682c333739a6f6ab7dc337463573c354Micah Villmow  } else {
3477d661468682c333739a6f6ab7dc337463573c354Micah Villmow    val->second.ABIAlign = abi_align;
3487d661468682c333739a6f6ab7dc337463573c354Micah Villmow    val->second.PrefAlign = pref_align;
3497d661468682c333739a6f6ab7dc337463573c354Micah Villmow    val->second.TypeBitWidth = bit_width;
3507d661468682c333739a6f6ab7dc337463573c354Micah Villmow  }
3517d661468682c333739a6f6ab7dc337463573c354Micah Villmow}
3527d661468682c333739a6f6ab7dc337463573c354Micah Villmow
353e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
35499b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow/// preferred if ABIInfo = false) the layout wants for the specified datatype.
35599b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
356e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                                      uint32_t BitWidth, bool ABIInfo,
357e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                                      Type *Ty) const {
358e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Check to see if we have an exact match and remember the best match we see.
359e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  int BestMatchIdx = -1;
360e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  int LargestInt = -1;
361e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
362bf07a512f2fd6bbcd0b217060656e9d12b9da5b0Micah Villmow    if (Alignments[i].AlignType == (unsigned)AlignType &&
363e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        Alignments[i].TypeBitWidth == BitWidth)
364e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
365e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
366e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // The best match so far depends on what we're looking for.
367e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow     if (AlignType == INTEGER_ALIGN &&
368e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow         Alignments[i].AlignType == INTEGER_ALIGN) {
369e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // The "best match" for integers is the smallest size that is larger than
370e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // the BitWidth requested.
371e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
372f578c89dc6ca3e79667c2aa9d0ac4fe409da7773Eli Bendersky          Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
373e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        BestMatchIdx = i;
374e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // However, if there isn't one that's larger, then we must use the
375e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // largest one we have (see below)
376e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      if (LargestInt == -1 ||
377e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow          Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
378e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        LargestInt = i;
379e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
380e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
381e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
382e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Okay, we didn't find an exact solution.  Fall back here depending on what
383e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // is being looked for.
384e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (BestMatchIdx == -1) {
385e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // If we didn't find an integer alignment, fall back on most conservative.
386e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if (AlignType == INTEGER_ALIGN) {
387e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      BestMatchIdx = LargestInt;
388e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    } else {
389e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
390e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
391e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // By default, use natural alignment for vector types. This is consistent
392e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // with what clang and llvm-gcc do.
393e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
394e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Align *= cast<VectorType>(Ty)->getNumElements();
395e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // If the alignment is not a power of 2, round up to the next power of 2.
396e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // This happens for non-power-of-2 length vectors.
397e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      if (Align & (Align-1))
398e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        Align = NextPowerOf2(Align);
399e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      return Align;
400e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
401e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
402e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
403e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Since we got a "best match" index, just return it.
404e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
405e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                 : Alignments[BestMatchIdx].PrefAlign;
406e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
407e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
408e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmownamespace {
409e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
410e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowclass StructLayoutMap {
411e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
412e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  LayoutInfoTy LayoutInfo;
413e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
414e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmowpublic:
415e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  virtual ~StructLayoutMap() {
416e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // Remove any layouts.
417e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
418e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow         I != E; ++I) {
419e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      StructLayout *Value = I->second;
420e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Value->~StructLayout();
421e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      free(Value);
422e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
423e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
424e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
425e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructLayout *&operator[](StructType *STy) {
426e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    return LayoutInfo[STy];
427e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
428e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
429e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // for debugging...
430e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  virtual void dump() const {}
431e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow};
432e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
433e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow} // end anonymous namespace
434e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
43599b11484d9c0dea9d010d0a37bacc82a11972617Micah VillmowDataLayout::~DataLayout() {
436e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  delete static_cast<StructLayoutMap*>(LayoutMap);
437e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
438e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
4398f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooperbool DataLayout::doFinalization(Module &M) {
4408f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooper  delete static_cast<StructLayoutMap*>(LayoutMap);
4418f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooper  LayoutMap = 0;
4428f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooper  return false;
4438f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooper}
4448f8af529fc6239985f73a4b3cf3a0b25692824edPete Cooper
44599b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowconst StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
446e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (!LayoutMap)
447e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    LayoutMap = new StructLayoutMap();
448e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
449e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
450e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructLayout *&SL = (*STM)[Ty];
451e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (SL) return SL;
452e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
453e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Otherwise, create the struct layout.  Because it is variable length, we
454e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // malloc it, then use placement new.
455e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  int NumElts = Ty->getNumElements();
456e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  StructLayout *L =
457e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
458e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
459e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Set SL before calling StructLayout's ctor.  The ctor could cause other
460e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // entries to be added to TheMap, invalidating our reference.
461e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  SL = L;
462e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
463e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  new (L) StructLayout(Ty, *this);
464e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
465e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return L;
466e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
467e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
46899b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowstd::string DataLayout::getStringRepresentation() const {
469e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  std::string Result;
470e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  raw_string_ostream OS(Result);
471e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
4727d661468682c333739a6f6ab7dc337463573c354Micah Villmow  OS << (LittleEndian ? "e" : "E");
4737d661468682c333739a6f6ab7dc337463573c354Micah Villmow  SmallVector<unsigned, 8> addrSpaces;
4747d661468682c333739a6f6ab7dc337463573c354Micah Villmow  // Lets get all of the known address spaces and sort them
4757d661468682c333739a6f6ab7dc337463573c354Micah Villmow  // into increasing order so that we can emit the string
4767d661468682c333739a6f6ab7dc337463573c354Micah Villmow  // in a cleaner format.
4777d661468682c333739a6f6ab7dc337463573c354Micah Villmow  for (DenseMap<unsigned, PointerAlignElem>::const_iterator
4787d661468682c333739a6f6ab7dc337463573c354Micah Villmow      pib = Pointers.begin(), pie = Pointers.end();
4797d661468682c333739a6f6ab7dc337463573c354Micah Villmow      pib != pie; ++pib) {
4807d661468682c333739a6f6ab7dc337463573c354Micah Villmow    addrSpaces.push_back(pib->first);
4817d661468682c333739a6f6ab7dc337463573c354Micah Villmow  }
4827d661468682c333739a6f6ab7dc337463573c354Micah Villmow  std::sort(addrSpaces.begin(), addrSpaces.end());
4836227d5c690504c7ada5780c00a635b282c46e275Craig Topper  for (SmallVectorImpl<unsigned>::iterator asb = addrSpaces.begin(),
4847d661468682c333739a6f6ab7dc337463573c354Micah Villmow      ase = addrSpaces.end(); asb != ase; ++asb) {
4857d661468682c333739a6f6ab7dc337463573c354Micah Villmow    const PointerAlignElem &PI = Pointers.find(*asb)->second;
4867d661468682c333739a6f6ab7dc337463573c354Micah Villmow    OS << "-p";
4877d661468682c333739a6f6ab7dc337463573c354Micah Villmow    if (PI.AddressSpace) {
4887d661468682c333739a6f6ab7dc337463573c354Micah Villmow      OS << PI.AddressSpace;
4897d661468682c333739a6f6ab7dc337463573c354Micah Villmow    }
4907d661468682c333739a6f6ab7dc337463573c354Micah Villmow     OS << ":" << PI.TypeBitWidth*8 << ':' << PI.ABIAlign*8
4917d661468682c333739a6f6ab7dc337463573c354Micah Villmow        << ':' << PI.PrefAlign*8;
4927d661468682c333739a6f6ab7dc337463573c354Micah Villmow  }
4937d661468682c333739a6f6ab7dc337463573c354Micah Villmow  OS << "-S" << StackNaturalAlign*8;
494e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
495e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
49699b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmow    const LayoutAlignElem &AI = Alignments[i];
497e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
498e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow       << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
499e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
500e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
501e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (!LegalIntWidths.empty()) {
502e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    OS << "-n" << (unsigned)LegalIntWidths[0];
503e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
504e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
505e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      OS << ':' << (unsigned)LegalIntWidths[i];
506e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
507e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return OS.str();
508e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
509e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
51058376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenaultunsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
51158376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault  assert(Ty->isPtrOrPtrVectorTy() &&
51258376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault         "This should only be called with a pointer or pointer vector type");
51358376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault
51458376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault  if (Ty->isPointerTy())
51558376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault    return getTypeSizeInBits(Ty);
51658376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault
517fe655dc15598ebd4c8077e4a67914e7f233e0774Matt Arsenault  return getTypeSizeInBits(Ty->getScalarType());
51858376d8ede436eb1fe474bca1582a4f7afe613f9Matt Arsenault}
519e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
520e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/*!
521e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  \param abi_or_pref Flag that determines which alignment is returned. true
522e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  returns the ABI alignment, false returns the preferred alignment.
523e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  \param Ty The underlying type for which alignment is determined.
524e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
525e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
526e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  == false) for the requested type \a Ty.
527e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow */
52899b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
529e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  int AlignType = -1;
530e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
531e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
532e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  switch (Ty->getTypeID()) {
533e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // Early escape for the non-numeric types.
534e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::LabelTyID:
535e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    return (abi_or_pref
5367d661468682c333739a6f6ab7dc337463573c354Micah Villmow            ? getPointerABIAlignment(0)
5377d661468682c333739a6f6ab7dc337463573c354Micah Villmow            : getPointerPrefAlignment(0));
5387d661468682c333739a6f6ab7dc337463573c354Micah Villmow  case Type::PointerTyID: {
5397d661468682c333739a6f6ab7dc337463573c354Micah Villmow    unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
5407d661468682c333739a6f6ab7dc337463573c354Micah Villmow    return (abi_or_pref
5417d661468682c333739a6f6ab7dc337463573c354Micah Villmow            ? getPointerABIAlignment(AS)
5427d661468682c333739a6f6ab7dc337463573c354Micah Villmow            : getPointerPrefAlignment(AS));
5437d661468682c333739a6f6ab7dc337463573c354Micah Villmow    }
544e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::ArrayTyID:
545e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
546e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
547e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::StructTyID: {
548e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // Packed structure types always have an ABI alignment of one.
549e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
550e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      return 1;
551e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
552e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    // Get the layout annotation... which is lazily created on demand.
553e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
554e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
555e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    return std::max(Align, Layout->getAlignment());
556e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
557e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::IntegerTyID:
558e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    AlignType = INTEGER_ALIGN;
559e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    break;
560e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::HalfTyID:
561e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::FloatTyID:
562e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::DoubleTyID:
563e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // PPC_FP128TyID and FP128TyID have different data contents, but the
564e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  // same size and alignment, so they look the same here.
565e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::PPC_FP128TyID:
566e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::FP128TyID:
567e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::X86_FP80TyID:
568e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    AlignType = FLOAT_ALIGN;
569e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    break;
570e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::X86_MMXTyID:
571e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  case Type::VectorTyID:
572e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    AlignType = VECTOR_ALIGN;
573e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    break;
574e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  default:
575e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    llvm_unreachable("Bad type for getAlignment!!!");
576e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
577e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
578e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
579e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                          abi_or_pref, Ty);
580e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
581e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
58299b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getABITypeAlignment(Type *Ty) const {
583e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return getAlignment(Ty, true);
584e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
585e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
586e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
587e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// an integer type of the specified bitwidth.
58899b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
589e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
590e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
591e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
59299b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const {
593e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
594e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if (Alignments[i].AlignType == STACK_ALIGN)
595e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      return Alignments[i].ABIAlign;
596e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
597e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return getABITypeAlignment(Ty);
598e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
599e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
60099b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
601e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return getAlignment(Ty, false);
602e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
603e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
60499b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
605e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  unsigned Align = getPrefTypeAlignment(Ty);
606e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
607e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return Log2_32(Align);
608e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
609e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
6107d661468682c333739a6f6ab7dc337463573c354Micah VillmowIntegerType *DataLayout::getIntPtrType(LLVMContext &C,
6117d661468682c333739a6f6ab7dc337463573c354Micah Villmow                                       unsigned AddressSpace) const {
6127d661468682c333739a6f6ab7dc337463573c354Micah Villmow  return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
613e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
614e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
6157ed4f94c1337b931524af99eb1aac72563888239Duncan SandsType *DataLayout::getIntPtrType(Type *Ty) const {
616ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth  assert(Ty->isPtrOrPtrVectorTy() &&
617ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth         "Expected a pointer or pointer vector type.");
618ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth  unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
6197ed4f94c1337b931524af99eb1aac72563888239Duncan Sands  IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
6207ed4f94c1337b931524af99eb1aac72563888239Duncan Sands  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
6217ed4f94c1337b931524af99eb1aac72563888239Duncan Sands    return VectorType::get(IntTy, VecTy->getNumElements());
6227ed4f94c1337b931524af99eb1aac72563888239Duncan Sands  return IntTy;
623aa76e9e2cf50af190de90bc778b7f7e42ef9ceffMicah Villmow}
624aa76e9e2cf50af190de90bc778b7f7e42ef9ceffMicah Villmow
6252be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de GrandmaisonType *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
6262be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison  for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
6272be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison    if (Width <= LegalIntWidths[i])
6282be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison      return Type::getIntNTy(C, LegalIntWidths[i]);
6292be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison  return 0;
6302be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison}
6312be921adc41fb079ce25d36bdd6402ca70d56451Arnaud A. de Grandmaison
63299b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowuint64_t DataLayout::getIndexedOffset(Type *ptrTy,
633e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow                                      ArrayRef<Value *> Indices) const {
634e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  Type *Ty = ptrTy;
635e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
636e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  uint64_t Result = 0;
637e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
638e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  generic_gep_type_iterator<Value* const*>
639e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    TI = gep_type_begin(ptrTy, Indices);
640e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
641e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow       ++CurIDX, ++TI) {
642e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if (StructType *STy = dyn_cast<StructType>(*TI)) {
643e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      assert(Indices[CurIDX]->getType() ==
644e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow             Type::getInt32Ty(ptrTy->getContext()) &&
645e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow             "Illegal struct idx");
646e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
647e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
648e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Get structure layout information...
649e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      const StructLayout *Layout = getStructLayout(STy);
650e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
651e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Add in the offset, as calculated by the structure layout info...
652e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Result += Layout->getElementOffset(FieldNo);
653e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
654e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Update Ty to refer to current element
655e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Ty = STy->getElementType(FieldNo);
656e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    } else {
657e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Update Ty to refer to current element
658e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      Ty = cast<SequentialType>(Ty)->getElementType();
659e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
660e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // Get the array index and the size of each array element.
661e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
662e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
663e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
664e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
665e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
666e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return Result;
667e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
668e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
669e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// getPreferredAlignment - Return the preferred alignment of the specified
670e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// global.  This includes an explicitly requested alignment (if the global
671e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// has one).
67299b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
673e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  Type *ElemType = GV->getType()->getElementType();
674e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  unsigned Alignment = getPrefTypeAlignment(ElemType);
675e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  unsigned GVAlignment = GV->getAlignment();
676e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (GVAlignment >= Alignment) {
677e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    Alignment = GVAlignment;
678e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  } else if (GVAlignment != 0) {
679e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
680e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
681e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
682e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  if (GV->hasInitializer() && GVAlignment == 0) {
683e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    if (Alignment < 16) {
684e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // If the global is not external, see if it is large.  If so, give it a
685e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      // larger alignment.
686e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow      if (getTypeSizeInBits(ElemType) > 128)
687e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow        Alignment = 16;    // 16-byte alignment.
688e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow    }
689e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  }
690e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return Alignment;
691e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
692e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow
693e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// getPreferredAlignmentLog - Return the preferred alignment of the
694e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// specified global, returned in log form.  This includes an explicitly
695e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow/// requested alignment (if the global has one).
69699b11484d9c0dea9d010d0a37bacc82a11972617Micah Villmowunsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
697e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow  return Log2_32(getPreferredAlignment(GV));
698e18c2ae7b2c725ccac041973f3df9ce20bfab26eMicah Villmow}
699