From 71177b0c4cd96b5a2858d49787638ee6072bb08b Mon Sep 17 00:00:00 2001 From: Amara Emerson Date: Sat, 17 Jul 2021 23:03:52 -0700 Subject: [PATCH] [GlobalISel] Fix known bits for G_BSWAP and B_BITREVERSE not doing anything. llvm::KnownBits::byteSwap() and reverse() don't modify in-place, so we weren't actually computing anything. This was causing a miscompile on an arm64 stage2 bootstrap clang build. --- lib/CodeGen/GlobalISel/GISelKnownBits.cpp | 4 ++-- unittests/CodeGen/GlobalISel/KnownBitsTest.cpp | 11 ++++++----- unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp | 11 ++++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/lib/CodeGen/GlobalISel/GISelKnownBits.cpp index a7f0d5fde12..8146a67d4df 100644 --- a/lib/CodeGen/GlobalISel/GISelKnownBits.cpp +++ b/lib/CodeGen/GlobalISel/GISelKnownBits.cpp @@ -501,13 +501,13 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known, case TargetOpcode::G_BSWAP: { Register SrcReg = MI.getOperand(1).getReg(); computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); - Known.byteSwap(); + Known = Known.byteSwap(); break; } case TargetOpcode::G_BITREVERSE: { Register SrcReg = MI.getOperand(1).getReg(); computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); - Known.reverseBits(); + Known = Known.reverseBits(); break; } case TargetOpcode::G_UBFX: { diff --git a/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp index 26153168e8e..e948f150e67 100644 --- a/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp +++ b/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp @@ -1369,7 +1369,8 @@ TEST_F(AArch64GISelMITest, TestKnownBitsBSwapBitReverse) { if (!TM) return; - const uint32_t TestVal = 0x11223344; + const uint32_t ByteSwappedVal = 0x44332211; + const uint32_t BitSwappedVal = 0x22cc4488; Register CopyBSwap = Copies[Copies.size() - 2]; Register CopyBitReverse = Copies[Copies.size() - 1]; @@ -1378,13 +1379,13 @@ TEST_F(AArch64GISelMITest, TestKnownBitsBSwapBitReverse) { KnownBits BSwapKnown = Info.getKnownBits(CopyBSwap); EXPECT_EQ(32u, BSwapKnown.getBitWidth()); - EXPECT_EQ(TestVal, BSwapKnown.One.getZExtValue()); - EXPECT_EQ(~TestVal, BSwapKnown.Zero.getZExtValue()); + EXPECT_EQ(ByteSwappedVal, BSwapKnown.One.getZExtValue()); + EXPECT_EQ(~ByteSwappedVal, BSwapKnown.Zero.getZExtValue()); KnownBits BitReverseKnown = Info.getKnownBits(CopyBitReverse); EXPECT_EQ(32u, BitReverseKnown.getBitWidth()); - EXPECT_EQ(TestVal, BitReverseKnown.One.getZExtValue()); - EXPECT_EQ(~TestVal, BitReverseKnown.Zero.getZExtValue()); + EXPECT_EQ(BitSwappedVal, BitReverseKnown.One.getZExtValue()); + EXPECT_EQ(~BitSwappedVal, BitReverseKnown.Zero.getZExtValue()); } TEST_F(AArch64GISelMITest, TestKnownBitsUMAX) { diff --git a/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp b/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp index 6def98803b6..a4767ead1bf 100644 --- a/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp +++ b/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp @@ -1227,7 +1227,8 @@ TEST_F(AArch64GISelMITest, TestVectorKnownBitsBSwapBitReverse) { if (!TM) return; - const uint32_t TestVal = 0x11223344; + const uint32_t ByteSwappedVal = 0x44332211; + const uint32_t BitSwappedVal = 0x22cc4488; Register CopyBSwap = Copies[Copies.size() - 2]; Register CopyBitReverse = Copies[Copies.size() - 1]; @@ -1236,13 +1237,13 @@ TEST_F(AArch64GISelMITest, TestVectorKnownBitsBSwapBitReverse) { KnownBits BSwapKnown = Info.getKnownBits(CopyBSwap); EXPECT_EQ(32u, BSwapKnown.getBitWidth()); - EXPECT_EQ(TestVal, BSwapKnown.One.getZExtValue()); - EXPECT_EQ(~TestVal, BSwapKnown.Zero.getZExtValue()); + EXPECT_EQ(ByteSwappedVal, BSwapKnown.One.getZExtValue()); + EXPECT_EQ(~ByteSwappedVal, BSwapKnown.Zero.getZExtValue()); KnownBits BitReverseKnown = Info.getKnownBits(CopyBitReverse); EXPECT_EQ(32u, BitReverseKnown.getBitWidth()); - EXPECT_EQ(TestVal, BitReverseKnown.One.getZExtValue()); - EXPECT_EQ(~TestVal, BitReverseKnown.Zero.getZExtValue()); + EXPECT_EQ(BitSwappedVal, BitReverseKnown.One.getZExtValue()); + EXPECT_EQ(~BitSwappedVal, BitReverseKnown.Zero.getZExtValue()); } TEST_F(AArch64GISelMITest, TestKnownBitsVectorUMAX) {