LLVM API Documentation

TargetData.h

Go to the documentation of this file.
00001 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines target properties related to datatype size/offset/alignment
00011 // information.  It uses lazy annotations to cache information about how
00012 // structure types are laid out and used.
00013 //
00014 // This structure should be created once, filled in if the defaults are not
00015 // correct and then passed around by const&.  None of the members functions
00016 // require modification to the object.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_TARGET_TARGETDATA_H
00021 #define LLVM_TARGET_TARGETDATA_H
00022 
00023 #include "llvm/Pass.h"
00024 #include "llvm/Support/DataTypes.h"
00025 #include <vector>
00026 #include <string>
00027 
00028 namespace llvm {
00029 
00030 class Value;
00031 class Type;
00032 class StructType;
00033 class StructLayout;
00034 
00035 class TargetData : public ImmutablePass {
00036   bool          LittleEndian;          // Defaults to false
00037   unsigned char BoolAlignment;         // Defaults to 1 byte
00038   unsigned char ByteAlignment;         // Defaults to 1 byte
00039   unsigned char ShortAlignment;        // Defaults to 2 bytes
00040   unsigned char IntAlignment;          // Defaults to 4 bytes
00041   unsigned char LongAlignment;         // Defaults to 8 bytes
00042   unsigned char FloatAlignment;        // Defaults to 4 bytes
00043   unsigned char DoubleAlignment;       // Defaults to 8 bytes
00044   unsigned char PointerSize;           // Defaults to 8 bytes
00045   unsigned char PointerAlignment;      // Defaults to 8 bytes
00046 
00047 public:
00048   TargetData(const std::string &TargetName = "",
00049              bool LittleEndian = false,
00050              unsigned char PtrSize = 8,
00051              unsigned char PtrAl   = 8, unsigned char DoubleAl = 8,
00052              unsigned char FloatAl = 4, unsigned char LongAl   = 8,
00053              unsigned char IntAl   = 4, unsigned char ShortAl  = 2,
00054              unsigned char ByteAl  = 1, unsigned char BoolAl   = 1);
00055 
00056   // Copy constructor
00057   TargetData (const TargetData &TD) :
00058     ImmutablePass(),
00059     LittleEndian(TD.isLittleEndian()),
00060     BoolAlignment(TD.getBoolAlignment()),
00061     ByteAlignment(TD.getByteAlignment()),
00062     ShortAlignment(TD.getShortAlignment()),
00063     IntAlignment(TD.getIntAlignment()),
00064     LongAlignment(TD.getLongAlignment()),
00065     FloatAlignment(TD.getFloatAlignment()),
00066     DoubleAlignment(TD.getDoubleAlignment()),
00067     PointerSize(TD.getPointerSize()),
00068     PointerAlignment(TD.getPointerAlignment()) {
00069   }
00070 
00071   TargetData(const std::string &ToolName, const Module *M);
00072   ~TargetData();  // Not virtual, do not subclass this class
00073 
00074   /// Target endianness...
00075   bool          isLittleEndian()       const { return     LittleEndian; }
00076   bool          isBigEndian()          const { return    !LittleEndian; }
00077 
00078   /// Target alignment constraints
00079   unsigned char getBoolAlignment()     const { return    BoolAlignment; }
00080   unsigned char getByteAlignment()     const { return    ByteAlignment; }
00081   unsigned char getShortAlignment()    const { return   ShortAlignment; }
00082   unsigned char getIntAlignment()      const { return     IntAlignment; }
00083   unsigned char getLongAlignment()     const { return    LongAlignment; }
00084   unsigned char getFloatAlignment()    const { return   FloatAlignment; }
00085   unsigned char getDoubleAlignment()   const { return  DoubleAlignment; }
00086   unsigned char getPointerAlignment()  const { return PointerAlignment; }
00087   unsigned char getPointerSize()       const { return      PointerSize; }
00088   unsigned char getPointerSizeInBits() const { return    8*PointerSize; }
00089 
00090   /// getTypeSize - Return the number of bytes necessary to hold the specified
00091   /// type.
00092   ///
00093   uint64_t getTypeSize(const Type *Ty) const;
00094 
00095   /// getTypeAlignment - Return the minimum required alignment for the specified
00096   /// type.
00097   ///
00098   unsigned char getTypeAlignment(const Type *Ty) const;
00099 
00100   /// getTypeAlignmentShift - Return the minimum required alignment for the
00101   /// specified type, returned as log2 of the value (a shift amount).
00102   ///
00103   unsigned char getTypeAlignmentShift(const Type *Ty) const;
00104 
00105   /// getIntPtrType - Return an unsigned integer type that is the same size or
00106   /// greater to the host pointer size.
00107   ///
00108   const Type *getIntPtrType() const;
00109 
00110   /// getIndexOffset - return the offset from the beginning of the type for the
00111   /// specified indices.  This is used to implement getelementptr.
00112   ///
00113   uint64_t getIndexedOffset(const Type *Ty,
00114                             const std::vector<Value*> &Indices) const;
00115 
00116   /// getStructLayout - Return a StructLayout object, indicating the alignment
00117   /// of the struct, its size, and the offsets of its fields.  Note that this
00118   /// information is lazily cached.
00119   const StructLayout *getStructLayout(const StructType *Ty) const;
00120   
00121   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
00122   /// objects.  If a TargetData object is alive when types are being refined and
00123   /// removed, this method must be called whenever a StructType is removed to
00124   /// avoid a dangling pointer in this cache.
00125   void InvalidateStructLayoutInfo(const StructType *Ty) const;
00126 };
00127 
00128 /// StructLayout - used to lazily calculate structure layout information for a
00129 /// target machine, based on the TargetData structure.
00130 ///
00131 class StructLayout {
00132 public:
00133   std::vector<uint64_t> MemberOffsets;
00134   uint64_t StructSize;
00135   unsigned StructAlignment;
00136 
00137   /// getElementContainingOffset - Given a valid offset into the structure,
00138   /// return the structure index that contains it.
00139   ///
00140   unsigned getElementContainingOffset(uint64_t Offset) const;
00141 
00142 private:
00143   friend class TargetData;   // Only TargetData can create this class
00144   StructLayout(const StructType *ST, const TargetData &TD);
00145 };
00146 
00147 } // End llvm namespace
00148 
00149 #endif