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"
36 class TargetData : public ImmutablePass {
37 bool LittleEndian; // Defaults to false
38 unsigned char BoolAlignment; // Defaults to 1 byte
39 unsigned char ByteAlignment; // Defaults to 1 byte
40 unsigned char ShortAlignment; // Defaults to 2 bytes
41 unsigned char IntAlignment; // Defaults to 4 bytes
42 unsigned char LongAlignment; // Defaults to 8 bytes
43 unsigned char FloatAlignment; // Defaults to 4 bytes
44 unsigned char DoubleAlignment; // Defaults to 8 bytes
45 unsigned char PointerSize; // Defaults to 8 bytes
46 unsigned char PointerAlignment; // Defaults to 8 bytes
49 /// Default ctor - This has to exist, because this is a pass, but it should
52 assert(0 && "ERROR: Bad TargetData ctor used. "
53 "Tool did not specify a TargetData to use?");
57 /// Constructs a TargetData from a string of the following format:
58 /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8"
59 /// The above string is considered the default, and any values not specified
60 /// in the string will be assumed to be as above.
61 TargetData(const std::string &TargetDescription) {
62 init(TargetDescription);
65 /// Initialize target data from properties stored in the module.
66 TargetData(const Module *M);
68 TargetData(const TargetData &TD) :
70 LittleEndian(TD.isLittleEndian()),
71 BoolAlignment(TD.getBoolAlignment()),
72 ByteAlignment(TD.getByteAlignment()),
73 ShortAlignment(TD.getShortAlignment()),
74 IntAlignment(TD.getIntAlignment()),
75 LongAlignment(TD.getLongAlignment()),
76 FloatAlignment(TD.getFloatAlignment()),
77 DoubleAlignment(TD.getDoubleAlignment()),
78 PointerSize(TD.getPointerSize()),
79 PointerAlignment(TD.getPointerAlignment()) {
82 ~TargetData(); // Not virtual, do not subclass this class
84 /// init - Specify configuration if not available at ctor time.
86 void init(const std::string &TargetDescription);
89 /// Target endianness...
90 bool isLittleEndian() const { return LittleEndian; }
91 bool isBigEndian() const { return !LittleEndian; }
93 /// Target alignment constraints
94 unsigned char getBoolAlignment() const { return BoolAlignment; }
95 unsigned char getByteAlignment() const { return ByteAlignment; }
96 unsigned char getShortAlignment() const { return ShortAlignment; }
97 unsigned char getIntAlignment() const { return IntAlignment; }
98 unsigned char getLongAlignment() const { return LongAlignment; }
99 unsigned char getFloatAlignment() const { return FloatAlignment; }
100 unsigned char getDoubleAlignment() const { return DoubleAlignment; }
101 unsigned char getPointerAlignment() const { return PointerAlignment; }
102 unsigned char getPointerSize() const { return PointerSize; }
103 unsigned char getPointerSizeInBits() const { return 8*PointerSize; }
105 /// getStringRepresentation - Return the string representation of the
106 /// TargetData. This representation is in the same format accepted by the
107 /// string constructor above.
108 std::string getStringRepresentation() const;
110 /// getTypeSize - Return the number of bytes necessary to hold the specified
113 uint64_t getTypeSize(const Type *Ty) const;
115 /// getTypeAlignment - Return the minimum required alignment for the specified
118 unsigned char getTypeAlignment(const Type *Ty) const;
120 /// getTypeAlignmentShift - Return the minimum required alignment for the
121 /// specified type, returned as log2 of the value (a shift amount).
123 unsigned char getTypeAlignmentShift(const Type *Ty) const;
125 /// getIntPtrType - Return an unsigned integer type that is the same size or
126 /// greater to the host pointer size.
128 const Type *getIntPtrType() const;
130 /// getIndexOffset - return the offset from the beginning of the type for the
131 /// specified indices. This is used to implement getelementptr.
133 uint64_t getIndexedOffset(const Type *Ty,
134 const std::vector<Value*> &Indices) const;
136 /// getStructLayout - Return a StructLayout object, indicating the alignment
137 /// of the struct, its size, and the offsets of its fields. Note that this
138 /// information is lazily cached.
139 const StructLayout *getStructLayout(const StructType *Ty) const;
141 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
142 /// objects. If a TargetData object is alive when types are being refined and
143 /// removed, this method must be called whenever a StructType is removed to
144 /// avoid a dangling pointer in this cache.
145 void InvalidateStructLayoutInfo(const StructType *Ty) const;
147 /// getPreferredAlignmentLog - Return the preferred alignment of the
148 /// specified global, returned in log form. This includes an explicitly
149 /// requested alignment (if the global has one).
150 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
153 /// StructLayout - used to lazily calculate structure layout information for a
154 /// target machine, based on the TargetData structure.
158 std::vector<uint64_t> MemberOffsets;
160 unsigned StructAlignment;
162 /// getElementContainingOffset - Given a valid offset into the structure,
163 /// return the structure index that contains it.
165 unsigned getElementContainingOffset(uint64_t Offset) const;
168 friend class TargetData; // Only TargetData can create this class
169 StructLayout(const StructType *ST, const TargetData &TD);
172 } // End llvm namespace