[llvm] [InstCombine] Simplify select using KnownBits of condition (PR #95923)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 00:18:29 PDT 2024


================
@@ -3520,6 +3520,33 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
   return false;
 }
 
+/// Check whether the KnownBits of a select arm may be affected by the
+/// select condition.
+static bool hasAffectedValue(Value *V, SmallPtrSetImpl<Value *> &Affected,
+                             unsigned Depth) {
+  if (Depth == MaxAnalysisRecursionDepth)
+    return false;
+
+  // Ignore the case where the select arm itself is affected. These cases
+  // are handled more efficiently by other optimizations.
+  if (Affected.contains(V) && Depth != 0)
+    return true;
+
+  if (auto *I = dyn_cast<Instruction>(V)) {
+    if (isa<PHINode>(I)) {
+      if (Depth == MaxAnalysisRecursionDepth - 1)
+        return false;
+      Depth = MaxAnalysisRecursionDepth - 2;
+    }
+    return any_of(I->operands(), [&](Value *Op) {
+      return Op->getType()->isIntOrIntVectorTy() &&
+             hasAffectedValue(Op, Affected, Depth + 1);
----------------
goldsteinn wrote:

As in, in `computeKnownBits`, by a depth of two, you will no longer hit any values in "Affected" (or at least rarely, if you hit any they will not be by-chance from multi-use). So it will be similiar to a normal `computeKnownBits` call which is not what this patch is really after.
I'm not sure if, however, if you need to normal `computeKnownBits` to provide some extra bits to fill in the constants.

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


More information about the llvm-commits mailing list