Skip to content

Commit 6b7e65a

Browse files
authored
[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.
1 parent 7e64ade commit 6b7e65a

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

llvm/lib/CodeGen/MachineInstrBundle.cpp

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/CodeGen/MachineInstrBundle.h"
10+
#include "llvm/ADT/SetVector.h"
1011
#include "llvm/ADT/SmallSet.h"
1112
#include "llvm/ADT/SmallVector.h"
1213
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -134,103 +135,82 @@ void llvm::finalizeBundle(MachineBasicBlock &MBB,
134135
BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE));
135136
Bundle.prepend(MIB);
136137

137-
SmallVector<Register, 32> LocalDefs;
138-
SmallSet<Register, 32> LocalDefSet;
138+
SmallSetVector<Register, 32> LocalDefs;
139139
SmallSet<Register, 8> DeadDefSet;
140140
SmallSet<Register, 16> KilledDefSet;
141-
SmallVector<Register, 8> ExternUses;
142-
SmallSet<Register, 8> ExternUseSet;
141+
SmallSetVector<Register, 8> ExternUses;
143142
SmallSet<Register, 8> KilledUseSet;
144143
SmallSet<Register, 8> UndefUseSet;
145-
SmallVector<MachineOperand*, 4> Defs;
146144
for (auto MII = FirstMI; MII != LastMI; ++MII) {
147145
// Debug instructions have no effects to track.
148146
if (MII->isDebugInstr())
149147
continue;
150148

151-
for (MachineOperand &MO : MII->operands()) {
152-
if (!MO.isReg())
153-
continue;
154-
if (MO.isDef()) {
155-
Defs.push_back(&MO);
156-
continue;
157-
}
158-
149+
for (MachineOperand &MO : MII->all_uses()) {
159150
Register Reg = MO.getReg();
160151
if (!Reg)
161152
continue;
162153

163-
if (LocalDefSet.count(Reg)) {
154+
if (LocalDefs.contains(Reg)) {
164155
MO.setIsInternalRead();
165-
if (MO.isKill())
156+
if (MO.isKill()) {
166157
// Internal def is now killed.
167158
KilledDefSet.insert(Reg);
159+
}
168160
} else {
169-
if (ExternUseSet.insert(Reg).second) {
170-
ExternUses.push_back(Reg);
161+
if (ExternUses.insert(Reg)) {
171162
if (MO.isUndef())
172163
UndefUseSet.insert(Reg);
173164
}
174-
if (MO.isKill())
165+
if (MO.isKill()) {
175166
// External def is now killed.
176167
KilledUseSet.insert(Reg);
168+
}
177169
}
178170
}
179171

180-
for (MachineOperand *MO : Defs) {
181-
Register Reg = MO->getReg();
172+
for (MachineOperand &MO : MII->all_defs()) {
173+
Register Reg = MO.getReg();
182174
if (!Reg)
183175
continue;
184176

185-
if (LocalDefSet.insert(Reg).second) {
186-
LocalDefs.push_back(Reg);
187-
if (MO->isDead()) {
177+
if (LocalDefs.insert(Reg)) {
178+
if (MO.isDead())
188179
DeadDefSet.insert(Reg);
189-
}
190180
} else {
191181
// Re-defined inside the bundle, it's no longer killed.
192182
KilledDefSet.erase(Reg);
193-
if (!MO->isDead())
183+
if (!MO.isDead()) {
194184
// Previously defined but dead.
195185
DeadDefSet.erase(Reg);
196-
}
197-
198-
if (!MO->isDead() && Reg.isPhysical()) {
199-
for (MCPhysReg SubReg : TRI->subregs(Reg)) {
200-
if (LocalDefSet.insert(SubReg).second)
201-
LocalDefs.push_back(SubReg);
202186
}
203187
}
188+
189+
if (!MO.isDead() && Reg.isPhysical())
190+
LocalDefs.insert_range(TRI->subregs(Reg));
204191
}
205192

206-
Defs.clear();
193+
// Set FrameSetup/FrameDestroy for the bundle. If any of the instructions
194+
// got the property, then also set it on the bundle.
195+
if (MII->getFlag(MachineInstr::FrameSetup))
196+
MIB.setMIFlag(MachineInstr::FrameSetup);
197+
if (MII->getFlag(MachineInstr::FrameDestroy))
198+
MIB.setMIFlag(MachineInstr::FrameDestroy);
207199
}
208200

209-
SmallSet<Register, 32> Added;
210201
for (Register Reg : LocalDefs) {
211-
if (Added.insert(Reg).second) {
212-
// If it's not live beyond end of the bundle, mark it dead.
213-
bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg);
214-
MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
215-
getImplRegState(true));
216-
}
202+
// If it's not live beyond end of the bundle, mark it dead.
203+
bool isDead = DeadDefSet.contains(Reg) || KilledDefSet.contains(Reg);
204+
MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) |
205+
getImplRegState(true));
217206
}
218207

219208
for (Register Reg : ExternUses) {
220-
bool isKill = KilledUseSet.count(Reg);
221-
bool isUndef = UndefUseSet.count(Reg);
209+
bool isKill = KilledUseSet.contains(Reg);
210+
bool isUndef = UndefUseSet.contains(Reg);
222211
MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) |
223212
getImplRegState(true));
224213
}
225-
226-
// Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got
227-
// the property, then also set it on the bundle.
228-
for (auto MII = FirstMI; MII != LastMI; ++MII) {
229-
if (MII->getFlag(MachineInstr::FrameSetup))
230-
MIB.setMIFlag(MachineInstr::FrameSetup);
231-
if (MII->getFlag(MachineInstr::FrameDestroy))
232-
MIB.setMIFlag(MachineInstr::FrameDestroy);
233-
}
234214
}
235215

236216
/// finalizeBundle - Same functionality as the previous finalizeBundle except

0 commit comments

Comments
 (0)