clang API Documentation
00001 //===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H 00011 #define LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H 00012 00013 #include "clang/AST/CharUnits.h" 00014 #include "clang/AST/Decl.h" 00015 #include "clang/Basic/LLVM.h" 00016 #include "llvm/ADT/DenseMap.h" 00017 #include "llvm/IR/DerivedTypes.h" 00018 00019 namespace llvm { 00020 class StructType; 00021 } 00022 00023 namespace clang { 00024 namespace CodeGen { 00025 00026 /// \brief Structure with information about how a bitfield should be accessed. 00027 /// 00028 /// Often we layout a sequence of bitfields as a contiguous sequence of bits. 00029 /// When the AST record layout does this, we represent it in the LLVM IR's type 00030 /// as either a sequence of i8 members or a byte array to reserve the number of 00031 /// bytes touched without forcing any particular alignment beyond the basic 00032 /// character alignment. 00033 /// 00034 /// Then accessing a particular bitfield involves converting this byte array 00035 /// into a single integer of that size (i24 or i40 -- may not be power-of-two 00036 /// size), loading it, and shifting and masking to extract the particular 00037 /// subsequence of bits which make up that particular bitfield. This structure 00038 /// encodes the information used to construct the extraction code sequences. 00039 /// The CGRecordLayout also has a field index which encodes which byte-sequence 00040 /// this bitfield falls within. Let's assume the following C struct: 00041 /// 00042 /// struct S { 00043 /// char a, b, c; 00044 /// unsigned bits : 3; 00045 /// unsigned more_bits : 4; 00046 /// unsigned still_more_bits : 7; 00047 /// }; 00048 /// 00049 /// This will end up as the following LLVM type. The first array is the 00050 /// bitfield, and the second is the padding out to a 4-byte alignmnet. 00051 /// 00052 /// %t = type { i8, i8, i8, i8, i8, [3 x i8] } 00053 /// 00054 /// When generating code to access more_bits, we'll generate something 00055 /// essentially like this: 00056 /// 00057 /// define i32 @foo(%t* %base) { 00058 /// %0 = gep %t* %base, i32 0, i32 3 00059 /// %2 = load i8* %1 00060 /// %3 = lshr i8 %2, 3 00061 /// %4 = and i8 %3, 15 00062 /// %5 = zext i8 %4 to i32 00063 /// ret i32 %i 00064 /// } 00065 /// 00066 struct CGBitFieldInfo { 00067 /// The offset within a contiguous run of bitfields that are represented as 00068 /// a single "field" within the LLVM struct type. This offset is in bits. 00069 unsigned Offset : 16; 00070 00071 /// The total size of the bit-field, in bits. 00072 unsigned Size : 15; 00073 00074 /// Whether the bit-field is signed. 00075 unsigned IsSigned : 1; 00076 00077 /// The storage size in bits which should be used when accessing this 00078 /// bitfield. 00079 unsigned StorageSize; 00080 00081 /// The alignment which should be used when accessing the bitfield. 00082 unsigned StorageAlignment; 00083 00084 CGBitFieldInfo() 00085 : Offset(), Size(), IsSigned(), StorageSize(), StorageAlignment() {} 00086 00087 CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned, 00088 unsigned StorageSize, unsigned StorageAlignment) 00089 : Offset(Offset), Size(Size), IsSigned(IsSigned), 00090 StorageSize(StorageSize), StorageAlignment(StorageAlignment) {} 00091 00092 void print(raw_ostream &OS) const; 00093 void dump() const; 00094 00095 /// \brief Given a bit-field decl, build an appropriate helper object for 00096 /// accessing that field (which is expected to have the given offset and 00097 /// size). 00098 static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, 00099 const FieldDecl *FD, 00100 uint64_t Offset, uint64_t Size, 00101 uint64_t StorageSize, 00102 uint64_t StorageAlignment); 00103 }; 00104 00105 /// CGRecordLayout - This class handles struct and union layout info while 00106 /// lowering AST types to LLVM types. 00107 /// 00108 /// These layout objects are only created on demand as IR generation requires. 00109 class CGRecordLayout { 00110 friend class CodeGenTypes; 00111 00112 CGRecordLayout(const CGRecordLayout &) LLVM_DELETED_FUNCTION; 00113 void operator=(const CGRecordLayout &) LLVM_DELETED_FUNCTION; 00114 00115 private: 00116 /// The LLVM type corresponding to this record layout; used when 00117 /// laying it out as a complete object. 00118 llvm::StructType *CompleteObjectType; 00119 00120 /// The LLVM type for the non-virtual part of this record layout; 00121 /// used when laying it out as a base subobject. 00122 llvm::StructType *BaseSubobjectType; 00123 00124 /// Map from (non-bit-field) struct field to the corresponding llvm struct 00125 /// type field no. This info is populated by record builder. 00126 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; 00127 00128 /// Map from (bit-field) struct field to the corresponding llvm struct type 00129 /// field no. This info is populated by record builder. 00130 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; 00131 00132 // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single 00133 // map for both virtual and non-virtual bases. 00134 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; 00135 00136 /// Map from virtual bases to their field index in the complete object. 00137 llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases; 00138 00139 /// False if any direct or indirect subobject of this class, when 00140 /// considered as a complete object, requires a non-zero bitpattern 00141 /// when zero-initialized. 00142 bool IsZeroInitializable : 1; 00143 00144 /// False if any direct or indirect subobject of this class, when 00145 /// considered as a base subobject, requires a non-zero bitpattern 00146 /// when zero-initialized. 00147 bool IsZeroInitializableAsBase : 1; 00148 00149 public: 00150 CGRecordLayout(llvm::StructType *CompleteObjectType, 00151 llvm::StructType *BaseSubobjectType, 00152 bool IsZeroInitializable, 00153 bool IsZeroInitializableAsBase) 00154 : CompleteObjectType(CompleteObjectType), 00155 BaseSubobjectType(BaseSubobjectType), 00156 IsZeroInitializable(IsZeroInitializable), 00157 IsZeroInitializableAsBase(IsZeroInitializableAsBase) {} 00158 00159 /// \brief Return the "complete object" LLVM type associated with 00160 /// this record. 00161 llvm::StructType *getLLVMType() const { 00162 return CompleteObjectType; 00163 } 00164 00165 /// \brief Return the "base subobject" LLVM type associated with 00166 /// this record. 00167 llvm::StructType *getBaseSubobjectLLVMType() const { 00168 return BaseSubobjectType; 00169 } 00170 00171 /// \brief Check whether this struct can be C++ zero-initialized 00172 /// with a zeroinitializer. 00173 bool isZeroInitializable() const { 00174 return IsZeroInitializable; 00175 } 00176 00177 /// \brief Check whether this struct can be C++ zero-initialized 00178 /// with a zeroinitializer when considered as a base subobject. 00179 bool isZeroInitializableAsBase() const { 00180 return IsZeroInitializableAsBase; 00181 } 00182 00183 /// \brief Return llvm::StructType element number that corresponds to the 00184 /// field FD. 00185 unsigned getLLVMFieldNo(const FieldDecl *FD) const { 00186 FD = FD->getCanonicalDecl(); 00187 assert(FieldInfo.count(FD) && "Invalid field for record!"); 00188 return FieldInfo.lookup(FD); 00189 } 00190 00191 unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const { 00192 assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!"); 00193 return NonVirtualBases.lookup(RD); 00194 } 00195 00196 /// \brief Return the LLVM field index corresponding to the given 00197 /// virtual base. Only valid when operating on the complete object. 00198 unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const { 00199 assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!"); 00200 return CompleteObjectVirtualBases.lookup(base); 00201 } 00202 00203 /// \brief Return the BitFieldInfo that corresponds to the field FD. 00204 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { 00205 FD = FD->getCanonicalDecl(); 00206 assert(FD->isBitField() && "Invalid call for non-bit-field decl!"); 00207 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator 00208 it = BitFields.find(FD); 00209 assert(it != BitFields.end() && "Unable to find bitfield info"); 00210 return it->second; 00211 } 00212 00213 void print(raw_ostream &OS) const; 00214 void dump() const; 00215 }; 00216 00217 } // end namespace CodeGen 00218 } // end namespace clang 00219 00220 #endif