@@ -4839,14 +4839,44 @@ static bool isFloatingPointZero(SDValue Op) {
4839
4839
return false;
4840
4840
}
4841
4841
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
+
4842
4867
/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
4843
4868
/// the given operands.
4844
4869
SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
4845
4870
SDValue &ARMcc, SelectionDAG &DAG,
4846
4871
const SDLoc &dl) const {
4847
4872
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, VT);
4879
+ } else if (!isLegalICmpImmediate((int32_t)C)) {
4850
4880
// Constant does not fit, try adjusting it by one.
4851
4881
switch (CC) {
4852
4882
default: break;
0 commit comments