@@ -83,13 +83,12 @@ class RISCVInstructionSelector : public InstructionSelector {
83
83
void renderImm (MachineInstrBuilder &MIB, const MachineInstr &MI,
84
84
int OpIdx) const ;
85
85
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,
87
87
// / RHS) to that of MI, but whose condition code matches one of the
88
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 ;
89
+ void getICMPOperandsForBranch (MachineInstr &MI, MachineIRBuilder &MIB,
90
+ MachineRegisterInfo &MRI, RISCVCC::CondCode &CC,
91
+ Register &LHS, Register &RHS) const ;
93
92
94
93
const RISCVSubtarget &STI;
95
94
const RISCVInstrInfo &TII;
@@ -527,26 +526,27 @@ static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
527
526
}
528
527
}
529
528
530
- void RISCVInstructionSelector::setICMPOperandsForBranch (
529
+ void RISCVInstructionSelector::getICMPOperandsForBranch (
531
530
MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI,
532
- CmpInst::Predicate &CC, MachineOperand &LHS, MachineOperand &RHS) const {
531
+ RISCVCC::CondCode &CC, Register &LHS, Register &RHS) const {
533
532
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 ();
537
537
538
538
// Adjust comparisons to use comparison with 0 if possible.
539
- MachineInstr *MaybeConstant = MRI.getVRegDef (RHS. getReg () );
539
+ MachineInstr *MaybeConstant = MRI.getVRegDef (RHS);
540
540
if (MaybeConstant && MaybeConstant->getOpcode () == TargetOpcode::G_CONSTANT) {
541
- switch (CC ) {
541
+ switch (ICMPCC ) {
542
542
case CmpInst::Predicate::ICMP_SGT:
543
543
// Convert X > -1 to X >= 0
544
544
if (MaybeConstant->getOperand (1 ).getCImm ()->getSExtValue () == -1 ) {
545
545
MachineInstr *Zero = MIB.buildConstant (
546
546
MRI.getType (MaybeConstant->getOperand (0 ).getReg ()), 0 );
547
547
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 ();
550
550
}
551
551
return ;
552
552
case CmpInst::Predicate::ICMP_SLT:
@@ -555,17 +555,17 @@ void RISCVInstructionSelector::setICMPOperandsForBranch(
555
555
MachineInstr *Zero = MIB.buildConstant (
556
556
MRI.getType (MaybeConstant->getOperand (0 ).getReg ()), 0 );
557
557
selectConstant (*Zero, MIB, MRI);
558
- CC = CmpInst::Predicate::ICMP_SGE;
558
+ CC = getRISCVCCFromICMP ( CmpInst::Predicate::ICMP_SGE) ;
559
559
RHS = LHS;
560
- LHS = MachineOperand::CreateReg ( Zero->getOperand (0 ).getReg (), false );
560
+ LHS = Zero->getOperand (0 ).getReg ();
561
561
}
562
562
return ;
563
563
default :
564
564
break ;
565
565
}
566
566
}
567
567
568
- switch (CC ) {
568
+ switch (ICMPCC ) {
569
569
default :
570
570
llvm_unreachable (" Expected ICMP CmpInst::Predicate." );
571
571
case CmpInst::Predicate::ICMP_EQ:
@@ -575,14 +575,15 @@ void RISCVInstructionSelector::setICMPOperandsForBranch(
575
575
case CmpInst::Predicate::ICMP_UGE:
576
576
case CmpInst::Predicate::ICMP_SGE:
577
577
// These CCs are supported directly by RISC-V branches.
578
+ CC = getRISCVCCFromICMP (ICMPCC);
578
579
return ;
579
580
case CmpInst::Predicate::ICMP_SGT:
580
581
case CmpInst::Predicate::ICMP_SLE:
581
582
case CmpInst::Predicate::ICMP_UGT:
582
583
case CmpInst::Predicate::ICMP_ULE:
583
584
// These CCs are not supported directly by RISC-V branches, but changing the
584
585
// direction of the CC and swapping LHS and RHS are.
585
- CC = CmpInst::getSwappedPredicate (CC );
586
+ CC = getRISCVCCFromICMP ( CmpInst::getSwappedPredicate (ICMPCC) );
586
587
std::swap (LHS, RHS);
587
588
return ;
588
589
}
@@ -592,31 +593,26 @@ bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
592
593
MachineIRBuilder &MIB,
593
594
MachineRegisterInfo &MRI) const {
594
595
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.
596
599
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 ());
620
616
MI.eraseFromParent ();
621
617
return constrainSelectedInstRegOperands (*Result, TII, TRI, RBI);
622
618
}
0 commit comments