@@ -82,7 +82,8 @@ namespace {
82
82
MachineBasicBlock::const_iterator E) const ;
83
83
bool hasLivePhysRegDefUses (const MachineInstr *MI,
84
84
const MachineBasicBlock *MBB,
85
- SmallSet<unsigned ,8 > &PhysRefs) const ;
85
+ SmallSet<unsigned ,8 > &PhysRefs,
86
+ SmallVector<unsigned ,8 > &PhysDefs) const ;
86
87
bool PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
87
88
SmallSet<unsigned ,8 > &PhysRefs) const ;
88
89
bool isCSECandidate (MachineInstr *MI);
@@ -189,7 +190,8 @@ MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
189
190
// / instruction does not uses a physical register.
190
191
bool MachineCSE::hasLivePhysRegDefUses (const MachineInstr *MI,
191
192
const MachineBasicBlock *MBB,
192
- SmallSet<unsigned ,8 > &PhysRefs) const {
193
+ SmallSet<unsigned ,8 > &PhysRefs,
194
+ SmallVector<unsigned ,8 > &PhysDefs) const {
193
195
MachineBasicBlock::const_iterator I = MI; I = llvm::next (I);
194
196
for (unsigned i = 0 , e = MI->getNumOperands (); i != e; ++i) {
195
197
const MachineOperand &MO = MI->getOperand (i);
@@ -206,6 +208,7 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
206
208
if (MO.isDef () &&
207
209
(MO.isDead () || isPhysDefTriviallyDead (Reg, I, MBB->end ())))
208
210
continue ;
211
+ PhysDefs.push_back (Reg);
209
212
PhysRefs.insert (Reg);
210
213
for (const unsigned *Alias = TRI->getAliasSet (Reg); *Alias; ++Alias)
211
214
PhysRefs.insert (*Alias);
@@ -216,35 +219,40 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
216
219
217
220
bool MachineCSE::PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
218
221
SmallSet<unsigned ,8 > &PhysRefs) const {
219
- // For now conservatively returns false if the common subexpression is
220
- // not in the same basic block as the given instruction.
221
- MachineBasicBlock *MBB = MI->getParent ();
222
- if (CSMI->getParent () != MBB)
223
- return false ;
224
- MachineBasicBlock::const_iterator I = CSMI; I = llvm::next (I);
225
- MachineBasicBlock::const_iterator E = MI;
222
+ // Look backward from MI to find CSMI.
226
223
unsigned LookAheadLeft = LookAheadLimit;
224
+ MachineBasicBlock::const_reverse_iterator I (MI);
225
+ MachineBasicBlock::const_reverse_iterator E (MI->getParent ()->rend ());
227
226
while (LookAheadLeft) {
228
- // Skip over dbg_value's.
229
- while (I != E && I->isDebugValue ())
230
- ++I;
227
+ while (LookAheadLeft && I != E) {
228
+ // Skip over dbg_value's.
229
+ while (I != E && I->isDebugValue ())
230
+ ++I;
231
231
232
- if (I == E )
233
- return true ;
232
+ if (&* I == CSMI )
233
+ return true ;
234
234
235
- for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
236
- const MachineOperand &MO = I->getOperand (i);
237
- if (!MO.isReg () || !MO.isDef ())
238
- continue ;
239
- unsigned MOReg = MO.getReg ();
240
- if (TargetRegisterInfo::isVirtualRegister (MOReg))
241
- continue ;
242
- if (PhysRefs.count (MOReg))
243
- return false ;
244
- }
235
+ for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
236
+ const MachineOperand &MO = I->getOperand (i);
237
+ if (!MO.isReg () || !MO.isDef ())
238
+ continue ;
239
+ unsigned MOReg = MO.getReg ();
240
+ if (TargetRegisterInfo::isVirtualRegister (MOReg))
241
+ continue ;
242
+ if (PhysRefs.count (MOReg))
243
+ return false ;
244
+ }
245
245
246
- --LookAheadLeft;
247
- ++I;
246
+ --LookAheadLeft;
247
+ ++I;
248
+ }
249
+ // Go back another BB; for now, only go back at most one BB.
250
+ MachineBasicBlock *CSBB = CSMI->getParent ();
251
+ MachineBasicBlock *BB = MI->getParent ();
252
+ if (!CSBB->isSuccessor (BB) || BB->pred_size () != 1 )
253
+ return false ;
254
+ I = CSBB->rbegin ();
255
+ E = CSBB->rend ();
248
256
}
249
257
250
258
return false ;
@@ -395,7 +403,8 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
395
403
// used, then it's not safe to replace it with a common subexpression.
396
404
// It's also not safe if the instruction uses physical registers.
397
405
SmallSet<unsigned ,8 > PhysRefs;
398
- if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs)) {
406
+ SmallVector<unsigned ,8 > DirectPhysRefs;
407
+ if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs, DirectPhysRefs)) {
399
408
FoundCSE = false ;
400
409
401
410
// ... Unless the CS is local and it also defines the physical register
@@ -448,6 +457,13 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
448
457
MRI->clearKillFlags (CSEPairs[i].second );
449
458
}
450
459
MI->eraseFromParent ();
460
+ if (!DirectPhysRefs.empty () && CSMI->getParent () != MBB) {
461
+ assert (CSMI->getParent ()->isSuccessor (MBB));
462
+ SmallVector<unsigned ,8 >::iterator PI = DirectPhysRefs.begin (),
463
+ PE = DirectPhysRefs.end ();
464
+ for (; PI != PE; ++PI)
465
+ MBB->addLiveIn (*PI);
466
+ }
451
467
++NumCSEs;
452
468
if (!PhysRefs.empty ())
453
469
++NumPhysCSEs;
0 commit comments