@@ -83,10 +83,13 @@ class RISCVInstructionSelector : public InstructionSelector {
83
83
void renderImm (MachineInstrBuilder &MIB, const MachineInstr &MI,
84
84
int OpIdx) const ;
85
85
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 ;
90
93
91
94
const RISCVSubtarget &STI;
92
95
const RISCVInstrInfo &TII;
@@ -524,13 +527,13 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
524
527
}
525
528
}
526
529
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 );
534
537
535
538
// Adjust comparisons to use comparison with 0 if possible.
536
539
MachineInstr *MaybeConstant = MRI.getVRegDef (RHS.getReg ());
@@ -542,20 +545,21 @@ MachineInstr *RISCVInstructionSelector::createICMPForBranch(
542
545
MachineInstr *Zero = MIB.buildConstant (
543
546
MRI.getType (MaybeConstant->getOperand (0 ).getReg ()), 0 );
544
547
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 );
547
550
}
548
- break ;
551
+ return ;
549
552
case CmpInst::Predicate::ICMP_SLT:
550
553
// Convert X < 1 to 0 >= X
551
554
if (MaybeConstant->getOperand (1 ).getCImm ()->getSExtValue () == 1 ) {
552
- MachineInstr *Zero= MIB.buildConstant (
555
+ MachineInstr *Zero = MIB.buildConstant (
553
556
MRI.getType (MaybeConstant->getOperand (0 ).getReg ()), 0 );
554
557
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 );
557
561
}
558
- break ;
562
+ return ;
559
563
default :
560
564
break ;
561
565
}
@@ -571,15 +575,16 @@ MachineInstr *RISCVInstructionSelector::createICMPForBranch(
571
575
case CmpInst::Predicate::ICMP_UGE:
572
576
case CmpInst::Predicate::ICMP_SGE:
573
577
// These CCs are supported directly by RISC-V branches.
574
- return MI ;
578
+ return ;
575
579
case CmpInst::Predicate::ICMP_SGT:
576
580
case CmpInst::Predicate::ICMP_SLE:
577
581
case CmpInst::Predicate::ICMP_UGT:
578
582
case CmpInst::Predicate::ICMP_ULE:
579
583
// These CCs are not supported directly by RISC-V branches, but changing the
580
584
// 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 ;
583
588
}
584
589
}
585
590
@@ -592,22 +597,17 @@ bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
592
597
if (MaybeICMP && MaybeICMP->getOpcode () == TargetOpcode::G_ICMP) {
593
598
// If MI is a G_SELECT(G_ICMP(tst, A, B), C, D) then we can use (A, B, tst)
594
599
// 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);
598
604
Result = MIB.buildInstr (RISCV::Select_GPR_Using_CC_GPR)
599
605
.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)));
604
609
Result->addOperand (MI.getOperand (2 ));
605
610
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 ();
611
611
} else {
612
612
Result = MIB.buildInstr (RISCV::Select_GPR_Using_CC_GPR)
613
613
.addDef (MI.getOperand (0 ).getReg ())
0 commit comments