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

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 18 10:16:04 PDT 2024


================
@@ -4018,5 +4018,30 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
   if (CondVal->getType() == SI.getType() && isKnownInversion(FalseVal, TrueVal))
     return BinaryOperator::CreateXor(CondVal, FalseVal);
 
+  if (SelType->isIntOrIntVectorTy()) {
+    // Try to simplify select arms based on KnownBits implied by the condition.
+    CondContext CC(CondVal);
+    findValuesAffectedByCondition(CondVal, /*IsAssume=*/false, [&](Value *V) {
+      CC.AffectedValues.insert(V);
+    });
----------------
goldsteinn wrote:

Maybe aggressive inst combine is the right place for this?
Maybe we could also track `select` instructions in DomConditionCache (although that would req some more complex logic to keep it updated as we created/delect select instructions).

Also, think for select you can at least limit `V` to values that are actually used by select arms.
Maybe create a set of uses up to Depth = 6 for each of the arms and only add affected values if they also hit that set? Otherwise can't really imagine this helping.

You might also try early out of `!isa<Constant>(TrueVal) && !isa<Constant>(FalseVal)` to avoid unnecessary setup.

Finally, maybe try simplifying both `True` and `False` arm in one go instead of essentially doubling the number of trips.

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


More information about the llvm-commits mailing list