Skip to content

Commit f36007e

Browse files
author
Jessica Paquette
committed
[GlobalISel] Implement computeKnownBits for G_SEXT_INREG
Just use the existing `Known.sextInReg` implementation. - Update KnownBitsTest.cpp. - Update combine-redundant-and.mir for a more concrete example. Differential Revision: https://reviews.llvm.org/D95484
1 parent 0554541 commit f36007e

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
347347
Known = Known.sext(BitWidth);
348348
break;
349349
}
350+
case TargetOpcode::G_SEXT_INREG: {
351+
computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
352+
Depth + 1);
353+
Known = Known.sextInReg(MI.getOperand(2).getImm());
354+
break;
355+
}
350356
case TargetOpcode::G_ANYEXT: {
351357
computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
352358
Depth + 1);

llvm/test/CodeGen/AMDGPU/GlobalISel/combine-redundant-and.mir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,24 @@ body: |
142142
$sgpr0 = COPY %6(s32)
143143
SI_RETURN_TO_EPILOG implicit $sgpr0
144144
...
145+
---
146+
name: test_sext_inreg
147+
tracksRegLiveness: true
148+
body: |
149+
bb.0:
150+
; CHECK-LABEL: name: test_sext_inreg
151+
; CHECK: %cst_1:_(s32) = G_CONSTANT i32 -5
152+
; CHECK: $sgpr0 = COPY %cst_1(s32)
153+
; CHECK: SI_RETURN_TO_EPILOG implicit $sgpr0
154+
%cst_1:_(s32) = G_CONSTANT i32 -5
155+
156+
; 000 ... 1011
157+
%cst_11:_(s32) = G_CONSTANT i32 11
158+
159+
; Sext from the 4th bit -> 111 ... 1011 = -5
160+
%sext_inreg_11:_(s32) = G_SEXT_INREG %cst_11, 4
161+
162+
%and:_(s32) = G_AND %cst_1(s32), %sext_inreg_11(s32)
163+
$sgpr0 = COPY %and(s32)
164+
SI_RETURN_TO_EPILOG implicit $sgpr0
165+
...

llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,78 @@ TEST_F(AArch64GISelMITest, TestKnownBitsExt) {
701701
EXPECT_EQ((uint64_t)0xfffffffe, Res.Zero.getZExtValue());
702702
}
703703

704+
TEST_F(AArch64GISelMITest, TestKnownBitsSextInReg) {
705+
StringRef MIRString = R"(
706+
; 000...0001
707+
%one:_(s32) = G_CONSTANT i32 1
708+
709+
; 000...0010
710+
%two:_(s32) = G_CONSTANT i32 2
711+
712+
; 000...1010
713+
%ten:_(s32) = G_CONSTANT i32 10
714+
715+
; ???...????
716+
%w0:_(s32) = COPY $w0
717+
718+
; ???...?1?
719+
%or:_(s32) = G_OR %w0, %two
720+
721+
; All bits are known.
722+
%inreg1:_(s32) = G_SEXT_INREG %one, 1
723+
%copy_inreg1:_(s32) = COPY %inreg1
724+
725+
; All bits unknown
726+
%inreg2:_(s32) = G_SEXT_INREG %or, 1
727+
%copy_inreg2:_(s32) = COPY %inreg2
728+
729+
; Extending from the only (known) set bit
730+
; 111...11?
731+
%inreg3:_(s32) = G_SEXT_INREG %or, 2
732+
%copy_inreg3:_(s32) = COPY %inreg3
733+
734+
; Extending from a known set bit, overwriting all of the high set bits.
735+
; 111...1110
736+
%inreg4:_(s32) = G_SEXT_INREG %ten, 2
737+
%copy_inreg4:_(s32) = COPY %inreg4
738+
739+
)";
740+
setUp(MIRString);
741+
if (!TM)
742+
return;
743+
GISelKnownBits Info(*MF);
744+
KnownBits Res;
745+
auto GetKB = [&](unsigned Idx) {
746+
Register CopyReg = Copies[Idx];
747+
auto *Copy = MRI->getVRegDef(CopyReg);
748+
return Info.getKnownBits(Copy->getOperand(1).getReg());
749+
};
750+
751+
// Every bit is known to be a 1.
752+
Res = GetKB(Copies.size() - 4);
753+
EXPECT_EQ(32u, Res.getBitWidth());
754+
EXPECT_TRUE(Res.isAllOnes());
755+
756+
// All bits are unknown
757+
Res = GetKB(Copies.size() - 3);
758+
EXPECT_EQ(32u, Res.getBitWidth());
759+
EXPECT_TRUE(Res.isUnknown());
760+
761+
// Extending from the only known set bit
762+
// 111...11?
763+
Res = GetKB(Copies.size() - 2);
764+
EXPECT_EQ(32u, Res.getBitWidth());
765+
EXPECT_EQ(0xFFFFFFFEu, Res.One.getZExtValue());
766+
EXPECT_EQ(0u, Res.Zero.getZExtValue());
767+
768+
// Extending from a known set bit, overwriting all of the high set bits.
769+
// 111...1110
770+
Res = GetKB(Copies.size() - 1);
771+
EXPECT_EQ(32u, Res.getBitWidth());
772+
EXPECT_EQ(0xFFFFFFFEu, Res.One.getZExtValue());
773+
EXPECT_EQ(1u, Res.Zero.getZExtValue());
774+
}
775+
704776
TEST_F(AArch64GISelMITest, TestKnownBitsMergeValues) {
705777
StringRef MIRString = R"(
706778
%val0:_(s16) = G_CONSTANT i16 35224

0 commit comments

Comments
 (0)