Convert alignment values to CharUnits. No change in functionality intended.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127848 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ken Dyck 2011-03-18 01:26:17 +00:00
Родитель 15c152e4f0
Коммит 7d2f9d54bd
1 изменённых файлов: 18 добавлений и 18 удалений

Просмотреть файл

@ -39,7 +39,7 @@ class ConstStructBuilder {
bool Packed; bool Packed;
CharUnits NextFieldOffsetInChars; CharUnits NextFieldOffsetInChars;
unsigned LLVMStructAlignment; CharUnits LLVMStructAlignment;
std::vector<llvm::Constant *> Elements; std::vector<llvm::Constant *> Elements;
public: public:
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
@ -49,7 +49,7 @@ private:
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
: CGM(CGM), CGF(CGF), Packed(false), : CGM(CGM), CGF(CGF), Packed(false),
NextFieldOffsetInChars(CharUnits::Zero()), NextFieldOffsetInChars(CharUnits::Zero()),
LLVMStructAlignment(1) { } LLVMStructAlignment(CharUnits::One()) { }
bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
llvm::Constant *InitExpr); llvm::Constant *InitExpr);
@ -65,9 +65,10 @@ private:
bool Build(InitListExpr *ILE); bool Build(InitListExpr *ILE);
unsigned getAlignment(const llvm::Constant *C) const { CharUnits getAlignment(const llvm::Constant *C) const {
if (Packed) return 1; if (Packed) return CharUnits::One();
return CGM.getTargetData().getABITypeAlignment(C->getType()); return CharUnits::fromQuantity(
CGM.getTargetData().getABITypeAlignment(C->getType()));
} }
CharUnits getSizeInChars(const llvm::Constant *C) const { CharUnits getSizeInChars(const llvm::Constant *C) const {
@ -87,12 +88,11 @@ AppendField(const FieldDecl *Field, uint64_t FieldOffset,
assert(NextFieldOffsetInChars <= FieldOffsetInChars assert(NextFieldOffsetInChars <= FieldOffsetInChars
&& "Field offset mismatch!"); && "Field offset mismatch!");
unsigned FieldAlignment = getAlignment(InitCst); CharUnits FieldAlignment = getAlignment(InitCst);
// Round up the field offset to the alignment of the field type. // Round up the field offset to the alignment of the field type.
CharUnits AlignedNextFieldOffsetInChars = CharUnits AlignedNextFieldOffsetInChars =
NextFieldOffsetInChars.RoundUpToAlignment( NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
CharUnits::fromQuantity(FieldAlignment));
if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) { if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
assert(!Packed && "Alignment is wrong even with a packed struct!"); assert(!Packed && "Alignment is wrong even with a packed struct!");
@ -120,7 +120,8 @@ AppendField(const FieldDecl *Field, uint64_t FieldOffset,
getSizeInChars(InitCst); getSizeInChars(InitCst);
if (Packed) if (Packed)
assert(LLVMStructAlignment == 1 && "Packed struct not byte-aligned!"); assert(LLVMStructAlignment == CharUnits::One() &&
"Packed struct not byte-aligned!");
else else
LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
@ -284,7 +285,8 @@ void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
llvm::Constant *C = llvm::UndefValue::get(Ty); llvm::Constant *C = llvm::UndefValue::get(Ty);
Elements.push_back(C); Elements.push_back(C);
assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!"); assert(getAlignment(C) == CharUnits::One() &&
"Padding must have 1 byte alignment!");
NextFieldOffsetInChars += getSizeInChars(C); NextFieldOffsetInChars += getSizeInChars(C);
} }
@ -303,11 +305,10 @@ void ConstStructBuilder::ConvertStructToPacked() {
for (unsigned i = 0, e = Elements.size(); i != e; ++i) { for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
llvm::Constant *C = Elements[i]; llvm::Constant *C = Elements[i];
unsigned ElementAlign = CharUnits ElementAlign = CharUnits::fromQuantity(
CGM.getTargetData().getABITypeAlignment(C->getType()); CGM.getTargetData().getABITypeAlignment(C->getType()));
CharUnits AlignedElementOffsetInChars = CharUnits AlignedElementOffsetInChars =
ElementOffsetInChars.RoundUpToAlignment( ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
CharUnits::fromQuantity(ElementAlign));
if (AlignedElementOffsetInChars > ElementOffsetInChars) { if (AlignedElementOffsetInChars > ElementOffsetInChars) {
// We need some padding. // We need some padding.
@ -331,7 +332,7 @@ void ConstStructBuilder::ConvertStructToPacked() {
"Packing the struct changed its size!"); "Packing the struct changed its size!");
Elements = PackedElements; Elements = PackedElements;
LLVMStructAlignment = 1; LLVMStructAlignment = CharUnits::One();
Packed = true; Packed = true;
} }
@ -388,8 +389,7 @@ bool ConstStructBuilder::Build(InitListExpr *ILE) {
} }
CharUnits LLVMSizeInChars = CharUnits LLVMSizeInChars =
NextFieldOffsetInChars.RoundUpToAlignment( NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
CharUnits::fromQuantity(LLVMStructAlignment));
// Check if we need to convert the struct to a packed struct. // Check if we need to convert the struct to a packed struct.
if (NextFieldOffsetInChars <= LayoutSizeInChars && if (NextFieldOffsetInChars <= LayoutSizeInChars &&
@ -422,7 +422,7 @@ llvm::Constant *ConstStructBuilder::
Builder.Elements, Builder.Packed); Builder.Elements, Builder.Packed);
assert(Builder.NextFieldOffsetInChars.RoundUpToAlignment( assert(Builder.NextFieldOffsetInChars.RoundUpToAlignment(
CharUnits::fromQuantity(Builder.getAlignment(Result))) == Builder.getAlignment(Result)) ==
Builder.getSizeInChars(Result) && "Size mismatch!"); Builder.getSizeInChars(Result) && "Size mismatch!");
return Result; return Result;