[llvm] [ValueTracking] Compute known bits from recursive select/phi (PR #113707)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 3 03:12:34 PST 2024


================
@@ -1566,6 +1566,22 @@ static void computeKnownBitsFromOperator(const Operator *I,
         // Skip direct self references.
         if (IncValue == P) continue;
 
+        // Recurse, but cap the recursion to one level, because we don't
+        // want to waste time spinning around in loops.
+        // TODO: See if we can base recursion limiter on number of incoming phi
+        // edges so we don't overly clamp analysis.
+        unsigned IncDepth = MaxAnalysisRecursionDepth - 1;
+
+        // If the Use is a select of this phi, use the knownbit of the other
+        // operand to break the recursion.
+        if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
+          if (SI->getTrueValue() == P || SI->getFalseValue() == P) {
+            IncValue = SI->getTrueValue() == P ? SI->getFalseValue()
+                                               : SI->getTrueValue();
+            IncDepth = Depth + 1;
----------------
nikic wrote:

Forwarding the question from https://github.com/llvm/llvm-project/pull/114689#pullrequestreview-2411822612 because the issue was introduced here: Why do we use the full depth for the select case?

As far as I can tell, a) there may be multiple incoming values in this form, resulting in a fan-out problem and b) we are not actually guaranteed that this breaks recursion (because the other select operand may also be based on the phi *indirectly*) so the fan-out may be recursive as well.

https://github.com/llvm/llvm-project/pull/113707


More information about the llvm-commits mailing list