Add a new constructor to TargetData that builds a TargetData from its
[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   TargetData(const std::string &TargetName = "",
49              bool LittleEndian = false,
50              unsigned char PtrSize = 8,
51              unsigned char PtrAl   = 8, unsigned char DoubleAl = 8,
52              unsigned char FloatAl = 4, unsigned char LongAl   = 8,
53              unsigned char IntAl   = 4, unsigned char ShortAl  = 2,
54              unsigned char ByteAl  = 1, unsigned char BoolAl   = 1);
55
56   /// Constructs a TargetData from a string of the following format:
57   /// "E-p:64:64-d:64:64-f:32:32-l:64:64-i:32:32-s:16:16-b:8:8-B:8: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 &TargetName,
61              const std::string &TargetDescription);
62   
63   // Copy constructor
64   TargetData (const TargetData &TD) :
65     ImmutablePass(),
66     LittleEndian(TD.isLittleEndian()),
67     BoolAlignment(TD.getBoolAlignment()),
68     ByteAlignment(TD.getByteAlignment()),
69     ShortAlignment(TD.getShortAlignment()),
70     IntAlignment(TD.getIntAlignment()),
71     LongAlignment(TD.getLongAlignment()),
72     FloatAlignment(TD.getFloatAlignment()),
73     DoubleAlignment(TD.getDoubleAlignment()),
74     PointerSize(TD.getPointerSize()),
75     PointerAlignment(TD.getPointerAlignment()) {
76   }
77
78   TargetData(const std::string &ToolName, const Module *M);
79   ~TargetData();  // Not virtual, do not subclass this class
80
81   /// Target endianness...
82   bool          isLittleEndian()       const { return     LittleEndian; }
83   bool          isBigEndian()          const { return    !LittleEndian; }
84
85   /// Target alignment constraints
86   unsigned char getBoolAlignment()     const { return    BoolAlignment; }
87   unsigned char getByteAlignment()     const { return    ByteAlignment; }
88   unsigned char getShortAlignment()    const { return   ShortAlignment; }
89   unsigned char getIntAlignment()      const { return     IntAlignment; }
90   unsigned char getLongAlignment()     const { return    LongAlignment; }
91   unsigned char getFloatAlignment()    const { return   FloatAlignment; }
92   unsigned char getDoubleAlignment()   const { return  DoubleAlignment; }
93   unsigned char getPointerAlignment()  const { return PointerAlignment; }
94   unsigned char getPointerSize()       const { return      PointerSize; }
95   unsigned char getPointerSizeInBits() const { return    8*PointerSize; }
96
97   /// getTypeSize - Return the number of bytes necessary to hold the specified
98   /// type.
99   ///
100   uint64_t getTypeSize(const Type *Ty) const;
101
102   /// getTypeAlignment - Return the minimum required alignment for the specified
103   /// type.
104   ///
105   unsigned char getTypeAlignment(const Type *Ty) const;
106
107   /// getTypeAlignmentShift - Return the minimum required alignment for the
108   /// specified type, returned as log2 of the value (a shift amount).
109   ///
110   unsigned char getTypeAlignmentShift(const Type *Ty) const;
111
112   /// getIntPtrType - Return an unsigned integer type that is the same size or
113   /// greater to the host pointer size.
114   ///
115   const Type *getIntPtrType() const;
116
117   /// getIndexOffset - return the offset from the beginning of the type for the
118   /// specified indices.  This is used to implement getelementptr.
119   ///
120   uint64_t getIndexedOffset(const Type *Ty,
121                             const std::vector<Value*> &Indices) const;
122
123   /// getStructLayout - Return a StructLayout object, indicating the alignment
124   /// of the struct, its size, and the offsets of its fields.  Note that this
125   /// information is lazily cached.
126   const StructLayout *getStructLayout(const StructType *Ty) const;
127   
128   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
129   /// objects.  If a TargetData object is alive when types are being refined and
130   /// removed, this method must be called whenever a StructType is removed to
131   /// avoid a dangling pointer in this cache.
132   void InvalidateStructLayoutInfo(const StructType *Ty) const;
133 };
134
135 /// StructLayout - used to lazily calculate structure layout information for a
136 /// target machine, based on the TargetData structure.
137 ///
138 class StructLayout {
139 public:
140   std::vector<uint64_t> MemberOffsets;
141   uint64_t StructSize;
142   unsigned StructAlignment;
143
144   /// getElementContainingOffset - Given a valid offset into the structure,
145   /// return the structure index that contains it.
146   ///
147   unsigned getElementContainingOffset(uint64_t Offset) const;
148
149 private:
150   friend class TargetData;   // Only TargetData can create this class
151   StructLayout(const StructType *ST, const TargetData &TD);
152 };
153
154 } // End llvm namespace
155
156 #endif