1 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target properties related to datatype size/offset/alignment
11 // information. It uses lazy annotations to cache information about how
12 // structure types are laid out and used.
14 // This structure should be created once, filled in if the defaults are not
15 // correct and then passed around by const&. None of the members functions
16 // require modification to the object.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_TARGET_TARGETDATA_H
21 #define LLVM_TARGET_TARGETDATA_H
23 #include "llvm/Pass.h"
24 #include "llvm/Support/DataTypes.h"
35 class TargetData : public ImmutablePass {
36 bool LittleEndian; // Defaults to false
37 unsigned char BoolAlignment; // Defaults to 1 byte
38 unsigned char ByteAlignment; // Defaults to 1 byte
39 unsigned char ShortAlignment; // Defaults to 2 bytes
40 unsigned char IntAlignment; // Defaults to 4 bytes
41 unsigned char LongAlignment; // Defaults to 8 bytes
42 unsigned char FloatAlignment; // Defaults to 4 bytes
43 unsigned char DoubleAlignment; // Defaults to 8 bytes
44 unsigned char PointerSize; // Defaults to 8 bytes
45 unsigned char PointerAlignment; // Defaults to 8 bytes
48 /// Default ctor - This has to exist, because this is a pass, but it should
51 assert(0 && "ERROR: Bad TargetData ctor used. "
52 "Tool did not specify a TargetData to use?");
56 /// Constructs a TargetData from a string of the following format:
57 /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8"
58 /// The above string is considered the default, and any values not specified
59 /// in the string will be assumed to be as above.
60 TargetData(const std::string &TargetDescription) {
61 init(TargetDescription);
64 /// Initialize target data from properties stored in the module.
65 TargetData(const Module *M);
67 TargetData(const TargetData &TD) :
69 LittleEndian(TD.isLittleEndian()),
70 BoolAlignment(TD.getBoolAlignment()),
71 ByteAlignment(TD.getByteAlignment()),
72 ShortAlignment(TD.getShortAlignment()),
73 IntAlignment(TD.getIntAlignment()),
74 LongAlignment(TD.getLongAlignment()),
75 FloatAlignment(TD.getFloatAlignment()),
76 DoubleAlignment(TD.getDoubleAlignment()),
77 PointerSize(TD.getPointerSize()),
78 PointerAlignment(TD.getPointerAlignment()) {
81 ~TargetData(); // Not virtual, do not subclass this class
83 /// init - Specify configuration if not available at ctor time.
85 void init(const std::string &TargetDescription);
88 /// Target endianness...
89 bool isLittleEndian() const { return LittleEndian; }
90 bool isBigEndian() const { return !LittleEndian; }
92 /// Target alignment constraints
93 unsigned char getBoolAlignment() const { return BoolAlignment; }
94 unsigned char getByteAlignment() const { return ByteAlignment; }
95 unsigned char getShortAlignment() const { return ShortAlignment; }
96 unsigned char getIntAlignment() const { return IntAlignment; }
97 unsigned char getLongAlignment() const { return LongAlignment; }
98 unsigned char getFloatAlignment() const { return FloatAlignment; }
99 unsigned char getDoubleAlignment() const { return DoubleAlignment; }
100 unsigned char getPointerAlignment() const { return PointerAlignment; }
101 unsigned char getPointerSize() const { return PointerSize; }
102 unsigned char getPointerSizeInBits() const { return 8*PointerSize; }
104 /// getStringRepresentation - Return the string representation of the
105 /// TargetData. This representation is in the same format accepted by the
106 /// string constructor above.
107 std::string getStringRepresentation() const;
109 /// getTypeSize - Return the number of bytes necessary to hold the specified
112 uint64_t getTypeSize(const Type *Ty) const;
114 /// getTypeAlignment - Return the minimum required alignment for the specified
117 unsigned char getTypeAlignment(const Type *Ty) const;
119 /// getTypeAlignmentShift - Return the minimum required alignment for the
120 /// specified type, returned as log2 of the value (a shift amount).
122 unsigned char getTypeAlignmentShift(const Type *Ty) const;
124 /// getIntPtrType - Return an unsigned integer type that is the same size or
125 /// greater to the host pointer size.
127 const Type *getIntPtrType() const;
129 /// getIndexOffset - return the offset from the beginning of the type for the
130 /// specified indices. This is used to implement getelementptr.
132 uint64_t getIndexedOffset(const Type *Ty,
133 const std::vector<Value*> &Indices) const;
135 /// getStructLayout - Return a StructLayout object, indicating the alignment
136 /// of the struct, its size, and the offsets of its fields. Note that this
137 /// information is lazily cached.
138 const StructLayout *getStructLayout(const StructType *Ty) const;
140 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
141 /// objects. If a TargetData object is alive when types are being refined and
142 /// removed, this method must be called whenever a StructType is removed to
143 /// avoid a dangling pointer in this cache.
144 void InvalidateStructLayoutInfo(const StructType *Ty) const;
147 /// StructLayout - used to lazily calculate structure layout information for a
148 /// target machine, based on the TargetData structure.
152 std::vector<uint64_t> MemberOffsets;
154 unsigned StructAlignment;
156 /// getElementContainingOffset - Given a valid offset into the structure,
157 /// return the structure index that contains it.
159 unsigned getElementContainingOffset(uint64_t Offset) const;
162 friend class TargetData; // Only TargetData can create this class
163 StructLayout(const StructType *ST, const TargetData &TD);
166 } // End llvm namespace