@@ -2995,17 +2995,42 @@ static void emitGlobalConstantArray(const DataLayout &DL,
2995
2995
}
2996
2996
}
2997
2997
2998
+ static void emitGlobalConstantLargeInt (const ConstantInt *CI, AsmPrinter &AP);
2999
+
2998
3000
static void emitGlobalConstantVector (const DataLayout &DL,
2999
3001
const ConstantVector *CV, AsmPrinter &AP,
3000
3002
AsmPrinter::AliasMapTy *AliasList) {
3001
- for (unsigned I = 0 , E = CV->getType ()->getNumElements (); I != E; ++I) {
3002
- emitGlobalAliasInline (AP, DL.getTypeAllocSize (CV->getType ()) * I, AliasList);
3003
- emitGlobalConstantImpl (DL, CV->getOperand (I), AP);
3003
+ Type *ElementType = CV->getType ()->getElementType ();
3004
+ uint64_t ElementSizeInBits = DL.getTypeSizeInBits (ElementType);
3005
+ uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits (ElementType);
3006
+ uint64_t EmittedSize;
3007
+ if (ElementSizeInBits != ElementAllocSizeInBits) {
3008
+ // If the allocation size of an element is different from the size in bits,
3009
+ // printing each element separately will insert incorrect padding.
3010
+ //
3011
+ // The general algorithm here is complicated; instead of writing it out
3012
+ // here, just use the existing code in ConstantFolding.
3013
+ Type *IntT =
3014
+ IntegerType::get (CV->getContext (), DL.getTypeSizeInBits (CV->getType ()));
3015
+ ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant (
3016
+ ConstantExpr::getBitCast (const_cast <ConstantVector *>(CV), IntT), DL));
3017
+ if (!CI) {
3018
+ report_fatal_error (
3019
+ " Cannot lower vector global with unusual element type" );
3020
+ }
3021
+ emitGlobalAliasInline (AP, 0 , AliasList);
3022
+ emitGlobalConstantLargeInt (CI, AP);
3023
+ EmittedSize = DL.getTypeStoreSize (CV->getType ());
3024
+ } else {
3025
+ for (unsigned I = 0 , E = CV->getType ()->getNumElements (); I != E; ++I) {
3026
+ emitGlobalAliasInline (AP, DL.getTypeAllocSize (CV->getType ()) * I, AliasList);
3027
+ emitGlobalConstantImpl (DL, CV->getOperand (I), AP);
3028
+ }
3029
+ EmittedSize =
3030
+ DL.getTypeAllocSize (ElementType) * CV->getType ()->getNumElements ();
3004
3031
}
3005
3032
3006
3033
unsigned Size = DL.getTypeAllocSize (CV->getType ());
3007
- unsigned EmittedSize = DL.getTypeAllocSize (CV->getType ()->getElementType ()) *
3008
- CV->getType ()->getNumElements ();
3009
3034
if (unsigned Padding = Size - EmittedSize)
3010
3035
AP.OutStreamer ->emitZeros (Padding);
3011
3036
}
0 commit comments