Skip to content

Commit cfb5abc

Browse files
committed
[RISCV] Explicitly check for supported opcodes in optimizeCondBranch.
We don't support any of the immediate branches in this function yet so explicitly exclude them rather than relying on isReg to return false. Remove use of AnalyzeBranch. It doesn't help us much. Part of the code was already getting the operands directly and it just complicated creating a new branch. I also inlined the modifyBranch function so we could use addReg on BuildMI. Ultimately I want to try to fix that the RISCVCC enum has turned into a proxy for opcode by having an entry for each vendor's branch instructions. So I probably won't commit this until I figure out if this helps move in that direction or if other changes are needed.
1 parent 48a21e6 commit cfb5abc

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,40 +1379,50 @@ bool RISCVInstrInfo::isFromLoadImm(const MachineRegisterInfo &MRI,
13791379
}
13801380

13811381
bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const {
1382+
bool IsSigned = false;
1383+
bool IsEquality = false;
1384+
switch (MI.getOpcode()) {
1385+
default:
1386+
return false;
1387+
case RISCV::BEQ:
1388+
case RISCV::BNE:
1389+
IsEquality = true;
1390+
break;
1391+
case RISCV::BGE:
1392+
case RISCV::BLT:
1393+
IsSigned = true;
1394+
break;
1395+
case RISCV::BGEU:
1396+
case RISCV::BLTU:
1397+
break;
1398+
}
1399+
13821400
MachineBasicBlock *MBB = MI.getParent();
13831401
MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13841402

1385-
MachineBasicBlock *TBB, *FBB;
1386-
SmallVector<MachineOperand, 3> Cond;
1387-
if (analyzeBranch(*MBB, TBB, FBB, Cond, /*AllowModify=*/false))
1388-
return false;
1403+
const MachineOperand &LHS = MI.getOperand(0);
1404+
const MachineOperand &RHS = MI.getOperand(1);
1405+
MachineBasicBlock *TBB = MI.getOperand(2).getMBB();
13891406

1390-
RISCVCC::CondCode CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
1407+
RISCVCC::CondCode CC = getCondFromBranchOpc(MI.getOpcode());
13911408
assert(CC != RISCVCC::COND_INVALID);
13921409

1393-
auto modifyBranch = [&]() {
1394-
// Build the new branch and remove the old one.
1395-
BuildMI(*MBB, MI, MI.getDebugLoc(),
1396-
getBrCond(static_cast<RISCVCC::CondCode>(Cond[0].getImm())))
1397-
.add(Cond[1])
1398-
.add(Cond[2])
1399-
.addMBB(TBB);
1400-
MI.eraseFromParent();
1401-
};
1402-
14031410
// Canonicalize conditional branches which can be constant folded into
14041411
// beqz or bnez. We can't modify the CFG here.
14051412
int64_t C0, C1;
1406-
if (isFromLoadImm(MRI, Cond[1], C0) && isFromLoadImm(MRI, Cond[2], C1)) {
1407-
unsigned NewCC =
1408-
evaluateCondBranch(CC, C0, C1) ? RISCVCC::COND_EQ : RISCVCC::COND_NE;
1409-
Cond[0] = MachineOperand::CreateImm(NewCC);
1410-
Cond[1] = Cond[2] = MachineOperand::CreateReg(RISCV::X0, /*isDef=*/false);
1411-
modifyBranch();
1413+
if (isFromLoadImm(MRI, LHS, C0) && isFromLoadImm(MRI, RHS, C1)) {
1414+
unsigned NewOpc = evaluateCondBranch(CC, C0, C1) ? RISCV::BEQ : RISCV::BNE;
1415+
MachineOperand Zero = MachineOperand::CreateReg(RISCV::X0, /*isDef=*/false);
1416+
// Build the new branch and remove the old one.
1417+
BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1418+
.add(Zero)
1419+
.add(Zero)
1420+
.addMBB(TBB);
1421+
MI.eraseFromParent();
14121422
return true;
14131423
}
14141424

1415-
if (CC == RISCVCC::COND_EQ || CC == RISCVCC::COND_NE)
1425+
if (IsEquality)
14161426
return false;
14171427

14181428
// For two constants C0 and C1 from
@@ -1432,8 +1442,6 @@ bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const {
14321442
//
14331443
// To make sure this optimization is really beneficial, we only
14341444
// optimize for cases where Y had only one use (i.e. only used by the branch).
1435-
MachineOperand &LHS = MI.getOperand(0);
1436-
MachineOperand &RHS = MI.getOperand(1);
14371445
// Try to find the register for constant Z; return
14381446
// invalid register otherwise.
14391447
auto searchConst = [&](int64_t C1) -> Register {
@@ -1449,23 +1457,25 @@ bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const {
14491457
return Register();
14501458
};
14511459

1460+
unsigned NewOpc = RISCVCC::getBrCond(getOppositeBranchCondition(CC));
1461+
14521462
// Might be case 1.
14531463
// Don't change 0 to 1 since we can use x0.
14541464
// For unsigned cases changing -1U to 0 would be incorrect.
14551465
// The incorrect case for signed would be INT_MAX, but isFromLoadImm can't
14561466
// return that.
14571467
if (isFromLoadImm(MRI, LHS, C0) && C0 != 0 && LHS.getReg().isVirtual() &&
1458-
MRI.hasOneUse(LHS.getReg()) &&
1459-
(CC == RISCVCC::COND_GE || CC == RISCVCC::COND_LT || C0 != -1)) {
1468+
MRI.hasOneUse(LHS.getReg()) && (IsSigned || C0 != -1)) {
14601469
assert(isInt<12>(C0) && "Unexpected immediate");
14611470
if (Register RegZ = searchConst(C0 + 1)) {
1462-
reverseBranchCondition(Cond);
1463-
Cond[1] = MachineOperand::CreateReg(RHS.getReg(), /*isDef=*/false);
1464-
Cond[2] = MachineOperand::CreateReg(RegZ, /*isDef=*/false);
1471+
BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1472+
.add(RHS)
1473+
.addReg(RegZ)
1474+
.addMBB(TBB);
14651475
// We might extend the live range of Z, clear its kill flag to
14661476
// account for this.
14671477
MRI.clearKillFlags(RegZ);
1468-
modifyBranch();
1478+
MI.eraseFromParent();
14691479
return true;
14701480
}
14711481
}
@@ -1479,13 +1489,14 @@ bool RISCVInstrInfo::optimizeCondBranch(MachineInstr &MI) const {
14791489
MRI.hasOneUse(RHS.getReg())) {
14801490
assert(isInt<12>(C0) && "Unexpected immediate");
14811491
if (Register RegZ = searchConst(C0 - 1)) {
1482-
reverseBranchCondition(Cond);
1483-
Cond[1] = MachineOperand::CreateReg(RegZ, /*isDef=*/false);
1484-
Cond[2] = MachineOperand::CreateReg(LHS.getReg(), /*isDef=*/false);
1492+
BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1493+
.addReg(RegZ)
1494+
.add(LHS)
1495+
.addMBB(TBB);
14851496
// We might extend the live range of Z, clear its kill flag to
14861497
// account for this.
14871498
MRI.clearKillFlags(RegZ);
1488-
modifyBranch();
1499+
MI.eraseFromParent();
14891500
return true;
14901501
}
14911502
}

0 commit comments

Comments
 (0)