[llvm] e4a9225 - [GISel][KnownBits] Give up on PHI analysis as soon as we don't know anything
Quentin Colombet via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 20 11:34:55 PST 2020
Author: Quentin Colombet
Date: 2020-02-20T11:34:01-08:00
New Revision: e4a9225f5d10f35cf9cf3664bb93620880228782
URL: https://github.com/llvm/llvm-project/commit/e4a9225f5d10f35cf9cf3664bb93620880228782
DIFF: https://github.com/llvm/llvm-project/commit/e4a9225f5d10f35cf9cf3664bb93620880228782.diff
LOG: [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.
Added:
Modified:
llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
index 04b29948b5ef..9b6d2fd8c215 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
@@ -156,6 +156,10 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
Depth + (Opcode != TargetOpcode::COPY));
Known.One &= Known2.One;
Known.Zero &= Known2.Zero;
+ // If we reach a point where we don't know anything
+ // just stop looking through the operands.
+ if (Known.One == 0 && Known.Zero == 0)
+ break;
} else {
// We know nothing.
Known = KnownBits(BitWidth);
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
index 72d713beacc8..f75de5c22593 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -123,6 +123,44 @@ TEST_F(GISelMITest, TestKnownBitsCstPHIToNonGenericReg) {
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
More information about the llvm-commits
mailing list