Move getPreferredAlignmentLog from AsmPrinter to TargetData
[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 class GlobalVariable;
35
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
47
48 public:
49   /// Default ctor - This has to exist, because this is a pass, but it should
50   /// never be used.
51   TargetData() {
52     assert(0 && "ERROR: Bad TargetData ctor used.  "
53            "Tool did not specify a TargetData to use?");
54     abort();
55   }
56     
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);
63   }
64
65   /// Initialize target data from properties stored in the module.
66   TargetData(const Module *M);
67
68   TargetData(const TargetData &TD) : 
69     ImmutablePass(),
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()) {
80   }
81
82   ~TargetData();  // Not virtual, do not subclass this class
83
84   /// init - Specify configuration if not available at ctor time.
85   ///
86   void init(const std::string &TargetDescription);
87   
88   
89   /// Target endianness...
90   bool          isLittleEndian()       const { return     LittleEndian; }
91   bool          isBigEndian()          const { return    !LittleEndian; }
92
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; }
104
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;
109
110   /// getTypeSize - Return the number of bytes necessary to hold the specified
111   /// type.
112   ///
113   uint64_t getTypeSize(const Type *Ty) const;
114
115   /// getTypeAlignment - Return the minimum required alignment for the specified
116   /// type.
117   ///
118   unsigned char getTypeAlignment(const Type *Ty) const;
119
120   /// getTypeAlignmentShift - Return the minimum required alignment for the
121   /// specified type, returned as log2 of the value (a shift amount).
122   ///
123   unsigned char getTypeAlignmentShift(const Type *Ty) const;
124
125   /// getIntPtrType - Return an unsigned integer type that is the same size or
126   /// greater to the host pointer size.
127   ///
128   const Type *getIntPtrType() const;
129
130   /// getIndexOffset - return the offset from the beginning of the type for the
131   /// specified indices.  This is used to implement getelementptr.
132   ///
133   uint64_t getIndexedOffset(const Type *Ty,
134                             const std::vector<Value*> &Indices) const;
135
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;
140   
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;
146
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   virtual unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
151 };
152
153 /// StructLayout - used to lazily calculate structure layout information for a
154 /// target machine, based on the TargetData structure.
155 ///
156 class StructLayout {
157 public:
158   std::vector<uint64_t> MemberOffsets;
159   uint64_t StructSize;
160   unsigned StructAlignment;
161
162   /// getElementContainingOffset - Given a valid offset into the structure,
163   /// return the structure index that contains it.
164   ///
165   unsigned getElementContainingOffset(uint64_t Offset) const;
166
167 private:
168   friend class TargetData;   // Only TargetData can create this class
169   StructLayout(const StructType *ST, const TargetData &TD);
170 };
171
172 } // End llvm namespace
173
174 #endif