simplify trivial function
[oota-llvm.git] / include / llvm / Target / TargetData.h
1 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
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.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_TARGET_TARGETDATA_H
21 #define LLVM_TARGET_TARGETDATA_H
22
23 #include "llvm/Pass.h"
24 #include "llvm/Support/DataTypes.h"
25 #include <vector>
26 #include <string>
27
28 namespace llvm {
29
30 class Value;
31 class Type;
32 class StructType;
33 class StructLayout;
34
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
46
47 public:
48   /// Default ctor - This has to exist, because this is a pass, but it should
49   /// never be used.
50   TargetData() {
51     assert(0 && "ERROR: Bad TargetData ctor used.  "
52            "Tool did not specify a TargetData to use?");
53     abort();
54   }
55     
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);
62   }
63
64   /// Initialize target data from properties stored in the module.
65   TargetData(const Module *M);
66
67   TargetData(const TargetData &TD) : 
68     ImmutablePass(),
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()) {
79   }
80
81   ~TargetData();  // Not virtual, do not subclass this class
82
83   /// init - Specify configuration if not available at ctor time.
84   ///
85   void init(const std::string &TargetDescription);
86   
87   
88   /// Target endianness...
89   bool          isLittleEndian()       const { return     LittleEndian; }
90   bool          isBigEndian()          const { return    !LittleEndian; }
91
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; }
103
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;
108
109   /// getTypeSize - Return the number of bytes necessary to hold the specified
110   /// type.
111   ///
112   uint64_t getTypeSize(const Type *Ty) const;
113
114   /// getTypeAlignment - Return the minimum required alignment for the specified
115   /// type.
116   ///
117   unsigned char getTypeAlignment(const Type *Ty) const;
118
119   /// getTypeAlignmentShift - Return the minimum required alignment for the
120   /// specified type, returned as log2 of the value (a shift amount).
121   ///
122   unsigned char getTypeAlignmentShift(const Type *Ty) const;
123
124   /// getIntPtrType - Return an unsigned integer type that is the same size or
125   /// greater to the host pointer size.
126   ///
127   const Type *getIntPtrType() const;
128
129   /// getIndexOffset - return the offset from the beginning of the type for the
130   /// specified indices.  This is used to implement getelementptr.
131   ///
132   uint64_t getIndexedOffset(const Type *Ty,
133                             const std::vector<Value*> &Indices) const;
134
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;
139   
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;
145 };
146
147 /// StructLayout - used to lazily calculate structure layout information for a
148 /// target machine, based on the TargetData structure.
149 ///
150 class StructLayout {
151 public:
152   std::vector<uint64_t> MemberOffsets;
153   uint64_t StructSize;
154   unsigned StructAlignment;
155
156   /// getElementContainingOffset - Given a valid offset into the structure,
157   /// return the structure index that contains it.
158   ///
159   unsigned getElementContainingOffset(uint64_t Offset) const;
160
161 private:
162   friend class TargetData;   // Only TargetData can create this class
163   StructLayout(const StructType *ST, const TargetData &TD);
164 };
165
166 } // End llvm namespace
167
168 #endif