@@ -807,6 +807,12 @@ ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM_,
807
807
setOperationAction(ISD::BSWAP, VT, Expand);
808
808
}
809
809
810
+ if (!Subtarget->isThumb1Only() && !Subtarget->hasV8_1MMainlineOps())
811
+ setOperationAction(ISD::SCMP, MVT::i32, Custom);
812
+
813
+ if (!Subtarget->hasV8_1MMainlineOps())
814
+ setOperationAction(ISD::UCMP, MVT::i32, Custom);
815
+
810
816
setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
811
817
setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
812
818
@@ -1639,6 +1645,10 @@ bool ARMTargetLowering::useSoftFloat() const {
1639
1645
return Subtarget->useSoftFloat();
1640
1646
}
1641
1647
1648
+ bool ARMTargetLowering::shouldExpandCmpUsingSelects(EVT VT) const {
1649
+ return !Subtarget->isThumb1Only() && VT.getSizeInBits() <= 32;
1650
+ }
1651
+
1642
1652
// FIXME: It might make sense to define the representative register class as the
1643
1653
// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1644
1654
// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
@@ -10617,6 +10627,134 @@ SDValue ARMTargetLowering::LowerFP_TO_BF16(SDValue Op,
10617
10627
return DAG.getBitcast(MVT::i32, Res);
10618
10628
}
10619
10629
10630
+ SDValue ARMTargetLowering::LowerCMP(SDValue Op, SelectionDAG &DAG) const {
10631
+ SDLoc dl(Op);
10632
+ SDValue LHS = Op.getOperand(0);
10633
+ SDValue RHS = Op.getOperand(1);
10634
+
10635
+ // Determine if this is signed or unsigned comparison
10636
+ bool IsSigned = (Op.getOpcode() == ISD::SCMP);
10637
+
10638
+ // Special case for Thumb1 UCMP only
10639
+ if (!IsSigned && Subtarget->isThumb1Only()) {
10640
+ // For Thumb unsigned comparison, use this sequence:
10641
+ // subs r2, r0, r1 ; r2 = LHS - RHS, sets flags
10642
+ // sbc r2, r2 ; r2 = r2 - r2 - !carry
10643
+ // cmp r1, r0 ; compare RHS with LHS
10644
+ // sbc r1, r1 ; r1 = r1 - r1 - !carry
10645
+ // subs r0, r2, r1 ; r0 = r2 - r1 (final result)
10646
+
10647
+ // First subtraction: LHS - RHS
10648
+ SDValue Sub1WithFlags = DAG.getNode(
10649
+ ARMISD::SUBC, dl, DAG.getVTList(MVT::i32, FlagsVT), LHS, RHS);
10650
+ SDValue Sub1Result = Sub1WithFlags.getValue(0);
10651
+ SDValue Flags1 = Sub1WithFlags.getValue(1);
10652
+
10653
+ // SUBE: Sub1Result - Sub1Result - !carry
10654
+ // This gives 0 if LHS >= RHS (unsigned), -1 if LHS < RHS (unsigned)
10655
+ SDValue Sbc1 =
10656
+ DAG.getNode(ARMISD::SUBE, dl, DAG.getVTList(MVT::i32, FlagsVT),
10657
+ Sub1Result, Sub1Result, Flags1);
10658
+ SDValue Sbc1Result = Sbc1.getValue(0);
10659
+
10660
+ // Second comparison: RHS vs LHS (reverse comparison)
10661
+ SDValue CmpFlags = DAG.getNode(ARMISD::CMP, dl, FlagsVT, RHS, LHS);
10662
+
10663
+ // SUBE: RHS - RHS - !carry
10664
+ // This gives 0 if RHS <= LHS (unsigned), -1 if RHS > LHS (unsigned)
10665
+ SDValue Sbc2 = DAG.getNode(
10666
+ ARMISD::SUBE, dl, DAG.getVTList(MVT::i32, FlagsVT), RHS, RHS, CmpFlags);
10667
+ SDValue Sbc2Result = Sbc2.getValue(0);
10668
+
10669
+ // Final subtraction: Sbc1Result - Sbc2Result (no flags needed)
10670
+ SDValue Result =
10671
+ DAG.getNode(ISD::SUB, dl, MVT::i32, Sbc1Result, Sbc2Result);
10672
+ if (Op.getValueType() != MVT::i32)
10673
+ Result = DAG.getSExtOrTrunc(Result, dl, Op.getValueType());
10674
+
10675
+ return Result;
10676
+ }
10677
+
10678
+ // For the ARM assembly pattern:
10679
+ // subs r0, r0, r1 ; subtract RHS from LHS and set flags
10680
+ // movgt r0, #1 ; if LHS > RHS, set result to 1 (GT for signed, HI for
10681
+ // unsigned) mvnlt r0, #0 ; if LHS < RHS, set result to -1 (LT for
10682
+ // signed, LO for unsigned)
10683
+ // ; if LHS == RHS, result remains 0 from the subs
10684
+
10685
+ // Optimization: if RHS is a subtraction against 0, use ADDC instead of SUBC
10686
+ SDValue AddOperand;
10687
+ unsigned Opcode = ARMISD::SUBC;
10688
+
10689
+ // Check if RHS is a subtraction against 0: (0 - X)
10690
+ if (RHS.getOpcode() == ISD::SUB) {
10691
+ SDValue SubLHS = RHS.getOperand(0);
10692
+ SDValue SubRHS = RHS.getOperand(1);
10693
+
10694
+ // Check if it's 0 - X
10695
+ if (isNullConstant(SubLHS)) {
10696
+ bool CanUseAdd = false;
10697
+ if (IsSigned) {
10698
+ // For SCMP: only if X is known to never be INT_MIN (to avoid overflow)
10699
+ if (RHS->getFlags().hasNoSignedWrap() || !DAG.computeKnownBits(SubRHS)
10700
+ .getSignedMinValue()
10701
+ .isMinSignedValue()) {
10702
+ CanUseAdd = true;
10703
+ }
10704
+ } else {
10705
+ // For UCMP: only if X is known to never be zero
10706
+ if (DAG.isKnownNeverZero(SubRHS)) {
10707
+ CanUseAdd = true;
10708
+ }
10709
+ }
10710
+
10711
+ if (CanUseAdd) {
10712
+ Opcode = ARMISD::ADDC;
10713
+ AddOperand = SubRHS; // Replace RHS with X, so we do LHS + X instead of
10714
+ // LHS - (0 - X)
10715
+ }
10716
+ }
10717
+ }
10718
+
10719
+ // Generate the operation with flags
10720
+ SDValue OpWithFlags;
10721
+ if (Opcode == ARMISD::ADDC) {
10722
+ // Use ADDC: LHS + AddOperand (where RHS was 0 - AddOperand)
10723
+ OpWithFlags = DAG.getNode(
10724
+ ARMISD::ADDC, dl, DAG.getVTList(MVT::i32, FlagsVT), LHS, AddOperand);
10725
+ } else {
10726
+ // Use ARMISD::SUBC to generate SUBS instruction (subtract with flags)
10727
+ OpWithFlags = DAG.getNode(ARMISD::SUBC, dl,
10728
+ DAG.getVTList(MVT::i32, FlagsVT), LHS, RHS);
10729
+ }
10730
+
10731
+ SDValue OpResult = OpWithFlags.getValue(0); // The operation result
10732
+ SDValue Flags = OpWithFlags.getValue(1); // The flags
10733
+
10734
+ // Constants for conditional moves
10735
+ SDValue One = DAG.getConstant(1, dl, MVT::i32);
10736
+ SDValue MinusOne = DAG.getAllOnesConstant(dl, MVT::i32);
10737
+
10738
+ // Select condition codes based on signed vs unsigned
10739
+ ARMCC::CondCodes GTCond = IsSigned ? ARMCC::GT : ARMCC::HI;
10740
+ ARMCC::CondCodes LTCond = IsSigned ? ARMCC::LT : ARMCC::LO;
10741
+
10742
+ // First conditional move: if greater than, set to 1
10743
+ SDValue GTCondValue = DAG.getConstant(GTCond, dl, MVT::i32);
10744
+ SDValue Result1 = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, OpResult, One,
10745
+ GTCondValue, Flags);
10746
+
10747
+ // Second conditional move: if less than, set to -1
10748
+ SDValue LTCondValue = DAG.getConstant(LTCond, dl, MVT::i32);
10749
+ SDValue Result2 = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, Result1, MinusOne,
10750
+ LTCondValue, Flags);
10751
+
10752
+ if (Op.getValueType() != MVT::i32)
10753
+ Result2 = DAG.getSExtOrTrunc(Result2, dl, Op.getValueType());
10754
+
10755
+ return Result2;
10756
+ }
10757
+
10620
10758
SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10621
10759
LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump());
10622
10760
switch (Op.getOpcode()) {
@@ -10745,6 +10883,9 @@ SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10745
10883
case ISD::FP_TO_BF16:
10746
10884
return LowerFP_TO_BF16(Op, DAG);
10747
10885
case ARMISD::WIN__DBZCHK: return SDValue();
10886
+ case ISD::UCMP:
10887
+ case ISD::SCMP:
10888
+ return LowerCMP(Op, DAG);
10748
10889
}
10749
10890
}
10750
10891
0 commit comments