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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 18 12:13:58 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);
+    });
----------------
nikic wrote:

As mentioned in the PR description, most of the overhead comes from simply adding a member to SimplifyQuery rather than anything happening in InstCombine. These are the numbers just for adding a dummy member: https://llvm-compile-time-tracker.com/compare.php?from=3ca17443ef4af21bdb1f3b4fbcfff672cbc6176c&to=e7a64d837a061d6afeac9c0f06c4827998d43561&stat=instructions:u

As such, I don't think we'll get any substantial improvement out of changing how exactly the KnownBits calculation is done.

> Maybe aggressive inst combine is the right place for this?

This would prevent extending this to use SimplifyDemandedBits, which enabled simplifying the expression without folding to constants (https://github.com/nikic/llvm-project/commit/1b8edbd1178b08fc544c6af4e1826a6b51262485). This will allow us to subsume special cases like https://github.com/llvm/llvm-project/pull/92658.

> 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).

I think managing invalidation for this would be quite tricky, and this approach would be inherently limited to one-use chains to the select only.

> Actually, think you really should only add `V` if its `TrueArm`/`FalseArm`.

Why? V may be used by TrueArm/FalseArm recursively.

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


More information about the llvm-commits mailing list