[PATCH] D132347: [RISCV] Use analyzeBranch in RISCVRedundantCopyElimination.
Craig Topper via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 21 22:06:14 PDT 2022
craig.topper created this revision.
craig.topper added reviewers: reames, asb, luismarques, frasercrmck, kito-cheng, arcbbb.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.
The existing code was incorrect if we had more than one conditional
branch instruction in a basic block. Though I don't think that will
occur, using analyzeBranch detects that as an unsupported case.
Overall this results in simpler code in RISCVRedundantCopyElimination.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132347
Files:
llvm/lib/Target/RISCV/RISCVRedundantCopyElimination.cpp
Index: llvm/lib/Target/RISCV/RISCVRedundantCopyElimination.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVRedundantCopyElimination.cpp
+++ llvm/lib/Target/RISCV/RISCVRedundantCopyElimination.cpp
@@ -24,6 +24,7 @@
//===----------------------------------------------------------------------===//
#include "RISCV.h"
+#include "RISCVInstrInfo.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -39,6 +40,7 @@
class RISCVRedundantCopyElimination : public MachineFunctionPass {
const MachineRegisterInfo *MRI;
const TargetRegisterInfo *TRI;
+ const TargetInstrInfo *TII;
public:
static char ID;
@@ -68,16 +70,17 @@
INITIALIZE_PASS(RISCVRedundantCopyElimination, "riscv-copyelim",
"RISCV redundant copy elimination pass", false, false)
-static bool guaranteesZeroRegInBlock(const MachineInstr &MI,
- const MachineBasicBlock &MBB) {
- unsigned Opc = MI.getOpcode();
- if (Opc == RISCV::BEQ && MI.getOperand(1).getReg() == RISCV::X0 &&
- &MBB == MI.getOperand(2).getMBB())
+static bool
+guaranteesZeroRegInBlock(MachineBasicBlock &MBB,
+ const SmallVectorImpl<MachineOperand> &Cond,
+ MachineBasicBlock *TBB) {
+ assert(Cond.size() == 3 && "Unexpected number of operands");
+ assert(TBB != nullptr && "Expected branch target basic block");
+ auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
+ if (CC == RISCVCC::COND_EQ && Cond[2].getReg() == RISCV::X0 && TBB == &MBB)
return true;
- if (Opc == RISCV::BNE && MI.getOperand(1).getReg() == RISCV::X0 &&
- &MBB != MI.getOperand(2).getMBB())
+ if (CC == RISCVCC::COND_NE && Cond[2].getReg() == RISCV::X0 && TBB != &MBB)
return true;
-
return false;
}
@@ -92,24 +95,17 @@
if (PredMBB->succ_size() != 2)
return false;
- MachineBasicBlock::iterator CondBr = PredMBB->getLastNonDebugInstr();
- if (CondBr == PredMBB->end())
+ MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
+ SmallVector<MachineOperand, 3> Cond;
+ if (TII->analyzeBranch(*PredMBB, TBB, FBB, Cond, /*AllowModify*/ false) ||
+ Cond.empty())
return false;
- while (true) {
- // If we run out of terminators, give up.
- if (!CondBr->isTerminator())
- return false;
- // If we found a branch with X0, stop searching and try to remove copies.
- if (guaranteesZeroRegInBlock(*CondBr, MBB))
- break;
- // If we reached the beginning of the basic block, give up.
- if (CondBr == PredMBB->begin())
- return false;
- --CondBr;
- }
+ // Is this a branch with X0?
+ if (!guaranteesZeroRegInBlock(MBB, Cond, TBB))
+ return false;
- Register TargetReg = CondBr->getOperand(0).getReg();
+ Register TargetReg = Cond[1].getReg();
if (!TargetReg)
return false;
@@ -146,7 +142,7 @@
// Otherwise, we have to fixup the use-def chain, starting with the
// BEQ/BNE. Conservatively mark as much as we can live.
- CondBr->clearRegisterKills(TargetReg, TRI);
+ Cond[1].setIsKill(false);
// Add newly used reg to the block's live-in list if it isn't there already.
if (!MBB.isLiveIn(TargetReg))
@@ -163,6 +159,7 @@
if (skipFunction(MF.getFunction()))
return false;
+ TII = MF.getSubtarget().getInstrInfo();
TRI = MF.getSubtarget().getRegisterInfo();
MRI = &MF.getRegInfo();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132347.454382.patch
Type: text/x-patch
Size: 3474 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220822/46e2321a/attachment.bin>
More information about the llvm-commits
mailing list