@@ -48472,62 +48472,53 @@ static SDValue combineSetCCMOVMSK(SDValue EFLAGS, X86::CondCode &CC,
48472
48472
return SDValue();
48473
48473
}
48474
48474
48475
- // Attempt to fold some (truncate (srl (add X, C1), C2)) patterns to
48476
- // (add (truncate (srl X, C2), C1')). C1' will be smaller than C1 so we are able
48477
- // to avoid generating code with MOVABS and large constants in certain cases.
48475
+ // Attempt to fold some (setcc (sub (truncate (srl (add X, C1), C2)), C3), CC)
48476
+ // patterns to (setcc (cmp (add (truncate (srl X, C2)), C1'), C3), CC). C1' will
48477
+ // be smaller than C1 so we are able to avoid generating code with MOVABS and
48478
+ // large constants in certain cases.
48478
48479
static SDValue combineSetCCTruncAdd(SDValue EFLAGS, X86::CondCode &CC,
48479
48480
SelectionDAG &DAG) {
48481
+ using namespace llvm::SDPatternMatch;
48480
48482
if (!(CC == X86::COND_E || CC == X86::COND_NE || CC == X86::COND_AE ||
48481
48483
CC == X86::COND_B))
48482
48484
return SDValue();
48483
48485
48484
- EVT VT = EFLAGS.getValueType();
48485
- if (EFLAGS.getOpcode() == X86ISD::SUB && VT == MVT::i32) {
48486
- SDValue CmpLHS = EFLAGS.getOperand(0);
48487
- auto *CmpConstant = dyn_cast<ConstantSDNode>(EFLAGS.getOperand(1));
48488
-
48489
- if (CmpLHS.getOpcode() != ISD::TRUNCATE || !CmpConstant)
48490
- return SDValue();
48491
-
48492
- SDValue Srl = CmpLHS.getOperand(0);
48493
- EVT SrlVT = Srl.getValueType();
48494
- if (Srl.getOpcode() != ISD::SRL || SrlVT != MVT::i64)
48495
- return SDValue();
48496
-
48497
- SDValue Add = Srl.getOperand(0);
48498
- // Avoid changing the ADD if it is used elsewhere.
48499
- if (Add.getOpcode() != ISD::ADD || !Add.hasOneUse())
48500
- return SDValue();
48501
-
48502
- auto *AddConstant = dyn_cast<ConstantSDNode>(Add.getOperand(1));
48503
- auto *SrlConstant = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
48504
- if (!AddConstant || !SrlConstant)
48505
- return SDValue();
48486
+ SDValue AddLhs;
48487
+ APInt AddConst, SrlConst, CmpConst;
48488
+ if (!sd_match(EFLAGS,
48489
+ m_AllOf(m_SpecificVT(MVT::i32),
48490
+ m_BinOp(X86ISD::SUB,
48491
+ m_Trunc(m_Srl(m_Add(m_Value(AddLhs),
48492
+ m_ConstInt(AddConst)),
48493
+ m_ConstInt(SrlConst))),
48494
+ m_ConstInt(CmpConst)))))
48495
+ return SDValue();
48506
48496
48507
- APInt AddConstVal = AddConstant->getAPIntValue() ;
48508
- APInt SrlConstVal = SrlConstant->getAPIntValue();
48509
- if (!SrlConstVal.ugt(VT.getSizeInBits( )))
48510
- return SDValue();
48497
+ SDValue Srl ;
48498
+ if (!sd_match(EFLAGS.getOperand(0).getOperand(0),
48499
+ m_AllOf(m_SpecificVT(MVT::i64), m_Value(Srl) )))
48500
+ return SDValue();
48511
48501
48512
- APInt CmpConstVal = CmpConstant->getAPIntValue();
48513
- APInt ShiftedAddConst = AddConstVal.lshr(SrlConstVal);
48514
- if (!CmpConstVal.ult(ShiftedAddConst.trunc(VT.getSizeInBits())) ||
48515
- (ShiftedAddConst.shl(SrlConstVal)) != AddConstVal)
48516
- return SDValue();
48502
+ // Avoid changing the ADD if it is used elsewhere.
48503
+ if (!Srl.getOperand(0).hasOneUse())
48504
+ return SDValue();
48517
48505
48518
- SDLoc DL(EFLAGS);
48519
- SDValue AddLHSSrl =
48520
- DAG.getNode(ISD::SRL, DL, SrlVT, Add.getOperand(0), Srl.getOperand(1));
48521
- SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, VT, AddLHSSrl);
48506
+ EVT VT = EFLAGS.getValueType();
48507
+ APInt ShiftedAddConst = AddConst.lshr(SrlConst);
48508
+ if (!CmpConst.ult(ShiftedAddConst.trunc(VT.getSizeInBits())) ||
48509
+ (ShiftedAddConst.shl(SrlConst)) != AddConst)
48510
+ return SDValue();
48522
48511
48523
- APInt NewAddConstVal =
48524
- (~((~AddConstVal).lshr(SrlConstVal))).trunc(VT.getSizeInBits());
48525
- SDValue NewAddConst = DAG.getConstant(NewAddConstVal, DL, VT);
48526
- SDValue NewAddNode = DAG.getNode(ISD::ADD, DL, VT, Trunc, NewAddConst);
48527
- return DAG.getNode(X86ISD::CMP, DL, VT, NewAddNode, EFLAGS.getOperand(1));
48528
- }
48512
+ SDLoc DL(EFLAGS);
48513
+ SDValue AddLHSSrl =
48514
+ DAG.getNode(ISD::SRL, DL, MVT::i64, AddLhs, Srl.getOperand(1));
48515
+ SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, VT, AddLHSSrl);
48529
48516
48530
- return SDValue();
48517
+ APInt NewAddConstVal =
48518
+ (~((~AddConst).lshr(SrlConst))).trunc(VT.getSizeInBits());
48519
+ SDValue NewAddConst = DAG.getConstant(NewAddConstVal, DL, VT);
48520
+ SDValue NewAddNode = DAG.getNode(ISD::ADD, DL, VT, Trunc, NewAddConst);
48521
+ return DAG.getNode(X86ISD::CMP, DL, VT, NewAddNode, EFLAGS.getOperand(1));
48531
48522
}
48532
48523
48533
48524
/// Optimize an EFLAGS definition used according to the condition code \p CC
0 commit comments