Skip to content

Commit 4c3206c

Browse files
committed
[RISCV][GISel] Sink more code into getOperandsForBranch. NFC
Move the m_GICmp match call in and use it to collect the operands. This code is also needed for G_BRCOND.
1 parent 9ba5b52 commit 4c3206c

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -740,19 +740,24 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
740740
}
741741
}
742742

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.
752757

753758
// Adjust comparisons to use comparison with 0 if possible.
754759
if (auto Constant = getIConstantVRegSExtVal(RHS, MRI)) {
755-
switch (ICMPCC) {
760+
switch (Pred) {
756761
case CmpInst::Predicate::ICMP_SGT:
757762
// Convert X > -1 to X >= 0
758763
if (*Constant == -1) {
@@ -775,7 +780,7 @@ static void getICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
775780
}
776781
}
777782

778-
switch (ICMPCC) {
783+
switch (Pred) {
779784
default:
780785
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
781786
case CmpInst::Predicate::ICMP_EQ:
@@ -785,33 +790,30 @@ static void getICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
785790
case CmpInst::Predicate::ICMP_UGE:
786791
case CmpInst::Predicate::ICMP_SGE:
787792
// These CCs are supported directly by RISC-V branches.
788-
CC = getRISCVCCFromICMP(ICMPCC);
789-
return;
793+
break;
790794
case CmpInst::Predicate::ICMP_SGT:
791795
case CmpInst::Predicate::ICMP_SLE:
792796
case CmpInst::Predicate::ICMP_UGT:
793797
case CmpInst::Predicate::ICMP_ULE:
794798
// These CCs are not supported directly by RISC-V branches, but changing the
795799
// direction of the CC and swapping LHS and RHS are.
796-
CC = getRISCVCCFromICMP(CmpInst::getSwappedPredicate(ICMPCC));
800+
Pred = CmpInst::getSwappedPredicate(Pred);
797801
std::swap(LHS, RHS);
798-
return;
802+
break;
799803
}
804+
805+
CC = getRISCVCCFromICMP(Pred);
806+
return;
800807
}
801808

802809
bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
803810
MachineIRBuilder &MIB,
804811
MachineRegisterInfo &MRI) const {
805812
auto &SelectMI = cast<GSelect>(MI);
806813

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);
815817

816818
MachineInstr *Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
817819
.addDef(SelectMI.getReg(0))

0 commit comments

Comments
 (0)