From: Matt Arsenault Date: Sat, 29 Apr 2023 23:31:01 +0000 (-0400) Subject: ValueTracking: Handle phi in computeKnownFPClass X-Git-Tag: upstream/17.0.6~8049 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cac9e427eb1ff3dabda8ac08968b998c3bc5ab47;p=platform%2Fupstream%2Fllvm.git ValueTracking: Handle phi in computeKnownFPClass Doesn't try the all the tricks computeKnownBits does. --- diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 3b70029..906ae81 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -5094,6 +5094,47 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts, Known, Depth + 1, Q, TLI); break; } + case Instruction::PHI: { + const PHINode *P = cast(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. + if (Depth < MaxAnalysisRecursionDepth - 1) { + // Skip if every incoming value references to ourself. + if (isa_and_nonnull(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, + MaxAnalysisRecursionDepth - 2, 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 c83fb62..3684f6f 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"