Skip to content

Commit a9d77fd

Browse files
committed
[X86] Address reviews
1 parent f530899 commit a9d77fd

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48472,62 +48472,53 @@ static SDValue combineSetCCMOVMSK(SDValue EFLAGS, X86::CondCode &CC,
4847248472
return SDValue();
4847348473
}
4847448474

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.
4847848479
static SDValue combineSetCCTruncAdd(SDValue EFLAGS, X86::CondCode &CC,
4847948480
SelectionDAG &DAG) {
48481+
using namespace llvm::SDPatternMatch;
4848048482
if (!(CC == X86::COND_E || CC == X86::COND_NE || CC == X86::COND_AE ||
4848148483
CC == X86::COND_B))
4848248484
return SDValue();
4848348485

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();
4850648496

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();
4851148501

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();
4851748505

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();
4852248511

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);
4852948516

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));
4853148522
}
4853248523

4853348524
/// Optimize an EFLAGS definition used according to the condition code \p CC

0 commit comments

Comments
 (0)