[llvm] [RISCV] Use RISCVInstrInfo::movImm to implement most of RISCVPostRAExpandPseudo::expandMovImm (PR #70389)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 26 15:55:08 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Craig Topper (topperc)
<details>
<summary>Changes</summary>
There are two commits. The first is a bit of refactoring to increase similarity and fix a bug that was already fixed in movImm. The second does the merging
---
Full diff: https://github.com/llvm/llvm-project/pull/70389.diff
3 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.cpp (+23-9)
- (modified) llvm/lib/Target/RISCV/RISCVInstrInfo.h (+2-1)
- (modified) llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp (+4-43)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index 0abf302bb25e310..e9c87f7b7b574cc 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -737,7 +737,8 @@ MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, Register DstReg, uint64_t Val,
- MachineInstr::MIFlag Flag) const {
+ MachineInstr::MIFlag Flag, bool DstRenamable,
+ bool DstIsDead) const {
Register SrcReg = RISCV::X0;
if (!STI.is64Bit() && !isInt<32>(Val))
@@ -747,28 +748,40 @@ void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
RISCVMatInt::generateInstSeq(Val, STI.getFeatureBits());
assert(!Seq.empty());
+ bool SrcRenamable = false;
+ unsigned Num = 0;
+
for (const RISCVMatInt::Inst &Inst : Seq) {
+ bool LastItem = ++Num == Seq.size();
+ unsigned DstRegState = getDeadRegState(DstIsDead && LastItem) |
+ getRenamableRegState(DstRenamable);
+ unsigned SrcRegState = getKillRegState(SrcReg != RISCV::X0) |
+ getRenamableRegState(SrcRenamable);
switch (Inst.getOpndKind()) {
case RISCVMatInt::Imm:
- BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()), DstReg)
+ BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
+ .addReg(DstReg, RegState::Define | DstRegState)
.addImm(Inst.getImm())
.setMIFlag(Flag);
break;
case RISCVMatInt::RegX0:
- BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()), DstReg)
- .addReg(SrcReg, getKillRegState(SrcReg != RISCV::X0))
+ BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
+ .addReg(DstReg, RegState::Define | DstRegState)
+ .addReg(SrcReg, SrcRegState)
.addReg(RISCV::X0)
.setMIFlag(Flag);
break;
case RISCVMatInt::RegReg:
- BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()), DstReg)
- .addReg(SrcReg, getKillRegState(SrcReg != RISCV::X0))
- .addReg(SrcReg, getKillRegState(SrcReg != RISCV::X0))
+ BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
+ .addReg(DstReg, RegState::Define | DstRegState)
+ .addReg(SrcReg, SrcRegState)
+ .addReg(SrcReg, SrcRegState)
.setMIFlag(Flag);
break;
case RISCVMatInt::RegImm:
- BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()), DstReg)
- .addReg(SrcReg, getKillRegState(SrcReg != RISCV::X0))
+ BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
+ .addReg(DstReg, RegState::Define | DstRegState)
+ .addReg(SrcReg, SrcRegState)
.addImm(Inst.getImm())
.setMIFlag(Flag);
break;
@@ -776,6 +789,7 @@ void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
// Only the first instruction has X0 as its source.
SrcReg = DstReg;
+ SrcRenamable = DstRenamable;
}
}
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 5584e5571c9bc35..5c33c5b6a5a1b2a 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -91,7 +91,8 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
// Materializes the given integer Val into DstReg.
void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, Register DstReg, uint64_t Val,
- MachineInstr::MIFlag Flag = MachineInstr::NoFlags) const;
+ MachineInstr::MIFlag Flag = MachineInstr::NoFlags,
+ bool DstRenamable = false, bool DstIsDead = false) const;
unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
diff --git a/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp b/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp
index 407e7cfd6fef830..bc9b66d6ca6b114 100644
--- a/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/RISCV/RISCVPostRAExpandPseudoInsts.cpp
@@ -92,52 +92,13 @@ bool RISCVPostRAExpandPseudo::expandMovImm(MachineBasicBlock &MBB,
Val, MBB.getParent()->getSubtarget().getFeatureBits());
assert(!Seq.empty());
- Register SrcReg = RISCV::X0;
Register DstReg = MBBI->getOperand(0).getReg();
bool DstIsDead = MBBI->getOperand(0).isDead();
bool Renamable = MBBI->getOperand(0).isRenamable();
- bool SrcRenamable = false;
- unsigned Num = 0;
-
- for (RISCVMatInt::Inst &Inst : Seq) {
- bool LastItem = ++Num == Seq.size();
- switch (Inst.getOpndKind()) {
- case RISCVMatInt::Imm:
- BuildMI(MBB, MBBI, DL, TII->get(Inst.getOpcode()))
- .addReg(DstReg, RegState::Define |
- getDeadRegState(DstIsDead && LastItem) |
- getRenamableRegState(Renamable))
- .addImm(Inst.getImm());
- break;
- case RISCVMatInt::RegX0:
- BuildMI(MBB, MBBI, DL, TII->get(Inst.getOpcode()))
- .addReg(DstReg, RegState::Define |
- getDeadRegState(DstIsDead && LastItem) |
- getRenamableRegState(Renamable))
- .addReg(SrcReg, RegState::Kill | getRenamableRegState(SrcRenamable))
- .addReg(RISCV::X0);
- break;
- case RISCVMatInt::RegReg:
- BuildMI(MBB, MBBI, DL, TII->get(Inst.getOpcode()))
- .addReg(DstReg, RegState::Define |
- getDeadRegState(DstIsDead && LastItem) |
- getRenamableRegState(Renamable))
- .addReg(SrcReg, RegState::Kill | getRenamableRegState(SrcRenamable))
- .addReg(SrcReg, RegState::Kill | getRenamableRegState(SrcRenamable));
- break;
- case RISCVMatInt::RegImm:
- BuildMI(MBB, MBBI, DL, TII->get(Inst.getOpcode()))
- .addReg(DstReg, RegState::Define |
- getDeadRegState(DstIsDead && LastItem) |
- getRenamableRegState(Renamable))
- .addReg(SrcReg, RegState::Kill | getRenamableRegState(SrcRenamable))
- .addImm(Inst.getImm());
- break;
- }
- // Only the first instruction has X0 as its source.
- SrcReg = DstReg;
- SrcRenamable = Renamable;
- }
+
+ TII->movImm(MBB, MBBI, DL, DstReg, Val, MachineInstr::NoFlags, Renamable,
+ DstIsDead);
+
MBBI->eraseFromParent();
return true;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/70389
More information about the llvm-commits
mailing list