@@ -94,6 +94,7 @@ namespace {
94
94
ScopedHashTable<MachineInstr *, unsigned , MachineInstrExpressionTrait,
95
95
AllocatorTy>;
96
96
using ScopeType = ScopedHTType::ScopeTy;
97
+ using PhysDefVector = SmallVector<std::pair<unsigned , unsigned >, 2 >;
97
98
98
99
unsigned LookAheadLimit = 0 ;
99
100
DenseMap<MachineBasicBlock *, ScopeType *> ScopeMap;
@@ -108,13 +109,11 @@ namespace {
108
109
MachineBasicBlock::const_iterator E) const ;
109
110
bool hasLivePhysRegDefUses (const MachineInstr *MI,
110
111
const MachineBasicBlock *MBB,
111
- SmallSet<unsigned ,8 > &PhysRefs,
112
- SmallVectorImpl<unsigned > &PhysDefs,
113
- bool &PhysUseDef) const ;
112
+ SmallSet<unsigned , 8 > &PhysRefs,
113
+ PhysDefVector &PhysDefs, bool &PhysUseDef) const ;
114
114
bool PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
115
- SmallSet<unsigned ,8 > &PhysRefs,
116
- SmallVectorImpl<unsigned > &PhysDefs,
117
- bool &NonLocal) const ;
115
+ SmallSet<unsigned , 8 > &PhysRefs,
116
+ PhysDefVector &PhysDefs, bool &NonLocal) const ;
118
117
bool isCSECandidate (MachineInstr *MI);
119
118
bool isProfitableToCSE (unsigned CSReg, unsigned Reg,
120
119
MachineInstr *CSMI, MachineInstr *MI);
@@ -255,9 +254,9 @@ static bool isCallerPreservedOrConstPhysReg(unsigned Reg,
255
254
// / instruction does not uses a physical register.
256
255
bool MachineCSE::hasLivePhysRegDefUses (const MachineInstr *MI,
257
256
const MachineBasicBlock *MBB,
258
- SmallSet<unsigned ,8 > &PhysRefs,
259
- SmallVectorImpl< unsigned > &PhysDefs,
260
- bool &PhysUseDef) const {
257
+ SmallSet<unsigned , 8 > &PhysRefs,
258
+ PhysDefVector &PhysDefs,
259
+ bool &PhysUseDef) const {
261
260
// First, add all uses to PhysRefs.
262
261
for (const MachineOperand &MO : MI->operands ()) {
263
262
if (!MO.isReg () || MO.isDef ())
@@ -277,7 +276,8 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
277
276
// (which currently contains only uses), set the PhysUseDef flag.
278
277
PhysUseDef = false ;
279
278
MachineBasicBlock::const_iterator I = MI; I = std::next (I);
280
- for (const MachineOperand &MO : MI->operands ()) {
279
+ for (const auto &MOP : llvm::enumerate (MI->operands ())) {
280
+ const MachineOperand &MO = MOP.value ();
281
281
if (!MO.isReg () || !MO.isDef ())
282
282
continue ;
283
283
unsigned Reg = MO.getReg ();
@@ -292,20 +292,21 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
292
292
// common since this pass is run before livevariables. We can scan
293
293
// forward a few instructions and check if it is obviously dead.
294
294
if (!MO.isDead () && !isPhysDefTriviallyDead (Reg, I, MBB->end ()))
295
- PhysDefs.push_back (Reg);
295
+ PhysDefs.push_back (std::make_pair (MOP. index (), Reg) );
296
296
}
297
297
298
298
// Finally, add all defs to PhysRefs as well.
299
299
for (unsigned i = 0 , e = PhysDefs.size (); i != e; ++i)
300
- for (MCRegAliasIterator AI (PhysDefs[i], TRI, true ); AI.isValid (); ++AI)
300
+ for (MCRegAliasIterator AI (PhysDefs[i].second , TRI, true ); AI.isValid ();
301
+ ++AI)
301
302
PhysRefs.insert (*AI);
302
303
303
304
return !PhysRefs.empty ();
304
305
}
305
306
306
307
bool MachineCSE::PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
307
- SmallSet<unsigned ,8 > &PhysRefs,
308
- SmallVectorImpl< unsigned > &PhysDefs,
308
+ SmallSet<unsigned , 8 > &PhysRefs,
309
+ PhysDefVector &PhysDefs,
309
310
bool &NonLocal) const {
310
311
// For now conservatively returns false if the common subexpression is
311
312
// not in the same basic block as the given instruction. The only exception
@@ -319,7 +320,8 @@ bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
319
320
return false ;
320
321
321
322
for (unsigned i = 0 , e = PhysDefs.size (); i != e; ++i) {
322
- if (MRI->isAllocatable (PhysDefs[i]) || MRI->isReserved (PhysDefs[i]))
323
+ if (MRI->isAllocatable (PhysDefs[i].second ) ||
324
+ MRI->isReserved (PhysDefs[i].second ))
323
325
// Avoid extending live range of physical registers if they are
324
326
// allocatable or reserved.
325
327
return false ;
@@ -535,7 +537,7 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
535
537
// It's also not safe if the instruction uses physical registers.
536
538
bool CrossMBBPhysDef = false ;
537
539
SmallSet<unsigned , 8 > PhysRefs;
538
- SmallVector< unsigned , 2 > PhysDefs;
540
+ PhysDefVector PhysDefs;
539
541
bool PhysUseDef = false ;
540
542
if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs,
541
543
PhysDefs, PhysUseDef)) {
@@ -634,6 +636,9 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
634
636
// we should make sure it is not dead at CSMI.
635
637
for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
636
638
CSMI->getOperand (ImplicitDefToUpdate).setIsDead (false );
639
+ for (auto PhysDef : PhysDefs)
640
+ if (!MI->getOperand (PhysDef.first ).isDead ())
641
+ CSMI->getOperand (PhysDef.first ).setIsDead (false );
637
642
638
643
// Go through implicit defs of CSMI and MI, and clear the kill flags on
639
644
// their uses in all the instructions between CSMI and MI.
@@ -662,9 +667,9 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
662
667
// Add physical register defs now coming in from a predecessor to MBB
663
668
// livein list.
664
669
while (!PhysDefs.empty ()) {
665
- unsigned LiveIn = PhysDefs.pop_back_val ();
666
- if (!MBB->isLiveIn (LiveIn))
667
- MBB->addLiveIn (LiveIn);
670
+ auto LiveIn = PhysDefs.pop_back_val ();
671
+ if (!MBB->isLiveIn (LiveIn. second ))
672
+ MBB->addLiveIn (LiveIn. second );
668
673
}
669
674
++NumCrossBBCSEs;
670
675
}
0 commit comments