@@ -82,8 +82,7 @@ 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,
86
- SmallVector<unsigned ,8 > &PhysDefs) const ;
85
+ SmallSet<unsigned ,8 > &PhysRefs) const ;
87
86
bool PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
88
87
SmallSet<unsigned ,8 > &PhysRefs) const ;
89
88
bool isCSECandidate (MachineInstr *MI);
@@ -190,8 +189,7 @@ MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
190
189
// / instruction does not uses a physical register.
191
190
bool MachineCSE::hasLivePhysRegDefUses (const MachineInstr *MI,
192
191
const MachineBasicBlock *MBB,
193
- SmallSet<unsigned ,8 > &PhysRefs,
194
- SmallVector<unsigned ,8 > &PhysDefs) const {
192
+ SmallSet<unsigned ,8 > &PhysRefs) const {
195
193
MachineBasicBlock::const_iterator I = MI; I = llvm::next (I);
196
194
for (unsigned i = 0 , e = MI->getNumOperands (); i != e; ++i) {
197
195
const MachineOperand &MO = MI->getOperand (i);
@@ -208,7 +206,6 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
208
206
if (MO.isDef () &&
209
207
(MO.isDead () || isPhysDefTriviallyDead (Reg, I, MBB->end ())))
210
208
continue ;
211
- PhysDefs.push_back (Reg);
212
209
PhysRefs.insert (Reg);
213
210
for (const unsigned *Alias = TRI->getAliasSet (Reg); *Alias; ++Alias)
214
211
PhysRefs.insert (*Alias);
@@ -219,40 +216,35 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
219
216
220
217
bool MachineCSE::PhysRegDefsReach (MachineInstr *CSMI, MachineInstr *MI,
221
218
SmallSet<unsigned ,8 > &PhysRefs) const {
222
- // Look backward from MI to find CSMI.
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;
223
226
unsigned LookAheadLeft = LookAheadLimit;
224
- MachineBasicBlock::const_reverse_iterator I (MI);
225
- MachineBasicBlock::const_reverse_iterator E (MI->getParent ()->rend ());
226
227
while (LookAheadLeft) {
227
- while (LookAheadLeft && I != E) {
228
- // Skip over dbg_value's.
229
- while (I != E && I->isDebugValue ())
230
- ++I;
231
-
232
- if (&*I == CSMI)
233
- return true ;
228
+ // Skip over dbg_value's.
229
+ while (I != E && I->isDebugValue ())
230
+ ++I;
234
231
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
- }
232
+ if (I == E)
233
+ return true ;
245
234
246
- --LookAheadLeft;
247
- ++I;
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 ;
248
244
}
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 ();
245
+
246
+ --LookAheadLeft;
247
+ ++I;
256
248
}
257
249
258
250
return false ;
@@ -403,8 +395,7 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
403
395
// used, then it's not safe to replace it with a common subexpression.
404
396
// It's also not safe if the instruction uses physical registers.
405
397
SmallSet<unsigned ,8 > PhysRefs;
406
- SmallVector<unsigned ,8 > DirectPhysRefs;
407
- if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs, DirectPhysRefs)) {
398
+ if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs)) {
408
399
FoundCSE = false ;
409
400
410
401
// ... Unless the CS is local and it also defines the physical register
@@ -457,13 +448,6 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
457
448
MRI->clearKillFlags (CSEPairs[i].second );
458
449
}
459
450
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
- }
467
451
++NumCSEs;
468
452
if (!PhysRefs.empty ())
469
453
++NumPhysCSEs;
0 commit comments