@@ -33,6 +33,8 @@ STATISTIC(NumCoalesces, "Number of copies coalesced");
33
33
STATISTIC (NumCSEs, " Number of common subexpression eliminated" );
34
34
STATISTIC (NumPhysCSEs,
35
35
" Number of physreg referencing common subexpr eliminated" );
36
+ STATISTIC (NumCrossBlockPhysCSEs,
37
+ " Number of physreg common subexprs cross-block eliminated" );
36
38
STATISTIC (NumCommutes, " Number of copies coalesced after commuting" );
37
39
38
40
namespace {
@@ -82,7 +84,8 @@ namespace {
82
84
MachineBasicBlock::const_iterator E) const ;
83
85
bool hasLivePhysRegDefUses (const MachineInstr *MI,
84
86
const MachineBasicBlock *MBB,
85
- SmallSet<unsigned ,8 > &PhysRefs) const ;
87
+ SmallSet<unsigned ,8 > &PhysRefs,
88
+ SmallVector<unsigned ,8 > &PhysDefs) const ;
86
89
bool PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
87
90
SmallSet<unsigned ,8 > &PhysRefs) const ;
88
91
bool isCSECandidate (MachineInstr *MI);
@@ -189,7 +192,8 @@ MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
189
192
// / instruction does not uses a physical register.
190
193
bool MachineCSE::hasLivePhysRegDefUses (const MachineInstr *MI,
191
194
const MachineBasicBlock *MBB,
192
- SmallSet<unsigned ,8 > &PhysRefs) const {
195
+ SmallSet<unsigned ,8 > &PhysRefs,
196
+ SmallVector<unsigned ,8 > &PhysDefs) const {
193
197
MachineBasicBlock::const_iterator I = MI; I = llvm::next (I);
194
198
for (unsigned i = 0 , e = MI->getNumOperands (); i != e; ++i) {
195
199
const MachineOperand &MO = MI->getOperand (i);
@@ -206,6 +210,7 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
206
210
if (MO.isDef () &&
207
211
(MO.isDead () || isPhysDefTriviallyDead (Reg, I, MBB->end ())))
208
212
continue ;
213
+ PhysDefs.push_back (Reg);
209
214
PhysRefs.insert (Reg);
210
215
for (const unsigned *Alias = TRI->getAliasSet (Reg); *Alias; ++Alias)
211
216
PhysRefs.insert (*Alias);
@@ -216,35 +221,43 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
216
221
217
222
bool MachineCSE::PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
218
223
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;
224
+ // Look backward from MI to find CSMI.
226
225
unsigned LookAheadLeft = LookAheadLimit;
226
+ MachineBasicBlock *CurBB = MI->getParent ();
227
+ MachineBasicBlock::const_reverse_iterator I (MI);
228
+ MachineBasicBlock::const_reverse_iterator E (CurBB->rend ());
227
229
while (LookAheadLeft) {
228
- // Skip over dbg_value's.
229
- while (I != E && I->isDebugValue ())
230
- ++I;
230
+ while (LookAheadLeft && I != E) {
231
+ // Skip over dbg_value's.
232
+ while (I != E && I->isDebugValue ())
233
+ ++I;
231
234
232
- if (I == E)
233
- return true ;
235
+ if (I == E) break ;
234
236
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
- }
237
+ if (&*I == CSMI)
238
+ return true ;
245
239
246
- --LookAheadLeft;
247
- ++I;
240
+ for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
241
+ const MachineOperand &MO = I->getOperand (i);
242
+ if (!MO.isReg () || !MO.isDef ())
243
+ continue ;
244
+ unsigned MOReg = MO.getReg ();
245
+ if (TargetRegisterInfo::isVirtualRegister (MOReg))
246
+ continue ;
247
+ if (PhysRefs.count (MOReg))
248
+ return false ;
249
+ }
250
+
251
+ --LookAheadLeft;
252
+ ++I;
253
+ }
254
+ // Go back another BB; for now, only go back at most one BB.
255
+ MachineBasicBlock *CSBB = CSMI->getParent ();
256
+ if (!CSBB->isSuccessor (CurBB) || CurBB->pred_size () != 1 )
257
+ return false ;
258
+ CurBB = CSBB;
259
+ I = CSBB->rbegin ();
260
+ E = CSBB->rend ();
248
261
}
249
262
250
263
return false ;
@@ -395,7 +408,8 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
395
408
// used, then it's not safe to replace it with a common subexpression.
396
409
// It's also not safe if the instruction uses physical registers.
397
410
SmallSet<unsigned ,8 > PhysRefs;
398
- if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs)) {
411
+ SmallVector<unsigned ,8 > DirectPhysRefs;
412
+ if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs, DirectPhysRefs)) {
399
413
FoundCSE = false ;
400
414
401
415
// ... Unless the CS is local and it also defines the physical register
@@ -448,6 +462,14 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
448
462
MRI->clearKillFlags (CSEPairs[i].second );
449
463
}
450
464
MI->eraseFromParent ();
465
+ if (!DirectPhysRefs.empty () && CSMI->getParent () != MBB) {
466
+ assert (CSMI->getParent ()->isSuccessor (MBB));
467
+ ++NumCrossBlockPhysCSEs;
468
+ SmallVector<unsigned ,8 >::iterator PI = DirectPhysRefs.begin (),
469
+ PE = DirectPhysRefs.end ();
470
+ for (; PI != PE; ++PI)
471
+ MBB->addLiveIn (*PI);
472
+ }
451
473
++NumCSEs;
452
474
if (!PhysRefs.empty ())
453
475
++NumPhysCSEs;
0 commit comments