@@ -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,133 @@ 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
+ unsigned Opcode = ARMISD::SUBC;
10687
+
10688
+ // Check if RHS is a subtraction against 0: (0 - X)
10689
+ if (RHS.getOpcode() == ISD::SUB) {
10690
+ SDValue SubLHS = RHS.getOperand(0);
10691
+ SDValue SubRHS = RHS.getOperand(1);
10692
+
10693
+ // Check if it's 0 - X
10694
+ if (isNullConstant(SubLHS)) {
10695
+ bool CanUseAdd = false;
10696
+ if (IsSigned) {
10697
+ // For SCMP: only if X is known to never be INT_MIN (to avoid overflow)
10698
+ if (RHS->getFlags().hasNoSignedWrap() || !DAG.computeKnownBits(SubRHS)
10699
+ .getSignedMinValue()
10700
+ .isMinSignedValue()) {
10701
+ CanUseAdd = true;
10702
+ }
10703
+ } else {
10704
+ // For UCMP: only if X is known to never be zero
10705
+ if (DAG.isKnownNeverZero(SubRHS)) {
10706
+ CanUseAdd = true;
10707
+ }
10708
+ }
10709
+
10710
+ if (CanUseAdd) {
10711
+ Opcode = ARMISD::ADDC;
10712
+ RHS = SubRHS; // Replace RHS with X, so we do LHS + X instead of
10713
+ // LHS - (0 - X)
10714
+ }
10715
+ }
10716
+ }
10717
+
10718
+ // Generate the operation with flags
10719
+ SDValue OpWithFlags;
10720
+ if (Opcode == ARMISD::ADDC) {
10721
+ // Use ADDC: LHS + RHS (where RHS was 0 - X, now X)
10722
+ OpWithFlags = DAG.getNode(ARMISD::ADDC, dl,
10723
+ DAG.getVTList(MVT::i32, FlagsVT), LHS, RHS);
10724
+ } else {
10725
+ // Use ARMISD::SUBC to generate SUBS instruction (subtract with flags)
10726
+ OpWithFlags = DAG.getNode(ARMISD::SUBC, dl,
10727
+ DAG.getVTList(MVT::i32, FlagsVT), LHS, RHS);
10728
+ }
10729
+
10730
+ SDValue OpResult = OpWithFlags.getValue(0); // The operation result
10731
+ SDValue Flags = OpWithFlags.getValue(1); // The flags
10732
+
10733
+ // Constants for conditional moves
10734
+ SDValue One = DAG.getConstant(1, dl, MVT::i32);
10735
+ SDValue MinusOne = DAG.getAllOnesConstant(dl, MVT::i32);
10736
+
10737
+ // Select condition codes based on signed vs unsigned
10738
+ ARMCC::CondCodes GTCond = IsSigned ? ARMCC::GT : ARMCC::HI;
10739
+ ARMCC::CondCodes LTCond = IsSigned ? ARMCC::LT : ARMCC::LO;
10740
+
10741
+ // First conditional move: if greater than, set to 1
10742
+ SDValue GTCondValue = DAG.getConstant(GTCond, dl, MVT::i32);
10743
+ SDValue Result1 = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, OpResult, One,
10744
+ GTCondValue, Flags);
10745
+
10746
+ // Second conditional move: if less than, set to -1
10747
+ SDValue LTCondValue = DAG.getConstant(LTCond, dl, MVT::i32);
10748
+ SDValue Result2 = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, Result1, MinusOne,
10749
+ LTCondValue, Flags);
10750
+
10751
+ if (Op.getValueType() != MVT::i32)
10752
+ Result2 = DAG.getSExtOrTrunc(Result2, dl, Op.getValueType());
10753
+
10754
+ return Result2;
10755
+ }
10756
+
10620
10757
SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10621
10758
LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump());
10622
10759
switch (Op.getOpcode()) {
@@ -10745,6 +10882,9 @@ SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10745
10882
case ISD::FP_TO_BF16:
10746
10883
return LowerFP_TO_BF16(Op, DAG);
10747
10884
case ARMISD::WIN__DBZCHK: return SDValue();
10885
+ case ISD::UCMP:
10886
+ case ISD::SCMP:
10887
+ return LowerCMP(Op, DAG);
10748
10888
}
10749
10889
}
10750
10890
0 commit comments