2012-10-04 23:01:22 +00:00
|
|
|
//===-- DataLayout.cpp - Data size & alignment routines --------------------==//
|
2012-10-04 22:08:14 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-10-04 23:01:22 +00:00
|
|
|
// This file defines layout properties related to datatype size/offset/alignment
|
2012-10-04 22:08:14 +00: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 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-12-19 23:03:03 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-01-03 19:21:54 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-04 10:40:04 +00:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-10-04 22:08:14 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-10-04 22:08:14 +00:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-10-04 22:08:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
// Handle the Pass registration stuff necessary to use DataLayout's.
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2014-02-25 17:30:31 +00:00
|
|
|
INITIALIZE_PASS(DataLayoutPass, "datalayout", "Data Layout", false, true)
|
|
|
|
char DataLayoutPass::ID = 0;
|
2012-10-04 22:08:14 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Support for StructLayout
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-04-16 15:41:18 +00:00
|
|
|
StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
|
2012-10-04 22:08:14 +00:00
|
|
|
assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
|
|
|
|
StructAlignment = 0;
|
|
|
|
StructSize = 0;
|
|
|
|
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 15:41:18 +00:00
|
|
|
unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
|
2012-10-04 22:08:14 +00:00
|
|
|
|
|
|
|
// Add padding if necessary to align the data element properly.
|
|
|
|
if ((StructSize & (TyAlign-1)) != 0)
|
2012-10-04 23:01:22 +00:00
|
|
|
StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign);
|
2012-10-04 22:08:14 +00:00
|
|
|
|
|
|
|
// Keep track of maximum alignment constraint.
|
|
|
|
StructAlignment = std::max(TyAlign, StructAlignment);
|
|
|
|
|
|
|
|
MemberOffsets[i] = StructSize;
|
2013-04-16 15:41:18 +00:00
|
|
|
StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
|
2012-10-04 22:08:14 +00: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.
|
|
|
|
if ((StructSize & (StructAlignment-1)) != 0)
|
2012-10-04 23:01:22 +00:00
|
|
|
StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment);
|
2012-10-04 22:08:14 +00: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-04 23:01:22 +00:00
|
|
|
// LayoutAlignElem, LayoutAlign support
|
2012-10-04 22:08:14 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
LayoutAlignElem
|
|
|
|
LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
|
2012-10-04 22:08:14 +00:00
|
|
|
unsigned pref_align, uint32_t bit_width) {
|
|
|
|
assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
|
2012-10-04 23:01:22 +00:00
|
|
|
LayoutAlignElem retval;
|
2012-10-04 22:08:14 +00:00
|
|
|
retval.AlignType = align_type;
|
|
|
|
retval.ABIAlign = abi_align;
|
|
|
|
retval.PrefAlign = pref_align;
|
|
|
|
retval.TypeBitWidth = bit_width;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-10-04 23:01:22 +00:00
|
|
|
LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
return (AlignType == rhs.AlignType
|
|
|
|
&& ABIAlign == rhs.ABIAlign
|
|
|
|
&& PrefAlign == rhs.PrefAlign
|
|
|
|
&& TypeBitWidth == rhs.TypeBitWidth);
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
const LayoutAlignElem
|
2013-11-19 20:28:04 +00:00
|
|
|
DataLayout::InvalidAlignmentElem = { INVALID_ALIGN, 0, 0, 0 };
|
2012-10-09 16:06:12 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PointerAlignElem, PointerAlign support
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PointerAlignElem
|
2013-12-13 23:15:20 +00:00
|
|
|
PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
|
|
|
|
unsigned PrefAlign, uint32_t TypeByteWidth) {
|
|
|
|
assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
|
2012-10-09 16:06:12 +00:00
|
|
|
PointerAlignElem retval;
|
2013-12-13 23:15:20 +00:00
|
|
|
retval.AddressSpace = AddressSpace;
|
|
|
|
retval.ABIAlign = ABIAlign;
|
|
|
|
retval.PrefAlign = PrefAlign;
|
|
|
|
retval.TypeByteWidth = TypeByteWidth;
|
2012-10-09 16:06:12 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
|
|
|
|
return (ABIAlign == rhs.ABIAlign
|
|
|
|
&& AddressSpace == rhs.AddressSpace
|
|
|
|
&& PrefAlign == rhs.PrefAlign
|
2013-12-13 23:15:20 +00:00
|
|
|
&& TypeByteWidth == rhs.TypeByteWidth);
|
2012-10-09 16:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PointerAlignElem
|
2013-11-19 20:28:04 +00:00
|
|
|
DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
|
2012-10-04 22:08:14 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-10-04 23:01:22 +00:00
|
|
|
// DataLayout Class Implementation
|
2012-10-04 22:08:14 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-03 19:21:54 +00:00
|
|
|
const char *DataLayout::getManglingComponent(const Triple &T) {
|
|
|
|
if (T.isOSBinFormatMachO())
|
|
|
|
return "-m:o";
|
|
|
|
if (T.isOSBinFormatELF() || T.isArch64Bit())
|
|
|
|
return "-m:e";
|
|
|
|
assert(T.isOSBinFormatCOFF());
|
2014-01-10 13:42:12 +00:00
|
|
|
return "-m:w";
|
2014-01-03 19:21:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 15:21:32 +00:00
|
|
|
static const LayoutAlignElem DefaultAlignments[] = {
|
2013-12-19 23:03:03 +00: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-25 22:23:04 +00:00
|
|
|
void DataLayout::reset(StringRef Desc) {
|
|
|
|
clear();
|
|
|
|
|
2012-10-04 22:08:14 +00:00
|
|
|
LayoutMap = 0;
|
|
|
|
LittleEndian = false;
|
|
|
|
StackNaturalAlign = 0;
|
2014-01-03 19:21:54 +00:00
|
|
|
ManglingMode = MM_None;
|
2012-10-04 22:08:14 +00:00
|
|
|
|
|
|
|
// Default alignments
|
2014-03-10 15:03:06 +00:00
|
|
|
for (const LayoutAlignElem &E : DefaultAlignments) {
|
2013-12-19 23:03:03 +00:00
|
|
|
setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
|
|
|
|
E.TypeBitWidth);
|
|
|
|
}
|
2012-10-09 16:06:12 +00:00
|
|
|
setPointerAlignment(0, 8, 8, 8);
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2012-11-30 10:06:59 +00: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);
|
|
|
|
assert((!Split.second.empty() || Split.first == Str) &&
|
|
|
|
"a trailing separator is not allowed");
|
|
|
|
return Split;
|
2012-11-14 09:04:56 +00:00
|
|
|
}
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2014-01-07 19:51:38 +00:00
|
|
|
/// Get an unsigned integer, including error checks.
|
2012-11-28 12:13:12 +00:00
|
|
|
static unsigned getInt(StringRef R) {
|
2014-01-13 22:04:55 +00:00
|
|
|
unsigned Result;
|
2012-11-28 14:32:52 +00:00
|
|
|
bool error = R.getAsInteger(10, Result); (void)error;
|
2014-01-13 22:04:55 +00:00
|
|
|
if (error)
|
|
|
|
report_fatal_error("not a number, or does not fit in an unsigned int");
|
2012-11-28 12:13:12 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
/// Convert bits into bytes. Assert if not a byte width multiple.
|
|
|
|
static unsigned inBytes(unsigned Bits) {
|
|
|
|
assert(Bits % 8 == 0 && "number of bits must be a byte width multiple");
|
|
|
|
return Bits / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataLayout::parseSpecifier(StringRef Desc) {
|
2012-10-04 22:08:14 +00:00
|
|
|
while (!Desc.empty()) {
|
2012-11-30 10:06:59 +00:00
|
|
|
// Split at '-'.
|
|
|
|
std::pair<StringRef, StringRef> Split = split(Desc, '-');
|
2012-10-04 22:08:14 +00:00
|
|
|
Desc = Split.second;
|
|
|
|
|
2012-11-30 10:06:59 +00: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-04 22:08:14 +00:00
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
char Specifier = Tok.front();
|
|
|
|
Tok = Tok.substr(1);
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
switch (Specifier) {
|
2014-01-01 22:29:43 +00:00
|
|
|
case 's':
|
|
|
|
// Ignored for backward compatibility.
|
|
|
|
// FIXME: remove this on LLVM 4.0.
|
|
|
|
break;
|
2012-10-04 22:08:14 +00:00
|
|
|
case 'E':
|
2012-11-14 09:04:56 +00:00
|
|
|
LittleEndian = false;
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2012-11-14 09:04:56 +00:00
|
|
|
LittleEndian = true;
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
case 'p': {
|
2012-11-30 10:06:59 +00:00
|
|
|
// Address space.
|
|
|
|
unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
|
|
|
|
assert(AddrSpace < 1 << 24 &&
|
|
|
|
"Invalid address space, must be a 24bit integer");
|
|
|
|
|
|
|
|
// Size.
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned PointerMemSize = inBytes(getInt(Tok));
|
|
|
|
|
|
|
|
// ABI alignment.
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned PointerABIAlign = inBytes(getInt(Tok));
|
|
|
|
|
|
|
|
// Preferred alignment.
|
|
|
|
unsigned PointerPrefAlign = PointerABIAlign;
|
|
|
|
if (!Rest.empty()) {
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
PointerPrefAlign = inBytes(getInt(Tok));
|
2012-10-04 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
|
|
|
|
PointerMemSize);
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'i':
|
|
|
|
case 'v':
|
|
|
|
case 'f':
|
2014-01-01 22:29:43 +00:00
|
|
|
case 'a': {
|
2012-10-04 22:08:14 +00:00
|
|
|
AlignTypeEnum AlignType;
|
2012-11-30 10:06:59 +00:00
|
|
|
switch (Specifier) {
|
2012-10-04 22:08:14 +00: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 10:06:59 +00:00
|
|
|
// Bit size.
|
|
|
|
unsigned Size = Tok.empty() ? 0 : getInt(Tok);
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2014-01-06 21:40:24 +00:00
|
|
|
assert((AlignType != AGGREGATE_ALIGN || Size == 0) &&
|
|
|
|
"These specifications don't have a size");
|
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
// ABI alignment.
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
unsigned ABIAlign = inBytes(getInt(Tok));
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2012-11-30 10:06:59 +00:00
|
|
|
// Preferred alignment.
|
|
|
|
unsigned PrefAlign = ABIAlign;
|
|
|
|
if (!Rest.empty()) {
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
PrefAlign = inBytes(getInt(Tok));
|
2012-10-04 22:08:14 +00:00
|
|
|
}
|
2012-11-30 10:06:59 +00:00
|
|
|
|
2012-11-14 09:04:56 +00:00
|
|
|
setAlignment(AlignType, ABIAlign, PrefAlign, Size);
|
2012-10-04 23:01:22 +00:00
|
|
|
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n': // Native integer types.
|
2012-11-30 10:06:59 +00:00
|
|
|
for (;;) {
|
|
|
|
unsigned Width = getInt(Tok);
|
|
|
|
assert(Width != 0 && "width must be non-zero");
|
2012-11-28 12:13:12 +00:00
|
|
|
LegalIntWidths.push_back(Width);
|
2012-11-30 10:06:59 +00:00
|
|
|
if (Rest.empty())
|
|
|
|
break;
|
|
|
|
Split = split(Rest, ':');
|
|
|
|
}
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
case 'S': { // Stack natural alignment.
|
2012-11-30 10:06:59 +00:00
|
|
|
StackNaturalAlign = inBytes(getInt(Tok));
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-03 19:21:54 +00:00
|
|
|
case 'm':
|
|
|
|
assert(Tok.empty());
|
|
|
|
assert(Rest.size() == 1);
|
|
|
|
switch(Rest[0]) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown mangling in datalayout string");
|
|
|
|
case 'e':
|
|
|
|
ManglingMode = MM_ELF;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
ManglingMode = MM_MachO;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
ManglingMode = MM_Mips;
|
|
|
|
break;
|
2014-01-10 13:42:12 +00:00
|
|
|
case 'w':
|
|
|
|
ManglingMode = MM_WINCOFF;
|
2014-01-03 19:21:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-04 22:08:14 +00:00
|
|
|
default:
|
2012-11-30 10:06:59 +00:00
|
|
|
llvm_unreachable("Unknown specifier in datalayout string");
|
2012-10-04 22:08:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 22:23:04 +00:00
|
|
|
DataLayout::DataLayout(const Module *M) : LayoutMap(0) {
|
2014-02-25 20:01:08 +00:00
|
|
|
const DataLayout *Other = M->getDataLayout();
|
|
|
|
if (Other)
|
|
|
|
*this = *Other;
|
|
|
|
else
|
2014-02-25 22:23:04 +00:00
|
|
|
reset("");
|
2014-02-25 20:01:08 +00:00
|
|
|
}
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2014-02-26 17:02:08 +00:00
|
|
|
bool DataLayout::operator==(const DataLayout &Other) const {
|
|
|
|
bool Ret = LittleEndian == Other.LittleEndian &&
|
|
|
|
StackNaturalAlign == Other.StackNaturalAlign &&
|
|
|
|
ManglingMode == Other.ManglingMode &&
|
|
|
|
LegalIntWidths == Other.LegalIntWidths &&
|
|
|
|
Alignments == Other.Alignments && Pointers == Pointers;
|
|
|
|
assert(Ret == (getStringRepresentation() == Other.getStringRepresentation()));
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2012-10-04 22:08:14 +00:00
|
|
|
void
|
2012-10-04 23:01:22 +00:00
|
|
|
DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
|
2012-10-04 22:08:14 +00:00
|
|
|
unsigned pref_align, uint32_t bit_width) {
|
|
|
|
assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
|
|
|
|
assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
|
|
|
|
assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
|
2014-03-10 15:03:06 +00:00
|
|
|
for (LayoutAlignElem &Elem : Alignments) {
|
|
|
|
if (Elem.AlignType == (unsigned)align_type &&
|
|
|
|
Elem.TypeBitWidth == bit_width) {
|
2012-10-04 22:08:14 +00:00
|
|
|
// Update the abi, preferred alignments.
|
2014-03-10 15:03:06 +00:00
|
|
|
Elem.ABIAlign = abi_align;
|
|
|
|
Elem.PrefAlign = pref_align;
|
2012-10-04 22:08:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
|
2012-10-04 22:08:14 +00:00
|
|
|
pref_align, bit_width));
|
|
|
|
}
|
|
|
|
|
2014-02-26 16:58:35 +00:00
|
|
|
DataLayout::PointersTy::iterator
|
2014-02-26 17:05:38 +00:00
|
|
|
DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
|
2014-02-26 16:58:35 +00:00
|
|
|
return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
|
2014-03-10 15:03:06 +00:00
|
|
|
[](const PointerAlignElem &A, uint32_t AddressSpace) {
|
|
|
|
return A.AddressSpace < AddressSpace;
|
|
|
|
});
|
2014-02-26 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 23:15:20 +00:00
|
|
|
void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
|
|
|
|
unsigned PrefAlign,
|
|
|
|
uint32_t TypeByteWidth) {
|
|
|
|
assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
|
2014-02-26 17:05:38 +00:00
|
|
|
PointersTy::iterator I = findPointerLowerBound(AddrSpace);
|
2014-02-26 16:58:35 +00:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
|
|
|
|
Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
|
|
|
|
TypeByteWidth));
|
2012-10-09 16:06:12 +00:00
|
|
|
} else {
|
2014-02-26 16:58:35 +00:00
|
|
|
I->ABIAlign = ABIAlign;
|
|
|
|
I->PrefAlign = PrefAlign;
|
|
|
|
I->TypeByteWidth = TypeByteWidth;
|
2012-10-09 16:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 22:08:14 +00:00
|
|
|
/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
|
2012-10-04 23:01:22 +00:00
|
|
|
/// preferred if ABIInfo = false) the layout wants for the specified datatype.
|
|
|
|
unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
|
2012-10-04 22:08:14 +00:00
|
|
|
uint32_t BitWidth, bool ABIInfo,
|
|
|
|
Type *Ty) const {
|
|
|
|
// 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-05 17:02:14 +00:00
|
|
|
if (Alignments[i].AlignType == (unsigned)AlignType &&
|
2012-10-04 22:08:14 +00: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.
|
|
|
|
if (AlignType == INTEGER_ALIGN &&
|
|
|
|
Alignments[i].AlignType == INTEGER_ALIGN) {
|
|
|
|
// 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-30 19:24:23 +00:00
|
|
|
Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
|
2012-10-04 22:08:14 +00: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;
|
|
|
|
} else {
|
|
|
|
assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we got a "best match" index, just return it.
|
|
|
|
return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
|
|
|
|
: Alignments[BestMatchIdx].PrefAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StructLayoutMap {
|
|
|
|
typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
|
|
|
|
LayoutInfoTy LayoutInfo;
|
|
|
|
|
|
|
|
public:
|
2014-03-10 15:03:06 +00:00
|
|
|
~StructLayoutMap() {
|
2012-10-04 22:08:14 +00:00
|
|
|
// Remove any layouts.
|
2014-03-10 15:03:06 +00:00
|
|
|
for (const auto &I : LayoutInfo) {
|
|
|
|
StructLayout *Value = I.second;
|
2012-10-04 22:08:14 +00:00
|
|
|
Value->~StructLayout();
|
|
|
|
free(Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StructLayout *&operator[](StructType *STy) {
|
|
|
|
return LayoutInfo[STy];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2014-02-25 22:23:04 +00:00
|
|
|
void DataLayout::clear() {
|
|
|
|
LegalIntWidths.clear();
|
|
|
|
Alignments.clear();
|
|
|
|
Pointers.clear();
|
|
|
|
delete static_cast<StructLayoutMap *>(LayoutMap);
|
|
|
|
LayoutMap = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
DataLayout::~DataLayout() {
|
2014-02-25 22:23:04 +00:00
|
|
|
clear();
|
2012-10-04 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
|
2012-10-04 22:08:14 +00: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;
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
std::string DataLayout::getStringRepresentation() const {
|
2012-10-04 22:08:14 +00:00
|
|
|
std::string Result;
|
|
|
|
raw_string_ostream OS(Result);
|
|
|
|
|
2012-10-09 16:06:12 +00:00
|
|
|
OS << (LittleEndian ? "e" : "E");
|
2014-01-03 19:21:54 +00:00
|
|
|
|
|
|
|
switch (ManglingMode) {
|
|
|
|
case MM_None:
|
|
|
|
break;
|
|
|
|
case MM_ELF:
|
|
|
|
OS << "-m:e";
|
|
|
|
break;
|
|
|
|
case MM_MachO:
|
|
|
|
OS << "-m:o";
|
|
|
|
break;
|
2014-01-10 13:42:12 +00:00
|
|
|
case MM_WINCOFF:
|
|
|
|
OS << "-m:w";
|
2014-01-03 19:21:54 +00:00
|
|
|
break;
|
|
|
|
case MM_Mips:
|
|
|
|
OS << "-m:m";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-10 15:03:06 +00:00
|
|
|
for (const PointerAlignElem &PI : Pointers) {
|
2013-12-19 23:03:03 +00:00
|
|
|
// Skip default.
|
|
|
|
if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
|
|
|
|
PI.TypeByteWidth == 8)
|
|
|
|
continue;
|
|
|
|
|
2012-10-09 16:06:12 +00:00
|
|
|
OS << "-p";
|
|
|
|
if (PI.AddressSpace) {
|
|
|
|
OS << PI.AddressSpace;
|
|
|
|
}
|
2013-12-19 23:03:03 +00:00
|
|
|
OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
|
|
|
|
if (PI.PrefAlign != PI.ABIAlign)
|
|
|
|
OS << ':' << PI.PrefAlign*8;
|
2012-10-09 16:06:12 +00:00
|
|
|
}
|
2012-10-04 22:08:14 +00:00
|
|
|
|
2014-03-10 15:03:06 +00:00
|
|
|
for (const LayoutAlignElem &AI : Alignments) {
|
|
|
|
if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments),
|
|
|
|
AI) != std::end(DefaultAlignments))
|
2013-12-19 23:03:03 +00:00
|
|
|
continue;
|
|
|
|
OS << '-' << (char)AI.AlignType;
|
|
|
|
if (AI.TypeBitWidth)
|
|
|
|
OS << AI.TypeBitWidth;
|
|
|
|
OS << ':' << AI.ABIAlign*8;
|
|
|
|
if (AI.ABIAlign != AI.PrefAlign)
|
|
|
|
OS << ':' << AI.PrefAlign*8;
|
2012-10-04 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!LegalIntWidths.empty()) {
|
|
|
|
OS << "-n" << (unsigned)LegalIntWidths[0];
|
|
|
|
|
|
|
|
for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
|
|
|
|
OS << ':' << (unsigned)LegalIntWidths[i];
|
|
|
|
}
|
2013-12-19 23:03:03 +00:00
|
|
|
|
|
|
|
if (StackNaturalAlign)
|
|
|
|
OS << "-S" << StackNaturalAlign*8;
|
|
|
|
|
2012-10-04 22:08:14 +00:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2014-02-26 16:49:40 +00:00
|
|
|
unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
|
2014-02-26 17:05:38 +00:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-26 16:58:35 +00:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-26 17:05:38 +00:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-26 16:58:35 +00:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
2014-02-26 16:58:35 +00:00
|
|
|
return I->ABIAlign;
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
|
2014-02-26 17:05:38 +00:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-26 16:58:35 +00:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-26 17:05:38 +00:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-26 16:58:35 +00:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
2014-02-26 16:58:35 +00:00
|
|
|
return I->PrefAlign;
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DataLayout::getPointerSize(unsigned AS) const {
|
2014-02-26 17:05:38 +00:00
|
|
|
PointersTy::const_iterator I = findPointerLowerBound(AS);
|
2014-02-26 16:58:35 +00:00
|
|
|
if (I == Pointers.end() || I->AddressSpace != AS) {
|
2014-02-26 17:05:38 +00:00
|
|
|
I = findPointerLowerBound(0);
|
2014-02-26 16:58:35 +00:00
|
|
|
assert(I->AddressSpace == 0);
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
2014-02-26 16:58:35 +00:00
|
|
|
return I->TypeByteWidth;
|
2014-02-26 16:49:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:37:20 +00:00
|
|
|
unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
|
|
|
|
assert(Ty->isPtrOrPtrVectorTy() &&
|
|
|
|
"This should only be called with a pointer or pointer vector type");
|
|
|
|
|
|
|
|
if (Ty->isPointerTy())
|
|
|
|
return getTypeSizeInBits(Ty);
|
|
|
|
|
2013-07-27 19:22:28 +00:00
|
|
|
return getTypeSizeInBits(Ty->getScalarType());
|
2013-07-26 17:37:20 +00:00
|
|
|
}
|
2012-10-04 22:08:14 +00: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.
|
|
|
|
*/
|
2012-10-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
|
2012-10-04 22:08:14 +00: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-09 16:06:12 +00:00
|
|
|
? getPointerABIAlignment(0)
|
|
|
|
: getPointerPrefAlignment(0));
|
|
|
|
case Type::PointerTyID: {
|
|
|
|
unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
|
|
|
|
return (abi_or_pref
|
|
|
|
? getPointerABIAlignment(AS)
|
|
|
|
: getPointerPrefAlignment(AS));
|
|
|
|
}
|
2012-10-04 22:08:14 +00: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);
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
return getAlignment(Ty, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
|
|
|
|
/// an integer type of the specified bitwidth.
|
2012-10-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
return getAlignment(Ty, false);
|
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
unsigned Align = getPrefTypeAlignment(Ty);
|
|
|
|
assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
|
|
|
|
return Log2_32(Align);
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:06:12 +00:00
|
|
|
IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
|
|
|
|
unsigned AddressSpace) const {
|
|
|
|
return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
|
2012-10-04 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 17:31:46 +00:00
|
|
|
Type *DataLayout::getIntPtrType(Type *Ty) const {
|
2012-11-01 08:07:29 +00:00
|
|
|
assert(Ty->isPtrOrPtrVectorTy() &&
|
|
|
|
"Expected a pointer or pointer vector type.");
|
|
|
|
unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
|
2012-10-29 17:31:46 +00:00
|
|
|
IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
|
|
|
|
if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return VectorType::get(IntTy, VecTy->getNumElements());
|
|
|
|
return IntTy;
|
2012-10-24 15:52:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 08:25:01 +00:00
|
|
|
Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
|
2014-03-10 15:03:06 +00:00
|
|
|
for (unsigned LegalIntWidth : LegalIntWidths)
|
|
|
|
if (Width <= LegalIntWidth)
|
|
|
|
return Type::getIntNTy(C, LegalIntWidth);
|
2013-03-22 08:25:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 22:43:16 +00:00
|
|
|
unsigned DataLayout::getLargestLegalIntTypeSize() const {
|
2014-03-10 15:03:06 +00:00
|
|
|
auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
|
|
|
|
return Max != LegalIntWidths.end() ? *Max : 0;
|
2013-09-16 22:43:16 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 23:01:22 +00:00
|
|
|
uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
|
2012-10-04 22:08:14 +00:00
|
|
|
ArrayRef<Value *> Indices) const {
|
|
|
|
Type *Ty = ptrTy;
|
|
|
|
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-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
|
2012-10-04 22:08:14 +00: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-04 23:01:22 +00:00
|
|
|
unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
|
2012-10-04 22:08:14 +00:00
|
|
|
return Log2_32(getPreferredAlignment(GV));
|
|
|
|
}
|
2014-02-25 17:30:31 +00:00
|
|
|
|
|
|
|
DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") {
|
|
|
|
report_fatal_error("Bad DataLayoutPass ctor used. Tool did not specify a "
|
|
|
|
"DataLayout to use?");
|
|
|
|
}
|
|
|
|
|
|
|
|
DataLayoutPass::~DataLayoutPass() {}
|
|
|
|
|
|
|
|
DataLayoutPass::DataLayoutPass(const DataLayout &DL)
|
|
|
|
: ImmutablePass(ID), DL(DL) {
|
|
|
|
initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
DataLayoutPass::DataLayoutPass(const Module *M) : ImmutablePass(ID), DL(M) {
|
|
|
|
initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|