-TargetData::TargetData(const std::string &TargetName,
- bool isLittleEndian, unsigned char PtrSize,
- unsigned char PtrAl, unsigned char DoubleAl,
- unsigned char FloatAl, unsigned char LongAl,
- unsigned char IntAl, unsigned char ShortAl,
- unsigned char ByteAl)
- : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
- AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
-
- // If this assert triggers, a pass "required" TargetData information, but the
- // top level tool did not provide once for it. We do not want to default
- // construct, or else we might end up using a bad endianness or pointer size!
- //
- assert(!TargetName.empty() &&
- "ERROR: Tool did not specify a target data to use!");
-
- LittleEndian = isLittleEndian;
- PointerSize = PtrSize;
- PointerAlignment = PtrAl;
- DoubleAlignment = DoubleAl;
- assert(DoubleAlignment == PtrAl &&
- "Double alignment and pointer alignment agree for now!");
- FloatAlignment = FloatAl;
- LongAlignment = LongAl;
- IntAlignment = IntAl;
- ShortAlignment = ShortAl;
- ByteAlignment = ByteAl;
-}
-
-TargetData::TargetData(const std::string &ToolName, const Module *M)
- : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
- AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
-
- LittleEndian = M->getEndianness() != Module::BigEndian;
- PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
- PointerAlignment = PointerSize;
- DoubleAlignment = PointerSize;
- FloatAlignment = 4;
- LongAlignment = 8;
- IntAlignment = 4;
- ShortAlignment = 2;
- ByteAlignment = 1;
+/*!
+ A TargetDescription string consists of a sequence of hyphen-delimited
+ specifiers for target endianness, pointer size and alignments, and various
+ primitive type sizes and alignments. A typical string looks something like:
+ <br><br>
+ "E-p:32:32:32-i1:8:8-i8:8:8-i32:32:32-i64:32:64-f32:32:32-f64:32:64"
+ <br><br>
+ (note: this string is not fully specified and is only an example.)
+ \p
+ Alignments come in two flavors: ABI and preferred. ABI alignment (abi_align,
+ below) dictates how a type will be aligned within an aggregate and when used
+ as an argument. Preferred alignment (pref_align, below) determines a type's
+ alignment when emitted as a global.
+ \p
+ Specifier string details:
+ <br><br>
+ <i>[E|e]</i>: Endianness. "E" specifies a big-endian target data model, "e"
+ specifies a little-endian target data model.
+ <br><br>
+ <i>p:@verbatim<size>:<abi_align>:<pref_align>@endverbatim</i>: Pointer size,
+ ABI and preferred alignment.
+ <br><br>
+ <i>@verbatim<type><size>:<abi_align>:<pref_align>@endverbatim</i>: Numeric type
+ alignment. Type is
+ one of <i>i|f|v|a</i>, corresponding to integer, floating point, vector (aka
+ packed) or aggregate. Size indicates the size, e.g., 32 or 64 bits.
+ \p
+ The default string, fully specified is:
+ <br><br>
+ "E-p:64:64:64-a0:0:0-f32:32:32-f64:0:64"
+ "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:0:64"
+ "-v64:64:64-v128:128:128"
+ <br><br>
+ Note that in the case of aggregates, 0 is the default ABI and preferred
+ alignment. This is a special case, where the aggregate's computed worst-case
+ alignment will be used.
+ */
+void TargetData::init(const std::string &TargetDescription) {
+ std::string temp = TargetDescription;
+
+ LittleEndian = false;
+ PointerMemSize = 8;
+ PointerABIAlign = 8;
+ PointerPrefAlign = PointerABIAlign;
+
+ // Default alignments
+ setAlignment(INTEGER_ALIGN, 1, 1, 1); // Bool
+ setAlignment(INTEGER_ALIGN, 1, 1, 8); // Byte
+ setAlignment(INTEGER_ALIGN, 2, 2, 16); // short
+ setAlignment(INTEGER_ALIGN, 4, 4, 32); // int
+ setAlignment(INTEGER_ALIGN, 4, 8, 64); // long
+ setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
+ setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
+ setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32
+ setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
+ setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct, union, class, ...
+
+ while (!temp.empty()) {
+ std::string token = getToken(temp, "-");
+ std::string arg0 = getToken(token, ":");
+ const char *p = arg0.c_str();
+ switch(*p) {
+ case 'E':
+ LittleEndian = false;
+ break;
+ case 'e':
+ LittleEndian = true;
+ break;
+ case 'p':
+ PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
+ PointerABIAlign = atoi(getToken(token,":").c_str()) / 8;
+ PointerPrefAlign = atoi(getToken(token,":").c_str()) / 8;
+ if (PointerPrefAlign == 0)
+ PointerPrefAlign = PointerABIAlign;
+ break;
+ case 'i':
+ case 'v':
+ case 'f':
+ case 'a':
+ case 's': {
+ AlignTypeEnum align_type = STACK_ALIGN; // Dummy init, silence warning
+ switch(*p) {
+ case 'i': align_type = INTEGER_ALIGN; break;
+ case 'v': align_type = VECTOR_ALIGN; break;
+ case 'f': align_type = FLOAT_ALIGN; break;
+ case 'a': align_type = AGGREGATE_ALIGN; break;
+ case 's': align_type = STACK_ALIGN; break;
+ }
+ uint32_t size = (uint32_t) atoi(++p);
+ unsigned char abi_align = atoi(getToken(token, ":").c_str()) / 8;
+ unsigned char pref_align = atoi(getToken(token, ":").c_str()) / 8;
+ if (pref_align == 0)
+ pref_align = abi_align;
+ setAlignment(align_type, abi_align, pref_align, size);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
+
+TargetData::TargetData(const Module *M)
+ : ImmutablePass(&ID) {
+ init(M->getDataLayout());
+}
+
+void
+TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
+ unsigned char pref_align, uint32_t bit_width) {
+ assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
+ for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
+ if (Alignments[i].AlignType == align_type &&
+ Alignments[i].TypeBitWidth == bit_width) {
+ // Update the abi, preferred alignments.
+ Alignments[i].ABIAlign = abi_align;
+ Alignments[i].PrefAlign = pref_align;
+ return;
+ }
+ }
+
+ Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
+ pref_align, bit_width));
+}
+
+/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
+/// preferred if ABIInfo = false) the target wants for the specified datatype.
+unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
+ uint32_t BitWidth, bool ABIInfo,
+ const 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) {
+ if (Alignments[i].AlignType == AlignType &&
+ 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 == VECTOR_ALIGN && Alignments[i].AlignType == VECTOR_ALIGN) {
+ // If this is a specification for a smaller vector type, we will fall back
+ // to it. This happens because <128 x double> can be implemented in terms
+ // of 64 <2 x double>.
+ if (Alignments[i].TypeBitWidth < BitWidth) {
+ // Verify that we pick the biggest of the fallbacks.
+ if (BestMatchIdx == -1 ||
+ Alignments[BestMatchIdx].TypeBitWidth < Alignments[i].TypeBitWidth)
+ BestMatchIdx = i;
+ }
+ } else 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 ||
+ Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
+ 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!");
+
+ // If we didn't find a vector size that is smaller or equal to this type,
+ // then we will end up scalarizing this to its element type. Just return
+ // the alignment of the element.
+ return getAlignment(cast<VectorType>(Ty)->getElementType(), ABIInfo);
+ }
+ }
+
+ // Since we got a "best match" index, just return it.
+ return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
+ : Alignments[BestMatchIdx].PrefAlign;
+}
+
+namespace {
+
+/// LayoutInfo - The lazy cache of structure layout information maintained by
+/// TargetData. Note that the struct types must have been free'd before
+/// llvm_shutdown is called (and thus this is deallocated) because all the
+/// targets with cached elements should have been destroyed.
+///
+typedef std::pair<const TargetData*,const StructType*> LayoutKey;
+
+struct DenseMapLayoutKeyInfo {
+ static inline LayoutKey getEmptyKey() { return LayoutKey(0, 0); }
+ static inline LayoutKey getTombstoneKey() {
+ return LayoutKey((TargetData*)(intptr_t)-1, 0);
+ }
+ static unsigned getHashValue(const LayoutKey &Val) {
+ return DenseMapInfo<void*>::getHashValue(Val.first) ^
+ DenseMapInfo<void*>::getHashValue(Val.second);
+ }
+ static bool isEqual(const LayoutKey &LHS, const LayoutKey &RHS) {
+ return LHS == RHS;
+ }
+
+ static bool isPod() { return true; }
+};
+
+typedef DenseMap<LayoutKey, StructLayout*, DenseMapLayoutKeyInfo> LayoutInfoTy;
+