[llvm] [SelectionDAG] Simplify vselect true, T, F -> T (PR #100992)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 02:35:28 PDT 2024


================
@@ -9927,13 +9927,27 @@ SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {
   if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
     return CondC->isZero() ? F : T;
 
-  // TODO: This should simplify VSELECT with non-zero constant condition using
-  // something like this (but check boolean contents to be complete?):
   if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
-                                                  /*AllowTruncation*/ true))
+                                                  /*AllowTruncation*/ true)) {
     if (CondC->isZero())
       return F;
 
+    switch (TLI->getBooleanContents(Cond.getValueType())) {
+    case TargetLowering::UndefinedBooleanContent:
+      if (CondC->getAPIntValue()[0])
+        return T;
+      break;
+    case TargetLowering::ZeroOrOneBooleanContent:
+      if (CondC->isOne())
+        return T;
+      break;
+    case TargetLowering::ZeroOrNegativeOneBooleanContent:
+      if (CondC->isAllOnes())
+        return T;
+      break;
----------------
RKSimon wrote:

Not sure if its worth it but we already do this in at least one other place I can recall (extractBooleanFlip) - we could add a `SelectionDAG::isBoolConstant(SDValue Cond, bool V)` helper to handle this?

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


More information about the llvm-commits mailing list