Skip to content

Commit 75233a5

Browse files
committed
[ARM] Port shouldBeAdjustedToZero to ARM
1 parent 320f682 commit 75233a5

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,14 +4839,44 @@ static bool isFloatingPointZero(SDValue Op) {
48394839
return false;
48404840
}
48414841

4842+
// TODO: This is copied from AArch64TargetLowering.cpp, which only has
4843+
// ands, subs, and adds affecting flags. In ARM, we have more than that, so this
4844+
// should be expanded to cover all the cases where we can adjust the condition
4845+
// code to zero.
4846+
static bool shouldBeAdjustedToZero(SDValue LHS, APInt C, ISD::CondCode &CC) {
4847+
// Special case where we can use pl or mi instead of lt or ge.
4848+
// Any instruction that sets flags can be optimized to use mi or pl
4849+
// this way.
4850+
if (C.isAllOnes() && (CC == ISD::SETLE || CC == ISD::SETGT)) {
4851+
CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
4852+
return true;
4853+
}
4854+
4855+
// TODO: Cover all cases where a comparison with 0 would be profitable...
4856+
if (LHS.getOpcode() != ISD::AND)
4857+
return false;
4858+
4859+
if (C.isOne() && (CC == ISD::SETLT || CC == ISD::SETGE)) {
4860+
CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
4861+
return true;
4862+
}
4863+
4864+
return false;
4865+
}
4866+
48424867
/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
48434868
/// the given operands.
48444869
SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
48454870
SDValue &ARMcc, SelectionDAG &DAG,
48464871
const SDLoc &dl) const {
48474872
if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
4848-
unsigned C = RHSC->getZExtValue();
4849-
if (!isLegalICmpImmediate((int32_t)C)) {
4873+
APInt CInt = RHSC->getAPIntValue();
4874+
unsigned C = CInt.getZExtValue();
4875+
if (shouldBeAdjustedToZero(LHS, CInt, CC)) {
4876+
// Adjust the constant to zero.
4877+
// CC has already been adjusted.
4878+
RHS = DAG.getConstant(0, dl, MVT::i32);
4879+
} else if (!isLegalICmpImmediate((int32_t)C)) {
48504880
// Constant does not fit, try adjusting it by one.
48514881
switch (CC) {
48524882
default: break;

0 commit comments

Comments
 (0)