Skip to content

Commit

Permalink
[AsmPrinter] Fix emission of non-standard integer constants for BE ta…
Browse files Browse the repository at this point in the history
…rgets

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
  • Loading branch information
LemonBoy authored and efriedma-quic committed Apr 27, 2020
1 parent 7a8c226 commit f30416f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
25 changes: 14 additions & 11 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Expand Up @@ -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);
Expand All @@ -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)))
Expand Down Expand Up @@ -2755,20 +2756,22 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
return AP.OutStreamer->emitZeros(Size);

if (const ConstantInt *CI = dyn_cast<ConstantInt>(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<ConstantFP>(CV))
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/ARM/emit-big-cst.ll
Expand Up @@ -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
Expand Down
34 changes: 29 additions & 5 deletions 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

Expand All @@ -15,3 +37,5 @@ define void @accessBig(i64* %storage) {
store i82 %tmp, i82* %addr
ret void
}

@smallCst = internal constant i24 1122867
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/X86/emit-big-cst.ll
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions llvm/test/CodeGen/X86/global-fill.ll
Expand Up @@ -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:
Expand Down

0 comments on commit f30416f

Please sign in to comment.