[cfe-commits] r76854 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp test/CodeGen/cast-to-union.c
Anders Carlsson
andersca at mac.com
Wed Jul 22 21:50:02 PDT 2009
Author: andersca
Date: Wed Jul 22 23:50:01 2009
New Revision: 76854
URL: http://llvm.org/viewvc/llvm-project?rev=76854&view=rev
Log:
Use arrays as union padding. Also, since the resulting struct will always contain a single element and either a single i8 element or an array of i8s, there's no reason to use a packed struct.
Modified:
cfe/trunk/lib/CodeGen/CGExprConstant.cpp
cfe/trunk/test/CodeGen/cast-to-union.c
Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=76854&r1=76853&r2=76854&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Wed Jul 22 23:50:01 2009
@@ -243,15 +243,18 @@
Types.push_back(C->getType());
unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
- while (CurSize < TotalSize) {
- Elts.push_back(VMContext.getNullValue(llvm::Type::Int8Ty));
- Types.push_back(llvm::Type::Int8Ty);
- CurSize++;
+
+ assert(CurSize <= TotalSize && "Union size mismatch!");
+ if (unsigned NumPadBytes = TotalSize - CurSize) {
+ const llvm::Type *Ty = llvm::Type::Int8Ty;
+ if (NumPadBytes > 1)
+ Ty = VMContext.getArrayType(Ty, NumPadBytes);
+
+ Elts.push_back(VMContext.getNullValue(Ty));
+ Types.push_back(Ty);
}
- // This always generates a packed struct
- // FIXME: Try to generate an unpacked struct when we can
- llvm::StructType* STy = VMContext.getStructType(Types, true);
+ llvm::StructType* STy = VMContext.getStructType(Types, false);
return VMContext.getConstantStruct(STy, Elts);
}
Modified: cfe/trunk/test/CodeGen/cast-to-union.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/cast-to-union.c?rev=76854&r1=76853&r2=76854&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/cast-to-union.c (original)
+++ cfe/trunk/test/CodeGen/cast-to-union.c Wed Jul 22 23:50:01 2009
@@ -1,7 +1,7 @@
// RUN: clang-cc -emit-llvm < %s -o %t &&
// RUN: grep "store i32 351, i32*" %t &&
-// RUN: grep "w = global %0 <{ i32 2, i8 0, i8 0, i8 0, i8 0 }>" %t &&
-// RUN: grep "y = global %1 <{ double 7.300000e+01 }>" %t
+// RUN: grep "w = global %0 { i32 2, \[4 x i8\] zeroinitializer }" %t &&
+// RUN: grep "y = global %1 { double 7.300000e+01 }" %t
union u { int i; double d; };
More information about the cfe-commits
mailing list