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 /// Constructs a TargetData from a string of the following format:
49 /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8"
50 /// The above string is considered the default, and any values not specified
51 /// in the string will be assumed to be as above.
52 TargetData(const std::string &TargetName = "",
53 const std::string &TargetDescription = "") {
54 assert(!TargetName.empty() &&
55 "ERROR: Tool did not specify a target data to use!");
56 init(TargetDescription);
60 TargetData (const TargetData &TD) :
62 LittleEndian(TD.isLittleEndian()),
63 BoolAlignment(TD.getBoolAlignment()),
64 ByteAlignment(TD.getByteAlignment()),
65 ShortAlignment(TD.getShortAlignment()),
66 IntAlignment(TD.getIntAlignment()),
67 LongAlignment(TD.getLongAlignment()),
68 FloatAlignment(TD.getFloatAlignment()),
69 DoubleAlignment(TD.getDoubleAlignment()),
70 PointerSize(TD.getPointerSize()),
71 PointerAlignment(TD.getPointerAlignment()) {
74 TargetData(const std::string &ToolName, const Module *M);
75 ~TargetData(); // Not virtual, do not subclass this class
77 /// init - Specify configuration if not available at ctor time.
79 void init(const std::string &TargetDescription);
82 /// Target endianness...
83 bool isLittleEndian() const { return LittleEndian; }
84 bool isBigEndian() const { return !LittleEndian; }
86 /// Target alignment constraints
87 unsigned char getBoolAlignment() const { return BoolAlignment; }
88 unsigned char getByteAlignment() const { return ByteAlignment; }
89 unsigned char getShortAlignment() const { return ShortAlignment; }
90 unsigned char getIntAlignment() const { return IntAlignment; }
91 unsigned char getLongAlignment() const { return LongAlignment; }
92 unsigned char getFloatAlignment() const { return FloatAlignment; }
93 unsigned char getDoubleAlignment() const { return DoubleAlignment; }
94 unsigned char getPointerAlignment() const { return PointerAlignment; }
95 unsigned char getPointerSize() const { return PointerSize; }
96 unsigned char getPointerSizeInBits() const { return 8*PointerSize; }
98 /// getStringRepresentation - Return the string representation of the
99 /// TargetData. This representation is in the same format accepted by the
100 /// string constructor above.
101 std::string getStringRepresentation() const;
103 /// getTypeSize - Return the number of bytes necessary to hold the specified
106 uint64_t getTypeSize(const Type *Ty) const;
108 /// getTypeAlignment - Return the minimum required alignment for the specified
111 unsigned char getTypeAlignment(const Type *Ty) const;
113 /// getTypeAlignmentShift - Return the minimum required alignment for the
114 /// specified type, returned as log2 of the value (a shift amount).
116 unsigned char getTypeAlignmentShift(const Type *Ty) const;
118 /// getIntPtrType - Return an unsigned integer type that is the same size or
119 /// greater to the host pointer size.
121 const Type *getIntPtrType() const;
123 /// getIndexOffset - return the offset from the beginning of the type for the
124 /// specified indices. This is used to implement getelementptr.
126 uint64_t getIndexedOffset(const Type *Ty,
127 const std::vector<Value*> &Indices) const;
129 /// getStructLayout - Return a StructLayout object, indicating the alignment
130 /// of the struct, its size, and the offsets of its fields. Note that this
131 /// information is lazily cached.
132 const StructLayout *getStructLayout(const StructType *Ty) const;
134 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
135 /// objects. If a TargetData object is alive when types are being refined and
136 /// removed, this method must be called whenever a StructType is removed to
137 /// avoid a dangling pointer in this cache.
138 void InvalidateStructLayoutInfo(const StructType *Ty) const;
141 /// StructLayout - used to lazily calculate structure layout information for a
142 /// target machine, based on the TargetData structure.
146 std::vector<uint64_t> MemberOffsets;
148 unsigned StructAlignment;
150 /// getElementContainingOffset - Given a valid offset into the structure,
151 /// return the structure index that contains it.
153 unsigned getElementContainingOffset(uint64_t Offset) const;
156 friend class TargetData; // Only TargetData can create this class
157 StructLayout(const StructType *ST, const TargetData &TD);
160 } // End llvm namespace