Skip to content

Commit a1b9d38

Browse files
fixup! [RISCV][GISel] Select G_SELECT (G_ICMP, A, B)
Don't create new G_ICMP
1 parent e81ab66 commit a1b9d38

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

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

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

86-
/// Returns a G_ICMP that is equivalent to MI, whose condition code matches
87-
/// one of the comparisons supported directly by branches in the RISC-V ISA.
88-
MachineInstr *createICMPForBranch(MachineInstr *MI, MachineIRBuilder &MIB,
89-
MachineRegisterInfo &MRI) const;
86+
/// Sets CC, LHS, and RHS so that they form an equivelent G_ICMP (CC, LHS,
87+
/// RHS) to that of MI, but whose condition code matches one of the
88+
/// 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;
9093

9194
const RISCVSubtarget &STI;
9295
const RISCVInstrInfo &TII;
@@ -524,13 +527,13 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
524527
}
525528
}
526529

527-
MachineInstr *RISCVInstructionSelector::createICMPForBranch(
528-
MachineInstr *MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI) const {
529-
assert(MI->getOpcode() == TargetOpcode::G_ICMP);
530-
CmpInst::Predicate CC =
531-
static_cast<CmpInst::Predicate>(MI->getOperand(1).getPredicate());
532-
MachineOperand &LHS = MI->getOperand(2);
533-
MachineOperand &RHS = MI->getOperand(3);
530+
void RISCVInstructionSelector::setICMPOperandsForBranch(
531+
MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI,
532+
CmpInst::Predicate &CC, MachineOperand &LHS, MachineOperand &RHS) const {
533+
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);
534537

535538
// Adjust comparisons to use comparison with 0 if possible.
536539
MachineInstr *MaybeConstant = MRI.getVRegDef(RHS.getReg());
@@ -542,20 +545,21 @@ MachineInstr *RISCVInstructionSelector::createICMPForBranch(
542545
MachineInstr *Zero = MIB.buildConstant(
543546
MRI.getType(MaybeConstant->getOperand(0).getReg()), 0);
544547
selectConstant(*Zero, MIB, MRI);
545-
return MIB.buildICmp(CmpInst::Predicate::ICMP_SGE, MI->getOperand(0),
546-
LHS, Zero->getOperand(0));
548+
CC = CmpInst::Predicate::ICMP_SGE;
549+
RHS = MachineOperand::CreateReg(Zero->getOperand(0).getReg(), false);
547550
}
548-
break;
551+
return;
549552
case CmpInst::Predicate::ICMP_SLT:
550553
// Convert X < 1 to 0 >= X
551554
if (MaybeConstant->getOperand(1).getCImm()->getSExtValue() == 1) {
552-
MachineInstr *Zero= MIB.buildConstant(
555+
MachineInstr *Zero = MIB.buildConstant(
553556
MRI.getType(MaybeConstant->getOperand(0).getReg()), 0);
554557
selectConstant(*Zero, MIB, MRI);
555-
return MIB.buildICmp(CmpInst::Predicate::ICMP_SGE, MI->getOperand(0),
556-
Zero->getOperand(0), LHS);
558+
CC = CmpInst::Predicate::ICMP_SGE;
559+
RHS = LHS;
560+
LHS = MachineOperand::CreateReg(Zero->getOperand(0).getReg(), false);
557561
}
558-
break;
562+
return;
559563
default:
560564
break;
561565
}
@@ -571,15 +575,16 @@ MachineInstr *RISCVInstructionSelector::createICMPForBranch(
571575
case CmpInst::Predicate::ICMP_UGE:
572576
case CmpInst::Predicate::ICMP_SGE:
573577
// These CCs are supported directly by RISC-V branches.
574-
return MI;
578+
return;
575579
case CmpInst::Predicate::ICMP_SGT:
576580
case CmpInst::Predicate::ICMP_SLE:
577581
case CmpInst::Predicate::ICMP_UGT:
578582
case CmpInst::Predicate::ICMP_ULE:
579583
// These CCs are not supported directly by RISC-V branches, but changing the
580584
// direction of the CC and swapping LHS and RHS are.
581-
return MIB.buildICmp(CmpInst::getSwappedPredicate(CC), MI->getOperand(0),
582-
RHS, LHS);
585+
CC = CmpInst::getSwappedPredicate(CC);
586+
std::swap(LHS, RHS);
587+
return;
583588
}
584589
}
585590

@@ -592,22 +597,17 @@ bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
592597
if (MaybeICMP && MaybeICMP->getOpcode() == TargetOpcode::G_ICMP) {
593598
// If MI is a G_SELECT(G_ICMP(tst, A, B), C, D) then we can use (A, B, tst)
594599
// as the (LHS, RHS, CC) of the Select_GPR_Using_CC_GPR.
595-
MachineInstr *ICMPForBranch = createICMPForBranch(MaybeICMP, MIB, MRI);
596-
CmpInst::Predicate CC = static_cast<CmpInst::Predicate>(
597-
ICMPForBranch->getOperand(1).getPredicate());
600+
CmpInst::Predicate CC;
601+
MachineOperand LHS = MaybeICMP->getOperand(2);
602+
MachineOperand RHS = MaybeICMP->getOperand(3);
603+
setICMPOperandsForBranch(*MaybeICMP, MIB, MRI, CC, LHS, RHS);
598604
Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
599605
.addDef(MI.getOperand(0).getReg());
600-
Result->addOperand(ICMPForBranch->getOperand(2));
601-
Result->addOperand(ICMPForBranch->getOperand(3));
602-
Result->addOperand(
603-
MachineOperand::CreateImm(getRISCVCCFromICMP(CC)));
606+
Result->addOperand(LHS);
607+
Result->addOperand(RHS);
608+
Result->addOperand(MachineOperand::CreateImm(getRISCVCCFromICMP(CC)));
604609
Result->addOperand(MI.getOperand(2));
605610
Result->addOperand(MI.getOperand(3));
606-
607-
// Delete ICMPForBranch since we know it has no users. Let the original
608-
// G_ICMP be selected normally in case it has other users.
609-
if (ICMPForBranch != MaybeICMP)
610-
ICMPForBranch->eraseFromParent();
611611
} else {
612612
Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
613613
.addDef(MI.getOperand(0).getReg())

0 commit comments

Comments
 (0)