Skip to content

Commit 0439a4e

Browse files
authored
[RISCV] Add new CondCode COND_CV_BEQIMM/COND_CV_BNEIMM for CV immediate branch (#135771)
If there is another branch instruction also with immediate operand, but it is used to specify which bit to be tested is set or clear. We only check whether operand2 is immediate or not here. There are no way to distinguish between them. So add new CondCode COND_CV_BEQIMM/COND_CV_BNEIMM that we can know what kinds of immediate branch instruction are matched in Select_* Pseudo.
1 parent e4f2191 commit 0439a4e

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
789789
RISCVCC::CondCode CC;
790790
getOperandsForBranch(MI.getOperand(0).getReg(), CC, LHS, RHS, *MRI);
791791

792-
auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(STI, CC), {}, {LHS, RHS})
792+
auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(CC), {}, {LHS, RHS})
793793
.addMBB(MI.getOperand(1).getMBB());
794794
MI.eraseFromParent();
795795
return constrainSelectedInstRegOperands(*Bcc, TII, TRI, RBI);

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20645,7 +20645,7 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
2064520645

2064620646
// Insert appropriate branch.
2064720647
if (MI.getOperand(2).isImm())
20648-
BuildMI(HeadMBB, DL, TII.getBrCond(CC, MI.getOperand(2).isImm()))
20648+
BuildMI(HeadMBB, DL, TII.getBrCond(CC))
2064920649
.addReg(LHS)
2065020650
.addImm(MI.getOperand(2).getImm())
2065120651
.addMBB(TailMBB);

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -974,10 +974,6 @@ static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
974974
switch (Opc) {
975975
default:
976976
return RISCVCC::COND_INVALID;
977-
case RISCV::CV_BEQIMM:
978-
return RISCVCC::COND_EQ;
979-
case RISCV::CV_BNEIMM:
980-
return RISCVCC::COND_NE;
981977
case RISCV::BEQ:
982978
return RISCVCC::COND_EQ;
983979
case RISCV::BNE:
@@ -990,6 +986,10 @@ static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
990986
return RISCVCC::COND_LTU;
991987
case RISCV::BGEU:
992988
return RISCVCC::COND_GEU;
989+
case RISCV::CV_BEQIMM:
990+
return RISCVCC::COND_CV_BEQIMM;
991+
case RISCV::CV_BNEIMM:
992+
return RISCVCC::COND_CV_BNEIMM;
993993
}
994994
}
995995

@@ -1027,23 +1027,14 @@ static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
10271027
Cond.push_back(LastInst.getOperand(1));
10281028
}
10291029

1030-
unsigned RISCVCC::getBrCond(const RISCVSubtarget &STI, RISCVCC::CondCode CC,
1031-
bool Imm) {
1030+
unsigned RISCVCC::getBrCond(RISCVCC::CondCode CC) {
10321031
switch (CC) {
10331032
default:
10341033
llvm_unreachable("Unknown condition code!");
10351034
case RISCVCC::COND_EQ:
1036-
if (!Imm)
1037-
return RISCV::BEQ;
1038-
if (STI.hasVendorXCVbi())
1039-
return RISCV::CV_BEQIMM;
1040-
llvm_unreachable("Unknown branch immediate!");
1035+
return RISCV::BEQ;
10411036
case RISCVCC::COND_NE:
1042-
if (!Imm)
1043-
return RISCV::BNE;
1044-
if (STI.hasVendorXCVbi())
1045-
return RISCV::CV_BNEIMM;
1046-
llvm_unreachable("Unknown branch immediate!");
1037+
return RISCV::BNE;
10471038
case RISCVCC::COND_LT:
10481039
return RISCV::BLT;
10491040
case RISCVCC::COND_GE:
@@ -1052,12 +1043,15 @@ unsigned RISCVCC::getBrCond(const RISCVSubtarget &STI, RISCVCC::CondCode CC,
10521043
return RISCV::BLTU;
10531044
case RISCVCC::COND_GEU:
10541045
return RISCV::BGEU;
1046+
case RISCVCC::COND_CV_BEQIMM:
1047+
return RISCV::CV_BEQIMM;
1048+
case RISCVCC::COND_CV_BNEIMM:
1049+
return RISCV::CV_BNEIMM;
10551050
}
10561051
}
10571052

1058-
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC,
1059-
bool Imm) const {
1060-
return get(RISCVCC::getBrCond(STI, CC, Imm));
1053+
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
1054+
return get(RISCVCC::getBrCond(CC));
10611055
}
10621056

10631057
RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
@@ -1076,6 +1070,10 @@ RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
10761070
return RISCVCC::COND_GEU;
10771071
case RISCVCC::COND_GEU:
10781072
return RISCVCC::COND_LTU;
1073+
case RISCVCC::COND_CV_BEQIMM:
1074+
return RISCVCC::COND_CV_BNEIMM;
1075+
case RISCVCC::COND_CV_BNEIMM:
1076+
return RISCVCC::COND_CV_BEQIMM;
10791077
}
10801078
}
10811079

