@@ -740,19 +740,24 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
740
740
}
741
741
}
742
742
743
- static void getICMPOperandsForBranch (MachineInstr &MI, MachineIRBuilder &MIB,
744
- MachineRegisterInfo &MRI,
745
- RISCVCC::CondCode &CC, Register &LHS,
746
- Register &RHS) {
747
- assert (MI.getOpcode () == TargetOpcode::G_ICMP);
748
- CmpInst::Predicate ICMPCC =
749
- static_cast <CmpInst::Predicate>(MI.getOperand (1 ).getPredicate ());
750
- LHS = MI.getOperand (2 ).getReg ();
751
- RHS = MI.getOperand (3 ).getReg ();
743
+ static void getOperandsForBranch (Register CondReg, MachineIRBuilder &MIB,
744
+ MachineRegisterInfo &MRI,
745
+ RISCVCC::CondCode &CC, Register &LHS,
746
+ Register &RHS) {
747
+ // Try to fold an ICmp. If that fails, use a NE compare with X0.
748
+ CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
749
+ if (!mi_match (CondReg, MRI, m_GICmp (m_Pred (Pred), m_Reg (LHS), m_Reg (RHS)))) {
750
+ LHS = CondReg;
751
+ RHS = RISCV::X0;
752
+ CC = RISCVCC::COND_NE;
753
+ return ;
754
+ }
755
+
756
+ // We found an ICmp, do some canonicalizations.
752
757
753
758
// Adjust comparisons to use comparison with 0 if possible.
754
759
if (auto Constant = getIConstantVRegSExtVal (RHS, MRI)) {
755
- switch (ICMPCC ) {
760
+ switch (Pred ) {
756
761
case CmpInst::Predicate::ICMP_SGT:
757
762
// Convert X > -1 to X >= 0
758
763
if (*Constant == -1 ) {
@@ -775,7 +780,7 @@ static void getICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
775
780
}
776
781
}
777
782
778
- switch (ICMPCC ) {
783
+ switch (Pred ) {
779
784
default :
780
785
llvm_unreachable (" Expected ICMP CmpInst::Predicate." );
781
786
case CmpInst::Predicate::ICMP_EQ:
@@ -785,33 +790,30 @@ static void getICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
785
790
case CmpInst::Predicate::ICMP_UGE:
786
791
case CmpInst::Predicate::ICMP_SGE:
787
792
// These CCs are supported directly by RISC-V branches.
788
- CC = getRISCVCCFromICMP (ICMPCC);
789
- return ;
793
+ break ;
790
794
case CmpInst::Predicate::ICMP_SGT:
791
795
case CmpInst::Predicate::ICMP_SLE:
792
796
case CmpInst::Predicate::ICMP_UGT:
793
797
case CmpInst::Predicate::ICMP_ULE:
794
798
// These CCs are not supported directly by RISC-V branches, but changing the
795
799
// direction of the CC and swapping LHS and RHS are.
796
- CC = getRISCVCCFromICMP ( CmpInst::getSwappedPredicate (ICMPCC) );
800
+ Pred = CmpInst::getSwappedPredicate (Pred );
797
801
std::swap (LHS, RHS);
798
- return ;
802
+ break ;
799
803
}
804
+
805
+ CC = getRISCVCCFromICMP (Pred);
806
+ return ;
800
807
}
801
808
802
809
bool RISCVInstructionSelector::selectSelect (MachineInstr &MI,
803
810
MachineIRBuilder &MIB,
804
811
MachineRegisterInfo &MRI) const {
805
812
auto &SelectMI = cast<GSelect>(MI);
806
813
807
- // If MI is a G_SELECT(G_ICMP(tst, A, B), C, D) then we can use (A, B, tst)
808
- // as the (LHS, RHS, CC) of the Select_GPR_Using_CC_GPR.
809
- Register LHS = SelectMI.getCondReg ();
810
- Register RHS = RISCV::X0;
811
- RISCVCC::CondCode CC = RISCVCC::COND_NE;
812
-
813
- if (mi_match (LHS, MRI, m_GICmp (m_Pred (), m_Reg (), m_Reg ())))
814
- getICMPOperandsForBranch (*MRI.getVRegDef (LHS), MIB, MRI, CC, LHS, RHS);
814
+ Register LHS, RHS;
815
+ RISCVCC::CondCode CC;
816
+ getOperandsForBranch (SelectMI.getCondReg (), MIB, MRI, CC, LHS, RHS);
815
817
816
818
MachineInstr *Result = MIB.buildInstr (RISCV::Select_GPR_Using_CC_GPR)
817
819
.addDef (SelectMI.getReg (0 ))
0 commit comments