[llvm] [CodeGen] Simplify finalizeBundle. NFC. (PR #139234)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Fri May 9 06:30:22 PDT 2025
https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/139234
>From a213065d18fb90bdac0260cafa3eb8e53f21c931 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Fri, 9 May 2025 10:32:37 +0100
Subject: [PATCH] [CodeGen] Simplify finalizeBundle. NFC.
Use all_uses and all_defs instead of separate Defs vector. Use
SmallSetVector instead of separate SmallSet and SmallVector. Remove
unneeded `Added` set. Fold FrameSetup/FrameDestroy into the main loop
instead of doing a separate loop over the bundled instructions.
---
llvm/lib/CodeGen/MachineInstrBundle.cpp | 80 ++++++++++---------------
1 file changed, 30 insertions(+), 50 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineInstrBundle.cpp b/llvm/lib/CodeGen/MachineInstrBundle.cpp
index 92189f6360683..34896c67144bc 100644
--- a/llvm/lib/CodeGen/MachineInstrBundle.cpp
+++ b/llvm/lib/CodeGen/MachineInstrBundle.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/MachineInstrBundle.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -134,103 +135,82 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
Bundle.prepend(MIB);
- SmallVector<Register, 32> LocalDefs;
- SmallSet<Register, 32> LocalDefSet;
+ SmallSetVector<Register, 32> LocalDefs;
SmallSet<Register, 8> DeadDefSet;
SmallSet<Register, 16> KilledDefSet;
- SmallVector<Register, 8> ExternUses;
- SmallSet<Register, 8> ExternUseSet;
+ SmallSetVector<Register, 8> ExternUses;
SmallSet<Register, 8> KilledUseSet;
SmallSet<Register, 8> UndefUseSet;
- SmallVector<MachineOperand*, 4> Defs;
for (auto MII = FirstMI; MII != LastMI; ++MII) {
// Debug instructions have no effects to track.
if (MII->isDebugInstr())
continue;
- for (MachineOperand &MO : MII->operands()) {
- if (!MO.isReg())
- continue;
- if (MO.isDef()) {
- Defs.push_back(&MO);
- continue;
- }
-
+ for (MachineOperand &MO : MII->all_uses()) {
Register Reg = MO.getReg();
if (!Reg)
continue;
- if (LocalDefSet.count(Reg)) {
+ if (LocalDefs.contains(Reg)) {
MO.setIsInternalRead();
- if (MO.isKill())
+ if (MO.isKill()) {
// Internal def is now killed.
KilledDefSet.insert(Reg);
+ }
} else {
- if (ExternUseSet.insert(Reg).second) {
- ExternUses.push_back(Reg);
+ if (ExternUses.insert(Reg)) {
if (MO.isUndef())
UndefUseSet.insert(Reg);
}
- if (MO.isKill())
+ if (MO.isKill()) {
// External def is now killed.
KilledUseSet.insert(Reg);
+ }
}
}
- for (MachineOperand *MO : Defs) {
- Register Reg = MO->getReg();
+ for (MachineOperand &MO : MII->all_defs()) {
+ Register Reg = MO.getReg();
if (!Reg)
continue;
- if (LocalDefSet.insert(Reg).second) {
- LocalDefs.push_back(Reg);
- if (MO->isDead()) {
+ if (LocalDefs.insert(Reg)) {
+ if (MO.isDead())
DeadDefSet.insert(Reg);
- }
} else {
// Re-defined inside the bundle, it's no longer killed.
KilledDefSet.erase(Reg);
- if (!MO->isDead())
+ if (!MO.isDead()) {
// Previously defined but dead.
DeadDefSet.erase(Reg);
- }
-
- if (!MO->isDead() && Reg.isPhysical()) {
- for (MCPhysReg SubReg : TRI->subregs(Reg)) {
- if (LocalDefSet.insert(SubReg).second)
- LocalDefs.push_back(SubReg);
}
}
+
+ if (!MO.isDead() && Reg.isPhysical())
+ LocalDefs.insert_range(TRI->subregs(Reg));
}
- Defs.clear();
+ // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions
+ // got the property, then also set it on the bundle.
+ if (MII->getFlag(MachineInstr::FrameSetup))
+ MIB.setMIFlag(MachineInstr::FrameSetup);
+ if (MII->getFlag(MachineInstr::FrameDestroy))
+ MIB.setMIFlag(MachineInstr::FrameDestroy);
}
- SmallSet<Register, 32> Added;
for (Register Reg : LocalDefs) {
- if (Added.insert(Reg).second) {
- // If it's not live beyond end of the bundle, mark it dead.
- bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
- MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
- getImplRegState(true));
- }
+ // If it's not live beyond end of the bundle, mark it dead.
+ bool isDead = DeadDefSet.contains(Reg) || KilledDefSet.contains(Reg);
+ MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
+ getImplRegState(true));
}
for (Register Reg : ExternUses) {
- bool isKill = KilledUseSet.count(Reg);
- bool isUndef = UndefUseSet.count(Reg);
+ bool isKill = KilledUseSet.contains(Reg);
+ bool isUndef = UndefUseSet.contains(Reg);
MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
getImplRegState(true));
}
-
- // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
- // the property, then also set it on the bundle.
- for (auto MII = FirstMI; MII != LastMI; ++MII) {
- if (MII->getFlag(MachineInstr::FrameSetup))
- MIB.setMIFlag(MachineInstr::FrameSetup);
- if (MII->getFlag(MachineInstr::FrameDestroy))
- MIB.setMIFlag(MachineInstr::FrameDestroy);
- }
}
/// finalizeBundle - Same functionality as the previous finalizeBundle except
More information about the llvm-commits
mailing list