[GISel][KnownBits] Give up on PHI analysis as soon as we don't know anything
When analyzing PHIs, we gather the known bits for every operand and
merge them together to get the known bits of the result of the PHI.
It is not unusual that merging the information leads to know nothing
on the result (e.g., phi a: i8 3, b: i8 unknown, ..., after looking at the
second argument we know we will know nothing on the result), thus, as
soon as we reach that state, stop analyzing the following operand (i.e.,
on the previous example, we won't process anything after looking at `b`).
This improves compile time in particular with PHIs with a large number
of operands.
NFC.
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
index 72d713b..f75de5c 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -123,6 +123,44 @@
EXPECT_EQ(Res.Zero.getZExtValue(), Res2.Zero.getZExtValue());
}
+// Check that we know nothing when at least one value of a PHI
+// comes from something we cannot analysis.
+// This test is not particularly interesting, it is just
+// here to cover the code that stops the analysis of PHIs
+// earlier. In that case, we would not even look at the
+// second incoming value.
+TEST_F(GISelMITest, TestKnownBitsUnknownPHI) {
+ StringRef MIRString =
+ " bb.10:\n"
+ " %10:_(s64) = COPY %0\n"
+ " %11:_(s1) = G_IMPLICIT_DEF\n"
+ " G_BRCOND %11(s1), %bb.11\n"
+ " G_BR %bb.12\n"
+ "\n"
+ " bb.11:\n"
+ " %12:_(s64) = G_CONSTANT i64 2\n"
+ " G_BR %bb.12\n"
+ "\n"
+ " bb.12:\n"
+ " %13:_(s64) = PHI %10(s64), %bb.10, %12(s64), %bb.11\n"
+ " %14:_(s64) = COPY %13\n";
+ setUp(MIRString);
+ if (!TM)
+ return;
+ Register CopyReg = Copies[Copies.size() - 1];
+ MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+ Register SrcReg = FinalCopy->getOperand(1).getReg();
+ Register DstReg = FinalCopy->getOperand(0).getReg();
+ GISelKnownBits Info(*MF);
+ KnownBits Res = Info.getKnownBits(SrcReg);
+ EXPECT_EQ((uint64_t)0, Res.One.getZExtValue());
+ EXPECT_EQ((uint64_t)0, Res.Zero.getZExtValue());
+
+ KnownBits Res2 = Info.getKnownBits(DstReg);
+ EXPECT_EQ(Res.One.getZExtValue(), Res2.One.getZExtValue());
+ EXPECT_EQ(Res.Zero.getZExtValue(), Res2.Zero.getZExtValue());
+}
+
// Check that we manage to process PHIs that loop on themselves.
// For now, the analysis just stops and assumes it knows nothing,
// eventually we could teach it how to properly track phis that