Skip to content

Commit 1a57237

Browse files
fixup! [RISCV][GISel] Select G_SELECT (G_ICMP, A, B)
Refactor NFC
1 parent a1b9d38 commit 1a57237

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

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

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ class RISCVInstructionSelector : public InstructionSelector {
8383
void renderImm(MachineInstrBuilder &MIB, const MachineInstr &MI,
8484
int OpIdx) const;
8585

86-
/// Sets CC, LHS, and RHS so that they form an equivelent G_ICMP (CC, LHS,
86+
/// Sets CC, LHS, and RHS so that they form an equivelent G_ICMP (ICMPCC, LHS,
8787
/// RHS) to that of MI, but whose condition code matches one of the
8888
/// comparisons supported directly by branches in the RISC-V ISA.
89-
void setICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
90-
MachineRegisterInfo &MRI,
91-
CmpInst::Predicate &CC, MachineOperand &LHS,
92-
MachineOperand &RHS) const;
89+
void getICMPOperandsForBranch(MachineInstr &MI, MachineIRBuilder &MIB,
90+
MachineRegisterInfo &MRI, RISCVCC::CondCode &CC,
91+
Register &LHS, Register &RHS) const;
9392

9493
const RISCVSubtarget &STI;
9594
const RISCVInstrInfo &TII;
@@ -527,26 +526,27 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
527526
}
528527
}
529528

