[llvm] [X86][CodeGen] Support lowering for CCMP/CTEST (PR #91747)
Shengchen Kan via llvm-commits
llvm-commits at lists.llvm.org
Sat May 25 22:11:22 PDT 2024
================
@@ -49217,6 +49228,148 @@ static SDValue combineBMILogicOp(SDNode *N, SelectionDAG &DAG,
return SDValue();
}
+static SDValue combineX86SubCmpForFlags(SDNode *N, SDValue Flag,
+ SelectionDAG &DAG,
+ TargetLowering::DAGCombinerInfo &DCI,
+ const X86Subtarget &ST) {
+ // cmp(setcc(cc, X), 0)
+ // brcond ne
+ // ->
+ // X
+ // brcond cc
+
+ // sub(setcc(cc, X), 1)
+ // brcond ne
+ // ->
+ // X
+ // brcond ~cc
+ //
+ // if only flag has users
+
+ SDValue SetCC = N->getOperand(0);
+
+ // TODO: Remove the check hasCCMP() and update the non-APX tests.
+ if (!ST.hasCCMP() || SetCC.getOpcode() != X86ISD::SETCC || !Flag.hasOneUse())
+ return SDValue();
+
+ // Check the only user of flag is `brcond ne`.
+ SDNode *BrCond = *Flag->uses().begin();
+ if (BrCond->getOpcode() != X86ISD::BRCOND)
+ return SDValue();
+ unsigned CondNo = 2;
+ if (static_cast<X86::CondCode>(BrCond->getConstantOperandVal(CondNo)) !=
+ X86::COND_NE)
+ return SDValue();
+
+ SDValue X = SetCC.getOperand(1);
+ // Replace API is called manually here b/c the number of results may change.
+ DAG.ReplaceAllUsesOfValueWith(Flag, X);
+
+ SDValue CCN = SetCC.getOperand(0);
+ X86::CondCode CC =
+ static_cast<X86::CondCode>(CCN->getAsAPIntVal().getSExtValue());
+ X86::CondCode OppositeCC = X86::GetOppositeBranchCondition(CC);
+ // Update CC for the consumer of the flag.
+ // The old CC is `ne`. Hence, when comparing the result with 0, we are
+ // checking if the second condition evaluates to true. When comparing the
+ // result with 1, we are checking uf the second condition evaluates to false.
+ SmallVector<SDValue> Ops(BrCond->op_values());
+ if (isNullConstant(N->getOperand(1)))
+ Ops[CondNo] = CCN;
+ else if (isOneConstant(N->getOperand(1)))
+ Ops[CondNo] = DAG.getTargetConstant(OppositeCC, SDLoc(BrCond), MVT::i8);
+
+ SDValue NewBrCond =
+ DAG.getNode(X86ISD::BRCOND, SDLoc(BrCond), BrCond->getValueType(0), Ops);
+ // Avoid self-assign error b/c CC1 can be `e/ne`.
+ // Replace API is called manually here b/c we're updating the user of the node
+ // being visited instead of the node itself.
+ if (BrCond != NewBrCond.getNode()) {
+ DAG.ReplaceAllUsesWith(BrCond, &NewBrCond);
----------------
KanRobert wrote:
I believe it's correct. You can find similar usage in `lowerShufflePairAsUNPCKAndPermute`
https://github.com/llvm/llvm-project/pull/91747
More information about the llvm-commits
mailing list