]> granicus.if.org Git - clang/commitdiff
add a new "getPreferredTypeAlign" method to return the preferred alignment
authorChris Lattner <sabre@nondot.org>
Tue, 27 Jan 2009 18:08:34 +0000 (18:08 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 27 Jan 2009 18:08:34 +0000 (18:08 +0000)
of a type.  The implementation is currently something of a hack, but is
sufficient for now and allows clients to be built on it.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63108 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTContext.h
lib/AST/ASTContext.cpp

index 684feb24025d539dc74b6caa6f6a95a555f21405..a01199d4b2aaf406aa9c55b8bf872a9a6035d2e5 100644 (file)
@@ -394,8 +394,8 @@ public:
     return getTypeInfo(T).first;
   }
   
-  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
-  /// method does not work on incomplete types.
+  /// getTypeAlign - Return the ABI-specified alignment of a type, in bits.
+  /// This method does not work on incomplete types.
   unsigned getTypeAlign(QualType T) {
     return getTypeInfo(T).second;
   }
@@ -403,6 +403,12 @@ public:
     return getTypeInfo(T).second;
   }
   
+  /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
+  /// type for the current target in bits.  This can be different than the ABI
+  /// alignment in cases where it is beneficial for performance to overalign
+  /// a data type.
+  unsigned getPreferredTypeAlign(const Type *T);
+  
   /// getDeclAlign - Return the alignment of the specified decl that should be
   /// returned by __alignof().  Note that bitfields do not have a valid
   /// alignment, so this method will assert on them.
index 2662860af2fef98c7379b5fa7a46e17d238ed386..e7c08e2da39ba42933c98c19a6d6d5c010eaf53f 100644 (file)
@@ -453,6 +453,22 @@ ASTContext::getTypeInfo(const Type *T) {
   return std::make_pair(Width, Align);
 }
 
+/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
+/// type for the current target in bits.  This can be different than the ABI
+/// alignment in cases where it is beneficial for performance to overalign
+/// a data type.
+unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
+  unsigned ABIAlign = getTypeAlign(T);
+  
+  // Doubles should be naturally aligned if possible.
+  if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
+    if (BT->getKind() == BuiltinType::Double)
+      return std::max(ABIAlign, 8U);
+  
+  return ABIAlign;
+}
+
+
 /// LayoutField - Field layout.
 void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
                                   bool IsUnion, unsigned StructPacking,