llvm.org GIT mirror llvm / 24e04b8
[AsmPrinter] Make isRepeatedByteSequence smarter about odd integer types - zext the value to alloc size first, then check if the value repeats with zero padding included. If so we can still emit a .space - Do the checking with APInt.isSplat(8), which handles non-pow2 types - Also handle large constants (bit width > 64) - In a ConstantArray all elements have the same type, so it's sufficient to check the first constant recursively and then just compare if all following constants are the same by pointer compare git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239977 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 5 years ago
2 changed file(s) with 45 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
17941794 /// composed of a repeated sequence of identical bytes and return the
17951795 /// byte value. If it is not a repeated sequence, return -1.
17961796 static int isRepeatedByteSequence(const Value *V, TargetMachine &TM) {
1797
17981797 if (const ConstantInt *CI = dyn_cast(V)) {
1799 if (CI->getBitWidth() > 64) return -1;
1800
1801 uint64_t Size =
1802 TM.getDataLayout()->getTypeAllocSize(V->getType());
1803 uint64_t Value = CI->getZExtValue();
1804
1805 // Make sure the constant is at least 8 bits long and has a power
1806 // of 2 bit width. This guarantees the constant bit width is
1807 // always a multiple of 8 bits, avoiding issues with padding out
1808 // to Size and other such corner cases.
1809 if (CI->getBitWidth() < 8 || !isPowerOf2_64(CI->getBitWidth())) return -1;
1810
1811 uint8_t Byte = static_cast(Value);
1812
1813 for (unsigned i = 1; i < Size; ++i) {
1814 Value >>= 8;
1815 if (static_cast(Value) != Byte) return -1;
1816 }
1817 return Byte;
1798 uint64_t Size = TM.getDataLayout()->getTypeAllocSizeInBits(V->getType());
1799 assert(Size % 8 == 0);
1800
1801 // Extend the element to take zero padding into account.
1802 APInt Value = CI->getValue().zextOrSelf(Size);
1803 if (!Value.isSplat(8))
1804 return -1;
1805
1806 return Value.zextOrTrunc(8).getZExtValue();
18181807 }
18191808 if (const ConstantArray *CA = dyn_cast(V)) {
18201809 // Make sure all array elements are sequences of the same repeated
18211810 // byte.
18221811 assert(CA->getNumOperands() != 0 && "Should be a CAZ");
1823 int Byte = isRepeatedByteSequence(CA->getOperand(0), TM);
1824 if (Byte == -1) return -1;
1825
1826 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1827 int ThisByte = isRepeatedByteSequence(CA->getOperand(i), TM);
1828 if (ThisByte == -1) return -1;
1829 if (Byte != ThisByte) return -1;
1830 }
1812 Constant *Op0 = CA->getOperand(0);
1813 int Byte = isRepeatedByteSequence(Op0, TM);
1814 if (Byte == -1)
1815 return -1;
1816
1817 // All array elements must be equal.
1818 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i)
1819 if (CA->getOperand(i) != Op0)
1820 return -1;
18311821 return Byte;
18321822 }
18331823
0 ; RUN: llc -mtriple=x86_64-apple-darwin < %s | FileCheck %s
1
2 @test1 = global [2 x i24] [i24 -1, i24 -1]
3 ; CHECK-LABEL: test1:
4 ; CHECK-NEXT: .long 16777215
5 ; CHECK-NEXT: .long 16777215
6
7 @test2 = global [2 x i7] [i7 1, i7 1]
8 ; CHECK-LABEL: test2:
9 ; CHECK-NEXT: .space 2,1
10
11 @test3 = global [4 x i128] [i128 -1, i128 -1, i128 -1, i128 -1]
12 ; CHECK-LABEL: test3:
13 ; CHECK-NEXT: .space 64,255
14
15 @test4 = global [3 x i16] [i16 257, i16 257, i16 257]
16 ; CHECK-LABEL: test4:
17 ; CHECK-NEXT: .space 6,1
18
19 @test5 = global [2 x [2 x i16]] [[2 x i16] [i16 257, i16 257], [2 x i16] [i16 -1, i16 -1]]
20 ; CHECK-LABEL: test5:
21 ; CHECK-NEXT: .space 4,1
22 ; CHECK-NEXT: .space 4,255
23
24 @test6 = global [2 x [2 x i16]] [[2 x i16] [i16 257, i16 257], [2 x i16] [i16 257, i16 257]]
25 ; CHECK-LABEL: test6:
26 ; CHECK-NEXT: .space 8,1