[llvm] 5b18d84 - Reapply "ValueTracking: Handle phi in computeKnownFPClass"
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu May 18 04:59:24 PDT 2023
Author: Matt Arsenault
Date: 2023-05-18T12:58:09+01:00
New Revision: 5b18d843806a9288a37e15fb194799847073979e
URL: https://github.com/llvm/llvm-project/commit/5b18d843806a9288a37e15fb194799847073979e
DIFF: https://github.com/llvm/llvm-project/commit/5b18d843806a9288a37e15fb194799847073979e.diff
LOG: Reapply "ValueTracking: Handle phi in computeKnownFPClass"
This reverts commit e13f88d1ff5234946af6349a9a7cf56fcb6c040e.
Fix off by one recursion limit check.
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/unittests/Analysis/ValueTrackingTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 089d560654ce2..bbd7a2487d43e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5103,6 +5103,49 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
Known, Depth + 1, Q, TLI);
break;
}
+ case Instruction::PHI: {
+ const PHINode *P = cast<PHINode>(Op);
+ // Unreachable blocks may have zero-operand PHI nodes.
+ if (P->getNumIncomingValues() == 0)
+ break;
+
+ // Otherwise take the unions of the known bit sets of the operands,
+ // taking conservative care to avoid excessive recursion.
+ const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
+
+ if (Depth < PhiRecursionLimit) {
+ // Skip if every incoming value references to ourself.
+ if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
+ break;
+
+ bool First = true;
+
+ for (Value *IncValue : P->incoming_values()) {
+ // Skip direct self references.
+ if (IncValue == P)
+ continue;
+
+ KnownFPClass KnownSrc;
+ // Recurse, but cap the recursion to two levels, because we don't want
+ // to waste time spinning around in loops. We need at least depth 2 to
+ // detect known sign bits.
+ computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
+ PhiRecursionLimit, Q, TLI);
+
+ if (First) {
+ Known = KnownSrc;
+ First = false;
+ } else {
+ Known |= KnownSrc;
+ }
+
+ if (Known.KnownFPClasses == fcAllFlags)
+ break;
+ }
+ }
+
+ break;
+ }
default:
break;
}
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index c83fb62830d0f..3684f6fb7d539 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -1630,6 +1630,114 @@ TEST_F(ComputeKnownFPClassTest, FMulNoZero) {
expectKnownFPClass(fcAllFlags, std::nullopt, A7);
}
+TEST_F(ComputeKnownFPClassTest, Phi) {
+ parseAssembly(
+ "define float @test(i1 %cond, float nofpclass(nan inf) %arg0, float nofpclass(nan) %arg1) {\n"
+ "entry:\n"
+ " br i1 %cond, label %bb0, label %bb1\n"
+ "bb0:\n"
+ " br label %ret\n"
+ "bb1:\n"
+ " br label %ret\n"
+ "ret:\n"
+ " %A = phi float [ %arg0, %bb0 ], [ %arg1, %bb1 ]\n"
+ " ret float %A\n"
+ "}\n");
+ expectKnownFPClass(~fcNan, std::nullopt);
+}
+
+TEST_F(ComputeKnownFPClassTest, PhiKnownSignFalse) {
+ parseAssembly(
+ "declare float @llvm.fabs.f32(float)"
+ "define float @test(i1 %cond, float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) {\n"
+ "entry:\n"
+ " br i1 %cond, label %bb0, label %bb1\n"
+ "bb0:\n"
+ " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
+ " br label %ret\n"
+ "bb1:\n"
+ " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
+ " br label %ret\n"
+ "ret:\n"
+ " %A = phi float [ %fabs.arg0, %bb0 ], [ %fabs.arg1, %bb1 ]\n"
+ " ret float %A\n"
+ "}\n");
+ expectKnownFPClass(fcPositive, false);
+}
+
+TEST_F(ComputeKnownFPClassTest, PhiKnownSignTrue) {
+ parseAssembly(
+ "declare float @llvm.fabs.f32(float)"
+ "define float @test(i1 %cond, float nofpclass(nan) %arg0, float %arg1) {\n"
+ "entry:\n"
+ " br i1 %cond, label %bb0, label %bb1\n"
+ "bb0:\n"
+ " %fabs.arg0 = call float @llvm.fabs.f32(float %arg0)\n"
+ " %fneg.fabs.arg0 = fneg float %fabs.arg0\n"
+ " br label %ret\n"
+ "bb1:\n"
+ " %fabs.arg1 = call float @llvm.fabs.f32(float %arg1)\n"
+ " %fneg.fabs.arg1 = fneg float %fabs.arg1\n"
+ " br label %ret\n"
+ "ret:\n"
+ " %A = phi float [ %fneg.fabs.arg0, %bb0 ], [ %fneg.fabs.arg1, %bb1 ]\n"
+ " ret float %A\n"
+ "}\n");
+ expectKnownFPClass(fcNegative | fcNan, true);
+}
+
+TEST_F(ComputeKnownFPClassTest, UnreachablePhi) {
+ parseAssembly(
+ "define float @test(float %arg) {\n"
+ "entry:\n"
+ " ret float 0.0\n"
+ "unreachable:\n"
+ " %A = phi float\n"
+ " ret float %A\n"
+ "}\n");
+ expectKnownFPClass(fcAllFlags, std::nullopt);
+}
+
+TEST_F(ComputeKnownFPClassTest, SelfPhiOnly) {
+ parseAssembly(
+ "define float @test(float %arg) {\n"
+ "entry:\n"
+ " ret float 0.0\n"
+ "loop:\n"
+ " %A = phi float [ %A, %loop ]\n"
+ " br label %loop\n"
+ "}\n");
+ expectKnownFPClass(fcAllFlags, std::nullopt);
+}
+
+TEST_F(ComputeKnownFPClassTest, SelfPhiFirstArg) {
+ parseAssembly(
+ "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
+ "entry:\n"
+ " br i1 %cond, label %loop, label %ret\n"
+ "loop:\n"
+ " %A = phi float [ %arg, %entry ], [ %A, %loop ]\n"
+ " br label %loop\n"
+ "ret:\n"
+ " ret float %A"
+ "}\n");
+ expectKnownFPClass(~fcInf, std::nullopt);
+}
+
+TEST_F(ComputeKnownFPClassTest, SelfPhiSecondArg) {
+ parseAssembly(
+ "define float @test(i1 %cond, float nofpclass(inf) %arg) {\n"
+ "entry:\n"
+ " br i1 %cond, label %loop, label %ret\n"
+ "loop:\n"
+ " %A = phi float [ %A, %loop ], [ %arg, %entry ]\n"
+ " br label %loop\n"
+ "ret:\n"
+ " ret float %A"
+ "}\n");
+ expectKnownFPClass(~fcInf, std::nullopt);
+}
+
TEST_F(ComputeKnownFPClassTest, CannotBeOrderedLessThanZero) {
parseAssembly("define float @test(float %arg) {\n"
" %A = fmul float %arg, %arg"
More information about the llvm-commits
mailing list