Skip to content

Commit 1283dca

Browse files
committed
[GISel] Correct the known bits of G_ANYEXT
Known bits for G_ANYEXT was incorrectly using KnownBits::zext, causing us to treat the high bits as zero even though they're (by definition) unknown. Differential Revision: https://reviews.llvm.org/D86323
1 parent 7092398 commit 1283dca

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
316316
case TargetOpcode::G_ANYEXT: {
317317
computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
318318
Depth + 1);
319-
Known = Known.zext(BitWidth);
319+
Known = Known.anyext(BitWidth);
320320
break;
321321
}
322322
case TargetOpcode::G_LOAD: {

llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,47 @@ TEST_F(AArch64GISelMITest, TestMetadata) {
511511
Mask.flipAllBits();
512512
EXPECT_EQ(Mask.getZExtValue(), Res.Zero.getZExtValue());
513513
}
514+
515+
TEST_F(AArch64GISelMITest, TestKnownBitsExt) {
516+
StringRef MIRString = " %c1:_(s16) = G_CONSTANT i16 1\n"
517+
" %x:_(s16) = G_IMPLICIT_DEF\n"
518+
" %y:_(s16) = G_AND %x, %c1\n"
519+
" %anyext:_(s32) = G_ANYEXT %y(s16)\n"
520+
" %r1:_(s32) = COPY %anyext\n"
521+
" %zext:_(s32) = G_ZEXT %y(s16)\n"
522+
" %r2:_(s32) = COPY %zext\n"
523+
" %sext:_(s32) = G_SEXT %y(s16)\n"
524+
" %r3:_(s32) = COPY %sext\n";
525+
setUp(MIRString);
526+
if (!TM)
527+
return;
528+
Register CopyRegAny = Copies[Copies.size() - 3];
529+
Register CopyRegZ = Copies[Copies.size() - 2];
530+
Register CopyRegS = Copies[Copies.size() - 1];
531+
532+
GISelKnownBits Info(*MF);
533+
MachineInstr *Copy;
534+
Register SrcReg;
535+
KnownBits Res;
536+
537+
Copy = MRI->getVRegDef(CopyRegAny);
538+
SrcReg = Copy->getOperand(1).getReg();
539+
Res = Info.getKnownBits(SrcReg);
540+
EXPECT_EQ((uint64_t)32, Res.getBitWidth());
541+
EXPECT_EQ((uint64_t)0, Res.One.getZExtValue());
542+
EXPECT_EQ((uint64_t)0x0000fffe, Res.Zero.getZExtValue());
543+
544+
Copy = MRI->getVRegDef(CopyRegZ);
545+
SrcReg = Copy->getOperand(1).getReg();
546+
Res = Info.getKnownBits(SrcReg);
547+
EXPECT_EQ((uint64_t)32, Res.getBitWidth());
548+
EXPECT_EQ((uint64_t)0, Res.One.getZExtValue());
549+
EXPECT_EQ((uint64_t)0xfffffffe, Res.Zero.getZExtValue());
550+
551+
Copy = MRI->getVRegDef(CopyRegS);
552+
SrcReg = Copy->getOperand(1).getReg();
553+
Res = Info.getKnownBits(SrcReg);
554+
EXPECT_EQ((uint64_t)32, Res.getBitWidth());
555+
EXPECT_EQ((uint64_t)0, Res.One.getZExtValue());
556+
EXPECT_EQ((uint64_t)0xfffffffe, Res.Zero.getZExtValue());
557+
}

0 commit comments

Comments
 (0)