530-
void RISCVInstructionSelector::setICMPOperandsForBranch(
529+
void RISCVInstructionSelector::getICMPOperandsForBranch(
531530
MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI,
532-
CmpInst::Predicate &CC, MachineOperand &LHS, MachineOperand &RHS) const {
531+
RISCVCC::CondCode &CC, Register &LHS, Register &RHS) const {
533532
assert(MI.getOpcode() == TargetOpcode::G_ICMP);
534-
CC = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
535-
LHS = MI.getOperand(2);
536-
RHS = MI.getOperand(3);
533+
CmpInst::Predicate ICMPCC =
534+
static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
535+
LHS = MI.getOperand(2).getReg();
536+
RHS = MI.getOperand(3).getReg();
537537

538538
// Adjust comparisons to use comparison with 0 if possible.
539-
MachineInstr *MaybeConstant = MRI.getVRegDef(RHS.getReg());
539+
MachineInstr *MaybeConstant = MRI.getVRegDef(RHS);
540540
if (MaybeConstant && MaybeConstant->getOpcode() == TargetOpcode::G_CONSTANT) {
541-
switch (CC) {
541+
switch (ICMPCC) {
542542
case CmpInst::Predicate::ICMP_SGT:
543543
// Convert X > -1 to X >= 0
544544
if (MaybeConstant->getOperand(1).getCImm()->getSExtValue() == -1) {
545545
MachineInstr *Zero = MIB.buildConstant(
546546
MRI.getType(MaybeConstant->getOperand(0).getReg()), 0);
547547
selectConstant(*Zero, MIB, MRI);
548-
CC = CmpInst::Predicate::ICMP_SGE;
549-
RHS = MachineOperand::CreateReg(Zero->getOperand(0).getReg(), false);
548+
CC = getRISCVCCFromICMP(CmpInst::Predicate::ICMP_SGE);
549+
RHS = Zero->getOperand(0).getReg();
550550
}
551551
return;
552552
case CmpInst::Predicate::ICMP_SLT:
@@ -555,17 +555,17 @@ void RISCVInstructionSelector::setICMPOperandsForBranch(
555555
MachineInstr *Zero = MIB.buildConstant(
556556
MRI.getType(MaybeConstant->getOperand(0).getReg()), 0);
557557
selectConstant(*Zero, MIB, MRI);
558-
CC = CmpInst::Predicate::ICMP_SGE;
558+
CC = getRISCVCCFromICMP(CmpInst::Predicate::ICMP_SGE);
559559
RHS = LHS;
560-
LHS = MachineOperand::CreateReg(Zero->getOperand(0).getReg(), false);
560+
LHS = Zero->getOperand(0).getReg();
561561
}
562562
return;
563563
default:
564564
break;
565565
}
566566
}
567567

568-
switch (CC) {
568+
switch (ICMPCC) {
569569
default:
570570
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
571571
case CmpInst::Predicate::ICMP_EQ:
@@ -575,14 +575,15 @@ void RISCVInstructionSelector::setICMPOperandsForBranch(
575575
case CmpInst::Predicate::ICMP_UGE:
576576
case CmpInst::Predicate::ICMP_SGE:
577577
// These CCs are supported directly by RISC-V branches.
578+
CC = getRISCVCCFromICMP(ICMPCC);
578579
return;
579580
case CmpInst::Predicate::ICMP_SGT:
580581
case CmpInst::Predicate::ICMP_SLE:
581582
case CmpInst::Predicate::ICMP_UGT:
582583
case CmpInst::Predicate::ICMP_ULE:
583584
// These CCs are not supported directly by RISC-V branches, but changing the
584585
// direction of the CC and swapping LHS and RHS are.
585-
CC = CmpInst::getSwappedPredicate(CC);
586+
CC = getRISCVCCFromICMP(CmpInst::getSwappedPredicate(ICMPCC));
586587
std::swap(LHS, RHS);
587588
return;
588589
}
@@ -592,31 +593,26 @@ bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
592593
MachineIRBuilder &MIB,
593594
MachineRegisterInfo &MRI) const {
594595
assert(MI.getOpcode() == TargetOpcode::G_SELECT);
595-
MachineInstr *Result;
596+
597+
// If MI is a G_SELECT(G_ICMP(tst, A, B), C, D) then we can use (A, B, tst)
598+
// as the (LHS, RHS, CC) of the Select_GPR_Using_CC_GPR.
596599
MachineInstr *MaybeICMP = MRI.getVRegDef(MI.getOperand(1).getReg());
597-
if (MaybeICMP && MaybeICMP->getOpcode() == TargetOpcode::G_ICMP) {
598-
// If MI is a G_SELECT(G_ICMP(tst, A, B), C, D) then we can use (A, B, tst)
599-
// as the (LHS, RHS, CC) of the Select_GPR_Using_CC_GPR.
600-
CmpInst::Predicate CC;
601-
MachineOperand LHS = MaybeICMP->getOperand(2);
602-
MachineOperand RHS = MaybeICMP->getOperand(3);
603-
setICMPOperandsForBranch(*MaybeICMP, MIB, MRI, CC, LHS, RHS);
604-
Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
605-
.addDef(MI.getOperand(0).getReg());
606-
Result->addOperand(LHS);
607-
Result->addOperand(RHS);
608-
Result->addOperand(MachineOperand::CreateImm(getRISCVCCFromICMP(CC)));
609-
Result->addOperand(MI.getOperand(2));
610-
Result->addOperand(MI.getOperand(3));
611-
} else {
612-
Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
613-
.addDef(MI.getOperand(0).getReg())
614-
.addReg(MI.getOperand(1).getReg())
615-
.addReg(RISCV::X0)
616-
.addImm(RISCVCC::COND_NE)
617-
.addReg(MI.getOperand(2).getReg())
618-
.addReg(MI.getOperand(3).getReg());
619-
}
600+
bool Op1IsICMP = MaybeICMP && MaybeICMP->getOpcode() == TargetOpcode::G_ICMP;
601+
RISCVCC::CondCode CC;
602+
Register LHS, RHS;
603+
if (Op1IsICMP)
604+
getICMPOperandsForBranch(*MaybeICMP, MIB, MRI, CC, LHS, RHS);
605+
606+
Register Op1 = Op1IsICMP ? LHS : MI.getOperand(1).getReg();
607+
Register Op2 = Op1IsICMP ? RHS : RISCV::X0;
608+
unsigned Op3 = Op1IsICMP ? CC : RISCVCC::COND_NE;
609+
MachineInstr *Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
610+
.addDef(MI.getOperand(0).getReg())
611+
.addReg(Op1)
612+
.addReg(Op2)
613+
.addImm(Op3)
614+
.addReg(MI.getOperand(2).getReg())
615+
.addReg(MI.getOperand(3).getReg());
620616
MI.eraseFromParent();
621617
return constrainSelectedInstRegOperands(*Result, TII, TRI, RBI);
622618
}

0 commit comments

Comments
 (0)