[llvm] [SelectionDAG] Detect impossible conditions using known bits analysis (PR #150715)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 25 15:57:08 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 9dbfcb146..08f4f4656 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -13534,48 +13534,68 @@ SDValue DAGCombiner::visitSETCC(SDNode *N) {
ConstantSDNode *N1C = cast<ConstantSDNode>(N1);
APInt C1 = N1C->getAPIntValue();
KnownBits KnownRHS = KnownBits::makeConstant(C1);
-
+
// Bail out early if RHS is unknown (shouldn't happen for constants)
if (KnownRHS.isUnknown())
return SDValue();
-
+
std::optional<bool> KnownVal;
-
+
// Handle special cases first (like GlobalISel does)
if (KnownRHS.isZero()) {
// x >=u 0 -> always true
- // x <u 0 -> always false
+ // x <u 0 -> always false
if (Cond == ISD::SETUGE)
KnownVal = true;
else if (Cond == ISD::SETULT)
KnownVal = false;
}
-
+
// If not handled by special cases, use ICmpInst::compare
if (!KnownVal) {
KnownBits KnownLHS = DAG.computeKnownBits(N0);
-
+
// Convert ISD::CondCode to CmpInst::Predicate
CmpInst::Predicate Pred;
switch (Cond) {
- case ISD::SETEQ: Pred = CmpInst::ICMP_EQ; break;
- case ISD::SETNE: Pred = CmpInst::ICMP_NE; break;
- case ISD::SETULT: Pred = CmpInst::ICMP_ULT; break;
- case ISD::SETULE: Pred = CmpInst::ICMP_ULE; break;
- case ISD::SETUGT: Pred = CmpInst::ICMP_UGT; break;
- case ISD::SETUGE: Pred = CmpInst::ICMP_UGE; break;
- case ISD::SETLT: Pred = CmpInst::ICMP_SLT; break;
- case ISD::SETLE: Pred = CmpInst::ICMP_SLE; break;
- case ISD::SETGT: Pred = CmpInst::ICMP_SGT; break;
- case ISD::SETGE: Pred = CmpInst::ICMP_SGE; break;
- default:
+ case ISD::SETEQ:
+ Pred = CmpInst::ICMP_EQ;
+ break;
+ case ISD::SETNE:
+ Pred = CmpInst::ICMP_NE;
+ break;
+ case ISD::SETULT:
+ Pred = CmpInst::ICMP_ULT;
+ break;
+ case ISD::SETULE:
+ Pred = CmpInst::ICMP_ULE;
+ break;
+ case ISD::SETUGT:
+ Pred = CmpInst::ICMP_UGT;
+ break;
+ case ISD::SETUGE:
+ Pred = CmpInst::ICMP_UGE;
+ break;
+ case ISD::SETLT:
+ Pred = CmpInst::ICMP_SLT;
+ break;
+ case ISD::SETLE:
+ Pred = CmpInst::ICMP_SLE;
+ break;
+ case ISD::SETGT:
+ Pred = CmpInst::ICMP_SGT;
+ break;
+ case ISD::SETGE:
+ Pred = CmpInst::ICMP_SGE;
+ break;
+ default:
return SDValue(); // Unsupported predicate
}
-
+
// Use the same logic as GlobalISel: ICmpInst::compare
KnownVal = ICmpInst::compare(KnownLHS, KnownRHS, Pred);
}
-
+
// If the comparison result is known, replace with constant
if (KnownVal)
return DAG.getConstant(*KnownVal ? 1 : 0, DL, VT);
``````````
</details>
https://github.com/llvm/llvm-project/pull/150715
More information about the llvm-commits
mailing list