@@ -1206,10 +1204,8 @@ unsigned RISCVInstrInfo::insertBranch(
12061204

12071205
// Either a one or two-way conditional branch.
12081206
auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
1209-
MachineInstr &CondMI = *BuildMI(&MBB, DL, getBrCond(CC, Cond[2].isImm()))
1210-
.add(Cond[1])
1211-
.add(Cond[2])
1212-
.addMBB(TBB);
1207+
MachineInstr &CondMI =
1208+
*BuildMI(&MBB, DL, getBrCond(CC)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
12131209
if (BytesAdded)
12141210
*BytesAdded += getInstSizeInBytes(CondMI);
12151211

llvm/lib/Target/RISCV/RISCVInstrInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ enum CondCode {
4141
COND_GE,
4242
COND_LTU,
4343
COND_GEU,
44+
COND_CV_BEQIMM,
45+
COND_CV_BNEIMM,
4446
COND_INVALID
4547
};
4648

4749
CondCode getOppositeBranchCondition(CondCode);
48-
unsigned getBrCond(const RISCVSubtarget &STI, CondCode CC, bool Imm = false);
50+
unsigned getBrCond(CondCode CC);
4951

5052
} // end of namespace RISCVCC
5153

@@ -65,7 +67,7 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
6567
explicit RISCVInstrInfo(RISCVSubtarget &STI);
6668

6769
MCInst getNop() const override;
68-
const MCInstrDesc &getBrCond(RISCVCC::CondCode CC, bool Imm = false) const;
70+
const MCInstrDesc &getBrCond(RISCVCC::CondCode CC) const;
6971

7072
Register isLoadFromStackSlot(const MachineInstr &MI,
7173
int &FrameIndex) const override;

llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,13 @@ let Predicates = [HasVendorXCValu, IsRV32], AddedComplexity = 1 in {
791791
// Patterns for immediate branching operations
792792
//===----------------------------------------------------------------------===//
793793

794+
def IntCCtoRISCVCCCV : SDNodeXForm<riscv_selectcc, [{
795+
ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
796+
assert(CC == ISD::SETEQ || CC == ISD::SETNE);
797+
RISCVCC::CondCode BrCC = CC == ISD::SETEQ ? RISCVCC::COND_CV_BEQIMM : RISCVCC::COND_CV_BNEIMM;
798+
return CurDAG->getTargetConstant(BrCC, SDLoc(N), Subtarget->getXLenVT());
799+
}]>;
800+
794801
let Predicates = [HasVendorXCVbi, IsRV32], AddedComplexity = 2 in {
795802
def : Pat<(riscv_brcc GPR:$rs1, simm5:$imm5, SETEQ, bb:$imm12),
796803
(CV_BEQIMM GPR:$rs1, simm5:$imm5, bare_simm13_lsb0:$imm12)>;
@@ -807,7 +814,7 @@ let Predicates = [HasVendorXCVbi, IsRV32], AddedComplexity = 2 in {
807814
: Pat<(riscv_selectcc_frag:$cc (i32 GPR:$lhs), simm5:$Constant, Cond,
808815
(i32 GPR:$truev), GPR:$falsev),
809816
(Select_GPR_Using_CC_Imm GPR:$lhs, simm5:$Constant,
810-
(IntCCtoRISCVCC $cc), GPR:$truev, GPR:$falsev)>;
817+
(IntCCtoRISCVCCCV $cc), GPR:$truev, GPR:$falsev)>;
811818

812819
def : Selectbi<SETEQ>;
813820
def : Selectbi<SETNE>;

0 commit comments

Comments
 (0)