[llvm] 6b7e65a - [CodeGen] Simplify finalizeBundle. NFC. (#139234)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 9 08:10:55 PDT 2025
Author: Jay Foad
Date: 2025-05-09T16:10:52+01:00
New Revision: 6b7e65a1115bbeb4d881d2ac0a3adaa86d47d598
URL: https://github.com/llvm/llvm-project/commit/6b7e65a1115bbeb4d881d2ac0a3adaa86d47d598
DIFF: https://github.com/llvm/llvm-project/commit/6b7e65a1115bbeb4d881d2ac0a3adaa86d47d598.diff
LOG: [CodeGen] Simplify finalizeBundle. NFC. (#139234)
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.
Added:
Modified:
llvm/lib/CodeGen/MachineInstrBundle.cpp
Removed:
################################################################################
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