From f30416fdde922eaa655934e050026930fefbd260 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 27 Apr 2020 14:56:30 -0700 Subject: [PATCH] [AsmPrinter] Fix emission of non-standard integer constants for BE targets The code assumed that zero-extending the integer constant to the designated alloc size would be fine even for BE targets, but that's not the case as that pulls in zeros from the MSB side while we actually expect the padding zeros to go after the LSB. I've changed the codepath handling the constant integers to use the store size for both small(er than u64) and big constants and then add zero padding right after that. Differential Revision: https://reviews.llvm.org/D78011 --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 25 ++++++++------ llvm/test/CodeGen/ARM/emit-big-cst.ll | 4 ++- llvm/test/CodeGen/Mips/emit-big-cst.ll | 34 ++++++++++++++++--- .../PowerPC/aix-xcoff-mergeable-const.ll | 6 ++-- llvm/test/CodeGen/X86/emit-big-cst.ll | 4 ++- llvm/test/CodeGen/X86/global-fill.ll | 8 +++-- 6 files changed, 58 insertions(+), 23 deletions(-) diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 0d31a9db41545..54b9aed3acdf1 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2617,9 +2617,10 @@ static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) { // [chunk1][chunk2] ... [chunkN]. // The most significant chunk is chunkN and it should be emitted first. // However, due to the alignment issue chunkN contains useless bits. - // Realign the chunks so that they contain only useless information: + // Realign the chunks so that they contain only useful information: // ExtraBits 0 1 (BitWidth / 64) - 1 // chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN] + ExtraBitsSize = alignTo(ExtraBitsSize, 8); ExtraBits = Realigned.getRawData()[0] & (((uint64_t)-1) >> (64 - ExtraBitsSize)); Realigned.lshrInPlace(ExtraBitsSize); @@ -2640,7 +2641,7 @@ static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) { // Emit the extra bits after the 64-bits chunks. // Emit a directive that fills the expected size. - uint64_t Size = AP.getDataLayout().getTypeAllocSize(CI->getType()); + uint64_t Size = AP.getDataLayout().getTypeStoreSize(CI->getType()); Size -= (BitWidth / 64) * 8; assert(Size && Size * 8 >= ExtraBitsSize && (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize))) @@ -2755,20 +2756,22 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV, return AP.OutStreamer->emitZeros(Size); if (const ConstantInt *CI = dyn_cast(CV)) { - switch (Size) { - case 1: - case 2: - case 4: - case 8: + const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType()); + + if (StoreSize < 8) { if (AP.isVerbose()) AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n", CI->getZExtValue()); - AP.OutStreamer->emitIntValue(CI->getZExtValue(), Size); - return; - default: + AP.OutStreamer->emitIntValue(CI->getZExtValue(), StoreSize); + } else { emitGlobalConstantLargeInt(CI, AP); - return; } + + // Emit tail padding if needed + if (Size != StoreSize) + AP.OutStreamer->emitZeros(Size - StoreSize); + + return; } if (const ConstantFP *CFP = dyn_cast(CV)) diff --git a/llvm/test/CodeGen/ARM/emit-big-cst.ll b/llvm/test/CodeGen/ARM/emit-big-cst.ll index 7453e8caa9465..e0c6d4e893e1f 100644 --- a/llvm/test/CodeGen/ARM/emit-big-cst.ll +++ b/llvm/test/CodeGen/ARM/emit-big-cst.ll @@ -4,7 +4,9 @@ ; CHECK: bigCst: ; CHECK-NEXT: .long 1694510592 ; CHECK-NEXT: .long 2960197 -; CHECK-NEXT: .long 26220 +; CHECK-NEXT: .short 26220 +; CHECK-NEXT: .byte 0 +; CHECK-NEXT: .zero 1 ; CHECK-NEXT: .size bigCst, 12 @bigCst = internal constant i82 483673642326615442599424 diff --git a/llvm/test/CodeGen/Mips/emit-big-cst.ll b/llvm/test/CodeGen/Mips/emit-big-cst.ll index 9bc96c89307d7..67c2f107db19a 100644 --- a/llvm/test/CodeGen/Mips/emit-big-cst.ll +++ b/llvm/test/CodeGen/Mips/emit-big-cst.ll @@ -1,10 +1,32 @@ -; RUN: llc -march=mips < %s | FileCheck %s +; RUN: llc -march=mips < %s | FileCheck %s --check-prefix=BE +; RUN: llc -march=mipsel < %s | FileCheck %s --check-prefix=LE ; Check assembly printing of odd constants. -; CHECK: bigCst: -; CHECK-NEXT: .8byte 1845068520838224192 -; CHECK-NEXT: .8byte 11776 -; CHECK-NEXT: .size bigCst, 16 +; BE-LABEL: bigCst: +; BE-NEXT: .8byte 28829195638097253 +; BE-NEXT: .2byte 46 +; BE-NEXT: .byte 0 +; BE-NEXT: .space 5 +; BE-NEXT: .size bigCst, 16 + +; LE-LABEL: bigCst: +; LE-NEXT: .8byte 12713950999227904 +; LE-NEXT: .2byte 26220 +; LE-NEXT: .byte 0 +; LE-NEXT: .space 5 +; LE-NEXT: .size bigCst, 16 + +; BE-LABEL: smallCst: +; BE-NEXT: .2byte 4386 +; BE-NEXT: .byte 51 +; BE-NEXT: .space 1 +; BE-NEXT: .size smallCst, 4 + +; LE-LABEL: smallCst: +; LE-NEXT: .2byte 8755 +; LE-NEXT: .byte 17 +; LE-NEXT: .space 1 +; LE-NEXT: .size smallCst, 4 @bigCst = internal constant i82 483673642326615442599424 @@ -15,3 +37,5 @@ define void @accessBig(i64* %storage) { store i82 %tmp, i82* %addr ret void } + +@smallCst = internal constant i24 1122867 diff --git a/llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll b/llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll index 5c1f55f4aac44..f987a20ccbab9 100644 --- a/llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll +++ b/llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll @@ -25,15 +25,15 @@ entry: ;CHECK: .csect .rodata[RO] ;CHECK-NEXT: .align 4 ;CHECK-NEXT: .L__const.main.cnst32: -;CHECK-NEXT: .llong 4611686018427387954 # 0x4000000000000032 +;CHECK-NEXT: .llong 4611686018427387954 ;CHECK-NEXT: .long 0 # 0x0 ;CHECK-NEXT: .space 4 -;CHECK-NEXT: .llong 0 # 0x0 +;CHECK-NEXT: .llong 0 ;CHECK-NEXT: .long 0 # 0x0 ;CHECK-NEXT: .space 4 ;CHECK-NEXT: .align 3 ;CHECK-NEXT: .L__const.main.cnst16: -;CHECK-NEXT: .llong 4611686018427387926 # 0x4000000000000016 +;CHECK-NEXT: .llong 4611686018427387926 ;CHECK-NEXT: .long 0 # 0x0 ;CHECK-NEXT: .space 4 ;CHECK-NEXT: .align 3 diff --git a/llvm/test/CodeGen/X86/emit-big-cst.ll b/llvm/test/CodeGen/X86/emit-big-cst.ll index 51852d00f823b..930684cc8b1f6 100644 --- a/llvm/test/CodeGen/X86/emit-big-cst.ll +++ b/llvm/test/CodeGen/X86/emit-big-cst.ll @@ -3,7 +3,9 @@ ; CHECK: bigCst: ; CHECK-NEXT: .quad 12713950999227904 -; CHECK-NEXT: .quad 26220 +; CHECK-NEXT: .short 26220 +; CHECK-NEXT: .byte 0 +; CHECK-NEXT: .zero 5 ; CHECK-NEXT: .size bigCst, 16 @bigCst = internal constant i82 483673642326615442599424 diff --git a/llvm/test/CodeGen/X86/global-fill.ll b/llvm/test/CodeGen/X86/global-fill.ll index 656c8ca2c323a..41493b2d917fb 100644 --- a/llvm/test/CodeGen/X86/global-fill.ll +++ b/llvm/test/CodeGen/X86/global-fill.ll @@ -2,8 +2,12 @@ @test1 = global [2 x i24] [i24 -1, i24 -1] ; CHECK-LABEL: test1: -; CHECK-NEXT: .long 16777215 -; CHECK-NEXT: .long 16777215 +; CHECK-NEXT: .short 65535 +; CHECK-NEXT: .byte 255 +; CHECK-NEXT: .space 1 +; CHECK-NEXT: .short 65535 +; CHECK-NEXT: .byte 255 +; CHECK-NEXT: .space 1 @test2 = global [2 x i7] [i7 1, i7 1] ; CHECK-LABEL: test2: