@@ -4846,94 +4846,11 @@ AMDGPUTargetLowering::foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
48464846 return SDValue ();
48474847}
48484848
4849- // Detect when CMP and SELECT use the same constant and fold them to avoid
4850- // loading the constant twice. Specifically handles patterns like:
4851- // %cmp = icmp eq i32 %val, 4242
4852- // %sel = select i1 %cmp, i32 4242, i32 %other
4853- // It can be optimized to reuse %val instead of 4242 in select.
4854- static SDValue
4855- foldCmpSelectWithSharedConstant (SDNode *N, TargetLowering::DAGCombinerInfo &DCI,
4856- const AMDGPUSubtarget *ST) {
4857- SDValue Cond = N->getOperand (0 );
4858- SDValue TrueVal = N->getOperand (1 );
4859- SDValue FalseVal = N->getOperand (2 );
4860-
4861- // Check if condition is a comparison.
4862- if (Cond.getOpcode () != ISD::SETCC)
4863- return SDValue ();
4864-
4865- SDValue LHS = Cond.getOperand (0 );
4866- SDValue RHS = Cond.getOperand (1 );
4867- ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand (2 ))->get ();
4868-
4869- bool isFloatingPoint = LHS.getValueType ().isFloatingPoint ();
4870- bool isInteger = LHS.getValueType ().isInteger ();
4871-
4872- // Handle simple floating-point and integer types only.
4873- if (!isFloatingPoint && !isInteger)
4874- return SDValue ();
4875-
4876- bool isEquality = CC == (isFloatingPoint ? ISD::SETOEQ : ISD::SETEQ);
4877- bool isNonEquality = CC == (isFloatingPoint ? ISD::SETONE : ISD::SETNE);
4878- if (!isEquality && !isNonEquality)
4879- return SDValue ();
4880-
4881- SDValue ArgVal, ConstVal;
4882- if ((isFloatingPoint && isa<ConstantFPSDNode>(RHS)) ||
4883- (isInteger && isa<ConstantSDNode>(RHS))) {
4884- ConstVal = RHS;
4885- ArgVal = LHS;
4886- } else if ((isFloatingPoint && isa<ConstantFPSDNode>(LHS)) ||
4887- (isInteger && isa<ConstantSDNode>(LHS))) {
4888- ConstVal = LHS;
4889- ArgVal = RHS;
4890- } else {
4891- return SDValue ();
4892- }
4893-
4894- // Check if constant should not be optimized - early return if not.
4895- if (isFloatingPoint) {
4896- const APFloat &Val = cast<ConstantFPSDNode>(ConstVal)->getValueAPF ();
4897- const GCNSubtarget *GCNST = static_cast <const GCNSubtarget *>(ST);
4898-
4899- // Only optimize normal floating-point values (finite, non-zero, and
4900- // non-subnormal as per IEEE 754), skip optimization for inlinable
4901- // floating-point constants.
4902- if (!Val.isNormal () || GCNST->getInstrInfo ()->isInlineConstant (Val))
4903- return SDValue ();
4904- } else {
4905- int64_t IntVal = cast<ConstantSDNode>(ConstVal)->getSExtValue ();
4906-
4907- // Skip optimization for inlinable integer immediates.
4908- // Inlinable immediates include: -16 to 64 (inclusive).
4909- if (IntVal >= -16 && IntVal <= 64 )
4910- return SDValue ();
4911- }
4912-
4913- // For equality and non-equality comparisons, patterns:
4914- // select (setcc x, const), const, y -> select (setcc x, const), x, y
4915- // select (setccinv x, const), y, const -> select (setccinv x, const), y, x
4916- if (!(isEquality && TrueVal == ConstVal) &&
4917- !(isNonEquality && FalseVal == ConstVal))
4918- return SDValue ();
4919-
4920- SDValue SelectLHS = (isEquality && TrueVal == ConstVal) ? ArgVal : TrueVal;
4921- SDValue SelectRHS =
4922- (isNonEquality && FalseVal == ConstVal) ? ArgVal : FalseVal;
4923- return DCI.DAG .getNode (ISD::SELECT, SDLoc (N), N->getValueType (0 ), Cond,
4924- SelectLHS, SelectRHS);
4925- }
4926-
49274849SDValue AMDGPUTargetLowering::performSelectCombine (SDNode *N,
49284850 DAGCombinerInfo &DCI) const {
49294851 if (SDValue Folded = foldFreeOpFromSelect (DCI, SDValue (N, 0 )))
49304852 return Folded;
49314853
4932- // Try to fold CMP + SELECT patterns with shared constants (both FP and
4933- // integer).
4934- if (SDValue Folded = foldCmpSelectWithSharedConstant (N, DCI, Subtarget))
4935- return Folded;
4936-
49374854 SDValue Cond = N->getOperand (0 );
49384855 if (Cond.getOpcode () != ISD::SETCC)
49394856 return SDValue ();
0 commit comments