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