Skip to content

Commit ce6eb4f

Browse files
committed
[GlobalISel][KnownBits] Implement G_CTPOP
Implementation copied almost verbatim from ValueTracking. Differential revision: https://reviews.llvm.org/D107606
1 parent a8a38ef commit ce6eb4f

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// Provides analysis for querying information about KnownBits during GISel
1010
/// passes.
1111
//
12-
//===------------------
12+
//===----------------------------------------------------------------------===//
1313
#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
1414
#include "llvm/Analysis/ValueTracking.h"
1515
#include "llvm/CodeGen/GlobalISel/Utils.h"
@@ -510,6 +510,18 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
510510
Known = Known.reverseBits();
511511
break;
512512
}
513+
case TargetOpcode::G_CTPOP: {
514+
computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
515+
Depth + 1);
516+
// We can bound the space the count needs. Also, bits known to be zero can't
517+
// contribute to the population.
518+
unsigned BitsPossiblySet = Known2.countMaxPopulation();
519+
unsigned LowBits = Log2_32(BitsPossiblySet)+1;
520+
Known.Zero.setBitsFrom(LowBits);
521+
// TODO: we could bound Known.One using the lower bound on the number of
522+
// bits which might be set provided by popcnt KnownOne2.
523+
break;
524+
}
513525
case TargetOpcode::G_UBFX: {
514526
KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
515527
computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,

llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,63 @@ TEST_F(AArch64GISelMITest, TestKnownBitsAssertZext) {
16721672
EXPECT_EQ(0xFFFFFFFFFFFFFFF8u, Res.Zero.getZExtValue());
16731673
}
16741674

1675+
TEST_F(AArch64GISelMITest, TestKnownBitsCTPOP) {
1676+
StringRef MIRString = R"(
1677+
%src:_(s32) = COPY $w0
1678+
%unknown:_(s32) = G_CTPOP %src
1679+
%constant_4294967295:_(s32) = G_CONSTANT i32 4294967295
1680+
%thirtytwo:_(s32) = G_CTPOP %constant_4294967295
1681+
%thirtytwo_copy:_(s32) = COPY %thirtytwo
1682+
%constant_15:_(s32) = G_CONSTANT i32 15
1683+
%four:_(s32) = G_CTPOP %constant_15
1684+
%four_copy:_(s32) = COPY %four
1685+
%constant_1:_(s32) = G_CONSTANT i32 1
1686+
%one:_(s32) = G_CTPOP %constant_1
1687+
%one_copy:_(s32) = COPY %one
1688+
)";
1689+
setUp(MIRString);
1690+
if (!TM)
1691+
return;
1692+
1693+
Register UnknownCopy = Copies[Copies.size() - 4];
1694+
Register ThirtytwoCopy = Copies[Copies.size() - 3];
1695+
Register FourCopy = Copies[Copies.size() - 2];
1696+
Register OneCopy = Copies[Copies.size() - 1];
1697+
1698+
GISelKnownBits Info(*MF);
1699+
MachineInstr *Copy;
1700+
Register SrcReg;
1701+
KnownBits Res;
1702+
1703+
Copy = MRI->getVRegDef(UnknownCopy);
1704+
SrcReg = Copy->getOperand(1).getReg();
1705+
Res = Info.getKnownBits(SrcReg);
1706+
EXPECT_EQ(1u, Res.getBitWidth());
1707+
EXPECT_EQ(0u, Res.One.getZExtValue());
1708+
EXPECT_EQ(0u, Res.Zero.getZExtValue());
1709+
1710+
Copy = MRI->getVRegDef(ThirtytwoCopy);
1711+
SrcReg = Copy->getOperand(1).getReg();
1712+
Res = Info.getKnownBits(SrcReg);
1713+
EXPECT_EQ(32u, Res.getBitWidth());
1714+
EXPECT_EQ(0u, Res.One.getZExtValue());
1715+
EXPECT_EQ(0xFFFFFFC0u, Res.Zero.getZExtValue());
1716+
1717+
Copy = MRI->getVRegDef(FourCopy);
1718+
SrcReg = Copy->getOperand(1).getReg();
1719+
Res = Info.getKnownBits(SrcReg);
1720+
EXPECT_EQ(32u, Res.getBitWidth());
1721+
EXPECT_EQ(0u, Res.One.getZExtValue());
1722+
EXPECT_EQ(0xFFFFFFF8u, Res.Zero.getZExtValue());
1723+
1724+
Copy = MRI->getVRegDef(OneCopy);
1725+
SrcReg = Copy->getOperand(1).getReg();
1726+
Res = Info.getKnownBits(SrcReg);
1727+
EXPECT_EQ(32u, Res.getBitWidth());
1728+
EXPECT_EQ(0u, Res.One.getZExtValue());
1729+
EXPECT_EQ(0xFFFFFFFEu, Res.Zero.getZExtValue());
1730+
}
1731+
16751732
TEST_F(AMDGPUGISelMITest, TestKnownBitsUBFX) {
16761733
StringRef MIRString = " %3:_(s32) = G_IMPLICIT_DEF\n"
16771734
" %4:_(s32) = G_CONSTANT i32 12\n"

0 commit comments

Comments
 (0)