Skip to content

[RISCV] Add new CondCode COND_CV_BEQIMM/COND_CV_BNEIMM for CV immediate branch #135771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
RISCVCC::CondCode CC;
getOperandsForBranch(MI.getOperand(0).getReg(), CC, LHS, RHS, *MRI);

auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(STI, CC), {}, {LHS, RHS})
auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(CC), {}, {LHS, RHS})
.addMBB(MI.getOperand(1).getMBB());
MI.eraseFromParent();
return constrainSelectedInstRegOperands(*Bcc, TII, TRI, RBI);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20640,7 +20640,7 @@ static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,

// Insert appropriate branch.
if (MI.getOperand(2).isImm())
BuildMI(HeadMBB, DL, TII.getBrCond(CC, MI.getOperand(2).isImm()))
BuildMI(HeadMBB, DL, TII.getBrCond(CC))
.addReg(LHS)
.addImm(MI.getOperand(2).getImm())
.addMBB(TailMBB);
Expand Down
42 changes: 19 additions & 23 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,10 +974,6 @@ static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
switch (Opc) {
default:
return RISCVCC::COND_INVALID;
case RISCV::CV_BEQIMM:
return RISCVCC::COND_EQ;
case RISCV::CV_BNEIMM:
return RISCVCC::COND_NE;
case RISCV::BEQ:
return RISCVCC::COND_EQ;
case RISCV::BNE:
Expand All @@ -990,6 +986,10 @@ static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
return RISCVCC::COND_LTU;
case RISCV::BGEU:
return RISCVCC::COND_GEU;
case RISCV::CV_BEQIMM:
return RISCVCC::COND_CV_BEQIMM;
case RISCV::CV_BNEIMM:
return RISCVCC::COND_CV_BNEIMM;
}
}

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

unsigned RISCVCC::getBrCond(const RISCVSubtarget &STI, RISCVCC::CondCode CC,
bool Imm) {
unsigned RISCVCC::getBrCond(RISCVCC::CondCode CC) {
switch (CC) {
default:
llvm_unreachable("Unknown condition code!");
case RISCVCC::COND_EQ:
if (!Imm)
return RISCV::BEQ;
if (STI.hasVendorXCVbi())
return RISCV::CV_BEQIMM;
llvm_unreachable("Unknown branch immediate!");
return RISCV::BEQ;
case RISCVCC::COND_NE:
if (!Imm)
return RISCV::BNE;
if (STI.hasVendorXCVbi())
return RISCV::CV_BNEIMM;
llvm_unreachable("Unknown branch immediate!");
return RISCV::BNE;
case RISCVCC::COND_LT:
return RISCV::BLT;
case RISCVCC::COND_GE:
Expand All @@ -1052,12 +1043,15 @@ unsigned RISCVCC::getBrCond(const RISCVSubtarget &STI, RISCVCC::CondCode CC,
return RISCV::BLTU;
case RISCVCC::COND_GEU:
return RISCV::BGEU;
case RISCVCC::COND_CV_BEQIMM:
return RISCV::CV_BEQIMM;
case RISCVCC::COND_CV_BNEIMM:
return RISCV::CV_BNEIMM;
}
}

const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC,
bool Imm) const {
return get(RISCVCC::getBrCond(STI, CC, Imm));
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
return get(RISCVCC::getBrCond(CC));
}

RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
Expand All @@ -1076,6 +1070,10 @@ RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
return RISCVCC::COND_GEU;
case RISCVCC::COND_GEU:
return RISCVCC::COND_LTU;
case RISCVCC::COND_CV_BEQIMM:
return RISCVCC::COND_CV_BNEIMM;
case RISCVCC::COND_CV_BNEIMM:
return RISCVCC::COND_CV_BEQIMM;
}
}

Expand Down Expand Up @@ -1206,10 +1204,8 @@ unsigned RISCVInstrInfo::insertBranch(

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

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ enum CondCode {
COND_GE,
COND_LTU,
COND_GEU,
COND_CV_BEQIMM,
COND_CV_BNEIMM,
COND_INVALID
};

CondCode getOppositeBranchCondition(CondCode);
unsigned getBrCond(const RISCVSubtarget &STI, CondCode CC, bool Imm = false);
unsigned getBrCond(CondCode CC);

} // end of namespace RISCVCC

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

MCInst getNop() const override;
const MCInstrDesc &getBrCond(RISCVCC::CondCode CC, bool Imm = false) const;
const MCInstrDesc &getBrCond(RISCVCC::CondCode CC) const;

Register isLoadFromStackSlot(const MachineInstr &MI,
int &FrameIndex) const override;
Expand Down
9 changes: 8 additions & 1 deletion llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,13 @@ let Predicates = [HasVendorXCValu, IsRV32], AddedComplexity = 1 in {
// Patterns for immediate branching operations
//===----------------------------------------------------------------------===//

def IntCCtoRISCVCCCV : SDNodeXForm<riscv_selectcc, [{
ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
assert(CC == ISD::SETEQ || CC == ISD::SETNE);
RISCVCC::CondCode BrCC = CC == ISD::SETEQ ? RISCVCC::COND_CV_BEQIMM : RISCVCC::COND_CV_BNEIMM;
return CurDAG->getTargetConstant(BrCC, SDLoc(N), Subtarget->getXLenVT());
}]>;

let Predicates = [HasVendorXCVbi, IsRV32], AddedComplexity = 2 in {
def : Pat<(riscv_brcc GPR:$rs1, simm5:$imm5, SETEQ, bb:$imm12),
(CV_BEQIMM GPR:$rs1, simm5:$imm5, bare_simm13_lsb0:$imm12)>;
Expand All @@ -807,7 +814,7 @@ let Predicates = [HasVendorXCVbi, IsRV32], AddedComplexity = 2 in {
: Pat<(riscv_selectcc_frag:$cc (i32 GPR:$lhs), simm5:$Constant, Cond,
(i32 GPR:$truev), GPR:$falsev),
(Select_GPR_Using_CC_Imm GPR:$lhs, simm5:$Constant,
(IntCCtoRISCVCC $cc), GPR:$truev, GPR:$falsev)>;
(IntCCtoRISCVCCCV $cc), GPR:$truev, GPR:$falsev)>;

def : Selectbi<SETEQ>;
def : Selectbi<SETNE>;
Expand Down
Loading