[llvm] [ValueTracking] Use select condition to help infer bits of arms (PR #84699)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 09:07:47 PDT 2024


================
@@ -1023,11 +1023,30 @@ static void computeKnownBitsFromOperator(const Operator *I,
     break;
   }
   case Instruction::Select: {
-    computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
-    computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
-
+    auto ComputeForArm = [&](bool Arm) {
+      unsigned ArmIdx = 1 + static_cast<unsigned>(!Arm);
+      KnownBits Res(Known.getBitWidth());
+      computeKnownBits(I->getOperand(ArmIdx), Res, Depth + 1, Q);
+      // See what condition implies about the bits of the two select arms.
+      if (!Known.isConstant()) {
+        KnownBits KnownFromCond(Known.getBitWidth());
+        computeKnownBitsFromCond(I->getOperand(ArmIdx), I->getOperand(0),
+                                 KnownFromCond, Depth + 1, Q, !Arm);
----------------
nikic wrote:

You need to consider a case where the instructions are not actually removed, e.g. https://alive2.llvm.org/ce/z/Fjkabr.

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


More information about the llvm-commits mailing list