2012-10-05 07:01:22 +08:00
|
|
|
//===-- DataLayout.cpp - Data size & alignment routines --------------------==//
|
2012-10-05 06:08:14 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-10-05 07:01:22 +08:00
|
|
|
// This file defines layout properties related to datatype size/offset/alignment
|
2012-10-05 06:08:14 +08:00
|
|
|
// information.
|
|
|
|
//
|
|
|
|
// This structure should be created once, filled in if the defaults are not
|
|
|
|
// correct and then passed around by const&. None of the members functions
|
|
|
|
// require modification to the object.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-12-20 07:03:03 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-01-04 03:21:54 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-04 18:40:04 +08:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-10-05 06:08:14 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-10-05 06:08:14 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-10-05 06:08:14 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Support for StructLayout
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
|
2012-10-05 06:08:14 +08:00
|
|
|
assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
|
|
|
|
StructAlignment = 0;
|
|
|
|
StructSize = 0;
|
2015-12-15 09:44:07 +08:00
|
|
|
IsPadded = false;
|
2012-10-05 06:08:14 +08:00
|
|
|
NumElements = ST->getNumElements();
|
|
|
|
|
|
|
|
// Loop over each of the elements, placing them in memory.
|
|
|
|
for (unsigned i = 0, e = NumElements; i != e; ++i) {
|
|
|
|
Type *Ty = ST->getElementType(i);
|
2013-04-16 23:41:18 +08:00
|
|
|
unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
|
2012-10-05 06:08:14 +08:00
|
|
|
|
|
|
|
// Add padding if necessary to align the data element properly.
|
2015-12-15 09:44:07 +08:00
|
|
|
if ((StructSize & (TyAlign-1)) != 0) {
|
|
|
|
IsPadded = true;
|
2014-10-20 14:13:33 +08:00
|
|
|
StructSize = RoundUpToAlignment(StructSize, TyAlign);
|
2015-12-15 09:44:07 +08:00
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
|
|
|
|
// Keep track of maximum alignment constraint.
|
|
|
|
StructAlignment = std::max(TyAlign, StructAlignment);
|
|
|
|
|
|
|
|
MemberOffsets[i] = StructSize;
|
2013-04-16 23:41:18 +08:00
|
|
|
StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty structures have alignment of 1 byte.
|
|
|
|
if (StructAlignment == 0) StructAlignment = 1;
|
|
|
|
|
|
|
|
// Add padding to the end of the struct so that it could be put in an array
|
|
|
|
// and all array elements would be aligned correctly.
|
2015-12-15 09:44:07 +08:00
|
|
|
if ((StructSize & (StructAlignment-1)) != 0) {
|
|
|
|
IsPadded = true;
|
2014-10-20 14:13:33 +08:00
|
|
|
StructSize = RoundUpToAlignment(StructSize, StructAlignment);
|
2015-12-15 09:44:07 +08:00
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getElementContainingOffset - Given a valid offset into the structure,
|
|
|
|
/// return the structure index that contains it.
|
|
|
|
unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
|
|
|
|
const uint64_t *SI =
|
|
|
|
std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
|
|
|
|
assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
|
|
|
|
--SI;
|
|
|
|
assert(*SI <= Offset && "upper_bound didn't work");
|
|
|
|
assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
|
|
|
|
(SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
|
|
|
|
"Upper bound didn't work!");
|
|
|
|
|
|
|
|
// Multiple fields can have the same offset if any of them are zero sized.
|
|
|
|
// For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
|
|
|
|
// at the i32 element, because it is the last element at that offset. This is
|
|
|
|
// the right one to return, because anything after it will have a higher
|
|
|
|
// offset, implying that this element is non-empty.
|
|
|
|
return SI-&MemberOffsets[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-10-05 07:01:22 +08:00
|
|
|
// LayoutAlignElem, LayoutAlign support
|
2012-10-05 06:08:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-10-05 07:01:22 +08:00
|
|
|
LayoutAlignElem
|
|
|
|
LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
|
2012-10-05 06:08:14 +08:00
|
|
|
unsigned pref_align, uint32_t bit_width) {
|
|
|
|
assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
|
2012-10-05 07:01:22 +08:00
|
|
|
LayoutAlignElem retval;
|
2012-10-05 06:08:14 +08:00
|
|
|
retval.AlignType = align_type;
|
|
|
|
retval.ABIAlign = abi_align;
|
|
|
|
retval.PrefAlign = pref_align;
|
|
|
|
retval.TypeBitWidth = bit_width;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-10-05 07:01:22 +08:00
|
|
|
LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
return (AlignType == rhs.AlignType
|
|
|
|
&& ABIAlign == rhs.ABIAlign
|
|
|
|
&& PrefAlign == rhs.PrefAlign
|
|
|
|
&& TypeBitWidth == rhs.TypeBitWidth);
|
|
|
|
}
|
|
|
|
|
2012-10-05 07:01:22 +08:00
|
|
|
const LayoutAlignElem
|
2013-11-20 04:28:04 +08:00
|
|
|
DataLayout::InvalidAlignmentElem = { INVALID_ALIGN, 0, 0, 0 };
|
2012-10-10 00:06:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PointerAlignElem, PointerAlign support
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PointerAlignElem
|
2013-12-14 07:15:20 +08:00
|
|
|
PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
|
|
|
|
unsigned PrefAlign, uint32_t TypeByteWidth) {
|
|
|
|
assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
|
2012-10-10 00:06:12 +08:00
|
|
|
PointerAlignElem retval;
|
2013-12-14 07:15:20 +08:00
|
|
|
retval.AddressSpace = AddressSpace;
|
|
|
|
retval.ABIAlign = ABIAlign;
|
|
|
|
retval.PrefAlign = PrefAlign;
|
|
|
|
retval.TypeByteWidth = TypeByteWidth;
|
2012-10-10 00:06:12 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
|
|
|
|
return (ABIAlign == rhs.ABIAlign
|
|
|
|
&& AddressSpace == rhs.AddressSpace
|
|
|
|
&& PrefAlign == rhs.PrefAlign
|
2013-12-14 07:15:20 +08:00
|
|
|
&& TypeByteWidth == rhs.TypeByteWidth);
|
2012-10-10 00:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const PointerAlignElem
|
2013-11-20 04:28:04 +08:00
|
|
|
DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
|
2012-10-05 06:08:14 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-10-05 07:01:22 +08:00
|
|
|
// DataLayout Class Implementation
|
2012-10-05 06:08:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-04 03:21:54 +08:00
|
|
|
const char *DataLayout::getManglingComponent(const Triple &T) {
|
|
|
|
if (T.isOSBinFormatMachO())
|
|
|
|
return "-m:o";
|
2015-03-18 07:54:51 +08:00
|
|
|
if (T.isOSWindows() && T.isOSBinFormatCOFF())
|
|
|
|
return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
|
2014-04-03 04:32:05 +08:00
|
|
|
return "-m:e";
|
2014-01-04 03:21:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-20 23:21:32 +08:00
|
|
|
static const LayoutAlignElem DefaultAlignments[] = {
|
2013-12-20 07:03:03 +08:00
|
|
|
{ INTEGER_ALIGN, 1, 1, 1 }, // i1
|
|
|
|
{ INTEGER_ALIGN, 8, 1, 1 }, // i8
|
|
|
|
{ INTEGER_ALIGN, 16, 2, 2 }, // i16
|
|
|
|
{ INTEGER_ALIGN, 32, 4, 4 }, // i32
|
|
|
|
{ INTEGER_ALIGN, 64, 4, 8 }, // i64
|
|
|
|
{ FLOAT_ALIGN, 16, 2, 2 }, // half
|
|
|
|
{ FLOAT_ALIGN, 32, 4, 4 }, // float
|
|
|
|
{ FLOAT_ALIGN, 64, 8, 8 }, // double
|
|
|
|
{ FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
|
|
|
|
{ VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
|
|
|
|
{ VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
|
|
|
|
{ AGGREGATE_ALIGN, 0, 0, 8 } // struct
|
|
|
|
};
|
|
|
|
|
2014-02-26 06:23:04 +08:00
|
|
|
void DataLayout::reset(StringRef Desc) {
|
|
|
|
clear();
|
|
|
|
|
2014-04-09 14:08:46 +08:00
|
|
|
LayoutMap = nullptr;
|
2014-10-20 18:41:29 +08:00
|
|
|
BigEndian = false;
|
2012-10-05 06:08:14 +08:00
|
|
|
StackNaturalAlign = 0;
|
2014-01-04 03:21:54 +08:00
|
|
|
ManglingMode = MM_None;
|
2012-10-05 06:08:14 +08:00
|
|
|
|
|
|
|
// Default alignments
|
2014-03-10 23:03:06 +08:00
|
|
|
for (const LayoutAlignElem &E : DefaultAlignments) {
|
2013-12-20 07:03:03 +08:00
|
|
|
setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
|
|
|
|
E.TypeBitWidth);
|
|
|
|
}
|
2012-10-10 00:06:12 +08:00
|
|
|
setPointerAlignment(0, 8, 8, 8);
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
parseSpecifier(Desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checked version of split, to ensure mandatory subparts.
|
|
|
|
static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
|
|
|
|
assert(!Str.empty() && "parse error, string can't be empty here");
|
|
|
|
std::pair<StringRef, StringRef> Split = Str.split(Separator);
|
2014-12-10 09:38:28 +08:00
|
|
|
if (Split.second.empty() && Split.first != Str)
|
|
|
|
report_fatal_error("Trailing separator in datalayout string");
|
2014-12-10 10:36:41 +08:00
|
|
|
if (!Split.second.empty() && Split.first.empty())
|
|
|
|
report_fatal_error("Expected token before separator in datalayout string");
|
2012-11-30 18:06:59 +08:00
|
|
|
return Split;
|
2012-11-14 17:04:56 +08:00
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2014-01-08 03:51:38 +08:00
|
|
|
/// Get an unsigned integer, including error checks.
|
2012-11-28 20:13:12 +08:00
|
|
|
static unsigned getInt(StringRef R) {
|
2014-01-14 06:04:55 +08:00
|
|
|
unsigned Result;
|
2012-11-28 22:32:52 +08:00
|
|
|
bool error = R.getAsInteger(10, Result); (void)error;
|
2014-01-14 06:04:55 +08:00
|
|
|
if (error)
|
|
|
|
report_fatal_error("not a number, or does not fit in an unsigned int");
|
2012-11-28 20:13:12 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
/// Convert bits into bytes. Assert if not a byte width multiple.
|
|
|
|
static unsigned inBytes(unsigned Bits) {
|
2014-12-10 09:38:28 +08:00
|
|
|
if (Bits % 8)
|
|
|
|
report_fatal_error("number of bits must be a byte width multiple");
|
2012-11-30 18:06:59 +08:00
|
|
|
return Bits / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataLayout::parseSpecifier(StringRef Desc) {
|
2015-03-05 02:43:29 +08:00
|
|
|
StringRepresentation = Desc;
|
2012-10-05 06:08:14 +08:00
|
|
|
while (!Desc.empty()) {
|
2012-11-30 18:06:59 +08:00
|
|
|
// Split at '-'.
|
|
|
|
std::pair<StringRef, StringRef> Split = split(Desc, '-');
|
2012-10-05 06:08:14 +08:00
|
|
|
Desc = Split.second;
|
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
// Split at ':'.
|
|
|
|
Split = split(Split.first, ':');
|
|
|
|
|
|
|
|
// Aliases used below.
|
|
|
|
StringRef &Tok = Split.first; // Current token.
|
|
|
|
StringRef &Rest = Split.second; // The rest of the string.
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
char Specifier = Tok.front();
|
|
|
|
Tok = Tok.substr(1);
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
switch (Specifier) {
|
2014-01-02 06:29:43 +08:00
|
|
|
case 's':
|
|
|
|
// Ignored for backward compatibility.
|
|
|
|
// FIXME: remove this on LLVM 4.0.
|
|
|
|
break;
|
2012-10-05 06:08:14 +08:00
|
|
|
case 'E':
|
2014-10-20 18:41:29 +08:00
|
|
|
BigEndian = true;
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
case 'e':
|
2014-10-20 18:41:29 +08:00
|
|
|
BigEndian = false;
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
case 'p': {
|
2012-11-30 18:06:59 +08:00
|
|
|
// Address space.
|
|
|
|
unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
|
2014-12-10 09:17:08 +08:00
|
|
|
if (!isUInt<24>(AddrSpace))
|
|
|
|
report_fatal_error("Invalid address space, must be a 24bit integer");
|
2012-11-30 18:06:59 +08:00
|
|
|
|
|
|
|
// Size.
|
2014-12-10 09:38:28 +08:00
|
|
|
if (Rest.empty())
|
|
|
|
report_fatal_error(
|
|
|
|
"Missing size specification for pointer in datalayout string");
|
2012-11-30 18:06:59 +08:00
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned PointerMemSize = inBytes(getInt(Tok));
|
2015-03-02 14:00:02 +08:00
|
|
|
if (!PointerMemSize)
|
|
|
|
report_fatal_error("Invalid pointer size of 0 bytes");
|
2012-11-30 18:06:59 +08:00
|
|
|
|
|
|
|
// ABI alignment.
|
2014-12-10 09:38:28 +08:00
|
|
|
if (Rest.empty())
|
|
|
|
report_fatal_error(
|
|
|
|
"Missing alignment specification for pointer in datalayout string");
|
2012-11-30 18:06:59 +08:00
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned PointerABIAlign = inBytes(getInt(Tok));
|
2015-03-02 14:33:51 +08:00
|
|
|
if (!isPowerOf2_64(PointerABIAlign))
|
|
|
|
report_fatal_error(
|
|
|
|
"Pointer ABI alignment must be a power of 2");
|
2012-11-30 18:06:59 +08:00
|
|
|
|
|
|
|
// Preferred alignment.
|
|
|
|
unsigned PointerPrefAlign = PointerABIAlign;
|
|
|
|
if (!Rest.empty()) {
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
PointerPrefAlign = inBytes(getInt(Tok));
|
2015-03-02 14:33:51 +08:00
|
|
|
if (!isPowerOf2_64(PointerPrefAlign))
|
|
|
|
report_fatal_error(
|
|
|
|
"Pointer preferred alignment must be a power of 2");
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
|
|
|
|
PointerMemSize);
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'i':
|
|
|
|
case 'v':
|
|
|
|
case 'f':
|
2014-01-02 06:29:43 +08:00
|
|
|
case 'a': {
|
2012-10-05 06:08:14 +08:00
|
|
|
AlignTypeEnum AlignType;
|
2012-11-30 18:06:59 +08:00
|
|
|
switch (Specifier) {
|
2012-10-05 06:08:14 +08:00
|
|
|
default:
|
|
|
|
case 'i': AlignType = INTEGER_ALIGN; break;
|
|
|
|
case 'v': AlignType = VECTOR_ALIGN; break;
|
|
|
|
case 'f': AlignType = FLOAT_ALIGN; break;
|
|
|
|
case 'a': AlignType = AGGREGATE_ALIGN; break;
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
// Bit size.
|
|
|
|
unsigned Size = Tok.empty() ? 0 : getInt(Tok);
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2014-12-10 09:17:08 +08:00
|
|
|
if (AlignType == AGGREGATE_ALIGN && Size != 0)
|
|
|
|
report_fatal_error(
|
|
|
|
"Sized aggregate specification in datalayout string");
|
2014-01-07 05:40:24 +08:00
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
// ABI alignment.
|
2014-12-10 10:36:41 +08:00
|
|
|
if (Rest.empty())
|
|
|
|
report_fatal_error(
|
|
|
|
"Missing alignment specification in datalayout string");
|
2012-11-30 18:06:59 +08:00
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned ABIAlign = inBytes(getInt(Tok));
|
2015-03-02 17:34:59 +08:00
|
|
|
if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
|
|
|
|
report_fatal_error(
|
|
|
|
"ABI alignment specification must be >0 for non-aggregate types");
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2012-11-30 18:06:59 +08:00
|
|
|
// Preferred alignment.
|
|
|
|
unsigned PrefAlign = ABIAlign;
|
|
|
|
if (!Rest.empty()) {
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
PrefAlign = inBytes(getInt(Tok));
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
2012-11-30 18:06:59 +08:00
|
|
|
|
2012-11-14 17:04:56 +08:00
|
|
|
setAlignment(AlignType, ABIAlign, PrefAlign, Size);
|
2012-10-05 07:01:22 +08:00
|
|
|
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n': // Native integer types.
|
2012-11-30 18:06:59 +08:00
|
|
|
for (;;) {
|
|
|
|
unsigned Width = getInt(Tok);
|
2014-12-10 09:17:08 +08:00
|
|
|
if (Width == 0)
|
|
|
|
report_fatal_error(
|
|
|
|
"Zero width native integer type in datalayout string");
|
2012-11-28 20:13:12 +08:00
|
|
|
LegalIntWidths.push_back(Width);
|
2012-11-30 18:06:59 +08:00
|
|
|
if (Rest.empty())
|
|
|
|
break;
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
case 'S': { // Stack natural alignment.
|
2012-11-30 18:06:59 +08:00
|
|
|
StackNaturalAlign = inBytes(getInt(Tok));
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-01-04 03:21:54 +08:00
|
|
|
case 'm':
|
2014-12-10 10:36:41 +08:00
|
|
|
if (!Tok.empty())
|
|
|
|
report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
|
|
|
|
if (Rest.empty())
|
|
|
|
report_fatal_error("Expected mangling specifier in datalayout string");
|
|
|
|
if (Rest.size() > 1)
|
|
|
|
report_fatal_error("Unknown mangling specifier in datalayout string");
|
2014-01-04 03:21:54 +08:00
|
|
|
switch(Rest[0]) {
|
|
|
|
default:
|
2014-12-10 09:17:08 +08:00
|
|
|
report_fatal_error("Unknown mangling in datalayout string");
|
2014-01-04 03:21:54 +08:00
|
|
|
case 'e':
|
|
|
|
ManglingMode = MM_ELF;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
ManglingMode = MM_MachO;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
ManglingMode = MM_Mips;
|
|
|
|
break;
|
2014-01-10 21:42:12 +08:00
|
|
|
case 'w':
|
2015-03-18 07:54:51 +08:00
|
|
|
ManglingMode = MM_WinCOFF;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
ManglingMode = MM_WinCOFFX86;
|
2014-01-04 03:21:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-05 06:08:14 +08:00
|
|
|
default:
|
2014-12-10 09:17:08 +08:00
|
|
|
report_fatal_error("Unknown specifier in datalayout string");
|
2012-10-05 06:08:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 14:08:46 +08:00
|
|
|
DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
|
2014-09-11 05:27:43 +08:00
|
|
|
init(M);
|
|
|
|
}
|
|
|
|
|
2015-03-05 02:43:29 +08:00
|
|
|
void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
|
2012-10-05 06:08:14 +08:00
|
|
|
|
2014-02-27 01:02:08 +08:00
|
|
|
bool DataLayout::operator==(const DataLayout &Other) const {
|
2014-10-20 18:41:29 +08:00
|
|
|
bool Ret = BigEndian == Other.BigEndian &&
|
2014-02-27 01:02:08 +08:00
|
|
|
StackNaturalAlign == Other.StackNaturalAlign &&
|
|
|
|
ManglingMode == Other.ManglingMode &&
|
|
|
|
LegalIntWidths == Other.LegalIntWidths &&
|
2014-04-23 01:47:03 +08:00
|
|
|
Alignments == Other.Alignments && Pointers == Other.Pointers;
|
2015-03-05 02:43:29 +08:00
|
|
|
// Note: getStringRepresentation() might differs, it is not canonicalized
|
2014-02-27 01:02:08 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2012-10-05 06:08:14 +08:00
|
|
|
void
|
2012-10-05 07:01:22 +08:00
|
|
|
DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
|
2012-10-05 06:08:14 +08:00
|
|
|
unsigned pref_align, uint32_t bit_width) {
|
2015-02-16 13:41:53 +08:00
|
|
|
if (!isUInt<24>(bit_width))
|
|
|
|
report_fatal_error("Invalid bit width, must be a 24bit integer");
|
|
|
|
if (!isUInt<16>(abi_align))
|
|
|
|
report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
|
|
|
|
if (!isUInt<16>(pref_align))
|
|
|
|
report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
|
2015-03-02 17:35:03 +08:00
|
|
|
if (abi_align != 0 && !isPowerOf2_64(abi_align))
|
|
|
|
report_fatal_error("Invalid ABI alignment, must be a power of 2");
|
|
|
|
if (pref_align != 0 && !isPowerOf2_64(pref_align))
|
|
|
|
report_fatal_error("Invalid preferred alignment, must be a power of 2");
|
2015-02-16 13:41:53 +08:00
|
|
|
|
|
|
|
if (pref_align < abi_align)
|
|
|
|
report_fatal_error(
|
|
|
|
"Preferred alignment cannot be less than the ABI alignment");
|
|
|
|
|
2014-03-10 23:03:06 +08:00
|
|
|
for (LayoutAlignElem &Elem : Alignments) {
|
|
|
|
if (Elem.AlignType == (unsigned)align_type &&
|
|
|
|
Elem.TypeBitWidth == bit_width) {
|
2012-10-05 06:08:14 +08:00
|
|
|
// Update the abi, preferred alignments.
|
2014-03-10 23:03:06 +08:00
|
|
|
Elem.ABIAlign = abi_align;
|
|
|
|
Elem.PrefAlign = pref_align;
|
2012-10-05 06:08:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-05 07:01:22 +08:00
|
|
|
Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
|
2012-10-05 06:08:14 +08:00
|
|
|
pref_align, bit_width));
|
|
|
|
}
|
|
|
|
|
2014-02-27 00:58:35 +08:00
|
|
|
DataLayout::PointersTy::iterator
|
2014-02-27 01:05:38 +08:00
|
|
|
DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
|
2014-02-27 00:58:35 +08:00
|
|
|
return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
|
2014-03-10 23:03:06 +08:00
|
|
|
[](const PointerAlignElem &A, uint32_t AddressSpace) {
|
|
|
|
return A.AddressSpace < AddressSpace;
|
|
|
|
});
|
2014-02-27 00:58:35 +08:00
|
|
|
}
|
|
|
|
|
2013-12-14 07:15:20 +08:00
|
|
|
void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
|
|
|
|
unsigned PrefAlign,
|
|
|
|
uint32_t TypeByteWidth) {
|
2015-02-16 13:41:55 +08:00
|
|
|
if (PrefAlign < ABIAlign)
|
|
|
|
report_fatal_error(
|
|
|
|
"Preferred alignment cannot be less than the ABI alignment");
|
|
|
|
|
2014-02-27 01:05:38 +08:00
|
|
|
PointersTy::iterator I = findPointerLowerBound(AddrSpace);
|
2014-02-27 00:58:35 +08:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
|
|
|
|
Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
|
|
|
|
TypeByteWidth));
|
2012-10-10 00:06:12 +08:00
|
|
|
} else {
|
2014-02-27 00:58:35 +08:00
|
|
|
I->ABIAlign = ABIAlign;
|
|
|
|
I->PrefAlign = PrefAlign;
|
|
|
|
I->TypeByteWidth = TypeByteWidth;
|
2012-10-10 00:06:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-05 06:08:14 +08:00
|
|
|
/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
|
2012-10-05 07:01:22 +08:00
|
|
|
/// preferred if ABIInfo = false) the layout wants for the specified datatype.
|
|
|
|
unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
|
2012-10-05 06:08:14 +08:00
|
|
|
uint32_t BitWidth, bool ABIInfo,
|
2015-07-28 01:15:28 +08:00
|
|
|
Type *Ty) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
// Check to see if we have an exact match and remember the best match we see.
|
|
|
|
int BestMatchIdx = -1;
|
|
|
|
int LargestInt = -1;
|
|
|
|
for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
|
2012-10-06 01:02:14 +08:00
|
|
|
if (Alignments[i].AlignType == (unsigned)AlignType &&
|
2012-10-05 06:08:14 +08:00
|
|
|
Alignments[i].TypeBitWidth == BitWidth)
|
|
|
|
return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
|
|
|
|
|
|
|
|
// The best match so far depends on what we're looking for.
|
2015-07-22 00:09:58 +08:00
|
|
|
if (AlignType == INTEGER_ALIGN &&
|
|
|
|
Alignments[i].AlignType == INTEGER_ALIGN) {
|
2012-10-05 06:08:14 +08:00
|
|
|
// The "best match" for integers is the smallest size that is larger than
|
|
|
|
// the BitWidth requested.
|
|
|
|
if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
|
2013-01-31 03:24:23 +08:00
|
|
|
Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
|
2012-10-05 06:08:14 +08:00
|
|
|
BestMatchIdx = i;
|
|
|
|
// However, if there isn't one that's larger, then we must use the
|
|
|
|
// largest one we have (see below)
|
|
|
|
if (LargestInt == -1 ||
|
|
|
|
Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
|
|
|
|
LargestInt = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we didn't find an exact solution. Fall back here depending on what
|
|
|
|
// is being looked for.
|
|
|
|
if (BestMatchIdx == -1) {
|
|
|
|
// If we didn't find an integer alignment, fall back on most conservative.
|
|
|
|
if (AlignType == INTEGER_ALIGN) {
|
|
|
|
BestMatchIdx = LargestInt;
|
2015-03-09 05:53:59 +08:00
|
|
|
} else if (AlignType == VECTOR_ALIGN) {
|
2012-10-05 06:08:14 +08:00
|
|
|
// By default, use natural alignment for vector types. This is consistent
|
|
|
|
// with what clang and llvm-gcc do.
|
|
|
|
unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
|
|
|
|
Align *= cast<VectorType>(Ty)->getNumElements();
|
|
|
|
// If the alignment is not a power of 2, round up to the next power of 2.
|
|
|
|
// This happens for non-power-of-2 length vectors.
|
|
|
|
if (Align & (Align-1))
|
|
|
|
Align = NextPowerOf2(Align);
|
|
|
|
return Align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 05:53:59 +08:00
|
|
|
// If we still couldn't find a reasonable default alignment, fall back
|
|
|
|
// to a simple heuristic that the alignment is the first power of two
|
|
|
|
// greater-or-equal to the store size of the type. This is a reasonable
|
|
|
|
// approximation of reality, and if the user wanted something less
|
|
|
|
// less conservative, they should have specified it explicitly in the data
|
|
|
|
// layout.
|
|
|
|
if (BestMatchIdx == -1) {
|
|
|
|
unsigned Align = getTypeStoreSize(Ty);
|
|
|
|
if (Align & (Align-1))
|
|
|
|
Align = NextPowerOf2(Align);
|
|
|
|
return Align;
|
|
|
|
}
|
|
|
|
|
2012-10-05 06:08:14 +08:00
|
|
|
// Since we got a "best match" index, just return it.
|
|
|
|
return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
|
|
|
|
: Alignments[BestMatchIdx].PrefAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StructLayoutMap {
|
2015-07-28 01:15:28 +08:00
|
|
|
typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
|
2012-10-05 06:08:14 +08:00
|
|
|
LayoutInfoTy LayoutInfo;
|
|
|
|
|
|
|
|
public:
|
2014-03-10 23:03:06 +08:00
|
|
|
~StructLayoutMap() {
|
2012-10-05 06:08:14 +08:00
|
|
|
// Remove any layouts.
|
2014-03-10 23:03:06 +08:00
|
|
|
for (const auto &I : LayoutInfo) {
|
|
|
|
StructLayout *Value = I.second;
|
2012-10-05 06:08:14 +08:00
|
|
|
Value->~StructLayout();
|
|
|
|
free(Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
StructLayout *&operator[](StructType *STy) {
|
2012-10-05 06:08:14 +08:00
|
|
|
return LayoutInfo[STy];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2014-02-26 06:23:04 +08:00
|
|
|
void DataLayout::clear() {
|
|
|
|
LegalIntWidths.clear();
|
|
|
|
Alignments.clear();
|
|
|
|
Pointers.clear();
|
|
|
|
delete static_cast<StructLayoutMap *>(LayoutMap);
|
2014-04-09 14:08:46 +08:00
|
|
|
LayoutMap = nullptr;
|
2014-02-26 06:23:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 07:01:22 +08:00
|
|
|
DataLayout::~DataLayout() {
|
2014-02-26 06:23:04 +08:00
|
|
|
clear();
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
if (!LayoutMap)
|
|
|
|
LayoutMap = new StructLayoutMap();
|
|
|
|
|
|
|
|
StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
|
|
|
|
StructLayout *&SL = (*STM)[Ty];
|
|
|
|
if (SL) return SL;
|
|
|
|
|
|
|
|
// Otherwise, create the struct layout. Because it is variable length, we
|
|
|
|
// malloc it, then use placement new.
|
|
|
|
int NumElts = Ty->getNumElements();
|
|
|
|
StructLayout *L =
|
|
|
|
(StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
|
|
|
|
|
|
|
|
// Set SL before calling StructLayout's ctor. The ctor could cause other
|
|
|
|
// entries to be added to TheMap, invalidating our reference.
|
|
|
|
SL = L;
|
|
|
|
|
|
|
|
new (L) StructLayout(Ty, *this);
|
|
|
|
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 00:49:40 +08:00
|
|
|
unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
|
2014-02-27 01:05:38 +08:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-27 00:58:35 +08:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-27 01:05:38 +08:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-27 00:58:35 +08:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
2014-02-27 00:58:35 +08:00
|
|
|
return I->ABIAlign;
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
|
2014-02-27 01:05:38 +08:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-27 00:58:35 +08:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-27 01:05:38 +08:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-27 00:58:35 +08:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
2014-02-27 00:58:35 +08:00
|
|
|
return I->PrefAlign;
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DataLayout::getPointerSize(unsigned AS) const {
|
2014-02-27 01:05:38 +08:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-27 00:58:35 +08:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-27 01:05:38 +08:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-27 00:58:35 +08:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
2014-02-27 00:58:35 +08:00
|
|
|
return I->TypeByteWidth;
|
2014-02-27 00:49:40 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
|
2013-07-27 01:37:20 +08:00
|
|
|
assert(Ty->isPtrOrPtrVectorTy() &&
|
|
|
|
"This should only be called with a pointer or pointer vector type");
|
|
|
|
|
|
|
|
if (Ty->isPointerTy())
|
|
|
|
return getTypeSizeInBits(Ty);
|
|
|
|
|
2013-07-28 03:22:28 +08:00
|
|
|
return getTypeSizeInBits(Ty->getScalarType());
|
2013-07-27 01:37:20 +08:00
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\param abi_or_pref Flag that determines which alignment is returned. true
|
|
|
|
returns the ABI alignment, false returns the preferred alignment.
|
|
|
|
\param Ty The underlying type for which alignment is determined.
|
|
|
|
|
|
|
|
Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
|
|
|
|
== false) for the requested type \a Ty.
|
|
|
|
*/
|
2015-07-28 01:15:28 +08:00
|
|
|
unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
int AlignType = -1;
|
|
|
|
|
|
|
|
assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
// Early escape for the non-numeric types.
|
|
|
|
case Type::LabelTyID:
|
|
|
|
return (abi_or_pref
|
2012-10-10 00:06:12 +08:00
|
|
|
? getPointerABIAlignment(0)
|
|
|
|
: getPointerPrefAlignment(0));
|
|
|
|
case Type::PointerTyID: {
|
2014-09-19 06:28:56 +08:00
|
|
|
unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
|
2012-10-10 00:06:12 +08:00
|
|
|
return (abi_or_pref
|
|
|
|
? getPointerABIAlignment(AS)
|
|
|
|
: getPointerPrefAlignment(AS));
|
|
|
|
}
|
2012-10-05 06:08:14 +08:00
|
|
|
case Type::ArrayTyID:
|
|
|
|
return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
|
|
|
// Packed structure types always have an ABI alignment of one.
|
|
|
|
if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Get the layout annotation... which is lazily created on demand.
|
|
|
|
const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
|
|
|
|
unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
|
|
|
|
return std::max(Align, Layout->getAlignment());
|
|
|
|
}
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
AlignType = INTEGER_ALIGN;
|
|
|
|
break;
|
|
|
|
case Type::HalfTyID:
|
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
// PPC_FP128TyID and FP128TyID have different data contents, but the
|
|
|
|
// same size and alignment, so they look the same here.
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
case Type::FP128TyID:
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
AlignType = FLOAT_ALIGN;
|
|
|
|
break;
|
|
|
|
case Type::X86_MMXTyID:
|
|
|
|
case Type::VectorTyID:
|
|
|
|
AlignType = VECTOR_ALIGN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Bad type for getAlignment!!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
|
|
|
|
abi_or_pref, Ty);
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
return getAlignment(Ty, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
|
|
|
|
/// an integer type of the specified bitwidth.
|
2012-10-05 07:01:22 +08:00
|
|
|
unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
|
2014-04-09 14:08:46 +08:00
|
|
|
return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
return getAlignment(Ty, false);
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
unsigned Align = getPrefTypeAlignment(Ty);
|
|
|
|
assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
|
|
|
|
return Log2_32(Align);
|
|
|
|
}
|
|
|
|
|
2012-10-10 00:06:12 +08:00
|
|
|
IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
|
|
|
|
unsigned AddressSpace) const {
|
|
|
|
return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
|
2012-10-05 06:08:14 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:28 +08:00
|
|
|
Type *DataLayout::getIntPtrType(Type *Ty) const {
|
2012-11-01 16:07:29 +08:00
|
|
|
assert(Ty->isPtrOrPtrVectorTy() &&
|
|
|
|
"Expected a pointer or pointer vector type.");
|
2014-04-24 05:10:15 +08:00
|
|
|
unsigned NumBits = getPointerTypeSizeInBits(Ty);
|
2012-10-30 01:31:46 +08:00
|
|
|
IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
|
2015-07-28 01:15:28 +08:00
|
|
|
if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
|
2012-10-30 01:31:46 +08:00
|
|
|
return VectorType::get(IntTy, VecTy->getNumElements());
|
|
|
|
return IntTy;
|
2012-10-24 23:52:52 +08:00
|
|
|
}
|
|
|
|
|
2013-03-22 16:25:01 +08:00
|
|
|
Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
|
2014-03-10 23:03:06 +08:00
|
|
|
for (unsigned LegalIntWidth : LegalIntWidths)
|
|
|
|
if (Width <= LegalIntWidth)
|
|
|
|
return Type::getIntNTy(C, LegalIntWidth);
|
2014-04-09 14:08:46 +08:00
|
|
|
return nullptr;
|
2013-03-22 16:25:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-17 06:43:16 +08:00
|
|
|
unsigned DataLayout::getLargestLegalIntTypeSize() const {
|
2014-03-10 23:03:06 +08:00
|
|
|
auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
|
|
|
|
return Max != LegalIntWidths.end() ? *Max : 0;
|
2013-09-17 06:43:16 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 07:01:22 +08:00
|
|
|
uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
|
2012-10-05 06:08:14 +08:00
|
|
|
ArrayRef<Value *> Indices) const {
|
2015-07-28 01:15:28 +08:00
|
|
|
Type *Ty = ptrTy;
|
2012-10-05 06:08:14 +08:00
|
|
|
assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
|
|
|
|
uint64_t Result = 0;
|
|
|
|
|
|
|
|
generic_gep_type_iterator<Value* const*>
|
|
|
|
TI = gep_type_begin(ptrTy, Indices);
|
|
|
|
for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
|
|
|
|
++CurIDX, ++TI) {
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(*TI)) {
|
|
|
|
assert(Indices[CurIDX]->getType() ==
|
|
|
|
Type::getInt32Ty(ptrTy->getContext()) &&
|
|
|
|
"Illegal struct idx");
|
|
|
|
unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
|
|
|
|
|
|
|
|
// Get structure layout information...
|
|
|
|
const StructLayout *Layout = getStructLayout(STy);
|
|
|
|
|
|
|
|
// Add in the offset, as calculated by the structure layout info...
|
|
|
|
Result += Layout->getElementOffset(FieldNo);
|
|
|
|
|
|
|
|
// Update Ty to refer to current element
|
|
|
|
Ty = STy->getElementType(FieldNo);
|
|
|
|
} else {
|
|
|
|
// Update Ty to refer to current element
|
|
|
|
Ty = cast<SequentialType>(Ty)->getElementType();
|
|
|
|
|
|
|
|
// Get the array index and the size of each array element.
|
|
|
|
if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
|
|
|
|
Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPreferredAlignment - Return the preferred alignment of the specified
|
|
|
|
/// global. This includes an explicitly requested alignment (if the global
|
|
|
|
/// has one).
|
2012-10-05 07:01:22 +08:00
|
|
|
unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
Type *ElemType = GV->getType()->getElementType();
|
|
|
|
unsigned Alignment = getPrefTypeAlignment(ElemType);
|
|
|
|
unsigned GVAlignment = GV->getAlignment();
|
|
|
|
if (GVAlignment >= Alignment) {
|
|
|
|
Alignment = GVAlignment;
|
|
|
|
} else if (GVAlignment != 0) {
|
|
|
|
Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GV->hasInitializer() && GVAlignment == 0) {
|
|
|
|
if (Alignment < 16) {
|
|
|
|
// If the global is not external, see if it is large. If so, give it a
|
|
|
|
// larger alignment.
|
|
|
|
if (getTypeSizeInBits(ElemType) > 128)
|
|
|
|
Alignment = 16; // 16-byte alignment.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPreferredAlignmentLog - Return the preferred alignment of the
|
|
|
|
/// specified global, returned in log form. This includes an explicitly
|
|
|
|
/// requested alignment (if the global has one).
|
2012-10-05 07:01:22 +08:00
|
|
|
unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
|
2012-10-05 06:08:14 +08:00
|
|
|
return Log2_32(getPreferredAlignment(GV));
|
|
|
|
}
|
2014-02-26 01:30:31 +08:00
|
|
|
|